Initial commit of vpp code.
[vpp.git] / vnet / 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_fib.h>
28 #include <vnet/l2/l2_learn.h>
29 #include <vnet/l2/l2_bd.h>
30
31 #include <vppinfra/bihash_template.c>
32
33 typedef struct {
34
35   /* hash table */
36   BVT(clib_bihash) mac_table;
37
38   /* convenience variables */
39   vlib_main_t * vlib_main;
40   vnet_main_t * vnet_main;
41 } l2fib_main_t;
42
43 l2fib_main_t l2fib_main;
44
45
46 // Format sw_if_index. If the value is ~0, use the text "N/A"
47 u8 * format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args)
48 {
49   vnet_main_t * vnm = va_arg (*args, vnet_main_t *);
50   u32 sw_if_index = va_arg (*args, u32);
51   if (sw_if_index == ~0)
52     return format (s, "N/A");
53   else 
54     return format (s, "%U",
55                    format_vnet_sw_interface_name, vnm,
56                    vnet_get_sw_interface (vnm, sw_if_index));
57 }
58
59 void l2fib_table_dump (u32 bd_index, l2fib_entry_key_t **l2fe_key,
60                        l2fib_entry_result_t **l2fe_res)
61 {
62   l2fib_main_t * msm = &l2fib_main;
63   BVT(clib_bihash) * h = &msm->mac_table;
64   clib_bihash_bucket_t * b;
65   BVT(clib_bihash_value) * v;
66   l2fib_entry_key_t key;
67   l2fib_entry_result_t result;
68   int i, j, k;
69
70   for (i = 0; i < h->nbuckets; i++)
71     {
72       b = &h->buckets[i];
73       if (b->offset == 0)
74         continue;
75       v = BV(clib_bihash_get_value) (h, b->offset);
76       for (j = 0; j < (1<<b->log2_pages); j++)
77         {
78           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
79             {
80               if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
81                 continue;
82
83               key.raw = v->kvp[k].key;
84               result.raw = v->kvp[k].value;
85
86               if ((bd_index == ~0) || (bd_index == key.fields.bd_index))
87                 {
88                   vec_add1 (*l2fe_key, key);
89                   vec_add1 (*l2fe_res, result);
90                 }
91             }
92           v++;
93         }
94     }
95 }
96
97 // Display the contents of the l2fib
98 static clib_error_t *
99 show_l2fib  (vlib_main_t * vm,
100               unformat_input_t * input,
101               vlib_cli_command_t * cmd)
102 {
103   bd_main_t * bdm = &bd_main;
104   l2fib_main_t * msm = &l2fib_main;
105   BVT(clib_bihash) * h = &msm->mac_table;
106   clib_bihash_bucket_t * b;
107   BVT(clib_bihash_value) * v;
108   l2fib_entry_key_t key;
109   l2fib_entry_result_t result;
110   u32 first_entry = 1;
111   u64 total_entries = 0;
112   int i, j, k;
113   u8 verbose = 0;
114   u8 raw = 0;
115   u32 bd_id, bd_index = ~0;
116
117   if (unformat (input, "raw"))
118     raw = 1;
119   else if (unformat (input, "verbose"))
120     verbose = 1;
121   else if (unformat (input, "bd_index %d", &bd_index))
122     verbose = 1;
123   else if (unformat (input, "bd_id %d", &bd_id))
124     {
125       uword *p = hash_get (bdm->bd_index_by_bd_id, bd_id);
126       if (p) 
127         {
128           verbose = 1;
129           bd_index = p[0];
130         }
131       else
132         {
133           vlib_cli_output (vm, "no such bridge domain id");
134           return 0;
135         }
136     }
137
138   for (i = 0; i < h->nbuckets; i++)
139     {
140       b = &h->buckets[i];
141       if (b->offset == 0)
142         continue;
143       v = BV(clib_bihash_get_value) (h, b->offset);
144       for (j = 0; j < (1<<b->log2_pages); j++)
145         {
146           for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
147             {
148               if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
149                 continue;
150               
151               if (verbose && first_entry)
152                 {
153                   first_entry=0;
154                   vlib_cli_output (vm, 
155                                    "%=19s%=7s%=30s%=7s%=8s%=8s%=5s%=9s%=11s",
156                                    "Mac Address", "BD Idx", "Interface", 
157                                    "Index", "static", "filter", "bvi", 
158                                    "refresh", "timestamp");
159                 }
160
161               key.raw = v->kvp[k].key;
162               result.raw = v->kvp[k].value;
163
164               if (verbose
165                 & ((bd_index >>31) || (bd_index == key.fields.bd_index)))
166                 {
167                   vlib_cli_output (vm, 
168                                    "%=19U%=7d%=30U%=7d%=8d%=8d%=5d%=9d%=11X", 
169                                    format_ethernet_address, key.fields.mac,
170                                    key.fields.bd_index,
171                                    format_vnet_sw_if_index_name_with_NA, 
172                                    msm->vnet_main, result.fields.sw_if_index,
173                                    result.fields.sw_if_index == ~0 
174                                    ? -1 : result.fields.sw_if_index,
175                                    result.fields.static_mac,
176                                    result.fields.filter,
177                                    result.fields.bvi,
178                                    result.fields.refresh,
179                                    result.fields.timestamp);
180                 }
181               total_entries++;
182             }
183           v++;
184         }
185     }
186
187   if (total_entries == 0)
188     vlib_cli_output (vm, "no l2fib entries");
189   else
190     vlib_cli_output (vm, "%lld l2fib entries", total_entries);
191
192   if (raw)
193     vlib_cli_output (vm, "Raw Hash Table:\n%U\n",
194                      BV(format_bihash), h, 1 /* verbose */);
195
196   return 0;
197 }
198
199 VLIB_CLI_COMMAND (show_l2fib_cli, static) = {
200   .path = "show l2fib",
201   .short_help = "show l2fib [verbose | bd_id <nn> | bd_index <nn> | raw]",
202   .function = show_l2fib,
203 };
204
205
206 // Remove all entries from the l2fib
207 void l2fib_clear_table (uint keep_static)
208 {
209   l2fib_main_t * mp = &l2fib_main;
210
211   if (keep_static) {
212     // TODO: remove only non-static entries
213   } else {
214     // Remove all entries
215     BV(clib_bihash_free) (&mp->mac_table);
216     BV(clib_bihash_init) (&mp->mac_table, "l2fib mac table", 
217                           L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
218   }
219
220   l2learn_main.global_learn_count = 0;
221 }
222
223 // Clear all entries in L2FIB
224 // TODO: Later we may want a way to remove only the non-static entries
225 static clib_error_t *
226 clear_l2fib  (vlib_main_t * vm,
227               unformat_input_t * input,
228               vlib_cli_command_t * cmd)
229 {
230   l2fib_clear_table (0);
231   return 0;
232 }
233
234 VLIB_CLI_COMMAND (clear_l2fib_cli, static) = {
235   .path = "clear l2fib",
236   .short_help = "Clear l2fib mac forwarding entries",
237   .function = clear_l2fib,
238 };
239
240
241 // Add an entry to the l2fib.
242 // If the entry already exists then overwrite it
243 void l2fib_add_entry (u64   mac,
244                       u32   bd_index,
245                       u32   sw_if_index,
246                       u32   static_mac,
247                       u32   filter_mac,
248                       u32   bvi_mac) {
249   l2fib_entry_key_t key;
250   l2fib_entry_result_t result;
251   __attribute__((unused)) u32 bucket_contents;
252   l2fib_main_t * mp = &l2fib_main; 
253   BVT(clib_bihash_kv) kv;
254   
255   // set up key
256   key.raw = l2fib_make_key ((u8 *)&mac, bd_index);
257
258   // set up result
259   result.raw = 0; // clear all fields
260   result.fields.sw_if_index = sw_if_index;
261   result.fields.static_mac = static_mac;
262   result.fields.filter = filter_mac;
263   result.fields.bvi = bvi_mac;
264
265   kv.key = key.raw;
266   kv.value = result.raw;
267
268   BV(clib_bihash_add_del) (&mp->mac_table, &kv, 1 /* is_add */);
269
270   // increment counter if dynamically learned mac
271   if (result.fields.static_mac) {
272      l2learn_main.global_learn_count++;
273   }
274 }
275
276 // Add an entry to the L2FIB
277 // The CLI format is:
278 //    l2fib add <mac> <bd> <intf> [static] [bvi]
279 //    l2fib add <mac> <bd> filter  
280 // Note that filter and bvi entries are always static
281 static clib_error_t *
282 l2fib_add  (vlib_main_t * vm,
283             unformat_input_t * input,
284             vlib_cli_command_t * cmd)
285 {
286   bd_main_t * bdm = &bd_main;
287   vnet_main_t * vnm = vnet_get_main();
288   clib_error_t * error = 0;
289   u64 mac;
290   u32 bd_id;
291   u32 bd_index;
292   u32 sw_if_index = ~0;
293   u32 filter_mac = 0;
294   u32 static_mac = 0;
295   u32 bvi_mac = 0;
296   uword * p;
297
298   if (! unformat_user (input, unformat_ethernet_address, &mac))
299     {
300       error = clib_error_return (0, "expected mac address `%U'",
301                                  format_unformat_error, input);
302       goto done;
303     }
304  
305   if (!unformat (input, "%d", &bd_id)) {
306       error = clib_error_return (0, "expected bridge domain ID `%U'",
307                                  format_unformat_error, input);
308       goto done;
309   }
310
311   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
312   if (!p) {
313       error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
314       goto done;
315   }
316   bd_index = p[0];
317
318   if (unformat (input, "filter")) {
319     filter_mac = 1;
320     static_mac = 1;
321
322   } else {
323
324     if (! unformat_user (input, unformat_vnet_sw_interface, vnm, &sw_if_index)) {
325       error = clib_error_return (0, "unknown interface `%U'",
326                                  format_unformat_error, input);
327       goto done;
328     }
329     if (unformat (input, "static")) {
330       static_mac = 1;
331     } else if (unformat (input, "bvi")) {
332       bvi_mac = 1;
333       static_mac = 1;
334     }
335   }
336
337   l2fib_add_entry(mac, bd_index, sw_if_index, static_mac, filter_mac, bvi_mac);
338
339  done:
340   return error;
341 }
342
343 VLIB_CLI_COMMAND (l2fib_add_cli, static) = {
344   .path = "l2fib add",
345   .short_help = "Add l2fib mac forwarding entry  <mac> <bd-id> filter | <intf> [static | bvi]",
346   .function = l2fib_add,
347 };
348
349
350 static clib_error_t *
351 l2fib_test_command_fn  (vlib_main_t * vm,
352                         unformat_input_t * input,
353                         vlib_cli_command_t * cmd)
354 {
355   clib_error_t * error = 0;
356   u64 mac, save_mac;
357   u32 bd_index = 0;
358   u32 sw_if_index = 8;
359   u32 filter_mac = 0;
360   u32 bvi_mac = 0;
361   u32 is_add = 0;
362   u32 is_del = 0;
363   u32 is_check = 0;
364   u32 count = 1;
365   int mac_set = 0;
366   int i;
367
368   while (unformat_check_input(input) != UNFORMAT_END_OF_INPUT)
369     {
370       if (unformat (input, "mac %U", unformat_ethernet_address, &mac))
371         mac_set = 1;
372       else if (unformat (input, "add"))
373         is_add = 1;
374       else if (unformat (input, "del"))
375         is_del = 1;
376       else if (unformat (input, "check"))
377         is_check = 1;
378       else if (unformat (input, "count %d", &count))
379         ;
380       else
381         break;
382   }
383
384   if (mac_set == 0)
385     return clib_error_return (0, "mac not set");
386
387   if (is_add == 0 && is_del == 0 && is_check == 0)
388     return clib_error_return (0, "noop: pick at least one of (add,del,check)");
389
390   save_mac = mac;
391
392   if (is_add)
393     {
394       for (i = 0; i < count; i++)
395         {
396           u64 tmp;
397           l2fib_add_entry(mac, bd_index, sw_if_index, mac, 
398                           filter_mac, bvi_mac);
399           tmp = clib_net_to_host_u64(mac);
400           tmp >>= 16;
401           tmp++;
402           tmp <<= 16;
403           mac = clib_host_to_net_u64 (tmp);
404         }
405     }
406
407   if (is_check)
408     {
409       BVT(clib_bihash_kv) kv;
410       l2fib_main_t * mp = &l2fib_main;
411
412       mac = save_mac;
413
414       for (i = 0; i < count; i++)
415         {
416           u64 tmp;
417           kv.key = l2fib_make_key ((u8 *)&mac, bd_index);
418           if (BV(clib_bihash_search) (&mp->mac_table, &kv, &kv))
419             {
420               clib_warning ("key %U AWOL", format_ethernet_address, &mac);
421               break;
422             }
423           tmp = clib_net_to_host_u64(mac);
424           tmp >>= 16;
425           tmp++;
426           tmp <<= 16;
427           mac = clib_host_to_net_u64 (tmp);
428         }
429     }
430
431   if (is_del)
432     {
433       for (i = 0; i < count; i++)
434         {
435           u64 tmp;
436           
437           l2fib_del_entry (mac, bd_index);
438
439           tmp = clib_net_to_host_u64(mac);
440           tmp >>= 16;
441           tmp++;
442           tmp <<= 16;
443           mac = clib_host_to_net_u64 (tmp);
444         }
445     }
446
447   return error;
448 }
449
450 VLIB_CLI_COMMAND (l2fib_test_command, static) = {
451   .path = "test l2fib",
452   .short_help = "test l2fib [del] mac <base-addr> count <nn>",
453   .function = l2fib_test_command_fn,
454 };
455
456
457 // Delete an entry from the l2fib.
458 // Return 0 if the entry was deleted, or 1 if it was not found
459 u32 l2fib_del_entry (u64   mac,
460                      u32   bd_index) {
461
462   l2fib_entry_result_t result;
463   l2fib_main_t * mp = &l2fib_main;
464   BVT(clib_bihash_kv) kv;
465
466   // set up key
467   kv.key = l2fib_make_key ((u8 *)&mac, bd_index);
468
469   if (BV(clib_bihash_search) (&mp->mac_table, &kv, &kv))
470     return 1;
471
472   result.raw = kv.value;
473
474   // decrement counter if dynamically learned mac
475   if (result.fields.static_mac) {
476     if (l2learn_main.global_learn_count > 0) {
477       l2learn_main.global_learn_count--;
478     }
479   }
480
481   // Remove entry from hash table
482   BV(clib_bihash_add_del) (&mp->mac_table, &kv, 0 /* is_add */);
483   return 0;
484 }
485
486 // Delete an entry from the L2FIB
487 // The CLI format is:
488 //    l2fib del <mac> <bd-id> 
489 static clib_error_t *
490 l2fib_del  (vlib_main_t * vm,
491             unformat_input_t * input,
492             vlib_cli_command_t * cmd)
493 {
494   bd_main_t * bdm = &bd_main;
495   clib_error_t * error = 0;
496   u64 mac;
497   u32 bd_id;
498   u32 bd_index;
499   uword * p;
500
501   if (! unformat_user (input, unformat_ethernet_address, &mac))
502     {
503       error = clib_error_return (0, "expected mac address `%U'",
504                                  format_unformat_error, input);
505       goto done;
506     }
507  
508   if (!unformat (input, "%d", &bd_id)) {
509       error = clib_error_return (0, "expected bridge domain ID `%U'",
510                                  format_unformat_error, input);
511       goto done;
512   }
513
514   p = hash_get (bdm->bd_index_by_bd_id, bd_id);
515   if (!p) {
516       error = clib_error_return (0, "bridge domain ID %d invalid", bd_id);
517       goto done;
518   }
519   bd_index = p[0];
520
521   // Delete the entry
522   if (l2fib_del_entry(mac, bd_index)) {
523     error = clib_error_return (0, "mac entry not found");
524     goto done;
525   }
526
527  done:
528   return error;
529 }
530
531 VLIB_CLI_COMMAND (l2fib_del_cli, static) = {
532   .path = "l2fib del",
533   .short_help = "Delete l2fib mac forwarding entry  <mac> <bd-id>",
534   .function = l2fib_del,
535 };
536
537
538 BVT(clib_bihash) *get_mac_table(void) {
539   l2fib_main_t * mp = &l2fib_main;
540   return &mp->mac_table;
541 }
542
543 clib_error_t *l2fib_init (vlib_main_t *vm)
544 {
545   l2fib_main_t * mp = &l2fib_main;
546   l2fib_entry_key_t test_key;
547   u8 test_mac[6];
548     
549   mp->vlib_main = vm;
550   mp->vnet_main = vnet_get_main();
551
552   // Create the hash table 
553   BV(clib_bihash_init) (&mp->mac_table, "l2fib mac table",
554                         L2FIB_NUM_BUCKETS, L2FIB_MEMORY_SIZE);
555
556   // verify the key constructor is good, since it is endian-sensitive
557   test_mac[0] = 0x11;
558   test_key.raw = 0;
559   test_key.raw = l2fib_make_key ((u8 *)&test_mac, 0x1234);
560   ASSERT (test_key.fields.mac[0] == 0x11);
561   ASSERT (test_key.fields.bd_index == 0x1234);
562
563   return 0;
564 }
565
566 VLIB_INIT_FUNCTION (l2fib_init);
567