L2FIB:CLI/API to flush all non-static entries
[vpp.git] / src / vnet / l2 / l2_fib.c
1 /*
2  * l2_fib.c : layer 2 forwarding table (aka mac table)
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
19 #include <vlib/vlib.h>
20 #include <vnet/vnet.h>
21 #include <vnet/pg/pg.h>
22 #include <vnet/ethernet/ethernet.h>
23 #include <vlib/cli.h>
24
25 #include <vppinfra/error.h>
26 #include <vppinfra/hash.h>
27 #include <vnet/l2/l2_input.h>
28 #include <vnet/l2/l2_fib.h>
29 #include <vnet/l2/l2_learn.h>
30 #include <vnet/l2/l2_bd.h>
31
32 #include <vppinfra/bihash_template.c>
33
34 /**
35  * @file
36  * @brief Ethernet MAC Address FIB Table Management.
37  *
38  * The MAC Address forwarding table for bridge-domains is called the l2fib.
39  * Entries are added automatically as part of mac learning, but MAC Addresses
40  * entries can also be added manually.
41  *
42  */
43
44 typedef struct
45 {
46
47   /* hash table */
48   BVT (clib_bihash) mac_table;
49
50   /* convenience variables */
51   vlib_main_t *vlib_main;
52   vnet_main_t *vnet_main;
53 } l2fib_main_t;
54
55 l2fib_main_t l2fib_main;
56
57 /** Format sw_if_index. If the value is ~0, use the text "N/A" */
58 u8 *
59 format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
60 {
61   vnet_main_t *vnm = va_arg (*args, vnet_main_t *);
62   u32 sw_if_index = va_arg (*args, u32);
63   if (sw_if_index == ~0)
64     return format (s, "N/A");
65   else
66     return format (s, "%U",
67                    format_vnet_sw_interface_name, vnm,
68                    vnet_get_sw_interface (vnm, sw_if_index));
69 }
70
71 void
72 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
73                   l2fib_entry_result_t ** l2fe_res)
74 {
75   l2fib_main_t *msm = &l2fib_main;
76   BVT (clib_bihash) * h = &msm->mac_table;
77   clib_bihash_bucket_t *b;
78   BVT (clib_bihash_value) * v;
79   l2fib_entry_key_t key;
80   l2fib_entry_result_t result;
81   int i, j, k;
82
83   for (i = 0; i < h->nbuckets; i++)
84     {
85       b = &h->buckets[i];
86       if (b->offset == 0)
87         continue;
88       v = BV (clib_bihash_get_value) (h, b->offset);
89       for (j = 0; j < (1 << b->log2_pages); j++)
90         {
91           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
92             {
93               if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
94                 continue;
95
96               key.raw = v->kvp[k].key;
97               result.raw = v->kvp[k].value;
98
99               if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
100                 {
101                   vec_add1 (*l2fe_key, key);
102                   vec_add1 (*l2fe_res, result);
103                 }
104             }
105           v++;
106         }
107     }
108 }
109
110 /** Display the contents of the l2fib. */
111 static clib_error_t *
112 show_l2fib (vlib_main_t * vm,
113             unformat_input_t * input, vlib_cli_command_t * cmd)
114 {
115   bd_main_t *bdm = &bd_main;
116   l2fib_main_t *msm = &l2fib_main;
117   l2_bridge_domain_t *bd_config;
118   BVT (clib_bihash) * h = &msm->mac_table;
119   clib_bihash_bucket_t *b;
120   BVT (clib_bihash_value) * v;
121   l2fib_entry_key_t key;
122   l2fib_entry_result_t result;
123   u32 first_entry = 1;
124   u64 total_entries = 0;
125   int i, j, k;
126   u8 verbose = 0;
127   u8 raw = 0;
128   u32 bd_id, bd_index = ~0;
129   u8 now = (u8) (vlib_time_now (vm) / 60);
130   u8 *s = 0;
131
132   if (unformat (input, "raw"))
133     raw = 1;
134   else if (unformat (input, "verbose"))
135     verbose = 1;
136   else if (unformat (input, "bd_index %d", &bd_index))
137     verbose = 1;
138   else if (unformat (input, "bd_id %d", &bd_id))
139     {
140       uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
141       if (p)
142         {
143           verbose = 1;
144           bd_index = p[0];
145         }
146       else
147         {
148           vlib_cli_output (vm, "no such bridge domain id");
149           return 0;
150         }
151     }
152
153   for (i = 0; i < h->nbuckets; i++)
154     {
155       b = &h->buckets[i];
156       if (b->offset == 0)
157         continue;
158       v = BV (clib_bihash_get_value) (h, b->offset);
159       for (j = 0; j < (1 << b->log2_pages); j++)
160         {
161           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
162             {
163               if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
164                 continue;
165
166               if (verbose && first_entry)
167                 {
168                   first_entry = 0;
169                   vlib_cli_output (vm,
170                                    "%=19s%=7s%=7s%=8s%=9s%=7s%=7s%=5s%=30s",
171                                    "Mac-Address", "BD-Idx", "If-Idx",
172                                    "BSN-ISN", "Age(min)", "static", "filter",
173                                    "bvi", "Interface-Name");
174                 }
175
176               key.raw = v->kvp[k].key;
177               result.raw = v->kvp[k].value;
178
179               if (verbose
180                   & ((bd_index >> 31) || (bd_index == key.fields.bd_index)))
181                 {
182                   bd_config = vec_elt_at_index (l2input_main.bd_configs,
183                                                 key.fields.bd_index);
184
185                   if (bd_config->mac_age && !result.fields.static_mac)
186                     {
187                       i16 delta = now - result.fields.timestamp;
188                       delta += delta < 0 ? 256 : 0;
189                       s = format (s, "%d", delta);
190                     }
191                   else
192                     s = format (s, "-");
193
194                   vlib_cli_output (vm,
195                                    "%=19U%=7d%=7d %3d/%-3d%=9v%=7s%=7s%=5s%=30U",
196                                    format_ethernet_address, key.fields.mac,
197                                    key.fields.bd_index,
198                                    result.fields.sw_if_index == ~0
199                                    ? -1 : result.fields.sw_if_index,
200                                    result.fields.sn.bd, result.fields.sn.swif,
201                                    s, result.fields.static_mac ? "*" : "-",
202                                    result.fields.filter ? "*" : "-",
203                                    result.fields.bvi ? "*" : "-",
204                                    format_vnet_sw_if_index_name_with_NA,
205                                    msm->vnet_main, result.fields.sw_if_index);
206                   vec_reset_length (s);
207                 }
208               total_entries++;
209             }
210           v++;
211         }
212     }
213
214   if (total_entries == 0)
215     vlib_cli_output (vm, "no l2fib entries");
216   else
217     vlib_cli_output (vm,
218                      "%lld l2fib entries with %d learned (or non-static) entries",
219                      total_entries, l2learn_main.global_learn_count);
220
221   if (raw)
222     vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
223                      BV (format_bihash), h, 1 /* verbose */ );
224
225   vec_free (s);
226   return 0;
227 }
228
229 /*?
230  * This command dispays the MAC Address entries of the L2 FIB table.
231  * Output can be filtered to just get the number of MAC Addresses or display
232  * each MAC Address for all bridge domains or just a single bridge domain.
233  *
234  * @cliexpar
235  * Example of how to display the number of MAC Address entries in the L2
236  * FIB table:
237  * @cliexstart{show l2fib}
238  * 3 l2fib entries
239  * @cliexend
240  * Example of how to display all the MAC Address entries in the L2
241  * FIB table:
242  * @cliexstart{show l2fib verbose}
243  *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
244  *  52:54:00:53:18:33    1      GigabitEthernet0/8/0.200      3       0       0     0      0         0
245  *  52:54:00:53:18:55    1      GigabitEthernet0/8/0.200      3       1       0     0      0         0
246  *  52:54:00:53:18:77    1                 N/A                -1      1       1     0      0         0
247  * 3 l2fib entries
248  * @cliexend
249 ?*/
250 /* *INDENT-OFF* */
251 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
252   .path = "show l2fib",
253   .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
254   .function = show_l2fib,
255 };
256 /* *INDENT-ON* */
257
258
259 /* Remove all entries from the l2fib */
260 void
261 l2fib_clear_table (void)
262 {
263   l2fib_main_t *mp = &l2fib_main;
264
265   /* Remove all entries */
266   BV (clib_bihash_free) (&mp->mac_table);
267   BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
268                          L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
269   l2learn_main.global_learn_count = 0;
270 }
271
272 /** Clear all entries in L2FIB.
273  * @TODO: Later we may want a way to remove only the non-static entries
274  */
275 static clib_error_t *
276 clear_l2fib (vlib_main_t * vm,
277              unformat_input_t * input, vlib_cli_command_t * cmd)
278 {
279   l2fib_clear_table ();
280   return 0;
281 }
282
283 /*?
284  * This command clears all the MAC Address entries from the L2 FIB table.
285  *
286  * @cliexpar
287  * Example of how to clear the L2 FIB Table:
288  * @cliexcmd{clear l2fib}
289  * Example to show the L2 FIB Table has been cleared:
290  * @cliexstart{show l2fib verbose}
291  * no l2fib entries
292  * @cliexend
293 ?*/
294 /* *INDENT-OFF* */
295 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
296   .path = "clear l2fib",
297   .short_help = "clear l2fib",
298   .function = clear_l2fib,
299 };
300 /* *INDENT-ON* */
301
302 static inline l2fib_seq_num_t
303 l2fib_cur_seq_num (u32 bd_index, u32 sw_if_index)
304 {
305   l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
306   l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
307   /* *INDENT-OFF* */
308   return (l2fib_seq_num_t) {
309     .swif = int_config->seq_num,
310     .bd = bd_config->seq_num,
311   };
312   /* *INDENT-ON* */
313 }
314
315 /**
316  * Add an entry to the l2fib.
317  * If the entry already exists then overwrite it
318  */
319 void
320 l2fib_add_entry (u64 mac, u32 bd_index,
321                  u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
322 {
323   l2fib_entry_key_t key;
324   l2fib_entry_result_t result;
325   __attribute__ ((unused)) u32 bucket_contents;
326   l2fib_main_t *mp = &l2fib_main;
327   BVT (clib_bihash_kv) kv;
328
329   /* set up key */
330   key.raw = l2fib_make_key ((u8 *) & mac, bd_index);
331
332   /* set up result */
333   result.raw = 0;               /* clear all fields */
334   result.fields.sw_if_index = sw_if_index;
335   result.fields.static_mac = static_mac;
336   result.fields.filter = filter_mac;
337   result.fields.bvi = bvi_mac;
338   if (!static_mac)
339     result.fields.sn = l2fib_cur_seq_num (bd_index, sw_if_index);
340
341   kv.key = key.raw;
342   kv.value = result.raw;
343
344   BV (clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */ );
345
346   /* increment counter if dynamically learned mac */
347   if (result.fields.static_mac == 0)
348     {
349       l2learn_main.global_learn_count++;
350     }
351 }
352
353 /**
354  * Add an entry to the L2FIB.
355  * The CLI format is:
356  *    l2fib add <mac> <bd> <intf> [static] [bvi]
357  *    l2fib add <mac> <bd> filter
358  * Note that filter and bvi entries are always static
359  */
360 static clib_error_t *
361 l2fib_add (vlib_main_t * vm,
362            unformat_input_t * input, vlib_cli_command_t * cmd)
363 {
364   bd_main_t *bdm = &bd_main;
365   vnet_main_t *vnm = vnet_get_main ();
366   clib_error_t *error = 0;
367   u64 mac;
368   u32 bd_id;
369   u32 bd_index;
370   u32 sw_if_index = ~0;
371   u32 filter_mac = 0;
372   u32 static_mac = 0;
373   u32 bvi_mac = 0;
374   uword *p;
375
376   if (!unformat_user (input, unformat_ethernet_address, &mac))
377     {
378       error = clib_error_return (0, "expected mac address `%U'",
379                                  format_unformat_error, input);
380       goto done;
381     }
382
383   if (!unformat (input, "%d", &bd_id))
384     {
385       error = clib_error_return (0, "expected bridge domain ID `%U'",
386                                  format_unformat_error, input);
387       goto done;
388     }
389
390   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
391   if (!p)
392     {
393       error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
394       goto done;
395     }
396   bd_index = p[0];
397
398   if (unformat (input, "filter"))
399     {
400       filter_mac = 1;
401       static_mac = 1;
402
403     }
404   else
405     {
406
407       if (!unformat_user
408           (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
409         {
410           error = clib_error_return (0, "unknown interface `%U'",
411                                      format_unformat_error, input);
412           goto done;
413         }
414       if (unformat (input, "static"))
415         {
416           static_mac = 1;
417         }
418       else if (unformat (input, "bvi"))
419         {
420           bvi_mac = 1;
421           static_mac = 1;
422         }
423     }
424
425   l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, filter_mac,
426                    bvi_mac);
427
428 done:
429   return error;
430 }
431
432 /*?
433  * This command adds a MAC Address entry to the L2 FIB table
434  * of an existing bridge-domain. The MAC Address can be static
435  * or dynamic. This command also allows a filter to be added,
436  * such that packets with given MAC Addresses (source mac or
437  * destination mac match) are dropped.
438  *
439  * @cliexpar
440  * Example of how to add a dynamic MAC Address entry to the L2 FIB table
441  * of a bridge-domain (where 200 is the bridge-domain-id):
442  * @cliexcmd{l2fib add 52:54:00:53:18:33 200 GigabitEthernet0/8/0.200}
443  * Example of how to add a static MAC Address entry to the L2 FIB table
444  * of a bridge-domain (where 200 is the bridge-domain-id):
445  * @cliexcmd{l2fib add 52:54:00:53:18:55 200 GigabitEthernet0/8/0.200 static}
446  * Example of how to add a filter such that a packet with the given MAC
447  * Address will be dropped in a given bridge-domain (where 200 is the
448  * bridge-domain-id):
449  * @cliexcmd{l2fib add 52:54:00:53:18:77 200 filter}
450  * Example of show command of the provisioned MAC Addresses and filters:
451  * @cliexstart{show l2fib verbose}
452  *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
453  *  52:54:00:53:18:33    1      GigabitEthernet0/8/0.200      3       0       0     0      0         0
454  *  52:54:00:53:18:55    1      GigabitEthernet0/8/0.200      3       1       0     0      0         0
455  *  52:54:00:53:18:77    1                 N/A                -1      1       1     0      0         0
456  * 3 l2fib entries
457  * @cliexend
458 ?*/
459 /* *INDENT-OFF* */
460 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
461   .path = "l2fib add",
462   .short_help = "l2fib add <mac> <bridge-domain-id> filter | <intf> [static | bvi]",
463   .function = l2fib_add,
464 };
465 /* *INDENT-ON* */
466
467
468 static clib_error_t *
469 l2fib_test_command_fn (vlib_main_t * vm,
470                        unformat_input_t * input, vlib_cli_command_t * cmd)
471 {
472   clib_error_t *error = 0;
473   u64 mac, save_mac;
474   u32 bd_index = 0;
475   u32 sw_if_index = 8;
476   u32 filter_mac = 0;
477   u32 bvi_mac = 0;
478   u32 is_add = 0;
479   u32 is_del = 0;
480   u32 is_check = 0;
481   u32 count = 1;
482   int mac_set = 0;
483   int i;
484
485   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
486     {
487       if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
488         mac_set = 1;
489       else if (unformat (input, "add"))
490         is_add = 1;
491       else if (unformat (input, "del"))
492         is_del = 1;
493       else if (unformat (input, "check"))
494         is_check = 1;
495       else if (unformat (input, "count %d", &count))
496         ;
497       else
498         break;
499     }
500
501   if (mac_set == 0)
502     return clib_error_return (0, "mac not set");
503
504   if (is_add == 0 && is_del == 0 && is_check == 0)
505     return clib_error_return (0,
506                               "noop: pick at least one of (add,del,check)");
507
508   save_mac = mac;
509
510   if (is_add)
511     {
512       for (i = 0; i < count; i++)
513         {
514           u64 tmp;
515           l2fib_add_entry (mac, bd_index, sw_if_index, mac,
516                            filter_mac, bvi_mac);
517           tmp = clib_net_to_host_u64 (mac);
518           tmp >>= 16;
519           tmp++;
520           tmp <<= 16;
521           mac = clib_host_to_net_u64 (tmp);
522         }
523     }
524
525   if (is_check)
526     {
527       BVT (clib_bihash_kv) kv;
528       l2fib_main_t *mp = &l2fib_main;
529
530       mac = save_mac;
531
532       for (i = 0; i < count; i++)
533         {
534           u64 tmp;
535           kv.key = l2fib_make_key ((u8 *) & mac, bd_index);
536           if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
537             {
538               clib_warning ("key %U AWOL", format_ethernet_address, &mac);
539               break;
540             }
541           tmp = clib_net_to_host_u64 (mac);
542           tmp >>= 16;
543           tmp++;
544           tmp <<= 16;
545           mac = clib_host_to_net_u64 (tmp);
546         }
547     }
548
549   if (is_del)
550     {
551       for (i = 0; i < count; i++)
552         {
553           u64 tmp;
554
555           l2fib_del_entry (mac, bd_index);
556
557           tmp = clib_net_to_host_u64 (mac);
558           tmp >>= 16;
559           tmp++;
560           tmp <<= 16;
561           mac = clib_host_to_net_u64 (tmp);
562         }
563     }
564
565   return error;
566 }
567
568 /*?
569  * The set of '<em>test l2fib</em>' commands allow the L2 FIB table of the default
570  * bridge domain (bridge-domain-id of 0) to be modified.
571  *
572  * @cliexpar
573  * @parblock
574  * Example of how to add a set of 4 sequential MAC Address entries to L2
575  * FIB table of the default bridge-domain:
576  * @cliexcmd{test l2fib add mac 52:54:00:53:00:00 count 4}
577  *
578  * Show the set of 4 sequential MAC Address entries that were added:
579  * @cliexstart{show l2fib verbose}
580  *     Mac Address     BD Idx           Interface           Index  static  filter  bvi  refresh  timestamp
581  * 52:54:00:53:00:00    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
582  * 52:54:00:53:00:01    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
583  * 52:54:00:53:00:03    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
584  * 52:54:00:53:00:02    0       GigabitEthernet0/8/0.300     8       0       0     0      0         0
585  * 4 l2fib entries
586  * @cliexend
587  *
588  * Example of how to check that the set of 4 sequential MAC Address
589  * entries were added to L2 FIB table of the default
590  * bridge-domain. Used a count of 5 to produce an error:
591  *
592  * @cliexcmd{test l2fib check mac 52:54:00:53:00:00 count 5}
593  * The output of the check command is in the log files. Log file
594  * location may vary based on your OS and Version:
595  *
596  * <b><em># tail -f /var/log/messages | grep l2fib_test_command_fn</em></b>
597  *
598  * Sep  7 17:15:24 localhost vnet[4952]: l2fib_test_command_fn:446: key 52:54:00:53:00:04 AWOL
599  *
600  * Example of how to delete a set of 4 sequential MAC Address entries
601  * from L2 FIB table of the default bridge-domain:
602  * @cliexcmd{test l2fib del mac 52:54:00:53:00:00 count 4}
603  * @endparblock
604 ?*/
605 /* *INDENT-OFF* */
606 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
607   .path = "test l2fib",
608   .short_help = "test l2fib [add|del|check] mac <base-addr> count <nn>",
609   .function = l2fib_test_command_fn,
610 };
611 /* *INDENT-ON* */
612
613
614 /**
615  * Delete an entry from the l2fib.
616  * Return 0 if the entry was deleted, or 1 if it was not found
617  */
618 static u32
619 l2fib_del_entry_by_key (u64 raw_key)
620 {
621
622   l2fib_entry_result_t result;
623   l2fib_main_t *mp = &l2fib_main;
624   BVT (clib_bihash_kv) kv;
625
626   /* set up key */
627   kv.key = raw_key;
628
629   if (BV (clib_bihash_search) (&mp->mac_table, &kv, &kv))
630     return 1;
631
632   result.raw = kv.value;
633
634   /* decrement counter if dynamically learned mac */
635   if (result.fields.static_mac == 0)
636     {
637       if (l2learn_main.global_learn_count > 0)
638         {
639           l2learn_main.global_learn_count--;
640         }
641     }
642
643   /* Remove entry from hash table */
644   BV (clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */ );
645   return 0;
646 }
647
648 /**
649  * Delete an entry from the l2fib.
650  * Return 0 if the entry was deleted, or 1 if it was not found
651  */
652 u32
653 l2fib_del_entry (u64 mac, u32 bd_index)
654 {
655   return l2fib_del_entry_by_key (l2fib_make_key ((u8 *) & mac, bd_index));
656 }
657
658 /**
659  * Delete an entry from the L2FIB.
660  * The CLI format is:
661  *    l2fib del <mac> <bd-id>
662  */
663 static clib_error_t *
664 l2fib_del (vlib_main_t * vm,
665            unformat_input_t * input, vlib_cli_command_t * cmd)
666 {
667   bd_main_t *bdm = &bd_main;
668   clib_error_t *error = 0;
669   u64 mac;
670   u32 bd_id;
671   u32 bd_index;
672   uword *p;
673
674   if (!unformat_user (input, unformat_ethernet_address, &mac))
675     {
676       error = clib_error_return (0, "expected mac address `%U'",
677                                  format_unformat_error, input);
678       goto done;
679     }
680
681   if (!unformat (input, "%d", &bd_id))
682     {
683       error = clib_error_return (0, "expected bridge domain ID `%U'",
684                                  format_unformat_error, input);
685       goto done;
686     }
687
688   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
689   if (!p)
690     {
691       error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
692       goto done;
693     }
694   bd_index = p[0];
695
696   /* Delete the entry */
697   if (l2fib_del_entry (mac, bd_index))
698     {
699       error = clib_error_return (0, "mac entry not found");
700       goto done;
701     }
702
703 done:
704   return error;
705 }
706
707 /*?
708  * This command deletes an existing MAC Address entry from the L2 FIB
709  * table of an existing bridge-domain.
710  *
711  * @cliexpar
712  * Example of how to delete a MAC Address entry from the L2 FIB table of a bridge-domain (where 200 is the bridge-domain-id):
713  * @cliexcmd{l2fib del 52:54:00:53:18:33 200}
714 ?*/
715 /* *INDENT-OFF* */
716 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
717   .path = "l2fib del",
718   .short_help = "l2fib del <mac> <bridge-domain-id>",
719   .function = l2fib_del,
720 };
721 /* *INDENT-ON* */
722
723 /**
724     Kick off ager to scan MACs to age/delete MAC entries
725 */
726 void
727 l2fib_start_ager_scan (vlib_main_t * vm)
728 {
729   l2_bridge_domain_t *bd_config;
730   int enable = 0;
731
732   /* check if there is at least one bd with mac aging enabled */
733   vec_foreach (bd_config, l2input_main.bd_configs)
734     if (bd_config->bd_id != ~0 && bd_config->mac_age != 0)
735     enable = 1;
736
737   vlib_process_signal_event (vm, l2fib_mac_age_scanner_process_node.index,
738                              enable ? L2_MAC_AGE_PROCESS_EVENT_START :
739                              L2_MAC_AGE_PROCESS_EVENT_ONE_PASS, 0);
740 }
741
742 /**
743     Flush all non static MACs from an interface
744 */
745 void
746 l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index)
747 {
748   l2_input_config_t *int_config = l2input_intf_config (sw_if_index);
749   int_config->seq_num += 1;
750   l2fib_start_ager_scan (vm);
751 }
752
753 /**
754     Flush all non static MACs in a bridge domain
755 */
756 void
757 l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index)
758 {
759   l2_bridge_domain_t *bd_config = l2input_bd_config (bd_index);
760   bd_config->seq_num += 1;
761   l2fib_start_ager_scan (vm);
762 }
763
764 /**
765     Flush all non static MACs - flushes all valid BDs
766 */
767 void
768 l2fib_flush_all_mac (vlib_main_t * vm)
769 {
770   l2_bridge_domain_t *bd_config;
771   vec_foreach (bd_config, l2input_main.bd_configs)
772     if (bd_is_valid (bd_config))
773     bd_config->seq_num += 1;
774
775   l2fib_start_ager_scan (vm);
776 }
777
778
779 /**
780     Flush MACs, except static ones, associated with an interface
781     The CLI format is:
782     l2fib flush-mac interface <if-name>
783 */
784 static clib_error_t *
785 l2fib_flush_mac_int (vlib_main_t * vm,
786                      unformat_input_t * input, vlib_cli_command_t * cmd)
787 {
788   vnet_main_t *vnm = vnet_get_main ();
789   clib_error_t *error = 0;
790   u32 sw_if_index;
791
792   if (!unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index))
793     {
794       error = clib_error_return (0, "unknown interface `%U'",
795                                  format_unformat_error, input);
796       goto done;
797     }
798
799   l2fib_flush_int_mac (vm, sw_if_index);
800
801 done:
802   return error;
803 }
804
805 /**
806     Flush all MACs, except static ones
807     The CLI format is:
808     l2fib flush-mac all
809 */
810 static clib_error_t *
811 l2fib_flush_mac_all (vlib_main_t * vm,
812                      unformat_input_t * input, vlib_cli_command_t * cmd)
813 {
814   l2fib_flush_all_mac (vm);
815   return 0;
816 }
817
818 /*?
819  * This command kick off ager to delete all existing MAC Address entries,
820  * except static ones, associated with an interface from the L2 FIB table.
821  *
822  * @cliexpar
823  * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
824  * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
825 ?*/
826 /* *INDENT-OFF* */
827 VLIB_CLI_COMMAND (l2fib_flush_mac_all_cli, static) = {
828   .path = "l2fib flush-mac all",
829   .short_help = "l2fib flush-mac all",
830   .function = l2fib_flush_mac_all,
831 };
832 /* *INDENT-ON* */
833
834 /*?
835  * This command kick off ager to delete all existing MAC Address entries,
836  * except static ones, associated with an interface from the L2 FIB table.
837  *
838  * @cliexpar
839  * Example of how to flush MAC Address entries learned on an interface from the L2 FIB table:
840  * @cliexcmd{l2fib flush-mac interface GigabitEthernet2/1/0}
841 ?*/
842 /* *INDENT-OFF* */
843 VLIB_CLI_COMMAND (l2fib_flush_mac_int_cli, static) = {
844   .path = "l2fib flush-mac interface",
845   .short_help = "l2fib flush-mac interface <if-name>",
846   .function = l2fib_flush_mac_int,
847 };
848 /* *INDENT-ON* */
849
850 /**
851     Flush bridge-domain MACs except static ones.
852     The CLI format is:
853     l2fib flush-mac bridge-domain <bd-id>
854 */
855 static clib_error_t *
856 l2fib_flush_mac_bd (vlib_main_t * vm,
857                     unformat_input_t * input, vlib_cli_command_t * cmd)
858 {
859   bd_main_t *bdm = &bd_main;
860   clib_error_t *error = 0;
861   u32 bd_index, bd_id;
862   uword *p;
863
864   if (!unformat (input, "%d", &bd_id))
865     {
866       error = clib_error_return (0, "expecting bridge-domain id but got `%U'",
867                                  format_unformat_error, input);
868       goto done;
869     }
870
871   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
872   if (p)
873     bd_index = *p;
874   else
875     return clib_error_return (0, "No such bridge domain %d", bd_id);
876
877   l2fib_flush_bd_mac (vm, bd_index);
878
879 done:
880   return error;
881 }
882
883 /*?
884  * This command kick off ager to delete all existing MAC Address entries,
885  * except static ones, in a bridge domain from the L2 FIB table.
886  *
887  * @cliexpar
888  * Example of how to flush MAC Address entries learned in a bridge domain from the L2 FIB table:
889  * @cliexcmd{l2fib flush-mac bridge-domain 1000}
890 ?*/
891 /* *INDENT-OFF* */
892 VLIB_CLI_COMMAND (l2fib_flush_mac_bd_cli, static) = {
893   .path = "l2fib flush-mac bridge-domain",
894   .short_help = "l2fib flush-mac bridge-domain <bd-id>",
895   .function = l2fib_flush_mac_bd,
896 };
897 /* *INDENT-ON* */
898
899 clib_error_t *
900 l2fib_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
901 {
902   l2_input_config_t *config = l2input_intf_config (sw_if_index);
903   if ((flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) == 0 && config->bridge)
904     l2fib_flush_int_mac (vnm->vlib_main, sw_if_index);
905   return 0;
906 }
907
908 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (l2fib_sw_interface_up_down);
909
910 BVT (clib_bihash) * get_mac_table (void)
911 {
912   l2fib_main_t *mp = &l2fib_main;
913   return &mp->mac_table;
914 }
915
916 static uword
917 l2fib_mac_age_scanner_process (vlib_main_t * vm, vlib_node_runtime_t * rt,
918                                vlib_frame_t * f)
919 {
920   uword event_type, *event_data = 0;
921   l2fib_main_t *msm = &l2fib_main;
922   bool enabled = 0;
923   f64 start_time, last_run_duration = 0, t;
924
925   while (1)
926     {
927       if (enabled)
928         vlib_process_wait_for_event_or_clock (vm, 60 - last_run_duration);
929       else
930         vlib_process_wait_for_event (vm);
931
932       event_type = vlib_process_get_events (vm, &event_data);
933       vec_reset_length (event_data);
934
935       switch (event_type)
936         {
937         case ~0:
938           break;
939         case L2_MAC_AGE_PROCESS_EVENT_START:
940           enabled = 1;
941           break;
942         case L2_MAC_AGE_PROCESS_EVENT_STOP:
943           enabled = 0;
944           continue;
945         case L2_MAC_AGE_PROCESS_EVENT_ONE_PASS:
946           enabled = 0;
947           break;
948         default:
949           ASSERT (0);
950         }
951       last_run_duration = start_time = vlib_time_now (vm);
952
953       BVT (clib_bihash) * h = &msm->mac_table;
954       int i, j, k;
955       for (i = 0; i < h->nbuckets; i++)
956         {
957           /* Allow no more than 10us without a pause */
958           t = vlib_time_now (vm);
959           if (t > start_time + 10e-6)
960             {
961               vlib_process_suspend (vm, 100e-6);        /* suspend for 100 us */
962               start_time = vlib_time_now (vm);
963             }
964
965           if (i < (h->nbuckets - 3))
966             {
967               clib_bihash_bucket_t *b = &h->buckets[i + 3];
968               CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, LOAD);
969               b = &h->buckets[i + 1];
970               if (b->offset)
971                 {
972                   BVT (clib_bihash_value) * v =
973                     BV (clib_bihash_get_value) (h, b->offset);
974                   CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, LOAD);
975                 }
976             }
977
978           clib_bihash_bucket_t *b = &h->buckets[i];
979           if (b->offset == 0)
980             continue;
981           BVT (clib_bihash_value) * v =
982             BV (clib_bihash_get_value) (h, b->offset);
983           for (j = 0; j < (1 << b->log2_pages); j++)
984             {
985               for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
986                 {
987                   if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
988                     continue;
989
990                   l2fib_entry_key_t key = {.raw = v->kvp[k].key };
991                   l2fib_entry_result_t result = {.raw = v->kvp[k].value };
992
993                   if (result.fields.static_mac)
994                     continue;
995
996                   u32 bd_index = key.fields.bd_index;
997                   u32 sw_if_index = result.fields.sw_if_index;
998                   u16 sn = l2fib_cur_seq_num (bd_index, sw_if_index).as_u16;
999                   if (result.fields.sn.as_u16 != sn)
1000                     {
1001                       l2fib_del_entry_by_key (key.raw);
1002                       continue;
1003                     }
1004                   l2_bridge_domain_t *bd_config =
1005                     vec_elt_at_index (l2input_main.bd_configs, bd_index);
1006
1007                   if (bd_config->mac_age == 0)
1008                     continue;
1009
1010                   i16 delta =
1011                     (u8) (start_time / 60) - result.fields.timestamp;
1012                   delta += delta < 0 ? 256 : 0;
1013
1014                   if (delta > bd_config->mac_age)
1015                     l2fib_del_entry_by_key (key.raw);
1016                 }
1017               v++;
1018             }
1019         }
1020       last_run_duration = vlib_time_now (vm) - last_run_duration;
1021     }
1022   return 0;
1023 }
1024
1025 /* *INDENT-OFF* */
1026 VLIB_REGISTER_NODE (l2fib_mac_age_scanner_process_node) = {
1027     .function = l2fib_mac_age_scanner_process,
1028     .type = VLIB_NODE_TYPE_PROCESS,
1029     .name = "l2fib-mac-age-scanner-process",
1030 };
1031 /* *INDENT-ON* */
1032
1033 clib_error_t *
1034 l2fib_init (vlib_main_t * vm)
1035 {
1036   l2fib_main_t *mp = &l2fib_main;
1037   l2fib_entry_key_t test_key;
1038   u8 test_mac[6];
1039
1040   mp->vlib_main = vm;
1041   mp->vnet_main = vnet_get_main ();
1042
1043   /* Create the hash table  */
1044   BV (clib_bihash_init) (&mp->mac_table, "l2fib mac table",
1045                          L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
1046
1047   /* verify the key constructor is good, since it is endian-sensitive */
1048   memset (test_mac, 0, sizeof (test_mac));
1049   test_mac[0] = 0x11;
1050   test_key.raw = 0;
1051   test_key.raw = l2fib_make_key ((u8 *) & test_mac, 0x1234);
1052   ASSERT (test_key.fields.mac[0] == 0x11);
1053   ASSERT (test_key.fields.bd_index == 0x1234);
1054
1055   return 0;
1056 }
1057
1058 VLIB_INIT_FUNCTION (l2fib_init);
1059
1060 /*
1061  * fd.io coding-style-patch-verification: ON
1062  *
1063  * Local Variables:
1064  * eval: (c-set-style "gnu")
1065  * End:
1066  */