blob: a3acc4b6d7e49838cdae15bfababd87b1254a453 [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;
91
92 return rv;
93}
94
John Loe26c81f2019-01-07 15:16:33 -050095static inline void
96bd_free_ip_mac_tables (l2_bridge_domain_t * bd)
97{
98 u64 mac_addr;
99 ip6_address_t *ip6_addr_key;
100
101 hash_free (bd->mac_by_ip4);
102 /* *INDENT-OFF* */
103 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
104 ({
105 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
106 }));
107 /* *INDENT-ON* */
108 hash_free (bd->mac_by_ip6);
109}
110
Eyal Barib1352ed2017-04-07 23:14:17 +0300111static int
112bd_delete (bd_main_t * bdm, u32 bd_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113{
John Lod77630a2017-04-28 00:33:36 -0400114 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
115 u32 bd_id = bd->bd_id;
Eyal Bariafc47aa2017-04-20 14:45:17 +0300116
John Lod48c8eb2017-05-05 12:35:25 -0400117 /* flush non-static MACs in BD and removed bd_id from hash table */
118 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
Eyal Barib1352ed2017-04-07 23:14:17 +0300119 hash_unset (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400120
121 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123
John Lod77630a2017-04-28 00:33:36 -0400124 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
125 bd->bd_id = ~0;
126 bd->feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127
Jerome Tollet48304142017-09-05 12:13:22 +0100128 /* free BD tag */
129 vec_free (bd->bd_tag);
130
John Lod48c8eb2017-05-05 12:35:25 -0400131 /* free memory used by BD */
John Lod77630a2017-04-28 00:33:36 -0400132 vec_free (bd->members);
John Loe26c81f2019-01-07 15:16:33 -0500133 bd_free_ip_mac_tables (bd);
John Loda1f2c72017-03-24 20:11:15 -0400134
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 return 0;
136}
137
Eyal Baric5b13602016-11-24 19:42:43 +0200138static void
139update_flood_count (l2_bridge_domain_t * bd_config)
140{
Neale Ranns87dad112018-04-09 01:53:01 -0700141 bd_config->flood_count = (vec_len (bd_config->members) -
142 (bd_config->tun_master_count ?
143 bd_config->tun_normal_count : 0));
144 bd_config->flood_count -= bd_config->no_flood_count;
Eyal Baric5b13602016-11-24 19:42:43 +0200145}
146
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147void
Dave Barach97d8dc22016-08-15 15:31:15 -0400148bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Neale Ranns87dad112018-04-09 01:53:01 -0700150 u32 ix = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200151 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
152 (vnet_get_main (), member->sw_if_index);
153
Dave Barach97d8dc22016-08-15 15:31:15 -0400154 /*
155 * Add one element to the vector
Neale Ranns87dad112018-04-09 01:53:01 -0700156 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
Dave Barach97d8dc22016-08-15 15:31:15 -0400157 * When flooding, the bvi interface (if present) must be the last member
158 * processed due to how BVI processing can change the packet. To enable
159 * this order, we make the bvi interface the first in the vector and
Neale Ranns87dad112018-04-09 01:53:01 -0700160 * flooding walks the vector in reverse. The flood-count determines where
161 * in the member list to start the walk from.
Dave Barach97d8dc22016-08-15 15:31:15 -0400162 */
Eyal Baric5b13602016-11-24 19:42:43 +0200163 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400164 {
Neale Ranns87dad112018-04-09 01:53:01 -0700165 case VNET_FLOOD_CLASS_NO_FLOOD:
166 bd_config->no_flood_count++;
167 ix = vec_len (bd_config->members);
168 break;
169 case VNET_FLOOD_CLASS_BVI:
170 ix = 0;
171 break;
Eyal Baric5b13602016-11-24 19:42:43 +0200172 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
173 bd_config->tun_master_count++;
174 /* Fall through */
Eyal Baric5b13602016-11-24 19:42:43 +0200175 case VNET_FLOOD_CLASS_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700176 ix = (vec_len (bd_config->members) -
177 bd_config->tun_normal_count - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200178 break;
179 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700180 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200181 bd_config->tun_normal_count++;
182 break;
183 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184
Eyal Baric5b13602016-11-24 19:42:43 +0200185 vec_insert_elts (bd_config->members, member, 1, ix);
186 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187}
188
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189#define BD_REMOVE_ERROR_OK 0
190#define BD_REMOVE_ERROR_NOT_FOUND 1
191
192u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400193bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194{
195 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400196
197 /* Find and delete the member */
198 vec_foreach_index (ix, bd_config->members)
199 {
Eyal Baric5b13602016-11-24 19:42:43 +0200200 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
201 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400202 {
Eyal Baric5b13602016-11-24 19:42:43 +0200203 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
204 (vnet_get_main (), sw_if_index);
205
206 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
207 {
208 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
209 bd_config->tun_master_count--;
210 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
211 bd_config->tun_normal_count--;
Eyal Barib6b04f02018-07-11 14:01:36 +0300212 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
213 bd_config->no_flood_count--;
Eyal Baric5b13602016-11-24 19:42:43 +0200214 }
Eyal Bari25b36672017-03-02 10:43:19 +0200215 vec_delete (bd_config->members, 1, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200216 update_flood_count (bd_config);
217
Dave Barach97d8dc22016-08-15 15:31:15 -0400218 return BD_REMOVE_ERROR_OK;
219 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220 }
221
222 return BD_REMOVE_ERROR_NOT_FOUND;
223}
224
225
Dave Barach97d8dc22016-08-15 15:31:15 -0400226clib_error_t *
227l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228{
229 bd_main_t *bdm = &bd_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400230 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
231 /*
Dave Barach11fb09e2020-08-06 12:10:09 -0400232 * create a placeholder bd with bd_id of 0 and bd_index of 0 with feature set
Dave Barach97d8dc22016-08-15 15:31:15 -0400233 * to packet drop only. Thus, packets received from any L2 interface with
234 * uninitialized bd_index of 0 can be dropped safely.
235 */
Eyal Barib1352ed2017-04-07 23:14:17 +0300236 u32 bd_index = bd_add_bd_index (bdm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400238 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Choonho Son05480792017-03-29 20:07:45 +0900239
240 bdm->vlib_main = vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 return 0;
242}
243
244VLIB_INIT_FUNCTION (l2bd_init);
245
Neale Ranns47a3d992020-09-29 15:38:51 +0000246l2_bridge_domain_t *
247bd_get (u32 bd_index)
248{
249 if (bd_index < vec_len (l2input_main.bd_configs))
250 return (vec_elt_at_index (l2input_main.bd_configs, bd_index));
251 return (NULL);
252}
253
254u32
255bd_input_walk (u32 bd_index, bd_input_walk_fn_t fn, void *data)
256{
257 l2_flood_member_t *member;
258 l2_bridge_domain_t *bd;
259 u32 sw_if_index;
260
261 sw_if_index = ~0;
262 bd = bd_get (bd_index);
263
264 ASSERT (bd);
265
266 vec_foreach (member, bd->members)
267 {
268 if (WALK_STOP == fn (bd_index, member->sw_if_index))
269 {
270 sw_if_index = member->sw_if_index;
271 break;
272 }
273 }
274
275 return (sw_if_index);
276}
277
278static void
279b2_input_recache (u32 bd_index)
280{
281 bd_input_walk (bd_index, l2input_recache, NULL);
282}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Dave Barach97d8dc22016-08-15 15:31:15 -0400284/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400285 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400286 Return 0 if ok, non-zero if for an error.
287*/
288u32
Neale Rannsb4743802018-09-05 09:13:57 -0700289bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
Dave Barach97d8dc22016-08-15 15:31:15 -0400290{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291
Eyal Bariafc47aa2017-04-20 14:45:17 +0300292 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293 bd_validate (bd_config);
Eyal Bariafc47aa2017-04-20 14:45:17 +0300294 u32 feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295
Dave Barach97d8dc22016-08-15 15:31:15 -0400296 if (flags & L2_LEARN)
297 {
298 feature_bitmap |= L2INPUT_FEAT_LEARN;
299 }
300 if (flags & L2_FWD)
301 {
302 feature_bitmap |= L2INPUT_FEAT_FWD;
303 }
304 if (flags & L2_FLOOD)
305 {
306 feature_bitmap |= L2INPUT_FEAT_FLOOD;
307 }
308 if (flags & L2_UU_FLOOD)
309 {
310 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
311 }
312 if (flags & L2_ARP_TERM)
313 {
314 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
315 }
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200316 if (flags & L2_ARP_UFWD)
317 {
318 feature_bitmap |= L2INPUT_FEAT_ARP_UFWD;
319 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Dave Barach97d8dc22016-08-15 15:31:15 -0400321 if (enable)
322 {
323 bd_config->feature_bitmap |= feature_bitmap;
324 }
325 else
326 {
327 bd_config->feature_bitmap &= ~feature_bitmap;
328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Neale Ranns47a3d992020-09-29 15:38:51 +0000330 b2_input_recache (bd_index);
331
John Lo8d00fff2017-08-03 00:35:36 -0400332 return bd_config->feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333}
334
Dave Barach97d8dc22016-08-15 15:31:15 -0400335/**
Damjan Mariond171d482016-12-05 14:16:38 +0100336 Set the mac age for the bridge domain.
337*/
338void
339bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
340{
341 l2_bridge_domain_t *bd_config;
342 int enable = 0;
343
344 vec_validate (l2input_main.bd_configs, bd_index);
345 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
346 bd_config->mac_age = age;
Neale Ranns47a3d992020-09-29 15:38:51 +0000347 b2_input_recache (bd_index);
Damjan Mariond171d482016-12-05 14:16:38 +0100348
349 /* check if there is at least one bd with mac aging enabled */
350 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Barifead6702017-04-04 04:46:32 +0300351 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
Damjan Mariond171d482016-12-05 14:16:38 +0100352
353 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
354 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
355 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
356}
357
Jerome Tollet50570ec2017-09-14 12:53:56 +0100358/**
359 Set the tag for the bridge domain.
360*/
Jerome Tollet48304142017-09-05 12:13:22 +0100361
Jerome Tollet50570ec2017-09-14 12:53:56 +0100362static void
Jerome Tollet48304142017-09-05 12:13:22 +0100363bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
364{
365 u8 *old;
366 l2_bridge_domain_t *bd_config;
367 vec_validate (l2input_main.bd_configs, bd_index);
368 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
369
370 old = bd_config->bd_tag;
371
372 if (bd_tag[0])
373 {
374 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
375 }
376 else
377 {
378 bd_config->bd_tag = NULL;
379 }
380
381 vec_free (old);
382}
383
Damjan Mariond171d482016-12-05 14:16:38 +0100384/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400385 Set bridge-domain learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400386 The CLI format is:
387 set bridge-domain learn <bd_id> [disable]
388*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389static clib_error_t *
390bd_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392{
Dave Barach97d8dc22016-08-15 15:31:15 -0400393 bd_main_t *bdm = &bd_main;
394 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395 u32 bd_index, bd_id;
396 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400397 uword *p;
398
399 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 {
401 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400402 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403 goto done;
404 }
405
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500406 if (bd_id == 0)
407 return clib_error_return (0,
408 "No operations on the default bridge domain are supported");
409
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
411
412 if (p == 0)
413 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400414
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 bd_index = p[0];
416
417 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400418 if (unformat (input, "disable"))
419 {
420 enable = 0;
421 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barach97d8dc22016-08-15 15:31:15 -0400423 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400424 bd_set_flags (vm, bd_index, L2_LEARN, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
Dave Barach97d8dc22016-08-15 15:31:15 -0400426done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 return error;
428}
429
Billy McFall22aa3e92016-09-09 08:46:40 -0400430/*?
431 * Layer 2 learning can be enabled and disabled on each
432 * interface and on each bridge-domain. Use this command to
433 * manage bridge-domains. It is enabled by default.
434 *
435 * @cliexpar
436 * Example of how to enable learning (where 200 is the bridge-domain-id):
437 * @cliexcmd{set bridge-domain learn 200}
438 * Example of how to disable learning (where 200 is the bridge-domain-id):
439 * @cliexcmd{set bridge-domain learn 200 disable}
440?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400441/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442VLIB_CLI_COMMAND (bd_learn_cli, static) = {
443 .path = "set bridge-domain learn",
444 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
445 .function = bd_learn,
446};
Dave Barach97d8dc22016-08-15 15:31:15 -0400447/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448
Dave Barach97d8dc22016-08-15 15:31:15 -0400449/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400450 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400451 The CLI format is:
452 set bridge-domain forward <bd_index> [disable]
453*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400455bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456{
Dave Barach97d8dc22016-08-15 15:31:15 -0400457 bd_main_t *bdm = &bd_main;
458 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 u32 bd_index, bd_id;
460 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400461 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Dave Barach97d8dc22016-08-15 15:31:15 -0400463 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 {
465 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400466 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 goto done;
468 }
469
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500470 if (bd_id == 0)
471 return clib_error_return (0,
472 "No operations on the default bridge domain are supported");
473
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
475
476 if (p == 0)
477 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400478
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 bd_index = p[0];
480
481 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400482 if (unformat (input, "disable"))
483 {
484 enable = 0;
485 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486
Dave Barach97d8dc22016-08-15 15:31:15 -0400487 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400488 bd_set_flags (vm, bd_index, L2_FWD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
Dave Barach97d8dc22016-08-15 15:31:15 -0400490done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700491 return error;
492}
493
Billy McFall22aa3e92016-09-09 08:46:40 -0400494
495/*?
496 * Layer 2 unicast forwarding can be enabled and disabled on each
497 * interface and on each bridge-domain. Use this command to
498 * manage bridge-domains. It is enabled by default.
499 *
500 * @cliexpar
501 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
502 * @cliexcmd{set bridge-domain forward 200}
503 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
504 * @cliexcmd{set bridge-domain forward 200 disable}
505?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400506/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
508 .path = "set bridge-domain forward",
509 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
510 .function = bd_fwd,
511};
Dave Barach97d8dc22016-08-15 15:31:15 -0400512/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700513
Dave Barach97d8dc22016-08-15 15:31:15 -0400514/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400515 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400516 The CLI format is:
517 set bridge-domain flood <bd_index> [disable]
518*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700519static clib_error_t *
520bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400521 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522{
Dave Barach97d8dc22016-08-15 15:31:15 -0400523 bd_main_t *bdm = &bd_main;
524 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 u32 bd_index, bd_id;
526 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400527 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Dave Barach97d8dc22016-08-15 15:31:15 -0400529 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 {
531 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400532 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 goto done;
534 }
535
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500536 if (bd_id == 0)
537 return clib_error_return (0,
538 "No operations on the default bridge domain are supported");
539
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
541
542 if (p == 0)
543 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400544
Ed Warnickecb9cada2015-12-08 15:45:58 -0700545 bd_index = p[0];
546
547 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400548 if (unformat (input, "disable"))
549 {
550 enable = 0;
551 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
Dave Barach97d8dc22016-08-15 15:31:15 -0400553 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400554 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555
Dave Barach97d8dc22016-08-15 15:31:15 -0400556done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 return error;
558}
559
Billy McFall22aa3e92016-09-09 08:46:40 -0400560/*?
561 * Layer 2 flooding can be enabled and disabled on each
562 * interface and on each bridge-domain. Use this command to
563 * manage bridge-domains. It is enabled by default.
564 *
565 * @cliexpar
566 * Example of how to enable flooding (where 200 is the bridge-domain-id):
567 * @cliexcmd{set bridge-domain flood 200}
568 * Example of how to disable flooding (where 200 is the bridge-domain-id):
569 * @cliexcmd{set bridge-domain flood 200 disable}
570?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400571/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572VLIB_CLI_COMMAND (bd_flood_cli, static) = {
573 .path = "set bridge-domain flood",
574 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
575 .function = bd_flood,
576};
Dave Barach97d8dc22016-08-15 15:31:15 -0400577/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578
Dave Barach97d8dc22016-08-15 15:31:15 -0400579/**
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300580 Set bridge-domain unknown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400581 The CLI format is:
582 set bridge-domain uu-flood <bd_index> [disable]
583*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584static clib_error_t *
585bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400586 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587{
Dave Barach97d8dc22016-08-15 15:31:15 -0400588 bd_main_t *bdm = &bd_main;
589 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 u32 bd_index, bd_id;
591 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400592 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barach97d8dc22016-08-15 15:31:15 -0400594 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 {
596 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400597 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 goto done;
599 }
600
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500601 if (bd_id == 0)
602 return clib_error_return (0,
603 "No operations on the default bridge domain are supported");
604
Ed Warnickecb9cada2015-12-08 15:45:58 -0700605 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
606
607 if (p == 0)
608 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400609
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 bd_index = p[0];
611
612 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400613 if (unformat (input, "disable"))
614 {
615 enable = 0;
616 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700617
Dave Barach97d8dc22016-08-15 15:31:15 -0400618 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400619 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620
Dave Barach97d8dc22016-08-15 15:31:15 -0400621done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700622 return error;
623}
624
Billy McFall22aa3e92016-09-09 08:46:40 -0400625/*?
626 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
627 * bridge-domain. It is enabled by default.
628 *
629 * @cliexpar
630 * Example of how to enable unknown-unicast flooding (where 200 is the
631 * bridge-domain-id):
632 * @cliexcmd{set bridge-domain uu-flood 200}
633 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
634 * @cliexcmd{set bridge-domain uu-flood 200 disable}
635?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400636/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700637VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
638 .path = "set bridge-domain uu-flood",
639 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
640 .function = bd_uu_flood,
641};
Dave Barach97d8dc22016-08-15 15:31:15 -0400642/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Dave Barach97d8dc22016-08-15 15:31:15 -0400644/**
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +0200645 Set bridge-domain arp-unicast forward enable/disable.
646 The CLI format is:
647 set bridge-domain arp-ufwd <bd_index> [disable]
648*/
649static clib_error_t *
650bd_arp_ufwd (vlib_main_t * vm,
651 unformat_input_t * input, vlib_cli_command_t * cmd)
652{
653 bd_main_t *bdm = &bd_main;
654 clib_error_t *error = 0;
655 u32 bd_index, bd_id;
656 u32 enable;
657 uword *p;
658
659 if (!unformat (input, "%d", &bd_id))
660 {
661 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
662 format_unformat_error, input);
663 goto done;
664 }
665
666 if (bd_id == 0)
667 return clib_error_return (0,
668 "No operations on the default bridge domain are supported");
669
670 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
671
672 if (p == 0)
673 return clib_error_return (0, "No such bridge domain %d", bd_id);
674
675 bd_index = p[0];
676
677 enable = 1;
678 if (unformat (input, "disable"))
679 {
680 enable = 0;
681 }
682
683 /* set the bridge domain flag */
684 bd_set_flags (vm, bd_index, L2_ARP_UFWD, enable);
685
686done:
687 return error;
688}
689
690/*?
691 * Layer 2 arp-unicast forwarding can be enabled and disabled on each
692 * bridge-domain. It is disabled by default.
693 *
694 * @cliexpar
695 * Example of how to enable arp-unicast forwarding (where 200 is the
696 * bridge-domain-id):
697 * @cliexcmd{set bridge-domain arp-ufwd 200}
698 * Example of how to disable arp-unicast forwarding (where 200 is the bridge-domain-id):
699 * @cliexcmd{set bridge-domain arp-ufwd 200 disable}
700?*/
701/* *INDENT-OFF* */
702VLIB_CLI_COMMAND (bd_arp_ufwd_cli, static) = {
703 .path = "set bridge-domain arp-ufwd",
704 .short_help = "set bridge-domain arp-ufwd <bridge-domain-id> [disable]",
705 .function = bd_arp_ufwd,
706};
707/* *INDENT-ON* */
708
709/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400710 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400711 The CLI format is:
712 set bridge-domain arp term <bridge-domain-id> [disable]
713*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714static clib_error_t *
715bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400716 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717{
Dave Barach97d8dc22016-08-15 15:31:15 -0400718 bd_main_t *bdm = &bd_main;
719 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 u32 bd_index, bd_id;
721 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400722 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
Dave Barach97d8dc22016-08-15 15:31:15 -0400724 if (!unformat (input, "%d", &bd_id))
725 {
726 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
727 format_unformat_error, input);
728 goto done;
729 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500731 if (bd_id == 0)
732 return clib_error_return (0,
733 "No operations on the default bridge domain are supported");
734
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400736 if (p)
737 bd_index = *p;
738 else
739 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700740
Dave Barach97d8dc22016-08-15 15:31:15 -0400741 enable = 1;
742 if (unformat (input, "disable"))
743 enable = 0;
744
745 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400746 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700747
748done:
749 return error;
750}
751
Damjan Mariond171d482016-12-05 14:16:38 +0100752static clib_error_t *
753bd_mac_age (vlib_main_t * vm,
754 unformat_input_t * input, vlib_cli_command_t * cmd)
755{
756 bd_main_t *bdm = &bd_main;
757 clib_error_t *error = 0;
758 u32 bd_index, bd_id;
759 u32 age;
760 uword *p;
761
762 if (!unformat (input, "%d", &bd_id))
763 {
764 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
765 format_unformat_error, input);
766 goto done;
767 }
768
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500769 if (bd_id == 0)
770 return clib_error_return (0,
771 "No operations on the default bridge domain are supported");
772
Damjan Mariond171d482016-12-05 14:16:38 +0100773 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
774
775 if (p == 0)
776 return clib_error_return (0, "No such bridge domain %d", bd_id);
777
778 bd_index = p[0];
779
780 if (!unformat (input, "%u", &age))
781 {
782 error =
783 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
784 format_unformat_error, input);
785 goto done;
786 }
787
788 /* set the bridge domain flag */
789 if (age > 255)
790 {
791 error =
792 clib_error_return (0, "mac aging time cannot be bigger than 255");
793 goto done;
794 }
795 bd_set_mac_age (vm, bd_index, (u8) age);
796
797done:
798 return error;
799}
800
801/*?
802 * Layer 2 mac aging can be enabled and disabled on each
803 * bridge-domain. Use this command to set or disable mac aging
804 * on specific bridge-domains. It is disabled by default.
805 *
806 * @cliexpar
807 * Example of how to set mac aging (where 200 is the bridge-domain-id and
808 * 5 is aging time in minutes):
809 * @cliexcmd{set bridge-domain mac-age 200 5}
810 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
811 * @cliexcmd{set bridge-domain flood 200 0}
812?*/
813/* *INDENT-OFF* */
814VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
815 .path = "set bridge-domain mac-age",
816 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
817 .function = bd_mac_age,
818};
819/* *INDENT-ON* */
820
Billy McFall22aa3e92016-09-09 08:46:40 -0400821/*?
822 * Modify whether or not an existing bridge-domain should terminate and respond
823 * to ARP Requests. ARP Termination is disabled by default.
824 *
825 * @cliexpar
826 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
827 * @cliexcmd{set bridge-domain arp term 200}
828 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
829 * @cliexcmd{set bridge-domain arp term 200 disable}
830?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400831/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
833 .path = "set bridge-domain arp term",
834 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
835 .function = bd_arp_term,
836};
Dave Barach97d8dc22016-08-15 15:31:15 -0400837/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
839
Dave Barach97d8dc22016-08-15 15:31:15 -0400840/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400841 * Add/delete IP address to MAC address mapping.
842 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400843 * The clib hash implementation stores uword entries in the hash table.
844 * The hash table mac_by_ip4 is keyed via IP4 address and store the
845 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400846 *
847 * @warning This only works for 64-bit processor with 8-byte uword;
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700848 * which means this code *WILL NOT WORK* for a 32-bit processor with
Chris Luke16bcf7d2016-09-01 14:31:46 -0400849 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400850 */
851u32
852bd_add_del_ip_mac (u32 bd_index,
Neale Ranns4d5b9172018-10-24 02:57:49 -0700853 ip46_type_t type,
854 const ip46_address_t * ip,
855 const mac_address_t * mac, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300857 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
Neale Ranns4d5b9172018-10-24 02:57:49 -0700858 u64 new_mac = mac_address_as_u64 (mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400859 u64 *old_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
Neale Ranns4d5b9172018-10-24 02:57:49 -0700861 /* make sure uword is 8 bytes */
862 ASSERT (sizeof (uword) == sizeof (u64));
Eyal Bariafc47aa2017-04-20 14:45:17 +0300863 ASSERT (bd_is_valid (bd_cfg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700864
Neale Ranns4d5b9172018-10-24 02:57:49 -0700865 if (IP46_TYPE_IP6 == type)
Dave Barach97d8dc22016-08-15 15:31:15 -0400866 {
John Lo1edfba92016-08-27 01:11:57 -0400867 ip6_address_t *ip6_addr_key;
868 hash_pair_t *hp;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700869 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400870 if (is_add)
871 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700872 if (old_mac == NULL)
873 {
874 /* new entry - allocate and create ip6 address key */
John Lo1edfba92016-08-27 01:11:57 -0400875 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
Neale Ranns4d5b9172018-10-24 02:57:49 -0700876 clib_memcpy (ip6_addr_key, &ip->ip6, sizeof (ip6_address_t));
John Lo1edfba92016-08-27 01:11:57 -0400877 }
878 else if (*old_mac == new_mac)
Neale Ranns4d5b9172018-10-24 02:57:49 -0700879 {
880 /* same mac entry already exist for ip6 address */
John Lo1edfba92016-08-27 01:11:57 -0400881 return 0;
882 }
883 else
Neale Ranns4d5b9172018-10-24 02:57:49 -0700884 {
885 /* update mac for ip6 address */
886 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400887 ip6_addr_key = (ip6_address_t *) hp->key;
888 }
889 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
890 }
891 else
892 {
893 if (old_mac && (*old_mac == new_mac))
894 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700895 hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400896 ip6_addr_key = (ip6_address_t *) hp->key;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700897 hash_unset_mem (bd_cfg->mac_by_ip6, &ip->ip6);
John Lo1edfba92016-08-27 01:11:57 -0400898 clib_mem_free (ip6_addr_key);
899 }
900 else
901 return 1;
902 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400904 else
905 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700906 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400907 if (is_add)
908 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400909 if (old_mac && (*old_mac == new_mac))
Neale Ranns4d5b9172018-10-24 02:57:49 -0700910 /* mac entry already exist */
911 return 0;
912 hash_set (bd_cfg->mac_by_ip4, ip->ip4.as_u32, new_mac);
Dave Barach97d8dc22016-08-15 15:31:15 -0400913 }
914 else
915 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400916 if (old_mac && (*old_mac == new_mac))
Neale Ranns4d5b9172018-10-24 02:57:49 -0700917 hash_unset (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400918 else
John Lo1edfba92016-08-27 01:11:57 -0400919 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400920 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400921 }
John Lo1edfba92016-08-27 01:11:57 -0400922 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923}
924
Dave Barach97d8dc22016-08-15 15:31:15 -0400925/**
John Loe26c81f2019-01-07 15:16:33 -0500926 * Flush IP address to MAC address mapping tables in a BD.
927 */
928void
929bd_flush_ip_mac (u32 bd_index)
930{
931 l2_bridge_domain_t *bd = l2input_bd_config (bd_index);
932 ASSERT (bd_is_valid (bd));
933 bd_free_ip_mac_tables (bd);
934 bd->mac_by_ip4 = 0;
935 bd->mac_by_ip6 =
936 hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
937}
938
939/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400940 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -0400941 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -0400942 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -0400943*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944static clib_error_t *
945bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400946 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947{
Neale Ranns4d5b9172018-10-24 02:57:49 -0700948 ip46_address_t ip_addr = ip46_address_initializer;
949 ip46_type_t type = IP46_TYPE_IP4;
Dave Barach97d8dc22016-08-15 15:31:15 -0400950 bd_main_t *bdm = &bd_main;
951 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952 u32 bd_index, bd_id;
Neale Ranns4d5b9172018-10-24 02:57:49 -0700953 mac_address_t mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954 u8 is_add = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400955 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700956
Dave Barach97d8dc22016-08-15 15:31:15 -0400957 if (!unformat (input, "%d", &bd_id))
958 {
959 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
960 format_unformat_error, input);
961 goto done;
962 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700963
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500964 if (bd_id == 0)
965 return clib_error_return (0,
966 "No operations on the default bridge domain are supported");
967
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
969
Dave Barach97d8dc22016-08-15 15:31:15 -0400970 if (p)
971 bd_index = *p;
972 else
973 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Neale Ranns4d5b9172018-10-24 02:57:49 -0700975 if (unformat (input, "%U", unformat_ip4_address, &ip_addr.ip4))
Dave Barach97d8dc22016-08-15 15:31:15 -0400976 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700977 type = IP46_TYPE_IP4;
Dave Barach97d8dc22016-08-15 15:31:15 -0400978 }
Neale Ranns4d5b9172018-10-24 02:57:49 -0700979 else if (unformat (input, "%U", unformat_ip6_address, &ip_addr.ip6))
Dave Barach97d8dc22016-08-15 15:31:15 -0400980 {
Neale Ranns4d5b9172018-10-24 02:57:49 -0700981 type = IP46_TYPE_IP6;
Dave Barach97d8dc22016-08-15 15:31:15 -0400982 }
John Loe26c81f2019-01-07 15:16:33 -0500983 else if (unformat (input, "del-all"))
984 {
985 bd_flush_ip_mac (bd_index);
986 goto done;
987 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400988 else
989 {
990 error = clib_error_return (0, "expecting IP address but got `%U'",
991 format_unformat_error, input);
992 goto done;
993 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994
Neale Ranns4d5b9172018-10-24 02:57:49 -0700995 if (!unformat (input, "%U", unformat_mac_address_t, &mac))
Dave Barach97d8dc22016-08-15 15:31:15 -0400996 {
997 error = clib_error_return (0, "expecting MAC address but got `%U'",
998 format_unformat_error, input);
999 goto done;
1000 }
1001
1002 if (unformat (input, "del"))
1003 {
1004 is_add = 0;
1005 }
1006
1007 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
Neale Ranns4d5b9172018-10-24 02:57:49 -07001008 if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, is_add))
Dave Barach97d8dc22016-08-15 15:31:15 -04001009 {
1010 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
1011 is_add ? "add" : "del",
Neale Ranns4d5b9172018-10-24 02:57:49 -07001012 format_ip46_address, &ip_addr, IP46_TYPE_ANY,
1013 format_mac_address_t, &mac);
Dave Barach97d8dc22016-08-15 15:31:15 -04001014 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015
1016done:
1017 return error;
1018}
1019
Billy McFall22aa3e92016-09-09 08:46:40 -04001020/*?
1021 * Add an ARP entry to an existing bridge-domain.
1022 *
1023 * @cliexpar
1024 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
1025 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
1026 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
1027 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
1028?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001029/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
1031 .path = "set bridge-domain arp entry",
John Loe26c81f2019-01-07 15:16:33 -05001032 .short_help = "set bridge-domain arp entry <bridge-domain-id> [<ip-addr> <mac-addr> [del] | del-all]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 .function = bd_arp_entry,
1034};
Dave Barach97d8dc22016-08-15 15:31:15 -04001035/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
Neale Rannsb4743802018-09-05 09:13:57 -07001037static u8 *
Neale Rannsb4743802018-09-05 09:13:57 -07001038format_uu_cfg (u8 * s, va_list * args)
1039{
1040 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
1041
1042 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
1043 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
1044 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
1045 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
1046 return (format (s, "flood"));
1047 else
1048 return (format (s, "drop"));
1049}
1050
Dave Barach97d8dc22016-08-15 15:31:15 -04001051/**
Chris Luke16bcf7d2016-09-01 14:31:46 -04001052 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -04001053 The CLI format is:
1054 show bridge-domain [<bd_index>]
1055*/
Ed Warnickecb9cada2015-12-08 15:45:58 -07001056static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -04001057bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058{
Dave Barach97d8dc22016-08-15 15:31:15 -04001059 vnet_main_t *vnm = vnet_get_main ();
1060 bd_main_t *bdm = &bd_main;
1061 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001062 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001063 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001064 u32 start, end;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065 u32 detail = 0;
1066 u32 intf = 0;
1067 u32 arp = 0;
Jerome Tollet48304142017-09-05 12:13:22 +01001068 u32 bd_tag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001069 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001070 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001071
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001072 start = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -04001073 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001074
Dave Barach97d8dc22016-08-15 15:31:15 -04001075 if (unformat (input, "%d", &bd_id))
1076 {
1077 if (unformat (input, "detail"))
1078 detail = 1;
1079 else if (unformat (input, "det"))
1080 detail = 1;
1081 if (unformat (input, "int"))
1082 intf = 1;
1083 if (unformat (input, "arp"))
1084 arp = 1;
Jerome Tollet48304142017-09-05 12:13:22 +01001085 if (unformat (input, "bd-tag"))
1086 bd_tag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001088 if (bd_id == 0)
1089 return clib_error_return (0,
1090 "No operations on the default bridge domain are supported");
1091
Dave Barach97d8dc22016-08-15 15:31:15 -04001092 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1093 if (p)
1094 bd_index = *p;
1095 else
1096 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097
Dave Barach97d8dc22016-08-15 15:31:15 -04001098 vec_validate (l2input_main.bd_configs, bd_index);
1099 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1100 if (bd_is_valid (bd_config))
1101 {
1102 start = bd_index;
1103 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001105 else
1106 {
1107 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1108 goto done;
1109 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001110 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001111
Dave Barach97d8dc22016-08-15 15:31:15 -04001112 /* Show all bridge-domains that have been initialized */
John Loda1f2c72017-03-24 20:11:15 -04001113 u32 printed = 0;
1114 u8 *as = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001115 for (bd_index = start; bd_index < end; bd_index++)
1116 {
1117 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1118 if (bd_is_valid (bd_config))
1119 {
1120 if (!printed)
1121 {
1122 printed = 1;
1123 vlib_cli_output (vm,
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001124 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s %=9s %=11s",
John Lo97934772017-05-18 22:26:47 -04001125 "BD-ID", "Index", "BSN", "Age(min)",
Neale Rannsb4743802018-09-05 09:13:57 -07001126 "Learning", "U-Forwrd", "UU-Flood",
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001127 "Flooding", "ARP-Term", "arp-ufwd",
1128 "BVI-Intf");
Dave Barach97d8dc22016-08-15 15:31:15 -04001129 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001130
John Loda1f2c72017-03-24 20:11:15 -04001131 if (bd_config->mac_age)
1132 as = format (as, "%d", bd_config->mac_age);
1133 else
1134 as = format (as, "off");
Dave Barach97d8dc22016-08-15 15:31:15 -04001135 vlib_cli_output (vm,
Neale Ranns47a3d992020-09-29 15:38:51 +00001136 "%=8d %=7d %=9v %=9s %=9s %=11U %=9s %=9s %=9s %=11U",
1137 bd_config->bd_id, bd_index, as,
Dave Barach97d8dc22016-08-15 15:31:15 -04001138 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1139 "on" : "off",
John Loda1f2c72017-03-24 20:11:15 -04001140 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1141 "on" : "off",
Neale Rannsb4743802018-09-05 09:13:57 -07001142 format_uu_cfg, bd_config,
Dave Barach97d8dc22016-08-15 15:31:15 -04001143 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1144 "on" : "off",
1145 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
John Loda1f2c72017-03-24 20:11:15 -04001146 "on" : "off",
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001147 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_UFWD ?
1148 "on" : "off",
John Loda1f2c72017-03-24 20:11:15 -04001149 format_vnet_sw_if_index_name_with_NA,
Dave Barach97d8dc22016-08-15 15:31:15 -04001150 vnm, bd_config->bvi_sw_if_index);
Neale Ranns47a3d992020-09-29 15:38:51 +00001151 vlib_cli_output (vm, "%U", format_l2_input_feature_bitmap,
1152 bd_config->feature_bitmap);
John Loda1f2c72017-03-24 20:11:15 -04001153 vec_reset_length (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001154
1155 if (detail || intf)
1156 {
1157 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +02001158 int i;
1159 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -04001160 {
Eyal Baric5b13602016-11-24 19:42:43 +02001161 l2_flood_member_t *member =
1162 vec_elt_at_index (bd_config->members, i);
Neale Ranns47a3d992020-09-29 15:38:51 +00001163 u8 swif_seq_num = l2_input_seq_num (member->sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -04001164 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +02001165 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -04001166 {
John Loda1f2c72017-03-24 20:11:15 -04001167 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1168 "Interface", "If-idx", "ISN", "SHG",
1169 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -04001170 }
1171 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1172 &tag1, &tag2);
John Loda1f2c72017-03-24 20:11:15 -04001173 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001174 format_vnet_sw_if_index_name, vnm,
1175 member->sw_if_index, member->sw_if_index,
Eyal Bari0f360dc2017-06-14 13:11:20 +03001176 swif_seq_num, member->shg,
Dave Barach97d8dc22016-08-15 15:31:15 -04001177 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +02001178 "-", i < bd_config->flood_count ? "*" : "-",
1179 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001180 }
Neale Rannsb4743802018-09-05 09:13:57 -07001181 if (~0 != bd_config->uu_fwd_sw_if_index)
1182 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1183 format_vnet_sw_if_index_name, vnm,
1184 bd_config->uu_fwd_sw_if_index,
1185 bd_config->uu_fwd_sw_if_index,
1186 0, 0, "uu", "-", "None");
1187
Dave Barach97d8dc22016-08-15 15:31:15 -04001188 }
1189
1190 if ((detail || arp) &&
1191 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1192 {
1193 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001194 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001195 u64 mac_addr;
1196 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001197 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001198
1199 /* *INDENT-OFF* */
1200 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1201 ({
John Lo1edfba92016-08-27 01:11:57 -04001202 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001203 format_ip4_address, &ip4_addr,
1204 format_ethernet_address, &mac_addr);
1205 }));
John Lo1edfba92016-08-27 01:11:57 -04001206
1207 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1208 ({
1209 vlib_cli_output (vm, "%=40U => %=20U",
1210 format_ip6_address, ip6_addr,
1211 format_ethernet_address, &mac_addr);
1212 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001213 /* *INDENT-ON* */
1214 }
Jerome Tollet48304142017-09-05 12:13:22 +01001215
1216 if ((detail || bd_tag) && (bd_config->bd_tag))
1217 {
1218 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1219
1220 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001221 }
1222 }
John Loda1f2c72017-03-24 20:11:15 -04001223 vec_free (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001224
1225 if (!printed)
1226 {
1227 vlib_cli_output (vm, "no bridge-domains in use");
1228 }
1229
1230done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001231 return error;
1232}
1233
Billy McFall22aa3e92016-09-09 08:46:40 -04001234/*?
1235 * Show a summary of all the bridge-domain instances or detailed view of a
1236 * single bridge-domain. Bridge-domains are created by adding an interface
1237 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1238 *
1239 * @cliexpar
1240 * @parblock
1241 * Example of displaying all bridge-domains:
1242 * @cliexstart{show bridge-domain}
1243 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1244 * 0 0 off off off off off local0
1245 * 200 1 on on on on off N/A
1246 * @cliexend
1247 *
1248 * Example of displaying details of a single bridge-domains:
1249 * @cliexstart{show bridge-domain 200 detail}
1250 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1251 * 200 1 on on on on off N/A
1252 *
1253 * Interface Index SHG BVI VLAN-Tag-Rewrite
1254 * GigabitEthernet0/8/0.200 3 0 - none
1255 * GigabitEthernet0/9/0.200 4 0 - none
1256 * @cliexend
1257 * @endparblock
1258?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001259/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001260VLIB_CLI_COMMAND (bd_show_cli, static) = {
1261 .path = "show bridge-domain",
Jerome Tollet48304142017-09-05 12:13:22 +01001262 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263 .function = bd_show,
1264};
Dave Barach97d8dc22016-08-15 15:31:15 -04001265/* *INDENT-ON* */
1266
Choonho Son05480792017-03-29 20:07:45 +09001267int
1268bd_add_del (l2_bridge_domain_add_del_args_t * a)
1269{
1270 bd_main_t *bdm = &bd_main;
Damjan Marion95147812020-09-14 12:18:44 +02001271 l2fib_main_t *fm = &l2fib_main;
Choonho Son05480792017-03-29 20:07:45 +09001272 vlib_main_t *vm = bdm->vlib_main;
Choonho Son05480792017-03-29 20:07:45 +09001273 int rv = 0;
1274
Damjan Marion95147812020-09-14 12:18:44 +02001275 if (fm->mac_table_initialized == 0)
1276 l2fib_table_init ();
1277
Eyal Barib1352ed2017-04-07 23:14:17 +03001278 u32 bd_index = bd_find_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001279 if (a->is_add)
1280 {
Eyal Barib1352ed2017-04-07 23:14:17 +03001281 if (bd_index != ~0)
1282 return VNET_API_ERROR_BD_ALREADY_EXISTS;
John Lo97934772017-05-18 22:26:47 -04001283 if (a->bd_id > L2_BD_ID_MAX)
1284 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
Eyal Barib1352ed2017-04-07 23:14:17 +03001285 bd_index = bd_add_bd_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001286
Neale Rannsb4743802018-09-05 09:13:57 -07001287 bd_flags_t enable_flags = 0, disable_flags = 0;
Choonho Son05480792017-03-29 20:07:45 +09001288 if (a->flood)
1289 enable_flags |= L2_FLOOD;
1290 else
1291 disable_flags |= L2_FLOOD;
1292
1293 if (a->uu_flood)
1294 enable_flags |= L2_UU_FLOOD;
1295 else
1296 disable_flags |= L2_UU_FLOOD;
1297
1298 if (a->forward)
1299 enable_flags |= L2_FWD;
1300 else
1301 disable_flags |= L2_FWD;
1302
1303 if (a->learn)
1304 enable_flags |= L2_LEARN;
1305 else
1306 disable_flags |= L2_LEARN;
1307
1308 if (a->arp_term)
1309 enable_flags |= L2_ARP_TERM;
1310 else
1311 disable_flags |= L2_ARP_TERM;
1312
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001313 if (a->arp_ufwd)
1314 enable_flags |= L2_ARP_UFWD;
1315 else
1316 disable_flags |= L2_ARP_UFWD;
1317
Choonho Son05480792017-03-29 20:07:45 +09001318 if (enable_flags)
1319 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1320
1321 if (disable_flags)
1322 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1323
1324 bd_set_mac_age (vm, bd_index, a->mac_age);
Jerome Tollet48304142017-09-05 12:13:22 +01001325
1326 if (a->bd_tag)
1327 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1328
Choonho Son05480792017-03-29 20:07:45 +09001329 }
1330 else
Eyal Barib1352ed2017-04-07 23:14:17 +03001331 {
1332 if (bd_index == ~0)
1333 return VNET_API_ERROR_NO_SUCH_ENTRY;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001334 if (bd_index == 0)
1335 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
Eyal Barib1352ed2017-04-07 23:14:17 +03001336 if (vec_len (l2input_main.bd_configs[bd_index].members))
1337 return VNET_API_ERROR_BD_IN_USE;
1338 rv = bd_delete (bdm, bd_index);
1339 }
Choonho Son05480792017-03-29 20:07:45 +09001340
1341 return rv;
1342}
1343
1344/**
1345 Create or delete bridge-domain.
Choonho Son5ee51f82017-04-05 19:09:52 +09001346 The CLI format:
Jerome Tollet50570ec2017-09-14 12:53:56 +01001347 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1348 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
Choonho Son05480792017-03-29 20:07:45 +09001349*/
1350
1351static clib_error_t *
1352bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1353 vlib_cli_command_t * cmd)
1354{
1355 unformat_input_t _line_input, *line_input = &_line_input;
1356 clib_error_t *error = 0;
1357 u8 is_add = 1;
1358 u32 bd_id = ~0;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001359 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term =
1360 0, arp_ufwd = 0;
Choonho Son05480792017-03-29 20:07:45 +09001361 u32 mac_age = 0;
Jerome Tollet48304142017-09-05 12:13:22 +01001362 u8 *bd_tag = NULL;
Choonho Son05480792017-03-29 20:07:45 +09001363 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1364 int rv;
1365
1366 /* Get a line of input. */
1367 if (!unformat_user (input, unformat_line_input, line_input))
1368 return 0;
1369
1370 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1371 {
1372 if (unformat (line_input, "%d", &bd_id))
1373 ;
1374 else if (unformat (line_input, "flood %d", &flood))
1375 ;
1376 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1377 ;
1378 else if (unformat (line_input, "forward %d", &forward))
1379 ;
Choonho Son5ee51f82017-04-05 19:09:52 +09001380 else if (unformat (line_input, "learn %d", &learn))
1381 ;
Choonho Son05480792017-03-29 20:07:45 +09001382 else if (unformat (line_input, "arp-term %d", &arp_term))
1383 ;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001384 else if (unformat (line_input, "arp-ufwd %d", &arp_ufwd))
1385 ;
Choonho Son05480792017-03-29 20:07:45 +09001386 else if (unformat (line_input, "mac-age %d", &mac_age))
1387 ;
Jerome Tollet48304142017-09-05 12:13:22 +01001388 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1389 ;
Choonho Son05480792017-03-29 20:07:45 +09001390 else if (unformat (line_input, "del"))
1391 {
1392 is_add = 0;
1393 flood = uu_flood = forward = learn = 0;
1394 }
1395 else
1396 break;
1397 }
1398
1399 if (bd_id == ~0)
1400 {
1401 error = clib_error_return (0, "bridge-domain-id not specified");
1402 goto done;
1403 }
1404
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001405 if (bd_id == 0)
1406 {
1407 error = clib_error_return (0, "bridge domain 0 can not be modified");
1408 goto done;
1409 }
1410
Choonho Son05480792017-03-29 20:07:45 +09001411 if (mac_age > 255)
1412 {
1413 error = clib_error_return (0, "mac age must be less than 256");
1414 goto done;
1415 }
Jerome Tollet48304142017-09-05 12:13:22 +01001416 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1417 {
1418 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1419 goto done;
1420 }
Choonho Son05480792017-03-29 20:07:45 +09001421
Dave Barachb7b92992018-10-17 10:38:51 -04001422 clib_memset (a, 0, sizeof (*a));
Choonho Son05480792017-03-29 20:07:45 +09001423 a->is_add = is_add;
1424 a->bd_id = bd_id;
1425 a->flood = (u8) flood;
1426 a->uu_flood = (u8) uu_flood;
1427 a->forward = (u8) forward;
1428 a->learn = (u8) learn;
1429 a->arp_term = (u8) arp_term;
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001430 a->arp_ufwd = (u8) arp_ufwd;
Choonho Son05480792017-03-29 20:07:45 +09001431 a->mac_age = (u8) mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +01001432 a->bd_tag = bd_tag;
Choonho Son05480792017-03-29 20:07:45 +09001433
1434 rv = bd_add_del (a);
1435
1436 switch (rv)
1437 {
1438 case 0:
1439 if (is_add)
1440 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1441 break;
Eyal Barib1352ed2017-04-07 23:14:17 +03001442 case VNET_API_ERROR_BD_IN_USE:
1443 error = clib_error_return (0, "bridge domain in use - remove members");
1444 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001445 case VNET_API_ERROR_NO_SUCH_ENTRY:
John Lo97934772017-05-18 22:26:47 -04001446 error = clib_error_return (0, "bridge domain ID does not exist");
Choonho Son05480792017-03-29 20:07:45 +09001447 goto done;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001448 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1449 error = clib_error_return (0, "bridge domain 0 can not be modified");
1450 goto done;
John Lo97934772017-05-18 22:26:47 -04001451 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1452 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1453 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001454 default:
1455 error = clib_error_return (0, "bd_add_del returned %d", rv);
1456 goto done;
1457 }
1458
1459done:
Jerome Tollet48304142017-09-05 12:13:22 +01001460 vec_free (bd_tag);
Choonho Son05480792017-03-29 20:07:45 +09001461 unformat_free (line_input);
1462
1463 return error;
1464}
1465
1466
1467/*?
1468 * Create/Delete bridge-domain instance
1469 *
1470 * @cliexpar
1471 * @parblock
1472 * Example of creating bridge-domain 1:
1473 * @cliexstart{create bridge-domain 1}
1474 * bridge-domain 1
1475 * @cliexend
1476 *
1477 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1478 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1479 * bridge-domain 2
1480 *
1481 * vpp# show bridge-domain
1482 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1483 * 0 0 0 off off off off off off local0
1484 * 1 1 0 off on on off on off N/A
1485 * 2 2 0 60 on on off on on N/A
1486 *
1487 * @cliexend
1488 *
1489 * Example of delete bridge-domain 1:
1490 * @cliexstart{create bridge-domain 1 del}
1491 * @cliexend
1492 * @endparblock
1493?*/
1494
1495/* *INDENT-OFF* */
1496VLIB_CLI_COMMAND (bd_create_cli, static) = {
1497 .path = "create bridge-domain",
1498 .short_help = "create bridge-domain <bridge-domain-id>"
1499 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
Mohsin Kazmi5e6f7342019-04-05 17:40:20 +02001500 " [arp-ufwd <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]",
Choonho Son05480792017-03-29 20:07:45 +09001501 .function = bd_add_del_command_fn,
1502};
1503/* *INDENT-ON* */
1504
1505
1506
Dave Barach97d8dc22016-08-15 15:31:15 -04001507/*
1508 * fd.io coding-style-patch-verification: ON
1509 *
1510 * Local Variables:
1511 * eval: (c-set-style "gnu")
1512 * End:
1513 */