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