FD.io VPP  v16.06
Vector Packet Processing
l2_fib.c
Go to the documentation of this file.
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 
32 
33 typedef struct {
34 
35  /* hash table */
36  BVT(clib_bihash) mac_table;
37 
38  /* convenience variables */
41 } l2fib_main_t;
42 
44 
45 
46 // Format sw_if_index. If the value is ~0, use the text "N/A"
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",
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;
65  BVT(clib_bihash_value) * v;
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 *
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;
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",
170  key.fields.bd_index,
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",
218  }
219 
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 *
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
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) {
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 *
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 *
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 
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
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) {
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 *
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 
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",
555 
556  // verify the key constructor is good, since it is endian-sensitive
557  memset (test_mac, 0, sizeof(test_mac));
558  test_mac[0] = 0x11;
559  test_key.raw = 0;
560  test_key.raw = l2fib_make_key ((u8 *)&test_mac, 0x1234);
561  ASSERT (test_key.fields.mac[0] == 0x11);
562  ASSERT (test_key.fields.bd_index == 0x1234);
563 
564  return 0;
565 }
566 
568 
static clib_error_t * l2fib_test_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fib.c:351
u32 global_learn_count
Definition: l2_learn.h:31
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
u8 * format_vnet_sw_if_index_name_with_NA(u8 *s, va_list *args)
Definition: l2_fib.c:47
uword unformat(unformat_input_t *i, char *fmt,...)
Definition: unformat.c:942
#define UNFORMAT_END_OF_INPUT
Definition: format.h:142
u32 l2fib_del_entry(u64 mac, u32 bd_index)
Definition: l2_fib.c:459
clib_error_t * l2fib_init(vlib_main_t *vm)
Definition: l2_fib.c:543
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define L2FIB_MEMORY_SIZE
Definition: l2_fib.h:28
unformat_function_t unformat_vnet_sw_interface
Definition: l2_fib.h:50
int BV() clib_bihash_add_del(BVT(clib_bihash)*h, BVT(clib_bihash_kv)*add_v, int is_add)
static clib_error_t * l2fib_del(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fib.c:490
always_inline uword unformat_check_input(unformat_input_t *i)
Definition: format.h:168
vnet_main_t * vnet_get_main(void)
Definition: misc.c:45
u8 * format_ethernet_address(u8 *s, va_list *args)
Definition: format.c:43
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:109
void BV() clib_bihash_init(BVT(clib_bihash)*h, char *name, u32 nbuckets, uword memory_size)
#define clib_warning(format, args...)
Definition: error.h:59
unsigned long u64
Definition: types.h:89
int vlib_main(vlib_main_t *vm, unformat_input_t *input)
Definition: main.c:1538
uword unformat_user(unformat_input_t *input, unformat_function_t *func,...)
Definition: unformat.c:953
uword * bd_index_by_bd_id
Definition: l2_bd.h:26
int BV() clib_bihash_search(BVT(clib_bihash)*h, BVT(clib_bihash_kv)*search_key, BVT(clib_bihash_kv)*valuep)
#define hash_get(h, key)
Definition: hash.h:231
format_function_t format_vnet_sw_interface_name
#define BIHASH_KVP_PER_PAGE
Definition: bihash_24_8.h:18
always_inline u64 l2fib_make_key(u8 *mac_address, u16 bd_index)
Definition: l2_fib.h:84
vnet_main_t vnet_main
Definition: misc.c:42
u8 *BV() format_bihash(u8 *s, va_list *args)
#define L2FIB_NUM_BUCKETS
Definition: l2_fib.h:27
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
l2fib_main_t l2fib_main
Definition: l2_fib.c:43
u64 raw
Definition: l2_fib.h:63
static clib_error_t * clear_l2fib(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fib.c:226
BVT(clib_bihash)
Definition: l2_fib.c:538
static clib_error_t * show_l2fib(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fib.c:99
struct l2fib_entry_key_t::@124::@126 fields
void l2fib_clear_table(uint keep_static)
Definition: l2_fib.c:207
void l2fib_add_entry(u64 mac, u32 bd_index, u32 sw_if_index, u32 static_mac, u32 filter_mac, u32 bvi_mac)
Definition: l2_fib.c:243
struct l2fib_entry_result_t::@128::@130 fields
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:187
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
u8 * format_unformat_error(u8 *s, va_list *va)
Definition: unformat.c:87
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
Definition: l2_fib.h:33
#define BV(a)
void BV() clib_bihash_free(BVT(clib_bihash)*h)
u64 uword
Definition: types.h:112
static void *BV() clib_bihash_get_value(BVT(clib_bihash)*h, uword offset)
unsigned char u8
Definition: types.h:56
always_inline vnet_sw_interface_t * vnet_get_sw_interface(vnet_main_t *vnm, u32 sw_if_index)
void l2fib_table_dump(u32 bd_index, l2fib_entry_key_t **l2fe_key, l2fib_entry_result_t **l2fe_res)
Definition: l2_fib.c:59
l2learn_main_t l2learn_main
Definition: l2_learn.h:45
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t
bd_main_t bd_main
Definition: l2_bd.c:35
static clib_error_t * l2fib_add(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
Definition: l2_fib.c:282
u64 raw
Definition: l2_fib.h:43