blob: a87d02f2ffdf1ed712c98a55073d87fa565c4194 [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;
John Lod48c8eb2017-05-05 12:35:25 -040097 u64 mac_addr;
98 ip6_address_t *ip6_addr_key;
Eyal Bariafc47aa2017-04-20 14:45:17 +030099
John Lod48c8eb2017-05-05 12:35:25 -0400100 /* flush non-static MACs in BD and removed bd_id from hash table */
101 l2fib_flush_bd_mac (vlib_get_main (), bd_index);
Eyal Barib1352ed2017-04-07 23:14:17 +0300102 hash_unset (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400103
104 /* mark this index clear */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
John Lod77630a2017-04-28 00:33:36 -0400107 /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
108 bd->bd_id = ~0;
109 bd->feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
John Lod48c8eb2017-05-05 12:35:25 -0400111 /* free memory used by BD */
John Lod77630a2017-04-28 00:33:36 -0400112 vec_free (bd->members);
113 hash_free (bd->mac_by_ip4);
John Lod48c8eb2017-05-05 12:35:25 -0400114 /* *INDENT-OFF* */
115 hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
116 ({
117 clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
118 }));
119 /* *INDENT-ON* */
John Lod77630a2017-04-28 00:33:36 -0400120 hash_free (bd->mac_by_ip6);
John Loda1f2c72017-03-24 20:11:15 -0400121
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 return 0;
123}
124
Eyal Baric5b13602016-11-24 19:42:43 +0200125static void
126update_flood_count (l2_bridge_domain_t * bd_config)
127{
128 bd_config->flood_count = vec_len (bd_config->members) -
129 (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
130}
131
Ed Warnickecb9cada2015-12-08 15:45:58 -0700132void
Dave Barach97d8dc22016-08-15 15:31:15 -0400133bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134{
Eyal Baric5b13602016-11-24 19:42:43 +0200135 u32 ix;
136 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
137 (vnet_get_main (), member->sw_if_index);
138
Dave Barach97d8dc22016-08-15 15:31:15 -0400139 /*
140 * Add one element to the vector
Eyal Baric5b13602016-11-24 19:42:43 +0200141 * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
Dave Barach97d8dc22016-08-15 15:31:15 -0400142 * When flooding, the bvi interface (if present) must be the last member
143 * processed due to how BVI processing can change the packet. To enable
144 * this order, we make the bvi interface the first in the vector and
145 * flooding walks the vector in reverse.
146 */
Eyal Baric5b13602016-11-24 19:42:43 +0200147 switch (sw_if->flood_class)
Dave Barach97d8dc22016-08-15 15:31:15 -0400148 {
Eyal Baric5b13602016-11-24 19:42:43 +0200149 case VNET_FLOOD_CLASS_TUNNEL_MASTER:
150 bd_config->tun_master_count++;
151 /* Fall through */
152 default:
153 /* Fall through */
154 case VNET_FLOOD_CLASS_NORMAL:
155 ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
156 vec_len (bd_config->members) - bd_config->tun_normal_count;
157 break;
158 case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
159 ix = vec_len (bd_config->members);
160 bd_config->tun_normal_count++;
161 break;
162 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163
Eyal Baric5b13602016-11-24 19:42:43 +0200164 vec_insert_elts (bd_config->members, member, 1, ix);
165 update_flood_count (bd_config);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700166}
167
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168#define BD_REMOVE_ERROR_OK 0
169#define BD_REMOVE_ERROR_NOT_FOUND 1
170
171u32
Dave Barach97d8dc22016-08-15 15:31:15 -0400172bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173{
174 u32 ix;
Dave Barach97d8dc22016-08-15 15:31:15 -0400175
176 /* Find and delete the member */
177 vec_foreach_index (ix, bd_config->members)
178 {
Eyal Baric5b13602016-11-24 19:42:43 +0200179 l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
180 if (m->sw_if_index == sw_if_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400181 {
Eyal Baric5b13602016-11-24 19:42:43 +0200182 vnet_sw_interface_t *sw_if = vnet_get_sw_interface
183 (vnet_get_main (), sw_if_index);
184
185 if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
186 {
187 if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
188 bd_config->tun_master_count--;
189 else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
190 bd_config->tun_normal_count--;
191 }
Eyal Bari25b36672017-03-02 10:43:19 +0200192 vec_delete (bd_config->members, 1, ix);
Eyal Baric5b13602016-11-24 19:42:43 +0200193 update_flood_count (bd_config);
194
Dave Barach97d8dc22016-08-15 15:31:15 -0400195 return BD_REMOVE_ERROR_OK;
196 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197 }
198
199 return BD_REMOVE_ERROR_NOT_FOUND;
200}
201
202
Dave Barach97d8dc22016-08-15 15:31:15 -0400203clib_error_t *
204l2bd_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205{
206 bd_main_t *bdm = &bd_main;
Dave Barach97d8dc22016-08-15 15:31:15 -0400207 bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
208 /*
209 * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
210 * to packet drop only. Thus, packets received from any L2 interface with
211 * uninitialized bd_index of 0 can be dropped safely.
212 */
Eyal Barib1352ed2017-04-07 23:14:17 +0300213 u32 bd_index = bd_add_bd_index (bdm, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214 ASSERT (bd_index == 0);
Dave Barach97d8dc22016-08-15 15:31:15 -0400215 l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
Choonho Son05480792017-03-29 20:07:45 +0900216
217 bdm->vlib_main = vm;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218 return 0;
219}
220
221VLIB_INIT_FUNCTION (l2bd_init);
222
223
Dave Barach97d8dc22016-08-15 15:31:15 -0400224/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400225 Set the learn/forward/flood flags for the bridge domain.
Dave Barach97d8dc22016-08-15 15:31:15 -0400226 Return 0 if ok, non-zero if for an error.
227*/
228u32
229bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
230{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231
Eyal Bariafc47aa2017-04-20 14:45:17 +0300232 l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 bd_validate (bd_config);
Eyal Bariafc47aa2017-04-20 14:45:17 +0300234 u32 feature_bitmap = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235
Dave Barach97d8dc22016-08-15 15:31:15 -0400236 if (flags & L2_LEARN)
237 {
238 feature_bitmap |= L2INPUT_FEAT_LEARN;
239 }
240 if (flags & L2_FWD)
241 {
242 feature_bitmap |= L2INPUT_FEAT_FWD;
243 }
244 if (flags & L2_FLOOD)
245 {
246 feature_bitmap |= L2INPUT_FEAT_FLOOD;
247 }
248 if (flags & L2_UU_FLOOD)
249 {
250 feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
251 }
252 if (flags & L2_ARP_TERM)
253 {
254 feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
255 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256
Dave Barach97d8dc22016-08-15 15:31:15 -0400257 if (enable)
258 {
259 bd_config->feature_bitmap |= feature_bitmap;
260 }
261 else
262 {
263 bd_config->feature_bitmap &= ~feature_bitmap;
264 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
266 return 0;
267}
268
Dave Barach97d8dc22016-08-15 15:31:15 -0400269/**
Damjan Mariond171d482016-12-05 14:16:38 +0100270 Set the mac age for the bridge domain.
271*/
272void
273bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
274{
275 l2_bridge_domain_t *bd_config;
276 int enable = 0;
277
278 vec_validate (l2input_main.bd_configs, bd_index);
279 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
280 bd_config->mac_age = age;
281
282 /* check if there is at least one bd with mac aging enabled */
283 vec_foreach (bd_config, l2input_main.bd_configs)
Eyal Barifead6702017-04-04 04:46:32 +0300284 enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
Damjan Mariond171d482016-12-05 14:16:38 +0100285
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
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500313 if (bd_id == 0)
314 return clib_error_return (0,
315 "No operations on the default bridge domain are supported");
316
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
318
319 if (p == 0)
320 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400321
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 bd_index = p[0];
323
324 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400325 if (unformat (input, "disable"))
326 {
327 enable = 0;
328 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach97d8dc22016-08-15 15:31:15 -0400330 /* set the bridge domain flag */
331 if (bd_set_flags (vm, bd_index, L2_LEARN, enable))
332 {
333 error =
334 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
335 goto done;
336 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Dave Barach97d8dc22016-08-15 15:31:15 -0400338done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 return error;
340}
341
Billy McFall22aa3e92016-09-09 08:46:40 -0400342/*?
343 * Layer 2 learning can be enabled and disabled on each
344 * interface and on each bridge-domain. Use this command to
345 * manage bridge-domains. It is enabled by default.
346 *
347 * @cliexpar
348 * Example of how to enable learning (where 200 is the bridge-domain-id):
349 * @cliexcmd{set bridge-domain learn 200}
350 * Example of how to disable learning (where 200 is the bridge-domain-id):
351 * @cliexcmd{set bridge-domain learn 200 disable}
352?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400353/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354VLIB_CLI_COMMAND (bd_learn_cli, static) = {
355 .path = "set bridge-domain learn",
356 .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
357 .function = bd_learn,
358};
Dave Barach97d8dc22016-08-15 15:31:15 -0400359/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
Dave Barach97d8dc22016-08-15 15:31:15 -0400361/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400362 Set bridge-domain forward enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400363 The CLI format is:
364 set bridge-domain forward <bd_index> [disable]
365*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700366static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400367bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368{
Dave Barach97d8dc22016-08-15 15:31:15 -0400369 bd_main_t *bdm = &bd_main;
370 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371 u32 bd_index, bd_id;
372 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400373 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374
Dave Barach97d8dc22016-08-15 15:31:15 -0400375 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376 {
377 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400378 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 goto done;
380 }
381
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500382 if (bd_id == 0)
383 return clib_error_return (0,
384 "No operations on the default bridge domain are supported");
385
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
387
388 if (p == 0)
389 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400390
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 bd_index = p[0];
392
393 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400394 if (unformat (input, "disable"))
395 {
396 enable = 0;
397 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398
Dave Barach97d8dc22016-08-15 15:31:15 -0400399 /* set the bridge domain flag */
400 if (bd_set_flags (vm, bd_index, L2_FWD, enable))
401 {
402 error =
403 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
404 goto done;
405 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Dave Barach97d8dc22016-08-15 15:31:15 -0400407done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 return error;
409}
410
Billy McFall22aa3e92016-09-09 08:46:40 -0400411
412/*?
413 * Layer 2 unicast forwarding can be enabled and disabled on each
414 * interface and on each bridge-domain. Use this command to
415 * manage bridge-domains. It is enabled by default.
416 *
417 * @cliexpar
418 * Example of how to enable forwarding (where 200 is the bridge-domain-id):
419 * @cliexcmd{set bridge-domain forward 200}
420 * Example of how to disable forwarding (where 200 is the bridge-domain-id):
421 * @cliexcmd{set bridge-domain forward 200 disable}
422?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400423/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
425 .path = "set bridge-domain forward",
426 .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
427 .function = bd_fwd,
428};
Dave Barach97d8dc22016-08-15 15:31:15 -0400429/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430
Dave Barach97d8dc22016-08-15 15:31:15 -0400431/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400432 Set bridge-domain flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400433 The CLI format is:
434 set bridge-domain flood <bd_index> [disable]
435*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436static clib_error_t *
437bd_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400438 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439{
Dave Barach97d8dc22016-08-15 15:31:15 -0400440 bd_main_t *bdm = &bd_main;
441 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 u32 bd_index, bd_id;
443 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400444 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445
Dave Barach97d8dc22016-08-15 15:31:15 -0400446 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 {
448 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400449 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 goto done;
451 }
452
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500453 if (bd_id == 0)
454 return clib_error_return (0,
455 "No operations on the default bridge domain are supported");
456
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
458
459 if (p == 0)
460 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400461
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 bd_index = p[0];
463
464 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400465 if (unformat (input, "disable"))
466 {
467 enable = 0;
468 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700469
Dave Barach97d8dc22016-08-15 15:31:15 -0400470 /* set the bridge domain flag */
471 if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
472 {
473 error =
474 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
475 goto done;
476 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Dave Barach97d8dc22016-08-15 15:31:15 -0400478done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700479 return error;
480}
481
Billy McFall22aa3e92016-09-09 08:46:40 -0400482/*?
483 * Layer 2 flooding can be enabled and disabled on each
484 * interface and on each bridge-domain. Use this command to
485 * manage bridge-domains. It is enabled by default.
486 *
487 * @cliexpar
488 * Example of how to enable flooding (where 200 is the bridge-domain-id):
489 * @cliexcmd{set bridge-domain flood 200}
490 * Example of how to disable flooding (where 200 is the bridge-domain-id):
491 * @cliexcmd{set bridge-domain flood 200 disable}
492?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400493/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494VLIB_CLI_COMMAND (bd_flood_cli, static) = {
495 .path = "set bridge-domain flood",
496 .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
497 .function = bd_flood,
498};
Dave Barach97d8dc22016-08-15 15:31:15 -0400499/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Dave Barach97d8dc22016-08-15 15:31:15 -0400501/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400502 Set bridge-domain unkown-unicast flood enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400503 The CLI format is:
504 set bridge-domain uu-flood <bd_index> [disable]
505*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506static clib_error_t *
507bd_uu_flood (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400508 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509{
Dave Barach97d8dc22016-08-15 15:31:15 -0400510 bd_main_t *bdm = &bd_main;
511 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700512 u32 bd_index, bd_id;
513 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400514 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515
Dave Barach97d8dc22016-08-15 15:31:15 -0400516 if (!unformat (input, "%d", &bd_id))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 {
518 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
Dave Barach97d8dc22016-08-15 15:31:15 -0400519 format_unformat_error, input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 goto done;
521 }
522
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500523 if (bd_id == 0)
524 return clib_error_return (0,
525 "No operations on the default bridge domain are supported");
526
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
528
529 if (p == 0)
530 return clib_error_return (0, "No such bridge domain %d", bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400531
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532 bd_index = p[0];
533
534 enable = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400535 if (unformat (input, "disable"))
536 {
537 enable = 0;
538 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539
Dave Barach97d8dc22016-08-15 15:31:15 -0400540 /* set the bridge domain flag */
541 if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
542 {
543 error =
544 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
545 goto done;
546 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547
Dave Barach97d8dc22016-08-15 15:31:15 -0400548done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 return error;
550}
551
Billy McFall22aa3e92016-09-09 08:46:40 -0400552/*?
553 * Layer 2 unknown-unicast flooding can be enabled and disabled on each
554 * bridge-domain. It is enabled by default.
555 *
556 * @cliexpar
557 * Example of how to enable unknown-unicast flooding (where 200 is the
558 * bridge-domain-id):
559 * @cliexcmd{set bridge-domain uu-flood 200}
560 * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
561 * @cliexcmd{set bridge-domain uu-flood 200 disable}
562?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400563/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
565 .path = "set bridge-domain uu-flood",
566 .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
567 .function = bd_uu_flood,
568};
Dave Barach97d8dc22016-08-15 15:31:15 -0400569/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Barach97d8dc22016-08-15 15:31:15 -0400571/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400572 Set bridge-domain arp term enable/disable.
Dave Barach97d8dc22016-08-15 15:31:15 -0400573 The CLI format is:
574 set bridge-domain arp term <bridge-domain-id> [disable]
575*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576static clib_error_t *
577bd_arp_term (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400578 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579{
Dave Barach97d8dc22016-08-15 15:31:15 -0400580 bd_main_t *bdm = &bd_main;
581 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 u32 bd_index, bd_id;
583 u32 enable;
Dave Barach97d8dc22016-08-15 15:31:15 -0400584 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach97d8dc22016-08-15 15:31:15 -0400586 if (!unformat (input, "%d", &bd_id))
587 {
588 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
589 format_unformat_error, input);
590 goto done;
591 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500593 if (bd_id == 0)
594 return clib_error_return (0,
595 "No operations on the default bridge domain are supported");
596
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
Dave Barach97d8dc22016-08-15 15:31:15 -0400598 if (p)
599 bd_index = *p;
600 else
601 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
Dave Barach97d8dc22016-08-15 15:31:15 -0400603 enable = 1;
604 if (unformat (input, "disable"))
605 enable = 0;
606
607 /* set the bridge domain flag */
608 if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
609 {
610 error =
611 clib_error_return (0, "bridge-domain id %d out of range", bd_index);
612 goto done;
613 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700614
615done:
616 return error;
617}
618
Damjan Mariond171d482016-12-05 14:16:38 +0100619static clib_error_t *
620bd_mac_age (vlib_main_t * vm,
621 unformat_input_t * input, vlib_cli_command_t * cmd)
622{
623 bd_main_t *bdm = &bd_main;
624 clib_error_t *error = 0;
625 u32 bd_index, bd_id;
626 u32 age;
627 uword *p;
628
629 if (!unformat (input, "%d", &bd_id))
630 {
631 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
632 format_unformat_error, input);
633 goto done;
634 }
635
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500636 if (bd_id == 0)
637 return clib_error_return (0,
638 "No operations on the default bridge domain are supported");
639
Damjan Mariond171d482016-12-05 14:16:38 +0100640 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
641
642 if (p == 0)
643 return clib_error_return (0, "No such bridge domain %d", bd_id);
644
645 bd_index = p[0];
646
647 if (!unformat (input, "%u", &age))
648 {
649 error =
650 clib_error_return (0, "expecting ageing time in minutes but got `%U'",
651 format_unformat_error, input);
652 goto done;
653 }
654
655 /* set the bridge domain flag */
656 if (age > 255)
657 {
658 error =
659 clib_error_return (0, "mac aging time cannot be bigger than 255");
660 goto done;
661 }
662 bd_set_mac_age (vm, bd_index, (u8) age);
663
664done:
665 return error;
666}
667
668/*?
669 * Layer 2 mac aging can be enabled and disabled on each
670 * bridge-domain. Use this command to set or disable mac aging
671 * on specific bridge-domains. It is disabled by default.
672 *
673 * @cliexpar
674 * Example of how to set mac aging (where 200 is the bridge-domain-id and
675 * 5 is aging time in minutes):
676 * @cliexcmd{set bridge-domain mac-age 200 5}
677 * Example of how to disable mac aging (where 200 is the bridge-domain-id):
678 * @cliexcmd{set bridge-domain flood 200 0}
679?*/
680/* *INDENT-OFF* */
681VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
682 .path = "set bridge-domain mac-age",
683 .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
684 .function = bd_mac_age,
685};
686/* *INDENT-ON* */
687
Billy McFall22aa3e92016-09-09 08:46:40 -0400688/*?
689 * Modify whether or not an existing bridge-domain should terminate and respond
690 * to ARP Requests. ARP Termination is disabled by default.
691 *
692 * @cliexpar
693 * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
694 * @cliexcmd{set bridge-domain arp term 200}
695 * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
696 * @cliexcmd{set bridge-domain arp term 200 disable}
697?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400698/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
700 .path = "set bridge-domain arp term",
701 .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
702 .function = bd_arp_term,
703};
Dave Barach97d8dc22016-08-15 15:31:15 -0400704/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
706
Dave Barach97d8dc22016-08-15 15:31:15 -0400707/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400708 * Add/delete IP address to MAC address mapping.
709 *
Dave Barach97d8dc22016-08-15 15:31:15 -0400710 * The clib hash implementation stores uword entries in the hash table.
711 * The hash table mac_by_ip4 is keyed via IP4 address and store the
712 * 6-byte MAC address directly in the hash table entry uword.
Chris Luke16bcf7d2016-09-01 14:31:46 -0400713 *
714 * @warning This only works for 64-bit processor with 8-byte uword;
715 * which means this code *WILL NOT WORK* for a 32-bit prcessor with
716 * 4-byte uword.
Dave Barach97d8dc22016-08-15 15:31:15 -0400717 */
718u32
719bd_add_del_ip_mac (u32 bd_index,
720 u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721{
Eyal Bariafc47aa2017-04-20 14:45:17 +0300722 l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700723 u64 new_mac = *(u64 *) mac_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -0400724 u64 *old_mac;
725 u16 *mac16 = (u16 *) & new_mac;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Dave Barach97d8dc22016-08-15 15:31:15 -0400727 ASSERT (sizeof (uword) == sizeof (u64)); /* make sure uword is 8 bytes */
Eyal Bariafc47aa2017-04-20 14:45:17 +0300728 ASSERT (bd_is_valid (bd_cfg));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729
John Lo1edfba92016-08-27 01:11:57 -0400730 mac16[3] = 0; /* Clear last 2 unsed bytes of the 8-byte MAC address */
Dave Barach97d8dc22016-08-15 15:31:15 -0400731 if (is_ip6)
732 {
John Lo1edfba92016-08-27 01:11:57 -0400733 ip6_address_t *ip6_addr_key;
734 hash_pair_t *hp;
735 old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
736 if (is_add)
737 {
738 if (old_mac == 0)
739 { /* new entry - allocate and craete ip6 address key */
740 ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
741 clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
742 }
743 else if (*old_mac == new_mac)
744 { /* same mac entry already exist for ip6 address */
745 return 0;
746 }
747 else
748 { /* updat mac for ip6 address */
749 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
750 ip6_addr_key = (ip6_address_t *) hp->key;
751 }
752 hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
753 }
754 else
755 {
756 if (old_mac && (*old_mac == new_mac))
757 {
758 hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
759 ip6_addr_key = (ip6_address_t *) hp->key;
760 hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
761 clib_mem_free (ip6_addr_key);
762 }
763 else
764 return 1;
765 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400767 else
768 {
769 ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
770 old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
771 if (is_add)
772 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400773 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400774 return 0; /* mac entry already exist */
Dave Barach97d8dc22016-08-15 15:31:15 -0400775 hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
776 }
777 else
778 {
Dave Barach97d8dc22016-08-15 15:31:15 -0400779 if (old_mac && (*old_mac == new_mac))
John Lo1edfba92016-08-27 01:11:57 -0400780 hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
Dave Barach97d8dc22016-08-15 15:31:15 -0400781 else
John Lo1edfba92016-08-27 01:11:57 -0400782 return 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400783 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400784 }
John Lo1edfba92016-08-27 01:11:57 -0400785 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700786}
787
Dave Barach97d8dc22016-08-15 15:31:15 -0400788/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400789 Set bridge-domain arp entry add/delete.
Dave Barach97d8dc22016-08-15 15:31:15 -0400790 The CLI format is:
Billy McFall22aa3e92016-09-09 08:46:40 -0400791 set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
Dave Barach97d8dc22016-08-15 15:31:15 -0400792*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793static clib_error_t *
794bd_arp_entry (vlib_main_t * vm,
Dave Barach97d8dc22016-08-15 15:31:15 -0400795 unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700796{
Dave Barach97d8dc22016-08-15 15:31:15 -0400797 bd_main_t *bdm = &bd_main;
798 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700799 u32 bd_index, bd_id;
800 u8 is_add = 1;
801 u8 is_ip6 = 0;
802 u8 ip_addr[16];
803 u8 mac_addr[6];
Dave Barach97d8dc22016-08-15 15:31:15 -0400804 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700805
Dave Barach97d8dc22016-08-15 15:31:15 -0400806 if (!unformat (input, "%d", &bd_id))
807 {
808 error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
809 format_unformat_error, input);
810 goto done;
811 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500813 if (bd_id == 0)
814 return clib_error_return (0,
815 "No operations on the default bridge domain are supported");
816
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
818
Dave Barach97d8dc22016-08-15 15:31:15 -0400819 if (p)
820 bd_index = *p;
821 else
822 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823
Dave Barach97d8dc22016-08-15 15:31:15 -0400824 if (unformat (input, "%U", unformat_ip4_address, ip_addr))
825 {
826 is_ip6 = 0;
827 }
828 else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
829 {
830 is_ip6 = 1;
831 }
832 else
833 {
834 error = clib_error_return (0, "expecting IP address but got `%U'",
835 format_unformat_error, input);
836 goto done;
837 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700838
Dave Barach97d8dc22016-08-15 15:31:15 -0400839 if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
840 {
841 error = clib_error_return (0, "expecting MAC address but got `%U'",
842 format_unformat_error, input);
843 goto done;
844 }
845
846 if (unformat (input, "del"))
847 {
848 is_add = 0;
849 }
850
851 /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
852 if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
853 {
854 error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
855 is_add ? "add" : "del",
John Lo1edfba92016-08-27 01:11:57 -0400856 is_ip6 ?
857 format_ip4_address : format_ip6_address,
858 ip_addr, format_ethernet_address, mac_addr);
Dave Barach97d8dc22016-08-15 15:31:15 -0400859 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860
861done:
862 return error;
863}
864
Billy McFall22aa3e92016-09-09 08:46:40 -0400865/*?
866 * Add an ARP entry to an existing bridge-domain.
867 *
868 * @cliexpar
869 * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
870 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
871 * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
872 * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
873?*/
Dave Barach97d8dc22016-08-15 15:31:15 -0400874/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
876 .path = "set bridge-domain arp entry",
Billy McFall22aa3e92016-09-09 08:46:40 -0400877 .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878 .function = bd_arp_entry,
879};
Dave Barach97d8dc22016-08-15 15:31:15 -0400880/* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700881
Dave Barach97d8dc22016-08-15 15:31:15 -0400882u8 *
883format_vtr (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700884{
Dave Barach97d8dc22016-08-15 15:31:15 -0400885 u32 vtr_op = va_arg (*args, u32);
886 u32 dot1q = va_arg (*args, u32);
887 u32 tag1 = va_arg (*args, u32);
888 u32 tag2 = va_arg (*args, u32);
889 switch (vtr_op)
890 {
Ed Warnickecb9cada2015-12-08 15:45:58 -0700891 case L2_VTR_DISABLED:
Dave Barach97d8dc22016-08-15 15:31:15 -0400892 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700893 case L2_VTR_PUSH_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400894 return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 case L2_VTR_PUSH_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400896 return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
897 tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898 case L2_VTR_POP_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400899 return format (s, "pop-1");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700900 case L2_VTR_POP_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400901 return format (s, "pop-2");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700902 case L2_VTR_TRANSLATE_1_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400903 return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700904 case L2_VTR_TRANSLATE_1_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400905 return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
906 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700907 case L2_VTR_TRANSLATE_2_1:
Dave Barach97d8dc22016-08-15 15:31:15 -0400908 return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 case L2_VTR_TRANSLATE_2_2:
Dave Barach97d8dc22016-08-15 15:31:15 -0400910 return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
911 tag1, tag2);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700912 default:
Dave Barach97d8dc22016-08-15 15:31:15 -0400913 return format (s, "none");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700914 }
915}
916
Dave Barach97d8dc22016-08-15 15:31:15 -0400917/**
Chris Luke16bcf7d2016-09-01 14:31:46 -0400918 Show bridge-domain state.
Dave Barach97d8dc22016-08-15 15:31:15 -0400919 The CLI format is:
920 show bridge-domain [<bd_index>]
921*/
Ed Warnickecb9cada2015-12-08 15:45:58 -0700922static clib_error_t *
Dave Barach97d8dc22016-08-15 15:31:15 -0400923bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700924{
Dave Barach97d8dc22016-08-15 15:31:15 -0400925 vnet_main_t *vnm = vnet_get_main ();
926 bd_main_t *bdm = &bd_main;
927 clib_error_t *error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928 u32 bd_index = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400929 l2_bridge_domain_t *bd_config;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930 u32 start, end;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931 u32 detail = 0;
932 u32 intf = 0;
933 u32 arp = 0;
934 u32 bd_id = ~0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400935 uword *p;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500937 start = 1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400938 end = vec_len (l2input_main.bd_configs);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939
Dave Barach97d8dc22016-08-15 15:31:15 -0400940 if (unformat (input, "%d", &bd_id))
941 {
942 if (unformat (input, "detail"))
943 detail = 1;
944 else if (unformat (input, "det"))
945 detail = 1;
946 if (unformat (input, "int"))
947 intf = 1;
948 if (unformat (input, "arp"))
949 arp = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700950
Jon Loeliger1c7d4852017-05-02 11:06:23 -0500951 if (bd_id == 0)
952 return clib_error_return (0,
953 "No operations on the default bridge domain are supported");
954
Dave Barach97d8dc22016-08-15 15:31:15 -0400955 p = hash_get (bdm->bd_index_by_bd_id, bd_id);
956 if (p)
957 bd_index = *p;
958 else
959 return clib_error_return (0, "No such bridge domain %d", bd_id);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960
Dave Barach97d8dc22016-08-15 15:31:15 -0400961 vec_validate (l2input_main.bd_configs, bd_index);
962 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
963 if (bd_is_valid (bd_config))
964 {
965 start = bd_index;
966 end = start + 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700967 }
Dave Barach97d8dc22016-08-15 15:31:15 -0400968 else
969 {
970 vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
971 goto done;
972 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700973 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700974
Dave Barach97d8dc22016-08-15 15:31:15 -0400975 /* Show all bridge-domains that have been initialized */
John Loda1f2c72017-03-24 20:11:15 -0400976 u32 printed = 0;
977 u8 *as = 0;
Dave Barach97d8dc22016-08-15 15:31:15 -0400978 for (bd_index = start; bd_index < end; bd_index++)
979 {
980 bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
981 if (bd_is_valid (bd_config))
982 {
983 if (!printed)
984 {
985 printed = 1;
986 vlib_cli_output (vm,
John Lo97934772017-05-18 22:26:47 -0400987 "%=8s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
988 "BD-ID", "Index", "BSN", "Age(min)",
989 "Learning", "U-Forwrd", "UU-Flood", "Flooding",
990 "ARP-Term", "BVI-Intf");
Dave Barach97d8dc22016-08-15 15:31:15 -0400991 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700992
John Loda1f2c72017-03-24 20:11:15 -0400993 if (bd_config->mac_age)
994 as = format (as, "%d", bd_config->mac_age);
995 else
996 as = format (as, "off");
Dave Barach97d8dc22016-08-15 15:31:15 -0400997 vlib_cli_output (vm,
John Lo97934772017-05-18 22:26:47 -0400998 "%=8d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
John Loda1f2c72017-03-24 20:11:15 -0400999 bd_config->bd_id, bd_index, bd_config->seq_num, as,
Dave Barach97d8dc22016-08-15 15:31:15 -04001000 bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
1001 "on" : "off",
John Loda1f2c72017-03-24 20:11:15 -04001002 bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
1003 "on" : "off",
Dave Barach97d8dc22016-08-15 15:31:15 -04001004 bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
1005 "on" : "off",
1006 bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
1007 "on" : "off",
1008 bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
John Loda1f2c72017-03-24 20:11:15 -04001009 "on" : "off",
1010 format_vnet_sw_if_index_name_with_NA,
Dave Barach97d8dc22016-08-15 15:31:15 -04001011 vnm, bd_config->bvi_sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -04001012 vec_reset_length (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001013
1014 if (detail || intf)
1015 {
1016 /* Show all member interfaces */
Eyal Baric5b13602016-11-24 19:42:43 +02001017 int i;
1018 vec_foreach_index (i, bd_config->members)
Dave Barach97d8dc22016-08-15 15:31:15 -04001019 {
Eyal Baric5b13602016-11-24 19:42:43 +02001020 l2_flood_member_t *member =
1021 vec_elt_at_index (bd_config->members, i);
Eyal Bari0f360dc2017-06-14 13:11:20 +03001022 u8 swif_seq_num = *l2fib_swif_seq_num (member->sw_if_index);
Dave Barach97d8dc22016-08-15 15:31:15 -04001023 u32 vtr_opr, dot1q, tag1, tag2;
Eyal Baric5b13602016-11-24 19:42:43 +02001024 if (i == 0)
Dave Barach97d8dc22016-08-15 15:31:15 -04001025 {
John Loda1f2c72017-03-24 20:11:15 -04001026 vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1027 "Interface", "If-idx", "ISN", "SHG",
1028 "BVI", "TxFlood", "VLAN-Tag-Rewrite");
Dave Barach97d8dc22016-08-15 15:31:15 -04001029 }
1030 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1031 &tag1, &tag2);
John Loda1f2c72017-03-24 20:11:15 -04001032 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001033 format_vnet_sw_if_index_name, vnm,
1034 member->sw_if_index, member->sw_if_index,
Eyal Bari0f360dc2017-06-14 13:11:20 +03001035 swif_seq_num, member->shg,
Dave Barach97d8dc22016-08-15 15:31:15 -04001036 member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
Eyal Baric5b13602016-11-24 19:42:43 +02001037 "-", i < bd_config->flood_count ? "*" : "-",
1038 format_vtr, vtr_opr, dot1q, tag1, tag2);
Dave Barach97d8dc22016-08-15 15:31:15 -04001039 }
1040 }
1041
1042 if ((detail || arp) &&
1043 (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1044 {
1045 u32 ip4_addr;
John Lo1edfba92016-08-27 01:11:57 -04001046 ip6_address_t *ip6_addr;
Dave Barach97d8dc22016-08-15 15:31:15 -04001047 u64 mac_addr;
1048 vlib_cli_output (vm,
John Lo1edfba92016-08-27 01:11:57 -04001049 "\n IP4/IP6 to MAC table for ARP Termination");
Dave Barach97d8dc22016-08-15 15:31:15 -04001050
1051 /* *INDENT-OFF* */
1052 hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1053 ({
John Lo1edfba92016-08-27 01:11:57 -04001054 vlib_cli_output (vm, "%=40U => %=20U",
Dave Barach97d8dc22016-08-15 15:31:15 -04001055 format_ip4_address, &ip4_addr,
1056 format_ethernet_address, &mac_addr);
1057 }));
John Lo1edfba92016-08-27 01:11:57 -04001058
1059 hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1060 ({
1061 vlib_cli_output (vm, "%=40U => %=20U",
1062 format_ip6_address, ip6_addr,
1063 format_ethernet_address, &mac_addr);
1064 }));
Dave Barach97d8dc22016-08-15 15:31:15 -04001065 /* *INDENT-ON* */
1066 }
1067 }
1068 }
John Loda1f2c72017-03-24 20:11:15 -04001069 vec_free (as);
Dave Barach97d8dc22016-08-15 15:31:15 -04001070
1071 if (!printed)
1072 {
1073 vlib_cli_output (vm, "no bridge-domains in use");
1074 }
1075
1076done:
Ed Warnickecb9cada2015-12-08 15:45:58 -07001077 return error;
1078}
1079
Billy McFall22aa3e92016-09-09 08:46:40 -04001080/*?
1081 * Show a summary of all the bridge-domain instances or detailed view of a
1082 * single bridge-domain. Bridge-domains are created by adding an interface
1083 * to a bridge using the '<em>set interface l2 bridge</em>' command.
1084 *
1085 * @cliexpar
1086 * @parblock
1087 * Example of displaying all bridge-domains:
1088 * @cliexstart{show bridge-domain}
1089 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1090 * 0 0 off off off off off local0
1091 * 200 1 on on on on off N/A
1092 * @cliexend
1093 *
1094 * Example of displaying details of a single bridge-domains:
1095 * @cliexstart{show bridge-domain 200 detail}
1096 * ID Index Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1097 * 200 1 on on on on off N/A
1098 *
1099 * Interface Index SHG BVI VLAN-Tag-Rewrite
1100 * GigabitEthernet0/8/0.200 3 0 - none
1101 * GigabitEthernet0/9/0.200 4 0 - none
1102 * @cliexend
1103 * @endparblock
1104?*/
Dave Barach97d8dc22016-08-15 15:31:15 -04001105/* *INDENT-OFF* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106VLIB_CLI_COMMAND (bd_show_cli, static) = {
1107 .path = "show bridge-domain",
1108 .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1109 .function = bd_show,
1110};
Dave Barach97d8dc22016-08-15 15:31:15 -04001111/* *INDENT-ON* */
1112
Choonho Son05480792017-03-29 20:07:45 +09001113int
1114bd_add_del (l2_bridge_domain_add_del_args_t * a)
1115{
1116 bd_main_t *bdm = &bd_main;
1117 vlib_main_t *vm = bdm->vlib_main;
Choonho Son05480792017-03-29 20:07:45 +09001118 int rv = 0;
1119
Eyal Barib1352ed2017-04-07 23:14:17 +03001120 u32 bd_index = bd_find_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001121 if (a->is_add)
1122 {
Eyal Barib1352ed2017-04-07 23:14:17 +03001123 if (bd_index != ~0)
1124 return VNET_API_ERROR_BD_ALREADY_EXISTS;
John Lo97934772017-05-18 22:26:47 -04001125 if (a->bd_id > L2_BD_ID_MAX)
1126 return VNET_API_ERROR_BD_ID_EXCEED_MAX;
Eyal Barib1352ed2017-04-07 23:14:17 +03001127 bd_index = bd_add_bd_index (bdm, a->bd_id);
Choonho Son05480792017-03-29 20:07:45 +09001128
Eyal Barib1352ed2017-04-07 23:14:17 +03001129 u32 enable_flags = 0, disable_flags = 0;
Choonho Son05480792017-03-29 20:07:45 +09001130 if (a->flood)
1131 enable_flags |= L2_FLOOD;
1132 else
1133 disable_flags |= L2_FLOOD;
1134
1135 if (a->uu_flood)
1136 enable_flags |= L2_UU_FLOOD;
1137 else
1138 disable_flags |= L2_UU_FLOOD;
1139
1140 if (a->forward)
1141 enable_flags |= L2_FWD;
1142 else
1143 disable_flags |= L2_FWD;
1144
1145 if (a->learn)
1146 enable_flags |= L2_LEARN;
1147 else
1148 disable_flags |= L2_LEARN;
1149
1150 if (a->arp_term)
1151 enable_flags |= L2_ARP_TERM;
1152 else
1153 disable_flags |= L2_ARP_TERM;
1154
1155 if (enable_flags)
1156 bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1157
1158 if (disable_flags)
1159 bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1160
1161 bd_set_mac_age (vm, bd_index, a->mac_age);
1162 }
1163 else
Eyal Barib1352ed2017-04-07 23:14:17 +03001164 {
1165 if (bd_index == ~0)
1166 return VNET_API_ERROR_NO_SUCH_ENTRY;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001167 if (bd_index == 0)
1168 return VNET_API_ERROR_BD_NOT_MODIFIABLE;
Eyal Barib1352ed2017-04-07 23:14:17 +03001169 if (vec_len (l2input_main.bd_configs[bd_index].members))
1170 return VNET_API_ERROR_BD_IN_USE;
1171 rv = bd_delete (bdm, bd_index);
1172 }
Choonho Son05480792017-03-29 20:07:45 +09001173
1174 return rv;
1175}
1176
1177/**
1178 Create or delete bridge-domain.
Choonho Son5ee51f82017-04-05 19:09:52 +09001179 The CLI format:
Choonho Son05480792017-03-29 20:07:45 +09001180 create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>]
1181 [flood <0|1>] [arp-term <0|1>] [mac-age <nn>] [del]
1182*/
1183
1184static clib_error_t *
1185bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1186 vlib_cli_command_t * cmd)
1187{
1188 unformat_input_t _line_input, *line_input = &_line_input;
1189 clib_error_t *error = 0;
1190 u8 is_add = 1;
1191 u32 bd_id = ~0;
Choonho Son5ee51f82017-04-05 19:09:52 +09001192 u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term = 0;
Choonho Son05480792017-03-29 20:07:45 +09001193 u32 mac_age = 0;
1194 l2_bridge_domain_add_del_args_t _a, *a = &_a;
1195 int rv;
1196
1197 /* Get a line of input. */
1198 if (!unformat_user (input, unformat_line_input, line_input))
1199 return 0;
1200
1201 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1202 {
1203 if (unformat (line_input, "%d", &bd_id))
1204 ;
1205 else if (unformat (line_input, "flood %d", &flood))
1206 ;
1207 else if (unformat (line_input, "uu-flood %d", &uu_flood))
1208 ;
1209 else if (unformat (line_input, "forward %d", &forward))
1210 ;
Choonho Son5ee51f82017-04-05 19:09:52 +09001211 else if (unformat (line_input, "learn %d", &learn))
1212 ;
Choonho Son05480792017-03-29 20:07:45 +09001213 else if (unformat (line_input, "arp-term %d", &arp_term))
1214 ;
1215 else if (unformat (line_input, "mac-age %d", &mac_age))
1216 ;
1217 else if (unformat (line_input, "del"))
1218 {
1219 is_add = 0;
1220 flood = uu_flood = forward = learn = 0;
1221 }
1222 else
1223 break;
1224 }
1225
1226 if (bd_id == ~0)
1227 {
1228 error = clib_error_return (0, "bridge-domain-id not specified");
1229 goto done;
1230 }
1231
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001232 if (bd_id == 0)
1233 {
1234 error = clib_error_return (0, "bridge domain 0 can not be modified");
1235 goto done;
1236 }
1237
Choonho Son05480792017-03-29 20:07:45 +09001238 if (mac_age > 255)
1239 {
1240 error = clib_error_return (0, "mac age must be less than 256");
1241 goto done;
1242 }
1243
1244 memset (a, 0, sizeof (*a));
1245 a->is_add = is_add;
1246 a->bd_id = bd_id;
1247 a->flood = (u8) flood;
1248 a->uu_flood = (u8) uu_flood;
1249 a->forward = (u8) forward;
1250 a->learn = (u8) learn;
1251 a->arp_term = (u8) arp_term;
1252 a->mac_age = (u8) mac_age;
1253
1254 rv = bd_add_del (a);
1255
1256 switch (rv)
1257 {
1258 case 0:
1259 if (is_add)
1260 vlib_cli_output (vm, "bridge-domain %d", bd_id);
1261 break;
Eyal Barib1352ed2017-04-07 23:14:17 +03001262 case VNET_API_ERROR_BD_IN_USE:
1263 error = clib_error_return (0, "bridge domain in use - remove members");
1264 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001265 case VNET_API_ERROR_NO_SUCH_ENTRY:
John Lo97934772017-05-18 22:26:47 -04001266 error = clib_error_return (0, "bridge domain ID does not exist");
Choonho Son05480792017-03-29 20:07:45 +09001267 goto done;
Jon Loeliger1c7d4852017-05-02 11:06:23 -05001268 case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1269 error = clib_error_return (0, "bridge domain 0 can not be modified");
1270 goto done;
John Lo97934772017-05-18 22:26:47 -04001271 case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1272 error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1273 goto done;
Choonho Son05480792017-03-29 20:07:45 +09001274 default:
1275 error = clib_error_return (0, "bd_add_del returned %d", rv);
1276 goto done;
1277 }
1278
1279done:
1280 unformat_free (line_input);
1281
1282 return error;
1283}
1284
1285
1286/*?
1287 * Create/Delete bridge-domain instance
1288 *
1289 * @cliexpar
1290 * @parblock
1291 * Example of creating bridge-domain 1:
1292 * @cliexstart{create bridge-domain 1}
1293 * bridge-domain 1
1294 * @cliexend
1295 *
1296 * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1297 * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1298 * bridge-domain 2
1299 *
1300 * vpp# show bridge-domain
1301 * ID Index BSN Age(min) Learning U-Forwrd UU-Flood Flooding ARP-Term BVI-Intf
1302 * 0 0 0 off off off off off off local0
1303 * 1 1 0 off on on off on off N/A
1304 * 2 2 0 60 on on off on on N/A
1305 *
1306 * @cliexend
1307 *
1308 * Example of delete bridge-domain 1:
1309 * @cliexstart{create bridge-domain 1 del}
1310 * @cliexend
1311 * @endparblock
1312?*/
1313
1314/* *INDENT-OFF* */
1315VLIB_CLI_COMMAND (bd_create_cli, static) = {
1316 .path = "create bridge-domain",
1317 .short_help = "create bridge-domain <bridge-domain-id>"
1318 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1319 " [mac-age <nn>] [del]",
1320 .function = bd_add_del_command_fn,
1321};
1322/* *INDENT-ON* */
1323
1324
1325
Dave Barach97d8dc22016-08-15 15:31:15 -04001326/*
1327 * fd.io coding-style-patch-verification: ON
1328 *
1329 * Local Variables:
1330 * eval: (c-set-style "gnu")
1331 * End:
1332 */