blob: c7392c03b58657762869ece8fe32b568b00a51df [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_bd.c : layer 2 bridge domain
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#include <vlib/vlib.h>
19#include <vnet/vnet.h>
20#include <vlib/cli.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>
Damjan Mariond171d482016-12-05 14:16:38 +010026#include <vnet/l2/l2_learn.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070027#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>
31
32#include <vppinfra/error.h>
33#include <vppinfra/hash.h>
34#include <vppinfra/vec.h>
35
Billy McFall22aa3e92016-09-09 08:46:40 -040036/**
37 * @file
38 * @brief Ethernet Bridge Domain.
39 *
40 * Code in this file manages Layer 2 bridge domains.
41 *
42 */
43
Ed Warnickecb9cada2015-12-08 15:45:58 -070044bd_main_t bd_main;
45
Dave Barach97d8dc22016-08-15 15:31:15 -040046/**
Chris Luke16bcf7d2016-09-01 14:31:46 -040047 Init bridge domain if not done already.
Dave Barach97d8dc22016-08-15 15:31:15 -040048 For feature bitmap, set all bits except ARP termination
49*/
Damjan Marion99d8c762015-12-14 15:01:56 +010050void
Dave Barach97d8dc22016-08-15 15:31:15 -040051bd_validate (l2_bridge_domain_t * bd_config)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
Eyal Barib1352ed2017-04-07 23:14:17 +030053 if (bd_is_valid (bd_config))
54 return;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +020055 bd_config->feature_bitmap =
56 ~(L2INPUT_FEAT_ARP_TERM | L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_ARP_UFWD);
Eyal Barib1352ed2017-04-07 23:14:17 +030057 bd_config->bvi_sw_if_index = ~0;
Neale Rannsb4743802018-09-05 09:13:57 -070058 bd_config->uu_fwd_sw_if_index = ~0;
Eyal Barib1352ed2017-04-07 23:14:17 +030059 bd_config->members = 0;
60 bd_config->flood_count = 0;
61 bd_config->tun_master_count = 0;
62 bd_config->tun_normal_count = 0;
Eyal Barib6b04f02018-07-11 14:01:36 +030063 bd_config->no_flood_count = 0;
Eyal Barib1352ed2017-04-07 23:14:17 +030064 bd_config->mac_by_ip4 = 0;
65 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
66 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -070067}
68
Dave Barach97d8dc22016-08-15 15:31:15 -040069u32
Eyal Barib1352ed2017-04-07 23:14:17 +030070bd_find_index (bd_main_t * bdm, u32 bd_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -070071{
Eyal Barib1352ed2017-04-07 23:14:17 +030072 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
73 if (!p)
74 return ~0;
75 return p[0];
76}
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
Eyal Barib1352ed2017-04-07 23:14:17 +030078u32
79bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
80{
81 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
82 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
Dave Barach97d8dc22016-08-15 15:31:15 -040083
Eyal Barib1352ed2017-04-07 23:14:17 +030084 /* mark this index taken */
Ed Warnickecb9cada2015-12-08 15:45:58 -070085 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
86
87 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
88
89 vec_validate (l2input_main.bd_configs, rv);
90 l2input_main.bd_configs[rv].bd_id = bd_id;
Jerome Tollet5f93e3b2020-12-18 09:44:24 +010091 l2input_main.bd_configs[rv].learn_limit =
92 l2learn_main.bd_default_learn_limit;
93 l2input_main.bd_configs[rv].learn_count = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094
95 return rv;
96}
97
John Loe26c81f2019-01-07 15:16:33 -050098static inline void
99bd_free_ip_mac_tables (l2_bridge_domain_t * bd)
100{
101 u64 mac_addr;
102 ip6_address_t *ip6_addr_key;
103
104 hash_free (bd->mac_by_ip4);
John Loe26c81f2019-01-07 15:16:33 -0500105 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
106 ({
107 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
108 }));
John Loe26c81f2019-01-07 15:16:33 -0500109 hash_free (bd->mac_by_ip6);
110}
111
Eyal Barib1352ed2017-04-07 23:14:17 +0300112static int
113bd_delete (bd_main_t * bdm, u32 bd_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
John Lod77630a2017-04-28 00:33:36 -0400115 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
116 u32 bd_id = bd->bd_id;
Eyal Bariafc47aa2017-04-20 14:45:17 +0300117
John Lod48c8eb2017-05-05 12:35:25 -0400118 /* flush non-static MACs in BD and removed bd_id from hash table */
119 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
Eyal Barib1352ed2017-04-07 23:14:17 +0300120 hash_unset (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400121
122 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124
John Lod77630a2017-04-28 00:33:36 -0400125 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
126 bd->bd_id = ~0;
127 bd->feature_bitmap = 0;
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100128 bd->learn_limit = 0;
129 bd->learn_count = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130
Jerome Tollet48304142017-09-05 12:13:22 +0100131 /* free BD tag */
132 vec_free (bd->bd_tag);
133
John Lod48c8eb2017-05-05 12:35:25 -0400134 /* free memory used by BD */
John Lod77630a2017-04-28 00:33:36 -0400135 vec_free (bd->members);
John Loe26c81f2019-01-07 15:16:33 -0500136 bd_free_ip_mac_tables (bd);
John Loda1f2c72017-03-24 20:11:15 -0400137
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 return 0;
139}
140
Eyal Baric5b13602016-11-24 19:42:43 +0200141static void
142update_flood_count (l2_bridge_domain_t * bd_config)
143{
Neale Ranns87dad112018-04-09 01:53:01 -0700144 bd_config->flood_count = (vec_len (bd_config->members) -
145 (bd_config->tun_master_count ?
146 bd_config->tun_normal_count : 0));
147 bd_config->flood_count -= bd_config->no_flood_count;
Eyal Baric5b13602016-11-24 19:42:43 +0200148}
149
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150void
Dave Barach97d8dc22016-08-15 15:31:15 -0400151bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152{
Neale Ranns87dad112018-04-09 01:53:01 -0700153 u32 ix = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200154 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
155 (vnet_get_main (), member->sw_if_index);
156
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 /*
158 * Add one element to the vector
Neale Ranns87dad112018-04-09 01:53:01 -0700159 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
Dave Barach97d8dc22016-08-15 15:31:15 -0400160 * When flooding, the bvi interface (if present) must be the last member
161 * processed due to how BVI processing can change the packet. To enable
162 * this order, we make the bvi interface the first in the vector and
Neale Ranns87dad112018-04-09 01:53:01 -0700163 * flooding walks the vector in reverse. The flood-count determines where
164 * in the member list to start the walk from.
Dave Barach97d8dc22016-08-15 15:31:15 -0400165 */
Eyal Baric5b13602016-11-24 19:42:43 +0200166 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400167 {
Neale Ranns87dad112018-04-09 01:53:01 -0700168 case VNET_FLOOD_CLASS_NO_FLOOD:
169 bd_config->no_flood_count++;
170 ix = vec_len (bd_config->members);
171 break;
172 case VNET_FLOOD_CLASS_BVI:
173 ix = 0;
174 break;
Eyal Baric5b13602016-11-24 19:42:43 +0200175 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
176 bd_config->tun_master_count++;
177 /* Fall through */
Eyal Baric5b13602016-11-24 19:42:43 +0200178 case VNET_FLOOD_CLASS_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700179 ix = (vec_len (bd_config->members) -
180 bd_config->tun_normal_count - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200181 break;
182 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700183 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200184 bd_config->tun_normal_count++;
185 break;
186 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Eyal Baric5b13602016-11-24 19:42:43 +0200188 vec_insert_elts (bd_config->members, member, 1, ix);
189 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190}
191
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192#define BD_REMOVE_ERROR_OK 0
193#define BD_REMOVE_ERROR_NOT_FOUND 1
194
195u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400196bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
198 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400199
200 /* Find and delete the member */
201 vec_foreach_index (ix, bd_config->members)
202 {
Eyal Baric5b13602016-11-24 19:42:43 +0200203 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
204 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400205 {
Eyal Baric5b13602016-11-24 19:42:43 +0200206 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
207 (vnet_get_main (), sw_if_index);
208
209 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
210 {
211 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
212 bd_config->tun_master_count--;
213 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
214 bd_config->tun_normal_count--;
Eyal Barib6b04f02018-07-11 14:01:36 +0300215 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
216 bd_config->no_flood_count--;
Eyal Baric5b13602016-11-24 19:42:43 +0200217 }
Eyal Bari25b36672017-03-02 10:43:19 +0200218 vec_delete (bd_config->members, 1, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200219 update_flood_count (bd_config);
220
Dave Barach97d8dc22016-08-15 15:31:15 -0400221 return BD_REMOVE_ERROR_OK;
222 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 }
224
225 return BD_REMOVE_ERROR_NOT_FOUND;
226}
227
228
Dave Barach97d8dc22016-08-15 15:31:15 -0400229clib_error_t *
230l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231{
232 bd_main_t *bdm = &bd_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400233 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
234 /*
Dave Barach11fb09e2020-08-06 12:10:09 -0400235 * create a placeholder bd with bd_id of 0 and bd_index of 0 with feature set
Dave Barach97d8dc22016-08-15 15:31:15 -0400236 * to packet drop only. Thus, packets received from any L2 interface with
237 * uninitialized bd_index of 0 can be dropped safely.
238 */
Eyal Barib1352ed2017-04-07 23:14:17 +0300239 u32 bd_index = bd_add_bd_index (bdm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400241 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Choonho Son05480792017-03-29 20:07:45 +0900242
243 bdm->vlib_main = vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244 return 0;
245}
246
247VLIB_INIT_FUNCTION (l2bd_init);
248
Neale Ranns47a3d992020-09-29 15:38:51 +0000249l2_bridge_domain_t *
250bd_get (u32 bd_index)
251{
252 if (bd_index < vec_len (l2input_main.bd_configs))
253 return (vec_elt_at_index (l2input_main.bd_configs, bd_index));
254 return (NULL);
255}
256
257u32
258bd_input_walk (u32 bd_index, bd_input_walk_fn_t fn, void *data)
259{
260 l2_flood_member_t *member;
261 l2_bridge_domain_t *bd;
262 u32 sw_if_index;
263
264 sw_if_index = ~0;
265 bd = bd_get (bd_index);
266
267 ASSERT (bd);
268
269 vec_foreach (member, bd->members)
270 {
271 if (WALK_STOP == fn (bd_index, member->sw_if_index))
272 {
273 sw_if_index = member->sw_if_index;
274 break;
275 }
276 }
277
278 return (sw_if_index);
279}
280
281static void
282b2_input_recache (u32 bd_index)
283{
284 bd_input_walk (bd_index, l2input_recache, NULL);
285}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286
Dave Barach97d8dc22016-08-15 15:31:15 -0400287/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400288 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400289 Return 0 if ok, non-zero if for an error.
290*/
291u32
Neale Rannsb4743802018-09-05 09:13:57 -0700292bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
Dave Barach97d8dc22016-08-15 15:31:15 -0400293{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
Eyal Bariafc47aa2017-04-20 14:45:17 +0300295 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 bd_validate (bd_config);
Eyal Bariafc47aa2017-04-20 14:45:17 +0300297 u32 feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298
Dave Barach97d8dc22016-08-15 15:31:15 -0400299 if (flags & L2_LEARN)
300 {
301 feature_bitmap |= L2INPUT_FEAT_LEARN;
302 }
303 if (flags & L2_FWD)
304 {
305 feature_bitmap |= L2INPUT_FEAT_FWD;
306 }
307 if (flags & L2_FLOOD)
308 {
309 feature_bitmap |= L2INPUT_FEAT_FLOOD;
310 }
311 if (flags & L2_UU_FLOOD)
312 {
313 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
314 }
315 if (flags & L2_ARP_TERM)
316 {
317 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
318 }
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200319 if (flags & L2_ARP_UFWD)
320 {
321 feature_bitmap |= L2INPUT_FEAT_ARP_UFWD;
322 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barach97d8dc22016-08-15 15:31:15 -0400324 if (enable)
325 {
326 bd_config->feature_bitmap |= feature_bitmap;
327 }
328 else
329 {
330 bd_config->feature_bitmap &= ~feature_bitmap;
331 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Neale Ranns47a3d992020-09-29 15:38:51 +0000333 b2_input_recache (bd_index);
334
John Lo8d00fff2017-08-03 00:35:36 -0400335 return bd_config->feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336}
337
Dave Barach97d8dc22016-08-15 15:31:15 -0400338/**
Damjan Mariond171d482016-12-05 14:16:38 +0100339 Set the mac age for the bridge domain.
340*/
341void
342bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
343{
344 l2_bridge_domain_t *bd_config;
345 int enable = 0;
346
347 vec_validate (l2input_main.bd_configs, bd_index);
348 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
349 bd_config->mac_age = age;
Neale Ranns47a3d992020-09-29 15:38:51 +0000350 b2_input_recache (bd_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100351
352 /* check if there is at least one bd with mac aging enabled */
353 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Barifead6702017-04-04 04:46:32 +0300354 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
Damjan Mariond171d482016-12-05 14:16:38 +0100355
356 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
357 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
358 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
359}
360
Jerome Tollet50570ec2017-09-14 12:53:56 +0100361/**
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100362 Set learn limit for the bridge domain.
363*/
364void
365bd_set_learn_limit (vlib_main_t *vm, u32 bd_index, u32 learn_limit)
366{
367 l2_bridge_domain_t *bd_config;
368 vec_validate (l2input_main.bd_configs, bd_index);
369 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
370 bd_config->learn_limit = learn_limit;
371}
372
373/**
Jerome Tollet50570ec2017-09-14 12:53:56 +0100374 Set the tag for the bridge domain.
375*/
Jerome Tollet50570ec2017-09-14 12:53:56 +0100376static void
Jerome Tollet48304142017-09-05 12:13:22 +0100377bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
378{
379 u8 *old;
380 l2_bridge_domain_t *bd_config;
381 vec_validate (l2input_main.bd_configs, bd_index);
382 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
383
384 old = bd_config->bd_tag;
385
386 if (bd_tag[0])
387 {
388 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
389 }
390 else
391 {
392 bd_config->bd_tag = NULL;
393 }
394
395 vec_free (old);
396}
397
Damjan Mariond171d482016-12-05 14:16:38 +0100398/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400399 Set bridge-domain learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400400 The CLI format is:
401 set bridge-domain learn <bd_id> [disable]
402*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403static clib_error_t *
404bd_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400405 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406{
Dave Barach97d8dc22016-08-15 15:31:15 -0400407 bd_main_t *bdm = &bd_main;
408 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409 u32 bd_index, bd_id;
410 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400411 uword *p;
412
413 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 {
415 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400416 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417 goto done;
418 }
419
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500420 if (bd_id == 0)
421 return clib_error_return (0,
422 "No operations on the default bridge domain are supported");
423
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
425
426 if (p == 0)
427 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400428
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 bd_index = p[0];
430
431 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400432 if (unformat (input, "disable"))
433 {
434 enable = 0;
435 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436
Dave Barach97d8dc22016-08-15 15:31:15 -0400437 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400438 bd_set_flags (vm, bd_index, L2_LEARN, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Dave Barach97d8dc22016-08-15 15:31:15 -0400440done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 return error;
442}
443
Billy McFall22aa3e92016-09-09 08:46:40 -0400444/*?
445 * Layer 2 learning can be enabled and disabled on each
446 * interface and on each bridge-domain. Use this command to
447 * manage bridge-domains. It is enabled by default.
448 *
449 * @cliexpar
450 * Example of how to enable learning (where 200 is the bridge-domain-id):
451 * @cliexcmd{set bridge-domain learn 200}
452 * Example of how to disable learning (where 200 is the bridge-domain-id):
453 * @cliexcmd{set bridge-domain learn 200 disable}
454?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455VLIB_CLI_COMMAND (bd_learn_cli, static) = {
456 .path = "set bridge-domain learn",
457 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
458 .function = bd_learn,
459};
460
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100461static clib_error_t *
462bd_default_learn_limit (vlib_main_t *vm, unformat_input_t *input,
463 vlib_cli_command_t *cmd)
464{
465 l2learn_main_t *l2m = &l2learn_main;
466 clib_error_t *error = 0;
467 u32 learn_limit;
468
469 if (!unformat (input, "%d", &learn_limit))
470 {
471 error = clib_error_return (
472 0, "expecting per bridge-domain max entry number got`%U'",
473 format_unformat_error, input);
474 goto done;
475 }
476
477 l2m->bd_default_learn_limit = learn_limit;
478
479done:
480 return error;
481}
482
483VLIB_CLI_COMMAND (bd_default_learn_limit_cli, static) = {
484 .path = "set bridge-domain default-learn-limit",
485 .short_help = "set bridge-domain default-learn-limit <maxentries>",
486 .function = bd_default_learn_limit,
487};
488
Dave Barach97d8dc22016-08-15 15:31:15 -0400489/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400490 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400491 The CLI format is:
492 set bridge-domain forward <bd_index> [disable]
493*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400495bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496{
Dave Barach97d8dc22016-08-15 15:31:15 -0400497 bd_main_t *bdm = &bd_main;
498 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 u32 bd_index, bd_id;
500 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400501 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502
Dave Barach97d8dc22016-08-15 15:31:15 -0400503 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504 {
505 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400506 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507 goto done;
508 }
509
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500510 if (bd_id == 0)
511 return clib_error_return (0,
512 "No operations on the default bridge domain are supported");
513
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
515
516 if (p == 0)
517 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400518
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519 bd_index = p[0];
520
521 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400522 if (unformat (input, "disable"))
523 {
524 enable = 0;
525 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
Dave Barach97d8dc22016-08-15 15:31:15 -0400527 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400528 bd_set_flags (vm, bd_index, L2_FWD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach97d8dc22016-08-15 15:31:15 -0400530done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 return error;
532}
533
Billy McFall22aa3e92016-09-09 08:46:40 -0400534
535/*?
536 * Layer 2 unicast forwarding can be enabled and disabled on each
537 * interface and on each bridge-domain. Use this command to
538 * manage bridge-domains. It is enabled by default.
539 *
540 * @cliexpar
541 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
542 * @cliexcmd{set bridge-domain forward 200}
543 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
544 * @cliexcmd{set bridge-domain forward 200 disable}
545?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
547 .path = "set bridge-domain forward",
548 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
549 .function = bd_fwd,
550};
551
Dave Barach97d8dc22016-08-15 15:31:15 -0400552/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400553 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400554 The CLI format is:
555 set bridge-domain flood <bd_index> [disable]
556*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557static clib_error_t *
558bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400559 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560{
Dave Barach97d8dc22016-08-15 15:31:15 -0400561 bd_main_t *bdm = &bd_main;
562 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 u32 bd_index, bd_id;
564 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400565 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566
Dave Barach97d8dc22016-08-15 15:31:15 -0400567 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568 {
569 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400570 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 goto done;
572 }
573
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500574 if (bd_id == 0)
575 return clib_error_return (0,
576 "No operations on the default bridge domain are supported");
577
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
579
580 if (p == 0)
581 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400582
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583 bd_index = p[0];
584
585 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400586 if (unformat (input, "disable"))
587 {
588 enable = 0;
589 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590
Dave Barach97d8dc22016-08-15 15:31:15 -0400591 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400592 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barach97d8dc22016-08-15 15:31:15 -0400594done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 return error;
596}
597
Billy McFall22aa3e92016-09-09 08:46:40 -0400598/*?
599 * Layer 2 flooding can be enabled and disabled on each
600 * interface and on each bridge-domain. Use this command to
601 * manage bridge-domains. It is enabled by default.
602 *
603 * @cliexpar
604 * Example of how to enable flooding (where 200 is the bridge-domain-id):
605 * @cliexcmd{set bridge-domain flood 200}
606 * Example of how to disable flooding (where 200 is the bridge-domain-id):
607 * @cliexcmd{set bridge-domain flood 200 disable}
608?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700609VLIB_CLI_COMMAND (bd_flood_cli, static) = {
610 .path = "set bridge-domain flood",
611 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
612 .function = bd_flood,
613};
614
Dave Barach97d8dc22016-08-15 15:31:15 -0400615/**
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300616 Set bridge-domain unknown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400617 The CLI format is:
618 set bridge-domain uu-flood <bd_index> [disable]
619*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620static clib_error_t *
621bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400622 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623{
Dave Barach97d8dc22016-08-15 15:31:15 -0400624 bd_main_t *bdm = &bd_main;
625 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626 u32 bd_index, bd_id;
627 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400628 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629
Dave Barach97d8dc22016-08-15 15:31:15 -0400630 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700631 {
632 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400633 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700634 goto done;
635 }
636
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500637 if (bd_id == 0)
638 return clib_error_return (0,
639 "No operations on the default bridge domain are supported");
640
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
642
643 if (p == 0)
644 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400645
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646 bd_index = p[0];
647
648 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400649 if (unformat (input, "disable"))
650 {
651 enable = 0;
652 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653
Dave Barach97d8dc22016-08-15 15:31:15 -0400654 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400655 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700656
Dave Barach97d8dc22016-08-15 15:31:15 -0400657done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700658 return error;
659}
660
Billy McFall22aa3e92016-09-09 08:46:40 -0400661/*?
662 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
663 * bridge-domain. It is enabled by default.
664 *
665 * @cliexpar
666 * Example of how to enable unknown-unicast flooding (where 200 is the
667 * bridge-domain-id):
668 * @cliexcmd{set bridge-domain uu-flood 200}
669 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
670 * @cliexcmd{set bridge-domain uu-flood 200 disable}
671?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
673 .path = "set bridge-domain uu-flood",
674 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
675 .function = bd_uu_flood,
676};
677
Dave Barach97d8dc22016-08-15 15:31:15 -0400678/**
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200679 Set bridge-domain arp-unicast forward enable/disable.
680 The CLI format is:
681 set bridge-domain arp-ufwd <bd_index> [disable]
682*/
683static clib_error_t *
684bd_arp_ufwd (vlib_main_t * vm,
685 unformat_input_t * input, vlib_cli_command_t * cmd)
686{
687 bd_main_t *bdm = &bd_main;
688 clib_error_t *error = 0;
689 u32 bd_index, bd_id;
690 u32 enable;
691 uword *p;
692
693 if (!unformat (input, "%d", &bd_id))
694 {
695 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
696 format_unformat_error, input);
697 goto done;
698 }
699
700 if (bd_id == 0)
701 return clib_error_return (0,
702 "No operations on the default bridge domain are supported");
703
704 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
705
706 if (p == 0)
707 return clib_error_return (0, "No such bridge domain %d", bd_id);
708
709 bd_index = p[0];
710
711 enable = 1;
712 if (unformat (input, "disable"))
713 {
714 enable = 0;
715 }
716
717 /* set the bridge domain flag */
718 bd_set_flags (vm, bd_index, L2_ARP_UFWD, enable);
719
720done:
721 return error;
722}
723
724/*?
725 * Layer 2 arp-unicast forwarding can be enabled and disabled on each
726 * bridge-domain. It is disabled by default.
727 *
728 * @cliexpar
729 * Example of how to enable arp-unicast forwarding (where 200 is the
730 * bridge-domain-id):
731 * @cliexcmd{set bridge-domain arp-ufwd 200}
732 * Example of how to disable arp-unicast forwarding (where 200 is the bridge-domain-id):
733 * @cliexcmd{set bridge-domain arp-ufwd 200 disable}
734?*/
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200735VLIB_CLI_COMMAND (bd_arp_ufwd_cli, static) = {
736 .path = "set bridge-domain arp-ufwd",
737 .short_help = "set bridge-domain arp-ufwd <bridge-domain-id> [disable]",
738 .function = bd_arp_ufwd,
739};
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200740
741/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400742 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400743 The CLI format is:
744 set bridge-domain arp term <bridge-domain-id> [disable]
745*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700746static clib_error_t *
747bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400748 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749{
Dave Barach97d8dc22016-08-15 15:31:15 -0400750 bd_main_t *bdm = &bd_main;
751 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700752 u32 bd_index, bd_id;
753 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400754 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755
Dave Barach97d8dc22016-08-15 15:31:15 -0400756 if (!unformat (input, "%d", &bd_id))
757 {
758 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
759 format_unformat_error, input);
760 goto done;
761 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500763 if (bd_id == 0)
764 return clib_error_return (0,
765 "No operations on the default bridge domain are supported");
766
Ed Warnickecb9cada2015-12-08 15:45:58 -0700767 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400768 if (p)
769 bd_index = *p;
770 else
771 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772
Dave Barach97d8dc22016-08-15 15:31:15 -0400773 enable = 1;
774 if (unformat (input, "disable"))
775 enable = 0;
776
777 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400778 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700779
780done:
781 return error;
782}
783
Damjan Mariond171d482016-12-05 14:16:38 +0100784static clib_error_t *
785bd_mac_age (vlib_main_t * vm,
786 unformat_input_t * input, vlib_cli_command_t * cmd)
787{
788 bd_main_t *bdm = &bd_main;
789 clib_error_t *error = 0;
790 u32 bd_index, bd_id;
791 u32 age;
792 uword *p;
793
794 if (!unformat (input, "%d", &bd_id))
795 {
796 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
797 format_unformat_error, input);
798 goto done;
799 }
800
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500801 if (bd_id == 0)
802 return clib_error_return (0,
803 "No operations on the default bridge domain are supported");
804
Damjan Mariond171d482016-12-05 14:16:38 +0100805 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
806
807 if (p == 0)
808 return clib_error_return (0, "No such bridge domain %d", bd_id);
809
810 bd_index = p[0];
811
812 if (!unformat (input, "%u", &age))
813 {
814 error =
815 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
816 format_unformat_error, input);
817 goto done;
818 }
819
820 /* set the bridge domain flag */
821 if (age > 255)
822 {
823 error =
824 clib_error_return (0, "mac aging time cannot be bigger than 255");
825 goto done;
826 }
827 bd_set_mac_age (vm, bd_index, (u8) age);
828
829done:
830 return error;
831}
832
833/*?
834 * Layer 2 mac aging can be enabled and disabled on each
835 * bridge-domain. Use this command to set or disable mac aging
836 * on specific bridge-domains. It is disabled by default.
837 *
838 * @cliexpar
839 * Example of how to set mac aging (where 200 is the bridge-domain-id and
840 * 5 is aging time in minutes):
841 * @cliexcmd{set bridge-domain mac-age 200 5}
842 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
843 * @cliexcmd{set bridge-domain flood 200 0}
844?*/
Damjan Mariond171d482016-12-05 14:16:38 +0100845VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
846 .path = "set bridge-domain mac-age",
847 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
848 .function = bd_mac_age,
849};
Damjan Mariond171d482016-12-05 14:16:38 +0100850
Jerome Tollet5f93e3b2020-12-18 09:44:24 +0100851static clib_error_t *
852bd_learn_limit (vlib_main_t *vm, unformat_input_t *input,
853 vlib_cli_command_t *cmd)
854{
855 bd_main_t *bdm = &bd_main;
856 clib_error_t *error = 0;
857 u32 bd_index, bd_id;
858 u32 learn_limit;
859 uword *p;
860
861 if (!unformat (input, "%d", &bd_id))
862 {
863 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
864 format_unformat_error, input);
865 goto done;
866 }
867
868 if (bd_id == 0)
869 return clib_error_return (
870 0, "No operations on the default bridge domain are supported");
871
872 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
873
874 if (p == 0)
875 return clib_error_return (0, "No such bridge domain %d", bd_id);
876
877 bd_index = p[0];
878
879 if (!unformat (input, "%u", &learn_limit))
880 {
881 error = clib_error_return (
882 0, "expecting maxium number of learned entries but got `%U'",
883 format_unformat_error, input);
884 goto done;
885 }
886
887 bd_set_learn_limit (vm, bd_index, learn_limit);
888
889done:
890 return error;
891}
892
893VLIB_CLI_COMMAND (bd_learn_limit_cli, static) = {
894 .path = "set bridge-domain learn-limit",
895 .short_help =
896 "set bridge-domain learn-limit <bridge-domain-id> <learn-limit>",
897 .function = bd_learn_limit,
898};
899
Billy McFall22aa3e92016-09-09 08:46:40 -0400900/*?
901 * Modify whether or not an existing bridge-domain should terminate and respond
902 * to ARP Requests. ARP Termination is disabled by default.
903 *
904 * @cliexpar
905 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
906 * @cliexcmd{set bridge-domain arp term 200}
907 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
908 * @cliexcmd{set bridge-domain arp term 200 disable}
909?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700910VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
911 .path = "set bridge-domain arp term",
912 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
913 .function = bd_arp_term,
914};
915
916
Dave Barach97d8dc22016-08-15 15:31:15 -0400917/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400918 * Add/delete IP address to MAC address mapping.
919 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400920 * The clib hash implementation stores uword entries in the hash table.
921 * The hash table mac_by_ip4 is keyed via IP4 address and store the
922 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400923 *
924 * @warning This only works for 64-bit processor with 8-byte uword;
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700925 * which means this code *WILL NOT WORK* for a 32-bit processor with
Chris Luke16bcf7d2016-09-01 14:31:46 -0400926 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400927 */
928u32
929bd_add_del_ip_mac (u32 bd_index,
Neale Ranns4d5b9172018-10-24 02:57:49 -0700930 ip46_type_t type,
931 const ip46_address_t * ip,
932 const mac_address_t * mac, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300934 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700935 u64 new_mac = mac_address_as_u64 (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400936 u64 *old_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937
Neale Ranns4d5b9172018-10-24 02:57:49 -0700938 /* make sure uword is 8 bytes */
939 ASSERT (sizeof (uword) == sizeof (u64));
Eyal Bariafc47aa2017-04-20 14:45:17 +0300940 ASSERT (bd_is_valid (bd_cfg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941
Neale Ranns4d5b9172018-10-24 02:57:49 -0700942 if (IP46_TYPE_IP6 == type)
Dave Barach97d8dc22016-08-15 15:31:15 -0400943 {
John Lo1edfba92016-08-27 01:11:57 -0400944 ip6_address_t *ip6_addr_key;
945 hash_pair_t *hp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700946 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400947 if (is_add)
948 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700949 if (old_mac == NULL)
950 {
951 /* new entry - allocate and create ip6 address key */
John Lo1edfba92016-08-27 01:11:57 -0400952 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
Neale Ranns4d5b9172018-10-24 02:57:49 -0700953 clib_memcpy (ip6_addr_key, &ip->ip6, sizeof (ip6_address_t));
John Lo1edfba92016-08-27 01:11:57 -0400954 }
955 else if (*old_mac == new_mac)
Neale Ranns4d5b9172018-10-24 02:57:49 -0700956 {
957 /* same mac entry already exist for ip6 address */
John Lo1edfba92016-08-27 01:11:57 -0400958 return 0;
959 }
960 else
Neale Ranns4d5b9172018-10-24 02:57:49 -0700961 {
962 /* update mac for ip6 address */
963 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400964 ip6_addr_key = (ip6_address_t *) hp->key;
965 }
966 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
967 }
968 else
969 {
970 if (old_mac && (*old_mac == new_mac))
971 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700972 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400973 ip6_addr_key = (ip6_address_t *) hp->key;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700974 hash_unset_mem (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400975 clib_mem_free (ip6_addr_key);
976 }
977 else
978 return 1;
979 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400981 else
982 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700983 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400984 if (is_add)
985 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400986 if (old_mac && (*old_mac == new_mac))
Neale Ranns4d5b9172018-10-24 02:57:49 -0700987 /* mac entry already exist */
988 return 0;
989 hash_set (bd_cfg->mac_by_ip4, ip->ip4.as_u32, new_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400990 }
991 else
992 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400993 if (old_mac && (*old_mac == new_mac))
Neale Ranns4d5b9172018-10-24 02:57:49 -0700994 hash_unset (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400995 else
John Lo1edfba92016-08-27 01:11:57 -0400996 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400997 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400998 }
John Lo1edfba92016-08-27 01:11:57 -0400999 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000}
1001
Dave Barach97d8dc22016-08-15 15:31:15 -04001002/**
John Loe26c81f2019-01-07 15:16:33 -05001003 * Flush IP address to MAC address mapping tables in a BD.
1004 */
1005void
1006bd_flush_ip_mac (u32 bd_index)
1007{
1008 l2_bridge_domain_t *bd = l2input_bd_config (bd_index);
1009 ASSERT (bd_is_valid (bd));
1010 bd_free_ip_mac_tables (bd);
1011 bd->mac_by_ip4 = 0;
1012 bd->mac_by_ip6 =
1013 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1014}
1015
1016/**
Chris Luke16bcf7d2016-09-01 14:31:46 -04001017 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -04001018 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -04001019 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -04001020*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021static clib_error_t *
1022bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -04001023 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024{
Neale Ranns4d5b9172018-10-24 02:57:49 -07001025 ip46_address_t ip_addr = ip46_address_initializer;
1026 ip46_type_t type = IP46_TYPE_IP4;
Dave Barach97d8dc22016-08-15 15:31:15 -04001027 bd_main_t *bdm = &bd_main;
1028 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029 u32 bd_index, bd_id;
Neale Ranns4d5b9172018-10-24 02:57:49 -07001030 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 u8 is_add = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -04001032 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033
Dave Barach97d8dc22016-08-15 15:31:15 -04001034 if (!unformat (input, "%d", &bd_id))
1035 {
1036 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
1037 format_unformat_error, input);
1038 goto done;
1039 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001041 if (bd_id == 0)
1042 return clib_error_return (0,
1043 "No operations on the default bridge domain are supported");
1044
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1046
Dave Barach97d8dc22016-08-15 15:31:15 -04001047 if (p)
1048 bd_index = *p;
1049 else
1050 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
Neale Ranns4d5b9172018-10-24 02:57:49 -07001052 if (unformat (input, "%U", unformat_ip4_address, &ip_addr.ip4))
Dave Barach97d8dc22016-08-15 15:31:15 -04001053 {
Neale Ranns4d5b9172018-10-24 02:57:49 -07001054 type = IP46_TYPE_IP4;
Dave Barach97d8dc22016-08-15 15:31:15 -04001055 }
Neale Ranns4d5b9172018-10-24 02:57:49 -07001056 else if (unformat (input, "%U", unformat_ip6_address, &ip_addr.ip6))
Dave Barach97d8dc22016-08-15 15:31:15 -04001057 {
Neale Ranns4d5b9172018-10-24 02:57:49 -07001058 type = IP46_TYPE_IP6;
Dave Barach97d8dc22016-08-15 15:31:15 -04001059 }
John Loe26c81f2019-01-07 15:16:33 -05001060 else if (unformat (input, "del-all"))
1061 {
1062 bd_flush_ip_mac (bd_index);
1063 goto done;
1064 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001065 else
1066 {
1067 error = clib_error_return (0, "expecting IP address but got `%U'",
1068 format_unformat_error, input);
1069 goto done;
1070 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071
Neale Ranns4d5b9172018-10-24 02:57:49 -07001072 if (!unformat (input, "%U", unformat_mac_address_t, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -04001073 {
1074 error = clib_error_return (0, "expecting MAC address but got `%U'",
1075 format_unformat_error, input);
1076 goto done;
1077 }
1078
1079 if (unformat (input, "del"))
1080 {
1081 is_add = 0;
1082 }
1083
1084 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
Neale Ranns4d5b9172018-10-24 02:57:49 -07001085 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, is_add))
Dave Barach97d8dc22016-08-15 15:31:15 -04001086 {
1087 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
1088 is_add ? "add" : "del",
Neale Ranns4d5b9172018-10-24 02:57:49 -07001089 format_ip46_address, &ip_addr, IP46_TYPE_ANY,
1090 format_mac_address_t, &mac);
Dave Barach97d8dc22016-08-15 15:31:15 -04001091 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092
1093done:
1094 return error;
1095}
1096
Billy McFall22aa3e92016-09-09 08:46:40 -04001097/*?
1098 * Add an ARP entry to an existing bridge-domain.
1099 *
1100 * @cliexpar
1101 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
1102 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
1103 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
1104 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
1105?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
1107 .path = "set bridge-domain arp entry",
John Loe26c81f2019-01-07 15:16:33 -05001108 .short_help = "set bridge-domain arp entry <bridge-domain-id> [<ip-addr> <mac-addr> [del] | del-all]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001109 .function = bd_arp_entry,
1110};
1111
Neale Rannsb4743802018-09-05 09:13:57 -07001112static u8 *
Neale Rannsb4743802018-09-05 09:13:57 -07001113format_uu_cfg (u8 * s, va_list * args)
1114{
1115 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
1116
1117 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
1118 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
1119 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
1120 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
1121 return (format (s, "flood"));
1122 else
1123 return (format (s, "drop"));
1124}
1125
Dave Barach97d8dc22016-08-15 15:31:15 -04001126/**
Chris Luke16bcf7d2016-09-01 14:31:46 -04001127 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -04001128 The CLI format is:
1129 show bridge-domain [<bd_index>]
1130*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -04001132bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001133{
Dave Barach97d8dc22016-08-15 15:31:15 -04001134 vnet_main_t *vnm = vnet_get_main ();
1135 bd_main_t *bdm = &bd_main;
1136 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001137 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001138 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001139 u32 start, end;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 u32 detail = 0;
1141 u32 intf = 0;
1142 u32 arp = 0;
Jerome Tollet48304142017-09-05 12:13:22 +01001143 u32 bd_tag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001145 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001146
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001147 start = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -04001148 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149
Dave Barach97d8dc22016-08-15 15:31:15 -04001150 if (unformat (input, "%d", &bd_id))
1151 {
1152 if (unformat (input, "detail"))
1153 detail = 1;
1154 else if (unformat (input, "det"))
1155 detail = 1;
1156 if (unformat (input, "int"))
1157 intf = 1;
1158 if (unformat (input, "arp"))
1159 arp = 1;
Jerome Tollet48304142017-09-05 12:13:22 +01001160 if (unformat (input, "bd-tag"))
1161 bd_tag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001162
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001163 if (bd_id == 0)
1164 return clib_error_return (0,
1165 "No operations on the default bridge domain are supported");
1166
Dave Barach97d8dc22016-08-15 15:31:15 -04001167 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1168 if (p)
1169 bd_index = *p;
1170 else
1171 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001172
Dave Barach97d8dc22016-08-15 15:31:15 -04001173 vec_validate (l2input_main.bd_configs, bd_index);
1174 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1175 if (bd_is_valid (bd_config))
1176 {
1177 start = bd_index;
1178 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001180 else
1181 {
1182 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1183 goto done;
1184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186
Dave Barach97d8dc22016-08-15 15:31:15 -04001187 /* Show all bridge-domains that have been initialized */
John Loda1f2c72017-03-24 20:11:15 -04001188 u32 printed = 0;
1189 u8 *as = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001190 for (bd_index = start; bd_index < end; bd_index++)
1191 {
1192 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1193 if (bd_is_valid (bd_config))
1194 {
1195 if (!printed)
1196 {
1197 printed = 1;
1198 vlib_cli_output (vm,
Jerome Tollet5f93e3b2020-12-18 09:44:24 +01001199 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s "
1200 "%=9s %=8s %=8s %=11s",
1201 "BD-ID", "Index", "BSN", "Age(min)", "Learning",
1202 "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
1203 "arp-ufwd", "Learn-count", "Learn-limit",
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001204 "BVI-Intf");
Dave Barach97d8dc22016-08-15 15:31:15 -04001205 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001206
John Loda1f2c72017-03-24 20:11:15 -04001207 if (bd_config->mac_age)
1208 as = format (as, "%d", bd_config->mac_age);
1209 else
1210 as = format (as, "off");
Jerome Tollet5f93e3b2020-12-18 09:44:24 +01001211 vlib_cli_output (
1212 vm,
1213 "%=8d %=7d %=4d %=9v %=9s %=9s %=11U %=9s %=9s %=9s %=8d %=8d "
1214 "%=11U",
1215 bd_config->bd_id, bd_index, bd_config->seq_num, as,
1216 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ? "on" : "off",
1217 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ? "on" : "off",
1218 format_uu_cfg, bd_config,
1219 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ? "on" : "off",
1220 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ? "on" : "off",
1221 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_UFWD ? "on" : "off",
1222 bd_config->learn_count, bd_config->learn_limit,
1223 format_vnet_sw_if_index_name_with_NA, vnm,
1224 bd_config->bvi_sw_if_index);
1225 if (detail)
1226 vlib_cli_output (vm, "%U", format_l2_input_feature_bitmap,
1227 bd_config->feature_bitmap);
John Loda1f2c72017-03-24 20:11:15 -04001228 vec_reset_length (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001229
1230 if (detail || intf)
1231 {
1232 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +02001233 int i;
1234 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -04001235 {
Eyal Baric5b13602016-11-24 19:42:43 +02001236 l2_flood_member_t *member =
1237 vec_elt_at_index (bd_config->members, i);
Neale Ranns47a3d992020-09-29 15:38:51 +00001238 u8 swif_seq_num = l2_input_seq_num (member->sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -04001239 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +02001240 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -04001241 {
John Loda1f2c72017-03-24 20:11:15 -04001242 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1243 "Interface", "If-idx", "ISN", "SHG",
1244 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -04001245 }
1246 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1247 &tag1, &tag2);
John Loda1f2c72017-03-24 20:11:15 -04001248 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001249 format_vnet_sw_if_index_name, vnm,
1250 member->sw_if_index, member->sw_if_index,
Eyal Bari0f360dc2017-06-14 13:11:20 +03001251 swif_seq_num, member->shg,
Dave Barach97d8dc22016-08-15 15:31:15 -04001252 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +02001253 "-", i < bd_config->flood_count ? "*" : "-",
1254 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001255 }
Neale Rannsb4743802018-09-05 09:13:57 -07001256 if (~0 != bd_config->uu_fwd_sw_if_index)
1257 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1258 format_vnet_sw_if_index_name, vnm,
1259 bd_config->uu_fwd_sw_if_index,
1260 bd_config->uu_fwd_sw_if_index,
1261 0, 0, "uu", "-", "None");
1262
Dave Barach97d8dc22016-08-15 15:31:15 -04001263 }
1264
1265 if ((detail || arp) &&
1266 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1267 {
1268 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001269 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001270 u64 mac_addr;
1271 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001272 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001273
Dave Barach97d8dc22016-08-15 15:31:15 -04001274 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1275 ({
John Lo1edfba92016-08-27 01:11:57 -04001276 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001277 format_ip4_address, &ip4_addr,
1278 format_ethernet_address, &mac_addr);
1279 }));
John Lo1edfba92016-08-27 01:11:57 -04001280
1281 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1282 ({
1283 vlib_cli_output (vm, "%=40U => %=20U",
1284 format_ip6_address, ip6_addr,
1285 format_ethernet_address, &mac_addr);
1286 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001287 }
Jerome Tollet48304142017-09-05 12:13:22 +01001288
1289 if ((detail || bd_tag) && (bd_config->bd_tag))
1290 {
1291 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1292
1293 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001294 }
1295 }
John Loda1f2c72017-03-24 20:11:15 -04001296 vec_free (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001297
1298 if (!printed)
1299 {
1300 vlib_cli_output (vm, "no bridge-domains in use");
1301 }
1302
1303done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001304 return error;
1305}
1306
Billy McFall22aa3e92016-09-09 08:46:40 -04001307/*?
1308 * Show a summary of all the bridge-domain instances or detailed view of a
1309 * single bridge-domain. Bridge-domains are created by adding an interface
1310 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1311 *
1312 * @cliexpar
1313 * @parblock
1314 * Example of displaying all bridge-domains:
1315 * @cliexstart{show bridge-domain}
1316 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1317 * 0 0 off off off off off local0
1318 * 200 1 on on on on off N/A
1319 * @cliexend
1320 *
1321 * Example of displaying details of a single bridge-domains:
1322 * @cliexstart{show bridge-domain 200 detail}
1323 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1324 * 200 1 on on on on off N/A
1325 *
1326 * Interface Index SHG BVI VLAN-Tag-Rewrite
1327 * GigabitEthernet0/8/0.200 3 0 - none
1328 * GigabitEthernet0/9/0.200 4 0 - none
1329 * @cliexend
1330 * @endparblock
1331?*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07001332VLIB_CLI_COMMAND (bd_show_cli, static) = {
1333 .path = "show bridge-domain",
Jerome Tollet48304142017-09-05 12:13:22 +01001334 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001335 .function = bd_show,
1336};
Dave Barach97d8dc22016-08-15 15:31:15 -04001337
Choonho Son05480792017-03-29 20:07:45 +09001338int
1339bd_add_del (l2_bridge_domain_add_del_args_t * a)
1340{
1341 bd_main_t *bdm = &bd_main;
Damjan Marion95147812020-09-14 12:18:44 +02001342 l2fib_main_t *fm = &l2fib_main;
Choonho Son05480792017-03-29 20:07:45 +09001343 vlib_main_t *vm = bdm->vlib_main;
Choonho Son05480792017-03-29 20:07:45 +09001344 int rv = 0;
1345
Damjan Marion95147812020-09-14 12:18:44 +02001346 if (fm->mac_table_initialized == 0)
1347 l2fib_table_init ();
1348
Eyal Barib1352ed2017-04-07 23:14:17 +03001349 u32 bd_index = bd_find_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001350 if (a->is_add)
1351 {
Eyal Barib1352ed2017-04-07 23:14:17 +03001352 if (bd_index != ~0)
1353 return VNET_API_ERROR_BD_ALREADY_EXISTS;
John Lo97934772017-05-18 22:26:47 -04001354 if (a->bd_id > L2_BD_ID_MAX)
1355 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
Eyal Barib1352ed2017-04-07 23:14:17 +03001356 bd_index = bd_add_bd_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001357
Neale Rannsb4743802018-09-05 09:13:57 -07001358 bd_flags_t enable_flags = 0, disable_flags = 0;
Choonho Son05480792017-03-29 20:07:45 +09001359 if (a->flood)
1360 enable_flags |= L2_FLOOD;
1361 else
1362 disable_flags |= L2_FLOOD;
1363
1364 if (a->uu_flood)
1365 enable_flags |= L2_UU_FLOOD;
1366 else
1367 disable_flags |= L2_UU_FLOOD;
1368
1369 if (a->forward)
1370 enable_flags |= L2_FWD;
1371 else
1372 disable_flags |= L2_FWD;
1373
1374 if (a->learn)
1375 enable_flags |= L2_LEARN;
1376 else
1377 disable_flags |= L2_LEARN;
1378
1379 if (a->arp_term)
1380 enable_flags |= L2_ARP_TERM;
1381 else
1382 disable_flags |= L2_ARP_TERM;
1383
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001384 if (a->arp_ufwd)
1385 enable_flags |= L2_ARP_UFWD;
1386 else
1387 disable_flags |= L2_ARP_UFWD;
1388
Choonho Son05480792017-03-29 20:07:45 +09001389 if (enable_flags)
1390 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1391
1392 if (disable_flags)
1393 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1394
1395 bd_set_mac_age (vm, bd_index, a->mac_age);
Jerome Tollet48304142017-09-05 12:13:22 +01001396
1397 if (a->bd_tag)
1398 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1399
Jerome Tollet5f93e3b2020-12-18 09:44:24 +01001400 bd_set_learn_limit (vm, bd_index, l2learn_main.bd_default_learn_limit);
1401 vec_elt_at_index (l2input_main.bd_configs, bd_index)->learn_count = 0;
Choonho Son05480792017-03-29 20:07:45 +09001402 }
1403 else
Eyal Barib1352ed2017-04-07 23:14:17 +03001404 {
1405 if (bd_index == ~0)
1406 return VNET_API_ERROR_NO_SUCH_ENTRY;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001407 if (bd_index == 0)
1408 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
Eyal Barib1352ed2017-04-07 23:14:17 +03001409 if (vec_len (l2input_main.bd_configs[bd_index].members))
1410 return VNET_API_ERROR_BD_IN_USE;
1411 rv = bd_delete (bdm, bd_index);
1412 }
Choonho Son05480792017-03-29 20:07:45 +09001413
1414 return rv;
1415}
1416
1417/**
1418 Create or delete bridge-domain.
Choonho Son5ee51f82017-04-05 19:09:52 +09001419 The CLI format:
Jerome Tollet50570ec2017-09-14 12:53:56 +01001420 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1421 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
Choonho Son05480792017-03-29 20:07:45 +09001422*/
1423
1424static clib_error_t *
1425bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1426 vlib_cli_command_t * cmd)
1427{
1428 unformat_input_t _line_input, *line_input = &_line_input;
1429 clib_error_t *error = 0;
1430 u8 is_add = 1;
1431 u32 bd_id = ~0;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001432 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term =
1433 0, arp_ufwd = 0;
Choonho Son05480792017-03-29 20:07:45 +09001434 u32 mac_age = 0;
Jerome Tollet48304142017-09-05 12:13:22 +01001435 u8 *bd_tag = NULL;
Choonho Son05480792017-03-29 20:07:45 +09001436 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1437 int rv;
1438
1439 /* Get a line of input. */
1440 if (!unformat_user (input, unformat_line_input, line_input))
1441 return 0;
1442
1443 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1444 {
1445 if (unformat (line_input, "%d", &bd_id))
1446 ;
1447 else if (unformat (line_input, "flood %d", &flood))
1448 ;
1449 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1450 ;
1451 else if (unformat (line_input, "forward %d", &forward))
1452 ;
Choonho Son5ee51f82017-04-05 19:09:52 +09001453 else if (unformat (line_input, "learn %d", &learn))
1454 ;
Choonho Son05480792017-03-29 20:07:45 +09001455 else if (unformat (line_input, "arp-term %d", &arp_term))
1456 ;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001457 else if (unformat (line_input, "arp-ufwd %d", &arp_ufwd))
1458 ;
Choonho Son05480792017-03-29 20:07:45 +09001459 else if (unformat (line_input, "mac-age %d", &mac_age))
1460 ;
Jerome Tollet48304142017-09-05 12:13:22 +01001461 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1462 ;
Choonho Son05480792017-03-29 20:07:45 +09001463 else if (unformat (line_input, "del"))
1464 {
1465 is_add = 0;
1466 flood = uu_flood = forward = learn = 0;
1467 }
1468 else
1469 break;
1470 }
1471
1472 if (bd_id == ~0)
1473 {
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001474 if (is_add)
1475 {
1476 bd_id = bd_get_unused_id ();
1477 }
1478 else
1479 {
1480 error = clib_error_return (0, "bridge-domain-id not specified");
1481 goto done;
1482 }
Choonho Son05480792017-03-29 20:07:45 +09001483 }
1484
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001485 if (bd_id == 0)
1486 {
1487 error = clib_error_return (0, "bridge domain 0 can not be modified");
1488 goto done;
1489 }
1490
Choonho Son05480792017-03-29 20:07:45 +09001491 if (mac_age > 255)
1492 {
1493 error = clib_error_return (0, "mac age must be less than 256");
1494 goto done;
1495 }
Jerome Tollet48304142017-09-05 12:13:22 +01001496 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1497 {
1498 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1499 goto done;
1500 }
Choonho Son05480792017-03-29 20:07:45 +09001501
Dave Barachb7b92992018-10-17 10:38:51 -04001502 clib_memset (a, 0, sizeof (*a));
Choonho Son05480792017-03-29 20:07:45 +09001503 a->is_add = is_add;
1504 a->bd_id = bd_id;
1505 a->flood = (u8) flood;
1506 a->uu_flood = (u8) uu_flood;
1507 a->forward = (u8) forward;
1508 a->learn = (u8) learn;
1509 a->arp_term = (u8) arp_term;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001510 a->arp_ufwd = (u8) arp_ufwd;
Choonho Son05480792017-03-29 20:07:45 +09001511 a->mac_age = (u8) mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +01001512 a->bd_tag = bd_tag;
Choonho Son05480792017-03-29 20:07:45 +09001513
1514 rv = bd_add_del (a);
1515
1516 switch (rv)
1517 {
1518 case 0:
1519 if (is_add)
1520 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1521 break;
Eyal Barib1352ed2017-04-07 23:14:17 +03001522 case VNET_API_ERROR_BD_IN_USE:
1523 error = clib_error_return (0, "bridge domain in use - remove members");
1524 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001525 case VNET_API_ERROR_NO_SUCH_ENTRY:
John Lo97934772017-05-18 22:26:47 -04001526 error = clib_error_return (0, "bridge domain ID does not exist");
Choonho Son05480792017-03-29 20:07:45 +09001527 goto done;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001528 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1529 error = clib_error_return (0, "bridge domain 0 can not be modified");
1530 goto done;
John Lo97934772017-05-18 22:26:47 -04001531 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1532 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1533 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001534 default:
1535 error = clib_error_return (0, "bd_add_del returned %d", rv);
1536 goto done;
1537 }
1538
1539done:
Jerome Tollet48304142017-09-05 12:13:22 +01001540 vec_free (bd_tag);
Choonho Son05480792017-03-29 20:07:45 +09001541 unformat_free (line_input);
1542
1543 return error;
1544}
1545
1546
1547/*?
1548 * Create/Delete bridge-domain instance
1549 *
1550 * @cliexpar
1551 * @parblock
1552 * Example of creating bridge-domain 1:
1553 * @cliexstart{create bridge-domain 1}
1554 * bridge-domain 1
1555 * @cliexend
1556 *
1557 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1558 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1559 * bridge-domain 2
1560 *
1561 * vpp# show bridge-domain
1562 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1563 * 0 0 0 off off off off off off local0
1564 * 1 1 0 off on on off on off N/A
1565 * 2 2 0 60 on on off on on N/A
1566 *
1567 * @cliexend
1568 *
1569 * Example of delete bridge-domain 1:
1570 * @cliexstart{create bridge-domain 1 del}
1571 * @cliexend
1572 * @endparblock
1573?*/
1574
Choonho Son05480792017-03-29 20:07:45 +09001575VLIB_CLI_COMMAND (bd_create_cli, static) = {
1576 .path = "create bridge-domain",
1577 .short_help = "create bridge-domain <bridge-domain-id>"
1578 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001579 " [arp-ufwd <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]",
Choonho Son05480792017-03-29 20:07:45 +09001580 .function = bd_add_del_command_fn,
1581};
Choonho Son05480792017-03-29 20:07:45 +09001582
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001583/*
1584 * Returns an unused bridge domain id, and ~0 if it can't find one.
1585 */
1586u32
Steven Luong662c59a2022-10-12 17:08:12 -07001587bd_get_unused_id (void)
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001588{
1589 bd_main_t *bdm = &bd_main;
1590 int i, j;
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001591 static u32 seed = 0;
Steven Luong662c59a2022-10-12 17:08:12 -07001592
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001593 /* limit to 1M tries */
1594 for (j = 0; j < 1 << 10; j++)
1595 {
Steven Luong662c59a2022-10-12 17:08:12 -07001596 seed = random_u32 (&seed);
1597 for (i = 0; i < 1 << 10; i++)
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001598 {
Steven Luong662c59a2022-10-12 17:08:12 -07001599 /*
1600 * iterate seed+0, seed+1, seed-1, seed+2, seed-2, ... to generate id
1601 */
1602 seed += (2 * (i % 2) - 1) * i;
1603 /* bd_id must be (1 <= bd_id <= L2_BD_ID_MAX) */
1604 seed &= L2_BD_ID_MAX;
1605 if (seed == 0)
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001606 continue;
1607 if (bd_find_index (bdm, seed) == ~0)
1608 return seed;
1609 }
1610 }
Choonho Son05480792017-03-29 20:07:45 +09001611
Laszlo Kiraly0f8f4352022-09-16 13:20:07 +02001612 return ~0;
1613}
Choonho Son05480792017-03-29 20:07:45 +09001614
Dave Barach97d8dc22016-08-15 15:31:15 -04001615/*
1616 * fd.io coding-style-patch-verification: ON
1617 *
1618 * Local Variables:
1619 * eval: (c-set-style "gnu")
1620 * End:
1621 */