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/ethernet/ethernet.h>
18 #include <vnet/feature/feature.h>
19 #include <vppinfra/error.h>
23 /* vector of dispositions, indexed by rx_sw_if_index */
24 u32 *tx_next_by_rx_sw_if_index;
25 u32 *tx_sw_if_index_by_rx_sw_if_index;
27 /* convenience variables */
28 vlib_main_t *vlib_main;
29 vnet_main_t *vnet_main;
38 /* packet trace format function */
40 format_l2_patch_trace (u8 * s, va_list * args)
42 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
43 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
44 l2_patch_trace_t *t = va_arg (*args, l2_patch_trace_t *);
46 s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index,
51 #ifndef CLIB_MARCH_VARIANT
52 l2_patch_main_t l2_patch_main;
54 extern l2_patch_main_t l2_patch_main;
57 extern vlib_node_registration_t l2_patch_node;
59 #define foreach_l2_patch_error \
60 _(PATCHED, "L2 patch packets") \
61 _(DROPPED, "L2 patch misconfigured drops")
65 #define _(sym,str) L2_PATCH_ERROR_##sym,
66 foreach_l2_patch_error
71 static char *l2_patch_error_strings[] = {
72 #define _(sym,string) string,
73 foreach_l2_patch_error
83 static_always_inline void
84 l2_patch_trace (vlib_main_t * vm, vlib_node_runtime_t * node,
85 l2_patch_main_t * l2pm, vlib_buffer_t * b, u32 sw_if_index)
89 if ((b->flags & VLIB_BUFFER_IS_TRACED) == 0)
92 t = vlib_add_trace (vm, node, b, sizeof (*t));
93 t->rx_sw_if_index = sw_if_index;
94 t->tx_sw_if_index = l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index];
97 static_always_inline void
98 l2_patch_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
99 l2_patch_main_t * l2pm, vlib_buffer_t ** b, u16 * next,
100 u32 n_left, int do_trace)
106 /* Prefetch next iteration. */
109 vlib_buffer_t **p = b + 4;
110 vlib_prefetch_buffer_header (p[0], LOAD);
111 vlib_prefetch_buffer_header (p[1], LOAD);
112 vlib_prefetch_buffer_header (p[2], LOAD);
113 vlib_prefetch_buffer_header (p[3], LOAD);
116 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
117 sw_if_index[1] = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
118 sw_if_index[2] = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
119 sw_if_index[3] = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
121 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[0]] != ~0);
122 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[0]] != ~0);
123 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[1]] != ~0);
124 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[1]] != ~0);
125 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[2]] != ~0);
126 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[2]] != ~0);
127 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[3]] != ~0);
128 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[3]] != ~0);
130 next[0] = l2pm->tx_next_by_rx_sw_if_index[sw_if_index[0]];
131 next[1] = l2pm->tx_next_by_rx_sw_if_index[sw_if_index[1]];
132 next[2] = l2pm->tx_next_by_rx_sw_if_index[sw_if_index[2]];
133 next[3] = l2pm->tx_next_by_rx_sw_if_index[sw_if_index[3]];
135 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
136 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[0]];
137 vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
138 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[1]];
139 vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
140 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[2]];
141 vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
142 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[3]];
146 l2_patch_trace (vm, node, l2pm, b[0], sw_if_index[0]);
147 l2_patch_trace (vm, node, l2pm, b[1], sw_if_index[1]);
148 l2_patch_trace (vm, node, l2pm, b[2], sw_if_index[2]);
149 l2_patch_trace (vm, node, l2pm, b[3], sw_if_index[3]);
160 sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
162 ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[0]] != ~0);
163 ASSERT (l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[0]] != ~0);
165 next[0] = l2pm->tx_next_by_rx_sw_if_index[sw_if_index[0]];
167 vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
168 l2pm->tx_sw_if_index_by_rx_sw_if_index[sw_if_index[0]];
171 l2_patch_trace (vm, node, l2pm, b[0], sw_if_index[0]);
180 VLIB_NODE_FN (l2_patch_node) (vlib_main_t * vm,
181 vlib_node_runtime_t * node,
182 vlib_frame_t * frame)
185 l2_patch_main_t *l2pm = &l2_patch_main;
186 vlib_node_t *n = vlib_get_node (vm, l2_patch_node.index);
187 u32 node_counter_base_index = n->error_heap_index;
188 vlib_error_main_t *em = &vm->error_main;
189 vlib_buffer_t *bufs[VLIB_FRAME_SIZE];
190 u16 nexts[VLIB_FRAME_SIZE];
192 from = vlib_frame_vector_args (frame);
194 vlib_get_buffers (vm, from, bufs, frame->n_vectors);
196 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
197 l2_patch_inline (vm, node, l2pm, bufs, nexts, frame->n_vectors, 1);
199 l2_patch_inline (vm, node, l2pm, bufs, nexts, frame->n_vectors, 0);
201 vlib_buffer_enqueue_to_next (vm, node, from, nexts, frame->n_vectors);
203 em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] +=
206 return frame->n_vectors;
210 VLIB_REGISTER_NODE (l2_patch_node) = {
212 .vector_size = sizeof (u32),
213 .format_trace = format_l2_patch_trace,
214 .type = VLIB_NODE_TYPE_INTERNAL,
216 .n_errors = ARRAY_LEN(l2_patch_error_strings),
217 .error_strings = l2_patch_error_strings,
219 .n_next_nodes = L2_PATCH_N_NEXT,
221 /* edit / add dispositions here */
223 [L2_PATCH_NEXT_DROP] = "error-drop",
229 vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add);
230 #ifndef CLIB_MARCH_VARIANT
232 vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add)
234 l2_patch_main_t *l2pm = &l2_patch_main;
235 vnet_hw_interface_t *rxhi, *txhi;
239 * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX
243 rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index);
245 /* Make sure caller didn't pass a vlan subif, etc. */
246 if (rxhi->sw_if_index != rx_sw_if_index)
247 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
249 txhi = vnet_get_sup_hw_interface (l2pm->vnet_main, tx_sw_if_index);
250 if (txhi->sw_if_index != tx_sw_if_index)
251 return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
255 tx_next_index = vlib_node_add_next (l2pm->vlib_main,
257 txhi->output_node_index);
259 vec_validate_init_empty (l2pm->tx_next_by_rx_sw_if_index,
262 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index;
263 vec_validate_init_empty (l2pm->tx_sw_if_index_by_rx_sw_if_index,
265 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]
268 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
269 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
271 vnet_feature_enable_disable ("device-input", "l2-patch",
272 rxhi->sw_if_index, 1, 0, 0);
273 vnet_feature_enable_disable ("port-rx-eth", "l2-patch",
274 rxhi->sw_if_index, 1, 0, 0);
278 ethernet_set_flags (l2pm->vnet_main, rxhi->hw_if_index,
279 /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
281 vnet_feature_enable_disable ("device-input", "l2-patch",
282 rxhi->sw_if_index, 0, 0, 0);
283 vnet_feature_enable_disable ("port-rx-eth", "l2-patch",
284 rxhi->sw_if_index, 0, 0, 0);
285 if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index)
287 l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0;
288 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0;
296 static clib_error_t *
297 test_patch_command_fn (vlib_main_t * vm,
298 unformat_input_t * input, vlib_cli_command_t * cmd)
300 l2_patch_main_t *l2pm = &l2_patch_main;
301 unformat_input_t _line_input, *line_input = &_line_input;
302 u32 rx_sw_if_index, tx_sw_if_index;
307 clib_error_t *error = NULL;
309 /* Get a line of input. */
310 if (!unformat_user (input, unformat_line_input, line_input))
313 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
315 if (unformat (line_input, "rx %U", unformat_vnet_sw_interface,
316 l2pm->vnet_main, &rx_sw_if_index))
318 else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface,
319 l2pm->vnet_main, &tx_sw_if_index))
321 else if (unformat (line_input, "del"))
329 error = clib_error_return (0, "rx interface not set");
335 error = clib_error_return (0, "tx interface not set");
339 rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add);
346 case VNET_API_ERROR_INVALID_SW_IF_INDEX:
347 error = clib_error_return (0, "rx interface not a physical port");
350 case VNET_API_ERROR_INVALID_SW_IF_INDEX_2:
351 error = clib_error_return (0, "tx interface not a physical port");
355 error = clib_error_return
356 (0, "WARNING: vnet_l2_patch_add_del returned %d", rv);
362 unformat_free (line_input);
368 * Create or delete a Layer 2 patch.
371 * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]}
373 * @todo This is incomplete. This needs a detailed description and a
377 VLIB_CLI_COMMAND (test_patch_command, static) = {
378 .path = "test l2patch",
379 .short_help = "test l2patch rx <intfc> tx <intfc> [del]",
380 .function = test_patch_command_fn,
384 /** Display the contents of the l2patch table. */
385 static clib_error_t *
386 show_l2patch (vlib_main_t * vm,
387 unformat_input_t * input, vlib_cli_command_t * cmd)
389 l2_patch_main_t *l2pm = &l2_patch_main;
393 ASSERT (vec_len (l2pm->tx_next_by_rx_sw_if_index) ==
394 vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index));
396 for (rx_sw_if_index = 0;
397 rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index);
401 l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index];
402 if (tx_sw_if_index != ~0)
405 vlib_cli_output (vm, "%26U -> %U",
406 format_vnet_sw_if_index_name,
407 l2pm->vnet_main, rx_sw_if_index,
408 format_vnet_sw_if_index_name,
409 l2pm->vnet_main, tx_sw_if_index);
414 vlib_cli_output (vm, "no l2patch entries");
420 * Show Layer 2 patch entries.
423 * @cliexstart{show l2patch}
425 * @todo This is incomplete. This needs a detailed description and a
429 VLIB_CLI_COMMAND (show_l2patch_cli, static) = {
430 .path = "show l2patch",
431 .short_help = "Show l2 interface cross-connect entries",
432 .function = show_l2patch,
436 static clib_error_t *
437 l2_patch_init (vlib_main_t * vm)
439 l2_patch_main_t *mp = &l2_patch_main;
442 mp->vnet_main = vnet_get_main ();
447 VLIB_INIT_FUNCTION (l2_patch_init);
450 * fd.io coding-style-patch-verification: ON
453 * eval: (c-set-style "gnu")