l2: migrate old MULTIARCH macros to VLIB_NODE_FN
[vpp.git] / src / vnet / l2 / l2_xcrw.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 <vnet/l2/l2_xcrw.h>
16
17 /**
18  * @file
19  * General L2 / L3 cross-connect, used to set up
20  * "L2 interface <--> your-favorite-tunnel-encap" tunnels.
21  *
22  * We set up a typical L2 cross-connect or (future) bridge
23  * to hook L2 interface(s) up to the L3 stack in arbitrary ways.
24  *
25  * Each l2_xcrw adjacency specifies 3 things:
26  *
27  * 1. The next graph node (presumably in the L3 stack) to
28  *    process the (L2 -> L3) packet
29  *
30  * 2. A new value for vnet_buffer(b)->sw_if_index[VLIB_TX]
31  *    (i.e. a lookup FIB index),
32  *
33  * 3. A rewrite string to apply.
34  *
35  * Example: to cross-connect an L2 interface or (future) bridge
36  * to an mpls-o-gre tunnel, set up the L2 rewrite string as shown in
37  * mpls_gre_rewrite, and use "mpls-post-rewrite" to fix the
38  * GRE IP header checksum and length fields.
39  */
40
41 typedef struct
42 {
43   u32 next_index;
44   u32 tx_fib_index;
45 } l2_xcrw_trace_t;
46
47 /* packet trace format function */
48 static u8 *
49 format_l2_xcrw_trace (u8 * s, va_list * args)
50 {
51   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53   l2_xcrw_trace_t *t = va_arg (*args, l2_xcrw_trace_t *);
54
55   s = format (s, "L2_XCRW: next index %d tx_fib_index %d",
56               t->next_index, t->tx_fib_index);
57   return s;
58 }
59
60 extern l2_xcrw_main_t l2_xcrw_main;
61
62 #ifndef CLIB_MARCH_VARIANT
63 l2_xcrw_main_t l2_xcrw_main;
64 #endif /* CLIB_MARCH_VARIANT */
65
66 static char *l2_xcrw_error_strings[] = {
67 #define _(sym,string) string,
68   foreach_l2_xcrw_error
69 #undef _
70 };
71
72 VLIB_NODE_FN (l2_xcrw_node) (vlib_main_t * vm,
73                              vlib_node_runtime_t * node, vlib_frame_t * frame)
74 {
75   u32 n_left_from, *from, *to_next;
76   l2_xcrw_next_t next_index;
77   l2_xcrw_main_t *xcm = &l2_xcrw_main;
78   vlib_node_t *n = vlib_get_node (vm, l2_xcrw_node.index);
79   u32 node_counter_base_index = n->error_heap_index;
80   vlib_error_main_t *em = &vm->error_main;
81
82   from = vlib_frame_vector_args (frame);
83   n_left_from = frame->n_vectors;
84   next_index = node->cached_next_index;
85
86   while (n_left_from > 0)
87     {
88       u32 n_left_to_next;
89
90       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
91
92       while (n_left_from >= 4 && n_left_to_next >= 2)
93         {
94           u32 bi0, bi1;
95           vlib_buffer_t *b0, *b1;
96           u32 next0, next1;
97           u32 sw_if_index0, sw_if_index1;
98           l2_xcrw_adjacency_t *adj0, *adj1;
99
100           /* Prefetch next iteration. */
101           {
102             vlib_buffer_t *p2, *p3;
103
104             p2 = vlib_get_buffer (vm, from[2]);
105             p3 = vlib_get_buffer (vm, from[3]);
106
107             vlib_prefetch_buffer_header (p2, LOAD);
108             vlib_prefetch_buffer_header (p3, LOAD);
109
110             CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
111             CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
112           }
113
114           /* speculatively enqueue b0 and b1 to the current next frame */
115           to_next[0] = bi0 = from[0];
116           to_next[1] = bi1 = from[1];
117           from += 2;
118           to_next += 2;
119           n_left_from -= 2;
120           n_left_to_next -= 2;
121
122           b0 = vlib_get_buffer (vm, bi0);
123           b1 = vlib_get_buffer (vm, bi1);
124
125           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
126           sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
127
128           adj0 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index0);
129           adj1 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index1);
130
131           next0 = adj0->rewrite_header.next_index;
132           vnet_buffer (b0)->sw_if_index[VLIB_TX] =
133             adj0->rewrite_header.sw_if_index;
134
135           next1 = adj1->rewrite_header.next_index;
136           vnet_buffer (b1)->sw_if_index[VLIB_TX] =
137             adj1->rewrite_header.sw_if_index;
138
139           em->counters[node_counter_base_index + next1]++;
140
141           if (PREDICT_TRUE (next0 > 0))
142             {
143               u8 *h0 = vlib_buffer_get_current (b0);
144               vnet_rewrite_one_header (adj0[0], h0,
145                                        adj0->rewrite_header.data_bytes);
146               vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
147               em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
148             }
149
150           if (PREDICT_TRUE (next1 > 0))
151             {
152               u8 *h1 = vlib_buffer_get_current (b1);
153               vnet_rewrite_one_header (adj1[0], h1,
154                                        adj1->rewrite_header.data_bytes);
155               vlib_buffer_advance (b1, -adj1->rewrite_header.data_bytes);
156               em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
157             }
158
159
160           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
161             {
162               if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
163                                  && (b0->flags & VLIB_BUFFER_IS_TRACED)))
164                 {
165                   l2_xcrw_trace_t *t =
166                     vlib_add_trace (vm, node, b0, sizeof (*t));
167                   t->next_index = next0;
168                   t->tx_fib_index = adj0->rewrite_header.sw_if_index;
169                 }
170               if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
171                                  && (b1->flags & VLIB_BUFFER_IS_TRACED)))
172                 {
173                   l2_xcrw_trace_t *t =
174                     vlib_add_trace (vm, node, b1, sizeof (*t));
175                   t->next_index = next1;
176                   t->tx_fib_index = adj1->rewrite_header.sw_if_index;
177                 }
178             }
179
180           /* verify speculative enqueues, maybe switch current next frame */
181           vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
182                                            to_next, n_left_to_next,
183                                            bi0, bi1, next0, next1);
184         }
185
186       while (n_left_from > 0 && n_left_to_next > 0)
187         {
188           u32 bi0;
189           vlib_buffer_t *b0;
190           u32 next0;
191           u32 sw_if_index0;
192           l2_xcrw_adjacency_t *adj0;
193
194           /* speculatively enqueue b0 to the current next frame */
195           bi0 = from[0];
196           to_next[0] = bi0;
197           from += 1;
198           to_next += 1;
199           n_left_from -= 1;
200           n_left_to_next -= 1;
201
202           b0 = vlib_get_buffer (vm, bi0);
203
204           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
205
206           adj0 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index0);
207
208           next0 = adj0->rewrite_header.next_index;
209           vnet_buffer (b0)->sw_if_index[VLIB_TX] =
210             adj0->rewrite_header.sw_if_index;
211
212           if (PREDICT_TRUE (next0 > 0))
213             {
214               u8 *h0 = vlib_buffer_get_current (b0);
215               vnet_rewrite_one_header (adj0[0], h0,
216                                        adj0->rewrite_header.data_bytes);
217               vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
218               em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
219             }
220
221           if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
222                              && (b0->flags & VLIB_BUFFER_IS_TRACED)))
223             {
224               l2_xcrw_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
225               t->next_index = next0;
226               t->tx_fib_index = adj0->rewrite_header.sw_if_index;
227             }
228
229           /* verify speculative enqueue, maybe switch current next frame */
230           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
231                                            to_next, n_left_to_next,
232                                            bi0, next0);
233         }
234
235       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
236     }
237
238   return frame->n_vectors;
239 }
240
241 /* *INDENT-OFF* */
242 VLIB_REGISTER_NODE (l2_xcrw_node) = {
243   .name = "l2-xcrw",
244   .vector_size = sizeof (u32),
245   .format_trace = format_l2_xcrw_trace,
246   .type = VLIB_NODE_TYPE_INTERNAL,
247
248   .n_errors = ARRAY_LEN(l2_xcrw_error_strings),
249   .error_strings = l2_xcrw_error_strings,
250
251   .n_next_nodes = L2_XCRW_N_NEXT,
252
253   /* edit / add dispositions here */
254   .next_nodes = {
255         [L2_XCRW_NEXT_DROP] = "error-drop",
256   },
257 };
258 /* *INDENT-ON* */
259
260 #ifndef CLIB_MARCH_VARIANT
261 clib_error_t *
262 l2_xcrw_init (vlib_main_t * vm)
263 {
264   l2_xcrw_main_t *mp = &l2_xcrw_main;
265
266   mp->vlib_main = vm;
267   mp->vnet_main = &vnet_main;
268   mp->tunnel_index_by_l2_sw_if_index = hash_create (0, sizeof (uword));
269
270   return 0;
271 }
272
273 VLIB_INIT_FUNCTION (l2_xcrw_init);
274
275 static uword
276 dummy_interface_tx (vlib_main_t * vm,
277                     vlib_node_runtime_t * node, vlib_frame_t * frame)
278 {
279   clib_warning ("you shouldn't be here, leaking buffers...");
280   return frame->n_vectors;
281 }
282
283 static u8 *
284 format_xcrw_name (u8 * s, va_list * args)
285 {
286   u32 dev_instance = va_arg (*args, u32);
287   return format (s, "xcrw%d", dev_instance);
288 }
289
290 /* *INDENT-OFF* */
291 VNET_DEVICE_CLASS (xcrw_device_class,static) = {
292   .name = "Xcrw",
293   .format_device_name = format_xcrw_name,
294   .tx_function = dummy_interface_tx,
295 };
296 /* *INDENT-ON* */
297
298 /* Create a sham tunnel interface and return its sw_if_index */
299 static u32
300 create_xcrw_interface (vlib_main_t * vm)
301 {
302   vnet_main_t *vnm = vnet_get_main ();
303   static u32 instance;
304   u8 address[6];
305   u32 hw_if_index;
306   vnet_hw_interface_t *hi;
307   u32 sw_if_index;
308
309   /* mac address doesn't really matter */
310   clib_memset (address, 0, sizeof (address));
311   address[2] = 0x12;
312
313   /* can returns error iff phy != 0 */
314   (void) ethernet_register_interface
315     (vnm, xcrw_device_class.index, instance++, address, &hw_if_index,
316      /* flag change */ 0);
317
318   hi = vnet_get_hw_interface (vnm, hw_if_index);
319   sw_if_index = hi->sw_if_index;
320   vnet_sw_interface_set_flags (vnm, sw_if_index,
321                                VNET_SW_INTERFACE_FLAG_ADMIN_UP);
322
323   /* Output to the sham tunnel invokes the encap node */
324   hi->output_node_index = l2_xcrw_node.index;
325
326   return sw_if_index;
327 }
328
329 int
330 vnet_configure_l2_xcrw (vlib_main_t * vm, vnet_main_t * vnm,
331                         u32 l2_sw_if_index, u32 tx_fib_index,
332                         u8 * rewrite, u32 next_node_index, int is_add)
333 {
334   l2_xcrw_main_t *xcm = &l2_xcrw_main;
335   l2_xcrw_adjacency_t *a;
336   l2_xcrw_tunnel_t *t;
337   uword *p;
338
339   if (is_add)
340     {
341
342       pool_get (xcm->tunnels, t);
343
344       /* No interface allocated? Do it. Otherwise, set admin up */
345       if (t->tunnel_sw_if_index == 0)
346         t->tunnel_sw_if_index = create_xcrw_interface (vm);
347       else
348         vnet_sw_interface_set_flags (vnm, t->tunnel_sw_if_index,
349                                      VNET_SW_INTERFACE_FLAG_ADMIN_UP);
350
351       t->l2_sw_if_index = l2_sw_if_index;
352
353       vec_validate (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
354
355       a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
356       clib_memset (a, 0, sizeof (*a));
357
358       a->rewrite_header.sw_if_index = tx_fib_index;
359
360       /*
361        * Add or find a dynamic disposition for the successor node,
362        * e.g. so we can ship pkts to mpls_post_rewrite...
363        */
364       a->rewrite_header.next_index =
365         vlib_node_add_next (vm, l2_xcrw_node.index, next_node_index);
366
367       if (vec_len (rewrite))
368         vnet_rewrite_set_data (a[0], rewrite, vec_len (rewrite));
369
370       set_int_l2_mode (vm, vnm, MODE_L2_XC, t->l2_sw_if_index, 0,
371                        L2_BD_PORT_TYPE_NORMAL, 0, t->tunnel_sw_if_index);
372       hash_set (xcm->tunnel_index_by_l2_sw_if_index,
373                 t->l2_sw_if_index, t - xcm->tunnels);
374       return 0;
375     }
376   else
377     {
378       p = hash_get (xcm->tunnel_index_by_l2_sw_if_index, l2_sw_if_index);
379       if (p == 0)
380         return VNET_API_ERROR_INVALID_SW_IF_INDEX;
381
382       t = pool_elt_at_index (xcm->tunnels, p[0]);
383
384       a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
385       /* Reset adj to drop traffic */
386       clib_memset (a, 0, sizeof (*a));
387
388       set_int_l2_mode (vm, vnm, MODE_L3, t->l2_sw_if_index, 0,
389                        L2_BD_PORT_TYPE_NORMAL, 0, 0);
390
391       vnet_sw_interface_set_flags (vnm, t->tunnel_sw_if_index, 0 /* down */ );
392
393       hash_unset (xcm->tunnel_index_by_l2_sw_if_index, l2_sw_if_index);
394       pool_put (xcm->tunnels, t);
395     }
396   return 0;
397 }
398
399
400 static clib_error_t *
401 set_l2_xcrw_command_fn (vlib_main_t * vm,
402                         unformat_input_t * input, vlib_cli_command_t * cmd)
403 {
404   unformat_input_t _line_input, *line_input = &_line_input;
405   int is_add = 1;
406   int is_ipv6 = 0;              /* for fib id -> fib index mapping */
407   u32 tx_fib_id = ~0;
408   u32 tx_fib_index = ~0;
409   u32 next_node_index = ~0;
410   u32 l2_sw_if_index;
411   u8 *rw = 0;
412   vnet_main_t *vnm = vnet_get_main ();
413   int rv;
414   clib_error_t *error = NULL;
415
416
417   if (!unformat_user (input, unformat_line_input, line_input))
418     return 0;
419
420   if (!unformat (line_input, "%U",
421                  unformat_vnet_sw_interface, vnm, &l2_sw_if_index))
422     {
423       error = clib_error_return (0, "unknown input '%U'",
424                                  format_unformat_error, line_input);
425       goto done;
426     }
427
428   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
429     {
430       if (unformat (line_input, "next %U",
431                     unformat_vlib_node, vm, &next_node_index))
432         ;
433       else if (unformat (line_input, "tx-fib-id %d", &tx_fib_id))
434         ;
435       else if (unformat (line_input, "del"))
436         is_add = 0;
437       else if (unformat (line_input, "ipv6"))
438         is_ipv6 = 1;
439       else if (unformat (line_input, "rw %U", unformat_hex_string, &rw));
440       else
441         break;
442     }
443
444   if (next_node_index == ~0)
445     {
446       error = clib_error_return (0, "next node not specified");
447       goto done;
448     }
449
450   if (tx_fib_id != ~0)
451     {
452       uword *p;
453
454       if (is_ipv6)
455         p = hash_get (ip6_main.fib_index_by_table_id, tx_fib_id);
456       else
457         p = hash_get (ip4_main.fib_index_by_table_id, tx_fib_id);
458
459       if (p == 0)
460         {
461           error =
462             clib_error_return (0, "nonexistent tx_fib_id %d", tx_fib_id);
463           goto done;
464         }
465
466       tx_fib_index = p[0];
467     }
468
469   rv = vnet_configure_l2_xcrw (vm, vnm, l2_sw_if_index, tx_fib_index,
470                                rw, next_node_index, is_add);
471
472   switch (rv)
473     {
474
475     case 0:
476       break;
477
478     case VNET_API_ERROR_INVALID_SW_IF_INDEX:
479       error = clib_error_return (0, "%U not cross-connected",
480                                  format_vnet_sw_if_index_name,
481                                  vnm, l2_sw_if_index);
482       goto done;
483
484     default:
485       error = clib_error_return (0, "vnet_configure_l2_xcrw returned %d", rv);
486       goto done;
487     }
488
489 done:
490   vec_free (rw);
491   unformat_free (line_input);
492
493   return error;
494 }
495
496 /*?
497  * Add or delete a Layer 2 to Layer 3 rewrite cross-connect. This is
498  * used to hook Layer 2 interface(s) up to the Layer 3 stack in
499  * arbitrary ways. For example, cross-connect an L2 interface or
500  * (future) bridge to an mpls-o-gre tunnel. Set up the L2 rewrite
501  * string as shown in mpls_gre_rewrite, and use \"mpls-post-rewrite\"
502  * to fix the GRE IP header checksum and length fields.
503  *
504  * @cliexpar
505  * @todo This is incomplete. This needs a detailed description and a
506  * practical example.
507 ?*/
508 /* *INDENT-OFF* */
509 VLIB_CLI_COMMAND (set_l2_xcrw_command, static) = {
510   .path = "set interface l2 xcrw",
511   .short_help =
512   "set interface l2 xcrw <interface> next <node-name>\n"
513   "    [del] [tx-fib-id <id>] [ipv6] rw <hex-bytes>",
514   .function = set_l2_xcrw_command_fn,
515 };
516 /* *INDENT-ON* */
517
518 #endif /* CLIB_MARCH_VARIANT */
519
520 static u8 *
521 format_l2xcrw (u8 * s, va_list * args)
522 {
523   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
524   l2_xcrw_tunnel_t *t = va_arg (*args, l2_xcrw_tunnel_t *);
525   l2_xcrw_main_t *xcm = &l2_xcrw_main;
526   vlib_main_t *vm = vlib_get_main ();
527   l2_xcrw_adjacency_t *a;
528   u8 *rewrite_string;
529
530   if (t == 0)
531     {
532       s = format (s, "%-25s%s", "L2 interface", "Tunnel Details");
533       return s;
534     }
535
536   s = format (s, "%-25U %U ",
537               format_vnet_sw_if_index_name, vnm, t->l2_sw_if_index,
538               format_vnet_sw_if_index_name, vnm, t->tunnel_sw_if_index);
539
540   a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
541
542   s = format (s, "next %U ",
543               format_vlib_next_node_name, vm, l2_xcrw_node.index,
544               a->rewrite_header.next_index);
545
546   if (a->rewrite_header.sw_if_index != ~0)
547     s = format (s, "tx fib index %d ", a->rewrite_header.sw_if_index);
548
549   if (a->rewrite_header.data_bytes)
550     {
551       rewrite_string = (u8 *) (a + 1);
552       rewrite_string -= a->rewrite_header.data_bytes;
553       s = format (s, "rewrite data: %U ",
554                   format_hex_bytes, rewrite_string,
555                   a->rewrite_header.data_bytes);
556     }
557
558   s = format (s, "\n");
559
560   return s;
561 }
562
563
564 static clib_error_t *
565 show_l2xcrw_command_fn (vlib_main_t * vm,
566                         unformat_input_t * input, vlib_cli_command_t * cmd)
567 {
568   vnet_main_t *vnm = vnet_get_main ();
569   l2_xcrw_main_t *xcm = &l2_xcrw_main;
570   l2_xcrw_tunnel_t *t;
571
572   if (pool_elts (xcm->tunnels) == 0)
573     {
574       vlib_cli_output (vm, "No L2 / L3 rewrite cross-connects configured");
575       return 0;
576     }
577
578   vlib_cli_output (vm, "%U", format_l2xcrw, 0, 0);
579
580   /* *INDENT-OFF* */
581   pool_foreach (t, xcm->tunnels,
582   ({
583     vlib_cli_output (vm, "%U", format_l2xcrw, vnm, t);
584   }));
585   /* *INDENT-ON* */
586
587   return 0;
588 }
589
590 /*?
591  * Display a Layer 2 to Layer 3 rewrite cross-connect. This is used to
592  * hook Layer 2 interface(s) up to the Layer 3 stack in arbitrary ways.
593  *
594  * @todo This is incomplete. This needs a detailed description and a
595  * practical example.
596 ?*/
597 /* *INDENT-OFF* */
598 VLIB_CLI_COMMAND (show_l2xcrw_command, static) = {
599   .path = "show l2xcrw",
600   .short_help = "show l2xcrw",
601   .function = show_l2xcrw_command_fn,
602 };
603 /* *INDENT-ON* */
604
605 /*
606  * fd.io coding-style-patch-verification: ON
607  *
608  * Local Variables:
609  * eval: (c-set-style "gnu")
610  * End:
611  */