FD.io VPP  v16.06
Vector Packet Processing
l2_fwd.c
Go to the documentation of this file.
1 /*
2  * l2_fwd.c : layer 2 forwarding using l2fib
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/vnet.h>
20 #include <vnet/pg/pg.h>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vlib/cli.h>
23 
24 #include <vnet/l2/l2_input.h>
25 #include <vnet/l2/l2_bvi.h>
26 #include <vnet/l2/l2_fwd.h>
27 #include <vnet/l2/l2_fib.h>
28 
29 #include <vppinfra/error.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/sparse_vec.h>
32 
33 
34 typedef struct {
35 
36  // Hash table
37  BVT(clib_bihash) *mac_table;
38 
39  // next node index for the L3 input node of each ethertype
40  next_by_ethertype_t l3_next;
41 
42  /* convenience variables */
45 } l2fwd_main_t;
46 
47 typedef struct {
48  /* per-pkt trace data */
49  u8 src[6];
50  u8 dst[6];
54 
55 /* packet trace format function */
56 static u8 * format_l2fwd_trace (u8 * s, va_list * args)
57 {
58  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
59  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
60  l2fwd_trace_t * t = va_arg (*args, l2fwd_trace_t *);
61 
62  s = format (s, "l2-fwd: sw_if_index %d dst %U src %U bd_index %d",
63  t->sw_if_index,
66  t->bd_index);
67  return s;
68 }
69 
71 
73 
74 #define foreach_l2fwd_error \
75 _(L2FWD, "L2 forward packets") \
76 _(FLOOD, "L2 forward misses") \
77 _(HIT, "L2 forward hits") \
78 _(BVI_BAD_MAC, "BVI L3 MAC mismatch") \
79 _(BVI_ETHERTYPE, "BVI packet with unhandled ethertype") \
80 _(FILTER_DROP, "Filter Mac Drop") \
81 _(REFLECT_DROP, "Reflection Drop")
82 
83 typedef enum {
84 #define _(sym,str) L2FWD_ERROR_##sym,
86 #undef _
89 
90 static char * l2fwd_error_strings[] = {
91 #define _(sym,string) string,
93 #undef _
94 };
95 
96 typedef enum {
101 } l2fwd_next_t;
102 
103 // Forward one packet based on the mac table lookup result
104 
107  vlib_node_runtime_t * node,
108  l2fwd_main_t * msm,
109  vlib_error_main_t * em,
110  vlib_buffer_t * b0,
111  u32 sw_if_index0,
112  l2fib_entry_result_t * result0,
113  u32 * next0)
114 {
115  if (PREDICT_FALSE (result0->raw == ~0)) {
116  // lookup miss, so flood
117  // TODO:replicate packet to each intf in bridge-domain
118  // For now just drop
119  if (vnet_buffer(b0)->l2.feature_bitmap & L2INPUT_FEAT_UU_FLOOD) {
120  *next0 = L2FWD_NEXT_FLOOD;
121  } else {
122  // Flooding is disabled
123  b0->error = node->errors[L2FWD_ERROR_FLOOD];
124  *next0 = L2FWD_NEXT_DROP;
125  }
126 
127  } else {
128 
129  // lookup hit, forward packet
130 #ifdef COUNTERS
131  em->counters[node_counter_base_index + L2FWD_ERROR_HIT] += 1;
132 #endif
133 
134  vnet_buffer(b0)->sw_if_index[VLIB_TX] = result0->fields.sw_if_index;
135  *next0 = L2FWD_NEXT_L2_OUTPUT;
136 
137  // perform reflection check
138  if (PREDICT_FALSE (sw_if_index0 == result0->fields.sw_if_index)) {
139  b0->error = node->errors[L2FWD_ERROR_REFLECT_DROP];
140  *next0 = L2FWD_NEXT_DROP;
141 
142  // perform filter check
143  } else if (PREDICT_FALSE (result0->fields.filter)) {
144  b0->error = node->errors[L2FWD_ERROR_FILTER_DROP];
145  *next0 = L2FWD_NEXT_DROP;
146 
147  // perform BVI check
148  } else if (PREDICT_FALSE (result0->fields.bvi)) {
149  u32 rc;
150  rc = l2_to_bvi (vm,
151  msm->vnet_main,
152  b0,
153  vnet_buffer(b0)->sw_if_index[VLIB_TX],
154  &msm->l3_next,
155  next0);
156 
157  if (PREDICT_FALSE(rc)) {
158  if (rc == TO_BVI_ERR_BAD_MAC) {
159  b0->error = node->errors[L2FWD_ERROR_BVI_BAD_MAC];
160  *next0 = L2FWD_NEXT_DROP;
161  } else if (rc == TO_BVI_ERR_ETHERTYPE) {
162  b0->error = node->errors[L2FWD_ERROR_BVI_ETHERTYPE];
163  *next0 = L2FWD_NEXT_DROP;
164  }
165  }
166  }
167  }
168 }
169 
170 
171 static uword
173  vlib_node_runtime_t * node,
174  vlib_frame_t * frame)
175 {
176  u32 n_left_from, * from, * to_next;
177  l2fwd_next_t next_index;
178  l2fwd_main_t * msm = &l2fwd_main;
179  vlib_node_t *n = vlib_get_node (vm, l2fwd_node.index);
180  CLIB_UNUSED(u32 node_counter_base_index) = n->error_heap_index;
181  vlib_error_main_t * em = &vm->error_main;
182  l2fib_entry_key_t cached_key;
183  l2fib_entry_result_t cached_result;
184 
185  // Clear the one-entry cache in case mac table was updated
186  cached_key.raw = ~0;
187  cached_result.raw = ~0;
188 
189  from = vlib_frame_vector_args (frame);
190  n_left_from = frame->n_vectors; /* number of packets to process */
191  next_index = node->cached_next_index;
192 
193  while (n_left_from > 0)
194  {
195  u32 n_left_to_next;
196 
197  /* get space to enqueue frame to graph node "next_index" */
198  vlib_get_next_frame (vm, node, next_index,
199  to_next, n_left_to_next);
200 
201  while (n_left_from >= 4 && n_left_to_next >= 2)
202  {
203  u32 bi0, bi1;
204  vlib_buffer_t * b0, * b1;
205  u32 next0, next1;
206  u32 sw_if_index0, sw_if_index1;
207  ethernet_header_t * h0, * h1;
208  l2fib_entry_key_t key0, key1;
209  l2fib_entry_result_t result0, result1;
210  u32 bucket0, bucket1;
211 
212  /* Prefetch next iteration. */
213  {
214  vlib_buffer_t * p2, * p3;
215 
216  p2 = vlib_get_buffer (vm, from[2]);
217  p3 = vlib_get_buffer (vm, from[3]);
218 
219  vlib_prefetch_buffer_header (p2, LOAD);
220  vlib_prefetch_buffer_header (p3, LOAD);
221 
224  }
225 
226  /* speculatively enqueue b0 and b1 to the current next frame */
227  /* bi is "buffer index", b is pointer to the buffer */
228  to_next[0] = bi0 = from[0];
229  to_next[1] = bi1 = from[1];
230  from += 2;
231  to_next += 2;
232  n_left_from -= 2;
233  n_left_to_next -= 2;
234 
235  b0 = vlib_get_buffer (vm, bi0);
236  b1 = vlib_get_buffer (vm, bi1);
237 
238  /* RX interface handles */
239  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
240  sw_if_index1 = vnet_buffer(b1)->sw_if_index[VLIB_RX];
241 
242  h0 = vlib_buffer_get_current (b0);
243  h1 = vlib_buffer_get_current (b1);
244 
246  {
247  if (b0->flags & VLIB_BUFFER_IS_TRACED)
248  {
249  l2fwd_trace_t *t =
250  vlib_add_trace (vm, node, b0, sizeof (*t));
251  t->sw_if_index = sw_if_index0;
252  t->bd_index = vnet_buffer(b0)->l2.bd_index;
253  clib_memcpy(t->src, h0->src_address, 6);
254  clib_memcpy(t->dst, h0->dst_address, 6);
255  }
256  if (b1->flags & VLIB_BUFFER_IS_TRACED)
257  {
258  l2fwd_trace_t *t =
259  vlib_add_trace (vm, node, b1, sizeof (*t));
260  t->sw_if_index = sw_if_index1;
261  t->bd_index = vnet_buffer(b1)->l2.bd_index;
262  clib_memcpy(t->src, h1->src_address, 6);
263  clib_memcpy(t->dst, h1->dst_address, 6);
264  }
265  }
266 
267  /* process 2 pkts */
268 #ifdef COUNTERS
269  em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 2;
270 #endif
271  l2fib_lookup_2 (msm->mac_table, &cached_key, &cached_result,
272  h0->dst_address,
273  h1->dst_address,
274  vnet_buffer(b0)->l2.bd_index,
275  vnet_buffer(b1)->l2.bd_index,
276  &key0, // not used
277  &key1, // not used
278  &bucket0, // not used
279  &bucket1, // not used
280  &result0,
281  &result1);
282  l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, &next0);
283  l2fwd_process (vm, node, msm, em, b1, sw_if_index1, &result1, &next1);
284 
285  /* verify speculative enqueues, maybe switch current next frame */
286  /* if next0==next1==next_index then nothing special needs to be done */
287  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
288  to_next, n_left_to_next,
289  bi0, bi1, next0, next1);
290  }
291 
292  while (n_left_from > 0 && n_left_to_next > 0)
293  {
294  u32 bi0;
295  vlib_buffer_t * b0;
296  u32 next0;
297  u32 sw_if_index0;
298  ethernet_header_t * h0;
299  l2fib_entry_key_t key0;
300  l2fib_entry_result_t result0;
301  u32 bucket0;
302 
303  /* speculatively enqueue b0 to the current next frame */
304  bi0 = from[0];
305  to_next[0] = bi0;
306  from += 1;
307  to_next += 1;
308  n_left_from -= 1;
309  n_left_to_next -= 1;
310 
311  b0 = vlib_get_buffer (vm, bi0);
312 
313  sw_if_index0 = vnet_buffer(b0)->sw_if_index[VLIB_RX];
314 
315  h0 = vlib_buffer_get_current (b0);
316 
318  && (b0->flags & VLIB_BUFFER_IS_TRACED))) {
319  l2fwd_trace_t *t =
320  vlib_add_trace (vm, node, b0, sizeof (*t));
321  t->sw_if_index = sw_if_index0;
322  t->bd_index = vnet_buffer(b0)->l2.bd_index;
323  clib_memcpy(t->src, h0->src_address, 6);
324  clib_memcpy(t->dst, h0->dst_address, 6);
325  }
326 
327  /* process 1 pkt */
328 #ifdef COUNTERS
329  em->counters[node_counter_base_index + L2FWD_ERROR_L2FWD] += 1;
330 #endif
331  l2fib_lookup_1 (msm->mac_table, &cached_key, &cached_result,
332  h0->dst_address, vnet_buffer(b0)->l2.bd_index,
333  &key0, // not used
334  &bucket0, // not used
335  &result0);
336  l2fwd_process (vm, node, msm, em, b0, sw_if_index0, &result0, &next0);
337 
338  /* verify speculative enqueue, maybe switch current next frame */
339  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
340  to_next, n_left_to_next,
341  bi0, next0);
342  }
343 
344  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
345  }
346 
347  return frame->n_vectors;
348 }
349 
350 VLIB_REGISTER_NODE (l2fwd_node,static) = {
351  .function = l2fwd_node_fn,
352  .name = "l2-fwd",
353  .vector_size = sizeof (u32),
354  .format_trace = format_l2fwd_trace,
356 
357  .n_errors = ARRAY_LEN(l2fwd_error_strings),
358  .error_strings = l2fwd_error_strings,
359 
360  .n_next_nodes = L2FWD_N_NEXT,
361 
362  /* edit / add dispositions here */
363  .next_nodes = {
364  [L2FWD_NEXT_L2_OUTPUT] = "l2-output",
365  [L2FWD_NEXT_FLOOD] = "l2-flood",
366  [L2FWD_NEXT_DROP] = "error-drop",
367  },
368 };
369 
371 {
372  l2fwd_main_t * mp = &l2fwd_main;
373 
374  mp->vlib_main = vm;
375  mp->vnet_main = vnet_get_main();
376 
377  /* init the hash table ptr */
378  mp->mac_table = get_mac_table();
379 
380  // Initialize the next nodes for each ethertype
381  next_by_ethertype_init (&mp->l3_next);
382 
383  return 0;
384 }
385 
387 
388 
389 // Add the L3 input node for this ethertype to the next nodes structure
390 void
393  u32 node_index)
394 {
395  l2fwd_main_t * mp = &l2fwd_main;
396  u32 next_index;
397 
398  next_index = vlib_node_add_next (vm,
399  l2fwd_node.index,
400  node_index);
401 
402  next_by_ethertype_register (&mp->l3_next, type, next_index);
403 }
404 
405 
406 // set subinterface forward enable/disable
407 // The CLI format is:
408 // set interface l2 forward <interface> [disable]
409 static clib_error_t *
411  unformat_input_t * input,
412  vlib_cli_command_t * cmd)
413 {
414  vnet_main_t * vnm = vnet_get_main();
415  clib_error_t * error = 0;
416  u32 sw_if_index;
417  u32 enable;
418 
419  if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
420  {
421  error = clib_error_return (0, "unknown interface `%U'",
422  format_unformat_error, input);
423  goto done;
424  }
425 
426  enable = 1;
427  if (unformat (input, "disable")) {
428  enable = 0;
429  }
430 
431  // set the interface flag
432  if (l2input_intf_config(sw_if_index)->xconnect) {
433  l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_XCONNECT, enable);
434  } else {
435  l2input_intf_bitmap_enable(sw_if_index, L2INPUT_FEAT_FWD, enable);
436  }
437 
438  done:
439  return error;
440 }
441 
442 VLIB_CLI_COMMAND (int_fwd_cli, static) = {
443  .path = "set interface l2 forward",
444  .short_help = "set interface l2 forward <interface> [disable]",
445  .function = int_fwd,
446 };
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Definition: main.c:459
u32 error_heap_index
Definition: node.h:244
#define CLIB_UNUSED(x)
Definition: clib.h:79
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
u16 bd_index
Definition: l2_fwd.c:52
#define TO_BVI_ERR_BAD_MAC
Definition: l2_bvi.h:28
always_inline vlib_node_t * vlib_get_node(vlib_main_t *vm, u32 i)
Definition: node_funcs.h:46
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
l2_input_config_t * l2input_intf_config(u32 sw_if_index)
Definition: l2_input.c:464
ethernet_type_t
Definition: packet.h:43
#define TO_BVI_ERR_ETHERTYPE
Definition: l2_bvi.h:29
static_always_inline u32 l2_to_bvi(vlib_main_t *vlib_main, vnet_main_t *vnet_main, vlib_buffer_t *b0, u32 bvi_sw_if_index, next_by_ethertype_t *l3_next, u32 *next0)
Definition: l2_bvi.h:36
u8 src_address[6]
Definition: packet.h:52
static uword l2fwd_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: l2_fwd.c:172
clib_error_t * l2fwd_init(vlib_main_t *vm)
Definition: l2_fwd.c:370
struct _vlib_node_registration vlib_node_registration_t
unformat_function_t unformat_vnet_sw_interface
Definition: l2_fib.h:50
vlib_error_t * errors
Definition: node.h:378
always_inline void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:184
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
#define static_always_inline
Definition: clib.h:85
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:43
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
u8 dst_address[6]
Definition: packet.h:51
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:1538
static_always_inline void l2fwd_process(vlib_main_t *vm, vlib_node_runtime_t *node, l2fwd_main_t *msm, vlib_error_main_t *em, vlib_buffer_t *b0, u32 sw_if_index0, l2fib_entry_result_t *result0, u32 *next0)
Definition: l2_fwd.c:106
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
always_inline void * vlib_frame_vector_args(vlib_frame_t *f)
Definition: node_funcs.h:202
static clib_error_t * int_fwd(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fwd.c:410
vlib_error_main_t error_main
Definition: main.h:124
void l2fwd_register_input_type(vlib_main_t *vm, ethernet_type_t type, u32 node_index)
Definition: l2_fwd.c:391
static char * l2fwd_error_strings[]
Definition: l2_fwd.c:90
static u8 * format_l2fwd_trace(u8 *s, va_list *args)
Definition: l2_fwd.c:56
#define PREDICT_FALSE(x)
Definition: clib.h:97
vnet_main_t vnet_main
Definition: misc.c:42
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Definition: buffer_node.h:43
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Definition: buffer_node.h:83
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:265
l2fwd_main_t l2fwd_main
Definition: l2_fwd.c:70
u64 raw
Definition: l2_fib.h:63
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:129
u64 * counters
Definition: error.h:73
clib_error_t * next_by_ethertype_register(next_by_ethertype_t *l3_next, u32 ethertype, u32 next_index)
Definition: node.c:1010
u16 n_vectors
Definition: node.h:307
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:82
static vlib_node_registration_t l2fwd_node
(constructor) VLIB_REGISTER_NODE (l2fwd_node)
Definition: l2_fwd.c:72
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define ARRAY_LEN(x)
Definition: clib.h:59
l2fwd_error_t
Definition: l2_fwd.c:83
static_always_inline void l2fib_lookup_2(BVT(clib_bihash)*mac_table, l2fib_entry_key_t *cached_key, l2fib_entry_result_t *cached_result, u8 *mac0, u8 *mac1, u16 bd_index0, u16 bd_index1, l2fib_entry_key_t *key0, l2fib_entry_key_t *key1, u32 *bucket0, u32 *bucket1, l2fib_entry_result_t *result0, l2fib_entry_result_t *result1)
Definition: l2_fib.h:161
struct l2fib_entry_result_t::@128::@130 fields
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
u32 sw_if_index
Definition: l2_fwd.c:51
u16 cached_next_index
Definition: node.h:422
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
#define vnet_buffer(b)
Definition: buffer.h:300
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
Definition: l2_fib.h:33
#define foreach_l2fwd_error
Definition: l2_fwd.c:74
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:225
u8 src[6]
Definition: l2_fwd.c:49
l2fwd_next_t
Definition: l2_fwd.c:96
always_inline uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:919
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:91
u64 uword
Definition: types.h:112
u32 l2input_intf_bitmap_enable(u32 sw_if_index, u32 feature_bitmap, u32 enable)
Definition: l2_input.c:473
Definition: defs.h:46
unsigned short u16
Definition: types.h:57
#define BVT(a)
unsigned char u8
Definition: types.h:56
always_inline void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
clib_error_t * next_by_ethertype_init(next_by_ethertype_t *l3_next)
Definition: node.c:983
u8 dst[6]
Definition: l2_fwd.c:50
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:162
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:140
u8 data[0]
Packet data.
Definition: buffer.h:150
static_always_inline void l2fib_lookup_1(BVT(clib_bihash)*mac_table, l2fib_entry_key_t *cached_key, l2fib_entry_result_t *cached_result, u8 *mac0, u16 bd_index0, l2fib_entry_key_t *key0, u32 *bucket0, l2fib_entry_result_t *result0)
Definition: l2_fib.h:118
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:84
always_inline vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
Definition: defs.h:45
u64 raw
Definition: l2_fib.h:43