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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/l2/l2_xcrw.h>
19 * General L2 / L3 cross-connect, used to set up
20 * "L2 interface <--> your-favorite-tunnel-encap" tunnels.
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.
25 * Each l2_xcrw adjacency specifies 3 things:
27 * 1. The next graph node (presumably in the L3 stack) to
28 * process the (L2 -> L3) packet
30 * 2. A new value for vnet_buffer(b)->sw_if_index[VLIB_TX]
31 * (i.e. a lookup FIB index),
33 * 3. A rewrite string to apply.
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.
47 /* packet trace format function */
49 format_l2_xcrw_trace (u8 * s, va_list * args)
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 *);
55 s = format (s, "L2_XCRW: next index %d tx_fib_index %d",
56 t->next_index, t->tx_fib_index);
60 l2_xcrw_main_t l2_xcrw_main;
62 static vlib_node_registration_t l2_xcrw_node;
64 static char *l2_xcrw_error_strings[] = {
65 #define _(sym,string) string,
71 l2_xcrw_node_fn (vlib_main_t * vm,
72 vlib_node_runtime_t * node, vlib_frame_t * frame)
74 u32 n_left_from, *from, *to_next;
75 l2_xcrw_next_t next_index;
76 l2_xcrw_main_t *xcm = &l2_xcrw_main;
77 vlib_node_t *n = vlib_get_node (vm, l2_xcrw_node.index);
78 u32 node_counter_base_index = n->error_heap_index;
79 vlib_error_main_t *em = &vm->error_main;
81 from = vlib_frame_vector_args (frame);
82 n_left_from = frame->n_vectors;
83 next_index = node->cached_next_index;
85 while (n_left_from > 0)
89 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
91 while (n_left_from >= 4 && n_left_to_next >= 2)
94 vlib_buffer_t *b0, *b1;
96 u32 sw_if_index0, sw_if_index1;
97 l2_xcrw_adjacency_t *adj0, *adj1;
99 /* Prefetch next iteration. */
101 vlib_buffer_t *p2, *p3;
103 p2 = vlib_get_buffer (vm, from[2]);
104 p3 = vlib_get_buffer (vm, from[3]);
106 vlib_prefetch_buffer_header (p2, LOAD);
107 vlib_prefetch_buffer_header (p3, LOAD);
109 CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
110 CLIB_PREFETCH (p3->data, CLIB_CACHE_LINE_BYTES, STORE);
113 /* speculatively enqueue b0 and b1 to the current next frame */
114 to_next[0] = bi0 = from[0];
115 to_next[1] = bi1 = from[1];
121 b0 = vlib_get_buffer (vm, bi0);
122 b1 = vlib_get_buffer (vm, bi1);
124 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
125 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
127 adj0 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index0);
128 adj1 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index1);
130 next0 = adj0->rewrite_header.next_index;
131 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
132 adj0->rewrite_header.sw_if_index;
134 next1 = adj1->rewrite_header.next_index;
135 vnet_buffer (b1)->sw_if_index[VLIB_TX] =
136 adj1->rewrite_header.sw_if_index;
138 em->counters[node_counter_base_index + next1]++;
140 if (PREDICT_TRUE (next0 > 0))
142 u8 *h0 = vlib_buffer_get_current (b0);
143 vnet_rewrite_one_header (adj0[0], h0,
144 adj0->rewrite_header.data_bytes);
145 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
146 em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
149 if (PREDICT_TRUE (next1 > 0))
151 u8 *h1 = vlib_buffer_get_current (b1);
152 vnet_rewrite_one_header (adj1[0], h1,
153 adj1->rewrite_header.data_bytes);
154 vlib_buffer_advance (b1, -adj1->rewrite_header.data_bytes);
155 em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
159 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
161 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
162 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
165 vlib_add_trace (vm, node, b0, sizeof (*t));
166 t->next_index = next0;
167 t->tx_fib_index = adj0->rewrite_header.sw_if_index;
169 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
170 && (b1->flags & VLIB_BUFFER_IS_TRACED)))
173 vlib_add_trace (vm, node, b1, sizeof (*t));
174 t->next_index = next1;
175 t->tx_fib_index = adj1->rewrite_header.sw_if_index;
179 /* verify speculative enqueues, maybe switch current next frame */
180 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
181 to_next, n_left_to_next,
182 bi0, bi1, next0, next1);
185 while (n_left_from > 0 && n_left_to_next > 0)
191 l2_xcrw_adjacency_t *adj0;
193 /* speculatively enqueue b0 to the current next frame */
201 b0 = vlib_get_buffer (vm, bi0);
203 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
205 adj0 = vec_elt_at_index (xcm->adj_by_sw_if_index, sw_if_index0);
207 next0 = adj0->rewrite_header.next_index;
208 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
209 adj0->rewrite_header.sw_if_index;
211 if (PREDICT_TRUE (next0 > 0))
213 u8 *h0 = vlib_buffer_get_current (b0);
214 vnet_rewrite_one_header (adj0[0], h0,
215 adj0->rewrite_header.data_bytes);
216 vlib_buffer_advance (b0, -adj0->rewrite_header.data_bytes);
217 em->counters[node_counter_base_index + L2_XCRW_ERROR_FWD]++;
220 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
221 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
223 l2_xcrw_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
224 t->next_index = next0;
225 t->tx_fib_index = adj0->rewrite_header.sw_if_index;
228 /* verify speculative enqueue, maybe switch current next frame */
229 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
230 to_next, n_left_to_next,
234 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
237 return frame->n_vectors;
241 VLIB_REGISTER_NODE (l2_xcrw_node, static) = {
242 .function = l2_xcrw_node_fn,
244 .vector_size = sizeof (u32),
245 .format_trace = format_l2_xcrw_trace,
246 .type = VLIB_NODE_TYPE_INTERNAL,
248 .n_errors = ARRAY_LEN(l2_xcrw_error_strings),
249 .error_strings = l2_xcrw_error_strings,
251 .n_next_nodes = L2_XCRW_N_NEXT,
253 /* edit / add dispositions here */
255 [L2_XCRW_NEXT_DROP] = "error-drop",
260 VLIB_NODE_FUNCTION_MULTIARCH (l2_xcrw_node, l2_xcrw_node_fn)
261 clib_error_t *l2_xcrw_init (vlib_main_t * vm)
263 l2_xcrw_main_t *mp = &l2_xcrw_main;
266 mp->vnet_main = &vnet_main;
267 mp->tunnel_index_by_l2_sw_if_index = hash_create (0, sizeof (uword));
272 VLIB_INIT_FUNCTION (l2_xcrw_init);
275 dummy_interface_tx (vlib_main_t * vm,
276 vlib_node_runtime_t * node, vlib_frame_t * frame)
278 clib_warning ("you shouldn't be here, leaking buffers...");
279 return frame->n_vectors;
283 format_xcrw_name (u8 * s, va_list * args)
285 u32 dev_instance = va_arg (*args, u32);
286 return format (s, "xcrw%d", dev_instance);
290 VNET_DEVICE_CLASS (xcrw_device_class,static) = {
292 .format_device_name = format_xcrw_name,
293 .tx_function = dummy_interface_tx,
297 /* Create a sham tunnel interface and return its sw_if_index */
299 create_xcrw_interface (vlib_main_t * vm)
301 vnet_main_t *vnm = vnet_get_main ();
305 vnet_hw_interface_t *hi;
308 /* mac address doesn't really matter */
309 clib_memset (address, 0, sizeof (address));
312 /* can returns error iff phy != 0 */
313 (void) ethernet_register_interface
314 (vnm, xcrw_device_class.index, instance++, address, &hw_if_index,
315 /* flag change */ 0);
317 hi = vnet_get_hw_interface (vnm, hw_if_index);
318 sw_if_index = hi->sw_if_index;
319 vnet_sw_interface_set_flags (vnm, sw_if_index,
320 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
322 /* Output to the sham tunnel invokes the encap node */
323 hi->output_node_index = l2_xcrw_node.index;
329 vnet_configure_l2_xcrw (vlib_main_t * vm, vnet_main_t * vnm,
330 u32 l2_sw_if_index, u32 tx_fib_index,
331 u8 * rewrite, u32 next_node_index, int is_add)
333 l2_xcrw_main_t *xcm = &l2_xcrw_main;
334 l2_xcrw_adjacency_t *a;
341 pool_get (xcm->tunnels, t);
343 /* No interface allocated? Do it. Otherwise, set admin up */
344 if (t->tunnel_sw_if_index == 0)
345 t->tunnel_sw_if_index = create_xcrw_interface (vm);
347 vnet_sw_interface_set_flags (vnm, t->tunnel_sw_if_index,
348 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
350 t->l2_sw_if_index = l2_sw_if_index;
352 vec_validate (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
354 a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
355 clib_memset (a, 0, sizeof (*a));
357 a->rewrite_header.sw_if_index = tx_fib_index;
360 * Add or find a dynamic disposition for the successor node,
361 * e.g. so we can ship pkts to mpls_post_rewrite...
363 a->rewrite_header.next_index =
364 vlib_node_add_next (vm, l2_xcrw_node.index, next_node_index);
366 if (vec_len (rewrite))
367 vnet_rewrite_set_data (a[0], rewrite, vec_len (rewrite));
369 set_int_l2_mode (vm, vnm, MODE_L2_XC, t->l2_sw_if_index, 0,
370 L2_BD_PORT_TYPE_NORMAL, 0, t->tunnel_sw_if_index);
371 hash_set (xcm->tunnel_index_by_l2_sw_if_index,
372 t->l2_sw_if_index, t - xcm->tunnels);
377 p = hash_get (xcm->tunnel_index_by_l2_sw_if_index, l2_sw_if_index);
379 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
381 t = pool_elt_at_index (xcm->tunnels, p[0]);
383 a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
384 /* Reset adj to drop traffic */
385 clib_memset (a, 0, sizeof (*a));
387 set_int_l2_mode (vm, vnm, MODE_L3, t->l2_sw_if_index, 0,
388 L2_BD_PORT_TYPE_NORMAL, 0, 0);
390 vnet_sw_interface_set_flags (vnm, t->tunnel_sw_if_index, 0 /* down */ );
392 hash_unset (xcm->tunnel_index_by_l2_sw_if_index, l2_sw_if_index);
393 pool_put (xcm->tunnels, t);
399 static clib_error_t *
400 set_l2_xcrw_command_fn (vlib_main_t * vm,
401 unformat_input_t * input, vlib_cli_command_t * cmd)
403 unformat_input_t _line_input, *line_input = &_line_input;
405 int is_ipv6 = 0; /* for fib id -> fib index mapping */
407 u32 tx_fib_index = ~0;
408 u32 next_node_index = ~0;
411 vnet_main_t *vnm = vnet_get_main ();
413 clib_error_t *error = NULL;
416 if (!unformat_user (input, unformat_line_input, line_input))
419 if (!unformat (line_input, "%U",
420 unformat_vnet_sw_interface, vnm, &l2_sw_if_index))
422 error = clib_error_return (0, "unknown input '%U'",
423 format_unformat_error, line_input);
427 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
429 if (unformat (line_input, "next %U",
430 unformat_vlib_node, vm, &next_node_index))
432 else if (unformat (line_input, "tx-fib-id %d", &tx_fib_id))
434 else if (unformat (line_input, "del"))
436 else if (unformat (line_input, "ipv6"))
438 else if (unformat (line_input, "rw %U", unformat_hex_string, &rw));
443 if (next_node_index == ~0)
445 error = clib_error_return (0, "next node not specified");
454 p = hash_get (ip6_main.fib_index_by_table_id, tx_fib_id);
456 p = hash_get (ip4_main.fib_index_by_table_id, tx_fib_id);
461 clib_error_return (0, "nonexistent tx_fib_id %d", tx_fib_id);
468 rv = vnet_configure_l2_xcrw (vm, vnm, l2_sw_if_index, tx_fib_index,
469 rw, next_node_index, is_add);
477 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
478 error = clib_error_return (0, "%U not cross-connected",
479 format_vnet_sw_if_index_name,
480 vnm, l2_sw_if_index);
484 error = clib_error_return (0, "vnet_configure_l2_xcrw returned %d", rv);
490 unformat_free (line_input);
496 * Add or delete a Layer 2 to Layer 3 rewrite cross-connect. This is
497 * used to hook Layer 2 interface(s) up to the Layer 3 stack in
498 * arbitrary ways. For example, cross-connect an L2 interface or
499 * (future) bridge to an mpls-o-gre tunnel. Set up the L2 rewrite
500 * string as shown in mpls_gre_rewrite, and use \"mpls-post-rewrite\"
501 * to fix the GRE IP header checksum and length fields.
504 * @todo This is incomplete. This needs a detailed description and a
508 VLIB_CLI_COMMAND (set_l2_xcrw_command, static) = {
509 .path = "set interface l2 xcrw",
511 "set interface l2 xcrw <interface> next <node-name>\n"
512 " [del] [tx-fib-id <id>] [ipv6] rw <hex-bytes>",
513 .function = set_l2_xcrw_command_fn,
518 format_l2xcrw (u8 * s, va_list * args)
520 vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
521 l2_xcrw_tunnel_t *t = va_arg (*args, l2_xcrw_tunnel_t *);
522 l2_xcrw_main_t *xcm = &l2_xcrw_main;
523 vlib_main_t *vm = vlib_get_main ();
524 l2_xcrw_adjacency_t *a;
529 s = format (s, "%-25s%s", "L2 interface", "Tunnel Details");
533 s = format (s, "%-25U %U ",
534 format_vnet_sw_if_index_name, vnm, t->l2_sw_if_index,
535 format_vnet_sw_if_index_name, vnm, t->tunnel_sw_if_index);
537 a = vec_elt_at_index (xcm->adj_by_sw_if_index, t->l2_sw_if_index);
539 s = format (s, "next %U ",
540 format_vlib_next_node_name, vm, l2_xcrw_node.index,
541 a->rewrite_header.next_index);
543 if (a->rewrite_header.sw_if_index != ~0)
544 s = format (s, "tx fib index %d ", a->rewrite_header.sw_if_index);
546 if (a->rewrite_header.data_bytes)
548 rewrite_string = (u8 *) (a + 1);
549 rewrite_string -= a->rewrite_header.data_bytes;
550 s = format (s, "rewrite data: %U ",
551 format_hex_bytes, rewrite_string,
552 a->rewrite_header.data_bytes);
555 s = format (s, "\n");
561 static clib_error_t *
562 show_l2xcrw_command_fn (vlib_main_t * vm,
563 unformat_input_t * input, vlib_cli_command_t * cmd)
565 vnet_main_t *vnm = vnet_get_main ();
566 l2_xcrw_main_t *xcm = &l2_xcrw_main;
569 if (pool_elts (xcm->tunnels) == 0)
571 vlib_cli_output (vm, "No L2 / L3 rewrite cross-connects configured");
575 vlib_cli_output (vm, "%U", format_l2xcrw, 0, 0);
578 pool_foreach (t, xcm->tunnels,
580 vlib_cli_output (vm, "%U", format_l2xcrw, vnm, t);
588 * Display a Layer 2 to Layer 3 rewrite cross-connect. This is used to
589 * hook Layer 2 interface(s) up to the Layer 3 stack in arbitrary ways.
591 * @todo This is incomplete. This needs a detailed description and a
595 VLIB_CLI_COMMAND (show_l2xcrw_command, static) = {
596 .path = "show l2xcrw",
597 .short_help = "show l2xcrw",
598 .function = show_l2xcrw_command_fn,
603 * fd.io coding-style-patch-verification: ON
606 * eval: (c-set-style "gnu")