vnet: export header files to build the plugins
[vpp.git] / src / examples / sample-plugin / sample / node.c
1 /*
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>
18 #include <vnet/ethernet/ethernet.h>
19 #include <vppinfra/error.h>
20 #include <sample/sample.h>
21
22 typedef struct
23 {
24   u32 next_index;
25   u32 sw_if_index;
26   u8 new_src_mac[6];
27   u8 new_dst_mac[6];
28 } sample_trace_t;
29
30
31 /* packet trace format function */
32 static u8 *
33 format_sample_trace (u8 * s, va_list * args)
34 {
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 *);
37   sample_trace_t *t = va_arg (*args, sample_trace_t *);
38
39   s = format (s, "SAMPLE: sw_if_index %d, next index %d\n",
40               t->sw_if_index, t->next_index);
41   s = format (s, "  new src %U -> new dst %U",
42               format_mac_address, t->new_src_mac,
43               format_mac_address, t->new_dst_mac);
44
45   return s;
46 }
47
48 extern vlib_node_registration_t sample_node;
49
50 #define foreach_sample_error \
51 _(SWAPPED, "Mac swap packets processed")
52
53 typedef enum
54 {
55 #define _(sym,str) SAMPLE_ERROR_##sym,
56   foreach_sample_error
57 #undef _
58     SAMPLE_N_ERROR,
59 } sample_error_t;
60
61 static char *sample_error_strings[] = {
62 #define _(sym,string) string,
63   foreach_sample_error
64 #undef _
65 };
66
67 typedef enum
68 {
69   SAMPLE_NEXT_INTERFACE_OUTPUT,
70   SAMPLE_N_NEXT,
71 } sample_next_t;
72
73 /*
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
80 #define VERSION_1 1
81 #ifdef VERSION_1
82 #define foreach_mac_address_offset              \
83 _(0)                                            \
84 _(1)                                            \
85 _(2)                                            \
86 _(3)                                            \
87 _(4)                                            \
88 _(5)
89
90 VLIB_NODE_FN (sample_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
91                             vlib_frame_t * frame)
92 {
93   u32 n_left_from, *from, *to_next;
94   sample_next_t next_index;
95   u32 pkts_swapped = 0;
96
97   from = vlib_frame_vector_args (frame);
98   n_left_from = frame->n_vectors;
99   next_index = node->cached_next_index;
100
101   while (n_left_from > 0)
102     {
103       u32 n_left_to_next;
104
105       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
106
107       while (n_left_from >= 4 && n_left_to_next >= 2)
108         {
109           u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
110           u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
111           u32 sw_if_index0, sw_if_index1;
112           u8 tmp0[6], tmp1[6];
113           ethernet_header_t *en0, *en1;
114           u32 bi0, bi1;
115           vlib_buffer_t *b0, *b1;
116
117           /* Prefetch next iteration. */
118           {
119             vlib_buffer_t *p2, *p3;
120
121             p2 = vlib_get_buffer (vm, from[2]);
122             p3 = vlib_get_buffer (vm, from[3]);
123
124             vlib_prefetch_buffer_header (p2, LOAD);
125             vlib_prefetch_buffer_header (p3, LOAD);
126
127             clib_prefetch_store (p2->data);
128             clib_prefetch_store (p3->data);
129           }
130
131           /* speculatively enqueue b0 and b1 to the current next frame */
132           to_next[0] = bi0 = from[0];
133           to_next[1] = bi1 = from[1];
134           from += 2;
135           to_next += 2;
136           n_left_from -= 2;
137           n_left_to_next -= 2;
138
139           b0 = vlib_get_buffer (vm, bi0);
140           b1 = vlib_get_buffer (vm, bi1);
141
142           ASSERT (b0->current_data == 0);
143           ASSERT (b1->current_data == 0);
144
145           en0 = vlib_buffer_get_current (b0);
146           en1 = vlib_buffer_get_current (b1);
147
148           /* This is not the fastest way to swap src + dst mac addresses */
149 #define _(a) tmp0[a] = en0->src_address[a];
150           foreach_mac_address_offset;
151 #undef _
152 #define _(a) en0->src_address[a] = en0->dst_address[a];
153           foreach_mac_address_offset;
154 #undef _
155 #define _(a) en0->dst_address[a] = tmp0[a];
156           foreach_mac_address_offset;
157 #undef _
158
159 #define _(a) tmp1[a] = en1->src_address[a];
160           foreach_mac_address_offset;
161 #undef _
162 #define _(a) en1->src_address[a] = en1->dst_address[a];
163           foreach_mac_address_offset;
164 #undef _
165 #define _(a) en1->dst_address[a] = tmp1[a];
166           foreach_mac_address_offset;
167 #undef _
168
169           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
170           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
171
172           /* Send pkt back out the RX interface */
173           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
174           vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1;
175
176           pkts_swapped += 2;
177
178           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
179             {
180               if (b0->flags & VLIB_BUFFER_IS_TRACED)
181                 {
182                   sample_trace_t *t =
183                     vlib_add_trace (vm, node, b0, sizeof (*t));
184                   t->sw_if_index = sw_if_index0;
185                   t->next_index = next0;
186                   clib_memcpy_fast (t->new_src_mac, en0->src_address,
187                                     sizeof (t->new_src_mac));
188                   clib_memcpy_fast (t->new_dst_mac, en0->dst_address,
189                                     sizeof (t->new_dst_mac));
190
191                 }
192               if (b1->flags & VLIB_BUFFER_IS_TRACED)
193                 {
194                   sample_trace_t *t =
195                     vlib_add_trace (vm, node, b1, sizeof (*t));
196                   t->sw_if_index = sw_if_index1;
197                   t->next_index = next1;
198                   clib_memcpy_fast (t->new_src_mac, en1->src_address,
199                                     sizeof (t->new_src_mac));
200                   clib_memcpy_fast (t->new_dst_mac, en1->dst_address,
201                                     sizeof (t->new_dst_mac));
202                 }
203             }
204
205           /* verify speculative enqueues, maybe switch current next frame */
206           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
207                                            to_next, n_left_to_next,
208                                            bi0, bi1, next0, next1);
209         }
210
211       while (n_left_from > 0 && n_left_to_next > 0)
212         {
213           u32 bi0;
214           vlib_buffer_t *b0;
215           u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
216           u32 sw_if_index0;
217           u8 tmp0[6];
218           ethernet_header_t *en0;
219
220           /* speculatively enqueue b0 to the current next frame */
221           bi0 = from[0];
222           to_next[0] = bi0;
223           from += 1;
224           to_next += 1;
225           n_left_from -= 1;
226           n_left_to_next -= 1;
227
228           b0 = vlib_get_buffer (vm, bi0);
229           /*
230            * Direct from the driver, we should be at offset 0
231            * aka at &b0->data[0]
232            */
233           ASSERT (b0->current_data == 0);
234
235           en0 = vlib_buffer_get_current (b0);
236
237           /* This is not the fastest way to swap src + dst mac addresses */
238 #define _(a) tmp0[a] = en0->src_address[a];
239           foreach_mac_address_offset;
240 #undef _
241 #define _(a) en0->src_address[a] = en0->dst_address[a];
242           foreach_mac_address_offset;
243 #undef _
244 #define _(a) en0->dst_address[a] = tmp0[a];
245           foreach_mac_address_offset;
246 #undef _
247
248           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
249
250           /* Send pkt back out the RX interface */
251           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
252
253           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
254                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
255             {
256               sample_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
257               t->sw_if_index = sw_if_index0;
258               t->next_index = next0;
259               clib_memcpy_fast (t->new_src_mac, en0->src_address,
260                                 sizeof (t->new_src_mac));
261               clib_memcpy_fast (t->new_dst_mac, en0->dst_address,
262                                 sizeof (t->new_dst_mac));
263             }
264
265           pkts_swapped += 1;
266
267           /* verify speculative enqueue, maybe switch current next frame */
268           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
269                                            to_next, n_left_to_next,
270                                            bi0, next0);
271         }
272
273       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
274     }
275
276   vlib_node_increment_counter (vm, sample_node.index,
277                                SAMPLE_ERROR_SWAPPED, pkts_swapped);
278   return frame->n_vectors;
279 }
280 #endif
281
282 /*
283  * This version swaps mac addresses using an MMX vector shuffle
284  * Node costs about 17 clocks/pkt at a vector size of 26
285  */
286 #ifdef VERSION_2
287 VLIB_NODE_FN (sample_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
288                             vlib_frame_t * frame)
289 {
290   u32 n_left_from, *from, *to_next;
291   sample_next_t next_index;
292   u32 pkts_swapped = 0;
293   /* Vector shuffle mask to swap src, dst */
294
295   from = vlib_frame_vector_args (frame);
296   n_left_from = frame->n_vectors;
297   next_index = node->cached_next_index;
298
299   while (n_left_from > 0)
300     {
301       u32 n_left_to_next;
302
303       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
304       while (n_left_from >= 4 && n_left_to_next >= 2)
305         {
306           u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
307           u32 next1 = SAMPLE_NEXT_INTERFACE_OUTPUT;
308           u32 sw_if_index0, sw_if_index1;
309           u8x16 src_dst0, src_dst1;
310           ethernet_header_t *en0, *en1;
311           u32 bi0, bi1;
312           vlib_buffer_t *b0, *b1;
313
314           /* Prefetch next iteration. */
315           {
316             vlib_buffer_t *p2, *p3;
317
318             p2 = vlib_get_buffer (vm, from[2]);
319             p3 = vlib_get_buffer (vm, from[3]);
320
321             vlib_prefetch_buffer_header (p2, LOAD);
322             vlib_prefetch_buffer_header (p3, LOAD);
323
324             clib_prefetch_store (p2->data);
325             clib_prefetch_store (p3->data);
326           }
327
328           /* speculatively enqueue b0 and b1 to the current next frame */
329           to_next[0] = bi0 = from[0];
330           to_next[1] = bi1 = from[1];
331           from += 2;
332           to_next += 2;
333           n_left_from -= 2;
334           n_left_to_next -= 2;
335
336           b0 = vlib_get_buffer (vm, bi0);
337           b1 = vlib_get_buffer (vm, bi1);
338
339           ASSERT (b0->current_data == 0);
340           ASSERT (b1->current_data == 0);
341
342           en0 = vlib_buffer_get_current (b0);
343           en1 = vlib_buffer_get_current (b1);
344
345           src_dst0 = ((u8x16 *) en0)[0];
346           src_dst1 = ((u8x16 *) en1)[0];
347           src_dst0 = u8x16_shuffle (src_dst0, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3,
348                                     4, 5, 12, 13, 14, 15);
349           src_dst1 = u8x16_shuffle (src_dst1, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3,
350                                     4, 5, 12, 13, 14, 15);
351           ((u8x16 *) en0)[0] = src_dst0;
352           ((u8x16 *) en1)[0] = src_dst1;
353
354           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
355           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
356
357           /* Send pkt back out the RX interface */
358           vnet_buffer (b0)->sw_if_index[VLIB_TX] = sw_if_index0;
359           vnet_buffer (b1)->sw_if_index[VLIB_TX] = sw_if_index1;
360
361           pkts_swapped += 2;
362
363           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
364             {
365               if (b0->flags & VLIB_BUFFER_IS_TRACED)
366                 {
367                   sample_trace_t *t =
368                     vlib_add_trace (vm, node, b0, sizeof (*t));
369                   t->sw_if_index = sw_if_index0;
370                   t->next_index = next0;
371                   clib_memcpy_fast (t->new_src_mac, en0->src_address,
372                                     sizeof (t->new_src_mac));
373                   clib_memcpy_fast (t->new_dst_mac, en0->dst_address,
374                                     sizeof (t->new_dst_mac));
375
376                 }
377               if (b1->flags & VLIB_BUFFER_IS_TRACED)
378                 {
379                   sample_trace_t *t =
380                     vlib_add_trace (vm, node, b1, sizeof (*t));
381                   t->sw_if_index = sw_if_index1;
382                   t->next_index = next1;
383                   clib_memcpy_fast (t->new_src_mac, en1->src_address,
384                                     sizeof (t->new_src_mac));
385                   clib_memcpy_fast (t->new_dst_mac, en1->dst_address,
386                                     sizeof (t->new_dst_mac));
387                 }
388             }
389
390           /* verify speculative enqueues, maybe switch current next frame */
391           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
392                                            to_next, n_left_to_next,
393                                            bi0, bi1, next0, next1);
394         }
395
396       while (n_left_from > 0 && n_left_to_next > 0)
397         {
398           u32 bi0;
399           vlib_buffer_t *b0;
400           u32 next0 = SAMPLE_NEXT_INTERFACE_OUTPUT;
401           u32 sw_if_index0;
402           u8x16 src_dst0;
403           ethernet_header_t *en0;
404
405           /* speculatively enqueue b0 to the current next frame */
406           bi0 = from[0];
407           to_next[0] = bi0;
408           from += 1;
409           to_next += 1;
410           n_left_from -= 1;
411           n_left_to_next -= 1;
412
413           b0 = vlib_get_buffer (vm, bi0);
414           /*
415            * Direct from the driver, we should be at offset 0
416            * aka at &b0->data[0]
417            */
418           ASSERT (b0->current_data == 0);
419
420           en0 = vlib_buffer_get_current (b0);
421           src_dst0 = ((u8x16 *) en0)[0];
422           src_dst0 = u8x16_shuffle (src_dst0, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3,
423                                     4, 5, 12, 13, 14, 15);
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_fast (t->new_src_mac, en0->src_address,
438                                 sizeof (t->new_src_mac));
439               clib_memcpy_fast (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 /* This would normally be a stack local, but since it's a constant... */
475 static const u16 nexts[VLIB_FRAME_SIZE] = { 0 };
476
477 VLIB_NODE_FN (sample_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
478                             vlib_frame_t * frame)
479 {
480   u32 n_left_from, *from;
481   u32 pkts_swapped = 0;
482   /* Vector shuffle mask to swap src, dst */
483   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b;
484   /* See comment below about sending all pkts to the same place... */
485   u16 *next __attribute__ ((unused));
486
487   from = vlib_frame_vector_args (frame);
488   n_left_from = frame->n_vectors;
489
490   vlib_get_buffers (vm, from, bufs, n_left_from);
491   b = bufs;
492   // next = nexts;
493
494   /*
495    * We send all pkts to SAMPLE_NEXT_INTERFACE_OUTPUT, aka
496    * graph arc 0. So the usual setting of next[0...3] is commented
497    * out below
498    */
499
500   while (n_left_from >= 4)
501     {
502       u8x16 src_dst0, src_dst1, src_dst2, src_dst3;
503       /* Prefetch next iteration. */
504       if (PREDICT_TRUE (n_left_from >= 8))
505         {
506           vlib_prefetch_buffer_header (b[4], STORE);
507           vlib_prefetch_buffer_header (b[5], STORE);
508           vlib_prefetch_buffer_header (b[6], STORE);
509           vlib_prefetch_buffer_header (b[7], STORE);
510           clib_prefetch_store (&b[4]->data);
511           clib_prefetch_store (&b[5]->data);
512           clib_prefetch_store (&b[6]->data);
513           clib_prefetch_store (&b[7]->data);
514         }
515
516       src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0];
517       src_dst1 = ((u8x16 *) vlib_buffer_get_current (b[1]))[0];
518       src_dst2 = ((u8x16 *) vlib_buffer_get_current (b[2]))[0];
519       src_dst3 = ((u8x16 *) vlib_buffer_get_current (b[3]))[0];
520
521       src_dst0 = u8x16_shuffle (src_dst0, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5,
522                                 12, 13, 14, 15);
523       src_dst1 = u8x16_shuffle (src_dst1, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5,
524                                 12, 13, 14, 15);
525       src_dst2 = u8x16_shuffle (src_dst2, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5,
526                                 12, 13, 14, 15);
527       src_dst3 = u8x16_shuffle (src_dst3, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5,
528                                 12, 13, 14, 15);
529
530       ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0;
531       ((u8x16 *) vlib_buffer_get_current (b[1]))[0] = src_dst1;
532       ((u8x16 *) vlib_buffer_get_current (b[2]))[0] = src_dst2;
533       ((u8x16 *) vlib_buffer_get_current (b[3]))[0] = src_dst3;
534
535       vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
536         vnet_buffer (b[0])->sw_if_index[VLIB_RX];
537       vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
538         vnet_buffer (b[1])->sw_if_index[VLIB_RX];
539       vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
540         vnet_buffer (b[2])->sw_if_index[VLIB_RX];
541       vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
542         vnet_buffer (b[3])->sw_if_index[VLIB_RX];
543
544       // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT;
545       // next[1] = SAMPLE_NEXT_INTERFACE_OUTPUT;
546       // next[2] = SAMPLE_NEXT_INTERFACE_OUTPUT;
547       // next[3] = SAMPLE_NEXT_INTERFACE_OUTPUT;
548
549       b += 4;
550       // next += 4;
551       n_left_from -= 4;
552       pkts_swapped += 4;
553     }
554
555   while (n_left_from > 0)
556     {
557       u8x16 src_dst0;
558       src_dst0 = ((u8x16 *) vlib_buffer_get_current (b[0]))[0];
559       src_dst0 = u8x16_shuffle (src_dst0, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5,
560                                 12, 13, 14, 15);
561       ((u8x16 *) vlib_buffer_get_current (b[0]))[0] = src_dst0;
562       vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
563         vnet_buffer (b[0])->sw_if_index[VLIB_RX];
564       // next[0] = SAMPLE_NEXT_INTERFACE_OUTPUT;
565
566       b += 1;
567       // next += 1;
568       n_left_from -= 1;
569       pkts_swapped += 1;
570
571     }
572   vlib_buffer_enqueue_to_next (vm, node, from, (u16 *) nexts,
573                                frame->n_vectors);
574
575   vlib_node_increment_counter (vm, sample_node.index,
576                                SAMPLE_ERROR_SWAPPED, pkts_swapped);
577
578   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
579     {
580       int i;
581       b = bufs;
582
583       for (i = 0; i < frame->n_vectors; i++)
584         {
585           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
586             {
587               ethernet_header_t *en;
588               sample_trace_t *t =
589                 vlib_add_trace (vm, node, b[0], sizeof (*t));
590               t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
591               t->next_index = SAMPLE_NEXT_INTERFACE_OUTPUT;
592               en = vlib_buffer_get_current (b[0]);
593               clib_memcpy_fast (t->new_src_mac, en->src_address,
594                                 sizeof (t->new_src_mac));
595               clib_memcpy_fast (t->new_dst_mac, en->dst_address,
596                                 sizeof (t->new_dst_mac));
597               b++;
598             }
599           else
600             break;
601         }
602     }
603   return frame->n_vectors;
604 }
605 #endif
606
607 /*
608  * This version computes all of the buffer pointers in
609  * one motion, uses a fully pipelined loop model, and
610  * traces the entire frame in one motion.
611  *
612  * It's performance-competative with other coding paradigms,
613  * and it's the simplest way to write performant vpp code
614  */
615
616
617 #ifdef VERSION_4
618
619 /* Final stage in the pipeline, do the mac swap */
620 static inline u32
621 last_stage (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
622 {
623   u8x16 src_dst0;
624   src_dst0 = ((u8x16 *) vlib_buffer_get_current (b))[0];
625   src_dst0 = u8x16_shuffle (src_dst0, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4, 5, 12,
626                             13, 14, 15);
627   ((u8x16 *) vlib_buffer_get_current (b))[0] = src_dst0;
628   vnet_buffer (b)->sw_if_index[VLIB_TX] =
629     vnet_buffer (b)->sw_if_index[VLIB_RX];
630   /* set next-index[] to 0 for this buffer */
631   return 0;
632 }
633
634 /*
635  * Add a couple of nil stages to increase the prefetch stride.
636  * For any specific platform, the optimal prefetch stride may differ.
637  */
638 static inline void
639 stage1 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
640 {
641 }
642
643 static inline void
644 stage2 (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_buffer_t * b)
645 {
646 }
647
648 #define NSTAGES 4
649 #define STAGE_INLINE inline __attribute__((__always_inline__))
650
651 #define stage0 generic_stage0
652
653 #include <vnet/pipeline.h>
654
655 VLIB_NODE_FN (sample_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
656                             vlib_frame_t * frame)
657 {
658   dispatch_pipeline (vm, node, frame);
659
660   vlib_node_increment_counter (vm, sample_node.index,
661                                SAMPLE_ERROR_SWAPPED, frame->n_vectors);
662   if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
663     {
664       int i;
665       b = bufs;
666
667       for (i = 0; i < frame->n_vectors; i++)
668         {
669           if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
670             {
671               ethernet_header_t *en;
672               sample_trace_t *t =
673                 vlib_add_trace (vm, node, b[0], sizeof (*t));
674               t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_TX];
675               t->next_index = SAMPLE_NEXT_INTERFACE_OUTPUT;
676               en = vlib_buffer_get_current (b[0]);
677               clib_memcpy_fast (t->new_src_mac, en->src_address,
678                                 sizeof (t->new_src_mac));
679               clib_memcpy_fast (t->new_dst_mac, en->dst_address,
680                                 sizeof (t->new_dst_mac));
681               b++;
682             }
683           else
684             break;
685         }
686     }
687   return frame->n_vectors;
688 }
689 #endif
690
691 VLIB_REGISTER_NODE (sample_node) =
692 {
693   .name = "sample",
694   .vector_size = sizeof (u32),
695   .format_trace = format_sample_trace,
696   .type = VLIB_NODE_TYPE_INTERNAL,
697
698   .n_errors = ARRAY_LEN(sample_error_strings),
699   .error_strings = sample_error_strings,
700
701   .n_next_nodes = SAMPLE_N_NEXT,
702
703   /* edit / add dispositions here */
704   .next_nodes = {
705     [SAMPLE_NEXT_INTERFACE_OUTPUT] = "interface-output",
706   },
707 };
708
709 /*
710  * fd.io coding-style-patch-verification: ON
711  *
712  * Local Variables:
713  * eval: (c-set-style "gnu")
714  * End:
715  */