2 * l2_input.c : layer 2 input packet processing
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/ip_packet.h>
24 #include <vnet/ip/ip4_packet.h>
25 #include <vnet/ip/ip6_packet.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/l2_output.h>
29 #include <vnet/l2/feat_bitmap.h>
30 #include <vnet/l2/l2_bvi.h>
31 #include <vnet/l2/l2_fib.h>
33 #include <vppinfra/error.h>
34 #include <vppinfra/hash.h>
35 #include <vppinfra/cache.h>
39 * @brief Interface Input Mode (Layer 2 Cross-Connect or Bridge / Layer 3).
41 * This file contains the CLI Commands that modify the input mode of an
42 * interface. For interfaces in a Layer 2 cross-connect, all packets
43 * received on one interface will be transmitted to the other. For
44 * interfaces in a bridge-domain, packets will be forwarded to other
45 * interfaces in the same bridge-domain based on destination mac address.
46 * For interfaces in Layer 3 mode, the packets will be routed.
49 /* Feature graph node names */
50 static char *l2input_feat_names[] = {
51 #define _(sym,name) name,
57 l2input_get_feat_names (void)
59 return l2input_feat_names;
65 /* per-pkt trace data */
72 /* packet trace format function */
74 format_l2input_trace (u8 * s, va_list * args)
76 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
77 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
78 l2input_trace_t *t = va_arg (*args, l2input_trace_t *);
80 s = format (s, "l2-input: sw_if_index %d dst %U src %U",
82 format_ethernet_address, t->dst,
83 format_ethernet_address, t->src);
87 l2input_main_t l2input_main;
89 #define foreach_l2input_error \
90 _(L2INPUT, "L2 input packets") \
91 _(DROP, "L2 input drops")
95 #define _(sym,str) L2INPUT_ERROR_##sym,
101 static char *l2input_error_strings[] = {
102 #define _(sym,string) string,
103 foreach_l2input_error
116 static_always_inline void
117 classify_and_dispatch (vlib_main_t * vm,
118 vlib_node_runtime_t * node,
120 l2input_main_t * msm, vlib_buffer_t * b0, u32 * next0)
123 * Load L2 input feature struct
124 * Load bridge domain struct
125 * Parse ethernet header to determine unicast/mcast/broadcast
127 * classify packet as IP/UDP/TCP, control, other
128 * mask feature bitmap
129 * go to first node in bitmap
130 * Later: optimize VTM
133 * set tx sw-if-handle
137 __attribute__ ((unused)) u8 l2bcast;
138 __attribute__ ((unused)) u8 l2mcast;
139 __attribute__ ((unused)) u8 l2_stat_kind;
142 l2_input_config_t *config;
143 l2_bridge_domain_t *bd_config;
147 ethernet_header_t *h0;
151 #define get_u32(addr) ( *((u32 *)(addr)) )
152 #define get_u16(addr) ( *((u16 *)(addr)) )
153 #define STATS_IF_LAYER2_UCAST_INPUT_CNT 0
154 #define STATS_IF_LAYER2_MCAST_INPUT_CNT 1
155 #define STATS_IF_LAYER2_BCAST_INPUT_CNT 2
157 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
159 h0 = vlib_buffer_get_current (b0);
160 l3h0 = (u8 *) h0 + vnet_buffer (b0)->l2.l2_len;
163 * Determine L3 packet type. Only need to check the common types.
164 * Used to filter out features that don't apply to common packets.
166 ethertype = clib_net_to_host_u16 (get_u16 (l3h0 - 2));
167 if (ethertype == ETHERNET_TYPE_IP4)
169 protocol = ((ip4_header_t *) l3h0)->protocol;
170 if ((protocol == IP_PROTOCOL_UDP) || (protocol == IP_PROTOCOL_TCP))
172 feat_mask = IP_UDP_TCP_FEAT_MASK;
176 feat_mask = IP4_FEAT_MASK;
179 else if (ethertype == ETHERNET_TYPE_IP6)
181 protocol = ((ip6_header_t *) l3h0)->protocol;
182 /* Don't bother checking for extension headers for now */
183 if ((protocol == IP_PROTOCOL_UDP) || (protocol == IP_PROTOCOL_TCP))
185 feat_mask = IP_UDP_TCP_FEAT_MASK;
189 feat_mask = IP6_FEAT_MASK;
192 else if (ethertype == ETHERNET_TYPE_MPLS_UNICAST)
194 feat_mask = IP6_FEAT_MASK;
198 /* allow all features */
202 /* determine layer2 kind for stat and mask */
203 mcast_dmac = ethernet_address_cast (h0->dst_address);
206 l2_stat_kind = STATS_IF_LAYER2_UCAST_INPUT_CNT;
207 if (PREDICT_FALSE (mcast_dmac))
209 u32 *dsthi = (u32 *) & h0->dst_address[0];
210 u32 *dstlo = (u32 *) & h0->dst_address[2];
211 protocol = ((ip6_header_t *) l3h0)->protocol;
213 /* Disable bridge forwarding (flooding will execute instead if not xconnect) */
214 feat_mask &= ~(L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD);
216 /* Disable ARP-term for non-ARP and non-ICMP6 packet */
217 if (ethertype != ETHERNET_TYPE_ARP &&
218 (ethertype != ETHERNET_TYPE_IP6 || protocol != IP_PROTOCOL_ICMP6))
219 feat_mask &= ~(L2INPUT_FEAT_ARP_TERM);
221 /* dest mac is multicast or broadcast */
222 if ((*dstlo == 0xFFFFFFFF) && (*dsthi == 0xFFFFFFFF))
224 /* dest mac == FF:FF:FF:FF:FF:FF */
225 l2_stat_kind = STATS_IF_LAYER2_BCAST_INPUT_CNT;
230 l2_stat_kind = STATS_IF_LAYER2_MCAST_INPUT_CNT;
234 /* TODO: take l2 stat */
236 /* Get config for the input interface */
237 config = vec_elt_at_index (msm->configs, sw_if_index0);
239 /* Save split horizon group */
240 vnet_buffer (b0)->l2.shg = config->shg;
242 if (config->xconnect)
244 /* Set the output interface */
245 vnet_buffer (b0)->sw_if_index[VLIB_TX] = config->output_sw_if_index;
252 * Check for from-BVI processing, TX is non-~0 if from BVI loopback
253 * Set SHG for BVI packets to 0 so it is not dropped for VXLAN tunnels
255 if (PREDICT_FALSE (vnet_buffer (b0)->sw_if_index[VLIB_TX] != ~0))
257 vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
258 vnet_buffer (b0)->l2.shg = 0;
261 /* Do bridge-domain processing */
262 bd_index0 = config->bd_index;
263 /* save BD ID for next feature graph nodes */
264 vnet_buffer (b0)->l2.bd_index = bd_index0;
266 /* Get config for the bridge domain interface */
267 bd_config = vec_elt_at_index (msm->bd_configs, bd_index0);
270 * Process bridge domain feature enables.
271 * To perform learning/flooding/forwarding, the corresponding bit
272 * must be enabled in both the input interface config and in the
273 * bridge domain config. In the bd_bitmap, bits for features other
274 * than learning/flooding/forwarding should always be set.
276 feat_mask = feat_mask & bd_config->feature_bitmap;
279 /* mask out features from bitmap using packet type and bd config */
280 feature_bitmap = config->feature_bitmap & feat_mask;
282 /* save for next feature graph nodes */
283 vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
285 /* Determine the next node */
286 *next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
292 l2input_node_fn (vlib_main_t * vm,
293 vlib_node_runtime_t * node, vlib_frame_t * frame)
295 u32 n_left_from, *from, *to_next;
296 l2input_next_t next_index;
297 l2input_main_t *msm = &l2input_main;
298 vlib_node_t *n = vlib_get_node (vm, l2input_node.index);
299 u32 node_counter_base_index = n->error_heap_index;
300 vlib_error_main_t *em = &vm->error_main;
301 u32 cpu_index = os_get_cpu_number ();
303 from = vlib_frame_vector_args (frame);
304 n_left_from = frame->n_vectors; /* number of packets to process */
305 next_index = node->cached_next_index;
307 while (n_left_from > 0)
311 /* get space to enqueue frame to graph node "next_index" */
312 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
314 while (n_left_from >= 6 && n_left_to_next >= 2)
317 vlib_buffer_t *b0, *b1;
319 u32 sw_if_index0, sw_if_index1;
321 /* Prefetch next iteration. */
323 vlib_buffer_t *p2, *p3, *p4, *p5;
324 u32 sw_if_index2, sw_if_index3;
326 p2 = vlib_get_buffer (vm, from[2]);
327 p3 = vlib_get_buffer (vm, from[3]);
328 p4 = vlib_get_buffer (vm, from[4]);
329 p5 = vlib_get_buffer (vm, from[5]);
331 /* Prefetch the buffer header and packet for the N+2 loop iteration */
332 vlib_prefetch_buffer_header (p4, LOAD);
333 vlib_prefetch_buffer_header (p5, LOAD);
335 CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
336 CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);
339 * Prefetch the input config for the N+1 loop iteration
340 * This depends on the buffer header above
342 sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_RX];
343 sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_RX];
344 CLIB_PREFETCH (&msm->configs[sw_if_index2], CLIB_CACHE_LINE_BYTES,
346 CLIB_PREFETCH (&msm->configs[sw_if_index3], CLIB_CACHE_LINE_BYTES,
350 * Don't bother prefetching the bridge-domain config (which
351 * depends on the input config above). Only a small number of
352 * bridge domains are expected. Plus the structure is small
353 * and several fit in a cache line.
357 /* speculatively enqueue b0 and b1 to the current next frame */
358 /* bi is "buffer index", b is pointer to the buffer */
359 to_next[0] = bi0 = from[0];
360 to_next[1] = bi1 = from[1];
366 b0 = vlib_get_buffer (vm, bi0);
367 b1 = vlib_get_buffer (vm, bi1);
369 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
371 /* RX interface handles */
372 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
373 sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
375 if (b0->flags & VLIB_BUFFER_IS_TRACED)
377 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
379 vlib_add_trace (vm, node, b0, sizeof (*t));
380 t->sw_if_index = sw_if_index0;
381 clib_memcpy (t->src, h0->src_address, 6);
382 clib_memcpy (t->dst, h0->dst_address, 6);
384 if (b1->flags & VLIB_BUFFER_IS_TRACED)
386 ethernet_header_t *h1 = vlib_buffer_get_current (b1);
388 vlib_add_trace (vm, node, b1, sizeof (*t));
389 t->sw_if_index = sw_if_index1;
390 clib_memcpy (t->src, h1->src_address, 6);
391 clib_memcpy (t->dst, h1->dst_address, 6);
395 em->counters[node_counter_base_index + L2INPUT_ERROR_L2INPUT] += 2;
397 classify_and_dispatch (vm, node, cpu_index, msm, b0, &next0);
399 classify_and_dispatch (vm, node, cpu_index, msm, b1, &next1);
401 /* verify speculative enqueues, maybe switch current next frame */
402 /* if next0==next1==next_index then nothing special needs to be done */
403 vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
404 to_next, n_left_to_next,
405 bi0, bi1, next0, next1);
408 while (n_left_from > 0 && n_left_to_next > 0)
415 /* speculatively enqueue b0 to the current next frame */
423 b0 = vlib_get_buffer (vm, bi0);
425 if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
426 && (b0->flags & VLIB_BUFFER_IS_TRACED)))
428 ethernet_header_t *h0 = vlib_buffer_get_current (b0);
429 l2input_trace_t *t = vlib_add_trace (vm, node, b0, sizeof (*t));
430 sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
431 t->sw_if_index = sw_if_index0;
432 clib_memcpy (t->src, h0->src_address, 6);
433 clib_memcpy (t->dst, h0->dst_address, 6);
436 em->counters[node_counter_base_index + L2INPUT_ERROR_L2INPUT] += 1;
438 classify_and_dispatch (vm, node, cpu_index, msm, b0, &next0);
440 /* verify speculative enqueue, maybe switch current next frame */
441 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
442 to_next, n_left_to_next,
446 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
449 return frame->n_vectors;
454 VLIB_REGISTER_NODE (l2input_node) = {
455 .function = l2input_node_fn,
457 .vector_size = sizeof (u32),
458 .format_trace = format_l2input_trace,
459 .format_buffer = format_ethernet_header_with_length,
460 .type = VLIB_NODE_TYPE_INTERNAL,
462 .n_errors = ARRAY_LEN(l2input_error_strings),
463 .error_strings = l2input_error_strings,
465 .n_next_nodes = L2INPUT_N_NEXT,
467 /* edit / add dispositions here */
469 [L2INPUT_NEXT_LEARN] = "l2-learn",
470 [L2INPUT_NEXT_FWD] = "l2-fwd",
471 [L2INPUT_NEXT_DROP] = "error-drop",
476 VLIB_NODE_FUNCTION_MULTIARCH (l2input_node, l2input_node_fn)
477 clib_error_t *l2input_init (vlib_main_t * vm)
479 l2input_main_t *mp = &l2input_main;
482 mp->vnet_main = vnet_get_main ();
484 /* Get packets RX'd from L2 interfaces */
485 ethernet_register_l2_input (vm, l2input_node.index);
487 /* Create the config vector */
488 vec_validate (mp->configs, 100);
489 /* create 100 sw interface entries and zero them */
491 /* Initialize the feature next-node indexes */
492 feat_bitmap_init_next_nodes (vm,
495 l2input_get_feat_names (),
496 mp->feat_next_node_index);
501 VLIB_INIT_FUNCTION (l2input_init);
504 /** Get a pointer to the config for the given interface. */
506 l2input_intf_config (u32 sw_if_index)
508 l2input_main_t *mp = &l2input_main;
510 vec_validate (mp->configs, sw_if_index);
511 return vec_elt_at_index (mp->configs, sw_if_index);
514 /** Enable (or disable) the feature in the bitmap for the given interface. */
516 l2input_intf_bitmap_enable (u32 sw_if_index, u32 feature_bitmap, u32 enable)
518 l2input_main_t *mp = &l2input_main;
519 l2_input_config_t *config;
521 vec_validate (mp->configs, sw_if_index);
522 config = vec_elt_at_index (mp->configs, sw_if_index);
526 config->feature_bitmap |= feature_bitmap;
530 config->feature_bitmap &= ~feature_bitmap;
533 return config->feature_bitmap;
537 l2input_set_bridge_features (u32 bd_index, u32 feat_mask, u32 feat_value)
539 l2_bridge_domain_t *bd_config;
540 vec_validate (l2input_main.bd_configs, bd_index);
541 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
542 bd_validate (bd_config);
543 bd_config->feature_bitmap =
544 (bd_config->feature_bitmap & ~feat_mask) | feat_value;
545 return bd_config->feature_bitmap;
549 * Set the subinterface to run in l2 or l3 mode.
550 * For L3 mode, just the sw_if_index is specified.
551 * For bridged mode, the bd id and bvi flag are also specified.
552 * For xconnect mode, the peer sw_if_index is also specified.
553 * Return 0 if ok, or non-0 if there was an error.
557 set_int_l2_mode (vlib_main_t * vm, vnet_main_t * vnet_main, u32 mode, u32 sw_if_index, u32 bd_index, /* for bridged interface */
558 u32 bvi, /* the bridged interface is the BVI */
559 u32 shg, /* the bridged interface's split horizon group */
560 u32 xc_sw_if_index) /* peer interface for xconnect */
562 l2input_main_t *mp = &l2input_main;
563 l2output_main_t *l2om = &l2output_main;
564 vnet_main_t *vnm = vnet_get_main ();
565 vnet_hw_interface_t *hi;
566 l2_output_config_t *out_config;
567 l2_input_config_t *config;
568 l2_bridge_domain_t *bd_config;
569 l2_flood_member_t member;
571 i32 l2_if_adjust = 0;
574 hi = vnet_get_sup_hw_interface (vnet_main, sw_if_index);
576 vec_validate (mp->configs, sw_if_index);
577 config = vec_elt_at_index (mp->configs, sw_if_index);
581 /* Interface is already in bridge mode. Undo the existing config. */
582 bd_config = vec_elt_at_index (mp->bd_configs, config->bd_index);
584 /* remove interface from flood vector */
585 bd_remove_member (bd_config, sw_if_index);
587 /* undo any BVI-related config */
588 if (bd_config->bvi_sw_if_index == sw_if_index)
590 bd_config->bvi_sw_if_index = ~0;
593 /* delete the l2fib entry for the bvi interface */
594 mac = *((u64 *) hi->hw_address);
595 l2fib_del_entry (mac, config->bd_index);
597 /* Make loop output node send packet back to ethernet-input node */
599 vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
601 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
602 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
606 else if (config->xconnect)
611 /* Initialize the l2-input configuration for the interface */
614 config->xconnect = 0;
617 config->bd_index = 0;
618 config->feature_bitmap = L2INPUT_FEAT_DROP;
620 * Directs the l2 output path to work out the interface
621 * output next-arc itself. Needed when recycling a sw_if_index.
623 vec_validate_init_empty (l2om->next_nodes.output_node_index_vec,
625 l2om->next_nodes.output_node_index_vec[sw_if_index] = ~0;
628 else if (mode == MODE_L2_CLASSIFY)
630 config->xconnect = 1;
632 config->output_sw_if_index = xc_sw_if_index;
634 /* Make sure last-chance drop is configured */
635 config->feature_bitmap |=
636 L2INPUT_FEAT_DROP | L2INPUT_FEAT_INPUT_CLASSIFY;
638 /* Make sure bridging features are disabled */
639 config->feature_bitmap &=
640 ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
641 shg = 0; /* not used in xconnect */
643 /* Insure all packets go to ethernet-input */
644 ethernet_set_rx_redirect (vnet_main, hi, 1);
649 if (mode == MODE_L2_BRIDGE)
652 * Remove a check that the interface must be an Ethernet.
653 * Specifically so we can bridge to L3 tunnel interfaces.
655 * if (hi->hw_class_index != ethernet_hw_interface_class.index)
659 return MODE_ERROR_ETH; /* non-ethernet */
661 config->xconnect = 0;
663 config->bd_index = bd_index;
666 * Enable forwarding, flooding, learning and ARP termination by default
667 * (note that ARP term is disabled on BD feature bitmap by default)
669 config->feature_bitmap |= L2INPUT_FEAT_FWD | L2INPUT_FEAT_UU_FLOOD |
670 L2INPUT_FEAT_FLOOD | L2INPUT_FEAT_LEARN | L2INPUT_FEAT_ARP_TERM;
672 /* Make sure last-chance drop is configured */
673 config->feature_bitmap |= L2INPUT_FEAT_DROP;
675 /* Make sure xconnect is disabled */
676 config->feature_bitmap &= ~L2INPUT_FEAT_XCONNECT;
678 /* Set up bridge domain */
679 vec_validate (mp->bd_configs, bd_index);
680 bd_config = vec_elt_at_index (mp->bd_configs, bd_index);
681 bd_validate (bd_config);
683 /* TODO: think: add l2fib entry even for non-bvi interface? */
685 /* Do BVI interface initializations */
688 /* ensure BD has no bvi interface (or replace that one with this??) */
689 if (bd_config->bvi_sw_if_index != ~0)
691 return MODE_ERROR_BVI_DEF; /* bd already has a bvi interface */
693 bd_config->bvi_sw_if_index = sw_if_index;
696 /* create the l2fib entry for the bvi interface */
697 mac = *((u64 *) hi->hw_address);
698 l2fib_add_entry (mac, bd_index, sw_if_index, 1, 0, 1); /* static + bvi */
700 /* Disable learning by default. no use since l2fib entry is static. */
701 config->feature_bitmap &= ~L2INPUT_FEAT_LEARN;
703 /* Make loop output node send packet to l2-input node */
705 vlib_node_add_named_next_with_slot (vm, hi->tx_node_index,
707 VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
708 ASSERT (slot == VNET_SIMULATED_ETHERNET_TX_NEXT_ETHERNET_INPUT);
711 /* Add interface to bridge-domain flood vector */
712 member.sw_if_index = sw_if_index;
713 member.flags = bvi ? L2_FLOOD_MEMBER_BVI : L2_FLOOD_MEMBER_NORMAL;
715 bd_add_member (bd_config, &member);
720 config->xconnect = 1;
722 config->output_sw_if_index = xc_sw_if_index;
724 /* Make sure last-chance drop is configured */
725 config->feature_bitmap |= L2INPUT_FEAT_DROP;
727 /* Make sure bridging features are disabled */
728 config->feature_bitmap &=
729 ~(L2INPUT_FEAT_LEARN | L2INPUT_FEAT_FWD | L2INPUT_FEAT_FLOOD);
731 config->feature_bitmap |= L2INPUT_FEAT_XCONNECT;
732 shg = 0; /* not used in xconnect */
735 /* set up split-horizon group */
737 out_config = l2output_intf_config (sw_if_index);
738 out_config->shg = shg;
741 * Test: remove this when non-IP features can be configured.
742 * Enable a non-IP feature to test IP feature masking
743 * config->feature_bitmap |= L2INPUT_FEAT_CTRL_PKT;
749 /* Adjust count of L2 interfaces */
750 hi->l2_if_count += l2_if_adjust;
752 if (hi->hw_class_index == ethernet_hw_interface_class.index)
754 if ((hi->l2_if_count == 1) && (l2_if_adjust == 1))
756 /* Just added first L2 interface on this port */
758 /* Set promiscuous mode on the l2 interface */
759 ethernet_set_flags (vnet_main, hi->hw_if_index,
760 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
762 /* ensure all packets go to ethernet-input */
763 ethernet_set_rx_redirect (vnet_main, hi, 1);
766 else if ((hi->l2_if_count == 0) && (l2_if_adjust == -1))
768 /* Just removed only L2 subinterface on this port */
770 /* Disable promiscuous mode on the l2 interface */
771 ethernet_set_flags (vnet_main, hi->hw_if_index, 0);
773 /* Allow ip packets to go directly to ip4-input etc */
774 ethernet_set_rx_redirect (vnet_main, hi, 0);
778 /* Set up the L2/L3 flag in the interface parsing tables */
779 ethernet_sw_interface_set_l2_mode (vnm, sw_if_index, (mode != MODE_L3));
785 * Set subinterface in bridging mode with a bridge-domain ID.
787 * set interface l2 bridge <interface> <bd> [bvi] [split-horizon-group]
789 static clib_error_t *
790 int_l2_bridge (vlib_main_t * vm,
791 unformat_input_t * input, vlib_cli_command_t * cmd)
793 vnet_main_t *vnm = vnet_get_main ();
794 clib_error_t *error = 0;
801 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
803 error = clib_error_return (0, "unknown interface `%U'",
804 format_unformat_error, input);
808 if (!unformat (input, "%d", &bd_id))
810 error = clib_error_return (0, "expected bridge domain ID `%U'",
811 format_unformat_error, input);
815 bd_index = bd_find_or_add_bd_index (&bd_main, bd_id);
818 bvi = unformat (input, "bvi");
820 /* optional split horizon group */
822 (void) unformat (input, "%d", &shg);
824 /* set the interface mode */
826 set_int_l2_mode (vm, vnm, MODE_L2_BRIDGE, sw_if_index, bd_index, bvi,
829 if (rc == MODE_ERROR_ETH)
831 error = clib_error_return (0, "bridged interface must be ethernet",
832 format_unformat_error, input);
834 else if (rc == MODE_ERROR_BVI_DEF)
837 clib_error_return (0, "bridge-domain already has a bvi interface",
838 format_unformat_error, input);
842 error = clib_error_return (0, "invalid configuration for interface",
843 format_unformat_error, input);
853 * Use this command put an interface into Layer 2 bridge domain. If a
854 * bridge-domain with the provided bridge-domain-id does not exist, it
855 * will be created. Interfaces in a bridge-domain forward packets to
856 * other interfaces in the same bridge-domain based on destination mac
857 * address. To remove an interface from a the Layer 2 bridge domain,
858 * put the interface in a different mode, for example Layer 3 mode.
860 * Optionally, an interface can be added to a Layer 2 bridge-domain as
861 * a Bridged Virtual Interface (bvi). Only one interface in a Layer 2
862 * bridge-domain can be a bvi.
864 * Optionally, a split-horizon group can also be specified. This defaults
865 * to 0 if not specified.
868 * Example of how to configure a Layer 2 bridge-domain with three
869 * interfaces (where 200 is the bridge-domain-id):
870 * @cliexcmd{set interface l2 bridge GigabitEthernet0/8/0.200 200}
871 * This interface is added a BVI interface:
872 * @cliexcmd{set interface l2 bridge GigabitEthernet0/9/0.200 200 bvi}
873 * This interface also has a split-horizon group of 1 specified:
874 * @cliexcmd{set interface l2 bridge GigabitEthernet0/a/0.200 200 1}
875 * Example of how to remove an interface from a Layer2 bridge-domain:
876 * @cliexcmd{set interface l3 GigabitEthernet0/a/0.200}
879 VLIB_CLI_COMMAND (int_l2_bridge_cli, static) = {
880 .path = "set interface l2 bridge",
881 .short_help = "set interface l2 bridge <interface> <bridge-domain-id> [bvi] [shg]",
882 .function = int_l2_bridge,
887 * Set subinterface in xconnect mode with another interface.
889 * set interface l2 xconnect <interface> <peer interface>
891 static clib_error_t *
892 int_l2_xc (vlib_main_t * vm,
893 unformat_input_t * input, vlib_cli_command_t * cmd)
895 vnet_main_t *vnm = vnet_get_main ();
896 clib_error_t *error = 0;
900 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
902 error = clib_error_return (0, "unknown interface `%U'",
903 format_unformat_error, input);
908 (input, unformat_vnet_sw_interface, vnm, &xc_sw_if_index))
910 error = clib_error_return (0, "unknown peer interface `%U'",
911 format_unformat_error, input);
915 /* set the interface mode */
917 (vm, vnm, MODE_L2_XC, sw_if_index, 0, 0, 0, xc_sw_if_index))
919 error = clib_error_return (0, "invalid configuration for interface",
920 format_unformat_error, input);
929 * Use this command put an interface into Layer 2 cross-connect mode.
930 * Both interfaces must be in this mode for bi-directioal traffic. All
931 * packets received on one interface will be transmitted to the other.
932 * To remove the Layer 2 cross-connect, put the interface in a different
933 * mode, for example Layer 3 mode.
936 * Example of how to configure a Layer2 cross-connect between two interfaces:
937 * @cliexcmd{set interface l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300}
938 * @cliexcmd{set interface l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300}
939 * Example of how to remove a Layer2 cross-connect:
940 * @cliexcmd{set interface l3 GigabitEthernet0/8/0.300}
941 * @cliexcmd{set interface l3 GigabitEthernet0/9/0.300}
944 VLIB_CLI_COMMAND (int_l2_xc_cli, static) = {
945 .path = "set interface l2 xconnect",
946 .short_help = "set interface l2 xconnect <interface> <peer interface>",
947 .function = int_l2_xc,
952 * Set subinterface in L3 mode.
954 * set interface l3 <interface>
956 static clib_error_t *
957 int_l3 (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
959 vnet_main_t *vnm = vnet_get_main ();
960 clib_error_t *error = 0;
963 if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
965 error = clib_error_return (0, "unknown interface `%U'",
966 format_unformat_error, input);
970 /* set the interface mode */
971 if (set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0))
973 error = clib_error_return (0, "invalid configuration for interface",
974 format_unformat_error, input);
983 * Modify the packet processing mode of the interface to Layer 3, which
984 * implies packets will be routed. This is the default mode of an interface.
985 * Use this command to remove an interface from a Layer 2 cross-connect or a
989 * Example of how to set the mode of an interface to Layer 3:
990 * @cliexcmd{set interface l3 GigabitEthernet0/8/0.200}
993 VLIB_CLI_COMMAND (int_l3_cli, static) = {
994 .path = "set interface l3",
995 .short_help = "set interface l3 <interface>",
1001 * Show interface mode.
1002 * The CLI format is:
1003 * show mode [<if-name1> <if-name2> ...]
1005 static clib_error_t *
1006 show_int_mode (vlib_main_t * vm,
1007 unformat_input_t * input, vlib_cli_command_t * cmd)
1009 vnet_main_t *vnm = vnet_get_main ();
1010 clib_error_t *error = 0;
1013 vnet_interface_main_t *im = &vnm->interface_main;
1014 vnet_sw_interface_t *si, *sis = 0;
1015 l2input_main_t *mp = &l2input_main;
1016 l2_input_config_t *config;
1018 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1022 /* See if user wants to show specific interface */
1024 (input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index))
1026 si = pool_elt_at_index (im->sw_interfaces, sw_if_index);
1027 vec_add1 (sis, si[0]);
1031 error = clib_error_return (0, "unknown input `%U'",
1032 format_unformat_error, input);
1038 if (vec_len (sis) == 0) /* Get all interfaces */
1040 /* Gather interfaces. */
1041 sis = vec_new (vnet_sw_interface_t, pool_elts (im->sw_interfaces));
1044 pool_foreach (si, im->sw_interfaces, ({ vec_add1 (sis, si[0]); }));
1048 vec_foreach (si, sis)
1050 vec_validate (mp->configs, si->sw_if_index);
1051 config = vec_elt_at_index (mp->configs, si->sw_if_index);
1056 bd_id = l2input_main.bd_configs[config->bd_index].bd_id;
1058 args = format (0, "bd_id %d%s%d", bd_id,
1059 config->bvi ? " bvi shg " : " shg ", config->shg);
1061 else if (config->xconnect)
1063 mode = "l2 xconnect";
1064 args = format (0, "%U",
1065 format_vnet_sw_if_index_name,
1066 vnm, config->output_sw_if_index);
1071 args = format (0, " ");
1073 vlib_cli_output (vm, "%s %U %v\n",
1075 format_vnet_sw_if_index_name,
1076 vnm, si->sw_if_index, args);
1087 * Show the packet processing mode (Layer2 xcross-onnect, Layer 2 bridge,
1088 * Layer 3 routed) of all interfaces and sub-interfaces, or limit the
1089 * output to just the provided list of interfaces and sub-interfaces.
1090 * The output shows the mode, the interface, and if the interface is
1091 * a member of a bridge, the bridge-domain-id and the split horizen group (shg).
1094 * Example of displaying the mode of all interfaces:
1095 * @cliexstart{show mode}
1097 * l3 GigabitEthernet0/8/0
1098 * l3 GigabitEthernet0/9/0
1099 * l3 GigabitEthernet0/a/0
1100 * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1101 * l2 bridge GigabitEthernet0/9/0.200 bd_id 200 shg 0
1102 * l2 bridge GigabitEthernet0/a/0.200 bd_id 200 shg 0
1103 * l2 xconnect GigabitEthernet0/8/0.300 GigabitEthernet0/9/0.300
1104 * l2 xconnect GigabitEthernet0/9/0.300 GigabitEthernet0/8/0.300
1106 * Example of displaying the mode of a seleted list of interfaces:
1107 * @cliexstart{show mode GigabitEthernet0/8/0 GigabitEthernet0/8/0.200}
1108 * l3 GigabitEthernet0/8/0
1109 * l2 bridge GigabitEthernet0/8/0.200 bd_id 200 shg 0
1113 VLIB_CLI_COMMAND (show_l2_mode, static) = {
1114 .path = "show mode",
1115 .short_help = "show mode [<if-name1> <if-name2> ...]",
1116 .function = show_int_mode,
1120 #define foreach_l2_init_function \
1121 _(feat_bitmap_drop_init) \
1123 _(l2_input_classify_init) \
1130 _(l2_efp_filter_init) \
1139 l2_init (vlib_main_t * vm)
1141 clib_error_t *error;
1144 if ((error = vlib_call_init_function (vm, a))) return error; } \
1146 foreach_l2_init_function;
1151 VLIB_INIT_FUNCTION (l2_init);
1154 * fd.io coding-style-patch-verification: ON
1157 * eval: (c-set-style "gnu")