blob: 4a2da59bc017279237493811cf5876f9101f86a6 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * l2_fib.h : layer 2 forwarding table (aka mac table)
3 *
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:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18#ifndef included_l2fib_h
19#define included_l2fib_h
20
21#include <vlib/vlib.h>
22#include <vppinfra/bihash_8_8.h>
23
24/*
25 * The size of the hash table
26 */
27#define L2FIB_NUM_BUCKETS (64 * 1024)
28#define L2FIB_MEMORY_SIZE (256<<20)
29
30/*
31 * The L2fib key is the mac address and bridge domain ID
32 */
Dave Barach97d8dc22016-08-15 15:31:15 -040033typedef struct
34{
35 union
36 {
37 struct
38 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070039 u16 bd_index;
Dave Barach97d8dc22016-08-15 15:31:15 -040040 u8 mac[6];
Ed Warnickecb9cada2015-12-08 15:45:58 -070041 } fields;
Dave Barach97d8dc22016-08-15 15:31:15 -040042 struct
43 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 u32 w0;
45 u32 w1;
46 } words;
47 u64 raw;
48 };
49} l2fib_entry_key_t;
50
Damjan Mariond171d482016-12-05 14:16:38 +010051STATIC_ASSERT_SIZEOF (l2fib_entry_key_t, 8);
52
Dave Barach97d8dc22016-08-15 15:31:15 -040053/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 * The l2fib entry results
55 */
Dave Barach97d8dc22016-08-15 15:31:15 -040056typedef struct
57{
58 union
59 {
60 struct
61 {
62 u32 sw_if_index; /* output sw_if_index (L3 interface if bvi==1) */
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barach97d8dc22016-08-15 15:31:15 -040064 u8 static_mac:1; /* static mac, no dataplane learning */
65 u8 bvi:1; /* mac is for a bridged virtual interface */
66 u8 filter:1; /* drop packets to/from this mac */
Damjan Mariond171d482016-12-05 14:16:38 +010067 u8 unused1:5;
Dave Barach97d8dc22016-08-15 15:31:15 -040068 u8 timestamp; /* timestamp for aging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 u16 unused2;
70 } fields;
71 u64 raw;
72 };
73} l2fib_entry_result_t;
74
Damjan Mariond171d482016-12-05 14:16:38 +010075STATIC_ASSERT_SIZEOF (l2fib_entry_result_t, 8);
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Dave Barach97d8dc22016-08-15 15:31:15 -040077/**
78 * Compute the hash for the given key and return
79 * the corresponding bucket index
80 */
81always_inline u32
82l2fib_compute_hash_bucket (l2fib_entry_key_t * key)
83{
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 u32 result;
85 u32 temp_a;
86 u32 temp_b;
87
Dave Barach97d8dc22016-08-15 15:31:15 -040088 result = 0xa5a5a5a5; /* some seed */
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 temp_a = key->words.w0;
90 temp_b = key->words.w1;
Dave Barach97d8dc22016-08-15 15:31:15 -040091 hash_mix32 (temp_a, temp_b, result);
Ed Warnickecb9cada2015-12-08 15:45:58 -070092
93 return result % L2FIB_NUM_BUCKETS;
94}
95
Dave Barach97d8dc22016-08-15 15:31:15 -040096always_inline u64
97l2fib_make_key (u8 * mac_address, u16 bd_index)
98{
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 u64 temp;
100
Dave Barach97d8dc22016-08-15 15:31:15 -0400101 /*
102 * The mac address in memory is A:B:C:D:E:F
103 * The bd id in register is H:L
104 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105#if CLIB_ARCH_IS_LITTLE_ENDIAN
Dave Barach97d8dc22016-08-15 15:31:15 -0400106 /*
107 * Create the in-register key as F:E:D:C:B:A:H:L
108 * In memory the key is L:H:A:B:C:D:E:F
109 */
Damjan Marion30230dd2016-11-24 22:20:05 +0100110 temp = *((u64 *) (mac_address)) << 16;
Dave Barach97d8dc22016-08-15 15:31:15 -0400111 temp = (temp & ~0xffff) | (u64) (bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112#else
Dave Barach97d8dc22016-08-15 15:31:15 -0400113 /*
114 * Create the in-register key as H:L:A:B:C:D:E:F
115 * In memory the key is H:L:A:B:C:D:E:F
116 */
117 temp = *((u64 *) (mac_address)) >> 16;
118 temp = temp | (((u64) bd_index) << 48);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119#endif
120
121 return temp;
122}
123
124
125
Dave Barach97d8dc22016-08-15 15:31:15 -0400126/**
127 * Lookup the entry for mac and bd_index in the mac table for 1 packet.
128 * Cached_key and cached_result are used as a one-entry cache.
129 * The function reads and updates them as needed.
130 *
131 * mac0 and bd_index0 are the keys. The entry is written to result0.
132 * If the entry was not found, result0 is set to ~0.
133 *
134 * key0 and bucket0 return with the computed key and hash bucket,
135 * convenient if the entry needs to be updated afterward.
136 * If the cached_result was used, bucket0 is set to ~0.
137 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400140l2fib_lookup_1 (BVT (clib_bihash) * mac_table,
141 l2fib_entry_key_t * cached_key,
142 l2fib_entry_result_t * cached_result,
143 u8 * mac0,
144 u16 bd_index0,
145 l2fib_entry_key_t * key0,
146 u32 * bucket0, l2fib_entry_result_t * result0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147{
Dave Barach97d8dc22016-08-15 15:31:15 -0400148 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 key0->raw = l2fib_make_key (mac0, bd_index0);
150 *bucket0 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
Dave Barach97d8dc22016-08-15 15:31:15 -0400152 if (key0->raw == cached_key->raw)
153 {
154 /* Hit in the one-entry cache */
155 result0->raw = cached_result->raw;
156 }
157 else
158 {
159 /* Do a regular mac table lookup */
160 BVT (clib_bihash_kv) kv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach97d8dc22016-08-15 15:31:15 -0400162 kv.key = key0->raw;
163 kv.value = ~0ULL;
164 BV (clib_bihash_search_inline) (mac_table, &kv);
165 result0->raw = kv.value;
166
167 /* Update one-entry cache */
168 cached_key->raw = key0->raw;
169 cached_result->raw = result0->raw;
170 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171}
172
173
Dave Barach97d8dc22016-08-15 15:31:15 -0400174/**
175 * Lookup the entry for mac and bd_index in the mac table for 2 packets.
176 * The lookups for the two packets are interleaved.
177 *
178 * Cached_key and cached_result are used as a one-entry cache.
179 * The function reads and updates them as needed.
180 *
181 * mac0 and bd_index0 are the keys. The entry is written to result0.
182 * If the entry was not found, result0 is set to ~0. The same
183 * holds for mac1/bd_index1/result1.
184 */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185static_always_inline void
Dave Barach97d8dc22016-08-15 15:31:15 -0400186l2fib_lookup_2 (BVT (clib_bihash) * mac_table,
187 l2fib_entry_key_t * cached_key,
188 l2fib_entry_result_t * cached_result,
189 u8 * mac0,
190 u8 * mac1,
191 u16 bd_index0,
192 u16 bd_index1,
193 l2fib_entry_key_t * key0,
194 l2fib_entry_key_t * key1,
195 u32 * bucket0,
196 u32 * bucket1,
197 l2fib_entry_result_t * result0,
198 l2fib_entry_result_t * result1)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700199{
Dave Barach97d8dc22016-08-15 15:31:15 -0400200 /* set up key */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700201 key0->raw = l2fib_make_key (mac0, bd_index0);
202 key1->raw = l2fib_make_key (mac1, bd_index1);
203
Dave Barach97d8dc22016-08-15 15:31:15 -0400204 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw))
205 {
206 /* Both hit in the one-entry cache */
207 result0->raw = cached_result->raw;
208 result1->raw = cached_result->raw;
209 *bucket0 = ~0;
210 *bucket1 = ~0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211
Dave Barach97d8dc22016-08-15 15:31:15 -0400212 }
213 else
214 {
215 BVT (clib_bihash_kv) kv0, kv1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
Dave Barach97d8dc22016-08-15 15:31:15 -0400217 /*
218 * Do a regular mac table lookup
219 * Interleave lookups for packet 0 and packet 1
220 */
221 kv0.key = key0->raw;
222 kv1.key = key1->raw;
223 kv0.value = ~0ULL;
224 kv1.value = ~0ULL;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700225
Dave Barach97d8dc22016-08-15 15:31:15 -0400226 BV (clib_bihash_search_inline) (mac_table, &kv0);
227 BV (clib_bihash_search_inline) (mac_table, &kv1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700228
Dave Barach97d8dc22016-08-15 15:31:15 -0400229 result0->raw = kv0.value;
230 result1->raw = kv1.value;
231
232 /* Update one-entry cache */
233 cached_key->raw = key1->raw;
234 cached_result->raw = result1->raw;
235 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236}
237
Damjan Mariondaa2cd12016-11-22 21:10:25 -0800238static_always_inline void
239l2fib_lookup_4 (BVT (clib_bihash) * mac_table,
240 l2fib_entry_key_t * cached_key,
241 l2fib_entry_result_t * cached_result,
242 u8 * mac0,
243 u8 * mac1,
244 u8 * mac2,
245 u8 * mac3,
246 u16 bd_index0,
247 u16 bd_index1,
248 u16 bd_index2,
249 u16 bd_index3,
250 l2fib_entry_key_t * key0,
251 l2fib_entry_key_t * key1,
252 l2fib_entry_key_t * key2,
253 l2fib_entry_key_t * key3,
254 u32 * bucket0,
255 u32 * bucket1,
256 u32 * bucket2,
257 u32 * bucket3,
258 l2fib_entry_result_t * result0,
259 l2fib_entry_result_t * result1,
260 l2fib_entry_result_t * result2,
261 l2fib_entry_result_t * result3)
262{
263 /* set up key */
264 key0->raw = l2fib_make_key (mac0, bd_index0);
265 key1->raw = l2fib_make_key (mac1, bd_index1);
266 key2->raw = l2fib_make_key (mac2, bd_index2);
267 key3->raw = l2fib_make_key (mac3, bd_index3);
268
269 if ((key0->raw == cached_key->raw) && (key1->raw == cached_key->raw) &&
270 (key2->raw == cached_key->raw) && (key3->raw == cached_key->raw))
271 {
272 /* Both hit in the one-entry cache */
273 result0->raw = cached_result->raw;
274 result1->raw = cached_result->raw;
275 result2->raw = cached_result->raw;
276 result3->raw = cached_result->raw;
277 *bucket0 = ~0;
278 *bucket1 = ~0;
279 *bucket2 = ~0;
280 *bucket3 = ~0;
281
282 }
283 else
284 {
285 BVT (clib_bihash_kv) kv0, kv1, kv2, kv3;
286
287 /*
288 * Do a regular mac table lookup
289 * Interleave lookups for packet 0 and packet 1
290 */
291 kv0.key = key0->raw;
292 kv1.key = key1->raw;
293 kv2.key = key2->raw;
294 kv3.key = key3->raw;
295 kv0.value = ~0ULL;
296 kv1.value = ~0ULL;
297 kv2.value = ~0ULL;
298 kv3.value = ~0ULL;
299
300 BV (clib_bihash_search_inline) (mac_table, &kv0);
301 BV (clib_bihash_search_inline) (mac_table, &kv1);
302 BV (clib_bihash_search_inline) (mac_table, &kv2);
303 BV (clib_bihash_search_inline) (mac_table, &kv3);
304
305 result0->raw = kv0.value;
306 result1->raw = kv1.value;
307 result2->raw = kv2.value;
308 result3->raw = kv3.value;
309
310 /* Update one-entry cache */
311 cached_key->raw = key1->raw;
312 cached_result->raw = result1->raw;
313 }
314}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Dave Barach97d8dc22016-08-15 15:31:15 -0400316BVT (clib_bihash) * get_mac_table (void);
317 void
318 l2fib_clear_table (uint keep_static);
319 void
320 l2fib_add_entry (u64 mac,
321 u32 bd_index,
322 u32 sw_if_index,
323 u32 static_mac, u32 drop_mac, u32 bvi_mac);
324u32
325l2fib_del_entry (u64 mac, u32 bd_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barach97d8dc22016-08-15 15:31:15 -0400327 void
328 l2fib_table_dump (u32 bd_index, l2fib_entry_key_t ** l2fe_key,
329 l2fib_entry_result_t ** l2fe_res);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Dave Barach97d8dc22016-08-15 15:31:15 -0400331 u8 *format_vnet_sw_if_index_name_with_NA (u8 * s, va_list * args);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
333#endif
Dave Barach97d8dc22016-08-15 15:31:15 -0400334
335/*
336 * fd.io coding-style-patch-verification: ON
337 *
338 * Local Variables:
339 * eval: (c-set-style "gnu")
340 * End:
341 */