FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
l2_input_node.c
Go to the documentation of this file.
1 /*
2  * l2_input.c : layer 2 input packet processing
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 <vnet/ethernet/packet.h>
23 #include <vnet/ip/ip4.h>
24 #include <vnet/ip/ip6.h>
25 #include <vnet/fib/fib_node.h>
27 #include <vlib/cli.h>
28 #include <vnet/l2/l2_input.h>
29 #include <vnet/l2/l2_output.h>
30 #include <vnet/l2/feat_bitmap.h>
31 #include <vnet/l2/l2_bvi.h>
32 #include <vnet/l2/l2_fib.h>
33 #include <vnet/l2/l2_bd.h>
34 
35 #include <vppinfra/error.h>
36 #include <vppinfra/hash.h>
37 #include <vppinfra/cache.h>
38 
39 /**
40  * @file
41  * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3).
42  *
43  * This file contains the CLI Commands that modify the input mode of an
44  * interface. For interfaces in a Layer 2 cross-connect, all packets
45  * received on one interface will be transmitted to the other. For
46  * interfaces in a bridge-domain, packets will be forwarded to other
47  * interfaces in the same bridge-domain based on destination mac address.
48  * For interfaces in Layer 3 mode, the packets will be routed.
49  */
50 
51 typedef struct
52 {
53  /* per-pkt trace data */
54  u8 dst_and_src[12];
58 
59 /* packet trace format function */
60 static u8 *
61 format_l2input_trace (u8 * s, va_list * args)
62 {
63  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
64  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
65  l2input_trace_t *t = va_arg (*args, l2input_trace_t *);
66 
67  s = format (s, "l2-input: sw_if_index %d dst %U src %U [%U]",
68  t->sw_if_index,
72  return s;
73 }
74 
76 
77 #ifndef CLIB_MARCH_VARIANT
79 #endif /* CLIB_MARCH_VARIANT */
80 
81 #define foreach_l2input_error \
82 _(L2INPUT, "L2 input packets") \
83 _(DROP, "L2 input drops")
84 
85 typedef enum
86 {
87 #define _(sym,str) L2INPUT_ERROR_##sym,
89 #undef _
92 
93 static char *l2input_error_strings[] = {
94 #define _(sym,string) string,
96 #undef _
97 };
98 
99 typedef enum
100 { /* */
106 
109 {
110  /*
111  * Load L2 input feature struct
112  * Load bridge domain struct
113  * Parse ethernet header to determine unicast/mcast/broadcast
114  * take L2 input stat
115  * classify packet as IP/UDP/TCP, control, other
116  * mask feature bitmap
117  * go to first node in bitmap
118  * Later: optimize VTM
119  *
120  * For L2XC,
121  * set tx sw-if-handle
122  */
123 
124  u32 feat_mask = ~0;
125  u32 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
127 
128  /* Get config for the input interface */
129  l2_input_config_t *config = vec_elt_at_index (msm->configs, sw_if_index0);
130 
131  /* Save split horizon group */
132  vnet_buffer (b0)->l2.shg = config->shg;
133 
134  /* determine layer2 kind for stat and mask */
136  {
137  u8 *l3h0 = (u8 *) h0 + vnet_buffer (b0)->l2.l2_len;
138 
139 #define get_u16(addr) ( *((u16 *)(addr)) )
140  u16 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
141  u8 protocol = ((ip6_header_t *) l3h0)->protocol;
142 
143  /* Disable bridge forwarding (flooding will execute instead if not xconnect) */
144  feat_mask &= ~(L2INPUT_FEAT_FWD |
145  L2INPUT_FEAT_UU_FLOOD |
146  L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_GBP_FWD);
147 
148  if (ethertype != ETHERNET_TYPE_ARP)
149  feat_mask &= ~(L2INPUT_FEAT_ARP_UFWD);
150 
151  /* Disable ARP-term for non-ARP and non-ICMP6 packet */
152  if (ethertype != ETHERNET_TYPE_ARP &&
153  (ethertype != ETHERNET_TYPE_IP6 || protocol != IP_PROTOCOL_ICMP6))
154  feat_mask &= ~(L2INPUT_FEAT_ARP_TERM);
155  /*
156  * For packet from BVI - set SHG of ARP request or ICMPv6 neighbor
157  * solicitation packet from BVI to 0 so it can also flood to VXLAN
158  * tunnels or other ports with the same SHG as that of the BVI.
159  */
160  else if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] ==
161  L2INPUT_BVI))
162  {
163  if (ethertype == ETHERNET_TYPE_ARP)
164  {
166  if (arp0->opcode ==
167  clib_host_to_net_u16 (ETHERNET_ARP_OPCODE_request))
168  vnet_buffer (b0)->l2.shg = 0;
169  }
170  else /* must be ICMPv6 */
171  {
172  ip6_header_t *iph0 = (ip6_header_t *) l3h0;
173  icmp6_neighbor_solicitation_or_advertisement_header_t *ndh0;
174  ndh0 = ip6_next_header (iph0);
175  if (ndh0->icmp.type == ICMP6_neighbor_solicitation)
176  vnet_buffer (b0)->l2.shg = 0;
177  }
178  }
179  }
180  else
181  {
182  /*
183  * For packet from BVI - set SHG of unicast packet from BVI to 0 so it
184  * is not dropped on output to VXLAN tunnels or other ports with the
185  * same SHG as that of the BVI.
186  */
188  L2INPUT_BVI))
189  vnet_buffer (b0)->l2.shg = 0;
190  }
191 
192 
193  if (l2_input_is_bridge (config))
194  {
195  /* Do bridge-domain processing */
196  /* save BD ID for next feature graph nodes */
197  vnet_buffer (b0)->l2.bd_index = config->bd_index;
198 
199  /* Save bridge domain and interface seq_num */
200  vnet_buffer (b0)->l2.l2fib_sn = l2_fib_mk_seq_num
201  (config->bd_seq_num, config->seq_num);
202  vnet_buffer (b0)->l2.bd_age = config->bd_mac_age;
203 
204  /*
205  * Process bridge domain feature enables.
206  * To perform learning/flooding/forwarding, the corresponding bit
207  * must be enabled in both the input interface config and in the
208  * bridge domain config. In the bd_bitmap, bits for features other
209  * than learning/flooding/forwarding should always be set.
210  */
211  feat_mask = feat_mask & config->bd_feature_bitmap;
212  }
213  else if (l2_input_is_xconnect (config))
214  {
215  /* Set the output interface */
216  vnet_buffer (b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index;
217  }
218  else
219  feat_mask = L2INPUT_FEAT_DROP;
220 
221  /* mask out features from bitmap using packet type and bd config */
222  u32 feature_bitmap = config->feature_bitmap & feat_mask;
223 
224  /* save for next feature graph nodes */
225  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
226 
227  /* Determine the next node */
229  feature_bitmap);
230 }
231 
235  int do_trace)
236 {
237  u32 n_left, *from;
241 
243  n_left = frame->n_vectors; /* number of packets to process */
244 
246 
247  while (n_left > 0)
248  {
249  while (n_left >= 8)
250  {
251  u32 sw_if_index0, sw_if_index1, sw_if_index2, sw_if_index3;
252 
253  /* Prefetch next iteration. */
254  {
255  /* Prefetch the buffer header and packet for the N+2 loop iteration */
256  vlib_prefetch_buffer_header (b[4], LOAD);
257  vlib_prefetch_buffer_header (b[5], LOAD);
258  vlib_prefetch_buffer_header (b[6], LOAD);
259  vlib_prefetch_buffer_header (b[7], LOAD);
260 
265  }
266 
267  classify_and_dispatch (msm, b[0], &next[0]);
268  classify_and_dispatch (msm, b[1], &next[1]);
269  classify_and_dispatch (msm, b[2], &next[2]);
270  classify_and_dispatch (msm, b[3], &next[3]);
271 
272  if (do_trace)
273  {
274  /* RX interface handles */
275  sw_if_index0 = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
276  sw_if_index1 = vnet_buffer (b[1])->sw_if_index[VLIB_RX];
277  sw_if_index2 = vnet_buffer (b[2])->sw_if_index[VLIB_RX];
278  sw_if_index3 = vnet_buffer (b[3])->sw_if_index[VLIB_RX];
279 
280  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
281  {
283  l2input_trace_t *t =
284  vlib_add_trace (vm, node, b[0], sizeof (*t));
285  t->sw_if_index = sw_if_index0;
286  t->feat_mask = vnet_buffer (b[0])->l2.feature_bitmap;
288  sizeof (h0->dst_address) +
289  sizeof (h0->src_address));
290  }
291  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
292  {
294  l2input_trace_t *t =
295  vlib_add_trace (vm, node, b[1], sizeof (*t));
296  t->sw_if_index = sw_if_index1;
297  t->feat_mask = vnet_buffer (b[1])->l2.feature_bitmap;
299  sizeof (h1->dst_address) +
300  sizeof (h1->src_address));
301  }
302  if (b[2]->flags & VLIB_BUFFER_IS_TRACED)
303  {
305  l2input_trace_t *t =
306  vlib_add_trace (vm, node, b[2], sizeof (*t));
307  t->sw_if_index = sw_if_index2;
308  t->feat_mask = vnet_buffer (b[2])->l2.feature_bitmap;
310  sizeof (h2->dst_address) +
311  sizeof (h2->src_address));
312  }
313  if (b[3]->flags & VLIB_BUFFER_IS_TRACED)
314  {
316  l2input_trace_t *t =
317  vlib_add_trace (vm, node, b[3], sizeof (*t));
318  t->sw_if_index = sw_if_index3;
319  t->feat_mask = vnet_buffer (b[3])->l2.feature_bitmap;
321  sizeof (h3->dst_address) +
322  sizeof (h3->src_address));
323  }
324  }
325 
326  b += 4;
327  n_left -= 4;
328  next += 4;
329  }
330 
331  while (n_left > 0)
332  {
333  classify_and_dispatch (msm, b[0], &next[0]);
334 
335  if (do_trace && PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
336  {
338  l2input_trace_t *t =
339  vlib_add_trace (vm, node, b[0], sizeof (*t));
340  t->sw_if_index = vnet_buffer (b[0])->sw_if_index[VLIB_RX];
341  t->feat_mask = vnet_buffer (b[0])->l2.feature_bitmap;
343  sizeof (h0->dst_address) +
344  sizeof (h0->src_address));
345  }
346 
347  b += 1;
348  next += 1;
349  n_left -= 1;
350  }
351  }
352 
354  L2INPUT_ERROR_L2INPUT, frame->n_vectors);
355 
357 
358  return frame->n_vectors;
359 }
360 
363 {
364  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
365  return l2input_node_inline (vm, node, frame, 1 /* do_trace */ );
366  return l2input_node_inline (vm, node, frame, 0 /* do_trace */ );
367 }
368 
369 /* *INDENT-OFF* */
371  .name = "l2-input",
372  .vector_size = sizeof (u32),
373  .format_trace = format_l2input_trace,
374  .format_buffer = format_ethernet_header_with_length,
376 
377  .n_errors = ARRAY_LEN(l2input_error_strings),
378  .error_strings = l2input_error_strings,
379 
380  .n_next_nodes = L2INPUT_N_NEXT,
381 
382  /* edit / add dispositions here */
383  .next_nodes = {
384  [L2INPUT_NEXT_LEARN] = "l2-learn",
385  [L2INPUT_NEXT_FWD] = "l2-fwd",
386  [L2INPUT_NEXT_DROP] = "error-drop",
387  },
388 };
389 /* *INDENT-ON* */
390 
391 /*
392  * fd.io coding-style-patch-verification: ON
393  *
394  * Local Variables:
395  * eval: (c-set-style "gnu")
396  * End:
397  */
vlib.h
l2input_node
vlib_node_registration_t l2input_node
(constructor) VLIB_REGISTER_NODE (l2input_node)
Definition: l2_input_node.c:370
ethernet_arp_header_t::opcode
u16 opcode
Definition: arp_packet.h:139
L2INPUT_NEXT_DROP
@ L2INPUT_NEXT_DROP
Definition: l2_input_node.c:103
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
l2_input_config_t
Definition: l2_input.h:42
l2input_main_t
Definition: l2_input.h:83
l2_input_config_t::bd_index
u16 bd_index
Definition: l2_input.h:51
pg.h
l2input_error_t
l2input_error_t
Definition: l2_input_node.c:85
l2input_trace_t::feat_mask
u32 feat_mask
Definition: l2_input_node.c:56
format_ethernet_address
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:44
ethernet_header_t::dst_address
u8 dst_address[6]
Definition: packet.h:55
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
ethernet_header_t::src_address
u8 src_address[6]
Definition: packet.h:56
next
u16 * next
Definition: nat44_ei_out2in.c:718
l2input_node_inline
static_always_inline uword l2input_node_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, int do_trace)
Definition: l2_input_node.c:233
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
format_l2_input_feature_bitmap
u8 * format_l2_input_feature_bitmap(u8 *s, va_list *args)
Definition: l2_input.c:65
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_input_is_xconnect
static bool l2_input_is_xconnect(const l2_input_config_t *input)
Definition: l2_input.h:240
u16
unsigned short u16
Definition: types.h:57
l2_input_config_t::output_sw_if_index
u32 output_sw_if_index
Definition: l2_input.h:56
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
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
fib_node.h
ip6_next_header
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:407
vlib_frame_t
Definition: node.h:372
clib_memcpy_fast
static_always_inline void * clib_memcpy_fast(void *restrict dst, const void *restrict src, size_t n)
Definition: string.h:92
ethernet.h
l2input_main
l2input_main_t l2input_main
Definition: l2_input_node.c:78
l2_fib.h
ethernet_arp_header_t
Definition: arp_packet.h:133
L2INPUT_NEXT_LEARN
@ L2INPUT_NEXT_LEARN
Definition: l2_input_node.c:101
CLIB_PREFETCH
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
l2input_next_t
l2input_next_t
Definition: l2_input_node.c:99
l2_input_config_t::bd_seq_num
u8 bd_seq_num
Definition: l2_input.h:52
packet.h
error.h
VLIB_NODE_FN
#define VLIB_NODE_FN(node)
Definition: node.h:202
l2_output.h
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vec_elt_at_index
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
Definition: vec_bootstrap.h:203
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
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
static_always_inline
#define static_always_inline
Definition: clib.h:112
l2_bvi.h
uword
u64 uword
Definition: types.h:112
ethernet_header_t
Definition: packet.h:52
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
l2_input_config_t::bd_mac_age
u8 bd_mac_age
Definition: l2_input.h:53
l2_input_config_t::seq_num
u8 seq_num
Definition: l2_input.h:70
l2_input_config_t::shg
u8 shg
Definition: l2_input.h:67
L2INPUT_NEXT_FWD
@ L2INPUT_NEXT_FWD
Definition: l2_input_node.c:102
l2_input_is_bridge
static bool l2_input_is_bridge(const l2_input_config_t *input)
Definition: l2_input.h:234
l2input_error_strings
static char * l2input_error_strings[]
Definition: l2_input_node.c:93
l2input_trace_t::dst_and_src
u8 dst_and_src[12]
Definition: l2_input_node.c:54
CLIB_CACHE_LINE_BYTES
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
l2input_main_t::feat_next_node_index
u32 feat_next_node_index[32]
Definition: l2_input.h:87
get_u16
#define get_u16(addr)
l2_input.h
data
u8 data[128]
Definition: ipsec_types.api:92
l2input_trace_t::sw_if_index
u32 sw_if_index
Definition: l2_input_node.c:55
l2input_trace_t
Definition: l2_input_node.c:51
format
description fragment has unexpected format
Definition: map.api:433
cache.h
ethernet_address_cast
static uword ethernet_address_cast(const u8 *a)
Definition: packet.h:67
u32
unsigned int u32
Definition: types.h:88
l2_fib_mk_seq_num
static_always_inline l2fib_seq_num_t l2_fib_mk_seq_num(u8 bd_sn, u8 if_sn)
Definition: l2_fib.h:110
l2_input_config_t::feature_bitmap
u32 feature_bitmap
Definition: l2_input.h:60
format_ethernet_header_with_length
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
protocol
vl_api_ip_proto_t protocol
Definition: lb_types.api:72
cli.h
n_left
u32 n_left
Definition: interface_output.c:1078
ip6_header_t
Definition: ip6_packet.h:294
feat_bitmap_get_next_node_index
static u32 feat_bitmap_get_next_node_index(u32 *next_nodes, u32 bitmap)
Return the graph node index for the feature corresponding to the first set bit in the bitmap.
Definition: feat_bitmap.h:79
ip4.h
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
hash.h
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
feat_bitmap.h
foreach_l2input_error
#define foreach_l2input_error
Definition: l2_input_node.c:81
arp_packet.h
format_l2input_trace
static u8 * format_l2input_trace(u8 *s, va_list *args)
Definition: l2_input_node.c:61
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
vnet.h
vlib_node_runtime_t
Definition: node.h:454
l2input_main_t::configs
l2_input_config_t * configs
Definition: l2_input.h:90
from
from
Definition: nat44_ei_hairpinning.c:415
sw_if_index
vl_api_interface_index_t sw_if_index
Definition: wireguard.api:34
L2INPUT_N_ERROR
@ L2INPUT_N_ERROR
Definition: l2_input_node.c:90
ip6.h
l2_bd.h
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
l2_input_config_t::bd_feature_bitmap
u32 bd_feature_bitmap
Definition: l2_input.h:64
classify_and_dispatch
static_always_inline void classify_and_dispatch(l2input_main_t *msm, vlib_buffer_t *b0, u16 *next0)
Definition: l2_input_node.c:108
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
L2INPUT_BVI
#define L2INPUT_BVI
Definition: l2_input.h:125
L2INPUT_N_NEXT
@ L2INPUT_N_NEXT
Definition: l2_input_node.c:104
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105