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