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->no_flood_count = 0;
62 bd_config->mac_by_ip4 = 0;
63 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
68 bd_find_index (bd_main_t * bdm, u32 bd_id)
70 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
77 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
79 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
80 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
82 /* mark this index taken */
83 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
85 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
87 vec_validate (l2input_main.bd_configs, rv);
88 l2input_main.bd_configs[rv].bd_id = bd_id;
94 bd_delete (bd_main_t * bdm, u32 bd_index)
96 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
97 u32 bd_id = bd->bd_id;
99 ip6_address_t *ip6_addr_key;
101 /* flush non-static MACs in BD and removed bd_id from hash table */
102 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
103 hash_unset (bdm->bd_index_by_bd_id, bd_id);
105 /* mark this index clear */
106 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
108 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
110 bd->feature_bitmap = 0;
113 vec_free (bd->bd_tag);
115 /* free memory used by BD */
116 vec_free (bd->members);
117 hash_free (bd->mac_by_ip4);
119 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
121 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
124 hash_free (bd->mac_by_ip6);
130 update_flood_count (l2_bridge_domain_t * bd_config)
132 bd_config->flood_count = (vec_len (bd_config->members) -
133 (bd_config->tun_master_count ?
134 bd_config->tun_normal_count : 0));
135 bd_config->flood_count -= bd_config->no_flood_count;
139 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
142 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
143 (vnet_get_main (), member->sw_if_index);
146 * Add one element to the vector
147 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
148 * When flooding, the bvi interface (if present) must be the last member
149 * processed due to how BVI processing can change the packet. To enable
150 * this order, we make the bvi interface the first in the vector and
151 * flooding walks the vector in reverse. The flood-count determines where
152 * in the member list to start the walk from.
154 switch (sw_if->flood_class)
156 case VNET_FLOOD_CLASS_NO_FLOOD:
157 bd_config->no_flood_count++;
158 ix = vec_len (bd_config->members);
160 case VNET_FLOOD_CLASS_BVI:
163 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
164 bd_config->tun_master_count++;
166 case VNET_FLOOD_CLASS_NORMAL:
167 ix = (vec_len (bd_config->members) -
168 bd_config->tun_normal_count - bd_config->no_flood_count);
170 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
171 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
172 bd_config->tun_normal_count++;
176 vec_insert_elts (bd_config->members, member, 1, ix);
177 update_flood_count (bd_config);
180 #define BD_REMOVE_ERROR_OK 0
181 #define BD_REMOVE_ERROR_NOT_FOUND 1
184 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
188 /* Find and delete the member */
189 vec_foreach_index (ix, bd_config->members)
191 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
192 if (m->sw_if_index == sw_if_index)
194 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
195 (vnet_get_main (), sw_if_index);
197 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
199 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
200 bd_config->tun_master_count--;
201 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
202 bd_config->tun_normal_count--;
203 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
204 bd_config->no_flood_count--;
206 vec_delete (bd_config->members, 1, ix);
207 update_flood_count (bd_config);
209 return BD_REMOVE_ERROR_OK;
213 return BD_REMOVE_ERROR_NOT_FOUND;
218 l2bd_init (vlib_main_t * vm)
220 bd_main_t *bdm = &bd_main;
221 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
223 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
224 * to packet drop only. Thus, packets received from any L2 interface with
225 * uninitialized bd_index of 0 can be dropped safely.
227 u32 bd_index = bd_add_bd_index (bdm, 0);
228 ASSERT (bd_index == 0);
229 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
235 VLIB_INIT_FUNCTION (l2bd_init);
239 Set the learn/forward/flood flags for the bridge domain.
240 Return 0 if ok, non-zero if for an error.
243 bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
246 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
247 bd_validate (bd_config);
248 u32 feature_bitmap = 0;
250 if (flags & L2_LEARN)
252 feature_bitmap |= L2INPUT_FEAT_LEARN;
256 feature_bitmap |= L2INPUT_FEAT_FWD;
258 if (flags & L2_FLOOD)
260 feature_bitmap |= L2INPUT_FEAT_FLOOD;
262 if (flags & L2_UU_FLOOD)
264 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
266 if (flags & L2_ARP_TERM)
268 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
273 bd_config->feature_bitmap |= feature_bitmap;
277 bd_config->feature_bitmap &= ~feature_bitmap;
280 return bd_config->feature_bitmap;
284 Set the mac age for the bridge domain.
287 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
289 l2_bridge_domain_t *bd_config;
292 vec_validate (l2input_main.bd_configs, bd_index);
293 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
294 bd_config->mac_age = age;
296 /* check if there is at least one bd with mac aging enabled */
297 vec_foreach (bd_config, l2input_main.bd_configs)
298 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
300 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
301 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
302 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
306 Set the tag for the bridge domain.
310 bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
313 l2_bridge_domain_t *bd_config;
314 vec_validate (l2input_main.bd_configs, bd_index);
315 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
317 old = bd_config->bd_tag;
321 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
325 bd_config->bd_tag = NULL;
332 Set bridge-domain learn enable/disable.
334 set bridge-domain learn <bd_id> [disable]
336 static clib_error_t *
337 bd_learn (vlib_main_t * vm,
338 unformat_input_t * input, vlib_cli_command_t * cmd)
340 bd_main_t *bdm = &bd_main;
341 clib_error_t *error = 0;
346 if (!unformat (input, "%d", &bd_id))
348 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
349 format_unformat_error, input);
354 return clib_error_return (0,
355 "No operations on the default bridge domain are supported");
357 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
360 return clib_error_return (0, "No such bridge domain %d", bd_id);
365 if (unformat (input, "disable"))
370 /* set the bridge domain flag */
371 bd_set_flags (vm, bd_index, L2_LEARN, enable);
378 * Layer 2 learning can be enabled and disabled on each
379 * interface and on each bridge-domain. Use this command to
380 * manage bridge-domains. It is enabled by default.
383 * Example of how to enable learning (where 200 is the bridge-domain-id):
384 * @cliexcmd{set bridge-domain learn 200}
385 * Example of how to disable learning (where 200 is the bridge-domain-id):
386 * @cliexcmd{set bridge-domain learn 200 disable}
389 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
390 .path = "set bridge-domain learn",
391 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
392 .function = bd_learn,
397 Set bridge-domain forward enable/disable.
399 set bridge-domain forward <bd_index> [disable]
401 static clib_error_t *
402 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
404 bd_main_t *bdm = &bd_main;
405 clib_error_t *error = 0;
410 if (!unformat (input, "%d", &bd_id))
412 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
413 format_unformat_error, input);
418 return clib_error_return (0,
419 "No operations on the default bridge domain are supported");
421 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
424 return clib_error_return (0, "No such bridge domain %d", bd_id);
429 if (unformat (input, "disable"))
434 /* set the bridge domain flag */
435 bd_set_flags (vm, bd_index, L2_FWD, enable);
443 * Layer 2 unicast forwarding can be enabled and disabled on each
444 * interface and on each bridge-domain. Use this command to
445 * manage bridge-domains. It is enabled by default.
448 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
449 * @cliexcmd{set bridge-domain forward 200}
450 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
451 * @cliexcmd{set bridge-domain forward 200 disable}
454 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
455 .path = "set bridge-domain forward",
456 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
462 Set bridge-domain flood enable/disable.
464 set bridge-domain flood <bd_index> [disable]
466 static clib_error_t *
467 bd_flood (vlib_main_t * vm,
468 unformat_input_t * input, vlib_cli_command_t * cmd)
470 bd_main_t *bdm = &bd_main;
471 clib_error_t *error = 0;
476 if (!unformat (input, "%d", &bd_id))
478 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
479 format_unformat_error, input);
484 return clib_error_return (0,
485 "No operations on the default bridge domain are supported");
487 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
490 return clib_error_return (0, "No such bridge domain %d", bd_id);
495 if (unformat (input, "disable"))
500 /* set the bridge domain flag */
501 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
508 * Layer 2 flooding can be enabled and disabled on each
509 * interface and on each bridge-domain. Use this command to
510 * manage bridge-domains. It is enabled by default.
513 * Example of how to enable flooding (where 200 is the bridge-domain-id):
514 * @cliexcmd{set bridge-domain flood 200}
515 * Example of how to disable flooding (where 200 is the bridge-domain-id):
516 * @cliexcmd{set bridge-domain flood 200 disable}
519 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
520 .path = "set bridge-domain flood",
521 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
522 .function = bd_flood,
527 Set bridge-domain unknown-unicast flood enable/disable.
529 set bridge-domain uu-flood <bd_index> [disable]
531 static clib_error_t *
532 bd_uu_flood (vlib_main_t * vm,
533 unformat_input_t * input, vlib_cli_command_t * cmd)
535 bd_main_t *bdm = &bd_main;
536 clib_error_t *error = 0;
541 if (!unformat (input, "%d", &bd_id))
543 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
544 format_unformat_error, input);
549 return clib_error_return (0,
550 "No operations on the default bridge domain are supported");
552 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
555 return clib_error_return (0, "No such bridge domain %d", bd_id);
560 if (unformat (input, "disable"))
565 /* set the bridge domain flag */
566 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
573 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
574 * bridge-domain. It is enabled by default.
577 * Example of how to enable unknown-unicast flooding (where 200 is the
579 * @cliexcmd{set bridge-domain uu-flood 200}
580 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
581 * @cliexcmd{set bridge-domain uu-flood 200 disable}
584 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
585 .path = "set bridge-domain uu-flood",
586 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
587 .function = bd_uu_flood,
592 Set bridge-domain arp term enable/disable.
594 set bridge-domain arp term <bridge-domain-id> [disable]
596 static clib_error_t *
597 bd_arp_term (vlib_main_t * vm,
598 unformat_input_t * input, vlib_cli_command_t * cmd)
600 bd_main_t *bdm = &bd_main;
601 clib_error_t *error = 0;
606 if (!unformat (input, "%d", &bd_id))
608 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
609 format_unformat_error, input);
614 return clib_error_return (0,
615 "No operations on the default bridge domain are supported");
617 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
621 return clib_error_return (0, "No such bridge domain %d", bd_id);
624 if (unformat (input, "disable"))
627 /* set the bridge domain flag */
628 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
634 static clib_error_t *
635 bd_mac_age (vlib_main_t * vm,
636 unformat_input_t * input, vlib_cli_command_t * cmd)
638 bd_main_t *bdm = &bd_main;
639 clib_error_t *error = 0;
644 if (!unformat (input, "%d", &bd_id))
646 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
647 format_unformat_error, input);
652 return clib_error_return (0,
653 "No operations on the default bridge domain are supported");
655 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
658 return clib_error_return (0, "No such bridge domain %d", bd_id);
662 if (!unformat (input, "%u", &age))
665 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
666 format_unformat_error, input);
670 /* set the bridge domain flag */
674 clib_error_return (0, "mac aging time cannot be bigger than 255");
677 bd_set_mac_age (vm, bd_index, (u8) age);
684 * Layer 2 mac aging can be enabled and disabled on each
685 * bridge-domain. Use this command to set or disable mac aging
686 * on specific bridge-domains. It is disabled by default.
689 * Example of how to set mac aging (where 200 is the bridge-domain-id and
690 * 5 is aging time in minutes):
691 * @cliexcmd{set bridge-domain mac-age 200 5}
692 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
693 * @cliexcmd{set bridge-domain flood 200 0}
696 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
697 .path = "set bridge-domain mac-age",
698 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
699 .function = bd_mac_age,
704 * Modify whether or not an existing bridge-domain should terminate and respond
705 * to ARP Requests. ARP Termination is disabled by default.
708 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
709 * @cliexcmd{set bridge-domain arp term 200}
710 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
711 * @cliexcmd{set bridge-domain arp term 200 disable}
714 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
715 .path = "set bridge-domain arp term",
716 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
717 .function = bd_arp_term,
723 * Add/delete IP address to MAC address mapping.
725 * The clib hash implementation stores uword entries in the hash table.
726 * The hash table mac_by_ip4 is keyed via IP4 address and store the
727 * 6-byte MAC address directly in the hash table entry uword.
729 * @warning This only works for 64-bit processor with 8-byte uword;
730 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
734 bd_add_del_ip_mac (u32 bd_index,
735 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
737 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
738 u64 new_mac = *(u64 *) mac_addr;
740 u16 *mac16 = (u16 *) & new_mac;
742 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
743 ASSERT (bd_is_valid (bd_cfg));
745 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
748 ip6_address_t *ip6_addr_key;
750 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
754 { /* new entry - allocate and craete ip6 address key */
755 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
756 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
758 else if (*old_mac == new_mac)
759 { /* same mac entry already exist for ip6 address */
763 { /* updat mac for ip6 address */
764 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
765 ip6_addr_key = (ip6_address_t *) hp->key;
767 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
771 if (old_mac && (*old_mac == new_mac))
773 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
774 ip6_addr_key = (ip6_address_t *) hp->key;
775 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
776 clib_mem_free (ip6_addr_key);
784 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
785 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
788 if (old_mac && (*old_mac == new_mac))
789 return 0; /* mac entry already exist */
790 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
794 if (old_mac && (*old_mac == new_mac))
795 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
804 Set bridge-domain arp entry add/delete.
806 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
808 static clib_error_t *
809 bd_arp_entry (vlib_main_t * vm,
810 unformat_input_t * input, vlib_cli_command_t * cmd)
812 bd_main_t *bdm = &bd_main;
813 clib_error_t *error = 0;
821 if (!unformat (input, "%d", &bd_id))
823 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
824 format_unformat_error, input);
829 return clib_error_return (0,
830 "No operations on the default bridge domain are supported");
832 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
837 return clib_error_return (0, "No such bridge domain %d", bd_id);
839 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
843 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
849 error = clib_error_return (0, "expecting IP address but got `%U'",
850 format_unformat_error, input);
854 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
856 error = clib_error_return (0, "expecting MAC address but got `%U'",
857 format_unformat_error, input);
861 if (unformat (input, "del"))
866 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
867 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
869 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
870 is_add ? "add" : "del",
872 format_ip4_address : format_ip6_address,
873 ip_addr, format_ethernet_address, mac_addr);
881 * Add an ARP entry to an existing bridge-domain.
884 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
885 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
886 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
887 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
890 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
891 .path = "set bridge-domain arp entry",
892 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
893 .function = bd_arp_entry,
898 format_vtr (u8 * s, va_list * args)
900 u32 vtr_op = va_arg (*args, u32);
901 u32 dot1q = va_arg (*args, u32);
902 u32 tag1 = va_arg (*args, u32);
903 u32 tag2 = va_arg (*args, u32);
906 case L2_VTR_DISABLED:
907 return format (s, "none");
909 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
911 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
914 return format (s, "pop-1");
916 return format (s, "pop-2");
917 case L2_VTR_TRANSLATE_1_1:
918 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
919 case L2_VTR_TRANSLATE_1_2:
920 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
922 case L2_VTR_TRANSLATE_2_1:
923 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
924 case L2_VTR_TRANSLATE_2_2:
925 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
928 return format (s, "none");
933 Show bridge-domain state.
935 show bridge-domain [<bd_index>]
937 static clib_error_t *
938 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
940 vnet_main_t *vnm = vnet_get_main ();
941 bd_main_t *bdm = &bd_main;
942 clib_error_t *error = 0;
944 l2_bridge_domain_t *bd_config;
954 end = vec_len (l2input_main.bd_configs);
956 if (unformat (input, "%d", &bd_id))
958 if (unformat (input, "detail"))
960 else if (unformat (input, "det"))
962 if (unformat (input, "int"))
964 if (unformat (input, "arp"))
966 if (unformat (input, "bd-tag"))
970 return clib_error_return (0,
971 "No operations on the default bridge domain are supported");
973 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
977 return clib_error_return (0, "No such bridge domain %d", bd_id);
979 vec_validate (l2input_main.bd_configs, bd_index);
980 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
981 if (bd_is_valid (bd_config))
988 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
993 /* Show all bridge-domains that have been initialized */
996 for (bd_index = start; bd_index < end; bd_index++)
998 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
999 if (bd_is_valid (bd_config))
1004 vlib_cli_output (vm,
1005 "%=8s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
1006 "BD-ID", "Index", "BSN", "Age(min)",
1007 "Learning", "U-Forwrd", "UU-Flood", "Flooding",
1008 "ARP-Term", "BVI-Intf");
1011 if (bd_config->mac_age)
1012 as = format (as, "%d", bd_config->mac_age);
1014 as = format (as, "off");
1015 vlib_cli_output (vm,
1016 "%=8d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
1017 bd_config->bd_id, bd_index, bd_config->seq_num, as,
1018 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1020 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1022 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
1024 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1026 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1028 format_vnet_sw_if_index_name_with_NA,
1029 vnm, bd_config->bvi_sw_if_index);
1030 vec_reset_length (as);
1034 /* Show all member interfaces */
1036 vec_foreach_index (i, bd_config->members)
1038 l2_flood_member_t *member =
1039 vec_elt_at_index (bd_config->members, i);
1040 u8 swif_seq_num = *l2fib_swif_seq_num (member->sw_if_index);
1041 u32 vtr_opr, dot1q, tag1, tag2;
1044 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1045 "Interface", "If-idx", "ISN", "SHG",
1046 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1048 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1050 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1051 format_vnet_sw_if_index_name, vnm,
1052 member->sw_if_index, member->sw_if_index,
1053 swif_seq_num, member->shg,
1054 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1055 "-", i < bd_config->flood_count ? "*" : "-",
1056 format_vtr, vtr_opr, dot1q, tag1, tag2);
1060 if ((detail || arp) &&
1061 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1064 ip6_address_t *ip6_addr;
1066 vlib_cli_output (vm,
1067 "\n IP4/IP6 to MAC table for ARP Termination");
1070 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1072 vlib_cli_output (vm, "%=40U => %=20U",
1073 format_ip4_address, &ip4_addr,
1074 format_ethernet_address, &mac_addr);
1077 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1079 vlib_cli_output (vm, "%=40U => %=20U",
1080 format_ip6_address, ip6_addr,
1081 format_ethernet_address, &mac_addr);
1086 if ((detail || bd_tag) && (bd_config->bd_tag))
1088 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1097 vlib_cli_output (vm, "no bridge-domains in use");
1105 * Show a summary of all the bridge-domain instances or detailed view of a
1106 * single bridge-domain. Bridge-domains are created by adding an interface
1107 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1111 * Example of displaying all bridge-domains:
1112 * @cliexstart{show bridge-domain}
1113 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1114 * 0 0 off off off off off local0
1115 * 200 1 on on on on off N/A
1118 * Example of displaying details of a single bridge-domains:
1119 * @cliexstart{show bridge-domain 200 detail}
1120 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1121 * 200 1 on on on on off N/A
1123 * Interface Index SHG BVI VLAN-Tag-Rewrite
1124 * GigabitEthernet0/8/0.200 3 0 - none
1125 * GigabitEthernet0/9/0.200 4 0 - none
1130 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1131 .path = "show bridge-domain",
1132 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
1133 .function = bd_show,
1138 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1140 bd_main_t *bdm = &bd_main;
1141 vlib_main_t *vm = bdm->vlib_main;
1144 u32 bd_index = bd_find_index (bdm, a->bd_id);
1148 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1149 if (a->bd_id > L2_BD_ID_MAX)
1150 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1151 bd_index = bd_add_bd_index (bdm, a->bd_id);
1153 u32 enable_flags = 0, disable_flags = 0;
1155 enable_flags |= L2_FLOOD;
1157 disable_flags |= L2_FLOOD;
1160 enable_flags |= L2_UU_FLOOD;
1162 disable_flags |= L2_UU_FLOOD;
1165 enable_flags |= L2_FWD;
1167 disable_flags |= L2_FWD;
1170 enable_flags |= L2_LEARN;
1172 disable_flags |= L2_LEARN;
1175 enable_flags |= L2_ARP_TERM;
1177 disable_flags |= L2_ARP_TERM;
1180 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1183 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1185 bd_set_mac_age (vm, bd_index, a->mac_age);
1188 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1194 return VNET_API_ERROR_NO_SUCH_ENTRY;
1196 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1197 if (vec_len (l2input_main.bd_configs[bd_index].members))
1198 return VNET_API_ERROR_BD_IN_USE;
1199 rv = bd_delete (bdm, bd_index);
1206 Create or delete bridge-domain.
1208 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1209 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
1212 static clib_error_t *
1213 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1214 vlib_cli_command_t * cmd)
1216 unformat_input_t _line_input, *line_input = &_line_input;
1217 clib_error_t *error = 0;
1220 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
1223 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1226 /* Get a line of input. */
1227 if (!unformat_user (input, unformat_line_input, line_input))
1230 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1232 if (unformat (line_input, "%d", &bd_id))
1234 else if (unformat (line_input, "flood %d", &flood))
1236 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1238 else if (unformat (line_input, "forward %d", &forward))
1240 else if (unformat (line_input, "learn %d", &learn))
1242 else if (unformat (line_input, "arp-term %d", &arp_term))
1244 else if (unformat (line_input, "mac-age %d", &mac_age))
1246 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1248 else if (unformat (line_input, "del"))
1251 flood = uu_flood = forward = learn = 0;
1259 error = clib_error_return (0, "bridge-domain-id not specified");
1265 error = clib_error_return (0, "bridge domain 0 can not be modified");
1271 error = clib_error_return (0, "mac age must be less than 256");
1274 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1276 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1280 memset (a, 0, sizeof (*a));
1283 a->flood = (u8) flood;
1284 a->uu_flood = (u8) uu_flood;
1285 a->forward = (u8) forward;
1286 a->learn = (u8) learn;
1287 a->arp_term = (u8) arp_term;
1288 a->mac_age = (u8) mac_age;
1291 rv = bd_add_del (a);
1297 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1299 case VNET_API_ERROR_BD_IN_USE:
1300 error = clib_error_return (0, "bridge domain in use - remove members");
1302 case VNET_API_ERROR_NO_SUCH_ENTRY:
1303 error = clib_error_return (0, "bridge domain ID does not exist");
1305 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1306 error = clib_error_return (0, "bridge domain 0 can not be modified");
1308 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1309 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1312 error = clib_error_return (0, "bd_add_del returned %d", rv);
1318 unformat_free (line_input);
1325 * Create/Delete bridge-domain instance
1329 * Example of creating bridge-domain 1:
1330 * @cliexstart{create bridge-domain 1}
1334 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1335 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1338 * vpp# show bridge-domain
1339 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1340 * 0 0 0 off off off off off off local0
1341 * 1 1 0 off on on off on off N/A
1342 * 2 2 0 60 on on off on on N/A
1346 * Example of delete bridge-domain 1:
1347 * @cliexstart{create bridge-domain 1 del}
1353 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1354 .path = "create bridge-domain",
1355 .short_help = "create bridge-domain <bridge-domain-id>"
1356 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1357 " [mac-age <nn>] [bd-tag <tag>] [del]",
1358 .function = bd_add_del_command_fn,
1365 * fd.io coding-style-patch-verification: ON
1368 * eval: (c-set-style "gnu")