FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
l2_patch.c
Go to the documentation of this file.
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 <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>
20 
21 typedef struct
22 {
23  /* vector of dispositions, indexed by rx_sw_if_index */
26 
27  /* convenience variables */
31 
32 typedef struct
33 {
37 
38 /* packet trace format function */
39 static u8 *
40 format_l2_patch_trace (u8 * s, va_list * args)
41 {
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 *);
45 
46  s = format (s, "L2_PATCH: rx %d tx %d", t->rx_sw_if_index,
47  t->tx_sw_if_index);
48  return s;
49 }
50 
51 #ifndef CLIB_MARCH_VARIANT
53 #else
55 #endif
56 
58 
59 #define foreach_l2_patch_error \
60 _(PATCHED, "L2 patch packets") \
61 _(DROPPED, "L2 patch misconfigured drops")
62 
63 typedef enum
64 {
65 #define _(sym,str) L2_PATCH_ERROR_##sym,
67 #undef _
70 
71 static char *l2_patch_error_strings[] = {
72 #define _(sym,string) string,
74 #undef _
75 };
76 
77 typedef enum
78 {
82 
86 {
88 
89  if ((b->flags & VLIB_BUFFER_IS_TRACED) == 0)
90  return;
91 
92  t = vlib_add_trace (vm, node, b, sizeof (*t));
95 }
96 
99  l2_patch_main_t * l2pm, vlib_buffer_t ** b, u16 * next,
100  u32 n_left, int do_trace)
101 {
102  u32 sw_if_index[4];
103 
104  while (n_left >= 4)
105  {
106  /* Prefetch next iteration. */
107  if (n_left >= 8)
108  {
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);
114  }
115 
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];
120 
121  ASSERT (l2pm->tx_next_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);
125  ASSERT (l2pm->tx_next_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);
129 
134 
135  vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
137  vnet_buffer (b[1])->sw_if_index[VLIB_TX] =
139  vnet_buffer (b[2])->sw_if_index[VLIB_TX] =
141  vnet_buffer (b[3])->sw_if_index[VLIB_TX] =
143 
144  if (do_trace)
145  {
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]);
150  }
151 
152  /* next */
153  next += 4;
154  b += 4;
155  n_left -= 4;
156  }
157 
158  while (n_left)
159  {
160  sw_if_index[0] = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
161 
162  ASSERT (l2pm->tx_next_by_rx_sw_if_index[sw_if_index[0]] != ~0);
164 
166 
167  vnet_buffer (b[0])->sw_if_index[VLIB_TX] =
169 
170  if (do_trace)
171  l2_patch_trace (vm, node, l2pm, b[0], sw_if_index[0]);
172 
173  /* next */
174  next += 1;
175  b += 1;
176  n_left -= 1;
177  }
178 }
179 
183 {
184  u32 *from;
187  u32 node_counter_base_index = n->error_heap_index;
191 
193 
194  vlib_get_buffers (vm, from, bufs, frame->n_vectors);
195 
196  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
197  l2_patch_inline (vm, node, l2pm, bufs, nexts, frame->n_vectors, 1);
198  else
199  l2_patch_inline (vm, node, l2pm, bufs, nexts, frame->n_vectors, 0);
200 
202 
203  em->counters[node_counter_base_index + L2_PATCH_ERROR_PATCHED] +=
204  frame->n_vectors;
205 
206  return frame->n_vectors;
207 }
208 
209 /* *INDENT-OFF* */
211  .name = "l2-patch",
212  .vector_size = sizeof (u32),
213  .format_trace = format_l2_patch_trace,
215 
216  .n_errors = ARRAY_LEN(l2_patch_error_strings),
217  .error_strings = l2_patch_error_strings,
218 
219  .n_next_nodes = L2_PATCH_N_NEXT,
220 
221  /* edit / add dispositions here */
222  .next_nodes = {
223  [L2_PATCH_NEXT_DROP] = "error-drop",
224  },
225 };
226 /* *INDENT-ON* */
227 
228 extern int
229 vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add);
230 #ifndef CLIB_MARCH_VARIANT
231 int
232 vnet_l2_patch_add_del (u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add)
233 {
235  vnet_hw_interface_t *rxhi, *txhi;
236  u32 tx_next_index;
237 
238  /*
239  * We assume that the API msg handler has used 2x VALIDATE_SW_IF_INDEX
240  * macros...
241  */
242 
243  rxhi = vnet_get_sup_hw_interface (l2pm->vnet_main, rx_sw_if_index);
244 
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;
248 
250  if (txhi->sw_if_index != tx_sw_if_index)
251  return VNET_API_ERROR_INVALID_SW_IF_INDEX_2;
252 
253  if (is_add)
254  {
255  tx_next_index = vlib_node_add_next (l2pm->vlib_main,
256  l2_patch_node.index,
257  txhi->output_node_index);
258 
260  rx_sw_if_index, ~0);
261 
262  l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = tx_next_index;
264  rx_sw_if_index, ~0);
265  l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index]
266  = txhi->sw_if_index;
267 
270 
271  vnet_feature_enable_disable ("device-input", "l2-patch",
272  rxhi->sw_if_index, 1, 0, 0);
273  }
274  else
275  {
277  /*ETHERNET_INTERFACE_FLAG_DEFAULT_L3 */ 0);
278 
279  vnet_feature_enable_disable ("device-input", "l2-patch",
280  rxhi->sw_if_index, 0, 0, 0);
281  if (vec_len (l2pm->tx_next_by_rx_sw_if_index) > rx_sw_if_index)
282  {
283  l2pm->tx_next_by_rx_sw_if_index[rx_sw_if_index] = ~0;
284  l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index] = ~0;
285  }
286  }
287 
288  return 0;
289 }
290 #endif
291 
292 static clib_error_t *
294  unformat_input_t * input, vlib_cli_command_t * cmd)
295 {
297  unformat_input_t _line_input, *line_input = &_line_input;
298  u32 rx_sw_if_index, tx_sw_if_index;
299  int rv;
300  int rx_set = 0;
301  int tx_set = 0;
302  int is_add = 1;
303  clib_error_t *error = NULL;
304 
305  /* Get a line of input. */
306  if (!unformat_user (input, unformat_line_input, line_input))
307  return 0;
308 
309  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
310  {
311  if (unformat (line_input, "rx %U", unformat_vnet_sw_interface,
312  l2pm->vnet_main, &rx_sw_if_index))
313  rx_set = 1;
314  else if (unformat (line_input, "tx %U", unformat_vnet_sw_interface,
315  l2pm->vnet_main, &tx_sw_if_index))
316  tx_set = 1;
317  else if (unformat (line_input, "del"))
318  is_add = 0;
319  else
320  break;
321  }
322 
323  if (rx_set == 0)
324  {
325  error = clib_error_return (0, "rx interface not set");
326  goto done;
327  }
328 
329  if (tx_set == 0)
330  {
331  error = clib_error_return (0, "tx interface not set");
332  goto done;
333  }
334 
335  rv = vnet_l2_patch_add_del (rx_sw_if_index, tx_sw_if_index, is_add);
336 
337  switch (rv)
338  {
339  case 0:
340  break;
341 
342  case VNET_API_ERROR_INVALID_SW_IF_INDEX:
343  error = clib_error_return (0, "rx interface not a physical port");
344  goto done;
345 
346  case VNET_API_ERROR_INVALID_SW_IF_INDEX_2:
347  error = clib_error_return (0, "tx interface not a physical port");
348  goto done;
349 
350  default:
352  (0, "WARNING: vnet_l2_patch_add_del returned %d", rv);
353  goto done;
354  }
355 
356 
357 done:
358  unformat_free (line_input);
359 
360  return error;
361 }
362 
363 /*?
364  * Create or delete a Layer 2 patch.
365  *
366  * @cliexpar
367  * @cliexstart{test l2patch rx <intfc> tx <intfc> [del]}
368  * @cliexend
369  * @todo This is incomplete. This needs a detailed description and a
370  * practical example.
371 ?*/
372 /* *INDENT-OFF* */
374  .path = "test l2patch",
375  .short_help = "test l2patch rx <intfc> tx <intfc> [del]",
376  .function = test_patch_command_fn,
377 };
378 /* *INDENT-ON* */
379 
380 /** Display the contents of the l2patch table. */
381 static clib_error_t *
383  unformat_input_t * input, vlib_cli_command_t * cmd)
384 {
386  u32 rx_sw_if_index;
387  u32 no_entries = 1;
388 
391 
392  for (rx_sw_if_index = 0;
393  rx_sw_if_index < vec_len (l2pm->tx_sw_if_index_by_rx_sw_if_index);
394  rx_sw_if_index++)
395  {
397  l2pm->tx_sw_if_index_by_rx_sw_if_index[rx_sw_if_index];
398  if (tx_sw_if_index != ~0)
399  {
400  no_entries = 0;
401  vlib_cli_output (vm, "%26U -> %U",
403  l2pm->vnet_main, rx_sw_if_index,
405  l2pm->vnet_main, tx_sw_if_index);
406  }
407  }
408 
409  if (no_entries)
410  vlib_cli_output (vm, "no l2patch entries");
411 
412  return 0;
413 }
414 
415 /*?
416  * Show Layer 2 patch entries.
417  *
418  * @cliexpar
419  * @cliexstart{show l2patch}
420  * @cliexend
421  * @todo This is incomplete. This needs a detailed description and a
422  * practical example.
423 ?*/
424 /* *INDENT-OFF* */
426  .path = "show l2patch",
427  .short_help = "Show l2 interface cross-connect entries",
428  .function = show_l2patch,
429 };
430 /* *INDENT-ON* */
431 
432 static clib_error_t *
434 {
436 
437  mp->vlib_main = vm;
438  mp->vnet_main = vnet_get_main ();
439 
440  return 0;
441 }
442 
444 
445 /*
446  * fd.io coding-style-patch-verification: ON
447  *
448  * Local Variables:
449  * eval: (c-set-style "gnu")
450  * End:
451  */
vlib.h
l2_patch_init
static clib_error_t * l2_patch_init(vlib_main_t *vm)
Definition: l2_patch.c:433
vlib_error_main_t
Definition: error.h:61
l2_patch_error_t
l2_patch_error_t
Definition: l2_patch.c:63
l2_patch_trace_t
Definition: l2_patch.c:32
unformat_user
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:989
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
vlib_node_add_next
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1177
show_l2patch
static clib_error_t * show_l2patch(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Display the contents of the l2patch table.
Definition: l2_patch.c:382
ethernet_set_flags
u32 ethernet_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: interface.c:441
unformat_line_input
unformat_function_t unformat_line_input
Definition: format.h:275
L2_PATCH_N_ERROR
@ L2_PATCH_N_ERROR
Definition: l2_patch.c:68
l2_patch_trace
static_always_inline void l2_patch_trace(vlib_main_t *vm, vlib_node_runtime_t *node, l2_patch_main_t *l2pm, vlib_buffer_t *b, u32 sw_if_index)
Definition: l2_patch.c:84
vnet_hw_interface_t::sw_if_index
u32 sw_if_index
Definition: interface.h:670
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
next
u16 * next
Definition: nat44_ei_out2in.c:718
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
l2_patch_main
l2_patch_main_t l2_patch_main
Definition: l2_patch.c:52
clib_error_return
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_cli_command_t::path
char * path
Definition: cli.h:96
vlib_main_t::error_main
vlib_error_main_t error_main
Definition: main.h:177
l2_patch_next_t
l2_patch_next_t
Definition: l2_patch.c:77
u16
unsigned short u16
Definition: types.h:57
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
l2_patch_main_t::vlib_main
vlib_main_t * vlib_main
Definition: l2_patch.c:28
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
unformat_input_t
struct _unformat_input_t unformat_input_t
vlib_frame_t
Definition: node.h:372
ethernet.h
error
Definition: cJSON.c:88
L2_PATCH_NEXT_DROP
@ L2_PATCH_NEXT_DROP
Definition: l2_patch.c:79
l2_patch_trace_t::rx_sw_if_index
u32 rx_sw_if_index
Definition: l2_patch.c:34
test_patch_command
static vlib_cli_command_t test_patch_command
(constructor) VLIB_CLI_COMMAND (test_patch_command)
Definition: l2_patch.c:373
unformat
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
l2_patch_error_strings
static char * l2_patch_error_strings[]
Definition: l2_patch.c:71
l2_patch_main_t::tx_sw_if_index_by_rx_sw_if_index
u32 * tx_sw_if_index_by_rx_sw_if_index
Definition: l2_patch.c:25
vnet_l2_patch_add_del
int vnet_l2_patch_add_del(u32 rx_sw_if_index, u32 tx_sw_if_index, int is_add)
Definition: l2_patch.c:232
vec_len
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
Definition: vec_bootstrap.h:142
unformat_free
static void unformat_free(unformat_input_t *i)
Definition: format.h:155
vlib_error_main_t::counters
u64 * counters
Definition: error.h:64
error.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
feature.h
test_patch_command_fn
static clib_error_t * test_patch_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_patch.c:293
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vnet_get_main
vnet_main_t * vnet_get_main(void)
Definition: pnat_test_stubs.h:56
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
show_l2patch_cli
static vlib_cli_command_t show_l2patch_cli
(constructor) VLIB_CLI_COMMAND (show_l2patch_cli)
Definition: l2_patch.c:425
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
ARRAY_LEN
#define ARRAY_LEN(x)
Definition: clib.h:70
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
unformat_check_input
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:163
l2_patch_node
vlib_node_registration_t l2_patch_node
(constructor) VLIB_REGISTER_NODE (l2_patch_node)
Definition: l2_patch.c:210
l2_patch_main_t::vnet_main
vnet_main_t * vnet_main
Definition: l2_patch.c:29
static_always_inline
#define static_always_inline
Definition: clib.h:112
tx_sw_if_index
vl_api_interface_index_t tx_sw_if_index
Definition: ip.api:548
vlib_get_node
static vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Get vlib node by index.
Definition: node_funcs.h:86
VLIB_CLI_COMMAND
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:163
l2_patch_main_t
Definition: l2_patch.c:21
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
vlib_cli_output
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:716
vnet_hw_interface_t
Definition: interface.h:638
vnet_main_t
Definition: vnet.h:76
foreach_l2_patch_error
#define foreach_l2_patch_error
Definition: l2_patch.c:59
format_vnet_sw_if_index_name
format_function_t format_vnet_sw_if_index_name
Definition: interface_funcs.h:455
unformat_vnet_sw_interface
unformat_function_t unformat_vnet_sw_interface
Definition: interface_funcs.h:459
format
description fragment has unexpected format
Definition: map.api:433
ASSERT
#define ASSERT(truth)
Definition: error_bootstrap.h:69
vec_validate_init_empty
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header,...
Definition: vec.h:570
format_l2_patch_trace
static u8 * format_l2_patch_trace(u8 *s, va_list *args)
Definition: l2_patch.c:40
u32
unsigned int u32
Definition: types.h:88
VLIB_INIT_FUNCTION
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:172
vnet_get_sup_hw_interface
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
Definition: interface_funcs.h:92
l2_patch_inline
static_always_inline void l2_patch_inline(vlib_main_t *vm, vlib_node_runtime_t *node, l2_patch_main_t *l2pm, vlib_buffer_t **b, u16 *next, u32 n_left, int do_trace)
Definition: l2_patch.c:98
n_left
u32 n_left
Definition: interface_output.c:1078
l2_patch_main_t::tx_next_by_rx_sw_if_index
u32 * tx_next_by_rx_sw_if_index
Definition: l2_patch.c:24
vnet_feature_enable_disable
int vnet_feature_enable_disable(const char *arc_name, const char *node_name, u32 sw_if_index, int enable_disable, void *feature_config, u32 n_feature_config_bytes)
Definition: pnat_test_stubs.h:50
vlib_main_t
Definition: main.h:102
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
clib_error_t
Definition: clib_error.h:21
vnet_hw_interface_t::output_node_index
u32 output_node_index
Definition: interface.h:653
vlib_init_function_t
clib_error_t *() vlib_init_function_t(struct vlib_main_t *vm)
Definition: init.h:51
ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
#define ETHERNET_INTERFACE_FLAG_ACCEPT_ALL
Definition: ethernet.h:163
vlib_node_t::error_heap_index
u32 error_heap_index
Definition: node.h:315
L2_PATCH_N_NEXT
@ L2_PATCH_N_NEXT
Definition: l2_patch.c:80
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
vnet_hw_interface_t::hw_if_index
u32 hw_if_index
Definition: interface.h:667
rv
int __clib_unused rv
Definition: application.c:491
vnet.h
vlib_node_runtime_t
Definition: node.h:454
vlib_cli_command_t
Definition: cli.h:92
from
from
Definition: nat44_ei_hairpinning.c:415
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
UNFORMAT_END_OF_INPUT
#define UNFORMAT_END_OF_INPUT
Definition: format.h:137
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
l2_patch_trace_t::tx_sw_if_index
u32 tx_sw_if_index
Definition: l2_patch.c:35
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169