2 * mpcap_node.c - unit-test plugin mapped pcap test node
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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,
36 #ifndef CLIB_MARCH_VARIANT
38 /* packet trace format function */
40 format_mpcap_trace (u8 * s, va_list * args)
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 *);
46 s = format (s, "MPCAP: sw_if_index %d, next index %d\n",
47 t->sw_if_index, t->next_index);
51 vlib_node_registration_t mpcap_node;
53 #endif /* CLIB_MARCH_VARIANT */
55 #define foreach_mpcap_error \
56 _(PROCESSED, "Mapped pcap packets processed")
60 #define _(sym,str) MPCAP_ERROR_##sym,
66 #ifndef CLIB_MARCH_VARIANT
67 static char *mpcap_error_strings[] = {
68 #define _(sym,string) string,
72 #endif /* CLIB_MARCH_VARIANT */
81 mpcap_inline (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * frame,
83 int is_ip4, int is_trace)
85 u32 n_left_from, *from;
86 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
87 u16 nexts[VLIB_FRAME_SIZE], *next;
88 mpcap_main_t *mm = &test_mpcap_main;
90 f64 now = vlib_time_now (vm);
91 u32 error_index = node->errors[MPCAP_ERROR_PROCESSED];
93 if (!(mm->flags & MPCAP_FLAG_INIT_DONE))
97 error = mpcap_init (mm);
99 clib_error_report (error);
100 mm->flags |= MPCAP_FLAG_INIT_DONE;
103 from = vlib_frame_vector_args (frame);
104 n_left_from = frame->n_vectors;
106 vlib_get_buffers (vm, from, bufs, n_left_from);
110 while (n_left_from >= 4)
112 /* Prefetch next iteration. */
113 if (PREDICT_TRUE (n_left_from >= 8))
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);
130 b[0]->error = error_index;
131 b[1]->error = error_index;
132 b[2]->error = error_index;
133 b[3]->error = error_index;
135 mpcap_add_buffer (mm, vm, now, from[i++],
136 vlib_buffer_length_in_chain (vm, b[0]));
138 mpcap_add_buffer (mm, vm, now, from[i++],
139 vlib_buffer_length_in_chain (vm, b[1]));
141 mpcap_add_buffer (mm, vm, now, from[i++],
142 vlib_buffer_length_in_chain (vm, b[2]));
144 mpcap_add_buffer (mm, vm, now, from[i++],
145 vlib_buffer_length_in_chain (vm, b[3]));
149 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
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];
155 if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
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];
161 if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
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];
167 if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
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];
180 while (n_left_from > 0)
182 mpcap_add_buffer (mm, vm, now, from[i++],
183 vlib_buffer_length_in_chain (vm, b[0]));
185 b[0]->error = error_index;
189 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
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];
202 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
204 return frame->n_vectors;
207 VLIB_NODE_FN (mpcap_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
208 vlib_frame_t * frame)
210 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
211 return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ ,
214 return mpcap_inline (vm, node, frame, 1 /* is_ip4 */ ,
219 #ifndef CLIB_MARCH_VARIANT
220 VLIB_REGISTER_NODE (mpcap_node) =
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,
228 .type = VLIB_NODE_TYPE_INTERNAL,
230 .n_errors = ARRAY_LEN(mpcap_error_strings),
231 .error_strings = mpcap_error_strings,
233 .n_next_nodes = MPCAP_N_NEXT,
235 /* edit / add dispositions here */
238 [MPCAP_NEXT_DROP] = "error-drop",
242 static clib_error_t *
243 mpcap_node_init (vlib_main_t *vm)
245 /* So we can send ethernet/IP packets from the pg... */
246 ethernet_setup_node (vm, mpcap_node.index);
249 VLIB_INIT_FUNCTION (mpcap_node_init);
251 #endif /* CLIB_MARCH_VARIANT */
255 * fd.io coding-style-patch-verification: ON
258 * eval: (c-set-style "gnu")