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 (64 * 1024)
28 #define L2FIB_MEMORY_SIZE (256<<20)
34 BVT (clib_bihash) mac_table;
36 /* per swif vector of sequence number for interface based flush of MACs */
39 /* convenience variables */
40 vlib_main_t *vlib_main;
41 vnet_main_t *vnet_main;
44 extern l2fib_main_t l2fib_main;
47 * The L2fib key is the mac address and bridge domain ID
67 STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
84 * The l2fib entry results
92 u32 sw_if_index; /* output sw_if_index (L3 interface if bvi==1) */
94 u8 static_mac:1; /* static mac, no dataplane learning */
95 u8 bvi:1; /* mac is for a bridged virtual interface */
96 u8 filter:1; /* drop packets to/from this mac */
98 u8 timestamp; /* timestamp for aging */
99 l2fib_seq_num_t sn; /* bd/int seq num */
103 } l2fib_entry_result_t;
105 STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
108 * Compute the hash for the given key and return
109 * the corresponding bucket index
112 l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
118 result = 0xa5a5a5a5; /* some seed */
119 temp_a = key->words.w0;
120 temp_b = key->words.w1;
121 hash_mix32 (temp_a, temp_b, result);
123 return result % L2FIB_NUM_BUCKETS;
127 l2fib_make_key (u8 * mac_address, u16 bd_index)
132 * The mac address in memory is A:B:C:D:E:F
133 * The bd id in register is H:L
135 #if CLIB_ARCH_IS_LITTLE_ENDIAN
137 * Create the in-register key as F:E:D:C:B:A:H:L
138 * In memory the key is L:H:A:B:C:D:E:F
140 temp = *((u64 *) (mac_address)) << 16;
141 temp = (temp & ~0xffff) | (u64) (bd_index);
144 * Create the in-register key as H:L:A:B:C:D:E:F
145 * In memory the key is H:L:A:B:C:D:E:F
147 temp = *((u64 *) (mac_address)) >> 16;
148 temp = temp | (((u64) bd_index) << 48);
157 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
158 * Cached_key and cached_result are used as a one-entry cache.
159 * The function reads and updates them as needed.
161 * mac0 and bd_index0 are the keys. The entry is written to result0.
162 * If the entry was not found, result0 is set to ~0.
164 * key0 and bucket0 return with the computed key and hash bucket,
165 * convenient if the entry needs to be updated afterward.
166 * If the cached_result was used, bucket0 is set to ~0.
169 static_always_inline void
170 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
171 l2fib_entry_key_t * cached_key,
172 l2fib_entry_result_t * cached_result,
175 l2fib_entry_key_t * key0,
176 u32 * bucket0, l2fib_entry_result_t * result0)
179 key0->raw = l2fib_make_key (mac0, bd_index0);
182 if (key0->raw == cached_key->raw)
184 /* Hit in the one-entry cache */
185 result0->raw = cached_result->raw;
189 /* Do a regular mac table lookup */
190 BVT (clib_bihash_kv) kv;
194 BV (clib_bihash_search_inline) (mac_table, &kv);
195 result0->raw = kv.value;
197 /* Update one-entry cache */
198 cached_key->raw = key0->raw;
199 cached_result->raw = result0->raw;
205 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
206 * The lookups for the two packets are interleaved.
208 * Cached_key and cached_result are used as a one-entry cache.
209 * The function reads and updates them as needed.
211 * mac0 and bd_index0 are the keys. The entry is written to result0.
212 * If the entry was not found, result0 is set to ~0. The same
213 * holds for mac1/bd_index1/result1.
215 static_always_inline void
216 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
217 l2fib_entry_key_t * cached_key,
218 l2fib_entry_result_t * cached_result,
223 l2fib_entry_key_t * key0,
224 l2fib_entry_key_t * key1,
227 l2fib_entry_result_t * result0,
228 l2fib_entry_result_t * result1)
231 key0->raw = l2fib_make_key (mac0, bd_index0);
232 key1->raw = l2fib_make_key (mac1, bd_index1);
234 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
236 /* Both hit in the one-entry cache */
237 result0->raw = cached_result->raw;
238 result1->raw = cached_result->raw;
245 BVT (clib_bihash_kv) kv0, kv1;
248 * Do a regular mac table lookup
249 * Interleave lookups for packet 0 and packet 1
256 BV (clib_bihash_search_inline) (mac_table, &kv0);
257 BV (clib_bihash_search_inline) (mac_table, &kv1);
259 result0->raw = kv0.value;
260 result1->raw = kv1.value;
262 /* Update one-entry cache */
263 cached_key->raw = key1->raw;
264 cached_result->raw = result1->raw;
268 static_always_inline void
269 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
270 l2fib_entry_key_t * cached_key,
271 l2fib_entry_result_t * cached_result,
280 l2fib_entry_key_t * key0,
281 l2fib_entry_key_t * key1,
282 l2fib_entry_key_t * key2,
283 l2fib_entry_key_t * key3,
288 l2fib_entry_result_t * result0,
289 l2fib_entry_result_t * result1,
290 l2fib_entry_result_t * result2,
291 l2fib_entry_result_t * result3)
294 key0->raw = l2fib_make_key (mac0, bd_index0);
295 key1->raw = l2fib_make_key (mac1, bd_index1);
296 key2->raw = l2fib_make_key (mac2, bd_index2);
297 key3->raw = l2fib_make_key (mac3, bd_index3);
299 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
300 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
302 /* Both hit in the one-entry cache */
303 result0->raw = cached_result->raw;
304 result1->raw = cached_result->raw;
305 result2->raw = cached_result->raw;
306 result3->raw = cached_result->raw;
315 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
318 * Do a regular mac table lookup
319 * Interleave lookups for packet 0 and packet 1
330 BV (clib_bihash_search_inline) (mac_table, &kv0);
331 BV (clib_bihash_search_inline) (mac_table, &kv1);
332 BV (clib_bihash_search_inline) (mac_table, &kv2);
333 BV (clib_bihash_search_inline) (mac_table, &kv3);
335 result0->raw = kv0.value;
336 result1->raw = kv1.value;
337 result2->raw = kv2.value;
338 result3->raw = kv3.value;
340 /* Update one-entry cache */
341 cached_key->raw = key1->raw;
342 cached_result->raw = result1->raw;
346 void l2fib_clear_table (void);
349 l2fib_add_entry (u64 mac,
351 u32 sw_if_index, u32 static_mac, u32 drop_mac, u32 bvi_mac);
354 l2fib_add_fwd_entry (u64 mac, u32 bd_index, u32 sw_if_index, u32 static_mac,
357 l2fib_add_entry (mac, bd_index, sw_if_index, static_mac, 0, bvi_mac);
361 l2fib_add_filter_entry (u64 mac, u32 bd_index)
363 l2fib_add_entry (mac, bd_index, ~0, 1, 1, 0);
366 u32 l2fib_del_entry (u64 mac, u32 bd_index);
368 void l2fib_start_ager_scan (vlib_main_t * vm);
370 void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
372 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
374 void l2fib_flush_all_mac (vlib_main_t * vm);
377 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
378 l2fib_entry_result_t ** l2fe_res);
380 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
382 static_always_inline u8 *
383 l2fib_swif_seq_num (u32 sw_if_index)
385 l2fib_main_t *mp = &l2fib_main;
386 vec_validate (mp->swif_seq_num, sw_if_index);
387 return vec_elt_at_index (mp->swif_seq_num, sw_if_index);
390 BVT (clib_bihash) * get_mac_table (void);
395 * fd.io coding-style-patch-verification: ON
398 * eval: (c-set-style "gnu")