FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
l2_fib.h
Go to the documentation of this file.
1 /*
2  * l2_fib.h : 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 #ifndef included_l2fib_h
19 #define included_l2fib_h
20 
21 #include <vlib/vlib.h>
22 #include <vppinfra/bihash_8_8.h>
23 
24 /*
25  * The size of the hash table
26  */
27 #define L2FIB_NUM_BUCKETS (64 * 1024)
28 #define L2FIB_MEMORY_SIZE (512<<20)
29 
30 /* Ager scan interval is 1 minute for aging */
31 #define L2FIB_AGE_SCAN_INTERVAL (60.0)
32 
33 /* MAC event scan delay is 100 msec unless specified by MAC event client */
34 #define L2FIB_EVENT_SCAN_DELAY_DEFAULT (0.1)
35 
36 /* Max MACs in a event message is 100 unless specified by MAC event client */
37 #define L2FIB_EVENT_MAX_MACS_DEFAULT (100)
38 
39 /* MAC event learn limit is 1000 unless specified by MAC event client */
40 #define L2FIB_EVENT_LEARN_LIMIT_DEFAULT (1000)
41 
42 typedef struct
43 {
44 
45  /* hash table */
46  BVT (clib_bihash) mac_table;
47 
48  /* per swif vector of sequence number for interface based flush of MACs */
50 
51  /* last event or ager scan duration */
54 
55  /* delay between event scans, default to 100 msec */
57 
58  /* max macs in event message, default to 100 entries */
60 
61  /* convenience variables */
64 } l2fib_main_t;
65 
67 
68 /*
69  * The L2fib key is the mac address and bridge domain ID
70  */
71 typedef struct
72 {
73  union
74  {
75  struct
76  {
78  u8 mac[6];
79  } fields;
80  struct
81  {
84  } words;
86  };
88 
90 
91 
92 typedef struct
93 {
94  union
95  {
96  struct
97  {
99  u8 bd;
100  };
102  };
104 
105 /**
106  * Flags associated with an L2 Fib Entry
107  * - static mac, no MAC move
108  * - not subject to age
109  * - mac is for a bridged virtual interface
110  * - drop packets to/from this mac
111  * - MAC learned to be sent in L2 MAC event
112  * -MAC learned is a MAC move
113  */
114 #define foreach_l2fib_entry_result_attr \
115  _(STATIC, 0, "static") \
116  _(AGE_NOT, 1, "age-not") \
117  _(BVI, 2, "bvi") \
118  _(FILTER, 3, "filter") \
119  _(LRN_EVT, 4, "learn-event") \
120  _(LRN_MOV, 5, "learn-move")
121 
123 {
125 #define _(a,v,s) L2FIB_ENTRY_RESULT_FLAG_##a = (1 << v),
127 #undef _
128 } __attribute__ ((packed)) l2fib_entry_result_flags_t;
129 
130 STATIC_ASSERT_SIZEOF (l2fib_entry_result_flags_t, 1);
131 
132 extern u8 *format_l2fib_entry_result_flags (u8 * s, va_list * args);
133 
134 /*
135  * The l2fib entry results
136  */
137 typedef struct l2fib_entry_result_t_
138 {
139  union
140  {
141  struct
142  {
143  u32 sw_if_index; /* output sw_if_index (L3 intf if bvi==1) */
144  l2fib_entry_result_flags_t flags;
145 
146  u8 timestamp; /* timestamp for aging */
147  l2fib_seq_num_t sn; /* bd/int seq num */
148  } fields;
150  };
152 
154 
155 #define _(a,v,s) \
156  always_inline int \
157  l2fib_entry_result_is_set_##a (const l2fib_entry_result_t *r) { \
158  return (r->fields.flags & L2FIB_ENTRY_RESULT_FLAG_##a); \
159  }
161 #undef _
162 #define _(a,v,s) \
163  always_inline void \
164  l2fib_entry_result_set_##a (l2fib_entry_result_t *r) { \
165  r->fields.flags |= L2FIB_ENTRY_RESULT_FLAG_##a; \
166  }
168 #undef _
169 #define _(a,v,s) \
170  always_inline void \
171  l2fib_entry_result_clear_##a (l2fib_entry_result_t *r) { \
172  r->fields.flags &= ~L2FIB_ENTRY_RESULT_FLAG_##a; \
173  }
175 #undef _
176  static inline void
178  l2fib_entry_result_flags_t bits)
179 {
180  r->fields.flags |= bits;
181 }
182 
183 static inline void
185  l2fib_entry_result_flags_t bits)
186 {
187  r->fields.flags &= ~bits;
188 }
189 
190 /* L2 MAC event entry action enums (see mac_entry definition in l2.api) */
191 typedef enum
192 {
197 
198 /**
199  * Compute the hash for the given key and return
200  * the corresponding bucket index
201  */
204 {
205  u32 result;
206  u32 temp_a;
207  u32 temp_b;
208 
209  result = 0xa5a5a5a5; /* some seed */
210  temp_a = key->words.w0;
211  temp_b = key->words.w1;
212  hash_mix32 (temp_a, temp_b, result);
213 
214  return result % L2FIB_NUM_BUCKETS;
215 }
216 
217 /**
218  * make address sanitizer skip this:
219  * The 6-Bytes mac-address is cast into an 8-Bytes u64, with 2 additional Bytes.
220  * l2fib_make_key() does read those two Bytes but does not use them.
221  */
222 always_inline u64 __attribute__ ((no_sanitize_address))
223 l2fib_make_key (const u8 * mac_address, u16 bd_index)
224 {
225  u64 temp;
226 
227  /*
228  * The mac address in memory is A:B:C:D:E:F
229  * The bd id in register is H:L
230  */
231 #if CLIB_ARCH_IS_LITTLE_ENDIAN
232  /*
233  * Create the in-register key as F:E:D:C:B:A:H:L
234  * In memory the key is L:H:A:B:C:D:E:F
235  */
236  temp = *((u64 *) (mac_address)) << 16;
237  temp = (temp & ~0xffff) | (u64) (bd_index);
238 #else
239  /*
240  * Create the in-register key as H:L:A:B:C:D:E:F
241  * In memory the key is H:L:A:B:C:D:E:F
242  */
243  temp = *((u64 *) (mac_address)) >> 16;
244  temp = temp | (((u64) bd_index) << 48);
245 #endif
246 
247  return temp;
248 }
249 
250 
251 
252 /**
253  * Lookup the entry for mac and bd_index in the mac table for 1 packet.
254  * Cached_key and cached_result are used as a one-entry cache.
255  * The function reads and updates them as needed.
256  *
257  * mac0 and bd_index0 are the keys. The entry is written to result0.
258  * If the entry was not found, result0 is set to ~0.
259  *
260  * key0 return with the computed key, convenient if the entry needs,
261  * to be updated afterward.
262  */
263 
265 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
266  l2fib_entry_key_t * cached_key,
267  l2fib_entry_result_t * cached_result,
268  u8 * mac0,
269  u16 bd_index0,
270  l2fib_entry_key_t * key0, l2fib_entry_result_t * result0)
271 {
272  /* set up key */
273  key0->raw = l2fib_make_key (mac0, bd_index0);
274 
275  if (key0->raw == cached_key->raw)
276  {
277  /* Hit in the one-entry cache */
278  result0->raw = cached_result->raw;
279  }
280  else
281  {
282  /* Do a regular mac table lookup */
283  BVT (clib_bihash_kv) kv;
284 
285  kv.key = key0->raw;
286  kv.value = ~0ULL;
287  BV (clib_bihash_search_inline) (mac_table, &kv);
288  result0->raw = kv.value;
289 
290  /* Update one-entry cache */
291  cached_key->raw = key0->raw;
292  cached_result->raw = result0->raw;
293  }
294 }
295 
296 
297 /**
298  * Lookup the entry for mac and bd_index in the mac table for 2 packets.
299  * The lookups for the two packets are interleaved.
300  *
301  * Cached_key and cached_result are used as a one-entry cache.
302  * The function reads and updates them as needed.
303  *
304  * mac0 and bd_index0 are the keys. The entry is written to result0.
305  * If the entry was not found, result0 is set to ~0. The same
306  * holds for mac1/bd_index1/result1.
307  */
309 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
310  l2fib_entry_key_t * cached_key,
311  l2fib_entry_result_t * cached_result,
312  u8 * mac0,
313  u8 * mac1,
314  u16 bd_index0,
315  u16 bd_index1,
316  l2fib_entry_key_t * key0,
317  l2fib_entry_key_t * key1,
318  l2fib_entry_result_t * result0,
319  l2fib_entry_result_t * result1)
320 {
321  /* set up key */
322  key0->raw = l2fib_make_key (mac0, bd_index0);
323  key1->raw = l2fib_make_key (mac1, bd_index1);
324 
325  if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
326  {
327  /* Both hit in the one-entry cache */
328  result0->raw = cached_result->raw;
329  result1->raw = cached_result->raw;
330  }
331  else
332  {
333  BVT (clib_bihash_kv) kv0, kv1;
334 
335  /*
336  * Do a regular mac table lookup
337  * Interleave lookups for packet 0 and packet 1
338  */
339  kv0.key = key0->raw;
340  kv1.key = key1->raw;
341  kv0.value = ~0ULL;
342  kv1.value = ~0ULL;
343 
344  BV (clib_bihash_search_inline) (mac_table, &kv0);
345  BV (clib_bihash_search_inline) (mac_table, &kv1);
346 
347  result0->raw = kv0.value;
348  result1->raw = kv1.value;
349 
350  /* Update one-entry cache */
351  cached_key->raw = key1->raw;
352  cached_result->raw = result1->raw;
353  }
354 }
355 
357 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
358  l2fib_entry_key_t * cached_key,
359  l2fib_entry_result_t * cached_result,
360  const u8 * mac0,
361  const u8 * mac1,
362  const u8 * mac2,
363  const u8 * mac3,
364  u16 bd_index0,
365  u16 bd_index1,
366  u16 bd_index2,
367  u16 bd_index3,
368  l2fib_entry_key_t * key0,
369  l2fib_entry_key_t * key1,
370  l2fib_entry_key_t * key2,
371  l2fib_entry_key_t * key3,
372  l2fib_entry_result_t * result0,
373  l2fib_entry_result_t * result1,
374  l2fib_entry_result_t * result2,
375  l2fib_entry_result_t * result3)
376 {
377  /* set up key */
378  key0->raw = l2fib_make_key (mac0, bd_index0);
379  key1->raw = l2fib_make_key (mac1, bd_index1);
380  key2->raw = l2fib_make_key (mac2, bd_index2);
381  key3->raw = l2fib_make_key (mac3, bd_index3);
382 
383  if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
384  (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
385  {
386  /* Both hit in the one-entry cache */
387  result0->raw = cached_result->raw;
388  result1->raw = cached_result->raw;
389  result2->raw = cached_result->raw;
390  result3->raw = cached_result->raw;
391  }
392  else
393  {
394  BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
395 
396  /*
397  * Do a regular mac table lookup
398  * Interleave lookups for packet 0 and packet 1
399  */
400  kv0.key = key0->raw;
401  kv1.key = key1->raw;
402  kv2.key = key2->raw;
403  kv3.key = key3->raw;
404  kv0.value = ~0ULL;
405  kv1.value = ~0ULL;
406  kv2.value = ~0ULL;
407  kv3.value = ~0ULL;
408 
409  BV (clib_bihash_search_inline) (mac_table, &kv0);
410  BV (clib_bihash_search_inline) (mac_table, &kv1);
411  BV (clib_bihash_search_inline) (mac_table, &kv2);
412  BV (clib_bihash_search_inline) (mac_table, &kv3);
413 
414  result0->raw = kv0.value;
415  result1->raw = kv1.value;
416  result2->raw = kv2.value;
417  result3->raw = kv3.value;
418 
419  /* Update one-entry cache */
420  cached_key->raw = key1->raw;
421  cached_result->raw = result1->raw;
422  }
423 }
424 
425 void l2fib_clear_table (void);
426 
427 void
428 l2fib_add_entry (const u8 * mac,
429  u32 bd_index,
430  u32 sw_if_index, l2fib_entry_result_flags_t flags);
431 
432 static inline void
433 l2fib_add_filter_entry (const u8 * mac, u32 bd_index)
434 {
435  l2fib_add_entry (mac, bd_index, ~0,
436  (L2FIB_ENTRY_RESULT_FLAG_FILTER |
437  L2FIB_ENTRY_RESULT_FLAG_STATIC));
438 }
439 
440 u32 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index);
441 
443 
444 void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
445 
446 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
447 
448 void l2fib_flush_all_mac (vlib_main_t * vm);
449 
450 void
451 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
452  l2fib_entry_result_t ** l2fe_res);
453 
454 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
455 
457 l2fib_swif_seq_num (u32 sw_if_index)
458 {
459  l2fib_main_t *mp = &l2fib_main;
460  return vec_elt_at_index (mp->swif_seq_num, sw_if_index);
461 }
462 
465 {
466  l2fib_main_t *mp = &l2fib_main;
467  vec_validate (mp->swif_seq_num, sw_if_index);
468  return l2fib_swif_seq_num (sw_if_index);
469 }
470 
471 BVT (clib_bihash) * get_mac_table (void);
472 
473 #endif
474 
475 /*
476  * fd.io coding-style-patch-verification: ON
477  *
478  * Local Variables:
479  * eval: (c-set-style "gnu")
480  * End:
481  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
f64 evt_scan_duration
Definition: l2_fib.h:52
vlib_main_t * vlib_main
Definition: l2_fib.h:62
vnet_main_t * vnet_main
Definition: l2_fib.h:63
unsigned long u64
Definition: types.h:89
u64 raw
Definition: l2_fib.h:149
u32 w1
Definition: l2_fib.h:83
l2fib_entry_result_flags_t flags
Definition: l2_fib.h:144
static_always_inline u8 * l2fib_swif_seq_num(u32 sw_if_index)
Definition: l2_fib.h:457
u32 max_macs_in_event
Definition: l2_fib.h:59
enum l2fib_entry_result_flags_t_ l2fib_entry_result_flags_t
unsigned char u8
Definition: types.h:56
double f64
Definition: types.h:142
Definition: l2_fib.h:137
u32 w0
Definition: l2_fib.h:82
#define static_always_inline
Definition: clib.h:99
#define always_inline
Definition: clib.h:98
static_always_inline void l2fib_lookup_1(BVT(clib_bihash) *mac_table, l2fib_entry_key_t *cached_key, l2fib_entry_result_t *cached_result, u8 *mac0, u16 bd_index0, l2fib_entry_key_t *key0, l2fib_entry_result_t *result0)
Lookup the entry for mac and bd_index in the mac table for 1 packet.
Definition: l2_fib.h:265
static void l2fib_entry_result_clear_bits(l2fib_entry_result_t *r, l2fib_entry_result_flags_t bits)
Definition: l2_fib.h:184
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
struct l2fib_entry_key_t::@254::@257 words
void l2fib_flush_all_mac(vlib_main_t *vm)
Flush all non static MACs - flushes all valid BDs.
Definition: l2_fib.c:826
unsigned int u32
Definition: types.h:88
struct l2fib_entry_result_t_ l2fib_entry_result_t
struct l2fib_entry_result_t_::@262::@264 fields
u32 sw_if_index
Definition: l2_fib.h:143
f64 event_scan_delay
Definition: l2_fib.h:56
static_always_inline u8 * l2fib_valid_swif_seq_num(u32 sw_if_index)
Definition: l2_fib.h:464
unsigned short u16
Definition: types.h:57
u8 * format_l2fib_entry_result_flags(u8 *s, va_list *args)
Definition: l2_fib.c:58
void l2fib_add_entry(const u8 *mac, u32 bd_index, u32 sw_if_index, l2fib_entry_result_flags_t flags)
Add an entry to the l2fib.
Definition: l2_fib.c:401
BVT(clib_bihash) *get_mac_table(void)
Definition: l2_fib.c:968
static_always_inline void l2fib_lookup_4(BVT(clib_bihash) *mac_table, l2fib_entry_key_t *cached_key, l2fib_entry_result_t *cached_result, const u8 *mac0, const u8 *mac1, const u8 *mac2, const u8 *mac3, u16 bd_index0, u16 bd_index1, u16 bd_index2, u16 bd_index3, l2fib_entry_key_t *key0, l2fib_entry_key_t *key1, l2fib_entry_key_t *key2, l2fib_entry_key_t *key3, l2fib_entry_result_t *result0, l2fib_entry_result_t *result1, l2fib_entry_result_t *result2, l2fib_entry_result_t *result3)
Definition: l2_fib.h:357
STATIC_ASSERT_SIZEOF(l2fib_entry_key_t, 8)
int clib_bihash_search_inline(clib_bihash *h, clib_bihash_kv *in_out_kv)
Search a bi-hash table.
#define L2FIB_NUM_BUCKETS
Definition: l2_fib.h:27
l2fib_seq_num_t sn
Definition: l2_fib.h:147
static_always_inline void l2fib_lookup_2(BVT(clib_bihash) *mac_table, l2fib_entry_key_t *cached_key, l2fib_entry_result_t *cached_result, u8 *mac0, u8 *mac1, u16 bd_index0, u16 bd_index1, l2fib_entry_key_t *key0, l2fib_entry_key_t *key1, l2fib_entry_result_t *result0, l2fib_entry_result_t *result1)
Lookup the entry for mac and bd_index in the mac table for 2 packets.
Definition: l2_fib.h:309
l2fib_entry_result_flags_t_
Definition: l2_fib.h:122
u8 * swif_seq_num
Definition: l2_fib.h:49
f64 age_scan_duration
Definition: l2_fib.h:53
void l2fib_flush_bd_mac(vlib_main_t *vm, u32 bd_index)
Flush all non static MACs in a bridge domain.
Definition: l2_fib.c:815
#define hash_mix32(a0, b0, c0)
Definition: hash.h:539
static u64 l2fib_make_key(const u8 *mac_address, u16 bd_index)
make address sanitizer skip this: The 6-Bytes mac-address is cast into an 8-Bytes u64...
Definition: l2_fib.h:223
u8 timestamp
Definition: l2_fib.h:146
void l2fib_clear_table(void)
Definition: l2_fib.c:343
Definition: l2_fib.h:71
void l2fib_table_dump(u32 bd_index, l2fib_entry_key_t **l2fe_key, l2fib_entry_result_t **l2fe_res)
Definition: l2_fib.c:132
void l2fib_flush_int_mac(vlib_main_t *vm, u32 sw_if_index)
Flush all non static MACs from an interface.
Definition: l2_fib.c:805
u8 mac_address[6]
#define foreach_l2fib_entry_result_attr
Flags associated with an L2 Fib Entry.
Definition: l2_fib.h:114
u32 l2fib_del_entry(const u8 *mac, u32 bd_index, u32 sw_if_index)
Delete an entry from the l2fib.
Definition: l2_fib.c:685
l2_mac_event_action_t
Definition: l2_fib.h:191
u8 * format_vnet_sw_if_index_name_with_NA(u8 *s, va_list *args)
Format sw_if_index.
Definition: l2_fib.c:91
static u32 l2fib_compute_hash_bucket(l2fib_entry_key_t *key)
Compute the hash for the given key and return the corresponding bucket index.
Definition: l2_fib.h:203
typedef key
Definition: ipsec.api:244
u16 bd_index
Definition: l2_fib.h:77
static void l2fib_add_filter_entry(const u8 *mac, u32 bd_index)
Definition: l2_fib.h:433
static foreach_l2fib_entry_result_attr void l2fib_entry_result_set_bits(l2fib_entry_result_t *r, l2fib_entry_result_flags_t bits)
Definition: l2_fib.h:177
l2fib_main_t l2fib_main
Definition: l2_fib.c:55
u64 raw
Definition: l2_fib.h:85
void l2fib_start_ager_scan(vlib_main_t *vm)
Kick off ager to scan MACs to age/delete MAC entries.
Definition: l2_fib.c:782