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