blob: 47bdce6af68703a9ff1890705c5ccd62fd8b966f [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;
Neale Rannsb4743802018-09-05 09:13:57 -070055 bd_config->feature_bitmap = ~(L2INPUT_FEAT_ARP_TERM | L2INPUT_FEAT_UU_FWD);
Eyal Barib1352ed2017-04-07 23:14:17 +030056 bd_config->bvi_sw_if_index = ~0;
Neale Rannsb4743802018-09-05 09:13:57 -070057 bd_config->uu_fwd_sw_if_index = ~0;
Eyal Barib1352ed2017-04-07 23:14:17 +030058 bd_config->members = 0;
59 bd_config->flood_count = 0;
60 bd_config->tun_master_count = 0;
61 bd_config->tun_normal_count = 0;
Eyal Barib6b04f02018-07-11 14:01:36 +030062 bd_config->no_flood_count = 0;
Eyal Barib1352ed2017-04-07 23:14:17 +030063 bd_config->mac_by_ip4 = 0;
64 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
65 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -070066}
67
Dave Barach97d8dc22016-08-15 15:31:15 -040068u32
Eyal Barib1352ed2017-04-07 23:14:17 +030069bd_find_index (bd_main_t * bdm, u32 bd_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -070070{
Eyal Barib1352ed2017-04-07 23:14:17 +030071 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
72 if (!p)
73 return ~0;
74 return p[0];
75}
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Eyal Barib1352ed2017-04-07 23:14:17 +030077u32
78bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
79{
80 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
81 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
Dave Barach97d8dc22016-08-15 15:31:15 -040082
Eyal Barib1352ed2017-04-07 23:14:17 +030083 /* mark this index taken */
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
85
86 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
87
88 vec_validate (l2input_main.bd_configs, rv);
89 l2input_main.bd_configs[rv].bd_id = bd_id;
90
91 return rv;
92}
93
Eyal Barib1352ed2017-04-07 23:14:17 +030094static int
95bd_delete (bd_main_t * bdm, u32 bd_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
John Lod77630a2017-04-28 00:33:36 -040097 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
98 u32 bd_id = bd->bd_id;
John Lod48c8eb2017-05-05 12:35:25 -040099 u64 mac_addr;
100 ip6_address_t *ip6_addr_key;
Eyal Bariafc47aa2017-04-20 14:45:17 +0300101
John Lod48c8eb2017-05-05 12:35:25 -0400102 /* flush non-static MACs in BD and removed bd_id from hash table */
103 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
Eyal Barib1352ed2017-04-07 23:14:17 +0300104 hash_unset (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400105
106 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
John Lod77630a2017-04-28 00:33:36 -0400109 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
110 bd->bd_id = ~0;
111 bd->feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112
Jerome Tollet48304142017-09-05 12:13:22 +0100113 /* free BD tag */
114 vec_free (bd->bd_tag);
115
John Lod48c8eb2017-05-05 12:35:25 -0400116 /* free memory used by BD */
John Lod77630a2017-04-28 00:33:36 -0400117 vec_free (bd->members);
118 hash_free (bd->mac_by_ip4);
John Lod48c8eb2017-05-05 12:35:25 -0400119 /* *INDENT-OFF* */
120 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
121 ({
122 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
123 }));
124 /* *INDENT-ON* */
John Lod77630a2017-04-28 00:33:36 -0400125 hash_free (bd->mac_by_ip6);
John Loda1f2c72017-03-24 20:11:15 -0400126
Ed Warnickecb9cada2015-12-08 15:45:58 -0700127 return 0;
128}
129
Eyal Baric5b13602016-11-24 19:42:43 +0200130static void
131update_flood_count (l2_bridge_domain_t * bd_config)
132{
Neale Ranns87dad112018-04-09 01:53:01 -0700133 bd_config->flood_count = (vec_len (bd_config->members) -
134 (bd_config->tun_master_count ?
135 bd_config->tun_normal_count : 0));
136 bd_config->flood_count -= bd_config->no_flood_count;
Eyal Baric5b13602016-11-24 19:42:43 +0200137}
138
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139void
Dave Barach97d8dc22016-08-15 15:31:15 -0400140bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141{
Neale Ranns87dad112018-04-09 01:53:01 -0700142 u32 ix = 0;
Eyal Baric5b13602016-11-24 19:42:43 +0200143 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
144 (vnet_get_main (), member->sw_if_index);
145
Dave Barach97d8dc22016-08-15 15:31:15 -0400146 /*
147 * Add one element to the vector
Neale Ranns87dad112018-04-09 01:53:01 -0700148 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
Dave Barach97d8dc22016-08-15 15:31:15 -0400149 * When flooding, the bvi interface (if present) must be the last member
150 * processed due to how BVI processing can change the packet. To enable
151 * this order, we make the bvi interface the first in the vector and
Neale Ranns87dad112018-04-09 01:53:01 -0700152 * flooding walks the vector in reverse. The flood-count determines where
153 * in the member list to start the walk from.
Dave Barach97d8dc22016-08-15 15:31:15 -0400154 */
Eyal Baric5b13602016-11-24 19:42:43 +0200155 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400156 {
Neale Ranns87dad112018-04-09 01:53:01 -0700157 case VNET_FLOOD_CLASS_NO_FLOOD:
158 bd_config->no_flood_count++;
159 ix = vec_len (bd_config->members);
160 break;
161 case VNET_FLOOD_CLASS_BVI:
162 ix = 0;
163 break;
Eyal Baric5b13602016-11-24 19:42:43 +0200164 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
165 bd_config->tun_master_count++;
166 /* Fall through */
Eyal Baric5b13602016-11-24 19:42:43 +0200167 case VNET_FLOOD_CLASS_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700168 ix = (vec_len (bd_config->members) -
169 bd_config->tun_normal_count - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200170 break;
171 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
Neale Ranns87dad112018-04-09 01:53:01 -0700172 ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
Eyal Baric5b13602016-11-24 19:42:43 +0200173 bd_config->tun_normal_count++;
174 break;
175 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176
Eyal Baric5b13602016-11-24 19:42:43 +0200177 vec_insert_elts (bd_config->members, member, 1, ix);
178 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179}
180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181#define BD_REMOVE_ERROR_OK 0
182#define BD_REMOVE_ERROR_NOT_FOUND 1
183
184u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400185bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700186{
187 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400188
189 /* Find and delete the member */
190 vec_foreach_index (ix, bd_config->members)
191 {
Eyal Baric5b13602016-11-24 19:42:43 +0200192 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
193 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400194 {
Eyal Baric5b13602016-11-24 19:42:43 +0200195 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
196 (vnet_get_main (), sw_if_index);
197
198 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
199 {
200 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
201 bd_config->tun_master_count--;
202 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
203 bd_config->tun_normal_count--;
Eyal Barib6b04f02018-07-11 14:01:36 +0300204 else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
205 bd_config->no_flood_count--;
Eyal Baric5b13602016-11-24 19:42:43 +0200206 }
Eyal Bari25b36672017-03-02 10:43:19 +0200207 vec_delete (bd_config->members, 1, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200208 update_flood_count (bd_config);
209
Dave Barach97d8dc22016-08-15 15:31:15 -0400210 return BD_REMOVE_ERROR_OK;
211 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212 }
213
214 return BD_REMOVE_ERROR_NOT_FOUND;
215}
216
217
Dave Barach97d8dc22016-08-15 15:31:15 -0400218clib_error_t *
219l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700220{
221 bd_main_t *bdm = &bd_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400222 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
223 /*
224 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
225 * to packet drop only. Thus, packets received from any L2 interface with
226 * uninitialized bd_index of 0 can be dropped safely.
227 */
Eyal Barib1352ed2017-04-07 23:14:17 +0300228 u32 bd_index = bd_add_bd_index (bdm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400230 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Choonho Son05480792017-03-29 20:07:45 +0900231
232 bdm->vlib_main = vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 return 0;
234}
235
236VLIB_INIT_FUNCTION (l2bd_init);
237
238
Dave Barach97d8dc22016-08-15 15:31:15 -0400239/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400240 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400241 Return 0 if ok, non-zero if for an error.
242*/
243u32
Neale Rannsb4743802018-09-05 09:13:57 -0700244bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
Dave Barach97d8dc22016-08-15 15:31:15 -0400245{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Eyal Bariafc47aa2017-04-20 14:45:17 +0300247 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 bd_validate (bd_config);
Eyal Bariafc47aa2017-04-20 14:45:17 +0300249 u32 feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Dave Barach97d8dc22016-08-15 15:31:15 -0400251 if (flags & L2_LEARN)
252 {
253 feature_bitmap |= L2INPUT_FEAT_LEARN;
254 }
255 if (flags & L2_FWD)
256 {
257 feature_bitmap |= L2INPUT_FEAT_FWD;
258 }
259 if (flags & L2_FLOOD)
260 {
261 feature_bitmap |= L2INPUT_FEAT_FLOOD;
262 }
263 if (flags & L2_UU_FLOOD)
264 {
265 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
266 }
267 if (flags & L2_ARP_TERM)
268 {
269 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
270 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Dave Barach97d8dc22016-08-15 15:31:15 -0400272 if (enable)
273 {
274 bd_config->feature_bitmap |= feature_bitmap;
275 }
276 else
277 {
278 bd_config->feature_bitmap &= ~feature_bitmap;
279 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
John Lo8d00fff2017-08-03 00:35:36 -0400281 return bd_config->feature_bitmap;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282}
283
Dave Barach97d8dc22016-08-15 15:31:15 -0400284/**
Damjan Mariond171d482016-12-05 14:16:38 +0100285 Set the mac age for the bridge domain.
286*/
287void
288bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
289{
290 l2_bridge_domain_t *bd_config;
291 int enable = 0;
292
293 vec_validate (l2input_main.bd_configs, bd_index);
294 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
295 bd_config->mac_age = age;
296
297 /* check if there is at least one bd with mac aging enabled */
298 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Barifead6702017-04-04 04:46:32 +0300299 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
Damjan Mariond171d482016-12-05 14:16:38 +0100300
301 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
302 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
303 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
304}
305
Jerome Tollet50570ec2017-09-14 12:53:56 +0100306/**
307 Set the tag for the bridge domain.
308*/
Jerome Tollet48304142017-09-05 12:13:22 +0100309
Jerome Tollet50570ec2017-09-14 12:53:56 +0100310static void
Jerome Tollet48304142017-09-05 12:13:22 +0100311bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
312{
313 u8 *old;
314 l2_bridge_domain_t *bd_config;
315 vec_validate (l2input_main.bd_configs, bd_index);
316 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
317
318 old = bd_config->bd_tag;
319
320 if (bd_tag[0])
321 {
322 bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
323 }
324 else
325 {
326 bd_config->bd_tag = NULL;
327 }
328
329 vec_free (old);
330}
331
Damjan Mariond171d482016-12-05 14:16:38 +0100332/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400333 Set bridge-domain learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400334 The CLI format is:
335 set bridge-domain learn <bd_id> [disable]
336*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337static clib_error_t *
338bd_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400339 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340{
Dave Barach97d8dc22016-08-15 15:31:15 -0400341 bd_main_t *bdm = &bd_main;
342 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343 u32 bd_index, bd_id;
344 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400345 uword *p;
346
347 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348 {
349 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400350 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 goto done;
352 }
353
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500354 if (bd_id == 0)
355 return clib_error_return (0,
356 "No operations on the default bridge domain are supported");
357
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
359
360 if (p == 0)
361 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400362
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 bd_index = p[0];
364
365 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400366 if (unformat (input, "disable"))
367 {
368 enable = 0;
369 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach97d8dc22016-08-15 15:31:15 -0400371 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400372 bd_set_flags (vm, bd_index, L2_LEARN, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373
Dave Barach97d8dc22016-08-15 15:31:15 -0400374done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 return error;
376}
377
Billy McFall22aa3e92016-09-09 08:46:40 -0400378/*?
379 * Layer 2 learning can be enabled and disabled on each
380 * interface and on each bridge-domain. Use this command to
381 * manage bridge-domains. It is enabled by default.
382 *
383 * @cliexpar
384 * Example of how to enable learning (where 200 is the bridge-domain-id):
385 * @cliexcmd{set bridge-domain learn 200}
386 * Example of how to disable learning (where 200 is the bridge-domain-id):
387 * @cliexcmd{set bridge-domain learn 200 disable}
388?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400389/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390VLIB_CLI_COMMAND (bd_learn_cli, static) = {
391 .path = "set bridge-domain learn",
392 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
393 .function = bd_learn,
394};
Dave Barach97d8dc22016-08-15 15:31:15 -0400395/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396
Dave Barach97d8dc22016-08-15 15:31:15 -0400397/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400398 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400399 The CLI format is:
400 set bridge-domain forward <bd_index> [disable]
401*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400403bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404{
Dave Barach97d8dc22016-08-15 15:31:15 -0400405 bd_main_t *bdm = &bd_main;
406 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700407 u32 bd_index, bd_id;
408 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400409 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410
Dave Barach97d8dc22016-08-15 15:31:15 -0400411 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412 {
413 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400414 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 goto done;
416 }
417
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500418 if (bd_id == 0)
419 return clib_error_return (0,
420 "No operations on the default bridge domain are supported");
421
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
423
424 if (p == 0)
425 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400426
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 bd_index = p[0];
428
429 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400430 if (unformat (input, "disable"))
431 {
432 enable = 0;
433 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434
Dave Barach97d8dc22016-08-15 15:31:15 -0400435 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400436 bd_set_flags (vm, bd_index, L2_FWD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Dave Barach97d8dc22016-08-15 15:31:15 -0400438done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 return error;
440}
441
Billy McFall22aa3e92016-09-09 08:46:40 -0400442
443/*?
444 * Layer 2 unicast forwarding can be enabled and disabled on each
445 * interface and on each bridge-domain. Use this command to
446 * manage bridge-domains. It is enabled by default.
447 *
448 * @cliexpar
449 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
450 * @cliexcmd{set bridge-domain forward 200}
451 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
452 * @cliexcmd{set bridge-domain forward 200 disable}
453?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400454/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
456 .path = "set bridge-domain forward",
457 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
458 .function = bd_fwd,
459};
Dave Barach97d8dc22016-08-15 15:31:15 -0400460/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461
Dave Barach97d8dc22016-08-15 15:31:15 -0400462/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400463 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400464 The CLI format is:
465 set bridge-domain flood <bd_index> [disable]
466*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467static clib_error_t *
468bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400469 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470{
Dave Barach97d8dc22016-08-15 15:31:15 -0400471 bd_main_t *bdm = &bd_main;
472 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 u32 bd_index, bd_id;
474 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400475 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476
Dave Barach97d8dc22016-08-15 15:31:15 -0400477 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478 {
479 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400480 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481 goto done;
482 }
483
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500484 if (bd_id == 0)
485 return clib_error_return (0,
486 "No operations on the default bridge domain are supported");
487
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
489
490 if (p == 0)
491 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400492
Ed Warnickecb9cada2015-12-08 15:45:58 -0700493 bd_index = p[0];
494
495 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400496 if (unformat (input, "disable"))
497 {
498 enable = 0;
499 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Dave Barach97d8dc22016-08-15 15:31:15 -0400501 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400502 bd_set_flags (vm, bd_index, L2_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
Dave Barach97d8dc22016-08-15 15:31:15 -0400504done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 return error;
506}
507
Billy McFall22aa3e92016-09-09 08:46:40 -0400508/*?
509 * Layer 2 flooding can be enabled and disabled on each
510 * interface and on each bridge-domain. Use this command to
511 * manage bridge-domains. It is enabled by default.
512 *
513 * @cliexpar
514 * Example of how to enable flooding (where 200 is the bridge-domain-id):
515 * @cliexcmd{set bridge-domain flood 200}
516 * Example of how to disable flooding (where 200 is the bridge-domain-id):
517 * @cliexcmd{set bridge-domain flood 200 disable}
518?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400519/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520VLIB_CLI_COMMAND (bd_flood_cli, static) = {
521 .path = "set bridge-domain flood",
522 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
523 .function = bd_flood,
524};
Dave Barach97d8dc22016-08-15 15:31:15 -0400525/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526
Dave Barach97d8dc22016-08-15 15:31:15 -0400527/**
Andrey "Zed" Zaikin701625b2018-04-18 17:07:07 +0300528 Set bridge-domain unknown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400529 The CLI format is:
530 set bridge-domain uu-flood <bd_index> [disable]
531*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532static clib_error_t *
533bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400534 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535{
Dave Barach97d8dc22016-08-15 15:31:15 -0400536 bd_main_t *bdm = &bd_main;
537 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 u32 bd_index, bd_id;
539 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400540 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Dave Barach97d8dc22016-08-15 15:31:15 -0400542 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 {
544 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400545 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546 goto done;
547 }
548
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500549 if (bd_id == 0)
550 return clib_error_return (0,
551 "No operations on the default bridge domain are supported");
552
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
554
555 if (p == 0)
556 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400557
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558 bd_index = p[0];
559
560 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400561 if (unformat (input, "disable"))
562 {
563 enable = 0;
564 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Dave Barach97d8dc22016-08-15 15:31:15 -0400566 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400567 bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700568
Dave Barach97d8dc22016-08-15 15:31:15 -0400569done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 return error;
571}
572
Billy McFall22aa3e92016-09-09 08:46:40 -0400573/*?
574 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
575 * bridge-domain. It is enabled by default.
576 *
577 * @cliexpar
578 * Example of how to enable unknown-unicast flooding (where 200 is the
579 * bridge-domain-id):
580 * @cliexcmd{set bridge-domain uu-flood 200}
581 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
582 * @cliexcmd{set bridge-domain uu-flood 200 disable}
583?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400584/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
586 .path = "set bridge-domain uu-flood",
587 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
588 .function = bd_uu_flood,
589};
Dave Barach97d8dc22016-08-15 15:31:15 -0400590/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Dave Barach97d8dc22016-08-15 15:31:15 -0400592/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400593 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400594 The CLI format is:
595 set bridge-domain arp term <bridge-domain-id> [disable]
596*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597static clib_error_t *
598bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400599 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600{
Dave Barach97d8dc22016-08-15 15:31:15 -0400601 bd_main_t *bdm = &bd_main;
602 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 u32 bd_index, bd_id;
604 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400605 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606
Dave Barach97d8dc22016-08-15 15:31:15 -0400607 if (!unformat (input, "%d", &bd_id))
608 {
609 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
610 format_unformat_error, input);
611 goto done;
612 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500614 if (bd_id == 0)
615 return clib_error_return (0,
616 "No operations on the default bridge domain are supported");
617
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400619 if (p)
620 bd_index = *p;
621 else
622 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623
Dave Barach97d8dc22016-08-15 15:31:15 -0400624 enable = 1;
625 if (unformat (input, "disable"))
626 enable = 0;
627
628 /* set the bridge domain flag */
John Lo8d00fff2017-08-03 00:35:36 -0400629 bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630
631done:
632 return error;
633}
634
Damjan Mariond171d482016-12-05 14:16:38 +0100635static clib_error_t *
636bd_mac_age (vlib_main_t * vm,
637 unformat_input_t * input, vlib_cli_command_t * cmd)
638{
639 bd_main_t *bdm = &bd_main;
640 clib_error_t *error = 0;
641 u32 bd_index, bd_id;
642 u32 age;
643 uword *p;
644
645 if (!unformat (input, "%d", &bd_id))
646 {
647 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
648 format_unformat_error, input);
649 goto done;
650 }
651
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500652 if (bd_id == 0)
653 return clib_error_return (0,
654 "No operations on the default bridge domain are supported");
655
Damjan Mariond171d482016-12-05 14:16:38 +0100656 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
657
658 if (p == 0)
659 return clib_error_return (0, "No such bridge domain %d", bd_id);
660
661 bd_index = p[0];
662
663 if (!unformat (input, "%u", &age))
664 {
665 error =
666 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
667 format_unformat_error, input);
668 goto done;
669 }
670
671 /* set the bridge domain flag */
672 if (age > 255)
673 {
674 error =
675 clib_error_return (0, "mac aging time cannot be bigger than 255");
676 goto done;
677 }
678 bd_set_mac_age (vm, bd_index, (u8) age);
679
680done:
681 return error;
682}
683
684/*?
685 * Layer 2 mac aging can be enabled and disabled on each
686 * bridge-domain. Use this command to set or disable mac aging
687 * on specific bridge-domains. It is disabled by default.
688 *
689 * @cliexpar
690 * Example of how to set mac aging (where 200 is the bridge-domain-id and
691 * 5 is aging time in minutes):
692 * @cliexcmd{set bridge-domain mac-age 200 5}
693 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
694 * @cliexcmd{set bridge-domain flood 200 0}
695?*/
696/* *INDENT-OFF* */
697VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
698 .path = "set bridge-domain mac-age",
699 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
700 .function = bd_mac_age,
701};
702/* *INDENT-ON* */
703
Billy McFall22aa3e92016-09-09 08:46:40 -0400704/*?
705 * Modify whether or not an existing bridge-domain should terminate and respond
706 * to ARP Requests. ARP Termination is disabled by default.
707 *
708 * @cliexpar
709 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
710 * @cliexcmd{set bridge-domain arp term 200}
711 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
712 * @cliexcmd{set bridge-domain arp term 200 disable}
713?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400714/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
716 .path = "set bridge-domain arp term",
717 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
718 .function = bd_arp_term,
719};
Dave Barach97d8dc22016-08-15 15:31:15 -0400720/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
722
Dave Barach97d8dc22016-08-15 15:31:15 -0400723/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400724 * Add/delete IP address to MAC address mapping.
725 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400726 * The clib hash implementation stores uword entries in the hash table.
727 * The hash table mac_by_ip4 is keyed via IP4 address and store the
728 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400729 *
730 * @warning This only works for 64-bit processor with 8-byte uword;
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700731 * which means this code *WILL NOT WORK* for a 32-bit processor with
Chris Luke16bcf7d2016-09-01 14:31:46 -0400732 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400733 */
734u32
735bd_add_del_ip_mac (u32 bd_index,
736 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300738 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739 u64 new_mac = *(u64 *) mac_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -0400740 u64 *old_mac;
741 u16 *mac16 = (u16 *) & new_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742
Dave Barach97d8dc22016-08-15 15:31:15 -0400743 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
Eyal Bariafc47aa2017-04-20 14:45:17 +0300744 ASSERT (bd_is_valid (bd_cfg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700746 mac16[3] = 0; /* Clear last 2 unused bytes of the 8-byte MAC address */
Dave Barach97d8dc22016-08-15 15:31:15 -0400747 if (is_ip6)
748 {
John Lo1edfba92016-08-27 01:11:57 -0400749 ip6_address_t *ip6_addr_key;
750 hash_pair_t *hp;
751 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
752 if (is_add)
753 {
754 if (old_mac == 0)
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700755 { /* new entry - allocate and create ip6 address key */
John Lo1edfba92016-08-27 01:11:57 -0400756 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
757 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
758 }
759 else if (*old_mac == new_mac)
760 { /* same mac entry already exist for ip6 address */
761 return 0;
762 }
763 else
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700764 { /* update mac for ip6 address */
John Lo1edfba92016-08-27 01:11:57 -0400765 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
766 ip6_addr_key = (ip6_address_t *) hp->key;
767 }
768 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
769 }
770 else
771 {
772 if (old_mac && (*old_mac == new_mac))
773 {
774 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
775 ip6_addr_key = (ip6_address_t *) hp->key;
776 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
777 clib_mem_free (ip6_addr_key);
778 }
779 else
780 return 1;
781 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700782 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400783 else
784 {
785 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
786 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
787 if (is_add)
788 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400789 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400790 return 0; /* mac entry already exist */
Dave Barach97d8dc22016-08-15 15:31:15 -0400791 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
792 }
793 else
794 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400795 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400796 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400797 else
John Lo1edfba92016-08-27 01:11:57 -0400798 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400799 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400800 }
John Lo1edfba92016-08-27 01:11:57 -0400801 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700802}
803
Dave Barach97d8dc22016-08-15 15:31:15 -0400804/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400805 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -0400806 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -0400807 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -0400808*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700809static clib_error_t *
810bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400811 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812{
Dave Barach97d8dc22016-08-15 15:31:15 -0400813 bd_main_t *bdm = &bd_main;
814 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815 u32 bd_index, bd_id;
816 u8 is_add = 1;
817 u8 is_ip6 = 0;
818 u8 ip_addr[16];
819 u8 mac_addr[6];
Dave Barach97d8dc22016-08-15 15:31:15 -0400820 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821
Dave Barach97d8dc22016-08-15 15:31:15 -0400822 if (!unformat (input, "%d", &bd_id))
823 {
824 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
825 format_unformat_error, input);
826 goto done;
827 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700828
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500829 if (bd_id == 0)
830 return clib_error_return (0,
831 "No operations on the default bridge domain are supported");
832
Ed Warnickecb9cada2015-12-08 15:45:58 -0700833 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
834
Dave Barach97d8dc22016-08-15 15:31:15 -0400835 if (p)
836 bd_index = *p;
837 else
838 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700839
Dave Barach97d8dc22016-08-15 15:31:15 -0400840 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
841 {
842 is_ip6 = 0;
843 }
844 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
845 {
846 is_ip6 = 1;
847 }
848 else
849 {
850 error = clib_error_return (0, "expecting IP address but got `%U'",
851 format_unformat_error, input);
852 goto done;
853 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
Dave Barach97d8dc22016-08-15 15:31:15 -0400855 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
856 {
857 error = clib_error_return (0, "expecting MAC address but got `%U'",
858 format_unformat_error, input);
859 goto done;
860 }
861
862 if (unformat (input, "del"))
863 {
864 is_add = 0;
865 }
866
867 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
868 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
869 {
870 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
871 is_add ? "add" : "del",
John Lo1edfba92016-08-27 01:11:57 -0400872 is_ip6 ?
873 format_ip4_address : format_ip6_address,
874 ip_addr, format_ethernet_address, mac_addr);
Dave Barach97d8dc22016-08-15 15:31:15 -0400875 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876
877done:
878 return error;
879}
880
Billy McFall22aa3e92016-09-09 08:46:40 -0400881/*?
882 * Add an ARP entry to an existing bridge-domain.
883 *
884 * @cliexpar
885 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
886 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
887 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
888 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
889?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400890/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
892 .path = "set bridge-domain arp entry",
Billy McFall22aa3e92016-09-09 08:46:40 -0400893 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 .function = bd_arp_entry,
895};
Dave Barach97d8dc22016-08-15 15:31:15 -0400896/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700897
Neale Rannsb4743802018-09-05 09:13:57 -0700898static u8 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400899format_vtr (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900{
Dave Barach97d8dc22016-08-15 15:31:15 -0400901 u32 vtr_op = va_arg (*args, u32);
902 u32 dot1q = va_arg (*args, u32);
903 u32 tag1 = va_arg (*args, u32);
904 u32 tag2 = va_arg (*args, u32);
905 switch (vtr_op)
906 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 case L2_VTR_DISABLED:
Dave Barach97d8dc22016-08-15 15:31:15 -0400908 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 case L2_VTR_PUSH_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400910 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700911 case L2_VTR_PUSH_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400912 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
913 tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 case L2_VTR_POP_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400915 return format (s, "pop-1");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916 case L2_VTR_POP_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400917 return format (s, "pop-2");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918 case L2_VTR_TRANSLATE_1_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400919 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 case L2_VTR_TRANSLATE_1_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400921 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
922 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923 case L2_VTR_TRANSLATE_2_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400924 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 case L2_VTR_TRANSLATE_2_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400926 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
927 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 default:
Dave Barach97d8dc22016-08-15 15:31:15 -0400929 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 }
931}
932
Neale Rannsb4743802018-09-05 09:13:57 -0700933static u8 *
934format_uu_cfg (u8 * s, va_list * args)
935{
936 l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
937
938 if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
939 return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
940 vnet_get_main (), bd_config->uu_fwd_sw_if_index));
941 else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
942 return (format (s, "flood"));
943 else
944 return (format (s, "drop"));
945}
946
Dave Barach97d8dc22016-08-15 15:31:15 -0400947/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400948 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -0400949 The CLI format is:
950 show bridge-domain [<bd_index>]
951*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700952static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400953bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954{
Dave Barach97d8dc22016-08-15 15:31:15 -0400955 vnet_main_t *vnm = vnet_get_main ();
956 bd_main_t *bdm = &bd_main;
957 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400959 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 u32 start, end;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 u32 detail = 0;
962 u32 intf = 0;
963 u32 arp = 0;
Jerome Tollet48304142017-09-05 12:13:22 +0100964 u32 bd_tag = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700965 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400966 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500968 start = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400969 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970
Dave Barach97d8dc22016-08-15 15:31:15 -0400971 if (unformat (input, "%d", &bd_id))
972 {
973 if (unformat (input, "detail"))
974 detail = 1;
975 else if (unformat (input, "det"))
976 detail = 1;
977 if (unformat (input, "int"))
978 intf = 1;
979 if (unformat (input, "arp"))
980 arp = 1;
Jerome Tollet48304142017-09-05 12:13:22 +0100981 if (unformat (input, "bd-tag"))
982 bd_tag = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700983
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500984 if (bd_id == 0)
985 return clib_error_return (0,
986 "No operations on the default bridge domain are supported");
987
Dave Barach97d8dc22016-08-15 15:31:15 -0400988 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
989 if (p)
990 bd_index = *p;
991 else
992 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700993
Dave Barach97d8dc22016-08-15 15:31:15 -0400994 vec_validate (l2input_main.bd_configs, bd_index);
995 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
996 if (bd_is_valid (bd_config))
997 {
998 start = bd_index;
999 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001001 else
1002 {
1003 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1004 goto done;
1005 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007
Dave Barach97d8dc22016-08-15 15:31:15 -04001008 /* Show all bridge-domains that have been initialized */
John Loda1f2c72017-03-24 20:11:15 -04001009 u32 printed = 0;
1010 u8 *as = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -04001011 for (bd_index = start; bd_index < end; bd_index++)
1012 {
1013 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1014 if (bd_is_valid (bd_config))
1015 {
1016 if (!printed)
1017 {
1018 printed = 1;
1019 vlib_cli_output (vm,
Neale Rannsb4743802018-09-05 09:13:57 -07001020 "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s %=11s",
John Lo97934772017-05-18 22:26:47 -04001021 "BD-ID", "Index", "BSN", "Age(min)",
Neale Rannsb4743802018-09-05 09:13:57 -07001022 "Learning", "U-Forwrd", "UU-Flood",
1023 "Flooding", "ARP-Term", "BVI-Intf");
Dave Barach97d8dc22016-08-15 15:31:15 -04001024 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025
John Loda1f2c72017-03-24 20:11:15 -04001026 if (bd_config->mac_age)
1027 as = format (as, "%d", bd_config->mac_age);
1028 else
1029 as = format (as, "off");
Dave Barach97d8dc22016-08-15 15:31:15 -04001030 vlib_cli_output (vm,
Neale Rannsb4743802018-09-05 09:13:57 -07001031 "%=8d %=7d %=4d %=9v %=9s %=9s %=11U %=9s %=9s %=11U",
John Loda1f2c72017-03-24 20:11:15 -04001032 bd_config->bd_id, bd_index, bd_config->seq_num, as,
Dave Barach97d8dc22016-08-15 15:31:15 -04001033 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1034 "on" : "off",
John Loda1f2c72017-03-24 20:11:15 -04001035 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1036 "on" : "off",
Neale Rannsb4743802018-09-05 09:13:57 -07001037 format_uu_cfg, bd_config,
Dave Barach97d8dc22016-08-15 15:31:15 -04001038 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1039 "on" : "off",
1040 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
John Loda1f2c72017-03-24 20:11:15 -04001041 "on" : "off",
1042 format_vnet_sw_if_index_name_with_NA,
Dave Barach97d8dc22016-08-15 15:31:15 -04001043 vnm, bd_config->bvi_sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -04001044 vec_reset_length (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001045
1046 if (detail || intf)
1047 {
1048 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +02001049 int i;
1050 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -04001051 {
Eyal Baric5b13602016-11-24 19:42:43 +02001052 l2_flood_member_t *member =
1053 vec_elt_at_index (bd_config->members, i);
Eyal Bari0f360dc2017-06-14 13:11:20 +03001054 u8 swif_seq_num = *l2fib_swif_seq_num (member->sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -04001055 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +02001056 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -04001057 {
John Loda1f2c72017-03-24 20:11:15 -04001058 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1059 "Interface", "If-idx", "ISN", "SHG",
1060 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -04001061 }
1062 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1063 &tag1, &tag2);
John Loda1f2c72017-03-24 20:11:15 -04001064 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001065 format_vnet_sw_if_index_name, vnm,
1066 member->sw_if_index, member->sw_if_index,
Eyal Bari0f360dc2017-06-14 13:11:20 +03001067 swif_seq_num, member->shg,
Dave Barach97d8dc22016-08-15 15:31:15 -04001068 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +02001069 "-", i < bd_config->flood_count ? "*" : "-",
1070 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001071 }
Neale Rannsb4743802018-09-05 09:13:57 -07001072 if (~0 != bd_config->uu_fwd_sw_if_index)
1073 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1074 format_vnet_sw_if_index_name, vnm,
1075 bd_config->uu_fwd_sw_if_index,
1076 bd_config->uu_fwd_sw_if_index,
1077 0, 0, "uu", "-", "None");
1078
Dave Barach97d8dc22016-08-15 15:31:15 -04001079 }
1080
1081 if ((detail || arp) &&
1082 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1083 {
1084 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001085 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001086 u64 mac_addr;
1087 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001088 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001089
1090 /* *INDENT-OFF* */
1091 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1092 ({
John Lo1edfba92016-08-27 01:11:57 -04001093 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001094 format_ip4_address, &ip4_addr,
1095 format_ethernet_address, &mac_addr);
1096 }));
John Lo1edfba92016-08-27 01:11:57 -04001097
1098 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1099 ({
1100 vlib_cli_output (vm, "%=40U => %=20U",
1101 format_ip6_address, ip6_addr,
1102 format_ethernet_address, &mac_addr);
1103 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001104 /* *INDENT-ON* */
1105 }
Jerome Tollet48304142017-09-05 12:13:22 +01001106
1107 if ((detail || bd_tag) && (bd_config->bd_tag))
1108 {
1109 vlib_cli_output (vm, "\n BD-Tag: %s", bd_config->bd_tag);
1110
1111 }
Dave Barach97d8dc22016-08-15 15:31:15 -04001112 }
1113 }
John Loda1f2c72017-03-24 20:11:15 -04001114 vec_free (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001115
1116 if (!printed)
1117 {
1118 vlib_cli_output (vm, "no bridge-domains in use");
1119 }
1120
1121done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122 return error;
1123}
1124
Billy McFall22aa3e92016-09-09 08:46:40 -04001125/*?
1126 * Show a summary of all the bridge-domain instances or detailed view of a
1127 * single bridge-domain. Bridge-domains are created by adding an interface
1128 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1129 *
1130 * @cliexpar
1131 * @parblock
1132 * Example of displaying all bridge-domains:
1133 * @cliexstart{show bridge-domain}
1134 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1135 * 0 0 off off off off off local0
1136 * 200 1 on on on on off N/A
1137 * @cliexend
1138 *
1139 * Example of displaying details of a single bridge-domains:
1140 * @cliexstart{show bridge-domain 200 detail}
1141 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1142 * 200 1 on on on on off N/A
1143 *
1144 * Interface Index SHG BVI VLAN-Tag-Rewrite
1145 * GigabitEthernet0/8/0.200 3 0 - none
1146 * GigabitEthernet0/9/0.200 4 0 - none
1147 * @cliexend
1148 * @endparblock
1149?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001150/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001151VLIB_CLI_COMMAND (bd_show_cli, static) = {
1152 .path = "show bridge-domain",
Jerome Tollet48304142017-09-05 12:13:22 +01001153 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154 .function = bd_show,
1155};
Dave Barach97d8dc22016-08-15 15:31:15 -04001156/* *INDENT-ON* */
1157
Choonho Son05480792017-03-29 20:07:45 +09001158int
1159bd_add_del (l2_bridge_domain_add_del_args_t * a)
1160{
1161 bd_main_t *bdm = &bd_main;
1162 vlib_main_t *vm = bdm->vlib_main;
Choonho Son05480792017-03-29 20:07:45 +09001163 int rv = 0;
1164
Eyal Barib1352ed2017-04-07 23:14:17 +03001165 u32 bd_index = bd_find_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001166 if (a->is_add)
1167 {
Eyal Barib1352ed2017-04-07 23:14:17 +03001168 if (bd_index != ~0)
1169 return VNET_API_ERROR_BD_ALREADY_EXISTS;
John Lo97934772017-05-18 22:26:47 -04001170 if (a->bd_id > L2_BD_ID_MAX)
1171 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
Eyal Barib1352ed2017-04-07 23:14:17 +03001172 bd_index = bd_add_bd_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001173
Neale Rannsb4743802018-09-05 09:13:57 -07001174 bd_flags_t enable_flags = 0, disable_flags = 0;
Choonho Son05480792017-03-29 20:07:45 +09001175 if (a->flood)
1176 enable_flags |= L2_FLOOD;
1177 else
1178 disable_flags |= L2_FLOOD;
1179
1180 if (a->uu_flood)
1181 enable_flags |= L2_UU_FLOOD;
1182 else
1183 disable_flags |= L2_UU_FLOOD;
1184
1185 if (a->forward)
1186 enable_flags |= L2_FWD;
1187 else
1188 disable_flags |= L2_FWD;
1189
1190 if (a->learn)
1191 enable_flags |= L2_LEARN;
1192 else
1193 disable_flags |= L2_LEARN;
1194
1195 if (a->arp_term)
1196 enable_flags |= L2_ARP_TERM;
1197 else
1198 disable_flags |= L2_ARP_TERM;
1199
1200 if (enable_flags)
1201 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1202
1203 if (disable_flags)
1204 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1205
1206 bd_set_mac_age (vm, bd_index, a->mac_age);
Jerome Tollet48304142017-09-05 12:13:22 +01001207
1208 if (a->bd_tag)
1209 bd_set_bd_tag (vm, bd_index, a->bd_tag);
1210
Choonho Son05480792017-03-29 20:07:45 +09001211 }
1212 else
Eyal Barib1352ed2017-04-07 23:14:17 +03001213 {
1214 if (bd_index == ~0)
1215 return VNET_API_ERROR_NO_SUCH_ENTRY;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001216 if (bd_index == 0)
1217 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
Eyal Barib1352ed2017-04-07 23:14:17 +03001218 if (vec_len (l2input_main.bd_configs[bd_index].members))
1219 return VNET_API_ERROR_BD_IN_USE;
1220 rv = bd_delete (bdm, bd_index);
1221 }
Choonho Son05480792017-03-29 20:07:45 +09001222
1223 return rv;
1224}
1225
1226/**
1227 Create or delete bridge-domain.
Choonho Son5ee51f82017-04-05 19:09:52 +09001228 The CLI format:
Jerome Tollet50570ec2017-09-14 12:53:56 +01001229 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1230 [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
Choonho Son05480792017-03-29 20:07:45 +09001231*/
1232
1233static clib_error_t *
1234bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1235 vlib_cli_command_t * cmd)
1236{
1237 unformat_input_t _line_input, *line_input = &_line_input;
1238 clib_error_t *error = 0;
1239 u8 is_add = 1;
1240 u32 bd_id = ~0;
Choonho Son5ee51f82017-04-05 19:09:52 +09001241 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
Choonho Son05480792017-03-29 20:07:45 +09001242 u32 mac_age = 0;
Jerome Tollet48304142017-09-05 12:13:22 +01001243 u8 *bd_tag = NULL;
Choonho Son05480792017-03-29 20:07:45 +09001244 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1245 int rv;
1246
1247 /* Get a line of input. */
1248 if (!unformat_user (input, unformat_line_input, line_input))
1249 return 0;
1250
1251 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1252 {
1253 if (unformat (line_input, "%d", &bd_id))
1254 ;
1255 else if (unformat (line_input, "flood %d", &flood))
1256 ;
1257 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1258 ;
1259 else if (unformat (line_input, "forward %d", &forward))
1260 ;
Choonho Son5ee51f82017-04-05 19:09:52 +09001261 else if (unformat (line_input, "learn %d", &learn))
1262 ;
Choonho Son05480792017-03-29 20:07:45 +09001263 else if (unformat (line_input, "arp-term %d", &arp_term))
1264 ;
1265 else if (unformat (line_input, "mac-age %d", &mac_age))
1266 ;
Jerome Tollet48304142017-09-05 12:13:22 +01001267 else if (unformat (line_input, "bd-tag %s", &bd_tag))
1268 ;
Choonho Son05480792017-03-29 20:07:45 +09001269 else if (unformat (line_input, "del"))
1270 {
1271 is_add = 0;
1272 flood = uu_flood = forward = learn = 0;
1273 }
1274 else
1275 break;
1276 }
1277
1278 if (bd_id == ~0)
1279 {
1280 error = clib_error_return (0, "bridge-domain-id not specified");
1281 goto done;
1282 }
1283
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001284 if (bd_id == 0)
1285 {
1286 error = clib_error_return (0, "bridge domain 0 can not be modified");
1287 goto done;
1288 }
1289
Choonho Son05480792017-03-29 20:07:45 +09001290 if (mac_age > 255)
1291 {
1292 error = clib_error_return (0, "mac age must be less than 256");
1293 goto done;
1294 }
Jerome Tollet48304142017-09-05 12:13:22 +01001295 if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1296 {
1297 error = clib_error_return (0, "bd-tag cannot be longer than 63");
1298 goto done;
1299 }
Choonho Son05480792017-03-29 20:07:45 +09001300
1301 memset (a, 0, sizeof (*a));
1302 a->is_add = is_add;
1303 a->bd_id = bd_id;
1304 a->flood = (u8) flood;
1305 a->uu_flood = (u8) uu_flood;
1306 a->forward = (u8) forward;
1307 a->learn = (u8) learn;
1308 a->arp_term = (u8) arp_term;
1309 a->mac_age = (u8) mac_age;
Jerome Tollet48304142017-09-05 12:13:22 +01001310 a->bd_tag = bd_tag;
Choonho Son05480792017-03-29 20:07:45 +09001311
1312 rv = bd_add_del (a);
1313
1314 switch (rv)
1315 {
1316 case 0:
1317 if (is_add)
1318 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1319 break;
Eyal Barib1352ed2017-04-07 23:14:17 +03001320 case VNET_API_ERROR_BD_IN_USE:
1321 error = clib_error_return (0, "bridge domain in use - remove members");
1322 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001323 case VNET_API_ERROR_NO_SUCH_ENTRY:
John Lo97934772017-05-18 22:26:47 -04001324 error = clib_error_return (0, "bridge domain ID does not exist");
Choonho Son05480792017-03-29 20:07:45 +09001325 goto done;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001326 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1327 error = clib_error_return (0, "bridge domain 0 can not be modified");
1328 goto done;
John Lo97934772017-05-18 22:26:47 -04001329 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1330 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1331 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001332 default:
1333 error = clib_error_return (0, "bd_add_del returned %d", rv);
1334 goto done;
1335 }
1336
1337done:
Jerome Tollet48304142017-09-05 12:13:22 +01001338 vec_free (bd_tag);
Choonho Son05480792017-03-29 20:07:45 +09001339 unformat_free (line_input);
1340
1341 return error;
1342}
1343
1344
1345/*?
1346 * Create/Delete bridge-domain instance
1347 *
1348 * @cliexpar
1349 * @parblock
1350 * Example of creating bridge-domain 1:
1351 * @cliexstart{create bridge-domain 1}
1352 * bridge-domain 1
1353 * @cliexend
1354 *
1355 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1356 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1357 * bridge-domain 2
1358 *
1359 * vpp# show bridge-domain
1360 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1361 * 0 0 0 off off off off off off local0
1362 * 1 1 0 off on on off on off N/A
1363 * 2 2 0 60 on on off on on N/A
1364 *
1365 * @cliexend
1366 *
1367 * Example of delete bridge-domain 1:
1368 * @cliexstart{create bridge-domain 1 del}
1369 * @cliexend
1370 * @endparblock
1371?*/
1372
1373/* *INDENT-OFF* */
1374VLIB_CLI_COMMAND (bd_create_cli, static) = {
1375 .path = "create bridge-domain",
1376 .short_help = "create bridge-domain <bridge-domain-id>"
1377 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
Jerome Tollet48304142017-09-05 12:13:22 +01001378 " [mac-age <nn>] [bd-tag <tag>] [del]",
Choonho Son05480792017-03-29 20:07:45 +09001379 .function = bd_add_del_command_fn,
1380};
1381/* *INDENT-ON* */
1382
1383
1384
Dave Barach97d8dc22016-08-15 15:31:15 -04001385/*
1386 * fd.io coding-style-patch-verification: ON
1387 *
1388 * Local Variables:
1389 * eval: (c-set-style "gnu")
1390 * End:
1391 */