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 <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>
23 u32 cached_next_index;
24 u32 cached_rx_sw_if_index;
26 /* vector of dispositions, indexed by rx_sw_if_index */
27 u32 *tx_next_by_rx_sw_if_index;
28 u32 *tx_sw_if_index_by_rx_sw_if_index;
30 /* convenience variables */
31 vlib_main_t *vlib_main;
32 vnet_main_t *vnet_main;
41 /* packet trace format function */
43 format_l2_patch_trace (u8 * s, va_list * args)
45 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
46 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
47 l2_patch_trace_t *t = va_arg (*args, l2_patch_trace_t *);
49 s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index,
54 l2_patch_main_t l2_patch_main;
56 static vlib_node_registration_t l2_patch_node;
58 #define foreach_l2_patch_error \
59 _(PATCHED, "L2 patch packets") \
60 _(DROPPED, "L2 patch misconfigured drops")
64 #define _(sym,str) L2_PATCH_ERROR_##sym,
65 foreach_l2_patch_error
70 static char *l2_patch_error_strings[] = {
71 #define _(sym,string) string,
72 foreach_l2_patch_error
83 l2_patch_node_fn (vlib_main_t * vm,
84 vlib_node_runtime_t * node, vlib_frame_t * frame)
86 u32 n_left_from, *from, *to_next;
87 l2_patch_next_t next_index;
88 l2_patch_main_t *l2pm = &l2_patch_main;
89 vlib_node_t *n = vlib_get_node (vm, l2_patch_node.index);
90 u32 node_counter_base_index = n->error_heap_index;
91 vlib_error_main_t *em = &vm->error_main;
93 from = vlib_frame_vector_args (frame);
94 n_left_from = frame->n_vectors;
95 next_index = node->cached_next_index;
97 while (n_left_from > 0)
101 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
103 while (n_left_from >= 4 && n_left_to_next >= 2)
106 vlib_buffer_t *b0, *b1;
108 u32 sw_if_index0, sw_if_index1;
110 /* Prefetch next iteration. */
112 vlib_buffer_t *p2, *p3;
114 p2 = vlib_get_buffer (vm, from[2]);
115 p3 = vlib_get_buffer (vm, from[3]);
117 vlib_prefetch_buffer_header (p2, LOAD);
118 vlib_prefetch_buffer_header (p3, LOAD);
120 /* So stupid / simple, we don't need to prefetch data */
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];
131 b0 = vlib_get_buffer (vm, bi0);
132 b1 = vlib_get_buffer (vm, bi1);
134 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
135 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
137 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
138 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
139 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index1] != ~0);
140 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1] != ~0);
142 if (PREDICT_TRUE (sw_if_index0 == l2pm->cached_rx_sw_if_index))
143 next0 = l2pm->cached_next_index;
146 next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
147 l2pm->cached_rx_sw_if_index = sw_if_index0;
148 l2pm->cached_next_index = next0;
151 if (PREDICT_TRUE (sw_if_index1 == l2pm->cached_rx_sw_if_index))
152 next1 = l2pm->cached_next_index;
154 next1 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index1];
156 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
158 if (b0->flags & VLIB_BUFFER_IS_TRACED)
160 l2_patch_trace_t *t =
161 vlib_add_trace (vm, node, b0, sizeof (*t));
162 t->rx_sw_if_index = sw_if_index0;
164 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
166 if (b1->flags & VLIB_BUFFER_IS_TRACED)
168 l2_patch_trace_t *t =
169 vlib_add_trace (vm, node, b1, sizeof (*t));
170 t->rx_sw_if_index = sw_if_index1;
172 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1];
176 /* verify speculative enqueues, maybe switch current next frame */
177 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
178 to_next, n_left_to_next,
179 bi0, bi1, next0, next1);
182 while (n_left_from > 0 && n_left_to_next > 0)
189 /* speculatively enqueue b0 to the current next frame */
197 b0 = vlib_get_buffer (vm, bi0);
199 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
201 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
202 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
204 if (PREDICT_TRUE (sw_if_index0 == l2pm->cached_rx_sw_if_index))
205 next0 = l2pm->cached_next_index;
208 next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
209 l2pm->cached_rx_sw_if_index = sw_if_index0;
210 l2pm->cached_next_index = next0;
213 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
215 if (b0->flags & VLIB_BUFFER_IS_TRACED)
217 l2_patch_trace_t *t =
218 vlib_add_trace (vm, node, b0, sizeof (*t));
219 t->rx_sw_if_index = sw_if_index0;
221 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
225 /* verify speculative enqueue, maybe switch current next frame */
226 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
227 to_next, n_left_to_next,
231 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
234 em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] +=
237 return frame->n_vectors;
241 VLIB_REGISTER_NODE (l2_patch_node, static) = {
242 .function = l2_patch_node_fn,
244 .vector_size = sizeof (u32),
245 .format_trace = format_l2_patch_trace,
246 .type = VLIB_NODE_TYPE_INTERNAL,
248 .n_errors = ARRAY_LEN(l2_patch_error_strings),
249 .error_strings = l2_patch_error_strings,
251 .n_next_nodes = L2_PATCH_N_NEXT,
253 /* edit / add dispositions here */
255 [L2_PATCH_NEXT_DROP] = "error-drop",
260 VLIB_NODE_FUNCTION_MULTIARCH (l2_patch_node, l2_patch_node_fn)
261 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
264 l2_patch_main_t *l2pm = &l2_patch_main;
265 vnet_hw_interface_t *rxhi, *txhi;
269 * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX
273 rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index);
275 /* Make sure caller didn't pass a vlan subif, etc. */
276 if (rxhi->sw_if_index != rx_sw_if_index)
277 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
279 txhi = vnet_get_sup_hw_interface (l2pm->vnet_main, tx_sw_if_index);
280 if (txhi->sw_if_index != tx_sw_if_index)
281 return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
285 tx_next_index = vlib_node_add_next (l2pm->vlib_main,
287 txhi->output_node_index);
289 vec_validate_init_empty (l2pm->tx_next_by_rx_sw_if_index,
292 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index;
293 vec_validate_init_empty (l2pm->tx_sw_if_index_by_rx_sw_if_index,
295 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]
298 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
299 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
301 vnet_hw_interface_rx_redirect_to_node (l2pm->vnet_main,
303 l2_patch_node.index);
307 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
308 0 /* disable promiscuous mode */ );
310 vnet_hw_interface_rx_redirect_to_node (l2pm->vnet_main,
313 if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index)
315 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0;
316 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0;
323 static clib_error_t *
324 test_patch_command_fn (vlib_main_t * vm,
325 unformat_input_t * input, vlib_cli_command_t * cmd)
327 l2_patch_main_t *l2pm = &l2_patch_main;
328 unformat_input_t _line_input, *line_input = &_line_input;
329 u32 rx_sw_if_index, tx_sw_if_index;
335 /* Get a line of input. */
336 if (!unformat_user (input, unformat_line_input, line_input))
339 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
341 if (unformat (line_input, "rx %U", unformat_vnet_sw_interface,
342 l2pm->vnet_main, &rx_sw_if_index))
344 else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface,
345 l2pm->vnet_main, &tx_sw_if_index))
347 else if (unformat (line_input, "del"))
354 return clib_error_return (0, "rx interface not set");
357 return clib_error_return (0, "tx interface not set");
359 rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add);
366 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
367 return clib_error_return (0, "rx interface not a physical port");
369 case VNET_API_ERROR_INVALID_SW_IF_INDEX_2:
370 return clib_error_return (0, "tx interface not a physical port");
373 return clib_error_return
374 (0, "WARNING: vnet_l2_patch_add_del returned %d", rv);
381 * Create or delete a Layer 2 patch.
384 * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]}
386 * @todo This is incomplete. This needs a detailed description and a
390 VLIB_CLI_COMMAND (test_patch_command, static) = {
391 .path = "test l2patch",
392 .short_help = "test l2patch rx <intfc> tx <intfc> [del]",
393 .function = test_patch_command_fn,
397 /** Display the contents of the l2patch table. */
398 static clib_error_t *
399 show_l2patch (vlib_main_t * vm,
400 unformat_input_t * input, vlib_cli_command_t * cmd)
402 l2_patch_main_t *l2pm = &l2_patch_main;
406 ASSERT (vec_len (l2pm->tx_next_by_rx_sw_if_index) ==
407 vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index));
409 for (rx_sw_if_index = 0;
410 rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index);
414 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index];
415 if (tx_sw_if_index != ~0)
418 vlib_cli_output (vm, "%26U -> %U",
419 format_vnet_sw_if_index_name,
420 l2pm->vnet_main, rx_sw_if_index,
421 format_vnet_sw_if_index_name,
422 l2pm->vnet_main, tx_sw_if_index);
427 vlib_cli_output (vm, "no l2patch entries");
433 * Show Layer 2 patch entries.
436 * @cliexstart{show l2patch}
438 * @todo This is incomplete. This needs a detailed description and a
442 VLIB_CLI_COMMAND (show_l2patch_cli, static) = {
443 .path = "show l2patch",
444 .short_help = "Show l2 interface cross-connect entries",
445 .function = show_l2patch,
450 l2_patch_init (vlib_main_t * vm)
452 l2_patch_main_t *mp = &l2_patch_main;
455 mp->vnet_main = vnet_get_main ();
460 VLIB_INIT_FUNCTION (l2_patch_init);
463 * fd.io coding-style-patch-verification: ON
466 * eval: (c-set-style "gnu")