session: remove ipv6 lookup threading assert
[vpp.git] / src / vnet / l2 / l2_bd.c
1 /*
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>
26 #include <vnet/l2/l2_learn.h>
27 #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
36 /**
37  * @file
38  * @brief Ethernet Bridge Domain.
39  *
40  * Code in this file manages Layer 2 bridge domains.
41  *
42  */
43
44 bd_main_t bd_main;
45
46 /**
47   Init bridge domain if not done already.
48   For feature bitmap, set all bits except ARP termination
49 */
50 void
51 bd_validate (l2_bridge_domain_t * bd_config)
52 {
53   if (bd_is_valid (bd_config))
54     return;
55   bd_config->feature_bitmap =
56     ~(L2INPUT_FEAT_ARP_TERM | L2INPUT_FEAT_UU_FWD | L2INPUT_FEAT_ARP_UFWD);
57   bd_config->bvi_sw_if_index = ~0;
58   bd_config->uu_fwd_sw_if_index = ~0;
59   bd_config->members = 0;
60   bd_config->flood_count = 0;
61   bd_config->tun_master_count = 0;
62   bd_config->tun_normal_count = 0;
63   bd_config->no_flood_count = 0;
64   bd_config->mac_by_ip4 = 0;
65   bd_config->mac_by_ip6 = hash_create_mem (0, sizeof (ip6_address_t),
66                                            sizeof (uword));
67 }
68
69 u32
70 bd_find_index (bd_main_t * bdm, u32 bd_id)
71 {
72   u32 *p = (u32 *) hash_get (bdm->bd_index_by_bd_id, bd_id);
73   if (!p)
74     return ~0;
75   return p[0];
76 }
77
78 u32
79 bd_add_bd_index (bd_main_t * bdm, u32 bd_id)
80 {
81   ASSERT (!hash_get (bdm->bd_index_by_bd_id, bd_id));
82   u32 rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
83
84   /* mark this index taken */
85   bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
86
87   hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
88
89   vec_validate (l2input_main.bd_configs, rv);
90   l2input_main.bd_configs[rv].bd_id = bd_id;
91   l2input_main.bd_configs[rv].learn_limit =
92     l2learn_main.bd_default_learn_limit;
93   l2input_main.bd_configs[rv].learn_count = 0;
94
95   return rv;
96 }
97
98 static inline void
99 bd_free_ip_mac_tables (l2_bridge_domain_t * bd)
100 {
101   u64 mac_addr;
102   ip6_address_t *ip6_addr_key;
103
104   hash_free (bd->mac_by_ip4);
105   hash_foreach_mem (ip6_addr_key, mac_addr, bd->mac_by_ip6,
106   ({
107     clib_mem_free (ip6_addr_key); /* free memory used for ip6 addr key */
108   }));
109   hash_free (bd->mac_by_ip6);
110 }
111
112 static int
113 bd_delete (bd_main_t * bdm, u32 bd_index)
114 {
115   l2_bridge_domain_t *bd = &l2input_main.bd_configs[bd_index];
116   u32 bd_id = bd->bd_id;
117
118   /* flush non-static MACs in BD and removed bd_id from hash table */
119   l2fib_flush_bd_mac (vlib_get_main (), bd_index);
120   hash_unset (bdm->bd_index_by_bd_id, bd_id);
121
122   /* mark this index clear */
123   bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
124
125   /* clear BD config for reuse: bd_id to -1 and clear feature_bitmap */
126   bd->bd_id = ~0;
127   bd->feature_bitmap = 0;
128   bd->learn_limit = 0;
129   bd->learn_count = ~0;
130
131   /* free BD tag */
132   vec_free (bd->bd_tag);
133
134   /* free memory used by BD */
135   vec_free (bd->members);
136   bd_free_ip_mac_tables (bd);
137
138   return 0;
139 }
140
141 static void
142 update_flood_count (l2_bridge_domain_t * bd_config)
143 {
144   bd_config->flood_count = (vec_len (bd_config->members) -
145                             (bd_config->tun_master_count ?
146                              bd_config->tun_normal_count : 0));
147   bd_config->flood_count -= bd_config->no_flood_count;
148 }
149
150 void
151 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
152 {
153   u32 ix = 0;
154   vnet_sw_interface_t *sw_if = vnet_get_sw_interface
155     (vnet_get_main (), member->sw_if_index);
156
157   /*
158    * Add one element to the vector
159    * vector is ordered [ bvi, normal/tun_masters..., tun_normals... no_flood]
160    * When flooding, the bvi interface (if present) must be the last member
161    * processed due to how BVI processing can change the packet. To enable
162    * this order, we make the bvi interface the first in the vector and
163    * flooding walks the vector in reverse. The flood-count determines where
164    * in the member list to start the walk from.
165    */
166   switch (sw_if->flood_class)
167     {
168     case VNET_FLOOD_CLASS_NO_FLOOD:
169       bd_config->no_flood_count++;
170       ix = vec_len (bd_config->members);
171       break;
172     case VNET_FLOOD_CLASS_BVI:
173       ix = 0;
174       break;
175     case VNET_FLOOD_CLASS_TUNNEL_MASTER:
176       bd_config->tun_master_count++;
177       /* Fall through */
178     case VNET_FLOOD_CLASS_NORMAL:
179       ix = (vec_len (bd_config->members) -
180             bd_config->tun_normal_count - bd_config->no_flood_count);
181       break;
182     case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
183       ix = (vec_len (bd_config->members) - bd_config->no_flood_count);
184       bd_config->tun_normal_count++;
185       break;
186     }
187
188   vec_insert_elts (bd_config->members, member, 1, ix);
189   update_flood_count (bd_config);
190 }
191
192 #define BD_REMOVE_ERROR_OK        0
193 #define BD_REMOVE_ERROR_NOT_FOUND 1
194
195 u32
196 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
197 {
198   u32 ix;
199
200   /* Find and delete the member */
201   vec_foreach_index (ix, bd_config->members)
202   {
203     l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
204     if (m->sw_if_index == sw_if_index)
205       {
206         vnet_sw_interface_t *sw_if = vnet_get_sw_interface
207           (vnet_get_main (), sw_if_index);
208
209         if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
210           {
211             if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
212               bd_config->tun_master_count--;
213             else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
214               bd_config->tun_normal_count--;
215             else if (sw_if->flood_class == VNET_FLOOD_CLASS_NO_FLOOD)
216               bd_config->no_flood_count--;
217           }
218         vec_delete (bd_config->members, 1, ix);
219         update_flood_count (bd_config);
220
221         return BD_REMOVE_ERROR_OK;
222       }
223   }
224
225   return BD_REMOVE_ERROR_NOT_FOUND;
226 }
227
228
229 clib_error_t *
230 l2bd_init (vlib_main_t * vm)
231 {
232   bd_main_t *bdm = &bd_main;
233   bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
234   /*
235    * create a placeholder bd with bd_id of 0 and bd_index of 0 with feature set
236    * to packet drop only. Thus, packets received from any L2 interface with
237    * uninitialized bd_index of 0 can be dropped safely.
238    */
239   u32 bd_index = bd_add_bd_index (bdm, 0);
240   ASSERT (bd_index == 0);
241   l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
242
243   bdm->vlib_main = vm;
244   return 0;
245 }
246
247 VLIB_INIT_FUNCTION (l2bd_init);
248
249 l2_bridge_domain_t *
250 bd_get (u32 bd_index)
251 {
252   if (bd_index < vec_len (l2input_main.bd_configs))
253     return (vec_elt_at_index (l2input_main.bd_configs, bd_index));
254   return (NULL);
255 }
256
257 u32
258 bd_input_walk (u32 bd_index, bd_input_walk_fn_t fn, void *data)
259 {
260   l2_flood_member_t *member;
261   l2_bridge_domain_t *bd;
262   u32 sw_if_index;
263
264   sw_if_index = ~0;
265   bd = bd_get (bd_index);
266
267   ASSERT (bd);
268
269   vec_foreach (member, bd->members)
270   {
271     if (WALK_STOP == fn (bd_index, member->sw_if_index))
272       {
273         sw_if_index = member->sw_if_index;
274         break;
275       }
276   }
277
278   return (sw_if_index);
279 }
280
281 static void
282 b2_input_recache (u32 bd_index)
283 {
284   bd_input_walk (bd_index, l2input_recache, NULL);
285 }
286
287 /**
288     Set the learn/forward/flood flags for the bridge domain.
289     Return 0 if ok, non-zero if for an error.
290 */
291 u32
292 bd_set_flags (vlib_main_t * vm, u32 bd_index, bd_flags_t flags, u32 enable)
293 {
294
295   l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
296   bd_validate (bd_config);
297   u32 feature_bitmap = 0;
298
299   if (flags & L2_LEARN)
300     {
301       feature_bitmap |= L2INPUT_FEAT_LEARN;
302     }
303   if (flags & L2_FWD)
304     {
305       feature_bitmap |= L2INPUT_FEAT_FWD;
306     }
307   if (flags & L2_FLOOD)
308     {
309       feature_bitmap |= L2INPUT_FEAT_FLOOD;
310     }
311   if (flags & L2_UU_FLOOD)
312     {
313       feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
314     }
315   if (flags & L2_ARP_TERM)
316     {
317       feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
318     }
319   if (flags & L2_ARP_UFWD)
320     {
321       feature_bitmap |= L2INPUT_FEAT_ARP_UFWD;
322     }
323
324   if (enable)
325     {
326       bd_config->feature_bitmap |= feature_bitmap;
327     }
328   else
329     {
330       bd_config->feature_bitmap &= ~feature_bitmap;
331     }
332
333   b2_input_recache (bd_index);
334
335   return bd_config->feature_bitmap;
336 }
337
338 /**
339     Set the mac age for the bridge domain.
340 */
341 void
342 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
343 {
344   l2_bridge_domain_t *bd_config;
345   int enable = 0;
346
347   vec_validate (l2input_main.bd_configs, bd_index);
348   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
349   bd_config->mac_age = age;
350   b2_input_recache (bd_index);
351
352   /* check if there is at least one bd with mac aging enabled */
353   vec_foreach (bd_config, l2input_main.bd_configs)
354     enable |= bd_config->bd_id != ~0 && bd_config->mac_age != 0;
355
356   vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
357                              enable ? L2_MAC_AGE_PROCESS_EVENT_START :
358                              L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
359 }
360
361 /**
362     Set learn limit for the bridge domain.
363 */
364 void
365 bd_set_learn_limit (vlib_main_t *vm, u32 bd_index, u32 learn_limit)
366 {
367   l2_bridge_domain_t *bd_config;
368   vec_validate (l2input_main.bd_configs, bd_index);
369   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
370   bd_config->learn_limit = learn_limit;
371 }
372
373 /**
374     Set the tag for the bridge domain.
375 */
376 static void
377 bd_set_bd_tag (vlib_main_t * vm, u32 bd_index, u8 * bd_tag)
378 {
379   u8 *old;
380   l2_bridge_domain_t *bd_config;
381   vec_validate (l2input_main.bd_configs, bd_index);
382   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
383
384   old = bd_config->bd_tag;
385
386   if (bd_tag[0])
387     {
388       bd_config->bd_tag = format (0, "%s%c", bd_tag, 0);
389     }
390   else
391     {
392       bd_config->bd_tag = NULL;
393     }
394
395   vec_free (old);
396 }
397
398 /**
399    Set bridge-domain learn enable/disable.
400    The CLI format is:
401    set bridge-domain learn <bd_id> [disable]
402 */
403 static clib_error_t *
404 bd_learn (vlib_main_t * vm,
405           unformat_input_t * input, vlib_cli_command_t * cmd)
406 {
407   bd_main_t *bdm = &bd_main;
408   clib_error_t *error = 0;
409   u32 bd_index, bd_id;
410   u32 enable;
411   uword *p;
412
413   if (!unformat (input, "%d", &bd_id))
414     {
415       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
416                                  format_unformat_error, input);
417       goto done;
418     }
419
420   if (bd_id == 0)
421     return clib_error_return (0,
422                               "No operations on the default bridge domain are supported");
423
424   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
425
426   if (p == 0)
427     return clib_error_return (0, "No such bridge domain %d", bd_id);
428
429   bd_index = p[0];
430
431   enable = 1;
432   if (unformat (input, "disable"))
433     {
434       enable = 0;
435     }
436
437   /* set the bridge domain flag */
438   bd_set_flags (vm, bd_index, L2_LEARN, enable);
439
440 done:
441   return error;
442 }
443
444 /*?
445  * Layer 2 learning can be enabled and disabled on each
446  * interface and on each bridge-domain. Use this command to
447  * manage bridge-domains. It is enabled by default.
448  *
449  * @cliexpar
450  * Example of how to enable learning (where 200 is the bridge-domain-id):
451  * @cliexcmd{set bridge-domain learn 200}
452  * Example of how to disable learning (where 200 is the bridge-domain-id):
453  * @cliexcmd{set bridge-domain learn 200 disable}
454 ?*/
455 VLIB_CLI_COMMAND (bd_learn_cli, static) = {
456   .path = "set bridge-domain learn",
457   .short_help = "set bridge-domain learn <bridge-domain-id> [disable]",
458   .function = bd_learn,
459 };
460
461 static clib_error_t *
462 bd_default_learn_limit (vlib_main_t *vm, unformat_input_t *input,
463                         vlib_cli_command_t *cmd)
464 {
465   l2learn_main_t *l2m = &l2learn_main;
466   clib_error_t *error = 0;
467   u32 learn_limit;
468
469   if (!unformat (input, "%d", &learn_limit))
470     {
471       error = clib_error_return (
472         0, "expecting per bridge-domain max entry number got`%U'",
473         format_unformat_error, input);
474       goto done;
475     }
476
477   l2m->bd_default_learn_limit = learn_limit;
478
479 done:
480   return error;
481 }
482
483 VLIB_CLI_COMMAND (bd_default_learn_limit_cli, static) = {
484   .path = "set bridge-domain default-learn-limit",
485   .short_help = "set bridge-domain default-learn-limit <maxentries>",
486   .function = bd_default_learn_limit,
487 };
488
489 /**
490     Set bridge-domain forward enable/disable.
491     The CLI format is:
492     set bridge-domain forward <bd_index> [disable]
493 */
494 static clib_error_t *
495 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
496 {
497   bd_main_t *bdm = &bd_main;
498   clib_error_t *error = 0;
499   u32 bd_index, bd_id;
500   u32 enable;
501   uword *p;
502
503   if (!unformat (input, "%d", &bd_id))
504     {
505       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
506                                  format_unformat_error, input);
507       goto done;
508     }
509
510   if (bd_id == 0)
511     return clib_error_return (0,
512                               "No operations on the default bridge domain are supported");
513
514   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
515
516   if (p == 0)
517     return clib_error_return (0, "No such bridge domain %d", bd_id);
518
519   bd_index = p[0];
520
521   enable = 1;
522   if (unformat (input, "disable"))
523     {
524       enable = 0;
525     }
526
527   /* set the bridge domain flag */
528   bd_set_flags (vm, bd_index, L2_FWD, enable);
529
530 done:
531   return error;
532 }
533
534
535 /*?
536  * Layer 2 unicast forwarding can be enabled and disabled on each
537  * interface and on each bridge-domain. Use this command to
538  * manage bridge-domains. It is enabled by default.
539  *
540  * @cliexpar
541  * Example of how to enable forwarding (where 200 is the bridge-domain-id):
542  * @cliexcmd{set bridge-domain forward 200}
543  * Example of how to disable forwarding (where 200 is the bridge-domain-id):
544  * @cliexcmd{set bridge-domain forward 200 disable}
545 ?*/
546 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
547   .path = "set bridge-domain forward",
548   .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
549   .function = bd_fwd,
550 };
551
552 /**
553     Set bridge-domain flood enable/disable.
554     The CLI format is:
555     set bridge-domain flood <bd_index> [disable]
556 */
557 static clib_error_t *
558 bd_flood (vlib_main_t * vm,
559           unformat_input_t * input, vlib_cli_command_t * cmd)
560 {
561   bd_main_t *bdm = &bd_main;
562   clib_error_t *error = 0;
563   u32 bd_index, bd_id;
564   u32 enable;
565   uword *p;
566
567   if (!unformat (input, "%d", &bd_id))
568     {
569       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
570                                  format_unformat_error, input);
571       goto done;
572     }
573
574   if (bd_id == 0)
575     return clib_error_return (0,
576                               "No operations on the default bridge domain are supported");
577
578   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
579
580   if (p == 0)
581     return clib_error_return (0, "No such bridge domain %d", bd_id);
582
583   bd_index = p[0];
584
585   enable = 1;
586   if (unformat (input, "disable"))
587     {
588       enable = 0;
589     }
590
591   /* set the bridge domain flag */
592   bd_set_flags (vm, bd_index, L2_FLOOD, enable);
593
594 done:
595   return error;
596 }
597
598 /*?
599  * Layer 2 flooding can be enabled and disabled on each
600  * interface and on each bridge-domain. Use this command to
601  * manage bridge-domains. It is enabled by default.
602  *
603  * @cliexpar
604  * Example of how to enable flooding (where 200 is the bridge-domain-id):
605  * @cliexcmd{set bridge-domain flood 200}
606  * Example of how to disable flooding (where 200 is the bridge-domain-id):
607  * @cliexcmd{set bridge-domain flood 200 disable}
608 ?*/
609 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
610   .path = "set bridge-domain flood",
611   .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
612   .function = bd_flood,
613 };
614
615 /**
616     Set bridge-domain unknown-unicast flood enable/disable.
617     The CLI format is:
618     set bridge-domain uu-flood <bd_index> [disable]
619 */
620 static clib_error_t *
621 bd_uu_flood (vlib_main_t * vm,
622              unformat_input_t * input, vlib_cli_command_t * cmd)
623 {
624   bd_main_t *bdm = &bd_main;
625   clib_error_t *error = 0;
626   u32 bd_index, bd_id;
627   u32 enable;
628   uword *p;
629
630   if (!unformat (input, "%d", &bd_id))
631     {
632       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
633                                  format_unformat_error, input);
634       goto done;
635     }
636
637   if (bd_id == 0)
638     return clib_error_return (0,
639                               "No operations on the default bridge domain are supported");
640
641   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
642
643   if (p == 0)
644     return clib_error_return (0, "No such bridge domain %d", bd_id);
645
646   bd_index = p[0];
647
648   enable = 1;
649   if (unformat (input, "disable"))
650     {
651       enable = 0;
652     }
653
654   /* set the bridge domain flag */
655   bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable);
656
657 done:
658   return error;
659 }
660
661 /*?
662  * Layer 2 unknown-unicast flooding can be enabled and disabled on each
663  * bridge-domain. It is enabled by default.
664  *
665  * @cliexpar
666  * Example of how to enable unknown-unicast flooding (where 200 is the
667  * bridge-domain-id):
668  * @cliexcmd{set bridge-domain uu-flood 200}
669  * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
670  * @cliexcmd{set bridge-domain uu-flood 200 disable}
671 ?*/
672 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
673   .path = "set bridge-domain uu-flood",
674   .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
675   .function = bd_uu_flood,
676 };
677
678 /**
679     Set bridge-domain arp-unicast forward enable/disable.
680     The CLI format is:
681     set bridge-domain arp-ufwd <bd_index> [disable]
682 */
683 static clib_error_t *
684 bd_arp_ufwd (vlib_main_t * vm,
685              unformat_input_t * input, vlib_cli_command_t * cmd)
686 {
687   bd_main_t *bdm = &bd_main;
688   clib_error_t *error = 0;
689   u32 bd_index, bd_id;
690   u32 enable;
691   uword *p;
692
693   if (!unformat (input, "%d", &bd_id))
694     {
695       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
696                                  format_unformat_error, input);
697       goto done;
698     }
699
700   if (bd_id == 0)
701     return clib_error_return (0,
702                               "No operations on the default bridge domain are supported");
703
704   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
705
706   if (p == 0)
707     return clib_error_return (0, "No such bridge domain %d", bd_id);
708
709   bd_index = p[0];
710
711   enable = 1;
712   if (unformat (input, "disable"))
713     {
714       enable = 0;
715     }
716
717   /* set the bridge domain flag */
718   bd_set_flags (vm, bd_index, L2_ARP_UFWD, enable);
719
720 done:
721   return error;
722 }
723
724 /*?
725  * Layer 2 arp-unicast forwarding can be enabled and disabled on each
726  * bridge-domain. It is disabled by default.
727  *
728  * @cliexpar
729  * Example of how to enable arp-unicast forwarding (where 200 is the
730  * bridge-domain-id):
731  * @cliexcmd{set bridge-domain arp-ufwd 200}
732  * Example of how to disable arp-unicast forwarding (where 200 is the bridge-domain-id):
733  * @cliexcmd{set bridge-domain arp-ufwd 200 disable}
734 ?*/
735 VLIB_CLI_COMMAND (bd_arp_ufwd_cli, static) = {
736   .path = "set bridge-domain arp-ufwd",
737   .short_help = "set bridge-domain arp-ufwd <bridge-domain-id> [disable]",
738   .function = bd_arp_ufwd,
739 };
740
741 /**
742     Set bridge-domain arp term enable/disable.
743     The CLI format is:
744     set bridge-domain arp term <bridge-domain-id> [disable]
745 */
746 static clib_error_t *
747 bd_arp_term (vlib_main_t * vm,
748              unformat_input_t * input, vlib_cli_command_t * cmd)
749 {
750   bd_main_t *bdm = &bd_main;
751   clib_error_t *error = 0;
752   u32 bd_index, bd_id;
753   u32 enable;
754   uword *p;
755
756   if (!unformat (input, "%d", &bd_id))
757     {
758       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
759                                  format_unformat_error, input);
760       goto done;
761     }
762
763   if (bd_id == 0)
764     return clib_error_return (0,
765                               "No operations on the default bridge domain are supported");
766
767   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
768   if (p)
769     bd_index = *p;
770   else
771     return clib_error_return (0, "No such bridge domain %d", bd_id);
772
773   enable = 1;
774   if (unformat (input, "disable"))
775     enable = 0;
776
777   /* set the bridge domain flag */
778   bd_set_flags (vm, bd_index, L2_ARP_TERM, enable);
779
780 done:
781   return error;
782 }
783
784 static clib_error_t *
785 bd_mac_age (vlib_main_t * vm,
786             unformat_input_t * input, vlib_cli_command_t * cmd)
787 {
788   bd_main_t *bdm = &bd_main;
789   clib_error_t *error = 0;
790   u32 bd_index, bd_id;
791   u32 age;
792   uword *p;
793
794   if (!unformat (input, "%d", &bd_id))
795     {
796       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
797                                  format_unformat_error, input);
798       goto done;
799     }
800
801   if (bd_id == 0)
802     return clib_error_return (0,
803                               "No operations on the default bridge domain are supported");
804
805   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
806
807   if (p == 0)
808     return clib_error_return (0, "No such bridge domain %d", bd_id);
809
810   bd_index = p[0];
811
812   if (!unformat (input, "%u", &age))
813     {
814       error =
815         clib_error_return (0, "expecting ageing time in minutes but got `%U'",
816                            format_unformat_error, input);
817       goto done;
818     }
819
820   /* set the bridge domain flag */
821   if (age > 255)
822     {
823       error =
824         clib_error_return (0, "mac aging time cannot be bigger than 255");
825       goto done;
826     }
827   bd_set_mac_age (vm, bd_index, (u8) age);
828
829 done:
830   return error;
831 }
832
833 /*?
834  * Layer 2 mac aging can be enabled and disabled on each
835  * bridge-domain. Use this command to set or disable mac aging
836  * on specific bridge-domains. It is disabled by default.
837  *
838  * @cliexpar
839  * Example of how to set mac aging (where 200 is the bridge-domain-id and
840  * 5 is aging time in minutes):
841  * @cliexcmd{set bridge-domain mac-age 200 5}
842  * Example of how to disable mac aging (where 200 is the bridge-domain-id):
843  * @cliexcmd{set bridge-domain flood 200 0}
844 ?*/
845 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
846   .path = "set bridge-domain mac-age",
847   .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
848   .function = bd_mac_age,
849 };
850
851 static clib_error_t *
852 bd_learn_limit (vlib_main_t *vm, unformat_input_t *input,
853                 vlib_cli_command_t *cmd)
854 {
855   bd_main_t *bdm = &bd_main;
856   clib_error_t *error = 0;
857   u32 bd_index, bd_id;
858   u32 learn_limit;
859   uword *p;
860
861   if (!unformat (input, "%d", &bd_id))
862     {
863       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
864                                  format_unformat_error, input);
865       goto done;
866     }
867
868   if (bd_id == 0)
869     return clib_error_return (
870       0, "No operations on the default bridge domain are supported");
871
872   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
873
874   if (p == 0)
875     return clib_error_return (0, "No such bridge domain %d", bd_id);
876
877   bd_index = p[0];
878
879   if (!unformat (input, "%u", &learn_limit))
880     {
881       error = clib_error_return (
882         0, "expecting maxium number of learned entries but got `%U'",
883         format_unformat_error, input);
884       goto done;
885     }
886
887   bd_set_learn_limit (vm, bd_index, learn_limit);
888
889 done:
890   return error;
891 }
892
893 VLIB_CLI_COMMAND (bd_learn_limit_cli, static) = {
894   .path = "set bridge-domain learn-limit",
895   .short_help =
896     "set bridge-domain learn-limit <bridge-domain-id> <learn-limit>",
897   .function = bd_learn_limit,
898 };
899
900 /*?
901  * Modify whether or not an existing bridge-domain should terminate and respond
902  * to ARP Requests. ARP Termination is disabled by default.
903  *
904  * @cliexpar
905  * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
906  * @cliexcmd{set bridge-domain arp term 200}
907  * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
908  * @cliexcmd{set bridge-domain arp term 200 disable}
909 ?*/
910 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
911   .path = "set bridge-domain arp term",
912   .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
913   .function = bd_arp_term,
914 };
915
916
917 /**
918  * Add/delete IP address to MAC address mapping.
919  *
920  * The clib hash implementation stores uword entries in the hash table.
921  * The hash table mac_by_ip4 is keyed via IP4 address and store the
922  * 6-byte MAC address directly in the hash table entry uword.
923  *
924  * @warning This only works for 64-bit processor with 8-byte uword;
925  * which means this code *WILL NOT WORK* for a 32-bit processor with
926  * 4-byte uword.
927  */
928 u32
929 bd_add_del_ip_mac (u32 bd_index,
930                    ip46_type_t type,
931                    const ip46_address_t * ip,
932                    const mac_address_t * mac, u8 is_add)
933 {
934   l2_bridge_domain_t *bd_cfg = l2input_bd_config (bd_index);
935   u64 new_mac = mac_address_as_u64 (mac);
936   u64 *old_mac;
937
938   /* make sure uword is 8 bytes */
939   ASSERT (sizeof (uword) == sizeof (u64));
940   ASSERT (bd_is_valid (bd_cfg));
941
942   if (IP46_TYPE_IP6 == type)
943     {
944       ip6_address_t *ip6_addr_key;
945       hash_pair_t *hp;
946       old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, &ip->ip6);
947       if (is_add)
948         {
949           if (old_mac == NULL)
950             {
951               /* new entry - allocate and create ip6 address key */
952               ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
953               clib_memcpy (ip6_addr_key, &ip->ip6, sizeof (ip6_address_t));
954             }
955           else if (*old_mac == new_mac)
956             {
957               /* same mac entry already exist for ip6 address */
958               return 0;
959             }
960           else
961             {
962               /* update mac for ip6 address */
963               hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
964               ip6_addr_key = (ip6_address_t *) hp->key;
965             }
966           hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
967         }
968       else
969         {
970           if (old_mac && (*old_mac == new_mac))
971             {
972               hp = hash_get_pair (bd_cfg->mac_by_ip6, &ip->ip6);
973               ip6_addr_key = (ip6_address_t *) hp->key;
974               hash_unset_mem (bd_cfg->mac_by_ip6, &ip->ip6);
975               clib_mem_free (ip6_addr_key);
976             }
977           else
978             return 1;
979         }
980     }
981   else
982     {
983       old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
984       if (is_add)
985         {
986           if (old_mac && (*old_mac == new_mac))
987             /* mac entry already exist */
988             return 0;
989           hash_set (bd_cfg->mac_by_ip4, ip->ip4.as_u32, new_mac);
990         }
991       else
992         {
993           if (old_mac && (*old_mac == new_mac))
994             hash_unset (bd_cfg->mac_by_ip4, ip->ip4.as_u32);
995           else
996             return 1;
997         }
998     }
999   return 0;
1000 }
1001
1002 /**
1003  * Flush IP address to MAC address mapping tables in a BD.
1004  */
1005 void
1006 bd_flush_ip_mac (u32 bd_index)
1007 {
1008   l2_bridge_domain_t *bd = l2input_bd_config (bd_index);
1009   ASSERT (bd_is_valid (bd));
1010   bd_free_ip_mac_tables (bd);
1011   bd->mac_by_ip4 = 0;
1012   bd->mac_by_ip6 =
1013     hash_create_mem (0, sizeof (ip6_address_t), sizeof (uword));
1014 }
1015
1016 /**
1017     Set bridge-domain arp entry add/delete.
1018     The CLI format is:
1019     set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
1020 */
1021 static clib_error_t *
1022 bd_arp_entry (vlib_main_t * vm,
1023               unformat_input_t * input, vlib_cli_command_t * cmd)
1024 {
1025   ip46_address_t ip_addr = ip46_address_initializer;
1026   ip46_type_t type = IP46_TYPE_IP4;
1027   bd_main_t *bdm = &bd_main;
1028   clib_error_t *error = 0;
1029   u32 bd_index, bd_id;
1030   mac_address_t mac;
1031   u8 is_add = 1;
1032   uword *p;
1033
1034   if (!unformat (input, "%d", &bd_id))
1035     {
1036       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
1037                                  format_unformat_error, input);
1038       goto done;
1039     }
1040
1041   if (bd_id == 0)
1042     return clib_error_return (0,
1043                               "No operations on the default bridge domain are supported");
1044
1045   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1046
1047   if (p)
1048     bd_index = *p;
1049   else
1050     return clib_error_return (0, "No such bridge domain %d", bd_id);
1051
1052   if (unformat (input, "%U", unformat_ip4_address, &ip_addr.ip4))
1053     {
1054       type = IP46_TYPE_IP4;
1055     }
1056   else if (unformat (input, "%U", unformat_ip6_address, &ip_addr.ip6))
1057     {
1058       type = IP46_TYPE_IP6;
1059     }
1060   else if (unformat (input, "del-all"))
1061     {
1062       bd_flush_ip_mac (bd_index);
1063       goto done;
1064     }
1065   else
1066     {
1067       error = clib_error_return (0, "expecting IP address but got `%U'",
1068                                  format_unformat_error, input);
1069       goto done;
1070     }
1071
1072   if (!unformat (input, "%U", unformat_mac_address_t, &mac))
1073     {
1074       error = clib_error_return (0, "expecting MAC address but got `%U'",
1075                                  format_unformat_error, input);
1076       goto done;
1077     }
1078
1079   if (unformat (input, "del"))
1080     {
1081       is_add = 0;
1082     }
1083
1084   /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
1085   if (bd_add_del_ip_mac (bd_index, type, &ip_addr, &mac, is_add))
1086     {
1087       error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
1088                                  is_add ? "add" : "del",
1089                                  format_ip46_address, &ip_addr, IP46_TYPE_ANY,
1090                                  format_mac_address_t, &mac);
1091     }
1092
1093 done:
1094   return error;
1095 }
1096
1097 /*?
1098  * Add an ARP entry to an existing bridge-domain.
1099  *
1100  * @cliexpar
1101  * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
1102  * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
1103  * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
1104  * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
1105 ?*/
1106 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
1107   .path = "set bridge-domain arp entry",
1108   .short_help = "set bridge-domain arp entry <bridge-domain-id> [<ip-addr> <mac-addr> [del] | del-all]",
1109   .function = bd_arp_entry,
1110 };
1111
1112 static u8 *
1113 format_uu_cfg (u8 * s, va_list * args)
1114 {
1115   l2_bridge_domain_t *bd_config = va_arg (*args, l2_bridge_domain_t *);
1116
1117   if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FWD)
1118     return (format (s, "%U", format_vnet_sw_if_index_name_with_NA,
1119                     vnet_get_main (), bd_config->uu_fwd_sw_if_index));
1120   else if (bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD)
1121     return (format (s, "flood"));
1122   else
1123     return (format (s, "drop"));
1124 }
1125
1126 /**
1127    Show bridge-domain state.
1128    The CLI format is:
1129    show bridge-domain [<bd_index>]
1130 */
1131 static clib_error_t *
1132 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
1133 {
1134   vnet_main_t *vnm = vnet_get_main ();
1135   bd_main_t *bdm = &bd_main;
1136   clib_error_t *error = 0;
1137   u32 bd_index = ~0;
1138   l2_bridge_domain_t *bd_config;
1139   u32 start, end;
1140   u32 detail = 0;
1141   u32 intf = 0;
1142   u32 arp = 0;
1143   u32 bd_tag = 0;
1144   u32 bd_id = ~0;
1145   uword *p;
1146
1147   start = 1;
1148   end = vec_len (l2input_main.bd_configs);
1149
1150   if (unformat (input, "%d", &bd_id))
1151     {
1152       if (unformat (input, "detail"))
1153         detail = 1;
1154       else if (unformat (input, "det"))
1155         detail = 1;
1156       if (unformat (input, "int"))
1157         intf = 1;
1158       if (unformat (input, "arp"))
1159         arp = 1;
1160       if (unformat (input, "bd-tag"))
1161         bd_tag = 1;
1162
1163       if (bd_id == 0)
1164         return clib_error_return (0,
1165                                   "No operations on the default bridge domain are supported");
1166
1167       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
1168       if (p)
1169         bd_index = *p;
1170       else
1171         return clib_error_return (0, "No such bridge domain %d", bd_id);
1172
1173       vec_validate (l2input_main.bd_configs, bd_index);
1174       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1175       if (bd_is_valid (bd_config))
1176         {
1177           start = bd_index;
1178           end = start + 1;
1179         }
1180       else
1181         {
1182           vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
1183           goto done;
1184         }
1185     }
1186
1187   /* Show all bridge-domains that have been initialized */
1188   u32 printed = 0;
1189   u8 *as = 0;
1190   for (bd_index = start; bd_index < end; bd_index++)
1191     {
1192       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
1193       if (bd_is_valid (bd_config))
1194         {
1195           if (!printed)
1196             {
1197               printed = 1;
1198               vlib_cli_output (vm,
1199                                "%=8s %=7s %=4s %=9s %=9s %=9s %=11s %=9s %=9s "
1200                                "%=9s %=8s %=8s %=11s",
1201                                "BD-ID", "Index", "BSN", "Age(min)", "Learning",
1202                                "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
1203                                "arp-ufwd", "Learn-count", "Learn-limit",
1204                                "BVI-Intf");
1205             }
1206
1207           if (bd_config->mac_age)
1208             as = format (as, "%d", bd_config->mac_age);
1209           else
1210             as = format (as, "off");
1211           vlib_cli_output (
1212             vm,
1213             "%=8d %=7d %=4d %=9v %=9s %=9s %=11U %=9s %=9s %=9s %=8d %=8d "
1214             "%=11U",
1215             bd_config->bd_id, bd_index, bd_config->seq_num, as,
1216             bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ? "on" : "off",
1217             bd_config->feature_bitmap & L2INPUT_FEAT_FWD ? "on" : "off",
1218             format_uu_cfg, bd_config,
1219             bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ? "on" : "off",
1220             bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ? "on" : "off",
1221             bd_config->feature_bitmap & L2INPUT_FEAT_ARP_UFWD ? "on" : "off",
1222             bd_config->learn_count, bd_config->learn_limit,
1223             format_vnet_sw_if_index_name_with_NA, vnm,
1224             bd_config->bvi_sw_if_index);
1225           if (detail)
1226             vlib_cli_output (vm, "%U", format_l2_input_feature_bitmap,
1227                              bd_config->feature_bitmap);
1228           vec_reset_length (as);
1229
1230           if (detail || intf)
1231             {
1232               /* Show all member interfaces */
1233               int i;
1234               vec_foreach_index (i, bd_config->members)
1235               {
1236                 l2_flood_member_t *member =
1237                   vec_elt_at_index (bd_config->members, i);
1238                 u8 swif_seq_num = l2_input_seq_num (member->sw_if_index);
1239                 u32 vtr_opr, dot1q, tag1, tag2;
1240                 if (i == 0)
1241                   {
1242                     vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1243                                      "Interface", "If-idx", "ISN", "SHG",
1244                                      "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1245                   }
1246                 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1247                            &tag1, &tag2);
1248                 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1249                                  format_vnet_sw_if_index_name, vnm,
1250                                  member->sw_if_index, member->sw_if_index,
1251                                  swif_seq_num, member->shg,
1252                                  member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1253                                  "-", i < bd_config->flood_count ? "*" : "-",
1254                                  format_vtr, vtr_opr, dot1q, tag1, tag2);
1255               }
1256               if (~0 != bd_config->uu_fwd_sw_if_index)
1257                 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30s",
1258                                  format_vnet_sw_if_index_name, vnm,
1259                                  bd_config->uu_fwd_sw_if_index,
1260                                  bd_config->uu_fwd_sw_if_index,
1261                                  0, 0, "uu", "-", "None");
1262
1263             }
1264
1265           if ((detail || arp) &&
1266               (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1267             {
1268               u32 ip4_addr;
1269               ip6_address_t *ip6_addr;
1270               u64 mac_addr;
1271               vlib_cli_output (vm,
1272                                "\n  IP4/IP6 to MAC table for ARP Termination");
1273
1274               hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1275               ({
1276                 vlib_cli_output (vm, "%=40U => %=20U",
1277                                  format_ip4_address, &ip4_addr,
1278                                  format_ethernet_address, &mac_addr);
1279               }));
1280
1281               hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1282               ({
1283                 vlib_cli_output (vm, "%=40U => %=20U",
1284                                  format_ip6_address, ip6_addr,
1285                                  format_ethernet_address, &mac_addr);
1286               }));
1287             }
1288
1289           if ((detail || bd_tag) && (bd_config->bd_tag))
1290             {
1291               vlib_cli_output (vm, "\n  BD-Tag: %s", bd_config->bd_tag);
1292
1293             }
1294         }
1295     }
1296   vec_free (as);
1297
1298   if (!printed)
1299     {
1300       vlib_cli_output (vm, "no bridge-domains in use");
1301     }
1302
1303 done:
1304   return error;
1305 }
1306
1307 /*?
1308  * Show a summary of all the bridge-domain instances or detailed view of a
1309  * single bridge-domain. Bridge-domains are created by adding an interface
1310  * to a bridge using the '<em>set interface l2 bridge</em>' command.
1311  *
1312  * @cliexpar
1313  * @parblock
1314  * Example of displaying all bridge-domains:
1315  * @cliexstart{show bridge-domain}
1316  *  ID   Index   Learning   U-Forwrd   UU-Flood   Flooding   ARP-Term     BVI-Intf
1317  *  0      0        off        off        off        off        off        local0
1318  * 200     1        on         on         on         on         off          N/A
1319  * @cliexend
1320  *
1321  * Example of displaying details of a single bridge-domains:
1322  * @cliexstart{show bridge-domain 200 detail}
1323  *  ID   Index   Learning   U-Forwrd   UU-Flood   Flooding   ARP-Term     BVI-Intf
1324  * 200     1        on         on         on         on         off          N/A
1325  *
1326  *          Interface           Index  SHG  BVI        VLAN-Tag-Rewrite
1327  *  GigabitEthernet0/8/0.200      3     0    -               none
1328  *  GigabitEthernet0/9/0.200      4     0    -               none
1329  * @cliexend
1330  * @endparblock
1331 ?*/
1332 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1333   .path = "show bridge-domain",
1334   .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp|bd-tag]]",
1335   .function = bd_show,
1336 };
1337
1338 int
1339 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1340 {
1341   bd_main_t *bdm = &bd_main;
1342   l2fib_main_t *fm = &l2fib_main;
1343   vlib_main_t *vm = bdm->vlib_main;
1344   int rv = 0;
1345
1346   if (fm->mac_table_initialized == 0)
1347     l2fib_table_init ();
1348
1349   u32 bd_index = bd_find_index (bdm, a->bd_id);
1350   if (a->is_add)
1351     {
1352       if (bd_index != ~0)
1353         return VNET_API_ERROR_BD_ALREADY_EXISTS;
1354       if (a->bd_id > L2_BD_ID_MAX)
1355         return VNET_API_ERROR_BD_ID_EXCEED_MAX;
1356       bd_index = bd_add_bd_index (bdm, a->bd_id);
1357
1358       bd_flags_t enable_flags = 0, disable_flags = 0;
1359       if (a->flood)
1360         enable_flags |= L2_FLOOD;
1361       else
1362         disable_flags |= L2_FLOOD;
1363
1364       if (a->uu_flood)
1365         enable_flags |= L2_UU_FLOOD;
1366       else
1367         disable_flags |= L2_UU_FLOOD;
1368
1369       if (a->forward)
1370         enable_flags |= L2_FWD;
1371       else
1372         disable_flags |= L2_FWD;
1373
1374       if (a->learn)
1375         enable_flags |= L2_LEARN;
1376       else
1377         disable_flags |= L2_LEARN;
1378
1379       if (a->arp_term)
1380         enable_flags |= L2_ARP_TERM;
1381       else
1382         disable_flags |= L2_ARP_TERM;
1383
1384       if (a->arp_ufwd)
1385         enable_flags |= L2_ARP_UFWD;
1386       else
1387         disable_flags |= L2_ARP_UFWD;
1388
1389       if (enable_flags)
1390         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1391
1392       if (disable_flags)
1393         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1394
1395       bd_set_mac_age (vm, bd_index, a->mac_age);
1396
1397       if (a->bd_tag)
1398         bd_set_bd_tag (vm, bd_index, a->bd_tag);
1399
1400       bd_set_learn_limit (vm, bd_index, l2learn_main.bd_default_learn_limit);
1401       vec_elt_at_index (l2input_main.bd_configs, bd_index)->learn_count = 0;
1402     }
1403   else
1404     {
1405       if (bd_index == ~0)
1406         return VNET_API_ERROR_NO_SUCH_ENTRY;
1407       if (bd_index == 0)
1408         return VNET_API_ERROR_BD_NOT_MODIFIABLE;
1409       if (vec_len (l2input_main.bd_configs[bd_index].members))
1410         return VNET_API_ERROR_BD_IN_USE;
1411       rv = bd_delete (bdm, bd_index);
1412     }
1413
1414   return rv;
1415 }
1416
1417 /**
1418    Create or delete bridge-domain.
1419    The CLI format:
1420    create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>]
1421                                         [arp-term <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]
1422 */
1423
1424 static clib_error_t *
1425 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1426                        vlib_cli_command_t * cmd)
1427 {
1428   unformat_input_t _line_input, *line_input = &_line_input;
1429   clib_error_t *error = 0;
1430   u8 is_add = 1;
1431   u32 bd_id = ~0;
1432   u32 flood = 1, forward = 1, learn = 1, uu_flood = 1, arp_term =
1433     0, arp_ufwd = 0;
1434   u32 mac_age = 0;
1435   u8 *bd_tag = NULL;
1436   l2_bridge_domain_add_del_args_t _a, *a = &_a;
1437   int rv;
1438
1439   /* Get a line of input. */
1440   if (!unformat_user (input, unformat_line_input, line_input))
1441     return 0;
1442
1443   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1444     {
1445       if (unformat (line_input, "%d", &bd_id))
1446         ;
1447       else if (unformat (line_input, "flood %d", &flood))
1448         ;
1449       else if (unformat (line_input, "uu-flood %d", &uu_flood))
1450         ;
1451       else if (unformat (line_input, "forward %d", &forward))
1452         ;
1453       else if (unformat (line_input, "learn %d", &learn))
1454         ;
1455       else if (unformat (line_input, "arp-term %d", &arp_term))
1456         ;
1457       else if (unformat (line_input, "arp-ufwd %d", &arp_ufwd))
1458         ;
1459       else if (unformat (line_input, "mac-age %d", &mac_age))
1460         ;
1461       else if (unformat (line_input, "bd-tag %s", &bd_tag))
1462         ;
1463       else if (unformat (line_input, "del"))
1464         {
1465           is_add = 0;
1466           flood = uu_flood = forward = learn = 0;
1467         }
1468       else
1469         break;
1470     }
1471
1472   if (bd_id == ~0)
1473     {
1474       if (is_add)
1475         {
1476           bd_id = bd_get_unused_id ();
1477         }
1478       else
1479         {
1480           error = clib_error_return (0, "bridge-domain-id not specified");
1481           goto done;
1482         }
1483     }
1484
1485   if (bd_id == 0)
1486     {
1487       error = clib_error_return (0, "bridge domain 0 can not be modified");
1488       goto done;
1489     }
1490
1491   if (mac_age > 255)
1492     {
1493       error = clib_error_return (0, "mac age must be less than 256");
1494       goto done;
1495     }
1496   if ((bd_tag) && (strlen ((char *) bd_tag) > 63))
1497     {
1498       error = clib_error_return (0, "bd-tag cannot be longer than 63");
1499       goto done;
1500     }
1501
1502   clib_memset (a, 0, sizeof (*a));
1503   a->is_add = is_add;
1504   a->bd_id = bd_id;
1505   a->flood = (u8) flood;
1506   a->uu_flood = (u8) uu_flood;
1507   a->forward = (u8) forward;
1508   a->learn = (u8) learn;
1509   a->arp_term = (u8) arp_term;
1510   a->arp_ufwd = (u8) arp_ufwd;
1511   a->mac_age = (u8) mac_age;
1512   a->bd_tag = bd_tag;
1513
1514   rv = bd_add_del (a);
1515
1516   switch (rv)
1517     {
1518     case 0:
1519       if (is_add)
1520         vlib_cli_output (vm, "bridge-domain %d", bd_id);
1521       break;
1522     case VNET_API_ERROR_BD_IN_USE:
1523       error = clib_error_return (0, "bridge domain in use - remove members");
1524       goto done;
1525     case VNET_API_ERROR_NO_SUCH_ENTRY:
1526       error = clib_error_return (0, "bridge domain ID does not exist");
1527       goto done;
1528     case VNET_API_ERROR_BD_NOT_MODIFIABLE:
1529       error = clib_error_return (0, "bridge domain 0 can not be modified");
1530       goto done;
1531     case VNET_API_ERROR_BD_ID_EXCEED_MAX:
1532       error = clib_error_return (0, "bridge domain ID exceed 16M limit");
1533       goto done;
1534     default:
1535       error = clib_error_return (0, "bd_add_del returned %d", rv);
1536       goto done;
1537     }
1538
1539 done:
1540   vec_free (bd_tag);
1541   unformat_free (line_input);
1542
1543   return error;
1544 }
1545
1546
1547 /*?
1548  * Create/Delete bridge-domain instance
1549  *
1550  * @cliexpar
1551  * @parblock
1552  * Example of creating bridge-domain 1:
1553  * @cliexstart{create bridge-domain 1}
1554  * bridge-domain 1
1555  * @cliexend
1556  *
1557  * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1558  * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1559  * bridge-domain 2
1560  *
1561  * vpp# show bridge-domain
1562  *   ID   Index   BSN  Age(min)  Learning  U-Forwrd  UU-Flood  Flooding  ARP-Term  BVI-Intf
1563  *   0      0      0     off       off       off       off       off       off      local0
1564  *   1      1      0     off        on        on       off        on       off       N/A
1565  *   2      2      0      60        on        on       off        on        on       N/A
1566  *
1567  * @cliexend
1568  *
1569  * Example of delete bridge-domain 1:
1570  * @cliexstart{create bridge-domain 1 del}
1571  * @cliexend
1572  * @endparblock
1573 ?*/
1574
1575 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1576   .path = "create bridge-domain",
1577   .short_help = "create bridge-domain <bridge-domain-id>"
1578                 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1579                 " [arp-ufwd <0|1>] [mac-age <nn>] [bd-tag <tag>] [del]",
1580   .function = bd_add_del_command_fn,
1581 };
1582
1583 /*
1584  * Returns an unused bridge domain id, and ~0 if it can't find one.
1585  */
1586 u32
1587 bd_get_unused_id (void)
1588 {
1589   bd_main_t *bdm = &bd_main;
1590   int i, j;
1591   static u32 seed = 0;
1592
1593   /* limit to 1M tries */
1594   for (j = 0; j < 1 << 10; j++)
1595     {
1596       seed = random_u32 (&seed);
1597       for (i = 0; i < 1 << 10; i++)
1598         {
1599           /*
1600            * iterate seed+0, seed+1, seed-1, seed+2, seed-2, ... to generate id
1601            */
1602           seed += (2 * (i % 2) - 1) * i;
1603           /* bd_id must be (1 <= bd_id <= L2_BD_ID_MAX) */
1604           seed &= L2_BD_ID_MAX;
1605           if (seed == 0)
1606             continue;
1607           if (bd_find_index (bdm, seed) == ~0)
1608             return seed;
1609         }
1610     }
1611
1612   return ~0;
1613 }
1614
1615 /*
1616  * fd.io coding-style-patch-verification: ON
1617  *
1618  * Local Variables:
1619  * eval: (c-set-style "gnu")
1620  * End:
1621  */