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
52 * The l2fib entry results
60 u32 sw_if_index; /* output sw_if_index (L3 interface if bvi==1) */
62 u8 static_mac:1; /* static mac, no dataplane learning */
63 u8 bvi:1; /* mac is for a bridged virtual interface */
64 u8 filter:1; /* drop packets to/from this mac */
65 u8 refresh:1; /* refresh flag for aging */
67 u8 timestamp; /* timestamp for aging */
72 } l2fib_entry_result_t;
76 * Compute the hash for the given key and return
77 * the corresponding bucket index
80 l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
86 result = 0xa5a5a5a5; /* some seed */
87 temp_a = key->words.w0;
88 temp_b = key->words.w1;
89 hash_mix32 (temp_a, temp_b, result);
91 return result % L2FIB_NUM_BUCKETS;
95 l2fib_make_key (u8 * mac_address, u16 bd_index)
100 * The mac address in memory is A:B:C:D:E:F
101 * The bd id in register is H:L
103 #if CLIB_ARCH_IS_LITTLE_ENDIAN
105 * Create the in-register key as F:E:D:C:B:A:H:L
106 * In memory the key is L:H:A:B:C:D:E:F
108 temp = *((u64 *) (mac_address - 2));
109 temp = (temp & ~0xffff) | (u64) (bd_index);
112 * Create the in-register key as H:L:A:B:C:D:E:F
113 * In memory the key is H:L:A:B:C:D:E:F
115 temp = *((u64 *) (mac_address)) >> 16;
116 temp = temp | (((u64) bd_index) << 48);
125 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
126 * Cached_key and cached_result are used as a one-entry cache.
127 * The function reads and updates them as needed.
129 * mac0 and bd_index0 are the keys. The entry is written to result0.
130 * If the entry was not found, result0 is set to ~0.
132 * key0 and bucket0 return with the computed key and hash bucket,
133 * convenient if the entry needs to be updated afterward.
134 * If the cached_result was used, bucket0 is set to ~0.
137 static_always_inline void
138 l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
139 l2fib_entry_key_t * cached_key,
140 l2fib_entry_result_t * cached_result,
143 l2fib_entry_key_t * key0,
144 u32 * bucket0, l2fib_entry_result_t * result0)
147 key0->raw = l2fib_make_key (mac0, bd_index0);
150 if (key0->raw == cached_key->raw)
152 /* Hit in the one-entry cache */
153 result0->raw = cached_result->raw;
157 /* Do a regular mac table lookup */
158 BVT (clib_bihash_kv) kv;
162 BV (clib_bihash_search_inline) (mac_table, &kv);
163 result0->raw = kv.value;
165 /* Update one-entry cache */
166 cached_key->raw = key0->raw;
167 cached_result->raw = result0->raw;
173 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
174 * The lookups for the two packets are interleaved.
176 * Cached_key and cached_result are used as a one-entry cache.
177 * The function reads and updates them as needed.
179 * mac0 and bd_index0 are the keys. The entry is written to result0.
180 * If the entry was not found, result0 is set to ~0. The same
181 * holds for mac1/bd_index1/result1.
183 static_always_inline void
184 l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
185 l2fib_entry_key_t * cached_key,
186 l2fib_entry_result_t * cached_result,
191 l2fib_entry_key_t * key0,
192 l2fib_entry_key_t * key1,
195 l2fib_entry_result_t * result0,
196 l2fib_entry_result_t * result1)
199 key0->raw = l2fib_make_key (mac0, bd_index0);
200 key1->raw = l2fib_make_key (mac1, bd_index1);
202 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
204 /* Both hit in the one-entry cache */
205 result0->raw = cached_result->raw;
206 result1->raw = cached_result->raw;
213 BVT (clib_bihash_kv) kv0, kv1;
216 * Do a regular mac table lookup
217 * Interleave lookups for packet 0 and packet 1
224 BV (clib_bihash_search_inline) (mac_table, &kv0);
225 BV (clib_bihash_search_inline) (mac_table, &kv1);
227 result0->raw = kv0.value;
228 result1->raw = kv1.value;
230 /* Update one-entry cache */
231 cached_key->raw = key1->raw;
232 cached_result->raw = result1->raw;
236 static_always_inline void
237 l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
238 l2fib_entry_key_t * cached_key,
239 l2fib_entry_result_t * cached_result,
248 l2fib_entry_key_t * key0,
249 l2fib_entry_key_t * key1,
250 l2fib_entry_key_t * key2,
251 l2fib_entry_key_t * key3,
256 l2fib_entry_result_t * result0,
257 l2fib_entry_result_t * result1,
258 l2fib_entry_result_t * result2,
259 l2fib_entry_result_t * result3)
262 key0->raw = l2fib_make_key (mac0, bd_index0);
263 key1->raw = l2fib_make_key (mac1, bd_index1);
264 key2->raw = l2fib_make_key (mac2, bd_index2);
265 key3->raw = l2fib_make_key (mac3, bd_index3);
267 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
268 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
270 /* Both hit in the one-entry cache */
271 result0->raw = cached_result->raw;
272 result1->raw = cached_result->raw;
273 result2->raw = cached_result->raw;
274 result3->raw = cached_result->raw;
283 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
286 * Do a regular mac table lookup
287 * Interleave lookups for packet 0 and packet 1
298 BV (clib_bihash_search_inline) (mac_table, &kv0);
299 BV (clib_bihash_search_inline) (mac_table, &kv1);
300 BV (clib_bihash_search_inline) (mac_table, &kv2);
301 BV (clib_bihash_search_inline) (mac_table, &kv3);
303 result0->raw = kv0.value;
304 result1->raw = kv1.value;
305 result2->raw = kv2.value;
306 result3->raw = kv3.value;
308 /* Update one-entry cache */
309 cached_key->raw = key1->raw;
310 cached_result->raw = result1->raw;
314 BVT (clib_bihash) * get_mac_table (void);
316 l2fib_clear_table (uint keep_static);
318 l2fib_add_entry (u64 mac,
321 u32 static_mac, u32 drop_mac, u32 bvi_mac);
323 l2fib_del_entry (u64 mac, u32 bd_index);
326 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
327 l2fib_entry_result_t ** l2fe_res);
329 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
334 * fd.io coding-style-patch-verification: ON
337 * eval: (c-set-style "gnu")