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