blob: 4ebbb5471341a06cd6971c7a66415383116ddeee [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;
55 bd_config->feature_bitmap = ~L2INPUT_FEAT_ARP_TERM;
56 bd_config->bvi_sw_if_index = ~0;
57 bd_config->members = 0;
58 bd_config->flood_count = 0;
59 bd_config->tun_master_count = 0;
60 bd_config->tun_normal_count = 0;
61 bd_config->mac_by_ip4 = 0;
62 bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
63 sizeof (uword));
Ed Warnickecb9cada2015-12-08 15:45:58 -070064}
65
Dave Barach97d8dc22016-08-15 15:31:15 -040066u32
Eyal Barib1352ed2017-04-07 23:14:17 +030067bd_find_index (bd_main_t * bdm, u32 bd_id)
Ed Warnickecb9cada2015-12-08 15:45:58 -070068{
Eyal Barib1352ed2017-04-07 23:14:17 +030069 u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
70 if (!p)
71 return ~0;
72 return p[0];
73}
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Eyal Barib1352ed2017-04-07 23:14:17 +030075u32
76bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
77{
78 ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
79 u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
Dave Barach97d8dc22016-08-15 15:31:15 -040080
Eyal Barib1352ed2017-04-07 23:14:17 +030081 /* mark this index taken */
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
83
84 hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
85
86 vec_validate (l2input_main.bd_configs, rv);
87 l2input_main.bd_configs[rv].bd_id = bd_id;
88
89 return rv;
90}
91
Eyal Barib1352ed2017-04-07 23:14:17 +030092static int
93bd_delete (bd_main_t * bdm, u32 bd_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
John Lod77630a2017-04-28 00:33:36 -040095 l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
96 u32 bd_id = bd->bd_id;
Eyal Barib1352ed2017-04-07 23:14:17 +030097 hash_unset (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -040098
99 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
John Lod77630a2017-04-28 00:33:36 -0400102 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
103 bd->bd_id = ~0;
104 bd->feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
John Lod77630a2017-04-28 00:33:36 -0400106 /* free memory used by BD and flush non-static MACs in BD */
107 vec_free (bd->members);
108 hash_free (bd->mac_by_ip4);
109 hash_free (bd->mac_by_ip6);
John Loda1f2c72017-03-24 20:11:15 -0400110 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
111
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 return 0;
113}
114
Eyal Baric5b13602016-11-24 19:42:43 +0200115static void
116update_flood_count (l2_bridge_domain_t * bd_config)
117{
118 bd_config->flood_count = vec_len (bd_config->members) -
119 (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
120}
121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122void
Dave Barach97d8dc22016-08-15 15:31:15 -0400123bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124{
Eyal Baric5b13602016-11-24 19:42:43 +0200125 u32 ix;
126 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
127 (vnet_get_main (), member->sw_if_index);
128
Dave Barach97d8dc22016-08-15 15:31:15 -0400129 /*
130 * Add one element to the vector
Eyal Baric5b13602016-11-24 19:42:43 +0200131 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
Dave Barach97d8dc22016-08-15 15:31:15 -0400132 * When flooding, the bvi interface (if present) must be the last member
133 * processed due to how BVI processing can change the packet. To enable
134 * this order, we make the bvi interface the first in the vector and
135 * flooding walks the vector in reverse.
136 */
Eyal Baric5b13602016-11-24 19:42:43 +0200137 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400138 {
Eyal Baric5b13602016-11-24 19:42:43 +0200139 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
140 bd_config->tun_master_count++;
141 /* Fall through */
142 default:
143 /* Fall through */
144 case VNET_FLOOD_CLASS_NORMAL:
145 ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
146 vec_len (bd_config->members) - bd_config->tun_normal_count;
147 break;
148 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
149 ix = vec_len (bd_config->members);
150 bd_config->tun_normal_count++;
151 break;
152 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
Eyal Baric5b13602016-11-24 19:42:43 +0200154 vec_insert_elts (bd_config->members, member, 1, ix);
155 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156}
157
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158#define BD_REMOVE_ERROR_OK 0
159#define BD_REMOVE_ERROR_NOT_FOUND 1
160
161u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400162bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163{
164 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400165
166 /* Find and delete the member */
167 vec_foreach_index (ix, bd_config->members)
168 {
Eyal Baric5b13602016-11-24 19:42:43 +0200169 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
170 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400171 {
Eyal Baric5b13602016-11-24 19:42:43 +0200172 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
173 (vnet_get_main (), sw_if_index);
174
175 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
176 {
177 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
178 bd_config->tun_master_count--;
179 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
180 bd_config->tun_normal_count--;
181 }
Eyal Bari25b36672017-03-02 10:43:19 +0200182 vec_delete (bd_config->members, 1, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200183 update_flood_count (bd_config);
184
Dave Barach97d8dc22016-08-15 15:31:15 -0400185 return BD_REMOVE_ERROR_OK;
186 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187 }
188
189 return BD_REMOVE_ERROR_NOT_FOUND;
190}
191
192
Dave Barach97d8dc22016-08-15 15:31:15 -0400193clib_error_t *
194l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700195{
196 bd_main_t *bdm = &bd_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400197 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
198 /*
199 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
200 * to packet drop only. Thus, packets received from any L2 interface with
201 * uninitialized bd_index of 0 can be dropped safely.
202 */
Eyal Barib1352ed2017-04-07 23:14:17 +0300203 u32 bd_index = bd_add_bd_index (bdm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400205 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Choonho Son05480792017-03-29 20:07:45 +0900206
207 bdm->vlib_main = vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208 return 0;
209}
210
211VLIB_INIT_FUNCTION (l2bd_init);
212
213
Dave Barach97d8dc22016-08-15 15:31:15 -0400214/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400215 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400216 Return 0 if ok, non-zero if for an error.
217*/
218u32
219bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
220{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Dave Barach97d8dc22016-08-15 15:31:15 -0400222 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 u32 feature_bitmap = 0;
224
225 vec_validate (l2input_main.bd_configs, bd_index);
Dave Barach97d8dc22016-08-15 15:31:15 -0400226 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
228 bd_validate (bd_config);
229
Dave Barach97d8dc22016-08-15 15:31:15 -0400230 if (flags & L2_LEARN)
231 {
232 feature_bitmap |= L2INPUT_FEAT_LEARN;
233 }
234 if (flags & L2_FWD)
235 {
236 feature_bitmap |= L2INPUT_FEAT_FWD;
237 }
238 if (flags & L2_FLOOD)
239 {
240 feature_bitmap |= L2INPUT_FEAT_FLOOD;
241 }
242 if (flags & L2_UU_FLOOD)
243 {
244 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
245 }
246 if (flags & L2_ARP_TERM)
247 {
248 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
249 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700250
Dave Barach97d8dc22016-08-15 15:31:15 -0400251 if (enable)
252 {
253 bd_config->feature_bitmap |= feature_bitmap;
254 }
255 else
256 {
257 bd_config->feature_bitmap &= ~feature_bitmap;
258 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
260 return 0;
261}
262
Dave Barach97d8dc22016-08-15 15:31:15 -0400263/**
Damjan Mariond171d482016-12-05 14:16:38 +0100264 Set the mac age for the bridge domain.
265*/
266void
267bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
268{
269 l2_bridge_domain_t *bd_config;
270 int enable = 0;
271
272 vec_validate (l2input_main.bd_configs, bd_index);
273 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
274 bd_config->mac_age = age;
275
276 /* check if there is at least one bd with mac aging enabled */
277 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Barifead6702017-04-04 04:46:32 +0300278 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
Damjan Mariond171d482016-12-05 14:16:38 +0100279
280 vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
281 enable ? L2_MAC_AGE_PROCESS_EVENT_START :
282 L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
283}
284
285/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400286 Set bridge-domain learn enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400287 The CLI format is:
288 set bridge-domain learn <bd_id> [disable]
289*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290static clib_error_t *
291bd_learn (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400292 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293{
Dave Barach97d8dc22016-08-15 15:31:15 -0400294 bd_main_t *bdm = &bd_main;
295 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 u32 bd_index, bd_id;
297 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400298 uword *p;
299
300 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301 {
302 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400303 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 goto done;
305 }
306
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500307 if (bd_id == 0)
308 return clib_error_return (0,
309 "No operations on the default bridge domain are supported");
310
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
312
313 if (p == 0)
314 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400315
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 bd_index = p[0];
317
318 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400319 if (unformat (input, "disable"))
320 {
321 enable = 0;
322 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barach97d8dc22016-08-15 15:31:15 -0400324 /* set the bridge domain flag */
325 if (bd_set_flags (vm, bd_index, L2_LEARN, enable))
326 {
327 error =
328 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
329 goto done;
330 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331
Dave Barach97d8dc22016-08-15 15:31:15 -0400332done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 return error;
334}
335
Billy McFall22aa3e92016-09-09 08:46:40 -0400336/*?
337 * Layer 2 learning can be enabled and disabled on each
338 * interface and on each bridge-domain. Use this command to
339 * manage bridge-domains. It is enabled by default.
340 *
341 * @cliexpar
342 * Example of how to enable learning (where 200 is the bridge-domain-id):
343 * @cliexcmd{set bridge-domain learn 200}
344 * Example of how to disable learning (where 200 is the bridge-domain-id):
345 * @cliexcmd{set bridge-domain learn 200 disable}
346?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400347/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700348VLIB_CLI_COMMAND (bd_learn_cli, static) = {
349 .path = "set bridge-domain learn",
350 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
351 .function = bd_learn,
352};
Dave Barach97d8dc22016-08-15 15:31:15 -0400353/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barach97d8dc22016-08-15 15:31:15 -0400355/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400356 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400357 The CLI format is:
358 set bridge-domain forward <bd_index> [disable]
359*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400361bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362{
Dave Barach97d8dc22016-08-15 15:31:15 -0400363 bd_main_t *bdm = &bd_main;
364 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 u32 bd_index, bd_id;
366 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400367 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Dave Barach97d8dc22016-08-15 15:31:15 -0400369 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 {
371 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400372 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 goto done;
374 }
375
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500376 if (bd_id == 0)
377 return clib_error_return (0,
378 "No operations on the default bridge domain are supported");
379
Ed Warnickecb9cada2015-12-08 15:45:58 -0700380 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
381
382 if (p == 0)
383 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400384
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 bd_index = p[0];
386
387 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400388 if (unformat (input, "disable"))
389 {
390 enable = 0;
391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392
Dave Barach97d8dc22016-08-15 15:31:15 -0400393 /* set the bridge domain flag */
394 if (bd_set_flags (vm, bd_index, L2_FWD, enable))
395 {
396 error =
397 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
398 goto done;
399 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400
Dave Barach97d8dc22016-08-15 15:31:15 -0400401done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 return error;
403}
404
Billy McFall22aa3e92016-09-09 08:46:40 -0400405
406/*?
407 * Layer 2 unicast forwarding can be enabled and disabled on each
408 * interface and on each bridge-domain. Use this command to
409 * manage bridge-domains. It is enabled by default.
410 *
411 * @cliexpar
412 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
413 * @cliexcmd{set bridge-domain forward 200}
414 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
415 * @cliexcmd{set bridge-domain forward 200 disable}
416?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400417/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
419 .path = "set bridge-domain forward",
420 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
421 .function = bd_fwd,
422};
Dave Barach97d8dc22016-08-15 15:31:15 -0400423/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
Dave Barach97d8dc22016-08-15 15:31:15 -0400425/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400426 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400427 The CLI format is:
428 set bridge-domain flood <bd_index> [disable]
429*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430static clib_error_t *
431bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400432 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433{
Dave Barach97d8dc22016-08-15 15:31:15 -0400434 bd_main_t *bdm = &bd_main;
435 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 u32 bd_index, bd_id;
437 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Dave Barach97d8dc22016-08-15 15:31:15 -0400440 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 {
442 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400443 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700444 goto done;
445 }
446
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500447 if (bd_id == 0)
448 return clib_error_return (0,
449 "No operations on the default bridge domain are supported");
450
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
452
453 if (p == 0)
454 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400455
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 bd_index = p[0];
457
458 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400459 if (unformat (input, "disable"))
460 {
461 enable = 0;
462 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700463
Dave Barach97d8dc22016-08-15 15:31:15 -0400464 /* set the bridge domain flag */
465 if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
466 {
467 error =
468 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
469 goto done;
470 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barach97d8dc22016-08-15 15:31:15 -0400472done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700473 return error;
474}
475
Billy McFall22aa3e92016-09-09 08:46:40 -0400476/*?
477 * Layer 2 flooding can be enabled and disabled on each
478 * interface and on each bridge-domain. Use this command to
479 * manage bridge-domains. It is enabled by default.
480 *
481 * @cliexpar
482 * Example of how to enable flooding (where 200 is the bridge-domain-id):
483 * @cliexcmd{set bridge-domain flood 200}
484 * Example of how to disable flooding (where 200 is the bridge-domain-id):
485 * @cliexcmd{set bridge-domain flood 200 disable}
486?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400487/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488VLIB_CLI_COMMAND (bd_flood_cli, static) = {
489 .path = "set bridge-domain flood",
490 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
491 .function = bd_flood,
492};
Dave Barach97d8dc22016-08-15 15:31:15 -0400493/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Dave Barach97d8dc22016-08-15 15:31:15 -0400495/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400496 Set bridge-domain unkown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400497 The CLI format is:
498 set bridge-domain uu-flood <bd_index> [disable]
499*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500static clib_error_t *
501bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400502 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503{
Dave Barach97d8dc22016-08-15 15:31:15 -0400504 bd_main_t *bdm = &bd_main;
505 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 u32 bd_index, bd_id;
507 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400508 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
Dave Barach97d8dc22016-08-15 15:31:15 -0400510 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511 {
512 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400513 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 goto done;
515 }
516
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500517 if (bd_id == 0)
518 return clib_error_return (0,
519 "No operations on the default bridge domain are supported");
520
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
522
523 if (p == 0)
524 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400525
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 bd_index = p[0];
527
528 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400529 if (unformat (input, "disable"))
530 {
531 enable = 0;
532 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533
Dave Barach97d8dc22016-08-15 15:31:15 -0400534 /* set the bridge domain flag */
535 if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
536 {
537 error =
538 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
539 goto done;
540 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541
Dave Barach97d8dc22016-08-15 15:31:15 -0400542done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 return error;
544}
545
Billy McFall22aa3e92016-09-09 08:46:40 -0400546/*?
547 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
548 * bridge-domain. It is enabled by default.
549 *
550 * @cliexpar
551 * Example of how to enable unknown-unicast flooding (where 200 is the
552 * bridge-domain-id):
553 * @cliexcmd{set bridge-domain uu-flood 200}
554 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
555 * @cliexcmd{set bridge-domain uu-flood 200 disable}
556?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400557/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700558VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
559 .path = "set bridge-domain uu-flood",
560 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
561 .function = bd_uu_flood,
562};
Dave Barach97d8dc22016-08-15 15:31:15 -0400563/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
Dave Barach97d8dc22016-08-15 15:31:15 -0400565/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400566 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400567 The CLI format is:
568 set bridge-domain arp term <bridge-domain-id> [disable]
569*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570static clib_error_t *
571bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400572 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573{
Dave Barach97d8dc22016-08-15 15:31:15 -0400574 bd_main_t *bdm = &bd_main;
575 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576 u32 bd_index, bd_id;
577 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Dave Barach97d8dc22016-08-15 15:31:15 -0400580 if (!unformat (input, "%d", &bd_id))
581 {
582 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
583 format_unformat_error, input);
584 goto done;
585 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500587 if (bd_id == 0)
588 return clib_error_return (0,
589 "No operations on the default bridge domain are supported");
590
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400592 if (p)
593 bd_index = *p;
594 else
595 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700596
Dave Barach97d8dc22016-08-15 15:31:15 -0400597 enable = 1;
598 if (unformat (input, "disable"))
599 enable = 0;
600
601 /* set the bridge domain flag */
602 if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
603 {
604 error =
605 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
606 goto done;
607 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
609done:
610 return error;
611}
612
Damjan Mariond171d482016-12-05 14:16:38 +0100613static clib_error_t *
614bd_mac_age (vlib_main_t * vm,
615 unformat_input_t * input, vlib_cli_command_t * cmd)
616{
617 bd_main_t *bdm = &bd_main;
618 clib_error_t *error = 0;
619 u32 bd_index, bd_id;
620 u32 age;
621 uword *p;
622
623 if (!unformat (input, "%d", &bd_id))
624 {
625 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
626 format_unformat_error, input);
627 goto done;
628 }
629
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500630 if (bd_id == 0)
631 return clib_error_return (0,
632 "No operations on the default bridge domain are supported");
633
Damjan Mariond171d482016-12-05 14:16:38 +0100634 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
635
636 if (p == 0)
637 return clib_error_return (0, "No such bridge domain %d", bd_id);
638
639 bd_index = p[0];
640
641 if (!unformat (input, "%u", &age))
642 {
643 error =
644 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
645 format_unformat_error, input);
646 goto done;
647 }
648
649 /* set the bridge domain flag */
650 if (age > 255)
651 {
652 error =
653 clib_error_return (0, "mac aging time cannot be bigger than 255");
654 goto done;
655 }
656 bd_set_mac_age (vm, bd_index, (u8) age);
657
658done:
659 return error;
660}
661
662/*?
663 * Layer 2 mac aging can be enabled and disabled on each
664 * bridge-domain. Use this command to set or disable mac aging
665 * on specific bridge-domains. It is disabled by default.
666 *
667 * @cliexpar
668 * Example of how to set mac aging (where 200 is the bridge-domain-id and
669 * 5 is aging time in minutes):
670 * @cliexcmd{set bridge-domain mac-age 200 5}
671 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
672 * @cliexcmd{set bridge-domain flood 200 0}
673?*/
674/* *INDENT-OFF* */
675VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
676 .path = "set bridge-domain mac-age",
677 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
678 .function = bd_mac_age,
679};
680/* *INDENT-ON* */
681
Billy McFall22aa3e92016-09-09 08:46:40 -0400682/*?
683 * Modify whether or not an existing bridge-domain should terminate and respond
684 * to ARP Requests. ARP Termination is disabled by default.
685 *
686 * @cliexpar
687 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
688 * @cliexcmd{set bridge-domain arp term 200}
689 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
690 * @cliexcmd{set bridge-domain arp term 200 disable}
691?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400692/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
694 .path = "set bridge-domain arp term",
695 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
696 .function = bd_arp_term,
697};
Dave Barach97d8dc22016-08-15 15:31:15 -0400698/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699
700
Dave Barach97d8dc22016-08-15 15:31:15 -0400701/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400702 * Add/delete IP address to MAC address mapping.
703 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400704 * The clib hash implementation stores uword entries in the hash table.
705 * The hash table mac_by_ip4 is keyed via IP4 address and store the
706 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400707 *
708 * @warning This only works for 64-bit processor with 8-byte uword;
709 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
710 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400711 */
712u32
713bd_add_del_ip_mac (u32 bd_index,
714 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715{
Dave Barach97d8dc22016-08-15 15:31:15 -0400716 l2input_main_t *l2im = &l2input_main;
717 l2_bridge_domain_t *bd_cfg = l2input_bd_config_from_index (l2im, bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 u64 new_mac = *(u64 *) mac_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -0400719 u64 *old_mac;
720 u16 *mac16 = (u16 *) & new_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721
Dave Barach97d8dc22016-08-15 15:31:15 -0400722 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723
John Lo1edfba92016-08-27 01:11:57 -0400724 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
Dave Barach97d8dc22016-08-15 15:31:15 -0400725 if (is_ip6)
726 {
John Lo1edfba92016-08-27 01:11:57 -0400727 ip6_address_t *ip6_addr_key;
728 hash_pair_t *hp;
729 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
730 if (is_add)
731 {
732 if (old_mac == 0)
733 { /* new entry - allocate and craete ip6 address key */
734 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
735 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
736 }
737 else if (*old_mac == new_mac)
738 { /* same mac entry already exist for ip6 address */
739 return 0;
740 }
741 else
742 { /* updat mac for ip6 address */
743 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
744 ip6_addr_key = (ip6_address_t *) hp->key;
745 }
746 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
747 }
748 else
749 {
750 if (old_mac && (*old_mac == new_mac))
751 {
752 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
753 ip6_addr_key = (ip6_address_t *) hp->key;
754 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
755 clib_mem_free (ip6_addr_key);
756 }
757 else
758 return 1;
759 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700760 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400761 else
762 {
763 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
764 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
765 if (is_add)
766 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400767 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400768 return 0; /* mac entry already exist */
Dave Barach97d8dc22016-08-15 15:31:15 -0400769 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
770 }
771 else
772 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400773 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400774 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400775 else
John Lo1edfba92016-08-27 01:11:57 -0400776 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400777 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400778 }
John Lo1edfba92016-08-27 01:11:57 -0400779 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700780}
781
Dave Barach97d8dc22016-08-15 15:31:15 -0400782/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400783 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -0400784 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -0400785 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -0400786*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787static clib_error_t *
788bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400789 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700790{
Dave Barach97d8dc22016-08-15 15:31:15 -0400791 bd_main_t *bdm = &bd_main;
792 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 u32 bd_index, bd_id;
794 u8 is_add = 1;
795 u8 is_ip6 = 0;
796 u8 ip_addr[16];
797 u8 mac_addr[6];
Dave Barach97d8dc22016-08-15 15:31:15 -0400798 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799
Dave Barach97d8dc22016-08-15 15:31:15 -0400800 if (!unformat (input, "%d", &bd_id))
801 {
802 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
803 format_unformat_error, input);
804 goto done;
805 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700806
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500807 if (bd_id == 0)
808 return clib_error_return (0,
809 "No operations on the default bridge domain are supported");
810
Ed Warnickecb9cada2015-12-08 15:45:58 -0700811 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
812
Dave Barach97d8dc22016-08-15 15:31:15 -0400813 if (p)
814 bd_index = *p;
815 else
816 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817
Dave Barach97d8dc22016-08-15 15:31:15 -0400818 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
819 {
820 is_ip6 = 0;
821 }
822 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
823 {
824 is_ip6 = 1;
825 }
826 else
827 {
828 error = clib_error_return (0, "expecting IP address but got `%U'",
829 format_unformat_error, input);
830 goto done;
831 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700832
Dave Barach97d8dc22016-08-15 15:31:15 -0400833 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
834 {
835 error = clib_error_return (0, "expecting MAC address but got `%U'",
836 format_unformat_error, input);
837 goto done;
838 }
839
840 if (unformat (input, "del"))
841 {
842 is_add = 0;
843 }
844
845 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
846 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
847 {
848 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
849 is_add ? "add" : "del",
John Lo1edfba92016-08-27 01:11:57 -0400850 is_ip6 ?
851 format_ip4_address : format_ip6_address,
852 ip_addr, format_ethernet_address, mac_addr);
Dave Barach97d8dc22016-08-15 15:31:15 -0400853 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854
855done:
856 return error;
857}
858
Billy McFall22aa3e92016-09-09 08:46:40 -0400859/*?
860 * Add an ARP entry to an existing bridge-domain.
861 *
862 * @cliexpar
863 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
864 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
865 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
866 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
867?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400868/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700869VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
870 .path = "set bridge-domain arp entry",
Billy McFall22aa3e92016-09-09 08:46:40 -0400871 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700872 .function = bd_arp_entry,
873};
Dave Barach97d8dc22016-08-15 15:31:15 -0400874/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875
Dave Barach97d8dc22016-08-15 15:31:15 -0400876u8 *
877format_vtr (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878{
Dave Barach97d8dc22016-08-15 15:31:15 -0400879 u32 vtr_op = va_arg (*args, u32);
880 u32 dot1q = va_arg (*args, u32);
881 u32 tag1 = va_arg (*args, u32);
882 u32 tag2 = va_arg (*args, u32);
883 switch (vtr_op)
884 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 case L2_VTR_DISABLED:
Dave Barach97d8dc22016-08-15 15:31:15 -0400886 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 case L2_VTR_PUSH_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400888 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700889 case L2_VTR_PUSH_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400890 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
891 tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892 case L2_VTR_POP_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400893 return format (s, "pop-1");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700894 case L2_VTR_POP_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400895 return format (s, "pop-2");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700896 case L2_VTR_TRANSLATE_1_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400897 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 case L2_VTR_TRANSLATE_1_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400899 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
900 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700901 case L2_VTR_TRANSLATE_2_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400902 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 case L2_VTR_TRANSLATE_2_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400904 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
905 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700906 default:
Dave Barach97d8dc22016-08-15 15:31:15 -0400907 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 }
909}
910
Dave Barach97d8dc22016-08-15 15:31:15 -0400911/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400912 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -0400913 The CLI format is:
914 show bridge-domain [<bd_index>]
915*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700916static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400917bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918{
Dave Barach97d8dc22016-08-15 15:31:15 -0400919 vnet_main_t *vnm = vnet_get_main ();
920 bd_main_t *bdm = &bd_main;
921 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400923 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924 u32 start, end;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700925 u32 detail = 0;
926 u32 intf = 0;
927 u32 arp = 0;
928 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400929 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500931 start = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400932 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933
Dave Barach97d8dc22016-08-15 15:31:15 -0400934 if (unformat (input, "%d", &bd_id))
935 {
936 if (unformat (input, "detail"))
937 detail = 1;
938 else if (unformat (input, "det"))
939 detail = 1;
940 if (unformat (input, "int"))
941 intf = 1;
942 if (unformat (input, "arp"))
943 arp = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700944
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500945 if (bd_id == 0)
946 return clib_error_return (0,
947 "No operations on the default bridge domain are supported");
948
Dave Barach97d8dc22016-08-15 15:31:15 -0400949 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
950 if (p)
951 bd_index = *p;
952 else
953 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954
Dave Barach97d8dc22016-08-15 15:31:15 -0400955 vec_validate (l2input_main.bd_configs, bd_index);
956 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
957 if (bd_is_valid (bd_config))
958 {
959 start = bd_index;
960 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700961 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400962 else
963 {
964 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
965 goto done;
966 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700968
Dave Barach97d8dc22016-08-15 15:31:15 -0400969 /* Show all bridge-domains that have been initialized */
John Loda1f2c72017-03-24 20:11:15 -0400970 u32 printed = 0;
971 u8 *as = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400972 for (bd_index = start; bd_index < end; bd_index++)
973 {
974 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
975 if (bd_is_valid (bd_config))
976 {
977 if (!printed)
978 {
979 printed = 1;
980 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400981 "%=5s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
982 "ID", "Index", "BSN", "Age(min)", "Learning",
983 "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
Dave Barach97d8dc22016-08-15 15:31:15 -0400984 "BVI-Intf");
985 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700986
John Loda1f2c72017-03-24 20:11:15 -0400987 if (bd_config->mac_age)
988 as = format (as, "%d", bd_config->mac_age);
989 else
990 as = format (as, "off");
Dave Barach97d8dc22016-08-15 15:31:15 -0400991 vlib_cli_output (vm,
John Loda1f2c72017-03-24 20:11:15 -0400992 "%=5d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
993 bd_config->bd_id, bd_index, bd_config->seq_num, as,
Dave Barach97d8dc22016-08-15 15:31:15 -0400994 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
995 "on" : "off",
John Loda1f2c72017-03-24 20:11:15 -0400996 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
997 "on" : "off",
Dave Barach97d8dc22016-08-15 15:31:15 -0400998 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
999 "on" : "off",
1000 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1001 "on" : "off",
1002 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
John Loda1f2c72017-03-24 20:11:15 -04001003 "on" : "off",
1004 format_vnet_sw_if_index_name_with_NA,
Dave Barach97d8dc22016-08-15 15:31:15 -04001005 vnm, bd_config->bvi_sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -04001006 vec_reset_length (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001007
1008 if (detail || intf)
1009 {
1010 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +02001011 int i;
1012 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -04001013 {
Eyal Baric5b13602016-11-24 19:42:43 +02001014 l2_flood_member_t *member =
1015 vec_elt_at_index (bd_config->members, i);
John Loda1f2c72017-03-24 20:11:15 -04001016 l2_input_config_t *int_config =
1017 l2input_intf_config (member->sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -04001018 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +02001019 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -04001020 {
John Loda1f2c72017-03-24 20:11:15 -04001021 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1022 "Interface", "If-idx", "ISN", "SHG",
1023 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -04001024 }
1025 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1026 &tag1, &tag2);
John Loda1f2c72017-03-24 20:11:15 -04001027 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001028 format_vnet_sw_if_index_name, vnm,
1029 member->sw_if_index, member->sw_if_index,
John Loda1f2c72017-03-24 20:11:15 -04001030 int_config->seq_num, member->shg,
Dave Barach97d8dc22016-08-15 15:31:15 -04001031 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +02001032 "-", i < bd_config->flood_count ? "*" : "-",
1033 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001034 }
1035 }
1036
1037 if ((detail || arp) &&
1038 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1039 {
1040 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001041 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001042 u64 mac_addr;
1043 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001044 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001045
1046 /* *INDENT-OFF* */
1047 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1048 ({
John Lo1edfba92016-08-27 01:11:57 -04001049 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001050 format_ip4_address, &ip4_addr,
1051 format_ethernet_address, &mac_addr);
1052 }));
John Lo1edfba92016-08-27 01:11:57 -04001053
1054 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1055 ({
1056 vlib_cli_output (vm, "%=40U => %=20U",
1057 format_ip6_address, ip6_addr,
1058 format_ethernet_address, &mac_addr);
1059 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001060 /* *INDENT-ON* */
1061 }
1062 }
1063 }
John Loda1f2c72017-03-24 20:11:15 -04001064 vec_free (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001065
1066 if (!printed)
1067 {
1068 vlib_cli_output (vm, "no bridge-domains in use");
1069 }
1070
1071done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 return error;
1073}
1074
Billy McFall22aa3e92016-09-09 08:46:40 -04001075/*?
1076 * Show a summary of all the bridge-domain instances or detailed view of a
1077 * single bridge-domain. Bridge-domains are created by adding an interface
1078 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1079 *
1080 * @cliexpar
1081 * @parblock
1082 * Example of displaying all bridge-domains:
1083 * @cliexstart{show bridge-domain}
1084 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1085 * 0 0 off off off off off local0
1086 * 200 1 on on on on off N/A
1087 * @cliexend
1088 *
1089 * Example of displaying details of a single bridge-domains:
1090 * @cliexstart{show bridge-domain 200 detail}
1091 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1092 * 200 1 on on on on off N/A
1093 *
1094 * Interface Index SHG BVI VLAN-Tag-Rewrite
1095 * GigabitEthernet0/8/0.200 3 0 - none
1096 * GigabitEthernet0/9/0.200 4 0 - none
1097 * @cliexend
1098 * @endparblock
1099?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001100/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101VLIB_CLI_COMMAND (bd_show_cli, static) = {
1102 .path = "show bridge-domain",
1103 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1104 .function = bd_show,
1105};
Dave Barach97d8dc22016-08-15 15:31:15 -04001106/* *INDENT-ON* */
1107
Choonho Son05480792017-03-29 20:07:45 +09001108int
1109bd_add_del (l2_bridge_domain_add_del_args_t * a)
1110{
1111 bd_main_t *bdm = &bd_main;
1112 vlib_main_t *vm = bdm->vlib_main;
Choonho Son05480792017-03-29 20:07:45 +09001113 int rv = 0;
1114
Eyal Barib1352ed2017-04-07 23:14:17 +03001115 u32 bd_index = bd_find_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001116 if (a->is_add)
1117 {
Eyal Barib1352ed2017-04-07 23:14:17 +03001118 if (bd_index != ~0)
1119 return VNET_API_ERROR_BD_ALREADY_EXISTS;
1120 bd_index = bd_add_bd_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001121
Eyal Barib1352ed2017-04-07 23:14:17 +03001122 u32 enable_flags = 0, disable_flags = 0;
Choonho Son05480792017-03-29 20:07:45 +09001123 if (a->flood)
1124 enable_flags |= L2_FLOOD;
1125 else
1126 disable_flags |= L2_FLOOD;
1127
1128 if (a->uu_flood)
1129 enable_flags |= L2_UU_FLOOD;
1130 else
1131 disable_flags |= L2_UU_FLOOD;
1132
1133 if (a->forward)
1134 enable_flags |= L2_FWD;
1135 else
1136 disable_flags |= L2_FWD;
1137
1138 if (a->learn)
1139 enable_flags |= L2_LEARN;
1140 else
1141 disable_flags |= L2_LEARN;
1142
1143 if (a->arp_term)
1144 enable_flags |= L2_ARP_TERM;
1145 else
1146 disable_flags |= L2_ARP_TERM;
1147
1148 if (enable_flags)
1149 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1150
1151 if (disable_flags)
1152 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1153
1154 bd_set_mac_age (vm, bd_index, a->mac_age);
1155 }
1156 else
Eyal Barib1352ed2017-04-07 23:14:17 +03001157 {
1158 if (bd_index == ~0)
1159 return VNET_API_ERROR_NO_SUCH_ENTRY;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001160 if (bd_index == 0)
1161 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
Eyal Barib1352ed2017-04-07 23:14:17 +03001162 if (vec_len (l2input_main.bd_configs[bd_index].members))
1163 return VNET_API_ERROR_BD_IN_USE;
1164 rv = bd_delete (bdm, bd_index);
1165 }
Choonho Son05480792017-03-29 20:07:45 +09001166
1167 return rv;
1168}
1169
1170/**
1171 Create or delete bridge-domain.
Choonho Son5ee51f82017-04-05 19:09:52 +09001172 The CLI format:
Choonho Son05480792017-03-29 20:07:45 +09001173 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>]
1174 [flood <0|1>] [arp-term <0|1>] [mac-age <nn>] [del]
1175*/
1176
1177static clib_error_t *
1178bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1179 vlib_cli_command_t * cmd)
1180{
1181 unformat_input_t _line_input, *line_input = &_line_input;
1182 clib_error_t *error = 0;
1183 u8 is_add = 1;
1184 u32 bd_id = ~0;
Choonho Son5ee51f82017-04-05 19:09:52 +09001185 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
Choonho Son05480792017-03-29 20:07:45 +09001186 u32 mac_age = 0;
1187 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1188 int rv;
1189
1190 /* Get a line of input. */
1191 if (!unformat_user (input, unformat_line_input, line_input))
1192 return 0;
1193
1194 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1195 {
1196 if (unformat (line_input, "%d", &bd_id))
1197 ;
1198 else if (unformat (line_input, "flood %d", &flood))
1199 ;
1200 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1201 ;
1202 else if (unformat (line_input, "forward %d", &forward))
1203 ;
Choonho Son5ee51f82017-04-05 19:09:52 +09001204 else if (unformat (line_input, "learn %d", &learn))
1205 ;
Choonho Son05480792017-03-29 20:07:45 +09001206 else if (unformat (line_input, "arp-term %d", &arp_term))
1207 ;
1208 else if (unformat (line_input, "mac-age %d", &mac_age))
1209 ;
1210 else if (unformat (line_input, "del"))
1211 {
1212 is_add = 0;
1213 flood = uu_flood = forward = learn = 0;
1214 }
1215 else
1216 break;
1217 }
1218
1219 if (bd_id == ~0)
1220 {
1221 error = clib_error_return (0, "bridge-domain-id not specified");
1222 goto done;
1223 }
1224
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001225 if (bd_id == 0)
1226 {
1227 error = clib_error_return (0, "bridge domain 0 can not be modified");
1228 goto done;
1229 }
1230
Choonho Son05480792017-03-29 20:07:45 +09001231 if (mac_age > 255)
1232 {
1233 error = clib_error_return (0, "mac age must be less than 256");
1234 goto done;
1235 }
1236
1237 memset (a, 0, sizeof (*a));
1238 a->is_add = is_add;
1239 a->bd_id = bd_id;
1240 a->flood = (u8) flood;
1241 a->uu_flood = (u8) uu_flood;
1242 a->forward = (u8) forward;
1243 a->learn = (u8) learn;
1244 a->arp_term = (u8) arp_term;
1245 a->mac_age = (u8) mac_age;
1246
1247 rv = bd_add_del (a);
1248
1249 switch (rv)
1250 {
1251 case 0:
1252 if (is_add)
1253 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1254 break;
Eyal Barib1352ed2017-04-07 23:14:17 +03001255 case VNET_API_ERROR_BD_IN_USE:
1256 error = clib_error_return (0, "bridge domain in use - remove members");
1257 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001258 case VNET_API_ERROR_NO_SUCH_ENTRY:
1259 error = clib_error_return (0, "bridge domain id does not exist");
1260 goto done;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001261 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1262 error = clib_error_return (0, "bridge domain 0 can not be modified");
1263 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001264 default:
1265 error = clib_error_return (0, "bd_add_del returned %d", rv);
1266 goto done;
1267 }
1268
1269done:
1270 unformat_free (line_input);
1271
1272 return error;
1273}
1274
1275
1276/*?
1277 * Create/Delete bridge-domain instance
1278 *
1279 * @cliexpar
1280 * @parblock
1281 * Example of creating bridge-domain 1:
1282 * @cliexstart{create bridge-domain 1}
1283 * bridge-domain 1
1284 * @cliexend
1285 *
1286 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1287 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1288 * bridge-domain 2
1289 *
1290 * vpp# show bridge-domain
1291 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1292 * 0 0 0 off off off off off off local0
1293 * 1 1 0 off on on off on off N/A
1294 * 2 2 0 60 on on off on on N/A
1295 *
1296 * @cliexend
1297 *
1298 * Example of delete bridge-domain 1:
1299 * @cliexstart{create bridge-domain 1 del}
1300 * @cliexend
1301 * @endparblock
1302?*/
1303
1304/* *INDENT-OFF* */
1305VLIB_CLI_COMMAND (bd_create_cli, static) = {
1306 .path = "create bridge-domain",
1307 .short_help = "create bridge-domain <bridge-domain-id>"
1308 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1309 " [mac-age <nn>] [del]",
1310 .function = bd_add_del_command_fn,
1311};
1312/* *INDENT-ON* */
1313
1314
1315
Dave Barach97d8dc22016-08-15 15:31:15 -04001316/*
1317 * fd.io coding-style-patch-verification: ON
1318 *
1319 * Local Variables:
1320 * eval: (c-set-style "gnu")
1321 * End:
1322 */