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;
56 bd_config->bvi_sw_if_index = ~0;
57 bd_config->members = 0;
58 bd_config->flood_count = 0;
59 bd_config->tun_master_count = 0;
60 bd_config->tun_normal_count = 0;
61 bd_config->mac_by_ip4 = 0;
62 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
67 bd_find_index (bd_main_t * bdm, u32 bd_id)
69 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
76 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
78 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
79 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
81 /* mark this index taken */
82 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
84 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
86 vec_validate (l2input_main.bd_configs, rv);
87 l2input_main.bd_configs[rv].bd_id = bd_id;
93 bd_delete (bd_main_t * bdm, u32 bd_index)
95 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
96 u32 bd_id = bd->bd_id;
98 ip6_address_t *ip6_addr_key;
100 /* flush non-static MACs in BD and removed bd_id from hash table */
101 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
102 hash_unset (bdm->bd_index_by_bd_id, bd_id);
104 /* mark this index clear */
105 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
107 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
109 bd->feature_bitmap = 0;
111 /* free memory used by BD */
112 vec_free (bd->members);
113 hash_free (bd->mac_by_ip4);
115 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
117 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
120 hash_free (bd->mac_by_ip6);
126 update_flood_count (l2_bridge_domain_t * bd_config)
128 bd_config->flood_count = vec_len (bd_config->members) -
129 (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
133 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
136 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
137 (vnet_get_main (), member->sw_if_index);
140 * Add one element to the vector
141 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
142 * When flooding, the bvi interface (if present) must be the last member
143 * processed due to how BVI processing can change the packet. To enable
144 * this order, we make the bvi interface the first in the vector and
145 * flooding walks the vector in reverse.
147 switch (sw_if->flood_class)
149 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
150 bd_config->tun_master_count++;
154 case VNET_FLOOD_CLASS_NORMAL:
155 ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
156 vec_len (bd_config->members) - bd_config->tun_normal_count;
158 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
159 ix = vec_len (bd_config->members);
160 bd_config->tun_normal_count++;
164 vec_insert_elts (bd_config->members, member, 1, ix);
165 update_flood_count (bd_config);
168 #define BD_REMOVE_ERROR_OK 0
169 #define BD_REMOVE_ERROR_NOT_FOUND 1
172 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
176 /* Find and delete the member */
177 vec_foreach_index (ix, bd_config->members)
179 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
180 if (m->sw_if_index == sw_if_index)
182 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
183 (vnet_get_main (), sw_if_index);
185 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
187 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
188 bd_config->tun_master_count--;
189 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
190 bd_config->tun_normal_count--;
192 vec_delete (bd_config->members, 1, ix);
193 update_flood_count (bd_config);
195 return BD_REMOVE_ERROR_OK;
199 return BD_REMOVE_ERROR_NOT_FOUND;
204 l2bd_init (vlib_main_t * vm)
206 bd_main_t *bdm = &bd_main;
207 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
209 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
210 * to packet drop only. Thus, packets received from any L2 interface with
211 * uninitialized bd_index of 0 can be dropped safely.
213 u32 bd_index = bd_add_bd_index (bdm, 0);
214 ASSERT (bd_index == 0);
215 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
221 VLIB_INIT_FUNCTION (l2bd_init);
225 Set the learn/forward/flood flags for the bridge domain.
226 Return 0 if ok, non-zero if for an error.
229 bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
232 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
233 bd_validate (bd_config);
234 u32 feature_bitmap = 0;
236 if (flags & L2_LEARN)
238 feature_bitmap |= L2INPUT_FEAT_LEARN;
242 feature_bitmap |= L2INPUT_FEAT_FWD;
244 if (flags & L2_FLOOD)
246 feature_bitmap |= L2INPUT_FEAT_FLOOD;
248 if (flags & L2_UU_FLOOD)
250 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
252 if (flags & L2_ARP_TERM)
254 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
259 bd_config->feature_bitmap |= feature_bitmap;
263 bd_config->feature_bitmap &= ~feature_bitmap;
270 Set the mac age for the bridge domain.
273 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
275 l2_bridge_domain_t *bd_config;
278 vec_validate (l2input_main.bd_configs, bd_index);
279 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
280 bd_config->mac_age = age;
282 /* check if there is at least one bd with mac aging enabled */
283 vec_foreach (bd_config, l2input_main.bd_configs)
284 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
286 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
287 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
288 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
292 Set bridge-domain learn enable/disable.
294 set bridge-domain learn <bd_id> [disable]
296 static clib_error_t *
297 bd_learn (vlib_main_t * vm,
298 unformat_input_t * input, vlib_cli_command_t * cmd)
300 bd_main_t *bdm = &bd_main;
301 clib_error_t *error = 0;
306 if (!unformat (input, "%d", &bd_id))
308 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
309 format_unformat_error, input);
314 return clib_error_return (0,
315 "No operations on the default bridge domain are supported");
317 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
320 return clib_error_return (0, "No such bridge domain %d", bd_id);
325 if (unformat (input, "disable"))
330 /* set the bridge domain flag */
331 if (bd_set_flags (vm, bd_index, L2_LEARN, enable))
334 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
343 * Layer 2 learning can be enabled and disabled on each
344 * interface and on each bridge-domain. Use this command to
345 * manage bridge-domains. It is enabled by default.
348 * Example of how to enable learning (where 200 is the bridge-domain-id):
349 * @cliexcmd{set bridge-domain learn 200}
350 * Example of how to disable learning (where 200 is the bridge-domain-id):
351 * @cliexcmd{set bridge-domain learn 200 disable}
354 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
355 .path = "set bridge-domain learn",
356 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
357 .function = bd_learn,
362 Set bridge-domain forward enable/disable.
364 set bridge-domain forward <bd_index> [disable]
366 static clib_error_t *
367 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
369 bd_main_t *bdm = &bd_main;
370 clib_error_t *error = 0;
375 if (!unformat (input, "%d", &bd_id))
377 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
378 format_unformat_error, input);
383 return clib_error_return (0,
384 "No operations on the default bridge domain are supported");
386 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
389 return clib_error_return (0, "No such bridge domain %d", bd_id);
394 if (unformat (input, "disable"))
399 /* set the bridge domain flag */
400 if (bd_set_flags (vm, bd_index, L2_FWD, enable))
403 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
413 * Layer 2 unicast forwarding can be enabled and disabled on each
414 * interface and on each bridge-domain. Use this command to
415 * manage bridge-domains. It is enabled by default.
418 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
419 * @cliexcmd{set bridge-domain forward 200}
420 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
421 * @cliexcmd{set bridge-domain forward 200 disable}
424 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
425 .path = "set bridge-domain forward",
426 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
432 Set bridge-domain flood enable/disable.
434 set bridge-domain flood <bd_index> [disable]
436 static clib_error_t *
437 bd_flood (vlib_main_t * vm,
438 unformat_input_t * input, vlib_cli_command_t * cmd)
440 bd_main_t *bdm = &bd_main;
441 clib_error_t *error = 0;
446 if (!unformat (input, "%d", &bd_id))
448 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
449 format_unformat_error, input);
454 return clib_error_return (0,
455 "No operations on the default bridge domain are supported");
457 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
460 return clib_error_return (0, "No such bridge domain %d", bd_id);
465 if (unformat (input, "disable"))
470 /* set the bridge domain flag */
471 if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
474 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
483 * Layer 2 flooding can be enabled and disabled on each
484 * interface and on each bridge-domain. Use this command to
485 * manage bridge-domains. It is enabled by default.
488 * Example of how to enable flooding (where 200 is the bridge-domain-id):
489 * @cliexcmd{set bridge-domain flood 200}
490 * Example of how to disable flooding (where 200 is the bridge-domain-id):
491 * @cliexcmd{set bridge-domain flood 200 disable}
494 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
495 .path = "set bridge-domain flood",
496 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
497 .function = bd_flood,
502 Set bridge-domain unkown-unicast flood enable/disable.
504 set bridge-domain uu-flood <bd_index> [disable]
506 static clib_error_t *
507 bd_uu_flood (vlib_main_t * vm,
508 unformat_input_t * input, vlib_cli_command_t * cmd)
510 bd_main_t *bdm = &bd_main;
511 clib_error_t *error = 0;
516 if (!unformat (input, "%d", &bd_id))
518 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
519 format_unformat_error, input);
524 return clib_error_return (0,
525 "No operations on the default bridge domain are supported");
527 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
530 return clib_error_return (0, "No such bridge domain %d", bd_id);
535 if (unformat (input, "disable"))
540 /* set the bridge domain flag */
541 if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
544 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
553 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
554 * bridge-domain. It is enabled by default.
557 * Example of how to enable unknown-unicast flooding (where 200 is the
559 * @cliexcmd{set bridge-domain uu-flood 200}
560 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
561 * @cliexcmd{set bridge-domain uu-flood 200 disable}
564 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
565 .path = "set bridge-domain uu-flood",
566 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
567 .function = bd_uu_flood,
572 Set bridge-domain arp term enable/disable.
574 set bridge-domain arp term <bridge-domain-id> [disable]
576 static clib_error_t *
577 bd_arp_term (vlib_main_t * vm,
578 unformat_input_t * input, vlib_cli_command_t * cmd)
580 bd_main_t *bdm = &bd_main;
581 clib_error_t *error = 0;
586 if (!unformat (input, "%d", &bd_id))
588 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
589 format_unformat_error, input);
594 return clib_error_return (0,
595 "No operations on the default bridge domain are supported");
597 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
601 return clib_error_return (0, "No such bridge domain %d", bd_id);
604 if (unformat (input, "disable"))
607 /* set the bridge domain flag */
608 if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
611 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
619 static clib_error_t *
620 bd_mac_age (vlib_main_t * vm,
621 unformat_input_t * input, vlib_cli_command_t * cmd)
623 bd_main_t *bdm = &bd_main;
624 clib_error_t *error = 0;
629 if (!unformat (input, "%d", &bd_id))
631 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
632 format_unformat_error, input);
637 return clib_error_return (0,
638 "No operations on the default bridge domain are supported");
640 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
643 return clib_error_return (0, "No such bridge domain %d", bd_id);
647 if (!unformat (input, "%u", &age))
650 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
651 format_unformat_error, input);
655 /* set the bridge domain flag */
659 clib_error_return (0, "mac aging time cannot be bigger than 255");
662 bd_set_mac_age (vm, bd_index, (u8) age);
669 * Layer 2 mac aging can be enabled and disabled on each
670 * bridge-domain. Use this command to set or disable mac aging
671 * on specific bridge-domains. It is disabled by default.
674 * Example of how to set mac aging (where 200 is the bridge-domain-id and
675 * 5 is aging time in minutes):
676 * @cliexcmd{set bridge-domain mac-age 200 5}
677 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
678 * @cliexcmd{set bridge-domain flood 200 0}
681 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
682 .path = "set bridge-domain mac-age",
683 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
684 .function = bd_mac_age,
689 * Modify whether or not an existing bridge-domain should terminate and respond
690 * to ARP Requests. ARP Termination is disabled by default.
693 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
694 * @cliexcmd{set bridge-domain arp term 200}
695 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
696 * @cliexcmd{set bridge-domain arp term 200 disable}
699 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
700 .path = "set bridge-domain arp term",
701 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
702 .function = bd_arp_term,
708 * Add/delete IP address to MAC address mapping.
710 * The clib hash implementation stores uword entries in the hash table.
711 * The hash table mac_by_ip4 is keyed via IP4 address and store the
712 * 6-byte MAC address directly in the hash table entry uword.
714 * @warning This only works for 64-bit processor with 8-byte uword;
715 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
719 bd_add_del_ip_mac (u32 bd_index,
720 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
722 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
723 u64 new_mac = *(u64 *) mac_addr;
725 u16 *mac16 = (u16 *) & new_mac;
727 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
728 ASSERT (bd_is_valid (bd_cfg));
730 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
733 ip6_address_t *ip6_addr_key;
735 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
739 { /* new entry - allocate and craete ip6 address key */
740 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
741 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
743 else if (*old_mac == new_mac)
744 { /* same mac entry already exist for ip6 address */
748 { /* updat mac for ip6 address */
749 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
750 ip6_addr_key = (ip6_address_t *) hp->key;
752 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
756 if (old_mac && (*old_mac == new_mac))
758 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
759 ip6_addr_key = (ip6_address_t *) hp->key;
760 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
761 clib_mem_free (ip6_addr_key);
769 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
770 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
773 if (old_mac && (*old_mac == new_mac))
774 return 0; /* mac entry already exist */
775 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
779 if (old_mac && (*old_mac == new_mac))
780 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
789 Set bridge-domain arp entry add/delete.
791 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
793 static clib_error_t *
794 bd_arp_entry (vlib_main_t * vm,
795 unformat_input_t * input, vlib_cli_command_t * cmd)
797 bd_main_t *bdm = &bd_main;
798 clib_error_t *error = 0;
806 if (!unformat (input, "%d", &bd_id))
808 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
809 format_unformat_error, input);
814 return clib_error_return (0,
815 "No operations on the default bridge domain are supported");
817 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
822 return clib_error_return (0, "No such bridge domain %d", bd_id);
824 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
828 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
834 error = clib_error_return (0, "expecting IP address but got `%U'",
835 format_unformat_error, input);
839 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
841 error = clib_error_return (0, "expecting MAC address but got `%U'",
842 format_unformat_error, input);
846 if (unformat (input, "del"))
851 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
852 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
854 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
855 is_add ? "add" : "del",
857 format_ip4_address : format_ip6_address,
858 ip_addr, format_ethernet_address, mac_addr);
866 * Add an ARP entry to an existing bridge-domain.
869 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
870 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
871 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
872 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
875 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
876 .path = "set bridge-domain arp entry",
877 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
878 .function = bd_arp_entry,
883 format_vtr (u8 * s, va_list * args)
885 u32 vtr_op = va_arg (*args, u32);
886 u32 dot1q = va_arg (*args, u32);
887 u32 tag1 = va_arg (*args, u32);
888 u32 tag2 = va_arg (*args, u32);
891 case L2_VTR_DISABLED:
892 return format (s, "none");
894 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
896 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
899 return format (s, "pop-1");
901 return format (s, "pop-2");
902 case L2_VTR_TRANSLATE_1_1:
903 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
904 case L2_VTR_TRANSLATE_1_2:
905 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
907 case L2_VTR_TRANSLATE_2_1:
908 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
909 case L2_VTR_TRANSLATE_2_2:
910 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
913 return format (s, "none");
918 Show bridge-domain state.
920 show bridge-domain [<bd_index>]
922 static clib_error_t *
923 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
925 vnet_main_t *vnm = vnet_get_main ();
926 bd_main_t *bdm = &bd_main;
927 clib_error_t *error = 0;
929 l2_bridge_domain_t *bd_config;
938 end = vec_len (l2input_main.bd_configs);
940 if (unformat (input, "%d", &bd_id))
942 if (unformat (input, "detail"))
944 else if (unformat (input, "det"))
946 if (unformat (input, "int"))
948 if (unformat (input, "arp"))
952 return clib_error_return (0,
953 "No operations on the default bridge domain are supported");
955 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
959 return clib_error_return (0, "No such bridge domain %d", bd_id);
961 vec_validate (l2input_main.bd_configs, bd_index);
962 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
963 if (bd_is_valid (bd_config))
970 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
975 /* Show all bridge-domains that have been initialized */
978 for (bd_index = start; bd_index < end; bd_index++)
980 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
981 if (bd_is_valid (bd_config))
987 "%=8s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
988 "BD-ID", "Index", "BSN", "Age(min)",
989 "Learning", "U-Forwrd", "UU-Flood", "Flooding",
990 "ARP-Term", "BVI-Intf");
993 if (bd_config->mac_age)
994 as = format (as, "%d", bd_config->mac_age);
996 as = format (as, "off");
998 "%=8d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
999 bd_config->bd_id, bd_index, bd_config->seq_num, as,
1000 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1002 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1004 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
1006 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1008 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1010 format_vnet_sw_if_index_name_with_NA,
1011 vnm, bd_config->bvi_sw_if_index);
1012 vec_reset_length (as);
1016 /* Show all member interfaces */
1018 vec_foreach_index (i, bd_config->members)
1020 l2_flood_member_t *member =
1021 vec_elt_at_index (bd_config->members, i);
1022 l2_input_config_t *int_config =
1023 l2input_intf_config (member->sw_if_index);
1024 u32 vtr_opr, dot1q, tag1, tag2;
1027 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1028 "Interface", "If-idx", "ISN", "SHG",
1029 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1031 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1033 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1034 format_vnet_sw_if_index_name, vnm,
1035 member->sw_if_index, member->sw_if_index,
1036 int_config->seq_num, member->shg,
1037 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1038 "-", i < bd_config->flood_count ? "*" : "-",
1039 format_vtr, vtr_opr, dot1q, tag1, tag2);
1043 if ((detail || arp) &&
1044 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1047 ip6_address_t *ip6_addr;
1049 vlib_cli_output (vm,
1050 "\n IP4/IP6 to MAC table for ARP Termination");
1053 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1055 vlib_cli_output (vm, "%=40U => %=20U",
1056 format_ip4_address, &ip4_addr,
1057 format_ethernet_address, &mac_addr);
1060 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1062 vlib_cli_output (vm, "%=40U => %=20U",
1063 format_ip6_address, ip6_addr,
1064 format_ethernet_address, &mac_addr);
1074 vlib_cli_output (vm, "no bridge-domains in use");
1082 * Show a summary of all the bridge-domain instances or detailed view of a
1083 * single bridge-domain. Bridge-domains are created by adding an interface
1084 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1088 * Example of displaying all bridge-domains:
1089 * @cliexstart{show bridge-domain}
1090 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1091 * 0 0 off off off off off local0
1092 * 200 1 on on on on off N/A
1095 * Example of displaying details of a single bridge-domains:
1096 * @cliexstart{show bridge-domain 200 detail}
1097 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1098 * 200 1 on on on on off N/A
1100 * Interface Index SHG BVI VLAN-Tag-Rewrite
1101 * GigabitEthernet0/8/0.200 3 0 - none
1102 * GigabitEthernet0/9/0.200 4 0 - none
1107 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1108 .path = "show bridge-domain",
1109 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1110 .function = bd_show,
1115 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1117 bd_main_t *bdm = &bd_main;
1118 vlib_main_t *vm = bdm->vlib_main;
1121 u32 bd_index = bd_find_index (bdm, a->bd_id);
1125 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1126 if (a->bd_id > L2_BD_ID_MAX)
1127 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1128 bd_index = bd_add_bd_index (bdm, a->bd_id);
1130 u32 enable_flags = 0, disable_flags = 0;
1132 enable_flags |= L2_FLOOD;
1134 disable_flags |= L2_FLOOD;
1137 enable_flags |= L2_UU_FLOOD;
1139 disable_flags |= L2_UU_FLOOD;
1142 enable_flags |= L2_FWD;
1144 disable_flags |= L2_FWD;
1147 enable_flags |= L2_LEARN;
1149 disable_flags |= L2_LEARN;
1152 enable_flags |= L2_ARP_TERM;
1154 disable_flags |= L2_ARP_TERM;
1157 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1160 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1162 bd_set_mac_age (vm, bd_index, a->mac_age);
1167 return VNET_API_ERROR_NO_SUCH_ENTRY;
1169 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1170 if (vec_len (l2input_main.bd_configs[bd_index].members))
1171 return VNET_API_ERROR_BD_IN_USE;
1172 rv = bd_delete (bdm, bd_index);
1179 Create or delete bridge-domain.
1181 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>]
1182 [flood <0|1>] [arp-term <0|1>] [mac-age <nn>] [del]
1185 static clib_error_t *
1186 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1187 vlib_cli_command_t * cmd)
1189 unformat_input_t _line_input, *line_input = &_line_input;
1190 clib_error_t *error = 0;
1193 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
1195 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1198 /* Get a line of input. */
1199 if (!unformat_user (input, unformat_line_input, line_input))
1202 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1204 if (unformat (line_input, "%d", &bd_id))
1206 else if (unformat (line_input, "flood %d", &flood))
1208 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1210 else if (unformat (line_input, "forward %d", &forward))
1212 else if (unformat (line_input, "learn %d", &learn))
1214 else if (unformat (line_input, "arp-term %d", &arp_term))
1216 else if (unformat (line_input, "mac-age %d", &mac_age))
1218 else if (unformat (line_input, "del"))
1221 flood = uu_flood = forward = learn = 0;
1229 error = clib_error_return (0, "bridge-domain-id not specified");
1235 error = clib_error_return (0, "bridge domain 0 can not be modified");
1241 error = clib_error_return (0, "mac age must be less than 256");
1245 memset (a, 0, sizeof (*a));
1248 a->flood = (u8) flood;
1249 a->uu_flood = (u8) uu_flood;
1250 a->forward = (u8) forward;
1251 a->learn = (u8) learn;
1252 a->arp_term = (u8) arp_term;
1253 a->mac_age = (u8) mac_age;
1255 rv = bd_add_del (a);
1261 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1263 case VNET_API_ERROR_BD_IN_USE:
1264 error = clib_error_return (0, "bridge domain in use - remove members");
1266 case VNET_API_ERROR_NO_SUCH_ENTRY:
1267 error = clib_error_return (0, "bridge domain ID does not exist");
1269 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1270 error = clib_error_return (0, "bridge domain 0 can not be modified");
1272 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1273 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1276 error = clib_error_return (0, "bd_add_del returned %d", rv);
1281 unformat_free (line_input);
1288 * Create/Delete bridge-domain instance
1292 * Example of creating bridge-domain 1:
1293 * @cliexstart{create bridge-domain 1}
1297 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1298 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1301 * vpp# show bridge-domain
1302 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1303 * 0 0 0 off off off off off off local0
1304 * 1 1 0 off on on off on off N/A
1305 * 2 2 0 60 on on off on on N/A
1309 * Example of delete bridge-domain 1:
1310 * @cliexstart{create bridge-domain 1 del}
1316 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1317 .path = "create bridge-domain",
1318 .short_help = "create bridge-domain <bridge-domain-id>"
1319 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1320 " [mac-age <nn>] [del]",
1321 .function = bd_add_del_command_fn,
1328 * fd.io coding-style-patch-verification: ON
1331 * eval: (c-set-style "gnu")