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 <vnet/feature/feature.h>
20 #include <vppinfra/error.h>
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;
28 /* convenience variables */
29 vlib_main_t *vlib_main;
30 vnet_main_t *vnet_main;
39 /* packet trace format function */
41 format_l2_patch_trace (u8 * s, va_list * args)
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 *);
47 s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index,
52 l2_patch_main_t l2_patch_main;
54 static vlib_node_registration_t l2_patch_node;
56 #define foreach_l2_patch_error \
57 _(PATCHED, "L2 patch packets") \
58 _(DROPPED, "L2 patch misconfigured drops")
62 #define _(sym,str) L2_PATCH_ERROR_##sym,
63 foreach_l2_patch_error
68 static char *l2_patch_error_strings[] = {
69 #define _(sym,string) string,
70 foreach_l2_patch_error
81 l2_patch_node_fn (vlib_main_t * vm,
82 vlib_node_runtime_t * node, vlib_frame_t * frame)
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;
91 from = vlib_frame_vector_args (frame);
92 n_left_from = frame->n_vectors;
93 next_index = node->cached_next_index;
95 while (n_left_from > 0)
99 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
101 while (n_left_from >= 4 && n_left_to_next >= 2)
104 vlib_buffer_t *b0, *b1;
106 u32 sw_if_index0, sw_if_index1;
108 /* Prefetch next iteration. */
110 vlib_buffer_t *p2, *p3;
112 p2 = vlib_get_buffer (vm, from[2]);
113 p3 = vlib_get_buffer (vm, from[3]);
115 vlib_prefetch_buffer_header (p2, LOAD);
116 vlib_prefetch_buffer_header (p3, LOAD);
118 /* So stupid / simple, we don't need to prefetch data */
121 /* speculatively enqueue b0 and b1 to the current next frame */
122 to_next[0] = bi0 = from[0];
123 to_next[1] = bi1 = from[1];
129 b0 = vlib_get_buffer (vm, bi0);
130 b1 = vlib_get_buffer (vm, bi1);
132 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
133 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
135 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
136 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
137 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index1] != ~0);
138 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1] != ~0);
140 next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
141 next1 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index1];
142 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
143 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
144 vnet_buffer (b1)->sw_if_index[VLIB_TX] =
145 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1];
147 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
149 if (b0->flags & VLIB_BUFFER_IS_TRACED)
151 l2_patch_trace_t *t =
152 vlib_add_trace (vm, node, b0, sizeof (*t));
153 t->rx_sw_if_index = sw_if_index0;
155 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
157 if (b1->flags & VLIB_BUFFER_IS_TRACED)
159 l2_patch_trace_t *t =
160 vlib_add_trace (vm, node, b1, sizeof (*t));
161 t->rx_sw_if_index = sw_if_index1;
163 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index1];
167 /* verify speculative enqueues, maybe switch current next frame */
168 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
169 to_next, n_left_to_next,
170 bi0, bi1, next0, next1);
173 while (n_left_from > 0 && n_left_to_next > 0)
180 /* speculatively enqueue b0 to the current next frame */
188 b0 = vlib_get_buffer (vm, bi0);
190 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
192 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index0] != ~0);
193 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0] != ~0);
195 next0 = l2pm->tx_next_by_rx_sw_if_index[sw_if_index0];
196 vnet_buffer (b0)->sw_if_index[VLIB_TX] =
197 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
199 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
201 if (b0->flags & VLIB_BUFFER_IS_TRACED)
203 l2_patch_trace_t *t =
204 vlib_add_trace (vm, node, b0, sizeof (*t));
205 t->rx_sw_if_index = sw_if_index0;
207 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index0];
211 /* verify speculative enqueue, maybe switch current next frame */
212 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
213 to_next, n_left_to_next,
217 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
220 em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] +=
223 return frame->n_vectors;
227 VLIB_REGISTER_NODE (l2_patch_node, static) = {
228 .function = l2_patch_node_fn,
230 .vector_size = sizeof (u32),
231 .format_trace = format_l2_patch_trace,
232 .type = VLIB_NODE_TYPE_INTERNAL,
234 .n_errors = ARRAY_LEN(l2_patch_error_strings),
235 .error_strings = l2_patch_error_strings,
237 .n_next_nodes = L2_PATCH_N_NEXT,
239 /* edit / add dispositions here */
241 [L2_PATCH_NEXT_DROP] = "error-drop",
246 VLIB_NODE_FUNCTION_MULTIARCH (l2_patch_node, l2_patch_node_fn)
247 int vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index,
250 l2_patch_main_t *l2pm = &l2_patch_main;
251 vnet_hw_interface_t *rxhi, *txhi;
255 * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX
259 rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index);
261 /* Make sure caller didn't pass a vlan subif, etc. */
262 if (rxhi->sw_if_index != rx_sw_if_index)
263 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
265 txhi = vnet_get_sup_hw_interface (l2pm->vnet_main, tx_sw_if_index);
266 if (txhi->sw_if_index != tx_sw_if_index)
267 return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
271 tx_next_index = vlib_node_add_next (l2pm->vlib_main,
273 txhi->output_node_index);
275 vec_validate_init_empty (l2pm->tx_next_by_rx_sw_if_index,
278 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index;
279 vec_validate_init_empty (l2pm->tx_sw_if_index_by_rx_sw_if_index,
281 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]
284 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
285 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
287 vnet_feature_enable_disable ("device-input", "l2-patch",
288 rxhi->hw_if_index, 1, 0, 0);
292 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
293 0 /* disable promiscuous mode */ );
295 vnet_feature_enable_disable ("device-input", "l2-patch",
296 rxhi->hw_if_index, 0, 0, 0);
297 if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index)
299 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0;
300 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0;
307 static clib_error_t *
308 test_patch_command_fn (vlib_main_t * vm,
309 unformat_input_t * input, vlib_cli_command_t * cmd)
311 l2_patch_main_t *l2pm = &l2_patch_main;
312 unformat_input_t _line_input, *line_input = &_line_input;
313 u32 rx_sw_if_index, tx_sw_if_index;
318 clib_error_t *error = NULL;
320 /* Get a line of input. */
321 if (!unformat_user (input, unformat_line_input, line_input))
324 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
326 if (unformat (line_input, "rx %U", unformat_vnet_sw_interface,
327 l2pm->vnet_main, &rx_sw_if_index))
329 else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface,
330 l2pm->vnet_main, &tx_sw_if_index))
332 else if (unformat (line_input, "del"))
340 error = clib_error_return (0, "rx interface not set");
346 error = clib_error_return (0, "tx interface not set");
350 rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add);
357 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
358 error = clib_error_return (0, "rx interface not a physical port");
361 case VNET_API_ERROR_INVALID_SW_IF_INDEX_2:
362 error = clib_error_return (0, "tx interface not a physical port");
366 error = clib_error_return
367 (0, "WARNING: vnet_l2_patch_add_del returned %d", rv);
373 unformat_free (line_input);
379 * Create or delete a Layer 2 patch.
382 * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]}
384 * @todo This is incomplete. This needs a detailed description and a
388 VLIB_CLI_COMMAND (test_patch_command, static) = {
389 .path = "test l2patch",
390 .short_help = "test l2patch rx <intfc> tx <intfc> [del]",
391 .function = test_patch_command_fn,
395 /** Display the contents of the l2patch table. */
396 static clib_error_t *
397 show_l2patch (vlib_main_t * vm,
398 unformat_input_t * input, vlib_cli_command_t * cmd)
400 l2_patch_main_t *l2pm = &l2_patch_main;
404 ASSERT (vec_len (l2pm->tx_next_by_rx_sw_if_index) ==
405 vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index));
407 for (rx_sw_if_index = 0;
408 rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index);
412 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index];
413 if (tx_sw_if_index != ~0)
416 vlib_cli_output (vm, "%26U -> %U",
417 format_vnet_sw_if_index_name,
418 l2pm->vnet_main, rx_sw_if_index,
419 format_vnet_sw_if_index_name,
420 l2pm->vnet_main, tx_sw_if_index);
425 vlib_cli_output (vm, "no l2patch entries");
431 * Show Layer 2 patch entries.
434 * @cliexstart{show l2patch}
436 * @todo This is incomplete. This needs a detailed description and a
440 VLIB_CLI_COMMAND (show_l2patch_cli, static) = {
441 .path = "show l2patch",
442 .short_help = "Show l2 interface cross-connect entries",
443 .function = show_l2patch,
448 l2_patch_init (vlib_main_t * vm)
450 l2_patch_main_t *mp = &l2_patch_main;
453 mp->vnet_main = vnet_get_main ();
458 VLIB_INIT_FUNCTION (l2_patch_init);
461 * fd.io coding-style-patch-verification: ON
464 * eval: (c-set-style "gnu")