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