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,
737 const ip46_address_t * ip,
738 const mac_address_t * mac, u8 is_add)
740 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
741 u64 new_mac = mac_address_as_u64 (mac);
744 /* make sure uword is 8 bytes */
745 ASSERT (sizeof (uword) == sizeof (u64));
746 ASSERT (bd_is_valid (bd_cfg));
748 if (IP46_TYPE_IP6 == type)
750 ip6_address_t *ip6_addr_key;
752 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, &ip->ip6);
757 /* new entry - allocate and create ip6 address key */
758 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
759 clib_memcpy (ip6_addr_key, &ip->ip6, sizeof (ip6_address_t));
761 else if (*old_mac == new_mac)
763 /* same mac entry already exist for ip6 address */
768 /* update mac for ip6 address */
769 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
770 ip6_addr_key = (ip6_address_t *) hp->key;
772 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
776 if (old_mac && (*old_mac == new_mac))
778 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
779 ip6_addr_key = (ip6_address_t *) hp->key;
780 hash_unset_mem (bd_cfg->mac_by_ip6, &ip->ip6);
781 clib_mem_free (ip6_addr_key);
789 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
792 if (old_mac && (*old_mac == new_mac))
793 /* mac entry already exist */
795 hash_set (bd_cfg->mac_by_ip4, ip->ip4.as_u32, new_mac);
799 if (old_mac && (*old_mac == new_mac))
800 hash_unset (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
809 Set bridge-domain arp entry add/delete.
811 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
813 static clib_error_t *
814 bd_arp_entry (vlib_main_t * vm,
815 unformat_input_t * input, vlib_cli_command_t * cmd)
817 ip46_address_t ip_addr = ip46_address_initializer;
818 ip46_type_t type = IP46_TYPE_IP4;
819 bd_main_t *bdm = &bd_main;
820 clib_error_t *error = 0;
826 if (!unformat (input, "%d", &bd_id))
828 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
829 format_unformat_error, input);
834 return clib_error_return (0,
835 "No operations on the default bridge domain are supported");
837 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
842 return clib_error_return (0, "No such bridge domain %d", bd_id);
844 if (unformat (input, "%U", unformat_ip4_address, &ip_addr.ip4))
846 type = IP46_TYPE_IP4;
848 else if (unformat (input, "%U", unformat_ip6_address, &ip_addr.ip6))
850 type = IP46_TYPE_IP6;
854 error = clib_error_return (0, "expecting IP address but got `%U'",
855 format_unformat_error, input);
859 if (!unformat (input, "%U", unformat_mac_address_t, &mac))
861 error = clib_error_return (0, "expecting MAC address but got `%U'",
862 format_unformat_error, input);
866 if (unformat (input, "del"))
871 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
872 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, is_add))
874 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
875 is_add ? "add" : "del",
876 format_ip46_address, &ip_addr, IP46_TYPE_ANY,
877 format_mac_address_t, &mac);
885 * Add an ARP entry to an existing bridge-domain.
888 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
889 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
890 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
891 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
894 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
895 .path = "set bridge-domain arp entry",
896 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
897 .function = bd_arp_entry,
902 format_vtr (u8 * s, va_list * args)
904 u32 vtr_op = va_arg (*args, u32);
905 u32 dot1q = va_arg (*args, u32);
906 u32 tag1 = va_arg (*args, u32);
907 u32 tag2 = va_arg (*args, u32);
910 case L2_VTR_DISABLED:
911 return format (s, "none");
913 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
915 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
918 return format (s, "pop-1");
920 return format (s, "pop-2");
921 case L2_VTR_TRANSLATE_1_1:
922 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
923 case L2_VTR_TRANSLATE_1_2:
924 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
926 case L2_VTR_TRANSLATE_2_1:
927 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
928 case L2_VTR_TRANSLATE_2_2:
929 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
932 return format (s, "none");
937 format_uu_cfg (u8 * s, va_list * args)
939 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
941 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
942 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
943 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
944 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
945 return (format (s, "flood"));
947 return (format (s, "drop"));
951 Show bridge-domain state.
953 show bridge-domain [<bd_index>]
955 static clib_error_t *
956 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
958 vnet_main_t *vnm = vnet_get_main ();
959 bd_main_t *bdm = &bd_main;
960 clib_error_t *error = 0;
962 l2_bridge_domain_t *bd_config;
972 end = vec_len (l2input_main.bd_configs);
974 if (unformat (input, "%d", &bd_id))
976 if (unformat (input, "detail"))
978 else if (unformat (input, "det"))
980 if (unformat (input, "int"))
982 if (unformat (input, "arp"))
984 if (unformat (input, "bd-tag"))
988 return clib_error_return (0,
989 "No operations on the default bridge domain are supported");
991 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
995 return clib_error_return (0, "No such bridge domain %d", bd_id);
997 vec_validate (l2input_main.bd_configs, bd_index);
998 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
999 if (bd_is_valid (bd_config))
1006 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1011 /* Show all bridge-domains that have been initialized */
1014 for (bd_index = start; bd_index < end; bd_index++)
1016 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1017 if (bd_is_valid (bd_config))
1022 vlib_cli_output (vm,
1023 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s %=11s",
1024 "BD-ID", "Index", "BSN", "Age(min)",
1025 "Learning", "U-Forwrd", "UU-Flood",
1026 "Flooding", "ARP-Term", "BVI-Intf");
1029 if (bd_config->mac_age)
1030 as = format (as, "%d", bd_config->mac_age);
1032 as = format (as, "off");
1033 vlib_cli_output (vm,
1034 "%=8d %=7d %=4d %=9v %=9s %=9s %=11U %=9s %=9s %=11U",
1035 bd_config->bd_id, bd_index, bd_config->seq_num, as,
1036 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1038 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1040 format_uu_cfg, bd_config,
1041 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1043 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
1045 format_vnet_sw_if_index_name_with_NA,
1046 vnm, bd_config->bvi_sw_if_index);
1047 vec_reset_length (as);
1051 /* Show all member interfaces */
1053 vec_foreach_index (i, bd_config->members)
1055 l2_flood_member_t *member =
1056 vec_elt_at_index (bd_config->members, i);
1057 u8 swif_seq_num = *l2fib_swif_seq_num (member->sw_if_index);
1058 u32 vtr_opr, dot1q, tag1, tag2;
1061 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1062 "Interface", "If-idx", "ISN", "SHG",
1063 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1065 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1067 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1068 format_vnet_sw_if_index_name, vnm,
1069 member->sw_if_index, member->sw_if_index,
1070 swif_seq_num, member->shg,
1071 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1072 "-", i < bd_config->flood_count ? "*" : "-",
1073 format_vtr, vtr_opr, dot1q, tag1, tag2);
1075 if (~0 != bd_config->uu_fwd_sw_if_index)
1076 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1077 format_vnet_sw_if_index_name, vnm,
1078 bd_config->uu_fwd_sw_if_index,
1079 bd_config->uu_fwd_sw_if_index,
1080 0, 0, "uu", "-", "None");
1084 if ((detail || arp) &&
1085 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1088 ip6_address_t *ip6_addr;
1090 vlib_cli_output (vm,
1091 "\n IP4/IP6 to MAC table for ARP Termination");
1094 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1096 vlib_cli_output (vm, "%=40U => %=20U",
1097 format_ip4_address, &ip4_addr,
1098 format_ethernet_address, &mac_addr);
1101 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1103 vlib_cli_output (vm, "%=40U => %=20U",
1104 format_ip6_address, ip6_addr,
1105 format_ethernet_address, &mac_addr);
1110 if ((detail || bd_tag) && (bd_config->bd_tag))
1112 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1121 vlib_cli_output (vm, "no bridge-domains in use");
1129 * Show a summary of all the bridge-domain instances or detailed view of a
1130 * single bridge-domain. Bridge-domains are created by adding an interface
1131 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1135 * Example of displaying all bridge-domains:
1136 * @cliexstart{show bridge-domain}
1137 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1138 * 0 0 off off off off off local0
1139 * 200 1 on on on on off N/A
1142 * Example of displaying details of a single bridge-domains:
1143 * @cliexstart{show bridge-domain 200 detail}
1144 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1145 * 200 1 on on on on off N/A
1147 * Interface Index SHG BVI VLAN-Tag-Rewrite
1148 * GigabitEthernet0/8/0.200 3 0 - none
1149 * GigabitEthernet0/9/0.200 4 0 - none
1154 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1155 .path = "show bridge-domain",
1156 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
1157 .function = bd_show,
1162 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1164 bd_main_t *bdm = &bd_main;
1165 vlib_main_t *vm = bdm->vlib_main;
1168 u32 bd_index = bd_find_index (bdm, a->bd_id);
1172 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1173 if (a->bd_id > L2_BD_ID_MAX)
1174 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1175 bd_index = bd_add_bd_index (bdm, a->bd_id);
1177 bd_flags_t enable_flags = 0, disable_flags = 0;
1179 enable_flags |= L2_FLOOD;
1181 disable_flags |= L2_FLOOD;
1184 enable_flags |= L2_UU_FLOOD;
1186 disable_flags |= L2_UU_FLOOD;
1189 enable_flags |= L2_FWD;
1191 disable_flags |= L2_FWD;
1194 enable_flags |= L2_LEARN;
1196 disable_flags |= L2_LEARN;
1199 enable_flags |= L2_ARP_TERM;
1201 disable_flags |= L2_ARP_TERM;
1204 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1207 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1209 bd_set_mac_age (vm, bd_index, a->mac_age);
1212 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1218 return VNET_API_ERROR_NO_SUCH_ENTRY;
1220 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1221 if (vec_len (l2input_main.bd_configs[bd_index].members))
1222 return VNET_API_ERROR_BD_IN_USE;
1223 rv = bd_delete (bdm, bd_index);
1230 Create or delete bridge-domain.
1232 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1233 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
1236 static clib_error_t *
1237 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1238 vlib_cli_command_t * cmd)
1240 unformat_input_t _line_input, *line_input = &_line_input;
1241 clib_error_t *error = 0;
1244 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
1247 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1250 /* Get a line of input. */
1251 if (!unformat_user (input, unformat_line_input, line_input))
1254 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1256 if (unformat (line_input, "%d", &bd_id))
1258 else if (unformat (line_input, "flood %d", &flood))
1260 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1262 else if (unformat (line_input, "forward %d", &forward))
1264 else if (unformat (line_input, "learn %d", &learn))
1266 else if (unformat (line_input, "arp-term %d", &arp_term))
1268 else if (unformat (line_input, "mac-age %d", &mac_age))
1270 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1272 else if (unformat (line_input, "del"))
1275 flood = uu_flood = forward = learn = 0;
1283 error = clib_error_return (0, "bridge-domain-id not specified");
1289 error = clib_error_return (0, "bridge domain 0 can not be modified");
1295 error = clib_error_return (0, "mac age must be less than 256");
1298 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1300 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1304 clib_memset (a, 0, sizeof (*a));
1307 a->flood = (u8) flood;
1308 a->uu_flood = (u8) uu_flood;
1309 a->forward = (u8) forward;
1310 a->learn = (u8) learn;
1311 a->arp_term = (u8) arp_term;
1312 a->mac_age = (u8) mac_age;
1315 rv = bd_add_del (a);
1321 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1323 case VNET_API_ERROR_BD_IN_USE:
1324 error = clib_error_return (0, "bridge domain in use - remove members");
1326 case VNET_API_ERROR_NO_SUCH_ENTRY:
1327 error = clib_error_return (0, "bridge domain ID does not exist");
1329 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1330 error = clib_error_return (0, "bridge domain 0 can not be modified");
1332 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1333 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1336 error = clib_error_return (0, "bd_add_del returned %d", rv);
1342 unformat_free (line_input);
1349 * Create/Delete bridge-domain instance
1353 * Example of creating bridge-domain 1:
1354 * @cliexstart{create bridge-domain 1}
1358 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1359 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1362 * vpp# show bridge-domain
1363 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1364 * 0 0 0 off off off off off off local0
1365 * 1 1 0 off on on off on off N/A
1366 * 2 2 0 60 on on off on on N/A
1370 * Example of delete bridge-domain 1:
1371 * @cliexstart{create bridge-domain 1 del}
1377 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1378 .path = "create bridge-domain",
1379 .short_help = "create bridge-domain <bridge-domain-id>"
1380 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1381 " [mac-age <nn>] [bd-tag <tag>] [del]",
1382 .function = bd_add_del_command_fn,
1389 * fd.io coding-style-patch-verification: ON
1392 * eval: (c-set-style "gnu")