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 /* last event or ager scan duration */
58 f64 evt_scan_duration;
59 f64 age_scan_duration;
61 /* delay between event scans, default to 100 msec */
64 /* max macs in event message, default to 100 entries */
65 u32 max_macs_in_event;
67 /* convenience variables */
68 vlib_main_t *vlib_main;
69 vnet_main_t *vnet_main;
72 extern l2fib_main_t l2fib_main;
75 * The L2fib key is the mac address and bridge domain ID
95 STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
98 * A combined representation of the sequence number associated
99 * with the interface and the BD.
100 * The BD is in higher bits, the interface in the lower bits, but
101 * the order is not important.
103 * It's convenient to represent this as an union of two u8s,
104 * but then in the DP one is forced to do short writes, followed
105 * by long reads, which is a sure thing for a stall
107 typedef u16 l2fib_seq_num_t;
109 static_always_inline l2fib_seq_num_t
110 l2_fib_mk_seq_num (u8 bd_sn, u8 if_sn)
112 return (((u16) bd_sn) << 8) | if_sn;
115 static_always_inline l2fib_seq_num_t
116 l2_fib_update_seq_num (l2fib_seq_num_t sn, u8 if_sn)
124 extern void l2_fib_extract_seq_num (l2fib_seq_num_t sn, u8 * bd_sn,
126 extern u8 *format_l2_fib_seq_num (u8 * s, va_list * a);
129 * Flags associated with an L2 Fib Entry
130 * - static mac, no MAC move
131 * - not subject to age
132 * - mac is for a bridged virtual interface
133 * - drop packets to/from this mac
134 * - MAC learned to be sent in L2 MAC event
135 * -MAC learned is a MAC move
137 #define foreach_l2fib_entry_result_attr \
138 _(STATIC, 0, "static") \
139 _(AGE_NOT, 1, "age-not") \
141 _(FILTER, 3, "filter") \
142 _(LRN_EVT, 4, "learn-event") \
143 _(LRN_MOV, 5, "learn-move")
145 typedef enum l2fib_entry_result_flags_t_
147 L2FIB_ENTRY_RESULT_FLAG_NONE = 0,
148 #define _(a,v,s) L2FIB_ENTRY_RESULT_FLAG_##a = (1 << v),
149 foreach_l2fib_entry_result_attr
151 } __attribute__ ((packed)) l2fib_entry_result_flags_t;
153 STATIC_ASSERT_SIZEOF (l2fib_entry_result_flags_t, 1);
155 extern u8 *format_l2fib_entry_result_flags (u8 * s, va_list * args);
158 * The l2fib entry results
160 typedef struct l2fib_entry_result_t_
166 u32 sw_if_index; /* output sw_if_index (L3 intf if bvi==1) */
167 l2fib_entry_result_flags_t flags;
169 u8 timestamp; /* timestamp for aging */
170 l2fib_seq_num_t sn; /* bd/int seq num */
174 } l2fib_entry_result_t;
176 STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
180 l2fib_entry_result_is_set_##a (const l2fib_entry_result_t *r) { \
181 return (r->fields.flags & L2FIB_ENTRY_RESULT_FLAG_##a); \
183 foreach_l2fib_entry_result_attr
187 l2fib_entry_result_set_##a (l2fib_entry_result_t *r) { \
188 r->fields.flags |= L2FIB_ENTRY_RESULT_FLAG_##a; \
190 foreach_l2fib_entry_result_attr
194 l2fib_entry_result_clear_##a (l2fib_entry_result_t *r) { \
195 r->fields.flags &= ~L2FIB_ENTRY_RESULT_FLAG_##a; \
197 foreach_l2fib_entry_result_attr
200 l2fib_entry_result_set_bits (l2fib_entry_result_t * r,
201 l2fib_entry_result_flags_t bits)
203 r->fields.flags |= bits;
207 l2fib_entry_result_clear_bits (l2fib_entry_result_t * r,
208 l2fib_entry_result_flags_t bits)
210 r->fields.flags &= ~bits;
213 /* L2 MAC event entry action enums (see mac_entry definition in l2.api) */
216 MAC_EVENT_ACTION_ADD = 0,
217 MAC_EVENT_ACTION_DELETE = 1,
218 MAC_EVENT_ACTION_MOVE = 2,
219 } l2_mac_event_action_t;
222 * Compute the hash for the given key and return
223 * the corresponding bucket index
226 l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
232 result = 0xa5a5a5a5; /* some seed */
233 temp_a = key->words.w0;
234 temp_b = key->words.w1;
235 hash_mix32 (temp_a, temp_b, result);
237 return result % L2FIB_NUM_BUCKETS;
241 l2fib_make_key (const u8 * mac_address, u16 bd_index)
243 l2fib_entry_key_t key = { .fields.bd_index = bd_index };
244 clib_memcpy_fast (&key.fields.mac, mac_address, sizeof (key.fields.mac));
251 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
252 * Cached_key and cached_result are used as a one-entry cache.
253 * The function reads and updates them as needed.
255 * mac0 and bd_index0 are the keys. The entry is written to result0.
256 * If the entry was not found, result0 is set to ~0.
258 * key0 return with the computed key, convenient if the entry needs,
259 * to be updated afterward.
262 static_always_inline void
263 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
264 l2fib_entry_key_t * cached_key,
265 l2fib_entry_result_t * cached_result,
268 l2fib_entry_key_t * key0, l2fib_entry_result_t * result0)
271 key0->raw = l2fib_make_key (mac0, bd_index0);
273 if (key0->raw == cached_key->raw)
275 /* Hit in the one-entry cache */
276 result0->raw = cached_result->raw;
280 /* Do a regular mac table lookup */
281 BVT (clib_bihash_kv) kv;
285 BV (clib_bihash_search_inline) (mac_table, &kv);
286 result0->raw = kv.value;
288 /* Update one-entry cache */
289 cached_key->raw = key0->raw;
290 cached_result->raw = result0->raw;
296 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
297 * The lookups for the two packets are interleaved.
299 * Cached_key and cached_result are used as a one-entry cache.
300 * The function reads and updates them as needed.
302 * mac0 and bd_index0 are the keys. The entry is written to result0.
303 * If the entry was not found, result0 is set to ~0. The same
304 * holds for mac1/bd_index1/result1.
306 static_always_inline void
307 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
308 l2fib_entry_key_t * cached_key,
309 l2fib_entry_result_t * cached_result,
314 l2fib_entry_key_t * key0,
315 l2fib_entry_key_t * key1,
316 l2fib_entry_result_t * result0,
317 l2fib_entry_result_t * result1)
320 key0->raw = l2fib_make_key (mac0, bd_index0);
321 key1->raw = l2fib_make_key (mac1, bd_index1);
323 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
325 /* Both hit in the one-entry cache */
326 result0->raw = cached_result->raw;
327 result1->raw = cached_result->raw;
331 BVT (clib_bihash_kv) kv0, kv1;
334 * Do a regular mac table lookup
335 * Interleave lookups for packet 0 and packet 1
342 BV (clib_bihash_search_inline) (mac_table, &kv0);
343 BV (clib_bihash_search_inline) (mac_table, &kv1);
345 result0->raw = kv0.value;
346 result1->raw = kv1.value;
348 /* Update one-entry cache */
349 cached_key->raw = key1->raw;
350 cached_result->raw = result1->raw;
354 static_always_inline void
355 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
356 l2fib_entry_key_t * cached_key,
357 l2fib_entry_result_t * cached_result,
366 l2fib_entry_key_t * key0,
367 l2fib_entry_key_t * key1,
368 l2fib_entry_key_t * key2,
369 l2fib_entry_key_t * key3,
370 l2fib_entry_result_t * result0,
371 l2fib_entry_result_t * result1,
372 l2fib_entry_result_t * result2,
373 l2fib_entry_result_t * result3)
376 key0->raw = l2fib_make_key (mac0, bd_index0);
377 key1->raw = l2fib_make_key (mac1, bd_index1);
378 key2->raw = l2fib_make_key (mac2, bd_index2);
379 key3->raw = l2fib_make_key (mac3, bd_index3);
381 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
382 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
384 /* Both hit in the one-entry cache */
385 result0->raw = cached_result->raw;
386 result1->raw = cached_result->raw;
387 result2->raw = cached_result->raw;
388 result3->raw = cached_result->raw;
392 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
395 * Do a regular mac table lookup
396 * Interleave lookups for packet 0 and packet 1
407 BV (clib_bihash_search_inline) (mac_table, &kv0);
408 BV (clib_bihash_search_inline) (mac_table, &kv1);
409 BV (clib_bihash_search_inline) (mac_table, &kv2);
410 BV (clib_bihash_search_inline) (mac_table, &kv3);
412 result0->raw = kv0.value;
413 result1->raw = kv1.value;
414 result2->raw = kv2.value;
415 result3->raw = kv3.value;
417 /* Update one-entry cache */
418 cached_key->raw = key1->raw;
419 cached_result->raw = result1->raw;
423 void l2fib_clear_table (void);
425 void l2fib_table_init (void);
428 l2fib_add_entry (const u8 * mac,
430 u32 sw_if_index, l2fib_entry_result_flags_t flags);
433 l2fib_add_filter_entry (const u8 * mac, u32 bd_index)
435 l2fib_add_entry (mac, bd_index, ~0,
436 (L2FIB_ENTRY_RESULT_FLAG_FILTER |
437 L2FIB_ENTRY_RESULT_FLAG_STATIC));
440 u32 l2fib_del_entry (const u8 * mac, u32 bd_index, u32 sw_if_index);
442 void l2fib_start_ager_scan (vlib_main_t * vm);
444 void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
446 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
448 void l2fib_flush_all_mac (vlib_main_t * vm);
451 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
452 l2fib_entry_result_t ** l2fe_res);
454 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
456 BVT (clib_bihash) * get_mac_table (void);
461 * fd.io coding-style-patch-verification: ON
464 * eval: (c-set-style "gnu")