CLI: create/delete bridge-domain
[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     {
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));
64     }
65 }
66
67 u32
68 bd_find_or_add_bd_index (bd_main_t * bdm, u32 bd_id)
69 {
70   uword *p;
71   u32 rv;
72
73   if (bd_id == ~0)
74     {
75       bd_id = 0;
76       while (hash_get (bdm->bd_index_by_bd_id, bd_id))
77         bd_id++;
78     }
79   else
80     {
81       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
82       if (p)
83         return (p[0]);
84     }
85
86   rv = clib_bitmap_first_clear (bdm->bd_index_bitmap);
87
88   /* mark this index busy */
89   bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, rv, 1);
90
91   hash_set (bdm->bd_index_by_bd_id, bd_id, rv);
92
93   vec_validate (l2input_main.bd_configs, rv);
94   l2input_main.bd_configs[rv].bd_id = bd_id;
95
96   return rv;
97 }
98
99 int
100 bd_delete_bd_index (bd_main_t * bdm, u32 bd_id)
101 {
102   uword *p;
103   u32 bd_index;
104
105   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
106   if (p == 0)
107     return VNET_API_ERROR_NO_SUCH_ENTRY;
108
109   bd_index = p[0];
110
111   /* mark this index clear */
112   bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap, bd_index, 0);
113   hash_unset (bdm->bd_index_by_bd_id, bd_id);
114
115   l2input_main.bd_configs[bd_index].bd_id = ~0;
116   l2input_main.bd_configs[bd_index].feature_bitmap = 0;
117
118   l2fib_flush_bd_mac (vlib_get_main (), bd_index);
119
120   return 0;
121 }
122
123 static void
124 update_flood_count (l2_bridge_domain_t * bd_config)
125 {
126   bd_config->flood_count = vec_len (bd_config->members) -
127     (bd_config->tun_master_count ? bd_config->tun_normal_count : 0);
128 }
129
130 void
131 bd_add_member (l2_bridge_domain_t * bd_config, l2_flood_member_t * member)
132 {
133   u32 ix;
134   vnet_sw_interface_t *sw_if = vnet_get_sw_interface
135     (vnet_get_main (), member->sw_if_index);
136
137   /*
138    * Add one element to the vector
139    * vector is ordered [ bvi, normal/tun_masters..., tun_normals... ]
140    * When flooding, the bvi interface (if present) must be the last member
141    * processed due to how BVI processing can change the packet. To enable
142    * this order, we make the bvi interface the first in the vector and
143    * flooding walks the vector in reverse.
144    */
145   switch (sw_if->flood_class)
146     {
147     case VNET_FLOOD_CLASS_TUNNEL_MASTER:
148       bd_config->tun_master_count++;
149       /* Fall through */
150     default:
151       /* Fall through */
152     case VNET_FLOOD_CLASS_NORMAL:
153       ix = (member->flags & L2_FLOOD_MEMBER_BVI) ? 0 :
154         vec_len (bd_config->members) - bd_config->tun_normal_count;
155       break;
156     case VNET_FLOOD_CLASS_TUNNEL_NORMAL:
157       ix = vec_len (bd_config->members);
158       bd_config->tun_normal_count++;
159       break;
160     }
161
162   vec_insert_elts (bd_config->members, member, 1, ix);
163   update_flood_count (bd_config);
164 }
165
166 #define BD_REMOVE_ERROR_OK        0
167 #define BD_REMOVE_ERROR_NOT_FOUND 1
168
169 u32
170 bd_remove_member (l2_bridge_domain_t * bd_config, u32 sw_if_index)
171 {
172   u32 ix;
173
174   /* Find and delete the member */
175   vec_foreach_index (ix, bd_config->members)
176   {
177     l2_flood_member_t *m = vec_elt_at_index (bd_config->members, ix);
178     if (m->sw_if_index == sw_if_index)
179       {
180         vnet_sw_interface_t *sw_if = vnet_get_sw_interface
181           (vnet_get_main (), sw_if_index);
182
183         if (sw_if->flood_class != VNET_FLOOD_CLASS_NORMAL)
184           {
185             if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_MASTER)
186               bd_config->tun_master_count--;
187             else if (sw_if->flood_class == VNET_FLOOD_CLASS_TUNNEL_NORMAL)
188               bd_config->tun_normal_count--;
189           }
190         vec_delete (bd_config->members, 1, ix);
191         update_flood_count (bd_config);
192
193         return BD_REMOVE_ERROR_OK;
194       }
195   }
196
197   return BD_REMOVE_ERROR_NOT_FOUND;
198 }
199
200
201 clib_error_t *
202 l2bd_init (vlib_main_t * vm)
203 {
204   bd_main_t *bdm = &bd_main;
205   u32 bd_index;
206   bdm->bd_index_by_bd_id = hash_create (0, sizeof (uword));
207   /*
208    * create a dummy bd with bd_id of 0 and bd_index of 0 with feature set
209    * to packet drop only. Thus, packets received from any L2 interface with
210    * uninitialized bd_index of 0 can be dropped safely.
211    */
212   bd_index = bd_find_or_add_bd_index (bdm, 0);
213   ASSERT (bd_index == 0);
214   l2input_main.bd_configs[0].feature_bitmap = L2INPUT_FEAT_DROP;
215
216   bdm->vlib_main = vm;
217   return 0;
218 }
219
220 VLIB_INIT_FUNCTION (l2bd_init);
221
222
223 /**
224     Set the learn/forward/flood flags for the bridge domain.
225     Return 0 if ok, non-zero if for an error.
226 */
227 u32
228 bd_set_flags (vlib_main_t * vm, u32 bd_index, u32 flags, u32 enable)
229 {
230
231   l2_bridge_domain_t *bd_config;
232   u32 feature_bitmap = 0;
233
234   vec_validate (l2input_main.bd_configs, bd_index);
235   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
236
237   bd_validate (bd_config);
238
239   if (flags & L2_LEARN)
240     {
241       feature_bitmap |= L2INPUT_FEAT_LEARN;
242     }
243   if (flags & L2_FWD)
244     {
245       feature_bitmap |= L2INPUT_FEAT_FWD;
246     }
247   if (flags & L2_FLOOD)
248     {
249       feature_bitmap |= L2INPUT_FEAT_FLOOD;
250     }
251   if (flags & L2_UU_FLOOD)
252     {
253       feature_bitmap |= L2INPUT_FEAT_UU_FLOOD;
254     }
255   if (flags & L2_ARP_TERM)
256     {
257       feature_bitmap |= L2INPUT_FEAT_ARP_TERM;
258     }
259
260   if (enable)
261     {
262       bd_config->feature_bitmap |= feature_bitmap;
263     }
264   else
265     {
266       bd_config->feature_bitmap &= ~feature_bitmap;
267     }
268
269   return 0;
270 }
271
272 /**
273     Set the mac age for the bridge domain.
274 */
275 void
276 bd_set_mac_age (vlib_main_t * vm, u32 bd_index, u8 age)
277 {
278   l2_bridge_domain_t *bd_config;
279   int enable = 0;
280
281   vec_validate (l2input_main.bd_configs, bd_index);
282   bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
283   bd_config->mac_age = age;
284
285   /* check if there is at least one bd with mac aging enabled */
286   vec_foreach (bd_config, l2input_main.bd_configs)
287     if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
288     enable = 1;
289
290   vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
291                              enable ? L2_MAC_AGE_PROCESS_EVENT_START :
292                              L2_MAC_AGE_PROCESS_EVENT_STOP, 0);
293 }
294
295 /**
296    Set bridge-domain learn enable/disable.
297    The CLI format is:
298    set bridge-domain learn <bd_id> [disable]
299 */
300 static clib_error_t *
301 bd_learn (vlib_main_t * vm,
302           unformat_input_t * input, vlib_cli_command_t * cmd)
303 {
304   bd_main_t *bdm = &bd_main;
305   clib_error_t *error = 0;
306   u32 bd_index, bd_id;
307   u32 enable;
308   uword *p;
309
310   if (!unformat (input, "%d", &bd_id))
311     {
312       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
313                                  format_unformat_error, input);
314       goto done;
315     }
316
317   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);
321
322   bd_index = p[0];
323
324   enable = 1;
325   if (unformat (input, "disable"))
326     {
327       enable = 0;
328     }
329
330   /* 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     }
337
338 done:
339   return error;
340 }
341
342 /*?
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 ?*/
353 /* *INDENT-OFF* */
354 VLIB_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 };
359 /* *INDENT-ON* */
360
361 /**
362     Set bridge-domain forward enable/disable.
363     The CLI format is:
364     set bridge-domain forward <bd_index> [disable]
365 */
366 static clib_error_t *
367 bd_fwd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
368 {
369   bd_main_t *bdm = &bd_main;
370   clib_error_t *error = 0;
371   u32 bd_index, bd_id;
372   u32 enable;
373   uword *p;
374
375   if (!unformat (input, "%d", &bd_id))
376     {
377       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
378                                  format_unformat_error, input);
379       goto done;
380     }
381
382   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
383
384   if (p == 0)
385     return clib_error_return (0, "No such bridge domain %d", bd_id);
386
387   bd_index = p[0];
388
389   enable = 1;
390   if (unformat (input, "disable"))
391     {
392       enable = 0;
393     }
394
395   /* set the bridge domain flag */
396   if (bd_set_flags (vm, bd_index, L2_FWD, enable))
397     {
398       error =
399         clib_error_return (0, "bridge-domain id %d out of range", bd_index);
400       goto done;
401     }
402
403 done:
404   return error;
405 }
406
407
408 /*?
409  * Layer 2 unicast forwarding can be enabled and disabled on each
410  * interface and on each bridge-domain. Use this command to
411  * manage bridge-domains. It is enabled by default.
412  *
413  * @cliexpar
414  * Example of how to enable forwarding (where 200 is the bridge-domain-id):
415  * @cliexcmd{set bridge-domain forward 200}
416  * Example of how to disable forwarding (where 200 is the bridge-domain-id):
417  * @cliexcmd{set bridge-domain forward 200 disable}
418 ?*/
419 /* *INDENT-OFF* */
420 VLIB_CLI_COMMAND (bd_fwd_cli, static) = {
421   .path = "set bridge-domain forward",
422   .short_help = "set bridge-domain forward <bridge-domain-id> [disable]",
423   .function = bd_fwd,
424 };
425 /* *INDENT-ON* */
426
427 /**
428     Set bridge-domain flood enable/disable.
429     The CLI format is:
430     set bridge-domain flood <bd_index> [disable]
431 */
432 static clib_error_t *
433 bd_flood (vlib_main_t * vm,
434           unformat_input_t * input, vlib_cli_command_t * cmd)
435 {
436   bd_main_t *bdm = &bd_main;
437   clib_error_t *error = 0;
438   u32 bd_index, bd_id;
439   u32 enable;
440   uword *p;
441
442   if (!unformat (input, "%d", &bd_id))
443     {
444       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
445                                  format_unformat_error, input);
446       goto done;
447     }
448
449   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
450
451   if (p == 0)
452     return clib_error_return (0, "No such bridge domain %d", bd_id);
453
454   bd_index = p[0];
455
456   enable = 1;
457   if (unformat (input, "disable"))
458     {
459       enable = 0;
460     }
461
462   /* set the bridge domain flag */
463   if (bd_set_flags (vm, bd_index, L2_FLOOD, enable))
464     {
465       error =
466         clib_error_return (0, "bridge-domain id %d out of range", bd_index);
467       goto done;
468     }
469
470 done:
471   return error;
472 }
473
474 /*?
475  * Layer 2 flooding can be enabled and disabled on each
476  * interface and on each bridge-domain. Use this command to
477  * manage bridge-domains. It is enabled by default.
478  *
479  * @cliexpar
480  * Example of how to enable flooding (where 200 is the bridge-domain-id):
481  * @cliexcmd{set bridge-domain flood 200}
482  * Example of how to disable flooding (where 200 is the bridge-domain-id):
483  * @cliexcmd{set bridge-domain flood 200 disable}
484 ?*/
485 /* *INDENT-OFF* */
486 VLIB_CLI_COMMAND (bd_flood_cli, static) = {
487   .path = "set bridge-domain flood",
488   .short_help = "set bridge-domain flood <bridge-domain-id> [disable]",
489   .function = bd_flood,
490 };
491 /* *INDENT-ON* */
492
493 /**
494     Set bridge-domain unkown-unicast flood enable/disable.
495     The CLI format is:
496     set bridge-domain uu-flood <bd_index> [disable]
497 */
498 static clib_error_t *
499 bd_uu_flood (vlib_main_t * vm,
500              unformat_input_t * input, vlib_cli_command_t * cmd)
501 {
502   bd_main_t *bdm = &bd_main;
503   clib_error_t *error = 0;
504   u32 bd_index, bd_id;
505   u32 enable;
506   uword *p;
507
508   if (!unformat (input, "%d", &bd_id))
509     {
510       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
511                                  format_unformat_error, input);
512       goto done;
513     }
514
515   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
516
517   if (p == 0)
518     return clib_error_return (0, "No such bridge domain %d", bd_id);
519
520   bd_index = p[0];
521
522   enable = 1;
523   if (unformat (input, "disable"))
524     {
525       enable = 0;
526     }
527
528   /* set the bridge domain flag */
529   if (bd_set_flags (vm, bd_index, L2_UU_FLOOD, enable))
530     {
531       error =
532         clib_error_return (0, "bridge-domain id %d out of range", bd_index);
533       goto done;
534     }
535
536 done:
537   return error;
538 }
539
540 /*?
541  * Layer 2 unknown-unicast flooding can be enabled and disabled on each
542  * bridge-domain. It is enabled by default.
543  *
544  * @cliexpar
545  * Example of how to enable unknown-unicast flooding (where 200 is the
546  * bridge-domain-id):
547  * @cliexcmd{set bridge-domain uu-flood 200}
548  * Example of how to disable unknown-unicast flooding (where 200 is the bridge-domain-id):
549  * @cliexcmd{set bridge-domain uu-flood 200 disable}
550 ?*/
551 /* *INDENT-OFF* */
552 VLIB_CLI_COMMAND (bd_uu_flood_cli, static) = {
553   .path = "set bridge-domain uu-flood",
554   .short_help = "set bridge-domain uu-flood <bridge-domain-id> [disable]",
555   .function = bd_uu_flood,
556 };
557 /* *INDENT-ON* */
558
559 /**
560     Set bridge-domain arp term enable/disable.
561     The CLI format is:
562     set bridge-domain arp term <bridge-domain-id> [disable]
563 */
564 static clib_error_t *
565 bd_arp_term (vlib_main_t * vm,
566              unformat_input_t * input, vlib_cli_command_t * cmd)
567 {
568   bd_main_t *bdm = &bd_main;
569   clib_error_t *error = 0;
570   u32 bd_index, bd_id;
571   u32 enable;
572   uword *p;
573
574   if (!unformat (input, "%d", &bd_id))
575     {
576       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
577                                  format_unformat_error, input);
578       goto done;
579     }
580
581   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
582   if (p)
583     bd_index = *p;
584   else
585     return clib_error_return (0, "No such bridge domain %d", bd_id);
586
587   enable = 1;
588   if (unformat (input, "disable"))
589     enable = 0;
590
591   /* set the bridge domain flag */
592   if (bd_set_flags (vm, bd_index, L2_ARP_TERM, enable))
593     {
594       error =
595         clib_error_return (0, "bridge-domain id %d out of range", bd_index);
596       goto done;
597     }
598
599 done:
600   return error;
601 }
602
603 static clib_error_t *
604 bd_mac_age (vlib_main_t * vm,
605             unformat_input_t * input, vlib_cli_command_t * cmd)
606 {
607   bd_main_t *bdm = &bd_main;
608   clib_error_t *error = 0;
609   u32 bd_index, bd_id;
610   u32 age;
611   uword *p;
612
613   if (!unformat (input, "%d", &bd_id))
614     {
615       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
616                                  format_unformat_error, input);
617       goto done;
618     }
619
620   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
621
622   if (p == 0)
623     return clib_error_return (0, "No such bridge domain %d", bd_id);
624
625   bd_index = p[0];
626
627   if (!unformat (input, "%u", &age))
628     {
629       error =
630         clib_error_return (0, "expecting ageing time in minutes but got `%U'",
631                            format_unformat_error, input);
632       goto done;
633     }
634
635   /* set the bridge domain flag */
636   if (age > 255)
637     {
638       error =
639         clib_error_return (0, "mac aging time cannot be bigger than 255");
640       goto done;
641     }
642   bd_set_mac_age (vm, bd_index, (u8) age);
643
644 done:
645   return error;
646 }
647
648 /*?
649  * Layer 2 mac aging can be enabled and disabled on each
650  * bridge-domain. Use this command to set or disable mac aging
651  * on specific bridge-domains. It is disabled by default.
652  *
653  * @cliexpar
654  * Example of how to set mac aging (where 200 is the bridge-domain-id and
655  * 5 is aging time in minutes):
656  * @cliexcmd{set bridge-domain mac-age 200 5}
657  * Example of how to disable mac aging (where 200 is the bridge-domain-id):
658  * @cliexcmd{set bridge-domain flood 200 0}
659 ?*/
660 /* *INDENT-OFF* */
661 VLIB_CLI_COMMAND (bd_mac_age_cli, static) = {
662   .path = "set bridge-domain mac-age",
663   .short_help = "set bridge-domain mac-age <bridge-domain-id> <mins>",
664   .function = bd_mac_age,
665 };
666 /* *INDENT-ON* */
667
668 /*?
669  * Modify whether or not an existing bridge-domain should terminate and respond
670  * to ARP Requests. ARP Termination is disabled by default.
671  *
672  * @cliexpar
673  * Example of how to enable ARP termination (where 200 is the bridge-domain-id):
674  * @cliexcmd{set bridge-domain arp term 200}
675  * Example of how to disable ARP termination (where 200 is the bridge-domain-id):
676  * @cliexcmd{set bridge-domain arp term 200 disable}
677 ?*/
678 /* *INDENT-OFF* */
679 VLIB_CLI_COMMAND (bd_arp_term_cli, static) = {
680   .path = "set bridge-domain arp term",
681   .short_help = "set bridge-domain arp term <bridge-domain-id> [disable]",
682   .function = bd_arp_term,
683 };
684 /* *INDENT-ON* */
685
686
687 /**
688  * Add/delete IP address to MAC address mapping.
689  *
690  * The clib hash implementation stores uword entries in the hash table.
691  * The hash table mac_by_ip4 is keyed via IP4 address and store the
692  * 6-byte MAC address directly in the hash table entry uword.
693  *
694  * @warning This only works for 64-bit processor with 8-byte uword;
695  * which means this code *WILL NOT WORK* for a 32-bit prcessor with
696  * 4-byte uword.
697  */
698 u32
699 bd_add_del_ip_mac (u32 bd_index,
700                    u8 * ip_addr, u8 * mac_addr, u8 is_ip6, u8 is_add)
701 {
702   l2input_main_t *l2im = &l2input_main;
703   l2_bridge_domain_t *bd_cfg = l2input_bd_config_from_index (l2im, bd_index);
704   u64 new_mac = *(u64 *) mac_addr;
705   u64 *old_mac;
706   u16 *mac16 = (u16 *) & new_mac;
707
708   ASSERT (sizeof (uword) == sizeof (u64));      /* make sure uword is 8 bytes */
709
710   mac16[3] = 0;                 /* Clear last 2 unsed bytes of the 8-byte MAC address */
711   if (is_ip6)
712     {
713       ip6_address_t *ip6_addr_key;
714       hash_pair_t *hp;
715       old_mac = (u64 *) hash_get_mem (bd_cfg->mac_by_ip6, ip_addr);
716       if (is_add)
717         {
718           if (old_mac == 0)
719             {                   /* new entry - allocate and craete ip6 address key */
720               ip6_addr_key = clib_mem_alloc (sizeof (ip6_address_t));
721               clib_memcpy (ip6_addr_key, ip_addr, sizeof (ip6_address_t));
722             }
723           else if (*old_mac == new_mac)
724             {                   /* same mac entry already exist for ip6 address */
725               return 0;
726             }
727           else
728             {                   /* updat mac for ip6 address */
729               hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
730               ip6_addr_key = (ip6_address_t *) hp->key;
731             }
732           hash_set_mem (bd_cfg->mac_by_ip6, ip6_addr_key, new_mac);
733         }
734       else
735         {
736           if (old_mac && (*old_mac == new_mac))
737             {
738               hp = hash_get_pair (bd_cfg->mac_by_ip6, ip_addr);
739               ip6_addr_key = (ip6_address_t *) hp->key;
740               hash_unset_mem (bd_cfg->mac_by_ip6, ip_addr);
741               clib_mem_free (ip6_addr_key);
742             }
743           else
744             return 1;
745         }
746     }
747   else
748     {
749       ip4_address_t ip4_addr = *(ip4_address_t *) ip_addr;
750       old_mac = (u64 *) hash_get (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
751       if (is_add)
752         {
753           if (old_mac && (*old_mac == new_mac))
754             return 0;           /* mac entry already exist */
755           hash_set (bd_cfg->mac_by_ip4, ip4_addr.as_u32, new_mac);
756         }
757       else
758         {
759           if (old_mac && (*old_mac == new_mac))
760             hash_unset (bd_cfg->mac_by_ip4, ip4_addr.as_u32);
761           else
762             return 1;
763         }
764     }
765   return 0;
766 }
767
768 /**
769     Set bridge-domain arp entry add/delete.
770     The CLI format is:
771     set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]
772 */
773 static clib_error_t *
774 bd_arp_entry (vlib_main_t * vm,
775               unformat_input_t * input, vlib_cli_command_t * cmd)
776 {
777   bd_main_t *bdm = &bd_main;
778   clib_error_t *error = 0;
779   u32 bd_index, bd_id;
780   u8 is_add = 1;
781   u8 is_ip6 = 0;
782   u8 ip_addr[16];
783   u8 mac_addr[6];
784   uword *p;
785
786   if (!unformat (input, "%d", &bd_id))
787     {
788       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
789                                  format_unformat_error, input);
790       goto done;
791     }
792
793   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
794
795   if (p)
796     bd_index = *p;
797   else
798     return clib_error_return (0, "No such bridge domain %d", bd_id);
799
800   if (unformat (input, "%U", unformat_ip4_address, ip_addr))
801     {
802       is_ip6 = 0;
803     }
804   else if (unformat (input, "%U", unformat_ip6_address, ip_addr))
805     {
806       is_ip6 = 1;
807     }
808   else
809     {
810       error = clib_error_return (0, "expecting IP address but got `%U'",
811                                  format_unformat_error, input);
812       goto done;
813     }
814
815   if (!unformat (input, "%U", unformat_ethernet_address, mac_addr))
816     {
817       error = clib_error_return (0, "expecting MAC address but got `%U'",
818                                  format_unformat_error, input);
819       goto done;
820     }
821
822   if (unformat (input, "del"))
823     {
824       is_add = 0;
825     }
826
827   /* set the bridge domain flagAdd IP-MAC entry into bridge domain */
828   if (bd_add_del_ip_mac (bd_index, ip_addr, mac_addr, is_ip6, is_add))
829     {
830       error = clib_error_return (0, "MAC %s for IP %U and MAC %U failed",
831                                  is_add ? "add" : "del",
832                                  is_ip6 ?
833                                  format_ip4_address : format_ip6_address,
834                                  ip_addr, format_ethernet_address, mac_addr);
835     }
836
837 done:
838   return error;
839 }
840
841 /*?
842  * Add an ARP entry to an existing bridge-domain.
843  *
844  * @cliexpar
845  * Example of how to add an ARP entry (where 200 is the bridge-domain-id):
846  * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a}
847  * Example of how to delete an ARP entry (where 200 is the bridge-domain-id):
848  * @cliexcmd{set bridge-domain arp entry 200 192.168.72.45 52:54:00:3b:83:1a del}
849 ?*/
850 /* *INDENT-OFF* */
851 VLIB_CLI_COMMAND (bd_arp_entry_cli, static) = {
852   .path = "set bridge-domain arp entry",
853   .short_help = "set bridge-domain arp entry <bridge-domain-id> <ip-addr> <mac-addr> [del]",
854   .function = bd_arp_entry,
855 };
856 /* *INDENT-ON* */
857
858 u8 *
859 format_vtr (u8 * s, va_list * args)
860 {
861   u32 vtr_op = va_arg (*args, u32);
862   u32 dot1q = va_arg (*args, u32);
863   u32 tag1 = va_arg (*args, u32);
864   u32 tag2 = va_arg (*args, u32);
865   switch (vtr_op)
866     {
867     case L2_VTR_DISABLED:
868       return format (s, "none");
869     case L2_VTR_PUSH_1:
870       return format (s, "push-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
871     case L2_VTR_PUSH_2:
872       return format (s, "push-2 %s %d %d", dot1q ? "dot1q" : "dot1ad", tag1,
873                      tag2);
874     case L2_VTR_POP_1:
875       return format (s, "pop-1");
876     case L2_VTR_POP_2:
877       return format (s, "pop-2");
878     case L2_VTR_TRANSLATE_1_1:
879       return format (s, "trans-1-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
880     case L2_VTR_TRANSLATE_1_2:
881       return format (s, "trans-1-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
882                      tag1, tag2);
883     case L2_VTR_TRANSLATE_2_1:
884       return format (s, "trans-2-1 %s %d", dot1q ? "dot1q" : "dot1ad", tag1);
885     case L2_VTR_TRANSLATE_2_2:
886       return format (s, "trans-2-2 %s %d %d", dot1q ? "dot1q" : "dot1ad",
887                      tag1, tag2);
888     default:
889       return format (s, "none");
890     }
891 }
892
893 /**
894    Show bridge-domain state.
895    The CLI format is:
896    show bridge-domain [<bd_index>]
897 */
898 static clib_error_t *
899 bd_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd)
900 {
901   vnet_main_t *vnm = vnet_get_main ();
902   bd_main_t *bdm = &bd_main;
903   clib_error_t *error = 0;
904   u32 bd_index = ~0;
905   l2_bridge_domain_t *bd_config;
906   u32 start, end;
907   u32 detail = 0;
908   u32 intf = 0;
909   u32 arp = 0;
910   u32 bd_id = ~0;
911   uword *p;
912
913   start = 0;
914   end = vec_len (l2input_main.bd_configs);
915
916   if (unformat (input, "%d", &bd_id))
917     {
918       if (unformat (input, "detail"))
919         detail = 1;
920       else if (unformat (input, "det"))
921         detail = 1;
922       if (unformat (input, "int"))
923         intf = 1;
924       if (unformat (input, "arp"))
925         arp = 1;
926
927       p = hash_get (bdm->bd_index_by_bd_id, bd_id);
928       if (p)
929         bd_index = *p;
930       else
931         return clib_error_return (0, "No such bridge domain %d", bd_id);
932
933       vec_validate (l2input_main.bd_configs, bd_index);
934       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
935       if (bd_is_valid (bd_config))
936         {
937           start = bd_index;
938           end = start + 1;
939         }
940       else
941         {
942           vlib_cli_output (vm, "bridge-domain %d not in use", bd_id);
943           goto done;
944         }
945     }
946
947   /* Show all bridge-domains that have been initialized */
948   u32 printed = 0;
949   u8 *as = 0;
950   for (bd_index = start; bd_index < end; bd_index++)
951     {
952       bd_config = vec_elt_at_index (l2input_main.bd_configs, bd_index);
953       if (bd_is_valid (bd_config))
954         {
955           if (!printed)
956             {
957               printed = 1;
958               vlib_cli_output (vm,
959                                "%=5s %=7s %=4s %=9s %=9s %=9s %=9s %=9s %=9s %=9s",
960                                "ID", "Index", "BSN", "Age(min)", "Learning",
961                                "U-Forwrd", "UU-Flood", "Flooding", "ARP-Term",
962                                "BVI-Intf");
963             }
964
965           if (bd_config->mac_age)
966             as = format (as, "%d", bd_config->mac_age);
967           else
968             as = format (as, "off");
969           vlib_cli_output (vm,
970                            "%=5d %=7d %=4d %=9v %=9s %=9s %=9s %=9s %=9s %=9U",
971                            bd_config->bd_id, bd_index, bd_config->seq_num, as,
972                            bd_config->feature_bitmap & L2INPUT_FEAT_LEARN ?
973                            "on" : "off",
974                            bd_config->feature_bitmap & L2INPUT_FEAT_FWD ?
975                            "on" : "off",
976                            bd_config->feature_bitmap & L2INPUT_FEAT_UU_FLOOD ?
977                            "on" : "off",
978                            bd_config->feature_bitmap & L2INPUT_FEAT_FLOOD ?
979                            "on" : "off",
980                            bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM ?
981                            "on" : "off",
982                            format_vnet_sw_if_index_name_with_NA,
983                            vnm, bd_config->bvi_sw_if_index);
984           vec_reset_length (as);
985
986           if (detail || intf)
987             {
988               /* Show all member interfaces */
989               int i;
990               vec_foreach_index (i, bd_config->members)
991               {
992                 l2_flood_member_t *member =
993                   vec_elt_at_index (bd_config->members, i);
994                 l2_input_config_t *int_config =
995                   l2input_intf_config (member->sw_if_index);
996                 u32 vtr_opr, dot1q, tag1, tag2;
997                 if (i == 0)
998                   {
999                     vlib_cli_output (vm, "\n%=30s%=7s%=5s%=5s%=5s%=9s%=30s",
1000                                      "Interface", "If-idx", "ISN", "SHG",
1001                                      "BVI", "TxFlood", "VLAN-Tag-Rewrite");
1002                   }
1003                 l2vtr_get (vm, vnm, member->sw_if_index, &vtr_opr, &dot1q,
1004                            &tag1, &tag2);
1005                 vlib_cli_output (vm, "%=30U%=7d%=5d%=5d%=5s%=9s%=30U",
1006                                  format_vnet_sw_if_index_name, vnm,
1007                                  member->sw_if_index, member->sw_if_index,
1008                                  int_config->seq_num, member->shg,
1009                                  member->flags & L2_FLOOD_MEMBER_BVI ? "*" :
1010                                  "-", i < bd_config->flood_count ? "*" : "-",
1011                                  format_vtr, vtr_opr, dot1q, tag1, tag2);
1012               }
1013             }
1014
1015           if ((detail || arp) &&
1016               (bd_config->feature_bitmap & L2INPUT_FEAT_ARP_TERM))
1017             {
1018               u32 ip4_addr;
1019               ip6_address_t *ip6_addr;
1020               u64 mac_addr;
1021               vlib_cli_output (vm,
1022                                "\n  IP4/IP6 to MAC table for ARP Termination");
1023
1024               /* *INDENT-OFF* */
1025               hash_foreach (ip4_addr, mac_addr, bd_config->mac_by_ip4,
1026               ({
1027                 vlib_cli_output (vm, "%=40U => %=20U",
1028                                  format_ip4_address, &ip4_addr,
1029                                  format_ethernet_address, &mac_addr);
1030               }));
1031
1032               hash_foreach_mem (ip6_addr, mac_addr, bd_config->mac_by_ip6,
1033               ({
1034                 vlib_cli_output (vm, "%=40U => %=20U",
1035                                  format_ip6_address, ip6_addr,
1036                                  format_ethernet_address, &mac_addr);
1037               }));
1038               /* *INDENT-ON* */
1039             }
1040         }
1041     }
1042   vec_free (as);
1043
1044   if (!printed)
1045     {
1046       vlib_cli_output (vm, "no bridge-domains in use");
1047     }
1048
1049 done:
1050   return error;
1051 }
1052
1053 /*?
1054  * Show a summary of all the bridge-domain instances or detailed view of a
1055  * single bridge-domain. Bridge-domains are created by adding an interface
1056  * to a bridge using the '<em>set interface l2 bridge</em>' command.
1057  *
1058  * @cliexpar
1059  * @parblock
1060  * Example of displaying all bridge-domains:
1061  * @cliexstart{show bridge-domain}
1062  *  ID   Index   Learning   U-Forwrd   UU-Flood   Flooding   ARP-Term     BVI-Intf
1063  *  0      0        off        off        off        off        off        local0
1064  * 200     1        on         on         on         on         off          N/A
1065  * @cliexend
1066  *
1067  * Example of displaying details of a single bridge-domains:
1068  * @cliexstart{show bridge-domain 200 detail}
1069  *  ID   Index   Learning   U-Forwrd   UU-Flood   Flooding   ARP-Term     BVI-Intf
1070  * 200     1        on         on         on         on         off          N/A
1071  *
1072  *          Interface           Index  SHG  BVI        VLAN-Tag-Rewrite
1073  *  GigabitEthernet0/8/0.200      3     0    -               none
1074  *  GigabitEthernet0/9/0.200      4     0    -               none
1075  * @cliexend
1076  * @endparblock
1077 ?*/
1078 /* *INDENT-OFF* */
1079 VLIB_CLI_COMMAND (bd_show_cli, static) = {
1080   .path = "show bridge-domain",
1081   .short_help = "show bridge-domain [bridge-domain-id [detail|int|arp]]",
1082   .function = bd_show,
1083 };
1084 /* *INDENT-ON* */
1085
1086 int
1087 bd_add_del (l2_bridge_domain_add_del_args_t * a)
1088 {
1089   bd_main_t *bdm = &bd_main;
1090   vlib_main_t *vm = bdm->vlib_main;
1091   u32 enable_flags = 0, disable_flags = 0;
1092   u32 bd_index = ~0;
1093   int rv = 0;
1094
1095   if (a->is_add)
1096     {
1097       bd_index = bd_find_or_add_bd_index (bdm, a->bd_id);
1098       if (bd_index == ~0)
1099         return bd_index;
1100
1101       if (a->flood)
1102         enable_flags |= L2_FLOOD;
1103       else
1104         disable_flags |= L2_FLOOD;
1105
1106       if (a->uu_flood)
1107         enable_flags |= L2_UU_FLOOD;
1108       else
1109         disable_flags |= L2_UU_FLOOD;
1110
1111       if (a->forward)
1112         enable_flags |= L2_FWD;
1113       else
1114         disable_flags |= L2_FWD;
1115
1116       if (a->learn)
1117         enable_flags |= L2_LEARN;
1118       else
1119         disable_flags |= L2_LEARN;
1120
1121       if (a->arp_term)
1122         enable_flags |= L2_ARP_TERM;
1123       else
1124         disable_flags |= L2_ARP_TERM;
1125
1126       if (enable_flags)
1127         bd_set_flags (vm, bd_index, enable_flags, 1 /* enable */ );
1128
1129       if (disable_flags)
1130         bd_set_flags (vm, bd_index, disable_flags, 0 /* disable */ );
1131
1132       bd_set_mac_age (vm, bd_index, a->mac_age);
1133     }
1134   else
1135     rv = bd_delete_bd_index (bdm, a->bd_id);
1136
1137   return rv;
1138 }
1139
1140 /**
1141    Create or delete bridge-domain.
1142    The CLI format is:
1143    create bridge-domain <bd_index> [learn <0|1>] [forward <0|1>] [uu-flood <0|1>]
1144                                    [flood <0|1>] [arp-term <0|1>] [mac-age <nn>] [del]
1145 */
1146
1147 static clib_error_t *
1148 bd_add_del_command_fn (vlib_main_t * vm, unformat_input_t * input,
1149                        vlib_cli_command_t * cmd)
1150 {
1151   unformat_input_t _line_input, *line_input = &_line_input;
1152   clib_error_t *error = 0;
1153   u8 is_add = 1;
1154   u32 bd_id = ~0;
1155   u32 flood = 1, forward = 1, learn = 1, uu_flood = 0, arp_term = 0;
1156   u32 mac_age = 0;
1157   l2_bridge_domain_add_del_args_t _a, *a = &_a;
1158   int rv;
1159
1160   /* Get a line of input. */
1161   if (!unformat_user (input, unformat_line_input, line_input))
1162     return 0;
1163
1164   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
1165     {
1166       if (unformat (line_input, "%d", &bd_id))
1167         ;
1168       else if (unformat (line_input, "flood %d", &flood))
1169         ;
1170       else if (unformat (line_input, "uu-flood %d", &uu_flood))
1171         ;
1172       else if (unformat (line_input, "forward %d", &forward))
1173         ;
1174       else if (unformat (line_input, "arp-term %d", &arp_term))
1175         ;
1176       else if (unformat (line_input, "mac-age %d", &mac_age))
1177         ;
1178       else if (unformat (line_input, "del"))
1179         {
1180           is_add = 0;
1181           flood = uu_flood = forward = learn = 0;
1182         }
1183       else
1184         break;
1185     }
1186
1187   if (bd_id == ~0)
1188     {
1189       error = clib_error_return (0, "bridge-domain-id not specified");
1190       goto done;
1191     }
1192
1193   if (mac_age > 255)
1194     {
1195       error = clib_error_return (0, "mac age must be less than 256");
1196       goto done;
1197     }
1198
1199   memset (a, 0, sizeof (*a));
1200   a->is_add = is_add;
1201   a->bd_id = bd_id;
1202   a->flood = (u8) flood;
1203   a->uu_flood = (u8) uu_flood;
1204   a->forward = (u8) forward;
1205   a->learn = (u8) learn;
1206   a->arp_term = (u8) arp_term;
1207   a->mac_age = (u8) mac_age;
1208
1209   rv = bd_add_del (a);
1210
1211   switch (rv)
1212     {
1213     case 0:
1214       if (is_add)
1215         vlib_cli_output (vm, "bridge-domain %d", bd_id);
1216       break;
1217     case VNET_API_ERROR_NO_SUCH_ENTRY:
1218       error = clib_error_return (0, "bridge domain id does not exist");
1219       goto done;
1220     default:
1221       error = clib_error_return (0, "bd_add_del returned %d", rv);
1222       goto done;
1223     }
1224
1225 done:
1226   unformat_free (line_input);
1227
1228   return error;
1229 }
1230
1231
1232 /*?
1233  * Create/Delete bridge-domain instance
1234  *
1235  * @cliexpar
1236  * @parblock
1237  * Example of creating bridge-domain 1:
1238  * @cliexstart{create bridge-domain 1}
1239  * bridge-domain 1
1240  * @cliexend
1241  *
1242  * Example of creating bridge-domain 2 with enabling arp-term, mac-age 60:
1243  * @cliexstart{create bridge-domain 2 arp-term 1 mac-age 60}
1244  * bridge-domain 2
1245  *
1246  * vpp# show bridge-domain
1247  *   ID   Index   BSN  Age(min)  Learning  U-Forwrd  UU-Flood  Flooding  ARP-Term  BVI-Intf
1248  *   0      0      0     off       off       off       off       off       off      local0
1249  *   1      1      0     off        on        on       off        on       off       N/A
1250  *   2      2      0      60        on        on       off        on        on       N/A
1251  *
1252  * @cliexend
1253  *
1254  * Example of delete bridge-domain 1:
1255  * @cliexstart{create bridge-domain 1 del}
1256  * @cliexend
1257  * @endparblock
1258 ?*/
1259
1260 /* *INDENT-OFF* */
1261 VLIB_CLI_COMMAND (bd_create_cli, static) = {
1262   .path = "create bridge-domain",
1263   .short_help = "create bridge-domain <bridge-domain-id>"
1264                 " [learn <0|1>] [forward <0|1>] [uu-flood <0|1>] [flood <0|1>] [arp-term <0|1>]"
1265                 " [mac-age <nn>] [del]",
1266   .function = bd_add_del_command_fn,
1267 };
1268 /* *INDENT-ON* */
1269
1270
1271
1272 /*
1273  * fd.io coding-style-patch-verification: ON
1274  *
1275  * Local Variables:
1276  * eval: (c-set-style "gnu")
1277  * End:
1278  */