FD.io VPP  v20.01-48-g3e0dafb74
Vector Packet Processing
mpcap_node.c
Go to the documentation of this file.
1 /*
2  * mpcap_node.c - unit-test plugin mapped pcap test node
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <vppinfra/mpcap.h>
22 #include <vnet/ethernet/ethernet.h>
23 
24 static mpcap_main_t test_mpcap_main = {
25  .file_name = "/tmp/mpcap_unittest.pcap",
26  .n_packets_to_capture = 15,
27  .packet_type = MPCAP_PACKET_TYPE_ethernet,
28 };
29 
30 typedef struct
31 {
35 
36 #ifndef CLIB_MARCH_VARIANT
37 
38 /* packet trace format function */
39 static u8 *
40 format_mpcap_trace (u8 * s, va_list * args)
41 {
42  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44  mpcap_trace_t *t = va_arg (*args, mpcap_trace_t *);
45 
46  s = format (s, "MPCAP: sw_if_index %d, next index %d\n",
47  t->sw_if_index, t->next_index);
48  return s;
49 }
50 
52 
53 #endif /* CLIB_MARCH_VARIANT */
54 
55 #define foreach_mpcap_error \
56 _(PROCESSED, "Mapped pcap packets processed")
57 
58 typedef enum
59 {
60 #define _(sym,str) MPCAP_ERROR_##sym,
62 #undef _
65 
66 #ifndef CLIB_MARCH_VARIANT
67 static char *mpcap_error_strings[] = {
68 #define _(sym,string) string,
70 #undef _
71 };
72 #endif /* CLIB_MARCH_VARIANT */
73 
74 typedef enum
75 {
78 } mpcap_next_t;
79 
83  int is_ip4, int is_trace)
84 {
85  u32 n_left_from, *from;
86  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
87  u16 nexts[VLIB_FRAME_SIZE], *next;
89  int i = 0;
90  f64 now = vlib_time_now (vm);
91  u32 error_index = node->errors[MPCAP_ERROR_PROCESSED];
92 
93  if (!(mm->flags & MPCAP_FLAG_INIT_DONE))
94  {
95  clib_error_t *error;
96 
97  error = mpcap_init (mm);
98  if (error)
99  clib_error_report (error);
101  }
102 
103  from = vlib_frame_vector_args (frame);
104  n_left_from = frame->n_vectors;
105 
106  vlib_get_buffers (vm, from, bufs, n_left_from);
107  b = bufs;
108  next = nexts;
109 
110  while (n_left_from >= 4)
111  {
112  /* Prefetch next iteration. */
113  if (PREDICT_TRUE (n_left_from >= 8))
114  {
115  vlib_prefetch_buffer_header (b[4], STORE);
116  vlib_prefetch_buffer_header (b[5], STORE);
117  vlib_prefetch_buffer_header (b[6], STORE);
118  vlib_prefetch_buffer_header (b[7], STORE);
119  CLIB_PREFETCH (b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
120  CLIB_PREFETCH (b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
121  CLIB_PREFETCH (b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
122  CLIB_PREFETCH (b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
123  }
124 
125  next[0] = 0;
126  next[1] = 0;
127  next[2] = 0;
128  next[3] = 0;
129 
130  b[0]->error = error_index;
131  b[1]->error = error_index;
132  b[2]->error = error_index;
133  b[3]->error = error_index;
134 
135  mpcap_add_buffer (mm, vm, now, from[i++],
136  vlib_buffer_length_in_chain (vm, b[0]));
137 
138  mpcap_add_buffer (mm, vm, now, from[i++],
139  vlib_buffer_length_in_chain (vm, b[1]));
140 
141  mpcap_add_buffer (mm, vm, now, from[i++],
142  vlib_buffer_length_in_chain (vm, b[2]));
143 
144  mpcap_add_buffer (mm, vm, now, from[i++],
145  vlib_buffer_length_in_chain (vm, b[3]));
146 
147  if (is_trace)
148  {
149  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
150  {
151  mpcap_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
152  t->next_index = next[0];
153  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
154  }
155  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
156  {
157  mpcap_trace_t *t = vlib_add_trace (vm, node, b[1], sizeof (*t));
158  t->next_index = next[1];
159  t->sw_if_index = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
160  }
161  if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
162  {
163  mpcap_trace_t *t = vlib_add_trace (vm, node, b[2], sizeof (*t));
164  t->next_index = next[2];
165  t->sw_if_index = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
166  }
167  if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
168  {
169  mpcap_trace_t *t = vlib_add_trace (vm, node, b[3], sizeof (*t));
170  t->next_index = next[3];
171  t->sw_if_index = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
172  }
173  }
174 
175  b += 4;
176  next += 4;
177  n_left_from -= 4;
178  }
179 
180  while (n_left_from > 0)
181  {
182  mpcap_add_buffer (mm, vm, now, from[i++],
183  vlib_buffer_length_in_chain (vm, b[0]));
184  next[0] = 0;
185  b[0]->error = error_index;
186 
187  if (is_trace)
188  {
189  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
190  {
191  mpcap_trace_t *t = vlib_add_trace (vm, node, b[0], sizeof (*t));
192  t->next_index = next[0];
193  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
194  }
195  }
196 
197  b += 1;
198  next += 1;
199  n_left_from -= 1;
200  }
201 
202  vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
203 
204  return frame->n_vectors;
205 }
206 
209 {
210  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
211  return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ ,
212  1 /* is_trace */ );
213  else
214  return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ ,
215  0 /* is_trace */ );
216 }
217 
218 /* *INDENT-OFF* */
219 #ifndef CLIB_MARCH_VARIANT
221 {
222  .name = "mpcap-unittest",
223  .vector_size = sizeof (u32),
224  .format_trace = format_mpcap_trace,
225  .format_buffer = format_ethernet_header_with_length,
226  .unformat_buffer = unformat_ethernet_header,
227 
229 
230  .n_errors = ARRAY_LEN(mpcap_error_strings),
231  .error_strings = mpcap_error_strings,
232 
233  .n_next_nodes = MPCAP_N_NEXT,
234 
235  /* edit / add dispositions here */
236  .next_nodes =
237  {
238  [MPCAP_NEXT_DROP] = "error-drop",
239  },
240 };
241 
242 static clib_error_t *
244 {
245  /* So we can send ethernet/IP packets from the pg... */
246  ethernet_setup_node (vm, mpcap_node.index);
247  return 0;
248 }
250 
251 #endif /* CLIB_MARCH_VARIANT */
252 /* *INDENT-ON* */
253 
254 /*
255  * fd.io coding-style-patch-verification: ON
256  *
257  * Local Variables:
258  * eval: (c-set-style "gnu")
259  * End:
260  */
#define CLIB_UNUSED(x)
Definition: clib.h:82
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:136
#define PREDICT_TRUE(x)
Definition: clib.h:112
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:279
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
static uword mpcap_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int is_ip4, int is_trace)
Definition: mpcap_node.c:81
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:470
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:366
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
#define foreach_mpcap_error
Definition: mpcap_node.c:55
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static u8 * format_mpcap_trace(u8 *s, va_list *args)
Definition: mpcap_node.c:40
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
unsigned int u32
Definition: types.h:88
static mpcap_main_t test_mpcap_main
Definition: mpcap_node.c:24
#define VLIB_FRAME_SIZE
Definition: node.h:378
mpcap_error_t
Definition: mpcap_node.c:58
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
unsigned short u16
Definition: types.h:57
char * file_name
File name of mpcap output.
Definition: mpcap.h:108
static void mpcap_add_buffer(mpcap_main_t *pm, vlib_main_t *vm, f64 time_now, u32 buffer_index, u32 n_bytes_in_trace)
Add buffer (vlib_buffer_t) to the trace.
Definition: mpcap.h:213
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define always_inline
Definition: ipsec.h:28
MPCAP main state data structure.
Definition: mpcap.h:105
vlib_main_t * vm
Definition: in2out_ed.c:1810
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
u8 is_ip4
Definition: lisp_gpe.api:232
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
u32 flags
Definition: vhost_user.h:141
u16 n_vectors
Definition: node.h:397
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:332
#define ARRAY_LEN(x)
Definition: clib.h:62
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1810
u32 flags
flags
Definition: mpcap.h:135
mpcap_next_t
Definition: mpcap_node.c:74
MPCAP utility definitions.
u8 data[128]
Definition: ipsec_types.api:87
#define clib_error_report(e)
Definition: error.h:113
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
struct _vlib_node_registration vlib_node_registration_t
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:244
#define vnet_buffer(b)
Definition: buffer.h:408
static char * mpcap_error_strings[]
Definition: mpcap_node.c:67
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1811
static_always_inline void vlib_get_buffers(vlib_main_t *vm, u32 *bi, vlib_buffer_t **b, int count)
Translate array of buffer indices into buffer pointers.
Definition: buffer_funcs.h:244
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:302
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
clib_error_t * mpcap_init(mpcap_main_t *pm)
Initialize a mapped pcap file.
Definition: mpcap.c:85
static void ethernet_setup_node(vlib_main_t *vm, u32 node_index)
Definition: ethernet.h:387
Definition: defs.h:46
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290
vlib_node_registration_t mpcap_node
(constructor) VLIB_REGISTER_NODE (mpcap_node)
Definition: mpcap_node.c:51
static clib_error_t * mpcap_node_init(vlib_main_t *vm)
Definition: mpcap_node.c:243