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