blob: 7f34be561689686acabf6b133ff616ba4f0706e4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <vlib/vlib.h>
16#include <vnet/vnet.h>
17#include <vnet/pg/pg.h>
Neale Ranns571ab202018-08-22 04:27:15 -070018#include <vnet/ethernet/ethernet.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070019#include <vppinfra/error.h>
20#include <sample/sample.h>
21
Dave Barach9594b562018-07-25 16:56:38 -040022typedef struct
23{
Ed Warnickecb9cada2015-12-08 15:45:58 -070024 u32 next_index;
25 u32 sw_if_index;
Dave Barachb7e2f3d2016-11-08 16:47:34 -050026 u8 new_src_mac[6];
27 u8 new_dst_mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -070028} sample_trace_t;
29
Dave Barachb7e2f3d2016-11-08 16:47:34 -050030
Ed Warnickecb9cada2015-12-08 15:45:58 -070031/* packet trace format function */
Dave Barach9594b562018-07-25 16:56:38 -040032static u8 *
33format_sample_trace (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -070034{
35 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
36 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
Dave Barach9594b562018-07-25 16:56:38 -040037 sample_trace_t *t = va_arg (*args, sample_trace_t *);
38
Dave Barachb7e2f3d2016-11-08 16:47:34 -050039 s = format (s, "SAMPLE: sw_if_index %d, next index %d\n",
Dave Barach9594b562018-07-25 16:56:38 -040040 t->sw_if_index, t->next_index);
Dave Barachb7e2f3d2016-11-08 16:47:34 -050041 s = format (s, " new src %U -> new dst %U",
Dave Barach9594b562018-07-25 16:56:38 -040042 format_mac_address, t->new_src_mac,
43 format_mac_address, t->new_dst_mac);
Dave Barachb7e2f3d2016-11-08 16:47:34 -050044
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 return s;
46}
47
48vlib_node_registration_t sample_node;
49
50#define foreach_sample_error \
51_(SWAPPED, "Mac swap packets processed")
52
Dave Barach9594b562018-07-25 16:56:38 -040053typedef enum
54{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055#define _(sym,str) SAMPLE_ERROR_##sym,
56 foreach_sample_error
57#undef _
Dave Barach9594b562018-07-25 16:56:38 -040058 SAMPLE_N_ERROR,
Ed Warnickecb9cada2015-12-08 15:45:58 -070059} sample_error_t;
60
Dave Barach9594b562018-07-25 16:56:38 -040061static char *sample_error_strings[] = {
Ed Warnickecb9cada2015-12-08 15:45:58 -070062#define _(sym,string) string,
63 foreach_sample_error
64#undef _
65};
66
Dave Barach9594b562018-07-25 16:56:38 -040067typedef enum
68{
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 SAMPLE_NEXT_INTERFACE_OUTPUT,
70 SAMPLE_N_NEXT,
71} sample_next_t;
72
Dave Barach9594b562018-07-25 16:56:38 -040073/*
74 * Simple dual/single loop version, default version which will compile
75 * everywhere.
76 *
77 * Node costs 30 clocks/pkt at a vector size of 51
78 */
79#define VERSION_1 1
80
81#ifdef VERSION_1
Ed Warnickecb9cada2015-12-08 15:45:58 -070082#define foreach_mac_address_offset \
83_(0) \
84_(1) \
85_(2) \
86_(3) \
87_(4) \
88_(5)
89
90static uword
91sample_node_fn (vlib_main_t * vm,
Dave Barach9594b562018-07-25 16:56:38 -040092 vlib_node_runtime_t * node, vlib_frame_t * frame)
Ed Warnickecb9cada2015-12-08 15:45:58 -070093{
Dave Barach9594b562018-07-25 16:56:38 -040094 u32 n_left_from, *from, *to_next;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 sample_next_t next_index;
96 u32 pkts_swapped = 0;
97
98 from = vlib_frame_vector_args (frame);
99 n_left_from = frame->n_vectors;
100 next_index = node->cached_next_index;
101
102 while (n_left_from > 0)
103 {
104 u32 n_left_to_next;
105
Dave Barach9594b562018-07-25 16:56:38 -0400106 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107
108 while (n_left_from >= 4 && n_left_to_next >= 2)
109 {
Dave Barach9594b562018-07-25 16:56:38 -0400110 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
111 u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
112 u32 sw_if_index0, sw_if_index1;
113 u8 tmp0[6], tmp1[6];
114 ethernet_header_t *en0, *en1;
115 u32 bi0, bi1;
116 vlib_buffer_t *b0, *b1;
117
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 /* Prefetch next iteration. */
119 {
Dave Barach9594b562018-07-25 16:56:38 -0400120 vlib_buffer_t *p2, *p3;
121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 p2 = vlib_get_buffer (vm, from[2]);
123 p3 = vlib_get_buffer (vm, from[3]);
Dave Barach9594b562018-07-25 16:56:38 -0400124
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 vlib_prefetch_buffer_header (p2, LOAD);
126 vlib_prefetch_buffer_header (p3, LOAD);
127
128 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
129 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
130 }
131
Dave Barach9594b562018-07-25 16:56:38 -0400132 /* speculatively enqueue b0 and b1 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700133 to_next[0] = bi0 = from[0];
134 to_next[1] = bi1 = from[1];
135 from += 2;
136 to_next += 2;
137 n_left_from -= 2;
138 n_left_to_next -= 2;
139
140 b0 = vlib_get_buffer (vm, bi0);
141 b1 = vlib_get_buffer (vm, bi1);
142
Dave Barach9594b562018-07-25 16:56:38 -0400143 ASSERT (b0->current_data == 0);
144 ASSERT (b1->current_data == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145
Dave Barach9594b562018-07-25 16:56:38 -0400146 en0 = vlib_buffer_get_current (b0);
147 en1 = vlib_buffer_get_current (b1);
148
149 /* This is not the fastest way to swap src + dst mac addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150#define _(a) tmp0[a] = en0->src_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400151 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152#undef _
153#define _(a) en0->src_address[a] = en0->dst_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400154 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155#undef _
156#define _(a) en0->dst_address[a] = tmp0[a];
Dave Barach9594b562018-07-25 16:56:38 -0400157 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158#undef _
159
160#define _(a) tmp1[a] = en1->src_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400161 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162#undef _
163#define _(a) en1->src_address[a] = en1->dst_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400164 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165#undef _
166#define _(a) en1->dst_address[a] = tmp1[a];
Dave Barach9594b562018-07-25 16:56:38 -0400167 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168#undef _
169
Dave Barach9594b562018-07-25 16:56:38 -0400170 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
171 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172
Dave Barach9594b562018-07-25 16:56:38 -0400173 /* Send pkt back out the RX interface */
174 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
175 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Dave Barach9594b562018-07-25 16:56:38 -0400177 pkts_swapped += 2;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178
Dave Barach9594b562018-07-25 16:56:38 -0400179 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
180 {
181 if (b0->flags & VLIB_BUFFER_IS_TRACED)
182 {
183 sample_trace_t *t =
184 vlib_add_trace (vm, node, b0, sizeof (*t));
185 t->sw_if_index = sw_if_index0;
186 t->next_index = next0;
187 clib_memcpy (t->new_src_mac, en0->src_address,
188 sizeof (t->new_src_mac));
189 clib_memcpy (t->new_dst_mac, en0->dst_address,
190 sizeof (t->new_dst_mac));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191
Dave Barach9594b562018-07-25 16:56:38 -0400192 }
193 if (b1->flags & VLIB_BUFFER_IS_TRACED)
194 {
195 sample_trace_t *t =
196 vlib_add_trace (vm, node, b1, sizeof (*t));
197 t->sw_if_index = sw_if_index1;
198 t->next_index = next1;
199 clib_memcpy (t->new_src_mac, en1->src_address,
200 sizeof (t->new_src_mac));
201 clib_memcpy (t->new_dst_mac, en1->dst_address,
202 sizeof (t->new_dst_mac));
203 }
204 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205
Dave Barach9594b562018-07-25 16:56:38 -0400206 /* verify speculative enqueues, maybe switch current next frame */
207 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
208 to_next, n_left_to_next,
209 bi0, bi1, next0, next1);
210 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
212 while (n_left_from > 0 && n_left_to_next > 0)
213 {
Dave Barach9594b562018-07-25 16:56:38 -0400214 u32 bi0;
215 vlib_buffer_t *b0;
216 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
217 u32 sw_if_index0;
218 u8 tmp0[6];
219 ethernet_header_t *en0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220
Dave Barach9594b562018-07-25 16:56:38 -0400221 /* speculatively enqueue b0 to the current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 bi0 = from[0];
223 to_next[0] = bi0;
224 from += 1;
225 to_next += 1;
226 n_left_from -= 1;
227 n_left_to_next -= 1;
228
229 b0 = vlib_get_buffer (vm, bi0);
Dave Barach9594b562018-07-25 16:56:38 -0400230 /*
231 * Direct from the driver, we should be at offset 0
232 * aka at &b0->data[0]
233 */
234 ASSERT (b0->current_data == 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Dave Barach9594b562018-07-25 16:56:38 -0400236 en0 = vlib_buffer_get_current (b0);
237
238 /* This is not the fastest way to swap src + dst mac addresses */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239#define _(a) tmp0[a] = en0->src_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400240 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241#undef _
242#define _(a) en0->src_address[a] = en0->dst_address[a];
Dave Barach9594b562018-07-25 16:56:38 -0400243 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244#undef _
245#define _(a) en0->dst_address[a] = tmp0[a];
Dave Barach9594b562018-07-25 16:56:38 -0400246 foreach_mac_address_offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247#undef _
248
Dave Barach9594b562018-07-25 16:56:38 -0400249 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Dave Barach9594b562018-07-25 16:56:38 -0400251 /* Send pkt back out the RX interface */
252 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253
Dave Barach9594b562018-07-25 16:56:38 -0400254 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
255 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
256 {
257 sample_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
258 t->sw_if_index = sw_if_index0;
259 t->next_index = next0;
260 clib_memcpy (t->new_src_mac, en0->src_address,
261 sizeof (t->new_src_mac));
262 clib_memcpy (t->new_dst_mac, en0->dst_address,
263 sizeof (t->new_dst_mac));
264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barach9594b562018-07-25 16:56:38 -0400266 pkts_swapped += 1;
267
268 /* verify speculative enqueue, maybe switch current next frame */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
270 to_next, n_left_to_next,
271 bi0, next0);
272 }
273
274 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
275 }
276
Dave Barach9594b562018-07-25 16:56:38 -0400277 vlib_node_increment_counter (vm, sample_node.index,
278 SAMPLE_ERROR_SWAPPED, pkts_swapped);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 return frame->n_vectors;
280}
Dave Barach9594b562018-07-25 16:56:38 -0400281#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barach9594b562018-07-25 16:56:38 -0400283/*
284 * This version swaps mac addresses using an MMX vector shuffle
285 * Node costs about 17 clocks/pkt at a vector size of 26
286 */
287#ifdef VERSION_2
288static uword
289sample_node_fn (vlib_main_t * vm,
290 vlib_node_runtime_t * node, vlib_frame_t * frame)
291{
292 u32 n_left_from, *from, *to_next;
293 sample_next_t next_index;
294 u32 pkts_swapped = 0;
295 /* Vector shuffle mask to swap src, dst */
296 u8x16 swapmac = { 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12, 13, 14, 15 };
297
298 from = vlib_frame_vector_args (frame);
299 n_left_from = frame->n_vectors;
300 next_index = node->cached_next_index;
301
302 while (n_left_from > 0)
303 {
304 u32 n_left_to_next;
305
306 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
307 while (n_left_from >= 4 && n_left_to_next >= 2)
308 {
309 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
310 u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
311 u32 sw_if_index0, sw_if_index1;
312 u8x16 src_dst0, src_dst1;
313 ethernet_header_t *en0, *en1;
314 u32 bi0, bi1;
315 vlib_buffer_t *b0, *b1;
316
317 /* Prefetch next iteration. */
318 {
319 vlib_buffer_t *p2, *p3;
320
321 p2 = vlib_get_buffer (vm, from[2]);
322 p3 = vlib_get_buffer (vm, from[3]);
323
324 vlib_prefetch_buffer_header (p2, LOAD);
325 vlib_prefetch_buffer_header (p3, LOAD);
326
327 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
328 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
329 }
330
331 /* speculatively enqueue b0 and b1 to the current next frame */
332 to_next[0] = bi0 = from[0];
333 to_next[1] = bi1 = from[1];
334 from += 2;
335 to_next += 2;
336 n_left_from -= 2;
337 n_left_to_next -= 2;
338
339 b0 = vlib_get_buffer (vm, bi0);
340 b1 = vlib_get_buffer (vm, bi1);
341
342 ASSERT (b0->current_data == 0);
343 ASSERT (b1->current_data == 0);
344
345 en0 = vlib_buffer_get_current (b0);
346 en1 = vlib_buffer_get_current (b1);
347
348 src_dst0 = ((u8x16 *) en0)[0];
349 src_dst1 = ((u8x16 *) en1)[0];
350 src_dst0 = u8x16_shuffle (src_dst0, swapmac);
351 src_dst1 = u8x16_shuffle (src_dst1, swapmac);
352 ((u8x16 *) en0)[0] = src_dst0;
353 ((u8x16 *) en1)[0] = src_dst1;
354
355 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
356 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
357
358 /* Send pkt back out the RX interface */
359 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
360 vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1;
361
362 pkts_swapped += 2;
363
364 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
365 {
366 if (b0->flags & VLIB_BUFFER_IS_TRACED)
367 {
368 sample_trace_t *t =
369 vlib_add_trace (vm, node, b0, sizeof (*t));
370 t->sw_if_index = sw_if_index0;
371 t->next_index = next0;
372 clib_memcpy (t->new_src_mac, en0->src_address,
373 sizeof (t->new_src_mac));
374 clib_memcpy (t->new_dst_mac, en0->dst_address,
375 sizeof (t->new_dst_mac));
376
377 }
378 if (b1->flags & VLIB_BUFFER_IS_TRACED)
379 {
380 sample_trace_t *t =
381 vlib_add_trace (vm, node, b1, sizeof (*t));
382 t->sw_if_index = sw_if_index1;
383 t->next_index = next1;
384 clib_memcpy (t->new_src_mac, en1->src_address,
385 sizeof (t->new_src_mac));
386 clib_memcpy (t->new_dst_mac, en1->dst_address,
387 sizeof (t->new_dst_mac));
388 }
389 }
390
391 /* verify speculative enqueues, maybe switch current next frame */
392 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
393 to_next, n_left_to_next,
394 bi0, bi1, next0, next1);
395 }
396
397 while (n_left_from > 0 && n_left_to_next > 0)
398 {
399 u32 bi0;
400 vlib_buffer_t *b0;
401 u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
402 u32 sw_if_index0;
403 u8x16 src_dst0;
404 ethernet_header_t *en0;
405
406 /* speculatively enqueue b0 to the current next frame */
407 bi0 = from[0];
408 to_next[0] = bi0;
409 from += 1;
410 to_next += 1;
411 n_left_from -= 1;
412 n_left_to_next -= 1;
413
414 b0 = vlib_get_buffer (vm, bi0);
415 /*
416 * Direct from the driver, we should be at offset 0
417 * aka at &b0->data[0]
418 */
419 ASSERT (b0->current_data == 0);
420
421 en0 = vlib_buffer_get_current (b0);
422 src_dst0 = ((u8x16 *) en0)[0];
423 src_dst0 = u8x16_shuffle (src_dst0, swapmac);
424 ((u8x16 *) en0)[0] = src_dst0;
425
426 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
427
428 /* Send pkt back out the RX interface */
429 vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
430
431 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
432 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
433 {
434 sample_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
435 t->sw_if_index = sw_if_index0;
436 t->next_index = next0;
437 clib_memcpy (t->new_src_mac, en0->src_address,
438 sizeof (t->new_src_mac));
439 clib_memcpy (t->new_dst_mac, en0->dst_address,
440 sizeof (t->new_dst_mac));
441 }
442
443 pkts_swapped += 1;
444
445 /* verify speculative enqueue, maybe switch current next frame */
446 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
447 to_next, n_left_to_next,
448 bi0, next0);
449 }
450
451 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
452 }
453
454 vlib_node_increment_counter (vm, sample_node.index,
455 SAMPLE_ERROR_SWAPPED, pkts_swapped);
456 return frame->n_vectors;
457}
458#endif
459
460
461/*
462 * This version computes all of the buffer pointers in
463 * one motion, uses a quad/single loop model, and
464 * traces the entire frame in one motion.
465 *
466 * Node costs about 16 clocks/pkt at a vector size of 26
467 *
468 * Some compilation drama with u8x16_shuffle, so turned off by
469 * default.
470 */
471
472#ifdef VERSION_3
473
474#define u8x16_shuffle __builtin_shuffle
475/* This would normally be a stack local, but since it's a constant... */
476static const u16 nexts[VLIB_FRAME_SIZE] = { 0 };
477
478static uword
479sample_node_fn (vlib_main_t * vm,
480 vlib_node_runtime_t * node, vlib_frame_t * frame)
481{
482 u32 n_left_from, *from;
483 u32 pkts_swapped = 0;
484 /* Vector shuffle mask to swap src, dst */
485 u8x16 swapmac = { 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12, 13, 14, 15 };
486 vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
487 /* See comment below about sending all pkts to the same place... */
488 u16 *next __attribute__ ((unused));
489
490 from = vlib_frame_vector_args (frame);
491 n_left_from = frame->n_vectors;
492
493 vlib_get_buffers (vm, from, bufs, n_left_from);
494 b = bufs;
495 // next = nexts;
496
497 /*
498 * We send all pkts to SAMPLE_NEXT_INTERFACE_OUTPUT, aka
499 * graph arc 0. So the usual setting of next[0...3] is commented
500 * out below
501 */
502
503 while (n_left_from >= 4)
504 {
505 u8x16 src_dst0, src_dst1, src_dst2, src_dst3;
506 /* Prefetch next iteration. */
507 if (PREDICT_TRUE (n_left_from >= 8))
508 {
509 vlib_prefetch_buffer_header (b[4], STORE);
510 vlib_prefetch_buffer_header (b[5], STORE);
511 vlib_prefetch_buffer_header (b[6], STORE);
512 vlib_prefetch_buffer_header (b[7], STORE);
513 CLIB_PREFETCH (&b[4]->data, CLIB_CACHE_LINE_BYTES, STORE);
514 CLIB_PREFETCH (&b[5]->data, CLIB_CACHE_LINE_BYTES, STORE);
515 CLIB_PREFETCH (&b[6]->data, CLIB_CACHE_LINE_BYTES, STORE);
516 CLIB_PREFETCH (&b[7]->data, CLIB_CACHE_LINE_BYTES, STORE);
517 }
518
519 src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0];
520 src_dst1 = ((u8x16 *) vlib_buffer_get_current (b[1]))[0];
521 src_dst2 = ((u8x16 *) vlib_buffer_get_current (b[2]))[0];
522 src_dst3 = ((u8x16 *) vlib_buffer_get_current (b[3]))[0];
523
524 src_dst0 = u8x16_shuffle (src_dst0, swapmac);
525 src_dst1 = u8x16_shuffle (src_dst1, swapmac);
526 src_dst2 = u8x16_shuffle (src_dst2, swapmac);
527 src_dst3 = u8x16_shuffle (src_dst3, swapmac);
528
529 ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0;
530 ((u8x16 *) vlib_buffer_get_current (b[1]))[0] = src_dst1;
531 ((u8x16 *) vlib_buffer_get_current (b[2]))[0] = src_dst2;
532 ((u8x16 *) vlib_buffer_get_current (b[3]))[0] = src_dst3;
533
534 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
535 vnet_buffer (b[0])->sw_if_index[VLIB_RX];
536 vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
537 vnet_buffer (b[1])->sw_if_index[VLIB_RX];
538 vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
539 vnet_buffer (b[2])->sw_if_index[VLIB_RX];
540 vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
541 vnet_buffer (b[3])->sw_if_index[VLIB_RX];
542
543 // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT;
544 // next[1] = SAMPLE_NEXT_INTERFACE_OUTPUT;
545 // next[2] = SAMPLE_NEXT_INTERFACE_OUTPUT;
546 // next[3] = SAMPLE_NEXT_INTERFACE_OUTPUT;
547
548 b += 4;
549 // next += 4;
550 n_left_from -= 4;
551 pkts_swapped += 4;
552 }
553
554 while (n_left_from > 0)
555 {
556 u8x16 src_dst0;
557 src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0];
558 src_dst0 = u8x16_shuffle (src_dst0, swapmac);
559 ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0;
560 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
561 vnet_buffer (b[0])->sw_if_index[VLIB_RX];
562 // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT;
563
564 b += 1;
565 // next += 1;
566 n_left_from -= 1;
567 pkts_swapped += 1;
568
569 }
570 vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
571 frame->n_vectors);
572
573 vlib_node_increment_counter (vm, sample_node.index,
574 SAMPLE_ERROR_SWAPPED, pkts_swapped);
575
576 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
577 {
578 int i;
579 b = bufs;
580
581 for (i = 0; i < frame->n_vectors; i++)
582 {
583 if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
584 {
585 ethernet_header_t *en;
586 sample_trace_t *t =
587 vlib_add_trace (vm, node, b[0], sizeof (*t));
588 t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
589 t->next_index = SAMPLE_NEXT_INTERFACE_OUTPUT;
590 en = vlib_buffer_get_current (b[0]);
591 clib_memcpy (t->new_src_mac, en->src_address,
592 sizeof (t->new_src_mac));
593 clib_memcpy (t->new_dst_mac, en->dst_address,
594 sizeof (t->new_dst_mac));
595 b++;
596 }
597 else
598 break;
599 }
600 }
601 return frame->n_vectors;
602}
603#endif
604
605/* *INDENT-OFF* */
606VLIB_REGISTER_NODE (sample_node) =
607{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 .function = sample_node_fn,
609 .name = "sample",
610 .vector_size = sizeof (u32),
611 .format_trace = format_sample_trace,
612 .type = VLIB_NODE_TYPE_INTERNAL,
Dave Barach9594b562018-07-25 16:56:38 -0400613
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614 .n_errors = ARRAY_LEN(sample_error_strings),
615 .error_strings = sample_error_strings,
616
617 .n_next_nodes = SAMPLE_N_NEXT,
618
619 /* edit / add dispositions here */
620 .next_nodes = {
Dave Barach9594b562018-07-25 16:56:38 -0400621 [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 },
623};
Dave Barach9594b562018-07-25 16:56:38 -0400624/* *INDENT-ON* */
625
626VLIB_NODE_FUNCTION_MULTIARCH (sample_node, sample_node_fn);
627
628/*
629 * fd.io coding-style-patch-verification: ON
630 *
631 * Local Variables:
632 * eval: (c-set-style "gnu")
633 * End:
634 */