FD.io VPP  v21.06-3-gbb25fbf28
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 <vnet/mpcap.h>
22 #include <vnet/ethernet/ethernet.h>
23 
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 {
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  {
96 
97  error = mpcap_init (mm);
98  if (error)
101  }
102 
104  n_left_from = frame->n_vectors;
105 
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);
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++],
137 
138  mpcap_add_buffer (mm, vm, now, from[i++],
140 
141  mpcap_add_buffer (mm, vm, now, from[i++],
143 
144  mpcap_add_buffer (mm, vm, now, from[i++],
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++],
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 
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... */
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  */
vlib.h
MPCAP_N_NEXT
@ MPCAP_N_NEXT
Definition: mpcap_node.c:77
mpcap_main_t::flags
u32 flags
flags
Definition: mpcap.h:134
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
mpcap_node
vlib_node_registration_t mpcap_node
(constructor) VLIB_REGISTER_NODE (mpcap_node)
Definition: mpcap_node.c:51
mpcap_next_t
mpcap_next_t
Definition: mpcap_node.c:74
pg.h
ethernet_setup_node
static void ethernet_setup_node(vlib_main_t *vm, u32 node_index)
Definition: ethernet.h:408
mpcap_init
__clib_export clib_error_t * mpcap_init(mpcap_main_t *pm)
Initialize a mapped pcap file.
Definition: mpcap.c:85
mpcap_error_strings
static char * mpcap_error_strings[]
Definition: mpcap_node.c:67
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
next
u16 * next
Definition: nat44_ei_out2in.c:718
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
mpcap_trace_t::sw_if_index
u32 sw_if_index
Definition: mpcap_node.c:32
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
clib_error_report
#define clib_error_report(e)
Definition: error.h:113
vlib_frame_t
Definition: node.h:372
vlib_buffer_length_in_chain
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:433
ethernet.h
error
Definition: cJSON.c:88
mpcap_trace_t
Definition: mpcap_node.c:30
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
vlib_buffer_t::error
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:145
error.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
test_mpcap_main
static mpcap_main_t test_mpcap_main
Definition: mpcap_node.c:24
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
MPCAP_N_ERROR
@ MPCAP_N_ERROR
Definition: mpcap_node.c:63
uword
u64 uword
Definition: types.h:112
i
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:261
f64
double f64
Definition: types.h:142
mpcap_add_buffer
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:70
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
data
u8 data[128]
Definition: ipsec_types.api:92
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
foreach_mpcap_error
#define foreach_mpcap_error
Definition: mpcap_node.c:55
unformat_ethernet_header
uword unformat_ethernet_header(unformat_input_t *input, va_list *args)
Definition: format.c:290
MPCAP_NEXT_DROP
@ MPCAP_NEXT_DROP
Definition: mpcap_node.c:76
format
description fragment has unexpected format
Definition: map.api:433
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
format_ethernet_header_with_length
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
mpcap_main_t
MPCAP main state data structure.
Definition: mpcap.h:104
now
f64 now
Definition: nat44_ei_out2in.c:710
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
mpcap_inline
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
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
MPCAP_FLAG_INIT_DONE
#define MPCAP_FLAG_INIT_DONE
Definition: mpcap.h:135
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
mpcap_node_init
static clib_error_t * mpcap_node_init(vlib_main_t *vm)
Definition: mpcap_node.c:243
mpcap_trace_t::next_index
u32 next_index
Definition: mpcap_node.c:33
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
vlib_time_now
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:325
vnet.h
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
PREDICT_TRUE
#define PREDICT_TRUE(x)
Definition: clib.h:125
mpcap_error_t
mpcap_error_t
Definition: mpcap_node.c:58
mpcap.h
n_left_from
n_left_from
Definition: nat44_ei_hairpinning.c:416
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
format_mpcap_trace
static u8 * format_mpcap_trace(u8 *s, va_list *args)
Definition: mpcap_node.c:40
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
mpcap_main_t::file_name
char * file_name
File name of mpcap output.
Definition: mpcap.h:107
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105