2 * l2_fib.h : layer 2 forwarding table (aka mac table)
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef included_l2fib_h
19 #define included_l2fib_h
21 #include <vlib/vlib.h>
22 #include <vppinfra/bihash_8_8.h>
25 * The size of the hash table
27 #define L2FIB_NUM_BUCKETS (256 * 1024)
28 #define L2FIB_MEMORY_SIZE (128<<20)
30 /* Ager scan interval is 1 minute for aging */
31 #define L2FIB_AGE_SCAN_INTERVAL (60.0)
33 /* MAC event scan delay is 100 msec unless specified by MAC event client */
34 #define L2FIB_EVENT_SCAN_DELAY_DEFAULT (0.1)
36 /* Max MACs in a event message is 100 unless specified by MAC event client */
37 #define L2FIB_EVENT_MAX_MACS_DEFAULT (100)
39 /* MAC event learn limit is 1000 unless specified by MAC event client */
40 #define L2FIB_EVENT_LEARN_LIMIT_DEFAULT (1000)
46 BVT (clib_bihash) mac_table;
48 /* number of buckets in the hash table */
49 uword mac_table_n_buckets;
51 /* hash table memory size */
52 uword mac_table_memory_size;
54 /* hash table initialized */
55 u8 mac_table_initialized;
57 /* per swif vector of sequence number for interface based flush of MACs */
60 /* last event or ager scan duration */
61 f64 evt_scan_duration;
62 f64 age_scan_duration;
64 /* delay between event scans, default to 100 msec */
67 /* max macs in event message, default to 100 entries */
68 u32 max_macs_in_event;
70 /* convenience variables */
71 vlib_main_t *vlib_main;
72 vnet_main_t *vnet_main;
75 extern l2fib_main_t l2fib_main;
78 * The L2fib key is the mac address and bridge domain ID
98 STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
115 * Flags associated with an L2 Fib Entry
116 * - static mac, no MAC move
117 * - not subject to age
118 * - mac is for a bridged virtual interface
119 * - drop packets to/from this mac
120 * - MAC learned to be sent in L2 MAC event
121 * -MAC learned is a MAC move
123 #define foreach_l2fib_entry_result_attr \
124 _(STATIC, 0, "static") \
125 _(AGE_NOT, 1, "age-not") \
127 _(FILTER, 3, "filter") \
128 _(LRN_EVT, 4, "learn-event") \
129 _(LRN_MOV, 5, "learn-move")
131 typedef enum l2fib_entry_result_flags_t_
133 L2FIB_ENTRY_RESULT_FLAG_NONE = 0,
134 #define _(a,v,s) L2FIB_ENTRY_RESULT_FLAG_##a = (1 << v),
135 foreach_l2fib_entry_result_attr
137 } __attribute__ ((packed)) l2fib_entry_result_flags_t;
139 STATIC_ASSERT_SIZEOF (l2fib_entry_result_flags_t, 1);
141 extern u8 *format_l2fib_entry_result_flags (u8 * s, va_list * args);
144 * The l2fib entry results
146 typedef struct l2fib_entry_result_t_
152 u32 sw_if_index; /* output sw_if_index (L3 intf if bvi==1) */
153 l2fib_entry_result_flags_t flags;
155 u8 timestamp; /* timestamp for aging */
156 l2fib_seq_num_t sn; /* bd/int seq num */
160 } l2fib_entry_result_t;
162 STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
166 l2fib_entry_result_is_set_##a (const l2fib_entry_result_t *r) { \
167 return (r->fields.flags & L2FIB_ENTRY_RESULT_FLAG_##a); \
169 foreach_l2fib_entry_result_attr
173 l2fib_entry_result_set_##a (l2fib_entry_result_t *r) { \
174 r->fields.flags |= L2FIB_ENTRY_RESULT_FLAG_##a; \
176 foreach_l2fib_entry_result_attr
180 l2fib_entry_result_clear_##a (l2fib_entry_result_t *r) { \
181 r->fields.flags &= ~L2FIB_ENTRY_RESULT_FLAG_##a; \
183 foreach_l2fib_entry_result_attr
186 l2fib_entry_result_set_bits (l2fib_entry_result_t * r,
187 l2fib_entry_result_flags_t bits)
189 r->fields.flags |= bits;
193 l2fib_entry_result_clear_bits (l2fib_entry_result_t * r,
194 l2fib_entry_result_flags_t bits)
196 r->fields.flags &= ~bits;
199 /* L2 MAC event entry action enums (see mac_entry definition in l2.api) */
202 MAC_EVENT_ACTION_ADD = 0,
203 MAC_EVENT_ACTION_DELETE = 1,
204 MAC_EVENT_ACTION_MOVE = 2,
205 } l2_mac_event_action_t;
208 * Compute the hash for the given key and return
209 * the corresponding bucket index
212 l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
218 result = 0xa5a5a5a5; /* some seed */
219 temp_a = key->words.w0;
220 temp_b = key->words.w1;
221 hash_mix32 (temp_a, temp_b, result);
223 return result % L2FIB_NUM_BUCKETS;
227 l2fib_make_key (const u8 * mac_address, u16 bd_index)
232 * The mac address in memory is A:B:C:D:E:F
233 * The bd id in register is H:L
235 #if CLIB_ARCH_IS_LITTLE_ENDIAN
237 * Create the in-register key as F:E:D:C:B:A:H:L
238 * In memory the key is L:H:A:B:C:D:E:F
240 temp = CLIB_MEM_OVERFLOW_LOAD (*, (u64 *) mac_address) << 16;
241 temp = (temp & ~0xffff) | (u64) (bd_index);
244 * Create the in-register key as H:L:A:B:C:D:E:F
245 * In memory the key is H:L:A:B:C:D:E:F
247 temp = CLIB_MEM_OVERFLOW_LOAD (*, (u64 *) mac_address) >> 16;
248 temp = temp | (((u64) bd_index) << 48);
257 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
258 * Cached_key and cached_result are used as a one-entry cache.
259 * The function reads and updates them as needed.
261 * mac0 and bd_index0 are the keys. The entry is written to result0.
262 * If the entry was not found, result0 is set to ~0.
264 * key0 return with the computed key, convenient if the entry needs,
265 * to be updated afterward.
268 static_always_inline void
269 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
270 l2fib_entry_key_t * cached_key,
271 l2fib_entry_result_t * cached_result,
274 l2fib_entry_key_t * key0, l2fib_entry_result_t * result0)
277 key0->raw = l2fib_make_key (mac0, bd_index0);
279 if (key0->raw == cached_key->raw)
281 /* Hit in the one-entry cache */
282 result0->raw = cached_result->raw;
286 /* Do a regular mac table lookup */
287 BVT (clib_bihash_kv) kv;
291 BV (clib_bihash_search_inline) (mac_table, &kv);
292 result0->raw = kv.value;
294 /* Update one-entry cache */
295 cached_key->raw = key0->raw;
296 cached_result->raw = result0->raw;
302 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
303 * The lookups for the two packets are interleaved.
305 * Cached_key and cached_result are used as a one-entry cache.
306 * The function reads and updates them as needed.
308 * mac0 and bd_index0 are the keys. The entry is written to result0.
309 * If the entry was not found, result0 is set to ~0. The same
310 * holds for mac1/bd_index1/result1.
312 static_always_inline void
313 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
314 l2fib_entry_key_t * cached_key,
315 l2fib_entry_result_t * cached_result,
320 l2fib_entry_key_t * key0,
321 l2fib_entry_key_t * key1,
322 l2fib_entry_result_t * result0,
323 l2fib_entry_result_t * result1)
326 key0->raw = l2fib_make_key (mac0, bd_index0);
327 key1->raw = l2fib_make_key (mac1, bd_index1);
329 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
331 /* Both hit in the one-entry cache */
332 result0->raw = cached_result->raw;
333 result1->raw = cached_result->raw;
337 BVT (clib_bihash_kv) kv0, kv1;
340 * Do a regular mac table lookup
341 * Interleave lookups for packet 0 and packet 1
348 BV (clib_bihash_search_inline) (mac_table, &kv0);
349 BV (clib_bihash_search_inline) (mac_table, &kv1);
351 result0->raw = kv0.value;
352 result1->raw = kv1.value;
354 /* Update one-entry cache */
355 cached_key->raw = key1->raw;
356 cached_result->raw = result1->raw;
360 static_always_inline void
361 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
362 l2fib_entry_key_t * cached_key,
363 l2fib_entry_result_t * cached_result,
372 l2fib_entry_key_t * key0,
373 l2fib_entry_key_t * key1,
374 l2fib_entry_key_t * key2,
375 l2fib_entry_key_t * key3,
376 l2fib_entry_result_t * result0,
377 l2fib_entry_result_t * result1,
378 l2fib_entry_result_t * result2,
379 l2fib_entry_result_t * result3)
382 key0->raw = l2fib_make_key (mac0, bd_index0);
383 key1->raw = l2fib_make_key (mac1, bd_index1);
384 key2->raw = l2fib_make_key (mac2, bd_index2);
385 key3->raw = l2fib_make_key (mac3, bd_index3);
387 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
388 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
390 /* Both hit in the one-entry cache */
391 result0->raw = cached_result->raw;
392 result1->raw = cached_result->raw;
393 result2->raw = cached_result->raw;
394 result3->raw = cached_result->raw;
398 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
401 * Do a regular mac table lookup
402 * Interleave lookups for packet 0 and packet 1
413 BV (clib_bihash_search_inline) (mac_table, &kv0);
414 BV (clib_bihash_search_inline) (mac_table, &kv1);
415 BV (clib_bihash_search_inline) (mac_table, &kv2);
416 BV (clib_bihash_search_inline) (mac_table, &kv3);
418 result0->raw = kv0.value;
419 result1->raw = kv1.value;
420 result2->raw = kv2.value;
421 result3->raw = kv3.value;
423 /* Update one-entry cache */
424 cached_key->raw = key1->raw;
425 cached_result->raw = result1->raw;
429 void l2fib_clear_table (void);
431 void l2fib_table_init (void);
434 l2fib_add_entry (const u8 * mac,
436 u32 sw_if_index, l2fib_entry_result_flags_t flags);
439 l2fib_add_filter_entry (const u8 * mac, u32 bd_index)
441 l2fib_add_entry (mac, bd_index, ~0,
442 (L2FIB_ENTRY_RESULT_FLAG_FILTER |
443 L2FIB_ENTRY_RESULT_FLAG_STATIC));
446 u32 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index);
448 void l2fib_start_ager_scan (vlib_main_t * vm);
450 void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
452 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
454 void l2fib_flush_all_mac (vlib_main_t * vm);
457 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
458 l2fib_entry_result_t ** l2fe_res);
460 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
462 static_always_inline u8 *
463 l2fib_swif_seq_num (u32 sw_if_index)
465 l2fib_main_t *mp = &l2fib_main;
466 return vec_elt_at_index (mp->swif_seq_num, sw_if_index);
469 static_always_inline u8 *
470 l2fib_valid_swif_seq_num (u32 sw_if_index)
472 l2fib_main_t *mp = &l2fib_main;
473 vec_validate (mp->swif_seq_num, sw_if_index);
474 return l2fib_swif_seq_num (sw_if_index);
477 BVT (clib_bihash) * get_mac_table (void);
482 * fd.io coding-style-patch-verification: ON
485 * eval: (c-set-style "gnu")