2 * l2_bd.c : layer 2 bridge domain
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>
21 #include <vnet/ethernet/ethernet.h>
22 #include <vnet/ip/format.h>
23 #include <vnet/l2/l2_input.h>
24 #include <vnet/l2/feat_bitmap.h>
25 #include <vnet/l2/l2_bd.h>
26 #include <vnet/l2/l2_learn.h>
27 #include <vnet/l2/l2_fib.h>
28 #include <vnet/l2/l2_vtr.h>
29 #include <vnet/ip/ip4_packet.h>
30 #include <vnet/ip/ip6_packet.h>
32 #include <vppinfra/error.h>
33 #include <vppinfra/hash.h>
34 #include <vppinfra/vec.h>
38 * @brief Ethernet Bridge Domain.
40 * Code in this file manages Layer 2 bridge domains.
47 Init bridge domain if not done already.
48 For feature bitmap, set all bits except ARP termination
51 bd_validate (l2_bridge_domain_t * bd_config)
53 if (bd_is_valid (bd_config))
55 bd_config->feature_bitmap = ~(L2INPUT_FEAT_ARP_TERM | L2INPUT_FEAT_UU_FWD);
56 bd_config->bvi_sw_if_index = ~0;
57 bd_config->uu_fwd_sw_if_index = ~0;
58 bd_config->members = 0;
59 bd_config->flood_count = 0;
60 bd_config->tun_master_count = 0;
61 bd_config->tun_normal_count = 0;
62 bd_config->no_flood_count = 0;
63 bd_config->mac_by_ip4 = 0;
64 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
69 bd_find_index (bd_main_t * bdm, u32 bd_id)
71 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
78 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
80 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
81 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
83 /* mark this index taken */
84 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
86 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
88 vec_validate (l2input_main.bd_configs, rv);
89 l2input_main.bd_configs[rv].bd_id = bd_id;
95 bd_delete (bd_main_t * bdm, u32 bd_index)
97 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
98 u32 bd_id = bd->bd_id;
100 ip6_address_t *ip6_addr_key;
102 /* flush non-static MACs in BD and removed bd_id from hash table */
103 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
104 hash_unset (bdm->bd_index_by_bd_id, bd_id);
106 /* mark this index clear */
107 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
109 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
111 bd->feature_bitmap = 0;
114 vec_free (bd->bd_tag);
116 /* free memory used by BD */
117 vec_free (bd->members);
118 hash_free (bd->mac_by_ip4);
120 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
122 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
125 hash_free (bd->mac_by_ip6);
131 update_flood_count (l2_bridge_domain_t * bd_config)
133 bd_config->flood_count = (vec_len (bd_config->members) -
134 (bd_config->tun_master_count ?
135 bd_config->tun_normal_count : 0));
136 bd_config->flood_count -= bd_config->no_flood_count;
140 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
143 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
144 (vnet_get_main (), member->sw_if_index);
147 * Add one element to the vector
148 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
149 * When flooding, the bvi interface (if present) must be the last member
150 * processed due to how BVI processing can change the packet. To enable
151 * this order, we make the bvi interface the first in the vector and
152 * flooding walks the vector in reverse. The flood-count determines where
153 * in the member list to start the walk from.
155 switch (sw_if->flood_class)
157 case VNET_FLOOD_CLASS_NO_FLOOD:
158 bd_config->no_flood_count++;
159 ix = vec_len (bd_config->members);
161 case VNET_FLOOD_CLASS_BVI:
164 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
165 bd_config->tun_master_count++;
167 case VNET_FLOOD_CLASS_NORMAL:
168 ix = (vec_len (bd_config->members) -
169 bd_config->tun_normal_count - bd_config->no_flood_count);
171 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
172 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
173 bd_config->tun_normal_count++;
177 vec_insert_elts (bd_config->members, member, 1, ix);
178 update_flood_count (bd_config);
181 #define BD_REMOVE_ERROR_OK 0
182 #define BD_REMOVE_ERROR_NOT_FOUND 1
185 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
189 /* Find and delete the member */
190 vec_foreach_index (ix, bd_config->members)
192 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
193 if (m->sw_if_index == sw_if_index)
195 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
196 (vnet_get_main (), sw_if_index);
198 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
200 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
201 bd_config->tun_master_count--;
202 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
203 bd_config->tun_normal_count--;
204 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
205 bd_config->no_flood_count--;
207 vec_delete (bd_config->members, 1, ix);
208 update_flood_count (bd_config);
210 return BD_REMOVE_ERROR_OK;
214 return BD_REMOVE_ERROR_NOT_FOUND;
219 l2bd_init (vlib_main_t * vm)
221 bd_main_t *bdm = &bd_main;
222 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
224 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
225 * to packet drop only. Thus, packets received from any L2 interface with
226 * uninitialized bd_index of 0 can be dropped safely.
228 u32 bd_index = bd_add_bd_index (bdm, 0);
229 ASSERT (bd_index == 0);
230 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
236 VLIB_INIT_FUNCTION (l2bd_init);
240 Set the learn/forward/flood flags for the bridge domain.
241 Return 0 if ok, non-zero if for an error.
244 bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
247 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
248 bd_validate (bd_config);
249 u32 feature_bitmap = 0;
251 if (flags & L2_LEARN)
253 feature_bitmap |= L2INPUT_FEAT_LEARN;
257 feature_bitmap |= L2INPUT_FEAT_FWD;
259 if (flags & L2_FLOOD)
261 feature_bitmap |= L2INPUT_FEAT_FLOOD;
263 if (flags & L2_UU_FLOOD)
265 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
267 if (flags & L2_ARP_TERM)
269 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
274 bd_config->feature_bitmap |= feature_bitmap;
278 bd_config->feature_bitmap &= ~feature_bitmap;
281 return bd_config->feature_bitmap;
285 Set the mac age for the bridge domain.
288 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
290 l2_bridge_domain_t *bd_config;
293 vec_validate (l2input_main.bd_configs, bd_index);
294 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
295 bd_config->mac_age = age;
297 /* check if there is at least one bd with mac aging enabled */
298 vec_foreach (bd_config, l2input_main.bd_configs)
299 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
301 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
302 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
303 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
307 Set the tag for the bridge domain.
311 bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
314 l2_bridge_domain_t *bd_config;
315 vec_validate (l2input_main.bd_configs, bd_index);
316 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
318 old = bd_config->bd_tag;
322 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
326 bd_config->bd_tag = NULL;
333 Set bridge-domain learn enable/disable.
335 set bridge-domain learn <bd_id> [disable]
337 static clib_error_t *
338 bd_learn (vlib_main_t * vm,
339 unformat_input_t * input, vlib_cli_command_t * cmd)
341 bd_main_t *bdm = &bd_main;
342 clib_error_t *error = 0;
347 if (!unformat (input, "%d", &bd_id))
349 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
350 format_unformat_error, input);
355 return clib_error_return (0,
356 "No operations on the default bridge domain are supported");
358 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
361 return clib_error_return (0, "No such bridge domain %d", bd_id);
366 if (unformat (input, "disable"))
371 /* set the bridge domain flag */
372 bd_set_flags (vm, bd_index, L2_LEARN, enable);
379 * Layer 2 learning can be enabled and disabled on each
380 * interface and on each bridge-domain. Use this command to
381 * manage bridge-domains. It is enabled by default.
384 * Example of how to enable learning (where 200 is the bridge-domain-id):
385 * @cliexcmd{set bridge-domain learn 200}
386 * Example of how to disable learning (where 200 is the bridge-domain-id):
387 * @cliexcmd{set bridge-domain learn 200 disable}
390 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
391 .path = "set bridge-domain learn",
392 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
393 .function = bd_learn,
398 Set bridge-domain forward enable/disable.
400 set bridge-domain forward <bd_index> [disable]
402 static clib_error_t *
403 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
405 bd_main_t *bdm = &bd_main;
406 clib_error_t *error = 0;
411 if (!unformat (input, "%d", &bd_id))
413 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
414 format_unformat_error, input);
419 return clib_error_return (0,
420 "No operations on the default bridge domain are supported");
422 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
425 return clib_error_return (0, "No such bridge domain %d", bd_id);
430 if (unformat (input, "disable"))
435 /* set the bridge domain flag */
436 bd_set_flags (vm, bd_index, L2_FWD, enable);
444 * Layer 2 unicast forwarding can be enabled and disabled on each
445 * interface and on each bridge-domain. Use this command to
446 * manage bridge-domains. It is enabled by default.
449 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
450 * @cliexcmd{set bridge-domain forward 200}
451 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
452 * @cliexcmd{set bridge-domain forward 200 disable}
455 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
456 .path = "set bridge-domain forward",
457 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
463 Set bridge-domain flood enable/disable.
465 set bridge-domain flood <bd_index> [disable]
467 static clib_error_t *
468 bd_flood (vlib_main_t * vm,
469 unformat_input_t * input, vlib_cli_command_t * cmd)
471 bd_main_t *bdm = &bd_main;
472 clib_error_t *error = 0;
477 if (!unformat (input, "%d", &bd_id))
479 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
480 format_unformat_error, input);
485 return clib_error_return (0,
486 "No operations on the default bridge domain are supported");
488 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
491 return clib_error_return (0, "No such bridge domain %d", bd_id);
496 if (unformat (input, "disable"))
501 /* set the bridge domain flag */
502 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
509 * Layer 2 flooding can be enabled and disabled on each
510 * interface and on each bridge-domain. Use this command to
511 * manage bridge-domains. It is enabled by default.
514 * Example of how to enable flooding (where 200 is the bridge-domain-id):
515 * @cliexcmd{set bridge-domain flood 200}
516 * Example of how to disable flooding (where 200 is the bridge-domain-id):
517 * @cliexcmd{set bridge-domain flood 200 disable}
520 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
521 .path = "set bridge-domain flood",
522 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
523 .function = bd_flood,
528 Set bridge-domain unknown-unicast flood enable/disable.
530 set bridge-domain uu-flood <bd_index> [disable]
532 static clib_error_t *
533 bd_uu_flood (vlib_main_t * vm,
534 unformat_input_t * input, vlib_cli_command_t * cmd)
536 bd_main_t *bdm = &bd_main;
537 clib_error_t *error = 0;
542 if (!unformat (input, "%d", &bd_id))
544 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
545 format_unformat_error, input);
550 return clib_error_return (0,
551 "No operations on the default bridge domain are supported");
553 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
556 return clib_error_return (0, "No such bridge domain %d", bd_id);
561 if (unformat (input, "disable"))
566 /* set the bridge domain flag */
567 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
574 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
575 * bridge-domain. It is enabled by default.
578 * Example of how to enable unknown-unicast flooding (where 200 is the
580 * @cliexcmd{set bridge-domain uu-flood 200}
581 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
582 * @cliexcmd{set bridge-domain uu-flood 200 disable}
585 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
586 .path = "set bridge-domain uu-flood",
587 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
588 .function = bd_uu_flood,
593 Set bridge-domain arp term enable/disable.
595 set bridge-domain arp term <bridge-domain-id> [disable]
597 static clib_error_t *
598 bd_arp_term (vlib_main_t * vm,
599 unformat_input_t * input, vlib_cli_command_t * cmd)
601 bd_main_t *bdm = &bd_main;
602 clib_error_t *error = 0;
607 if (!unformat (input, "%d", &bd_id))
609 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
610 format_unformat_error, input);
615 return clib_error_return (0,
616 "No operations on the default bridge domain are supported");
618 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
622 return clib_error_return (0, "No such bridge domain %d", bd_id);
625 if (unformat (input, "disable"))
628 /* set the bridge domain flag */
629 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
635 static clib_error_t *
636 bd_mac_age (vlib_main_t * vm,
637 unformat_input_t * input, vlib_cli_command_t * cmd)
639 bd_main_t *bdm = &bd_main;
640 clib_error_t *error = 0;
645 if (!unformat (input, "%d", &bd_id))
647 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
648 format_unformat_error, input);
653 return clib_error_return (0,
654 "No operations on the default bridge domain are supported");
656 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
659 return clib_error_return (0, "No such bridge domain %d", bd_id);
663 if (!unformat (input, "%u", &age))
666 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
667 format_unformat_error, input);
671 /* set the bridge domain flag */
675 clib_error_return (0, "mac aging time cannot be bigger than 255");
678 bd_set_mac_age (vm, bd_index, (u8) age);
685 * Layer 2 mac aging can be enabled and disabled on each
686 * bridge-domain. Use this command to set or disable mac aging
687 * on specific bridge-domains. It is disabled by default.
690 * Example of how to set mac aging (where 200 is the bridge-domain-id and
691 * 5 is aging time in minutes):
692 * @cliexcmd{set bridge-domain mac-age 200 5}
693 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
694 * @cliexcmd{set bridge-domain flood 200 0}
697 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
698 .path = "set bridge-domain mac-age",
699 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
700 .function = bd_mac_age,
705 * Modify whether or not an existing bridge-domain should terminate and respond
706 * to ARP Requests. ARP Termination is disabled by default.
709 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
710 * @cliexcmd{set bridge-domain arp term 200}
711 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
712 * @cliexcmd{set bridge-domain arp term 200 disable}
715 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
716 .path = "set bridge-domain arp term",
717 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
718 .function = bd_arp_term,
724 * Add/delete IP address to MAC address mapping.
726 * The clib hash implementation stores uword entries in the hash table.
727 * The hash table mac_by_ip4 is keyed via IP4 address and store the
728 * 6-byte MAC address directly in the hash table entry uword.
730 * @warning This only works for 64-bit processor with 8-byte uword;
731 * which means this code *WILL NOT WORK* for a 32-bit processor with
735 bd_add_del_ip_mac (u32 bd_index,
736 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
738 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
739 u64 new_mac = *(u64 *) mac_addr;
741 u16 *mac16 = (u16 *) & new_mac;
743 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
744 ASSERT (bd_is_valid (bd_cfg));
746 mac16[3] = 0; /* Clear last 2 unused bytes of the 8-byte MAC address */
749 ip6_address_t *ip6_addr_key;
751 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
755 { /* new entry - allocate and create ip6 address key */
756 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
757 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
759 else if (*old_mac == new_mac)
760 { /* same mac entry already exist for ip6 address */
764 { /* update mac for ip6 address */
765 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
766 ip6_addr_key = (ip6_address_t *) hp->key;
768 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
772 if (old_mac && (*old_mac == new_mac))
774 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
775 ip6_addr_key = (ip6_address_t *) hp->key;
776 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
777 clib_mem_free (ip6_addr_key);
785 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
786 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
789 if (old_mac && (*old_mac == new_mac))
790 return 0; /* mac entry already exist */
791 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
795 if (old_mac && (*old_mac == new_mac))
796 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
805 Set bridge-domain arp entry add/delete.
807 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
809 static clib_error_t *
810 bd_arp_entry (vlib_main_t * vm,
811 unformat_input_t * input, vlib_cli_command_t * cmd)
813 bd_main_t *bdm = &bd_main;
814 clib_error_t *error = 0;
822 if (!unformat (input, "%d", &bd_id))
824 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
825 format_unformat_error, input);
830 return clib_error_return (0,
831 "No operations on the default bridge domain are supported");
833 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
838 return clib_error_return (0, "No such bridge domain %d", bd_id);
840 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
844 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
850 error = clib_error_return (0, "expecting IP address but got `%U'",
851 format_unformat_error, input);
855 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
857 error = clib_error_return (0, "expecting MAC address but got `%U'",
858 format_unformat_error, input);
862 if (unformat (input, "del"))
867 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
868 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
870 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
871 is_add ? "add" : "del",
873 format_ip4_address : format_ip6_address,
874 ip_addr, format_ethernet_address, mac_addr);
882 * Add an ARP entry to an existing bridge-domain.
885 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
886 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
887 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
888 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
891 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
892 .path = "set bridge-domain arp entry",
893 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
894 .function = bd_arp_entry,
899 format_vtr (u8 * s, va_list * args)
901 u32 vtr_op = va_arg (*args, u32);
902 u32 dot1q = va_arg (*args, u32);
903 u32 tag1 = va_arg (*args, u32);
904 u32 tag2 = va_arg (*args, u32);
907 case L2_VTR_DISABLED:
908 return format (s, "none");
910 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
912 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
915 return format (s, "pop-1");
917 return format (s, "pop-2");
918 case L2_VTR_TRANSLATE_1_1:
919 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
920 case L2_VTR_TRANSLATE_1_2:
921 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
923 case L2_VTR_TRANSLATE_2_1:
924 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
925 case L2_VTR_TRANSLATE_2_2:
926 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
929 return format (s, "none");
934 format_uu_cfg (u8 * s, va_list * args)
936 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
938 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
939 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
940 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
941 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
942 return (format (s, "flood"));
944 return (format (s, "drop"));
948 Show bridge-domain state.
950 show bridge-domain [<bd_index>]
952 static clib_error_t *
953 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
955 vnet_main_t *vnm = vnet_get_main ();
956 bd_main_t *bdm = &bd_main;
957 clib_error_t *error = 0;
959 l2_bridge_domain_t *bd_config;
969 end = vec_len (l2input_main.bd_configs);
971 if (unformat (input, "%d", &bd_id))
973 if (unformat (input, "detail"))
975 else if (unformat (input, "det"))
977 if (unformat (input, "int"))
979 if (unformat (input, "arp"))
981 if (unformat (input, "bd-tag"))
985 return clib_error_return (0,
986 "No operations on the default bridge domain are supported");
988 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
992 return clib_error_return (0, "No such bridge domain %d", bd_id);
994 vec_validate (l2input_main.bd_configs, bd_index);
995 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
996 if (bd_is_valid (bd_config))
1003 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1008 /* Show all bridge-domains that have been initialized */
1011 for (bd_index = start; bd_index < end; bd_index++)
1013 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1014 if (bd_is_valid (bd_config))
1019 vlib_cli_output (vm,
1020 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s %=11s",
1021 "BD-ID", "Index", "BSN", "Age(min)",
1022 "Learning", "U-Forwrd", "UU-Flood",
1023 "Flooding", "ARP-Term", "BVI-Intf");
1026 if (bd_config->mac_age)
1027 as = format (as, "%d", bd_config->mac_age);
1029 as = format (as, "off");
1030 vlib_cli_output (vm,
1031 "%=8d %=7d %=4d %=9v %=9s %=9s %=11U %=9s %=9s %=11U",
1032 bd_config->bd_id, bd_index, bd_config->seq_num, as,
1033 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1035 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1037 format_uu_cfg, bd_config,
1038 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1040 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1042 format_vnet_sw_if_index_name_with_NA,
1043 vnm, bd_config->bvi_sw_if_index);
1044 vec_reset_length (as);
1048 /* Show all member interfaces */
1050 vec_foreach_index (i, bd_config->members)
1052 l2_flood_member_t *member =
1053 vec_elt_at_index (bd_config->members, i);
1054 u8 swif_seq_num = *l2fib_swif_seq_num (member->sw_if_index);
1055 u32 vtr_opr, dot1q, tag1, tag2;
1058 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1059 "Interface", "If-idx", "ISN", "SHG",
1060 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1062 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1064 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1065 format_vnet_sw_if_index_name, vnm,
1066 member->sw_if_index, member->sw_if_index,
1067 swif_seq_num, member->shg,
1068 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1069 "-", i < bd_config->flood_count ? "*" : "-",
1070 format_vtr, vtr_opr, dot1q, tag1, tag2);
1072 if (~0 != bd_config->uu_fwd_sw_if_index)
1073 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1074 format_vnet_sw_if_index_name, vnm,
1075 bd_config->uu_fwd_sw_if_index,
1076 bd_config->uu_fwd_sw_if_index,
1077 0, 0, "uu", "-", "None");
1081 if ((detail || arp) &&
1082 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1085 ip6_address_t *ip6_addr;
1087 vlib_cli_output (vm,
1088 "\n IP4/IP6 to MAC table for ARP Termination");
1091 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1093 vlib_cli_output (vm, "%=40U => %=20U",
1094 format_ip4_address, &ip4_addr,
1095 format_ethernet_address, &mac_addr);
1098 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1100 vlib_cli_output (vm, "%=40U => %=20U",
1101 format_ip6_address, ip6_addr,
1102 format_ethernet_address, &mac_addr);
1107 if ((detail || bd_tag) && (bd_config->bd_tag))
1109 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1118 vlib_cli_output (vm, "no bridge-domains in use");
1126 * Show a summary of all the bridge-domain instances or detailed view of a
1127 * single bridge-domain. Bridge-domains are created by adding an interface
1128 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1132 * Example of displaying all bridge-domains:
1133 * @cliexstart{show bridge-domain}
1134 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1135 * 0 0 off off off off off local0
1136 * 200 1 on on on on off N/A
1139 * Example of displaying details of a single bridge-domains:
1140 * @cliexstart{show bridge-domain 200 detail}
1141 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1142 * 200 1 on on on on off N/A
1144 * Interface Index SHG BVI VLAN-Tag-Rewrite
1145 * GigabitEthernet0/8/0.200 3 0 - none
1146 * GigabitEthernet0/9/0.200 4 0 - none
1151 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1152 .path = "show bridge-domain",
1153 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
1154 .function = bd_show,
1159 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1161 bd_main_t *bdm = &bd_main;
1162 vlib_main_t *vm = bdm->vlib_main;
1165 u32 bd_index = bd_find_index (bdm, a->bd_id);
1169 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1170 if (a->bd_id > L2_BD_ID_MAX)
1171 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1172 bd_index = bd_add_bd_index (bdm, a->bd_id);
1174 bd_flags_t enable_flags = 0, disable_flags = 0;
1176 enable_flags |= L2_FLOOD;
1178 disable_flags |= L2_FLOOD;
1181 enable_flags |= L2_UU_FLOOD;
1183 disable_flags |= L2_UU_FLOOD;
1186 enable_flags |= L2_FWD;
1188 disable_flags |= L2_FWD;
1191 enable_flags |= L2_LEARN;
1193 disable_flags |= L2_LEARN;
1196 enable_flags |= L2_ARP_TERM;
1198 disable_flags |= L2_ARP_TERM;
1201 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1204 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1206 bd_set_mac_age (vm, bd_index, a->mac_age);
1209 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1215 return VNET_API_ERROR_NO_SUCH_ENTRY;
1217 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1218 if (vec_len (l2input_main.bd_configs[bd_index].members))
1219 return VNET_API_ERROR_BD_IN_USE;
1220 rv = bd_delete (bdm, bd_index);
1227 Create or delete bridge-domain.
1229 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1230 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
1233 static clib_error_t *
1234 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1235 vlib_cli_command_t * cmd)
1237 unformat_input_t _line_input, *line_input = &_line_input;
1238 clib_error_t *error = 0;
1241 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
1244 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1247 /* Get a line of input. */
1248 if (!unformat_user (input, unformat_line_input, line_input))
1251 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1253 if (unformat (line_input, "%d", &bd_id))
1255 else if (unformat (line_input, "flood %d", &flood))
1257 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1259 else if (unformat (line_input, "forward %d", &forward))
1261 else if (unformat (line_input, "learn %d", &learn))
1263 else if (unformat (line_input, "arp-term %d", &arp_term))
1265 else if (unformat (line_input, "mac-age %d", &mac_age))
1267 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1269 else if (unformat (line_input, "del"))
1272 flood = uu_flood = forward = learn = 0;
1280 error = clib_error_return (0, "bridge-domain-id not specified");
1286 error = clib_error_return (0, "bridge domain 0 can not be modified");
1292 error = clib_error_return (0, "mac age must be less than 256");
1295 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1297 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1301 memset (a, 0, sizeof (*a));
1304 a->flood = (u8) flood;
1305 a->uu_flood = (u8) uu_flood;
1306 a->forward = (u8) forward;
1307 a->learn = (u8) learn;
1308 a->arp_term = (u8) arp_term;
1309 a->mac_age = (u8) mac_age;
1312 rv = bd_add_del (a);
1318 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1320 case VNET_API_ERROR_BD_IN_USE:
1321 error = clib_error_return (0, "bridge domain in use - remove members");
1323 case VNET_API_ERROR_NO_SUCH_ENTRY:
1324 error = clib_error_return (0, "bridge domain ID does not exist");
1326 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1327 error = clib_error_return (0, "bridge domain 0 can not be modified");
1329 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1330 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1333 error = clib_error_return (0, "bd_add_del returned %d", rv);
1339 unformat_free (line_input);
1346 * Create/Delete bridge-domain instance
1350 * Example of creating bridge-domain 1:
1351 * @cliexstart{create bridge-domain 1}
1355 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1356 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1359 * vpp# show bridge-domain
1360 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1361 * 0 0 0 off off off off off off local0
1362 * 1 1 0 off on on off on off N/A
1363 * 2 2 0 60 on on off on on N/A
1367 * Example of delete bridge-domain 1:
1368 * @cliexstart{create bridge-domain 1 del}
1374 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1375 .path = "create bridge-domain",
1376 .short_help = "create bridge-domain <bridge-domain-id>"
1377 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1378 " [mac-age <nn>] [bd-tag <tag>] [del]",
1379 .function = bd_add_del_command_fn,
1386 * fd.io coding-style-patch-verification: ON
1389 * eval: (c-set-style "gnu")