blob: 22f83d0b3e436185980d60e053f1c58352533f2b [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{
Dave Barach97d8dc22016-08-15 15:31:15 -040053 if (!bd_is_valid (bd_config))
54 {
55 bd_config->feature_bitmap = ~L2INPUT_FEAT_ARP_TERM;
56 bd_config->bvi_sw_if_index = ~0;
57 bd_config->members = 0;
Eyal Baric5b13602016-11-24 19:42:43 +020058 bd_config->flood_count = 0;
59 bd_config->tun_master_count = 0;
60 bd_config->tun_normal_count = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -040061 bd_config->mac_by_ip4 = 0;
John Lo1edfba92016-08-27 01:11:57 -040062 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
63 sizeof (uword));
Dave Barach97d8dc22016-08-15 15:31:15 -040064 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070065}
66
Dave Barach97d8dc22016-08-15 15:31:15 -040067u32
68bd_find_or_add_bd_index (bd_main_t * bdm, u32 bd_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069{
Dave Barach97d8dc22016-08-15 15:31:15 -040070 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -070071 u32 rv;
72
Dave Barach97d8dc22016-08-15 15:31:15 -040073 if (bd_id == ~0)
74 {
75 bd_id = 0;
76 while (hash_get (bdm->bd_index_by_bd_id, bd_id))
77 bd_id++;
78 }
79 else
80 {
81 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
82 if (p)
83 return (p[0]);
84 }
85
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
87
Dave Barach97d8dc22016-08-15 15:31:15 -040088 /* mark this index busy */
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
90
91 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
92
93 vec_validate (l2input_main.bd_configs, rv);
94 l2input_main.bd_configs[rv].bd_id = bd_id;
95
96 return rv;
97}
98
Dave Barach97d8dc22016-08-15 15:31:15 -040099int
100bd_delete_bd_index (bd_main_t * bdm, u32 bd_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101{
Dave Barach97d8dc22016-08-15 15:31:15 -0400102 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103 u32 bd_index;
104
105 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
106 if (p == 0)
107 return -1;
108
109 bd_index = p[0];
Dave Barach97d8dc22016-08-15 15:31:15 -0400110
111 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
113 hash_unset (bdm->bd_index_by_bd_id, bd_id);
114
115 l2input_main.bd_configs[bd_index].bd_id = ~0;
116 l2input_main.bd_configs[bd_index].feature_bitmap = 0;
117
118 return 0;
119}
120
Eyal Baric5b13602016-11-24 19:42:43 +0200121static void
122update_flood_count (l2_bridge_domain_t * bd_config)
123{
124 bd_config->flood_count = vec_len (bd_config->members) -
125 (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
126}
127
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128void
Dave Barach97d8dc22016-08-15 15:31:15 -0400129bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130{
Eyal Baric5b13602016-11-24 19:42:43 +0200131 u32 ix;
132 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
133 (vnet_get_main (), member->sw_if_index);
134
Dave Barach97d8dc22016-08-15 15:31:15 -0400135 /*
136 * Add one element to the vector
Eyal Baric5b13602016-11-24 19:42:43 +0200137 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
Dave Barach97d8dc22016-08-15 15:31:15 -0400138 * When flooding, the bvi interface (if present) must be the last member
139 * processed due to how BVI processing can change the packet. To enable
140 * this order, we make the bvi interface the first in the vector and
141 * flooding walks the vector in reverse.
142 */
Eyal Baric5b13602016-11-24 19:42:43 +0200143 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400144 {
Eyal Baric5b13602016-11-24 19:42:43 +0200145 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
146 bd_config->tun_master_count++;
147 /* Fall through */
148 default:
149 /* Fall through */
150 case VNET_FLOOD_CLASS_NORMAL:
151 ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
152 vec_len (bd_config->members) - bd_config->tun_normal_count;
153 break;
154 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
155 ix = vec_len (bd_config->members);
156 bd_config->tun_normal_count++;
157 break;
158 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Eyal Baric5b13602016-11-24 19:42:43 +0200160 vec_insert_elts (bd_config->members, member, 1, ix);
161 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162}
163
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164#define BD_REMOVE_ERROR_OK 0
165#define BD_REMOVE_ERROR_NOT_FOUND 1
166
167u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400168bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169{
170 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400171
172 /* Find and delete the member */
173 vec_foreach_index (ix, bd_config->members)
174 {
Eyal Baric5b13602016-11-24 19:42:43 +0200175 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
176 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400177 {
Eyal Baric5b13602016-11-24 19:42:43 +0200178 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
179 (vnet_get_main (), sw_if_index);
180
181 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
182 {
183 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
184 bd_config->tun_master_count--;
185 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
186 bd_config->tun_normal_count--;
187 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400188 vec_del1 (bd_config->members, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200189 update_flood_count (bd_config);
190
Dave Barach97d8dc22016-08-15 15:31:15 -0400191 return BD_REMOVE_ERROR_OK;
192 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700193 }
194
195 return BD_REMOVE_ERROR_NOT_FOUND;
196}
197
198
Dave Barach97d8dc22016-08-15 15:31:15 -0400199clib_error_t *
200l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201{
202 bd_main_t *bdm = &bd_main;
203 u32 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -0400204 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
205 /*
206 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
207 * to packet drop only. Thus, packets received from any L2 interface with
208 * uninitialized bd_index of 0 can be dropped safely.
209 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 bd_index = bd_find_or_add_bd_index (bdm, 0);
211 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 return 0;
214}
215
216VLIB_INIT_FUNCTION (l2bd_init);
217
218
Dave Barach97d8dc22016-08-15 15:31:15 -0400219/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400220 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400221 Return 0 if ok, non-zero if for an error.
222*/
223u32
224bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
225{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226
Dave Barach97d8dc22016-08-15 15:31:15 -0400227 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228 u32 feature_bitmap = 0;
229
230 vec_validate (l2input_main.bd_configs, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400231 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700232
233 bd_validate (bd_config);
234
Dave Barach97d8dc22016-08-15 15:31:15 -0400235 if (flags & L2_LEARN)
236 {
237 feature_bitmap |= L2INPUT_FEAT_LEARN;
238 }
239 if (flags & L2_FWD)
240 {
241 feature_bitmap |= L2INPUT_FEAT_FWD;
242 }
243 if (flags & L2_FLOOD)
244 {
245 feature_bitmap |= L2INPUT_FEAT_FLOOD;
246 }
247 if (flags & L2_UU_FLOOD)
248 {
249 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
250 }
251 if (flags & L2_ARP_TERM)
252 {
253 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
254 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255
Dave Barach97d8dc22016-08-15 15:31:15 -0400256 if (enable)
257 {
258 bd_config->feature_bitmap |= feature_bitmap;
259 }
260 else
261 {
262 bd_config->feature_bitmap &= ~feature_bitmap;
263 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
265 return 0;
266}
267
Dave Barach97d8dc22016-08-15 15:31:15 -0400268/**
Damjan Mariond171d482016-12-05 14:16:38 +0100269 Set the mac age for the bridge domain.
270*/
271void
272bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
273{
274 l2_bridge_domain_t *bd_config;
275 int enable = 0;
276
277 vec_validate (l2input_main.bd_configs, bd_index);
278 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
279 bd_config->mac_age = age;
280
281 /* check if there is at least one bd with mac aging enabled */
282 vec_foreach (bd_config, l2input_main.bd_configs)
283 if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
284 enable = 1;
285
286 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
287 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
288 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
289}
290
291/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400292 Set bridge-domain learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400293 The CLI format is:
294 set bridge-domain learn <bd_id> [disable]
295*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296static clib_error_t *
297bd_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400298 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299{
Dave Barach97d8dc22016-08-15 15:31:15 -0400300 bd_main_t *bdm = &bd_main;
301 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 u32 bd_index, bd_id;
303 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400304 uword *p;
305
306 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700307 {
308 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400309 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700310 goto done;
311 }
312
313 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
314
315 if (p == 0)
316 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400317
Ed Warnickecb9cada2015-12-08 15:45:58 -0700318 bd_index = p[0];
319
320 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400321 if (unformat (input, "disable"))
322 {
323 enable = 0;
324 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325
Dave Barach97d8dc22016-08-15 15:31:15 -0400326 /* set the bridge domain flag */
327 if (bd_set_flags (vm, bd_index, L2_LEARN, enable))
328 {
329 error =
330 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
331 goto done;
332 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333
Dave Barach97d8dc22016-08-15 15:31:15 -0400334done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 return error;
336}
337
Billy McFall22aa3e92016-09-09 08:46:40 -0400338/*?
339 * Layer 2 learning can be enabled and disabled on each
340 * interface and on each bridge-domain. Use this command to
341 * manage bridge-domains. It is enabled by default.
342 *
343 * @cliexpar
344 * Example of how to enable learning (where 200 is the bridge-domain-id):
345 * @cliexcmd{set bridge-domain learn 200}
346 * Example of how to disable learning (where 200 is the bridge-domain-id):
347 * @cliexcmd{set bridge-domain learn 200 disable}
348?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400349/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700350VLIB_CLI_COMMAND (bd_learn_cli, static) = {
351 .path = "set bridge-domain learn",
352 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
353 .function = bd_learn,
354};
Dave Barach97d8dc22016-08-15 15:31:15 -0400355/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barach97d8dc22016-08-15 15:31:15 -0400357/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400358 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400359 The CLI format is:
360 set bridge-domain forward <bd_index> [disable]
361*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400363bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364{
Dave Barach97d8dc22016-08-15 15:31:15 -0400365 bd_main_t *bdm = &bd_main;
366 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 u32 bd_index, bd_id;
368 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400369 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370
Dave Barach97d8dc22016-08-15 15:31:15 -0400371 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 {
373 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400374 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375 goto done;
376 }
377
378 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
379
380 if (p == 0)
381 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400382
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383 bd_index = p[0];
384
385 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400386 if (unformat (input, "disable"))
387 {
388 enable = 0;
389 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach97d8dc22016-08-15 15:31:15 -0400391 /* set the bridge domain flag */
392 if (bd_set_flags (vm, bd_index, L2_FWD, enable))
393 {
394 error =
395 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
396 goto done;
397 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Dave Barach97d8dc22016-08-15 15:31:15 -0400399done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 return error;
401}
402
Billy McFall22aa3e92016-09-09 08:46:40 -0400403
404/*?
405 * Layer 2 unicast forwarding can be enabled and disabled on each
406 * interface and on each bridge-domain. Use this command to
407 * manage bridge-domains. It is enabled by default.
408 *
409 * @cliexpar
410 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
411 * @cliexcmd{set bridge-domain forward 200}
412 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
413 * @cliexcmd{set bridge-domain forward 200 disable}
414?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400415/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700416VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
417 .path = "set bridge-domain forward",
418 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
419 .function = bd_fwd,
420};
Dave Barach97d8dc22016-08-15 15:31:15 -0400421/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Dave Barach97d8dc22016-08-15 15:31:15 -0400423/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400424 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400425 The CLI format is:
426 set bridge-domain flood <bd_index> [disable]
427*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428static clib_error_t *
429bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400430 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431{
Dave Barach97d8dc22016-08-15 15:31:15 -0400432 bd_main_t *bdm = &bd_main;
433 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700434 u32 bd_index, bd_id;
435 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400436 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439 {
440 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400441 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 goto done;
443 }
444
445 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
446
447 if (p == 0)
448 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400449
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 bd_index = p[0];
451
452 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400453 if (unformat (input, "disable"))
454 {
455 enable = 0;
456 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457
Dave Barach97d8dc22016-08-15 15:31:15 -0400458 /* set the bridge domain flag */
459 if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
460 {
461 error =
462 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
463 goto done;
464 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465
Dave Barach97d8dc22016-08-15 15:31:15 -0400466done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467 return error;
468}
469
Billy McFall22aa3e92016-09-09 08:46:40 -0400470/*?
471 * Layer 2 flooding can be enabled and disabled on each
472 * interface and on each bridge-domain. Use this command to
473 * manage bridge-domains. It is enabled by default.
474 *
475 * @cliexpar
476 * Example of how to enable flooding (where 200 is the bridge-domain-id):
477 * @cliexcmd{set bridge-domain flood 200}
478 * Example of how to disable flooding (where 200 is the bridge-domain-id):
479 * @cliexcmd{set bridge-domain flood 200 disable}
480?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400481/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482VLIB_CLI_COMMAND (bd_flood_cli, static) = {
483 .path = "set bridge-domain flood",
484 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
485 .function = bd_flood,
486};
Dave Barach97d8dc22016-08-15 15:31:15 -0400487/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
Dave Barach97d8dc22016-08-15 15:31:15 -0400489/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400490 Set bridge-domain unkown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400491 The CLI format is:
492 set bridge-domain uu-flood <bd_index> [disable]
493*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494static clib_error_t *
495bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400496 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700497{
Dave Barach97d8dc22016-08-15 15:31:15 -0400498 bd_main_t *bdm = &bd_main;
499 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 u32 bd_index, bd_id;
501 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400502 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503
Dave Barach97d8dc22016-08-15 15:31:15 -0400504 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 {
506 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400507 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508 goto done;
509 }
510
511 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
512
513 if (p == 0)
514 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400515
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 bd_index = p[0];
517
518 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400519 if (unformat (input, "disable"))
520 {
521 enable = 0;
522 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
Dave Barach97d8dc22016-08-15 15:31:15 -0400524 /* set the bridge domain flag */
525 if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
526 {
527 error =
528 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
529 goto done;
530 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531
Dave Barach97d8dc22016-08-15 15:31:15 -0400532done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 return error;
534}
535
Billy McFall22aa3e92016-09-09 08:46:40 -0400536/*?
537 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
538 * bridge-domain. It is enabled by default.
539 *
540 * @cliexpar
541 * Example of how to enable unknown-unicast flooding (where 200 is the
542 * bridge-domain-id):
543 * @cliexcmd{set bridge-domain uu-flood 200}
544 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
545 * @cliexcmd{set bridge-domain uu-flood 200 disable}
546?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400547/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
549 .path = "set bridge-domain uu-flood",
550 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
551 .function = bd_uu_flood,
552};
Dave Barach97d8dc22016-08-15 15:31:15 -0400553/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Dave Barach97d8dc22016-08-15 15:31:15 -0400555/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400556 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400557 The CLI format is:
558 set bridge-domain arp term <bridge-domain-id> [disable]
559*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560static clib_error_t *
561bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400562 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563{
Dave Barach97d8dc22016-08-15 15:31:15 -0400564 bd_main_t *bdm = &bd_main;
565 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 u32 bd_index, bd_id;
567 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400568 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
Dave Barach97d8dc22016-08-15 15:31:15 -0400570 if (!unformat (input, "%d", &bd_id))
571 {
572 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
573 format_unformat_error, input);
574 goto done;
575 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
577 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 if (p)
579 bd_index = *p;
580 else
581 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach97d8dc22016-08-15 15:31:15 -0400583 enable = 1;
584 if (unformat (input, "disable"))
585 enable = 0;
586
587 /* set the bridge domain flag */
588 if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
589 {
590 error =
591 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
592 goto done;
593 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
595done:
596 return error;
597}
598
Damjan Mariond171d482016-12-05 14:16:38 +0100599static clib_error_t *
600bd_mac_age (vlib_main_t * vm,
601 unformat_input_t * input, vlib_cli_command_t * cmd)
602{
603 bd_main_t *bdm = &bd_main;
604 clib_error_t *error = 0;
605 u32 bd_index, bd_id;
606 u32 age;
607 uword *p;
608
609 if (!unformat (input, "%d", &bd_id))
610 {
611 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
612 format_unformat_error, input);
613 goto done;
614 }
615
616 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
617
618 if (p == 0)
619 return clib_error_return (0, "No such bridge domain %d", bd_id);
620
621 bd_index = p[0];
622
623 if (!unformat (input, "%u", &age))
624 {
625 error =
626 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
627 format_unformat_error, input);
628 goto done;
629 }
630
631 /* set the bridge domain flag */
632 if (age > 255)
633 {
634 error =
635 clib_error_return (0, "mac aging time cannot be bigger than 255");
636 goto done;
637 }
638 bd_set_mac_age (vm, bd_index, (u8) age);
639
640done:
641 return error;
642}
643
644/*?
645 * Layer 2 mac aging can be enabled and disabled on each
646 * bridge-domain. Use this command to set or disable mac aging
647 * on specific bridge-domains. It is disabled by default.
648 *
649 * @cliexpar
650 * Example of how to set mac aging (where 200 is the bridge-domain-id and
651 * 5 is aging time in minutes):
652 * @cliexcmd{set bridge-domain mac-age 200 5}
653 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
654 * @cliexcmd{set bridge-domain flood 200 0}
655?*/
656/* *INDENT-OFF* */
657VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
658 .path = "set bridge-domain mac-age",
659 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
660 .function = bd_mac_age,
661};
662/* *INDENT-ON* */
663
Billy McFall22aa3e92016-09-09 08:46:40 -0400664/*?
665 * Modify whether or not an existing bridge-domain should terminate and respond
666 * to ARP Requests. ARP Termination is disabled by default.
667 *
668 * @cliexpar
669 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
670 * @cliexcmd{set bridge-domain arp term 200}
671 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
672 * @cliexcmd{set bridge-domain arp term 200 disable}
673?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400674/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
676 .path = "set bridge-domain arp term",
677 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
678 .function = bd_arp_term,
679};
Dave Barach97d8dc22016-08-15 15:31:15 -0400680/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700681
682
Dave Barach97d8dc22016-08-15 15:31:15 -0400683/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400684 * Add/delete IP address to MAC address mapping.
685 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400686 * The clib hash implementation stores uword entries in the hash table.
687 * The hash table mac_by_ip4 is keyed via IP4 address and store the
688 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400689 *
690 * @warning This only works for 64-bit processor with 8-byte uword;
691 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
692 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400693 */
694u32
695bd_add_del_ip_mac (u32 bd_index,
696 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697{
Dave Barach97d8dc22016-08-15 15:31:15 -0400698 l2input_main_t *l2im = &l2input_main;
699 l2_bridge_domain_t *bd_cfg = l2input_bd_config_from_index (l2im, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700700 u64 new_mac = *(u64 *) mac_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -0400701 u64 *old_mac;
702 u16 *mac16 = (u16 *) & new_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700703
Dave Barach97d8dc22016-08-15 15:31:15 -0400704 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
John Lo1edfba92016-08-27 01:11:57 -0400706 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
Dave Barach97d8dc22016-08-15 15:31:15 -0400707 if (is_ip6)
708 {
John Lo1edfba92016-08-27 01:11:57 -0400709 ip6_address_t *ip6_addr_key;
710 hash_pair_t *hp;
711 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
712 if (is_add)
713 {
714 if (old_mac == 0)
715 { /* new entry - allocate and craete ip6 address key */
716 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
717 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
718 }
719 else if (*old_mac == new_mac)
720 { /* same mac entry already exist for ip6 address */
721 return 0;
722 }
723 else
724 { /* updat mac for ip6 address */
725 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
726 ip6_addr_key = (ip6_address_t *) hp->key;
727 }
728 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
729 }
730 else
731 {
732 if (old_mac && (*old_mac == new_mac))
733 {
734 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
735 ip6_addr_key = (ip6_address_t *) hp->key;
736 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
737 clib_mem_free (ip6_addr_key);
738 }
739 else
740 return 1;
741 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400743 else
744 {
745 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
746 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
747 if (is_add)
748 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400749 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400750 return 0; /* mac entry already exist */
Dave Barach97d8dc22016-08-15 15:31:15 -0400751 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
752 }
753 else
754 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400755 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400756 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400757 else
John Lo1edfba92016-08-27 01:11:57 -0400758 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400759 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400760 }
John Lo1edfba92016-08-27 01:11:57 -0400761 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762}
763
Dave Barach97d8dc22016-08-15 15:31:15 -0400764/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400765 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -0400766 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -0400767 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -0400768*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700769static clib_error_t *
770bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400771 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700772{
Dave Barach97d8dc22016-08-15 15:31:15 -0400773 bd_main_t *bdm = &bd_main;
774 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700775 u32 bd_index, bd_id;
776 u8 is_add = 1;
777 u8 is_ip6 = 0;
778 u8 ip_addr[16];
779 u8 mac_addr[6];
Dave Barach97d8dc22016-08-15 15:31:15 -0400780 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781
Dave Barach97d8dc22016-08-15 15:31:15 -0400782 if (!unformat (input, "%d", &bd_id))
783 {
784 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
785 format_unformat_error, input);
786 goto done;
787 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700788
789 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
790
Dave Barach97d8dc22016-08-15 15:31:15 -0400791 if (p)
792 bd_index = *p;
793 else
794 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700795
Dave Barach97d8dc22016-08-15 15:31:15 -0400796 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
797 {
798 is_ip6 = 0;
799 }
800 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
801 {
802 is_ip6 = 1;
803 }
804 else
805 {
806 error = clib_error_return (0, "expecting IP address but got `%U'",
807 format_unformat_error, input);
808 goto done;
809 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700810
Dave Barach97d8dc22016-08-15 15:31:15 -0400811 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
812 {
813 error = clib_error_return (0, "expecting MAC address but got `%U'",
814 format_unformat_error, input);
815 goto done;
816 }
817
818 if (unformat (input, "del"))
819 {
820 is_add = 0;
821 }
822
823 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
824 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
825 {
826 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
827 is_add ? "add" : "del",
John Lo1edfba92016-08-27 01:11:57 -0400828 is_ip6 ?
829 format_ip4_address : format_ip6_address,
830 ip_addr, format_ethernet_address, mac_addr);
Dave Barach97d8dc22016-08-15 15:31:15 -0400831 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
833done:
834 return error;
835}
836
Billy McFall22aa3e92016-09-09 08:46:40 -0400837/*?
838 * Add an ARP entry to an existing bridge-domain.
839 *
840 * @cliexpar
841 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
842 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
843 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
844 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
845?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400846/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
848 .path = "set bridge-domain arp entry",
Billy McFall22aa3e92016-09-09 08:46:40 -0400849 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850 .function = bd_arp_entry,
851};
Dave Barach97d8dc22016-08-15 15:31:15 -0400852/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700853
Dave Barach97d8dc22016-08-15 15:31:15 -0400854u8 *
855format_vtr (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856{
Dave Barach97d8dc22016-08-15 15:31:15 -0400857 u32 vtr_op = va_arg (*args, u32);
858 u32 dot1q = va_arg (*args, u32);
859 u32 tag1 = va_arg (*args, u32);
860 u32 tag2 = va_arg (*args, u32);
861 switch (vtr_op)
862 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700863 case L2_VTR_DISABLED:
Dave Barach97d8dc22016-08-15 15:31:15 -0400864 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700865 case L2_VTR_PUSH_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400866 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700867 case L2_VTR_PUSH_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400868 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
869 tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700870 case L2_VTR_POP_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400871 return format (s, "pop-1");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 case L2_VTR_POP_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400873 return format (s, "pop-2");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874 case L2_VTR_TRANSLATE_1_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400875 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876 case L2_VTR_TRANSLATE_1_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400877 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
878 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 case L2_VTR_TRANSLATE_2_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400880 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881 case L2_VTR_TRANSLATE_2_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400882 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
883 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884 default:
Dave Barach97d8dc22016-08-15 15:31:15 -0400885 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700886 }
887}
888
Dave Barach97d8dc22016-08-15 15:31:15 -0400889/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400890 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -0400891 The CLI format is:
892 show bridge-domain [<bd_index>]
893*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400895bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896{
Dave Barach97d8dc22016-08-15 15:31:15 -0400897 vnet_main_t *vnm = vnet_get_main ();
898 bd_main_t *bdm = &bd_main;
899 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400901 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 u32 start, end;
903 u32 printed;
904 u32 detail = 0;
905 u32 intf = 0;
906 u32 arp = 0;
907 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400908 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909
910 start = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400911 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912
Dave Barach97d8dc22016-08-15 15:31:15 -0400913 if (unformat (input, "%d", &bd_id))
914 {
915 if (unformat (input, "detail"))
916 detail = 1;
917 else if (unformat (input, "det"))
918 detail = 1;
919 if (unformat (input, "int"))
920 intf = 1;
921 if (unformat (input, "arp"))
922 arp = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700923
Dave Barach97d8dc22016-08-15 15:31:15 -0400924 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
925 if (p)
926 bd_index = *p;
927 else
928 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929
Dave Barach97d8dc22016-08-15 15:31:15 -0400930 vec_validate (l2input_main.bd_configs, bd_index);
931 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
932 if (bd_is_valid (bd_config))
933 {
934 start = bd_index;
935 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400937 else
938 {
939 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
940 goto done;
941 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700943
Dave Barach97d8dc22016-08-15 15:31:15 -0400944 /* Show all bridge-domains that have been initialized */
945 printed = 0;
946 for (bd_index = start; bd_index < end; bd_index++)
947 {
948 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
949 if (bd_is_valid (bd_config))
950 {
951 if (!printed)
952 {
953 printed = 1;
954 vlib_cli_output (vm,
955 "%=5s %=7s %=10s %=10s %=10s %=10s %=10s %=14s",
956 "ID", "Index", "Learning", "U-Forwrd",
957 "UU-Flood", "Flooding", "ARP-Term",
958 "BVI-Intf");
959 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960
Dave Barach97d8dc22016-08-15 15:31:15 -0400961 vlib_cli_output (vm,
962 "%=5d %=7d %=10s %=10s %=10s %=10s %=10s %=14U",
963 bd_config->bd_id, bd_index,
964 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
965 "on" : "off",
966 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ? "on"
967 : "off",
968 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
969 "on" : "off",
970 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
971 "on" : "off",
972 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
973 "on" : "off", format_vnet_sw_if_index_name_with_NA,
974 vnm, bd_config->bvi_sw_if_index);
975
976 if (detail || intf)
977 {
978 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +0200979 int i;
980 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -0400981 {
Eyal Baric5b13602016-11-24 19:42:43 +0200982 l2_flood_member_t *member =
983 vec_elt_at_index (bd_config->members, i);
Dave Barach97d8dc22016-08-15 15:31:15 -0400984 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +0200985 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -0400986 {
Eyal Baric5b13602016-11-24 19:42:43 +0200987 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=9s%=30s",
Dave Barach97d8dc22016-08-15 15:31:15 -0400988 "Interface", "Index", "SHG", "BVI",
Eyal Baric5b13602016-11-24 19:42:43 +0200989 "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -0400990 }
991 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
992 &tag1, &tag2);
Eyal Baric5b13602016-11-24 19:42:43 +0200993 vlib_cli_output (vm, "%=30U%=7d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -0400994 format_vnet_sw_if_index_name, vnm,
995 member->sw_if_index, member->sw_if_index,
996 member->shg,
997 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +0200998 "-", i < bd_config->flood_count ? "*" : "-",
999 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001000 }
1001 }
1002
1003 if ((detail || arp) &&
1004 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1005 {
1006 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001007 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001008 u64 mac_addr;
1009 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001010 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001011
1012 /* *INDENT-OFF* */
1013 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1014 ({
John Lo1edfba92016-08-27 01:11:57 -04001015 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001016 format_ip4_address, &ip4_addr,
1017 format_ethernet_address, &mac_addr);
1018 }));
John Lo1edfba92016-08-27 01:11:57 -04001019
1020 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1021 ({
1022 vlib_cli_output (vm, "%=40U => %=20U",
1023 format_ip6_address, ip6_addr,
1024 format_ethernet_address, &mac_addr);
1025 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001026 /* *INDENT-ON* */
1027 }
1028 }
1029 }
1030
1031 if (!printed)
1032 {
1033 vlib_cli_output (vm, "no bridge-domains in use");
1034 }
1035
1036done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 return error;
1038}
1039
Billy McFall22aa3e92016-09-09 08:46:40 -04001040/*?
1041 * Show a summary of all the bridge-domain instances or detailed view of a
1042 * single bridge-domain. Bridge-domains are created by adding an interface
1043 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1044 *
1045 * @cliexpar
1046 * @parblock
1047 * Example of displaying all bridge-domains:
1048 * @cliexstart{show bridge-domain}
1049 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1050 * 0 0 off off off off off local0
1051 * 200 1 on on on on off N/A
1052 * @cliexend
1053 *
1054 * Example of displaying details of a single bridge-domains:
1055 * @cliexstart{show bridge-domain 200 detail}
1056 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1057 * 200 1 on on on on off N/A
1058 *
1059 * Interface Index SHG BVI VLAN-Tag-Rewrite
1060 * GigabitEthernet0/8/0.200 3 0 - none
1061 * GigabitEthernet0/9/0.200 4 0 - none
1062 * @cliexend
1063 * @endparblock
1064?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001065/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001066VLIB_CLI_COMMAND (bd_show_cli, static) = {
1067 .path = "show bridge-domain",
1068 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1069 .function = bd_show,
1070};
Dave Barach97d8dc22016-08-15 15:31:15 -04001071/* *INDENT-ON* */
1072
1073/*
1074 * fd.io coding-style-patch-verification: ON
1075 *
1076 * Local Variables:
1077 * eval: (c-set-style "gnu")
1078 * End:
1079 */