blob: 60c197f2a572094b0f7e9c8b51153e281bec4e6a [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
58 /* max macs in evet message, default to 100 entries */
59 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
Dave Barach97d8dc22016-08-15 15:31:15 -0400105/*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106 * The l2fib entry results
107 */
Dave Barach97d8dc22016-08-15 15:31:15 -0400108typedef struct
109{
110 union
111 {
112 struct
113 {
John Lo8d00fff2017-08-03 00:35:36 -0400114 u32 sw_if_index; /* output sw_if_index (L3 intf if bvi==1) */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
John Lo8d00fff2017-08-03 00:35:36 -0400116 u8 static_mac:1; /* static mac, no MAC move */
117 u8 age_not:1; /* not subject to age */
Dave Barach97d8dc22016-08-15 15:31:15 -0400118 u8 bvi:1; /* mac is for a bridged virtual interface */
119 u8 filter:1; /* drop packets to/from this mac */
John Lo8d00fff2017-08-03 00:35:36 -0400120 u8 lrn_evt:1; /* MAC learned to be sent in L2 MAC event */
121 u8 unused:3;
122
Dave Barach97d8dc22016-08-15 15:31:15 -0400123 u8 timestamp; /* timestamp for aging */
Eyal Bari7537e712017-04-27 14:07:55 +0300124 l2fib_seq_num_t sn; /* bd/int seq num */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125 } fields;
126 u64 raw;
127 };
128} l2fib_entry_result_t;
129
Damjan Mariond171d482016-12-05 14:16:38 +0100130STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131
Dave Barach97d8dc22016-08-15 15:31:15 -0400132/**
133 * Compute the hash for the given key and return
134 * the corresponding bucket index
135 */
136always_inline u32
137l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
138{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 u32 result;
140 u32 temp_a;
141 u32 temp_b;
142
Dave Barach97d8dc22016-08-15 15:31:15 -0400143 result = 0xa5a5a5a5; /* some seed */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 temp_a = key->words.w0;
145 temp_b = key->words.w1;
Dave Barach97d8dc22016-08-15 15:31:15 -0400146 hash_mix32 (temp_a, temp_b, result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
148 return result % L2FIB_NUM_BUCKETS;
149}
150
Gabriel Gannea30d9032017-10-24 10:24:57 +0200151/**
152 * make address sanitizer skip this:
153 * The 6-Bytes mac-address is cast into an 8-Bytes u64, with 2 additional Bytes.
154 * l2fib_make_key() does read those two Bytes but does not use them.
155 */
156always_inline u64 __attribute__ ((no_sanitize_address))
Dave Barach97d8dc22016-08-15 15:31:15 -0400157l2fib_make_key (u8 * mac_address, u16 bd_index)
158{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 u64 temp;
160
Dave Barach97d8dc22016-08-15 15:31:15 -0400161 /*
162 * The mac address in memory is A:B:C:D:E:F
163 * The bd id in register is H:L
164 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165#if CLIB_ARCH_IS_LITTLE_ENDIAN
Dave Barach97d8dc22016-08-15 15:31:15 -0400166 /*
167 * Create the in-register key as F:E:D:C:B:A:H:L
168 * In memory the key is L:H:A:B:C:D:E:F
169 */
Damjan Marion30230dd2016-11-24 22:20:05 +0100170 temp = *((u64 *) (mac_address)) << 16;
Dave Barach97d8dc22016-08-15 15:31:15 -0400171 temp = (temp & ~0xffff) | (u64) (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172#else
Dave Barach97d8dc22016-08-15 15:31:15 -0400173 /*
174 * Create the in-register key as H:L:A:B:C:D:E:F
175 * In memory the key is H:L:A:B:C:D:E:F
176 */
177 temp = *((u64 *) (mac_address)) >> 16;
178 temp = temp | (((u64) bd_index) << 48);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700179#endif
180
181 return temp;
182}
183
184
185
Dave Barach97d8dc22016-08-15 15:31:15 -0400186/**
187 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
188 * Cached_key and cached_result are used as a one-entry cache.
189 * The function reads and updates them as needed.
190 *
191 * mac0 and bd_index0 are the keys. The entry is written to result0.
192 * If the entry was not found, result0 is set to ~0.
193 *
194 * key0 and bucket0 return with the computed key and hash bucket,
195 * convenient if the entry needs to be updated afterward.
196 * If the cached_result was used, bucket0 is set to ~0.
197 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198
199static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400200l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
201 l2fib_entry_key_t * cached_key,
202 l2fib_entry_result_t * cached_result,
203 u8 * mac0,
204 u16 bd_index0,
205 l2fib_entry_key_t * key0,
206 u32 * bucket0, l2fib_entry_result_t * result0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207{
Dave Barach97d8dc22016-08-15 15:31:15 -0400208 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209 key0->raw = l2fib_make_key (mac0, bd_index0);
210 *bucket0 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 if (key0->raw == cached_key->raw)
213 {
214 /* Hit in the one-entry cache */
215 result0->raw = cached_result->raw;
216 }
217 else
218 {
219 /* Do a regular mac table lookup */
220 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221
Dave Barach97d8dc22016-08-15 15:31:15 -0400222 kv.key = key0->raw;
223 kv.value = ~0ULL;
224 BV (clib_bihash_search_inline) (mac_table, &kv);
225 result0->raw = kv.value;
226
227 /* Update one-entry cache */
228 cached_key->raw = key0->raw;
229 cached_result->raw = result0->raw;
230 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231}
232
233
Dave Barach97d8dc22016-08-15 15:31:15 -0400234/**
235 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
236 * The lookups for the two packets are interleaved.
237 *
238 * Cached_key and cached_result are used as a one-entry cache.
239 * The function reads and updates them as needed.
240 *
241 * mac0 and bd_index0 are the keys. The entry is written to result0.
242 * If the entry was not found, result0 is set to ~0. The same
243 * holds for mac1/bd_index1/result1.
244 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400246l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
247 l2fib_entry_key_t * cached_key,
248 l2fib_entry_result_t * cached_result,
249 u8 * mac0,
250 u8 * mac1,
251 u16 bd_index0,
252 u16 bd_index1,
253 l2fib_entry_key_t * key0,
254 l2fib_entry_key_t * key1,
255 u32 * bucket0,
256 u32 * bucket1,
257 l2fib_entry_result_t * result0,
258 l2fib_entry_result_t * result1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259{
Dave Barach97d8dc22016-08-15 15:31:15 -0400260 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 key0->raw = l2fib_make_key (mac0, bd_index0);
262 key1->raw = l2fib_make_key (mac1, bd_index1);
263
Dave Barach97d8dc22016-08-15 15:31:15 -0400264 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
265 {
266 /* Both hit in the one-entry cache */
267 result0->raw = cached_result->raw;
268 result1->raw = cached_result->raw;
269 *bucket0 = ~0;
270 *bucket1 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271
Dave Barach97d8dc22016-08-15 15:31:15 -0400272 }
273 else
274 {
275 BVT (clib_bihash_kv) kv0, kv1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
Dave Barach97d8dc22016-08-15 15:31:15 -0400277 /*
278 * Do a regular mac table lookup
279 * Interleave lookups for packet 0 and packet 1
280 */
281 kv0.key = key0->raw;
282 kv1.key = key1->raw;
283 kv0.value = ~0ULL;
284 kv1.value = ~0ULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
Dave Barach97d8dc22016-08-15 15:31:15 -0400286 BV (clib_bihash_search_inline) (mac_table, &kv0);
287 BV (clib_bihash_search_inline) (mac_table, &kv1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barach97d8dc22016-08-15 15:31:15 -0400289 result0->raw = kv0.value;
290 result1->raw = kv1.value;
291
292 /* Update one-entry cache */
293 cached_key->raw = key1->raw;
294 cached_result->raw = result1->raw;
295 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296}
297
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800298static_always_inline void
299l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
300 l2fib_entry_key_t * cached_key,
301 l2fib_entry_result_t * cached_result,
302 u8 * mac0,
303 u8 * mac1,
304 u8 * mac2,
305 u8 * mac3,
306 u16 bd_index0,
307 u16 bd_index1,
308 u16 bd_index2,
309 u16 bd_index3,
310 l2fib_entry_key_t * key0,
311 l2fib_entry_key_t * key1,
312 l2fib_entry_key_t * key2,
313 l2fib_entry_key_t * key3,
314 u32 * bucket0,
315 u32 * bucket1,
316 u32 * bucket2,
317 u32 * bucket3,
318 l2fib_entry_result_t * result0,
319 l2fib_entry_result_t * result1,
320 l2fib_entry_result_t * result2,
321 l2fib_entry_result_t * result3)
322{
323 /* set up key */
324 key0->raw = l2fib_make_key (mac0, bd_index0);
325 key1->raw = l2fib_make_key (mac1, bd_index1);
326 key2->raw = l2fib_make_key (mac2, bd_index2);
327 key3->raw = l2fib_make_key (mac3, bd_index3);
328
329 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
330 (key2->raw == cached_key->raw) && (key3->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 result2->raw = cached_result->raw;
336 result3->raw = cached_result->raw;
337 *bucket0 = ~0;
338 *bucket1 = ~0;
339 *bucket2 = ~0;
340 *bucket3 = ~0;
341
342 }
343 else
344 {
345 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
346
347 /*
348 * Do a regular mac table lookup
349 * Interleave lookups for packet 0 and packet 1
350 */
351 kv0.key = key0->raw;
352 kv1.key = key1->raw;
353 kv2.key = key2->raw;
354 kv3.key = key3->raw;
355 kv0.value = ~0ULL;
356 kv1.value = ~0ULL;
357 kv2.value = ~0ULL;
358 kv3.value = ~0ULL;
359
360 BV (clib_bihash_search_inline) (mac_table, &kv0);
361 BV (clib_bihash_search_inline) (mac_table, &kv1);
362 BV (clib_bihash_search_inline) (mac_table, &kv2);
363 BV (clib_bihash_search_inline) (mac_table, &kv3);
364
365 result0->raw = kv0.value;
366 result1->raw = kv1.value;
367 result2->raw = kv2.value;
368 result3->raw = kv3.value;
369
370 /* Update one-entry cache */
371 cached_key->raw = key1->raw;
372 cached_result->raw = result1->raw;
373 }
374}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Eyal Bari7537e712017-04-27 14:07:55 +0300376void l2fib_clear_table (void);
John Loda1f2c72017-03-24 20:11:15 -0400377
378void
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200379l2fib_add_entry (u8 * mac,
John Loda1f2c72017-03-24 20:11:15 -0400380 u32 bd_index,
John Lo8d00fff2017-08-03 00:35:36 -0400381 u32 sw_if_index, u8 static_mac, u8 drop_mac, u8 bvi_mac);
John Loda1f2c72017-03-24 20:11:15 -0400382
Eyal Bari31a71ab2017-06-25 14:42:33 +0300383static inline void
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200384l2fib_add_fwd_entry (u8 * mac, u32 bd_index, u32 sw_if_index, u8 static_mac,
John Lo8d00fff2017-08-03 00:35:36 -0400385 u8 bvi_mac)
Eyal Bari31a71ab2017-06-25 14:42:33 +0300386{
387 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, 0, bvi_mac);
388}
389
390static inline void
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200391l2fib_add_filter_entry (u8 * mac, u32 bd_index)
Eyal Bari31a71ab2017-06-25 14:42:33 +0300392{
393 l2fib_add_entry (mac, bd_index, ~0, 1, 1, 0);
394}
395
Mohsin Kazmi57938f62017-10-27 21:28:07 +0200396u32 l2fib_del_entry (u8 * mac, u32 bd_index);
John Loda1f2c72017-03-24 20:11:15 -0400397
398void l2fib_start_ager_scan (vlib_main_t * vm);
399
400void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
401
402void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
403
Eyal Bari7537e712017-04-27 14:07:55 +0300404void l2fib_flush_all_mac (vlib_main_t * vm);
405
John Loda1f2c72017-03-24 20:11:15 -0400406void
407l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
408 l2fib_entry_result_t ** l2fe_res);
409
410u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
411
Eyal Bari0f360dc2017-06-14 13:11:20 +0300412static_always_inline u8 *
413l2fib_swif_seq_num (u32 sw_if_index)
414{
415 l2fib_main_t *mp = &l2fib_main;
Eyal Bari0f360dc2017-06-14 13:11:20 +0300416 return vec_elt_at_index (mp->swif_seq_num, sw_if_index);
417}
418
Eyal Barib12ac562017-07-18 13:25:19 +0300419static_always_inline u8 *
420l2fib_valid_swif_seq_num (u32 sw_if_index)
421{
422 l2fib_main_t *mp = &l2fib_main;
423 vec_validate (mp->swif_seq_num, sw_if_index);
424 return l2fib_swif_seq_num (sw_if_index);
425}
426
Dave Barach97d8dc22016-08-15 15:31:15 -0400427BVT (clib_bihash) * get_mac_table (void);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
429#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400430
431/*
432 * fd.io coding-style-patch-verification: ON
433 *
434 * Local Variables:
435 * eval: (c-set-style "gnu")
436 * End:
437 */