BD ARP entry use common API types
[vpp.git] / src / vnet / l2 / l2_patch.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 <vnet/feature/feature.h>
20 #include <vppinfra/error.h>
21
22 typedef struct
23 {
24   /* vector of dispositions, indexed by rx_sw_if_index */
25   u32 *tx_next_by_rx_sw_if_index;
26   u32 *tx_sw_if_index_by_rx_sw_if_index;
27
28   /* convenience variables */
29   vlib_main_t *vlib_main;
30   vnet_main_t *vnet_main;
31 } l2_patch_main_t;
32
33 typedef struct
34 {
35   u32 rx_sw_if_index;
36   u32 tx_sw_if_index;
37 } l2_patch_trace_t;
38
39 /* packet trace format function */
40 static u8 *
41 format_l2_patch_trace (u8 * s, va_list * args)
42 {
43   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
44   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
45   l2_patch_trace_t *t = va_arg (*args, l2_patch_trace_t *);
46
47   s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index,
48               t->tx_sw_if_index);
49   return s;
50 }
51
52 l2_patch_main_t l2_patch_main;
53
54 static vlib_node_registration_t l2_patch_node;
55
56 #define foreach_l2_patch_error                  \
57 _(PATCHED, "L2 patch packets")                  \
58 _(DROPPED, "L2 patch misconfigured drops")
59
60 typedef enum
61 {
62 #define _(sym,str) L2_PATCH_ERROR_##sym,
63   foreach_l2_patch_error
64 #undef _
65     L2_PATCH_N_ERROR,
66 } l2_patch_error_t;
67
68 static char *l2_patch_error_strings[] = {
69 #define _(sym,string) string,
70   foreach_l2_patch_error
71 #undef _
72 };
73
74 typedef enum
75 {
76   L2_PATCH_NEXT_DROP,
77   L2_PATCH_N_NEXT,
78 } l2_patch_next_t;
79
80 static uword
81 l2_patch_node_fn (vlib_main_t * vm,
82                   vlib_node_runtime_t * node, vlib_frame_t * frame)
83 {
84   u32 n_left_from, *from, *to_next;
85   l2_patch_next_t next_index;
86   l2_patch_main_t *l2pm = &l2_patch_main;
87   vlib_node_t *n = vlib_get_node (vm, l2_patch_node.index);
88   u32 node_counter_base_index = n->error_heap_index;
89   vlib_error_main_t *em = &vm->error_main;
90
91   from = vlib_frame_vector_args (frame);
92   n_left_from = frame->n_vectors;
93   next_index = node->cached_next_index;
94
95   while (n_left_from > 0)
96     {
97       u32 n_left_to_next;
98
99       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
100
101       while (n_left_from >= 8 && n_left_to_next >= 4)
102         {
103           u32 bi0, bi1, bi2, bi3;
104           vlib_buffer_t *b0, *b1, *b2, *b3;
105           u32 next0, next1, next2, next3;
106           u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
107
108           /* Prefetch next iteration. */
109           {
110             vlib_buffer_t *p4, *p5, *p6, *p7;
111
112             p4 = vlib_get_buffer (vm, from[4]);
113             p5 = vlib_get_buffer (vm, from[5]);
114             p6 = vlib_get_buffer (vm, from[6]);
115             p7 = vlib_get_buffer (vm, from[7]);
116
117             vlib_prefetch_buffer_header (p4, LOAD);
118             vlib_prefetch_buffer_header (p5, LOAD);
119             vlib_prefetch_buffer_header (p6, LOAD);
120             vlib_prefetch_buffer_header (p7, LOAD);
121           }
122
123           /* speculatively enqueue b0 and b1 to the current next frame */
124           to_next[0] = bi0 = from[0];
125           to_next[1] = bi1 = from[1];
126           to_next[2] = bi2 = from[2];
127           to_next[3] = bi3 = from[3];
128           from += 4;
129           to_next += 4;
130           n_left_from -= 4;
131           n_left_to_next -= 4;
132
133           b0 = vlib_get_buffer (vm, bi0);
134           b1 = vlib_get_buffer (vm, bi1);
135           b2 = vlib_get_buffer (vm, bi2);
136           b3 = vlib_get_buffer (vm, bi3);
137
138           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
139           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
140           sw_if_index2 = vnet_buffer (b2)->sw_if_index[VLIB_RX];
141           sw_if_index3 = vnet_buffer (b3)->sw_if_index[VLIB_RX];
142
143           ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
144           ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
145           ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index1] != ~0);
146           ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1] != ~0);
147           ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index2] != ~0);
148           ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index2] != ~0);
149           ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index3] != ~0);
150           ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index3] != ~0);
151
152           next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
153           next1 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index1];
154           next2 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index2];
155           next3 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index3];
156
157           vnet_buffer (b0)->sw_if_index[VLIB_TX] =
158             l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
159           vnet_buffer (b1)->sw_if_index[VLIB_TX] =
160             l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1];
161           vnet_buffer (b2)->sw_if_index[VLIB_TX] =
162             l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index2];
163           vnet_buffer (b3)->sw_if_index[VLIB_TX] =
164             l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index3];
165
166           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
167             {
168               if (b0->flags & VLIB_BUFFER_IS_TRACED)
169                 {
170                   l2_patch_trace_t *t =
171                     vlib_add_trace (vm, node, b0, sizeof (*t));
172                   t->rx_sw_if_index = sw_if_index0;
173                   t->tx_sw_if_index =
174                     l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
175                 }
176               if (b1->flags & VLIB_BUFFER_IS_TRACED)
177                 {
178                   l2_patch_trace_t *t =
179                     vlib_add_trace (vm, node, b1, sizeof (*t));
180                   t->rx_sw_if_index = sw_if_index1;
181                   t->tx_sw_if_index =
182                     l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1];
183                 }
184               if (b2->flags & VLIB_BUFFER_IS_TRACED)
185                 {
186                   l2_patch_trace_t *t =
187                     vlib_add_trace (vm, node, b2, sizeof (*t));
188                   t->rx_sw_if_index = sw_if_index2;
189                   t->tx_sw_if_index =
190                     l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index2];
191                 }
192               if (b3->flags & VLIB_BUFFER_IS_TRACED)
193                 {
194                   l2_patch_trace_t *t =
195                     vlib_add_trace (vm, node, b3, sizeof (*t));
196                   t->rx_sw_if_index = sw_if_index3;
197                   t->tx_sw_if_index =
198                     l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index3];
199                 }
200             }
201
202           /* verify speculative enqueues, maybe switch current next frame */
203           vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
204                                            to_next, n_left_to_next,
205                                            bi0, bi1, bi2, bi3,
206                                            next0, next1, next2, next3);
207         }
208
209       while (n_left_from > 0 && n_left_to_next > 0)
210         {
211           u32 bi0;
212           vlib_buffer_t *b0;
213           u32 next0;
214           u32 sw_if_index0;
215
216           /* speculatively enqueue b0 to the current next frame */
217           bi0 = from[0];
218           to_next[0] = bi0;
219           from += 1;
220           to_next += 1;
221           n_left_from -= 1;
222           n_left_to_next -= 1;
223
224           b0 = vlib_get_buffer (vm, bi0);
225
226           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
227
228           ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
229           ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
230
231           next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
232           vnet_buffer (b0)->sw_if_index[VLIB_TX] =
233             l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
234
235           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
236             {
237               if (b0->flags & VLIB_BUFFER_IS_TRACED)
238                 {
239                   l2_patch_trace_t *t =
240                     vlib_add_trace (vm, node, b0, sizeof (*t));
241                   t->rx_sw_if_index = sw_if_index0;
242                   t->tx_sw_if_index =
243                     l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
244                 }
245             }
246
247           /* verify speculative enqueue, maybe switch current next frame */
248           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
249                                            to_next, n_left_to_next,
250                                            bi0, next0);
251         }
252
253       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
254     }
255
256   em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] +=
257     frame->n_vectors;
258
259   return frame->n_vectors;
260 }
261
262 /* *INDENT-OFF* */
263 VLIB_REGISTER_NODE (l2_patch_node, static) = {
264   .function = l2_patch_node_fn,
265   .name = "l2-patch",
266   .vector_size = sizeof (u32),
267   .format_trace = format_l2_patch_trace,
268   .type = VLIB_NODE_TYPE_INTERNAL,
269
270   .n_errors = ARRAY_LEN(l2_patch_error_strings),
271   .error_strings = l2_patch_error_strings,
272
273   .n_next_nodes = L2_PATCH_N_NEXT,
274
275   /* edit / add dispositions here */
276   .next_nodes = {
277         [L2_PATCH_NEXT_DROP] = "error-drop",
278   },
279 };
280 /* *INDENT-ON* */
281
282 VLIB_NODE_FUNCTION_MULTIARCH (l2_patch_node, l2_patch_node_fn)
283      int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
284                                 int is_add)
285 {
286   l2_patch_main_t *l2pm = &l2_patch_main;
287   vnet_hw_interface_t *rxhi, *txhi;
288   u32 tx_next_index;
289
290   /*
291    * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX
292    * macros...
293    */
294
295   rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index);
296
297   /* Make sure caller didn't pass a vlan subif, etc. */
298   if (rxhi->sw_if_index != rx_sw_if_index)
299     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
300
301   txhi = vnet_get_sup_hw_interface (l2pm->vnet_main, tx_sw_if_index);
302   if (txhi->sw_if_index != tx_sw_if_index)
303     return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
304
305   if (is_add)
306     {
307       tx_next_index = vlib_node_add_next (l2pm->vlib_main,
308                                           l2_patch_node.index,
309                                           txhi->output_node_index);
310
311       vec_validate_init_empty (l2pm->tx_next_by_rx_sw_if_index,
312                                rx_sw_if_index, ~0);
313
314       l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index;
315       vec_validate_init_empty (l2pm->tx_sw_if_index_by_rx_sw_if_index,
316                                rx_sw_if_index, ~0);
317       l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]
318         = txhi->sw_if_index;
319
320       ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
321                           ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
322
323       vnet_feature_enable_disable ("device-input", "l2-patch",
324                                    rxhi->hw_if_index, 1, 0, 0);
325     }
326   else
327     {
328       ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
329                           0 /* disable promiscuous mode */ );
330
331       vnet_feature_enable_disable ("device-input", "l2-patch",
332                                    rxhi->hw_if_index, 0, 0, 0);
333       if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index)
334         {
335           l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0;
336           l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0;
337         }
338     }
339
340   return 0;
341 }
342
343 static clib_error_t *
344 test_patch_command_fn (vlib_main_t * vm,
345                        unformat_input_t * input, vlib_cli_command_t * cmd)
346 {
347   l2_patch_main_t *l2pm = &l2_patch_main;
348   unformat_input_t _line_input, *line_input = &_line_input;
349   u32 rx_sw_if_index, tx_sw_if_index;
350   int rv;
351   int rx_set = 0;
352   int tx_set = 0;
353   int is_add = 1;
354   clib_error_t *error = NULL;
355
356   /* Get a line of input. */
357   if (!unformat_user (input, unformat_line_input, line_input))
358     return 0;
359
360   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
361     {
362       if (unformat (line_input, "rx %U", unformat_vnet_sw_interface,
363                     l2pm->vnet_main, &rx_sw_if_index))
364         rx_set = 1;
365       else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface,
366                          l2pm->vnet_main, &tx_sw_if_index))
367         tx_set = 1;
368       else if (unformat (line_input, "del"))
369         is_add = 0;
370       else
371         break;
372     }
373
374   if (rx_set == 0)
375     {
376       error = clib_error_return (0, "rx interface not set");
377       goto done;
378     }
379
380   if (tx_set == 0)
381     {
382       error = clib_error_return (0, "tx interface not set");
383       goto done;
384     }
385
386   rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add);
387
388   switch (rv)
389     {
390     case 0:
391       break;
392
393     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
394       error = clib_error_return (0, "rx interface not a physical port");
395       goto done;
396
397     case VNET_API_ERROR_INVALID_SW_IF_INDEX_2:
398       error = clib_error_return (0, "tx interface not a physical port");
399       goto done;
400
401     default:
402       error = clib_error_return
403         (0, "WARNING: vnet_l2_patch_add_del returned %d", rv);
404       goto done;
405     }
406
407
408 done:
409   unformat_free (line_input);
410
411   return error;
412 }
413
414 /*?
415  * Create or delete a Layer 2 patch.
416  *
417  * @cliexpar
418  * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]}
419  * @cliexend
420  * @todo This is incomplete. This needs a detailed description and a
421  * practical example.
422 ?*/
423 /* *INDENT-OFF* */
424 VLIB_CLI_COMMAND (test_patch_command, static) = {
425     .path = "test l2patch",
426     .short_help = "test l2patch rx <intfc> tx <intfc> [del]",
427     .function = test_patch_command_fn,
428 };
429 /* *INDENT-ON* */
430
431 /** Display the contents of the l2patch table. */
432 static clib_error_t *
433 show_l2patch (vlib_main_t * vm,
434               unformat_input_t * input, vlib_cli_command_t * cmd)
435 {
436   l2_patch_main_t *l2pm = &l2_patch_main;
437   u32 rx_sw_if_index;
438   u32 no_entries = 1;
439
440   ASSERT (vec_len (l2pm->tx_next_by_rx_sw_if_index) ==
441           vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index));
442
443   for (rx_sw_if_index = 0;
444        rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index);
445        rx_sw_if_index++)
446     {
447       u32 tx_sw_if_index =
448         l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index];
449       if (tx_sw_if_index != ~0)
450         {
451           no_entries = 0;
452           vlib_cli_output (vm, "%26U -> %U",
453                            format_vnet_sw_if_index_name,
454                            l2pm->vnet_main, rx_sw_if_index,
455                            format_vnet_sw_if_index_name,
456                            l2pm->vnet_main, tx_sw_if_index);
457         }
458     }
459
460   if (no_entries)
461     vlib_cli_output (vm, "no l2patch entries");
462
463   return 0;
464 }
465
466 /*?
467  * Show Layer 2 patch entries.
468  *
469  * @cliexpar
470  * @cliexstart{show l2patch}
471  * @cliexend
472  * @todo This is incomplete. This needs a detailed description and a
473  * practical example.
474 ?*/
475 /* *INDENT-OFF* */
476 VLIB_CLI_COMMAND (show_l2patch_cli, static) = {
477   .path = "show l2patch",
478   .short_help = "Show l2 interface cross-connect entries",
479   .function = show_l2patch,
480 };
481 /* *INDENT-ON* */
482
483 clib_error_t *
484 l2_patch_init (vlib_main_t * vm)
485 {
486   l2_patch_main_t *mp = &l2_patch_main;
487
488   mp->vlib_main = vm;
489   mp->vnet_main = vnet_get_main ();
490
491   return 0;
492 }
493
494 VLIB_INIT_FUNCTION (l2_patch_init);
495
496 /*
497  * fd.io coding-style-patch-verification: ON
498  *
499  * Local Variables:
500  * eval: (c-set-style "gnu")
501  * End:
502  */