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)
31 * The L2fib key is the mac address and bridge domain ID
51 STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
68 * The l2fib entry results
76 u32 sw_if_index; /* output sw_if_index (L3 interface if bvi==1) */
78 u8 static_mac:1; /* static mac, no dataplane learning */
79 u8 bvi:1; /* mac is for a bridged virtual interface */
80 u8 filter:1; /* drop packets to/from this mac */
82 u8 timestamp; /* timestamp for aging */
83 l2fib_seq_num_t sn; /* bd/int seq num */
87 } l2fib_entry_result_t;
89 STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
92 * Compute the hash for the given key and return
93 * the corresponding bucket index
96 l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
102 result = 0xa5a5a5a5; /* some seed */
103 temp_a = key->words.w0;
104 temp_b = key->words.w1;
105 hash_mix32 (temp_a, temp_b, result);
107 return result % L2FIB_NUM_BUCKETS;
111 l2fib_make_key (u8 * mac_address, u16 bd_index)
116 * The mac address in memory is A:B:C:D:E:F
117 * The bd id in register is H:L
119 #if CLIB_ARCH_IS_LITTLE_ENDIAN
121 * Create the in-register key as F:E:D:C:B:A:H:L
122 * In memory the key is L:H:A:B:C:D:E:F
124 temp = *((u64 *) (mac_address)) << 16;
125 temp = (temp & ~0xffff) | (u64) (bd_index);
128 * Create the in-register key as H:L:A:B:C:D:E:F
129 * In memory the key is H:L:A:B:C:D:E:F
131 temp = *((u64 *) (mac_address)) >> 16;
132 temp = temp | (((u64) bd_index) << 48);
141 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
142 * Cached_key and cached_result are used as a one-entry cache.
143 * The function reads and updates them as needed.
145 * mac0 and bd_index0 are the keys. The entry is written to result0.
146 * If the entry was not found, result0 is set to ~0.
148 * key0 and bucket0 return with the computed key and hash bucket,
149 * convenient if the entry needs to be updated afterward.
150 * If the cached_result was used, bucket0 is set to ~0.
153 static_always_inline void
154 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
155 l2fib_entry_key_t * cached_key,
156 l2fib_entry_result_t * cached_result,
159 l2fib_entry_key_t * key0,
160 u32 * bucket0, l2fib_entry_result_t * result0)
163 key0->raw = l2fib_make_key (mac0, bd_index0);
166 if (key0->raw == cached_key->raw)
168 /* Hit in the one-entry cache */
169 result0->raw = cached_result->raw;
173 /* Do a regular mac table lookup */
174 BVT (clib_bihash_kv) kv;
178 BV (clib_bihash_search_inline) (mac_table, &kv);
179 result0->raw = kv.value;
181 /* Update one-entry cache */
182 cached_key->raw = key0->raw;
183 cached_result->raw = result0->raw;
189 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
190 * The lookups for the two packets are interleaved.
192 * Cached_key and cached_result are used as a one-entry cache.
193 * The function reads and updates them as needed.
195 * mac0 and bd_index0 are the keys. The entry is written to result0.
196 * If the entry was not found, result0 is set to ~0. The same
197 * holds for mac1/bd_index1/result1.
199 static_always_inline void
200 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
201 l2fib_entry_key_t * cached_key,
202 l2fib_entry_result_t * cached_result,
207 l2fib_entry_key_t * key0,
208 l2fib_entry_key_t * key1,
211 l2fib_entry_result_t * result0,
212 l2fib_entry_result_t * result1)
215 key0->raw = l2fib_make_key (mac0, bd_index0);
216 key1->raw = l2fib_make_key (mac1, bd_index1);
218 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
220 /* Both hit in the one-entry cache */
221 result0->raw = cached_result->raw;
222 result1->raw = cached_result->raw;
229 BVT (clib_bihash_kv) kv0, kv1;
232 * Do a regular mac table lookup
233 * Interleave lookups for packet 0 and packet 1
240 BV (clib_bihash_search_inline) (mac_table, &kv0);
241 BV (clib_bihash_search_inline) (mac_table, &kv1);
243 result0->raw = kv0.value;
244 result1->raw = kv1.value;
246 /* Update one-entry cache */
247 cached_key->raw = key1->raw;
248 cached_result->raw = result1->raw;
252 static_always_inline void
253 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
254 l2fib_entry_key_t * cached_key,
255 l2fib_entry_result_t * cached_result,
264 l2fib_entry_key_t * key0,
265 l2fib_entry_key_t * key1,
266 l2fib_entry_key_t * key2,
267 l2fib_entry_key_t * key3,
272 l2fib_entry_result_t * result0,
273 l2fib_entry_result_t * result1,
274 l2fib_entry_result_t * result2,
275 l2fib_entry_result_t * result3)
278 key0->raw = l2fib_make_key (mac0, bd_index0);
279 key1->raw = l2fib_make_key (mac1, bd_index1);
280 key2->raw = l2fib_make_key (mac2, bd_index2);
281 key3->raw = l2fib_make_key (mac3, bd_index3);
283 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
284 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
286 /* Both hit in the one-entry cache */
287 result0->raw = cached_result->raw;
288 result1->raw = cached_result->raw;
289 result2->raw = cached_result->raw;
290 result3->raw = cached_result->raw;
299 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
302 * Do a regular mac table lookup
303 * Interleave lookups for packet 0 and packet 1
314 BV (clib_bihash_search_inline) (mac_table, &kv0);
315 BV (clib_bihash_search_inline) (mac_table, &kv1);
316 BV (clib_bihash_search_inline) (mac_table, &kv2);
317 BV (clib_bihash_search_inline) (mac_table, &kv3);
319 result0->raw = kv0.value;
320 result1->raw = kv1.value;
321 result2->raw = kv2.value;
322 result3->raw = kv3.value;
324 /* Update one-entry cache */
325 cached_key->raw = key1->raw;
326 cached_result->raw = result1->raw;
330 void l2fib_clear_table (void);
333 l2fib_add_entry (u64 mac,
335 u32 sw_if_index, u32 static_mac, u32 drop_mac, u32 bvi_mac);
337 u32 l2fib_del_entry (u64 mac, u32 bd_index);
339 void l2fib_start_ager_scan (vlib_main_t * vm);
341 void l2fib_flush_int_mac (vlib_main_t * vm, u32 sw_if_index);
343 void l2fib_flush_bd_mac (vlib_main_t * vm, u32 bd_index);
345 void l2fib_flush_all_mac (vlib_main_t * vm);
348 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
349 l2fib_entry_result_t ** l2fe_res);
351 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
353 BVT (clib_bihash) * get_mac_table (void);
358 * fd.io coding-style-patch-verification: ON
361 * eval: (c-set-style "gnu")