blob: d8b37d2a5869b6f761719506c5b9e50c79d7dd2c [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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)
John Lodbfa5742017-09-02 08:27:36 -040028#define L2FIB_MEMORY_SIZE (512<<20)
Ed Warnickecb9cada2015-12-08 15:45:58 -070029
John Lo8d00fff2017-08-03 00:35:36 -040030/* 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
Eyal Bari0f360dc2017-06-14 13:11:20 +030042typedef 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 */
49 u8 *swif_seq_num;
50
John Lo8d00fff2017-08-03 00:35:36 -040051 /* last event or ager scan duration */
52 f64 evt_scan_duration;
53 f64 age_scan_duration;
54
55 /* delay between event scans, default to 100 msec */
56 f64 event_scan_delay;
57
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -070058 /* max macs in event message, default to 100 entries */
John Lo8d00fff2017-08-03 00:35:36 -040059 u32 max_macs_in_event;
60
Eyal Bari0f360dc2017-06-14 13:11:20 +030061 /* convenience variables */
62 vlib_main_t *vlib_main;
63 vnet_main_t *vnet_main;
64} l2fib_main_t;
65
66extern l2fib_main_t l2fib_main;
67
Ed Warnickecb9cada2015-12-08 15:45:58 -070068/*
69 * The L2fib key is the mac address and bridge domain ID
70 */
Dave Barach97d8dc22016-08-15 15:31:15 -040071typedef struct
72{
73 union
74 {
75 struct
76 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 u16 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -040078 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 } fields;
Dave Barach97d8dc22016-08-15 15:31:15 -040080 struct
81 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 u32 w0;
83 u32 w1;
84 } words;
85 u64 raw;
86 };
87} l2fib_entry_key_t;
88
Damjan Mariond171d482016-12-05 14:16:38 +010089STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
90
Eyal Bari7537e712017-04-27 14:07:55 +030091
92typedef struct
93{
94 union
95 {
96 struct
97 {
98 u8 swif;
99 u8 bd;
100 };
101 u16 as_u16;
102 };
103} l2fib_seq_num_t;
104
Neale Rannsb54d0812018-09-06 06:22:56 -0700105/**
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
122typedef enum l2fib_entry_result_flags_t_
123{
124 L2FIB_ENTRY_RESULT_FLAG_NONE = 0,
125#define _(a,v,s) L2FIB_ENTRY_RESULT_FLAG_##a = (1 << v),
126 foreach_l2fib_entry_result_attr
127#undef _
128} __attribute__ ((packed)) l2fib_entry_result_flags_t;
129
130STATIC_ASSERT_SIZEOF (l2fib_entry_result_flags_t, 1);
131
Neale Ranns7d645f72018-10-24 02:25:06 -0700132extern u8 *format_l2fib_entry_result_flags (u8 * s, va_list * args);
133
Dave Barach97d8dc22016-08-15 15:31:15 -0400134/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 * The l2fib entry results
136 */
Neale Rannsb54d0812018-09-06 06:22:56 -0700137typedef struct l2fib_entry_result_t_
Dave Barach97d8dc22016-08-15 15:31:15 -0400138{
139 union
140 {
141 struct
142 {
John Lo8d00fff2017-08-03 00:35:36 -0400143 u32 sw_if_index; /* output sw_if_index (L3 intf if bvi==1) */
Neale Rannsb54d0812018-09-06 06:22:56 -0700144 l2fib_entry_result_flags_t flags;
John Lo8d00fff2017-08-03 00:35:36 -0400145
Dave Barach97d8dc22016-08-15 15:31:15 -0400146 u8 timestamp; /* timestamp for aging */
Eyal Bari7537e712017-04-27 14:07:55 +0300147 l2fib_seq_num_t sn; /* bd/int seq num */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700148 } fields;
149 u64 raw;
150 };
151} l2fib_entry_result_t;
152
Damjan Mariond171d482016-12-05 14:16:38 +0100153STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Neale Rannsb54d0812018-09-06 06:22:56 -0700155#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 }
160foreach_l2fib_entry_result_attr
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 }
167 foreach_l2fib_entry_result_attr
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 }
174 foreach_l2fib_entry_result_attr
175#undef _
176 static inline void
177l2fib_entry_result_set_bits (l2fib_entry_result_t * r,
178 l2fib_entry_result_flags_t bits)
179{
180 r->fields.flags |= bits;
181}
182
183static inline void
184l2fib_entry_result_clear_bits (l2fib_entry_result_t * r,
185 l2fib_entry_result_flags_t bits)
186{
187 r->fields.flags &= ~bits;
188}
189
John Loe23c99e2018-03-13 21:53:18 -0400190/* L2 MAC event entry action enums (see mac_entry definition in l2.api) */
191typedef enum
192{
193 MAC_EVENT_ACTION_ADD = 0,
194 MAC_EVENT_ACTION_DELETE = 1,
195 MAC_EVENT_ACTION_MOVE = 2,
196} l2_mac_event_action_t;
197
Dave Barach97d8dc22016-08-15 15:31:15 -0400198/**
199 * Compute the hash for the given key and return
200 * the corresponding bucket index
201 */
202always_inline u32
203l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
204{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 u32 result;
206 u32 temp_a;
207 u32 temp_b;
208
Dave Barach97d8dc22016-08-15 15:31:15 -0400209 result = 0xa5a5a5a5; /* some seed */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700210 temp_a = key->words.w0;
211 temp_b = key->words.w1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 hash_mix32 (temp_a, temp_b, result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213
214 return result % L2FIB_NUM_BUCKETS;
215}
216
Gabriel Gannea30d9032017-10-24 10:24:57 +0200217/**
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 */
222always_inline u64 __attribute__ ((no_sanitize_address))
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700223l2fib_make_key (const u8 * mac_address, u16 bd_index)
Dave Barach97d8dc22016-08-15 15:31:15 -0400224{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225 u64 temp;
226
Dave Barach97d8dc22016-08-15 15:31:15 -0400227 /*
228 * The mac address in memory is A:B:C:D:E:F
229 * The bd id in register is H:L
230 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231#if CLIB_ARCH_IS_LITTLE_ENDIAN
Dave Barach97d8dc22016-08-15 15:31:15 -0400232 /*
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 */
Damjan Marion30230dd2016-11-24 22:20:05 +0100236 temp = *((u64 *) (mac_address)) << 16;
Dave Barach97d8dc22016-08-15 15:31:15 -0400237 temp = (temp & ~0xffff) | (u64) (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238#else
Dave Barach97d8dc22016-08-15 15:31:15 -0400239 /*
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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245#endif
246
247 return temp;
248}
249
250
251
Dave Barach97d8dc22016-08-15 15:31:15 -0400252/**
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 and bucket0 return with the computed key and hash bucket,
261 * convenient if the entry needs to be updated afterward.
262 * If the cached_result was used, bucket0 is set to ~0.
263 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
265static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400266l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
267 l2fib_entry_key_t * cached_key,
268 l2fib_entry_result_t * cached_result,
269 u8 * mac0,
270 u16 bd_index0,
271 l2fib_entry_key_t * key0,
272 u32 * bucket0, l2fib_entry_result_t * result0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273{
Dave Barach97d8dc22016-08-15 15:31:15 -0400274 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 key0->raw = l2fib_make_key (mac0, bd_index0);
276 *bucket0 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Dave Barach97d8dc22016-08-15 15:31:15 -0400278 if (key0->raw == cached_key->raw)
279 {
280 /* Hit in the one-entry cache */
281 result0->raw = cached_result->raw;
282 }
283 else
284 {
285 /* Do a regular mac table lookup */
286 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287
Dave Barach97d8dc22016-08-15 15:31:15 -0400288 kv.key = key0->raw;
289 kv.value = ~0ULL;
290 BV (clib_bihash_search_inline) (mac_table, &kv);
291 result0->raw = kv.value;
292
293 /* Update one-entry cache */
294 cached_key->raw = key0->raw;
295 cached_result->raw = result0->raw;
296 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297}
298
299
Dave Barach97d8dc22016-08-15 15:31:15 -0400300/**
301 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
302 * The lookups for the two packets are interleaved.
303 *
304 * Cached_key and cached_result are used as a one-entry cache.
305 * The function reads and updates them as needed.
306 *
307 * mac0 and bd_index0 are the keys. The entry is written to result0.
308 * If the entry was not found, result0 is set to ~0. The same
309 * holds for mac1/bd_index1/result1.
310 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400312l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
313 l2fib_entry_key_t * cached_key,
314 l2fib_entry_result_t * cached_result,
315 u8 * mac0,
316 u8 * mac1,
317 u16 bd_index0,
318 u16 bd_index1,
319 l2fib_entry_key_t * key0,
320 l2fib_entry_key_t * key1,
321 u32 * bucket0,
322 u32 * bucket1,
323 l2fib_entry_result_t * result0,
324 l2fib_entry_result_t * result1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325{
Dave Barach97d8dc22016-08-15 15:31:15 -0400326 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 key0->raw = l2fib_make_key (mac0, bd_index0);
328 key1->raw = l2fib_make_key (mac1, bd_index1);
329
Dave Barach97d8dc22016-08-15 15:31:15 -0400330 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
331 {
332 /* Both hit in the one-entry cache */
333 result0->raw = cached_result->raw;
334 result1->raw = cached_result->raw;
335 *bucket0 = ~0;
336 *bucket1 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337
Dave Barach97d8dc22016-08-15 15:31:15 -0400338 }
339 else
340 {
341 BVT (clib_bihash_kv) kv0, kv1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Dave Barach97d8dc22016-08-15 15:31:15 -0400343 /*
344 * Do a regular mac table lookup
345 * Interleave lookups for packet 0 and packet 1
346 */
347 kv0.key = key0->raw;
348 kv1.key = key1->raw;
349 kv0.value = ~0ULL;
350 kv1.value = ~0ULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351
Dave Barach97d8dc22016-08-15 15:31:15 -0400352 BV (clib_bihash_search_inline) (mac_table, &kv0);
353 BV (clib_bihash_search_inline) (mac_table, &kv1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barach97d8dc22016-08-15 15:31:15 -0400355 result0->raw = kv0.value;
356 result1->raw = kv1.value;
357
358 /* Update one-entry cache */
359 cached_key->raw = key1->raw;
360 cached_result->raw = result1->raw;
361 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362}
363
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800364static_always_inline void
365l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
366 l2fib_entry_key_t * cached_key,
367 l2fib_entry_result_t * cached_result,
Neale Rannseb1525f2018-09-09 04:41:02 -0400368 const u8 * mac0,
369 const u8 * mac1,
370 const u8 * mac2,
371 const u8 * mac3,
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800372 u16 bd_index0,
373 u16 bd_index1,
374 u16 bd_index2,
375 u16 bd_index3,
376 l2fib_entry_key_t * key0,
377 l2fib_entry_key_t * key1,
378 l2fib_entry_key_t * key2,
379 l2fib_entry_key_t * key3,
380 u32 * bucket0,
381 u32 * bucket1,
382 u32 * bucket2,
383 u32 * bucket3,
384 l2fib_entry_result_t * result0,
385 l2fib_entry_result_t * result1,
386 l2fib_entry_result_t * result2,
387 l2fib_entry_result_t * result3)
388{
389 /* set up key */
390 key0->raw = l2fib_make_key (mac0, bd_index0);
391 key1->raw = l2fib_make_key (mac1, bd_index1);
392 key2->raw = l2fib_make_key (mac2, bd_index2);
393 key3->raw = l2fib_make_key (mac3, bd_index3);
394
395 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
396 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
397 {
398 /* Both hit in the one-entry cache */
399 result0->raw = cached_result->raw;
400 result1->raw = cached_result->raw;
401 result2->raw = cached_result->raw;
402 result3->raw = cached_result->raw;
403 *bucket0 = ~0;
404 *bucket1 = ~0;
405 *bucket2 = ~0;
406 *bucket3 = ~0;
407
408 }
409 else
410 {
411 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
412
413 /*
414 * Do a regular mac table lookup
415 * Interleave lookups for packet 0 and packet 1
416 */
417 kv0.key = key0->raw;
418 kv1.key = key1->raw;
419 kv2.key = key2->raw;
420 kv3.key = key3->raw;
421 kv0.value = ~0ULL;
422 kv1.value = ~0ULL;
423 kv2.value = ~0ULL;
424 kv3.value = ~0ULL;
425
426 BV (clib_bihash_search_inline) (mac_table, &kv0);
427 BV (clib_bihash_search_inline) (mac_table, &kv1);
428 BV (clib_bihash_search_inline) (mac_table, &kv2);
429 BV (clib_bihash_search_inline) (mac_table, &kv3);
430
431 result0->raw = kv0.value;
432 result1->raw = kv1.value;
433 result2->raw = kv2.value;
434 result3->raw = kv3.value;
435
436 /* Update one-entry cache */
437 cached_key->raw = key1->raw;
438 cached_result->raw = result1->raw;
439 }
440}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Eyal Bari7537e712017-04-27 14:07:55 +0300442void l2fib_clear_table (void);
John Loda1f2c72017-03-24 20:11:15 -0400443
444void
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700445l2fib_add_entry (const u8 * mac,
John Loda1f2c72017-03-24 20:11:15 -0400446 u32 bd_index,
Neale Rannsb54d0812018-09-06 06:22:56 -0700447 u32 sw_if_index, l2fib_entry_result_flags_t flags);
Eyal Bari31a71ab2017-06-25 14:42:33 +0300448
449static inline void
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700450l2fib_add_filter_entry (const u8 * mac, u32 bd_index)
Eyal Bari31a71ab2017-06-25 14:42:33 +0300451{
Neale Rannsb54d0812018-09-06 06:22:56 -0700452 l2fib_add_entry (mac, bd_index, ~0,
453 (L2FIB_ENTRY_RESULT_FLAG_FILTER |
454 L2FIB_ENTRY_RESULT_FLAG_STATIC));
Eyal Bari31a71ab2017-06-25 14:42:33 +0300455}
456
Neale Ranns3b81a1e2018-09-06 09:50:26 -0700457u32 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index);
John Loda1f2c72017-03-24 20:11:15 -0400458
459void l2fib_start_ager_scan (vlib_main_t * vm);
460
461void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
462
463void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
464
Eyal Bari7537e712017-04-27 14:07:55 +0300465void l2fib_flush_all_mac (vlib_main_t * vm);
466
John Loda1f2c72017-03-24 20:11:15 -0400467void
468l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
469 l2fib_entry_result_t ** l2fe_res);
470
471u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
472
Eyal Bari0f360dc2017-06-14 13:11:20 +0300473static_always_inline u8 *
474l2fib_swif_seq_num (u32 sw_if_index)
475{
476 l2fib_main_t *mp = &l2fib_main;
Eyal Bari0f360dc2017-06-14 13:11:20 +0300477 return vec_elt_at_index (mp->swif_seq_num, sw_if_index);
478}
479
Eyal Barib12ac562017-07-18 13:25:19 +0300480static_always_inline u8 *
481l2fib_valid_swif_seq_num (u32 sw_if_index)
482{
483 l2fib_main_t *mp = &l2fib_main;
484 vec_validate (mp->swif_seq_num, sw_if_index);
485 return l2fib_swif_seq_num (sw_if_index);
486}
487
Dave Barach97d8dc22016-08-15 15:31:15 -0400488BVT (clib_bihash) * get_mac_table (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
490#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400491
492/*
493 * fd.io coding-style-patch-verification: ON
494 *
495 * Local Variables:
496 * eval: (c-set-style "gnu")
497 * End:
498 */