blob: 2d8fbc8040443dc6f93bbef7ddacff74f2fee435 [file] [log] [blame]
Florin Corase127a7e2016-02-18 22:20:01 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/lisp-cp/gid_dictionary.h>
17
Filip Tehlard5fcc462016-10-17 16:20:18 +020018typedef struct
19{
20 void *arg;
21 ip_prefix_t src;
22 foreach_subprefix_match_cb_t cb;
23 union
24 {
25 gid_ip4_table_t *ip4_table;
26 gid_ip6_table_t *ip6_table;
27 };
28} sfib_entry_arg_t;
29
30static u32 ip4_lookup (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key);
31
32static u32 ip6_lookup (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key);
33
34static void
35foreach_sfib4_subprefix (BVT (clib_bihash_kv) * kvp, void *arg)
36{
37 sfib_entry_arg_t *a = arg;
38 u32 ip = (u32) kvp->key[0];
39 ip4_address_t *mask;
40 u8 plen = ip_prefix_len (&a->src);
41
Filip Tehlar218170b2016-10-26 14:58:18 +020042 ASSERT (plen <= 32);
Filip Tehlard5fcc462016-10-17 16:20:18 +020043 mask = &a->ip4_table->ip4_fib_masks[plen];
44
Filip Tehlarfb9931f2016-12-09 13:52:38 +010045 u32 src_ip = ip_prefix_v4 (&a->src).as_u32;
Filip Tehlard5fcc462016-10-17 16:20:18 +020046 src_ip &= mask->as_u32;
Filip Tehlarfb9931f2016-12-09 13:52:38 +010047 ip &= mask->as_u32;
48
Filip Tehlard5fcc462016-10-17 16:20:18 +020049 if (src_ip == ip)
50 {
51 /* found sub-prefix of src prefix */
52 (a->cb) (kvp->value, a->arg);
53 }
54}
55
56static void
57gid_dict_foreach_ip4_subprefix (gid_dictionary_t * db, u32 vni,
58 ip_prefix_t * src, ip_prefix_t * dst,
59 foreach_subprefix_match_cb_t cb, void *arg)
60{
61 u32 sfi;
62 gid_ip4_table_t *sfib4;
63 sfib_entry_arg_t a;
64
65 sfi = ip4_lookup (&db->dst_ip4_table, vni, dst);
66 if (GID_LOOKUP_MISS == sfi)
67 return;
68
69 sfib4 = pool_elt_at_index (db->src_ip4_table_pool, sfi);
70
71 a.arg = arg;
72 a.cb = cb;
73 a.src = src[0];
74 a.ip4_table = sfib4;
75
76 BV (clib_bihash_foreach_key_value_pair) (&sfib4->ip4_lookup_table,
77 foreach_sfib4_subprefix, &a);
78}
79
80static void
81foreach_sfib6_subprefix (BVT (clib_bihash_kv) * kvp, void *arg)
82{
83 sfib_entry_arg_t *a = arg;
84 ip6_address_t ip;
85 ip6_address_t *mask;
86 u8 plen = ip_prefix_len (&a->src);
87
88 mask = &a->ip6_table->ip6_fib_masks[plen];
89 ip.as_u64[0] = kvp->key[0];
90 ip.as_u64[1] = kvp->key[1];
91
92 if (ip6_address_is_equal_masked (&ip_prefix_v6 (&a->src), &ip, mask))
93 {
94 /* found sub-prefix of src prefix */
95 (a->cb) (kvp->value, a->arg);
96 }
97}
98
99static void
100gid_dict_foreach_ip6_subprefix (gid_dictionary_t * db, u32 vni,
101 ip_prefix_t * src, ip_prefix_t * dst,
102 foreach_subprefix_match_cb_t cb, void *arg)
103{
104 u32 sfi;
105 gid_ip6_table_t *sfib6;
106 sfib_entry_arg_t a;
107
108 sfi = ip6_lookup (&db->dst_ip6_table, vni, dst);
109 if (GID_LOOKUP_MISS == sfi)
110 return;
111
112 sfib6 = pool_elt_at_index (db->src_ip6_table_pool, sfi);
113
114 a.arg = arg;
115 a.cb = cb;
116 a.src = src[0];
117 a.ip6_table = sfib6;
118
119 BV (clib_bihash_foreach_key_value_pair) (&sfib6->ip6_lookup_table,
120 foreach_sfib6_subprefix, &a);
121}
122
123void
124gid_dict_foreach_subprefix (gid_dictionary_t * db, gid_address_t * eid,
125 foreach_subprefix_match_cb_t cb, void *arg)
126{
127 ip_prefix_t *ippref = &gid_address_sd_dst_ippref (eid);
128
129 if (IP4 == ip_prefix_version (ippref))
130 gid_dict_foreach_ip4_subprefix (db, gid_address_vni (eid),
131 &gid_address_sd_src_ippref (eid),
132 &gid_address_sd_dst_ippref (eid), cb,
133 arg);
134 else
135 gid_dict_foreach_ip6_subprefix (db, gid_address_vni (eid),
136 &gid_address_sd_src_ippref (eid),
137 &gid_address_sd_dst_ippref (eid), cb,
138 arg);
139}
140
Filip Tehlard5a65db2017-05-17 17:21:10 +0200141void
Filip Tehlar05879992017-09-05 15:46:09 +0200142gid_dict_foreach_l2_arp_ndp_entry (gid_dictionary_t * db, void (*cb)
143 (BVT (clib_bihash_kv) * kvp, void *arg),
144 void *ht)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200145{
Filip Tehlar05879992017-09-05 15:46:09 +0200146 gid_l2_arp_ndp_table_t *tab = &db->arp_ndp_table;
147 BV (clib_bihash_foreach_key_value_pair) (&tab->arp_ndp_lookup_table, cb,
148 ht);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200149}
150
Florin Corasdca88042016-09-14 16:01:38 +0200151static void
152make_mac_sd_key (BVT (clib_bihash_kv) * kv, u32 vni, u8 src_mac[6],
153 u8 dst_mac[6])
154{
155 kv->key[0] = (u64) vni;
156 kv->key[1] = mac_to_u64 (dst_mac);
157 kv->key[2] = src_mac ? mac_to_u64 (src_mac) : (u64) 0;
158}
159
Florin Corase127a7e2016-02-18 22:20:01 +0100160static u32
Florin Corasdca88042016-09-14 16:01:38 +0200161mac_sd_lookup (gid_mac_table_t * db, u32 vni, u8 * dst, u8 * src)
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200162{
163 int rv;
Florin Corasa2157cf2016-08-16 21:09:14 +0200164 BVT (clib_bihash_kv) kv, value;
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200165
Florin Corasdca88042016-09-14 16:01:38 +0200166 make_mac_sd_key (&kv, vni, src, dst);
Florin Corasa2157cf2016-08-16 21:09:14 +0200167 rv = BV (clib_bihash_search_inline_2) (&db->mac_lookup_table, &kv, &value);
Florin Corasdca88042016-09-14 16:01:38 +0200168
169 /* no match, try with src 0, catch all for dst */
170 if (rv != 0)
171 {
172 kv.key[2] = 0;
173 rv = BV (clib_bihash_search_inline_2) (&db->mac_lookup_table, &kv,
174 &value);
175 if (rv == 0)
176 return value.value;
177 }
178 else
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200179 return value.value;
180
181 return GID_LOOKUP_MISS;
182}
183
184static u32
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200185ip4_lookup_exact_match (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key)
186{
187 int rv;
188 BVT (clib_bihash_kv) kv, value;
189
190 ip4_address_t *mask;
191
192 mask = &db->ip4_fib_masks[ip_prefix_len (key)];
193
194 kv.key[0] = ((u64) vni << 32) | (ip_prefix_v4 (key).as_u32 & mask->as_u32);
195 kv.key[1] = 0;
196 kv.key[2] = 0;
197
198 rv = BV (clib_bihash_search_inline_2) (&db->ip4_lookup_table, &kv, &value);
199 if (rv == 0)
200 return value.value;
201
202 return GID_LOOKUP_MISS;
203}
204
205static u32
Florin Corasdca88042016-09-14 16:01:38 +0200206ip4_lookup (gid_ip4_table_t * db, u32 vni, ip_prefix_t * key)
Florin Corase127a7e2016-02-18 22:20:01 +0100207{
208 int i, len;
209 int rv;
Florin Corasa2157cf2016-08-16 21:09:14 +0200210 BVT (clib_bihash_kv) kv, value;
Florin Corase127a7e2016-02-18 22:20:01 +0100211
212 len = vec_len (db->ip4_prefix_lengths_in_search_order);
213
214 for (i = 0; i < len; i++)
215 {
216 int dst_address_length = db->ip4_prefix_lengths_in_search_order[i];
Florin Corasa2157cf2016-08-16 21:09:14 +0200217 ip4_address_t *mask;
Florin Corase127a7e2016-02-18 22:20:01 +0100218
Florin Corasa2157cf2016-08-16 21:09:14 +0200219 ASSERT (dst_address_length >= 0 && dst_address_length <= 32);
Florin Corase127a7e2016-02-18 22:20:01 +0100220
221 mask = &db->ip4_fib_masks[dst_address_length];
222
Florin Corasa2157cf2016-08-16 21:09:14 +0200223 kv.key[0] =
224 ((u64) vni << 32) | (ip_prefix_v4 (key).as_u32 & mask->as_u32);
Florin Corase127a7e2016-02-18 22:20:01 +0100225 kv.key[1] = 0;
226 kv.key[2] = 0;
227
Florin Corasa2157cf2016-08-16 21:09:14 +0200228 rv =
229 BV (clib_bihash_search_inline_2) (&db->ip4_lookup_table, &kv, &value);
Florin Corase127a7e2016-02-18 22:20:01 +0100230 if (rv == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200231 return value.value;
Florin Corase127a7e2016-02-18 22:20:01 +0100232 }
233
234 return GID_LOOKUP_MISS;
235}
236
237static u32
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200238ip6_lookup_exact_match (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key)
239{
240 int rv;
241 BVT (clib_bihash_kv) kv, value;
242
243 ip6_address_t *mask;
244 mask = &db->ip6_fib_masks[ip_prefix_len (key)];
245
246 kv.key[0] = ip_prefix_v6 (key).as_u64[0] & mask->as_u64[0];
247 kv.key[1] = ip_prefix_v6 (key).as_u64[1] & mask->as_u64[1];
248 kv.key[2] = (u64) vni;
249
250 rv = BV (clib_bihash_search_inline_2) (&db->ip6_lookup_table, &kv, &value);
251 if (rv == 0)
252 return value.value;
253
254 return GID_LOOKUP_MISS;
255}
256
257static u32
Florin Corasdca88042016-09-14 16:01:38 +0200258ip6_lookup (gid_ip6_table_t * db, u32 vni, ip_prefix_t * key)
Florin Corase127a7e2016-02-18 22:20:01 +0100259{
260 int i, len;
261 int rv;
Florin Corasa2157cf2016-08-16 21:09:14 +0200262 BVT (clib_bihash_kv) kv, value;
Florin Corase127a7e2016-02-18 22:20:01 +0100263
264 len = vec_len (db->ip6_prefix_lengths_in_search_order);
265
266 for (i = 0; i < len; i++)
267 {
268 int dst_address_length = db->ip6_prefix_lengths_in_search_order[i];
Florin Corasa2157cf2016-08-16 21:09:14 +0200269 ip6_address_t *mask;
Florin Corase127a7e2016-02-18 22:20:01 +0100270
Florin Corasa2157cf2016-08-16 21:09:14 +0200271 ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
Florin Corase127a7e2016-02-18 22:20:01 +0100272
273 mask = &db->ip6_fib_masks[dst_address_length];
274
Florin Corasa2157cf2016-08-16 21:09:14 +0200275 kv.key[0] = ip_prefix_v6 (key).as_u64[0] & mask->as_u64[0];
276 kv.key[1] = ip_prefix_v6 (key).as_u64[1] & mask->as_u64[1];
277 kv.key[2] = (u64) vni;
Florin Corase127a7e2016-02-18 22:20:01 +0100278
Florin Corasa2157cf2016-08-16 21:09:14 +0200279 rv =
280 BV (clib_bihash_search_inline_2) (&db->ip6_lookup_table, &kv, &value);
Florin Corase127a7e2016-02-18 22:20:01 +0100281 if (rv == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200282 return value.value;
Florin Corase127a7e2016-02-18 22:20:01 +0100283 }
284
285 return GID_LOOKUP_MISS;
286}
287
288static u32
Florin Corasdca88042016-09-14 16:01:38 +0200289ip_sd_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t * dst,
290 ip_prefix_t * src)
Florin Corase127a7e2016-02-18 22:20:01 +0100291{
Florin Corasdca88042016-09-14 16:01:38 +0200292 u32 sfi;
293 gid_ip4_table_t *sfib4;
294 gid_ip6_table_t *sfib6;
295
296 switch (ip_prefix_version (dst))
Florin Corase127a7e2016-02-18 22:20:01 +0100297 {
298 case IP4:
Florin Corasdca88042016-09-14 16:01:38 +0200299 sfi = ip4_lookup (&db->dst_ip4_table, vni, dst);
300 if (GID_LOOKUP_MISS != sfi)
301 sfib4 = pool_elt_at_index (db->src_ip4_table_pool, sfi);
302 else
303 return GID_LOOKUP_MISS;
304
305 if (!src)
306 {
307 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400308 clib_memset (&sp, 0, sizeof (sp));
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200309 return ip4_lookup_exact_match (sfib4, 0, &sp);
Florin Corasdca88042016-09-14 16:01:38 +0200310 }
311 else
312 return ip4_lookup (sfib4, 0, src);
313
Florin Corase127a7e2016-02-18 22:20:01 +0100314 break;
315 case IP6:
Florin Corasdca88042016-09-14 16:01:38 +0200316 sfi = ip6_lookup (&db->dst_ip6_table, vni, dst);
317 if (GID_LOOKUP_MISS != sfi)
318 sfib6 = pool_elt_at_index (db->src_ip6_table_pool, sfi);
319 else
320 return GID_LOOKUP_MISS;
321
322 if (!src)
323 {
324 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400325 clib_memset (&sp, 0, sizeof (sp));
Florin Corasdca88042016-09-14 16:01:38 +0200326 ip_prefix_version (&sp) = IP6;
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200327 return ip6_lookup_exact_match (sfib6, 0, &sp);
Florin Corasdca88042016-09-14 16:01:38 +0200328 }
329 else
330 return ip6_lookup (sfib6, 0, src);
331
Florin Corase127a7e2016-02-18 22:20:01 +0100332 break;
333 default:
Florin Corasa2157cf2016-08-16 21:09:14 +0200334 clib_warning ("address type %d not supported!",
Florin Corasdca88042016-09-14 16:01:38 +0200335 ip_prefix_version (dst));
Florin Corase127a7e2016-02-18 22:20:01 +0100336 break;
337 }
Florin Corasdca88042016-09-14 16:01:38 +0200338 return GID_LOOKUP_MISS;
Florin Corase127a7e2016-02-18 22:20:01 +0100339}
340
Filip Tehlard5a65db2017-05-17 17:21:10 +0200341static void
Filip Tehlar05879992017-09-05 15:46:09 +0200342make_arp_ndp_key (BVT (clib_bihash_kv) * kv, u32 bd, ip_address_t * addr)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200343{
Filip Tehlar05879992017-09-05 15:46:09 +0200344 kv->key[0] = ((u64) bd << 32) | (u32) ip_addr_version (addr);
345 if (ip_addr_version (addr) == IP4)
346 {
347 kv->key[1] = (u64) addr->ip.v4.as_u32;
348 kv->key[2] = (u64) 0;
349 }
350 else
351 {
352 kv->key[1] = (u64) addr->ip.v6.as_u64[0];
353 kv->key[2] = (u64) addr->ip.v6.as_u64[1];
354 }
Filip Tehlard5a65db2017-05-17 17:21:10 +0200355}
356
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200357static void
358make_nsh_key (BVT (clib_bihash_kv) * kv, u32 vni, u32 spi, u8 si)
359{
360 kv->key[0] = (u64) vni;
361 kv->key[1] = (u64) spi;
362 kv->key[2] = (u64) si;
363}
364
Filip Tehlard5a65db2017-05-17 17:21:10 +0200365static u64
Filip Tehlar05879992017-09-05 15:46:09 +0200366arp_ndp_lookup (gid_l2_arp_ndp_table_t * db, u32 bd, ip_address_t * key)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200367{
368 int rv;
369 BVT (clib_bihash_kv) kv, value;
370
Filip Tehlar05879992017-09-05 15:46:09 +0200371 make_arp_ndp_key (&kv, bd, key);
372 rv = BV (clib_bihash_search_inline_2) (&db->arp_ndp_lookup_table, &kv,
373 &value);
Filip Tehlard5a65db2017-05-17 17:21:10 +0200374
375 if (rv == 0)
376 return value.value;
377
378 return GID_LOOKUP_MISS_L2;
379}
380
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200381static u32
382nsh_lookup (gid_nsh_table_t * db, u32 vni, u32 spi, u8 si)
383{
384 int rv;
385 BVT (clib_bihash_kv) kv, value;
386
387 make_nsh_key (&kv, vni, spi, si);
388 rv = BV (clib_bihash_search_inline_2) (&db->nsh_lookup_table, &kv, &value);
389
390 if (rv == 0)
391 return value.value;
392
393 return GID_LOOKUP_MISS;
394}
395
Filip Tehlard5a65db2017-05-17 17:21:10 +0200396u64
Florin Corase127a7e2016-02-18 22:20:01 +0100397gid_dictionary_lookup (gid_dictionary_t * db, gid_address_t * key)
398{
Florin Corase127a7e2016-02-18 22:20:01 +0100399 switch (gid_address_type (key))
400 {
Filip Tehlarbeceab92016-04-20 17:21:55 +0200401 case GID_ADDR_IP_PREFIX:
Florin Corasdca88042016-09-14 16:01:38 +0200402 return ip_sd_lookup (db, gid_address_vni (key),
403 &gid_address_ippref (key), 0);
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200404 case GID_ADDR_MAC:
Florin Corasdca88042016-09-14 16:01:38 +0200405 return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (key),
406 gid_address_mac (key), 0);
407 case GID_ADDR_SRC_DST:
408 switch (gid_address_sd_dst_type (key))
409 {
410 case FID_ADDR_IP_PREF:
411 return ip_sd_lookup (db, gid_address_vni (key),
412 &gid_address_sd_dst_ippref (key),
413 &gid_address_sd_src_ippref (key));
414 break;
415 case FID_ADDR_MAC:
416 return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (key),
417 gid_address_sd_dst_mac (key),
418 gid_address_sd_src_mac (key));
419 break;
420 default:
421 clib_warning ("Source/Dest address type %d not supported!",
422 gid_address_sd_dst_type (key));
423 break;
424 }
425 break;
Filip Tehlard5a65db2017-05-17 17:21:10 +0200426 case GID_ADDR_ARP:
Filip Tehlar05879992017-09-05 15:46:09 +0200427 case GID_ADDR_NDP:
428 return arp_ndp_lookup (&db->arp_ndp_table, gid_address_arp_ndp_bd (key),
429 &gid_address_arp_ndp_ip (key));
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200430 case GID_ADDR_NSH:
431 return nsh_lookup (&db->nsh_table, gid_address_vni (key),
432 gid_address_nsh_spi (key), gid_address_nsh_si (key));
Florin Corase127a7e2016-02-18 22:20:01 +0100433 default:
Florin Corasa2157cf2016-08-16 21:09:14 +0200434 clib_warning ("address type %d not supported!", gid_address_type (key));
Florin Corase127a7e2016-02-18 22:20:01 +0100435 break;
436 }
Florin Corasf727db92016-06-23 15:01:58 +0200437 return GID_LOOKUP_MISS;
Florin Corase127a7e2016-02-18 22:20:01 +0100438}
439
Florin Corasdca88042016-09-14 16:01:38 +0200440u32
441gid_dictionary_sd_lookup (gid_dictionary_t * db, gid_address_t * dst,
442 gid_address_t * src)
443{
444 switch (gid_address_type (dst))
445 {
446 case GID_ADDR_IP_PREFIX:
447 return ip_sd_lookup (db, gid_address_vni (dst),
448 &gid_address_ippref (dst),
449 &gid_address_ippref (src));
450 case GID_ADDR_MAC:
451 return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (dst),
452 gid_address_mac (dst), gid_address_mac (src));
Filip Tehlard5fcc462016-10-17 16:20:18 +0200453 case GID_ADDR_SRC_DST:
454 switch (gid_address_sd_dst_type (dst))
455 {
456 case FID_ADDR_IP_PREF:
457 return ip_sd_lookup (db, gid_address_vni (dst),
458 &gid_address_sd_dst_ippref (dst),
459 &gid_address_sd_src_ippref (dst));
460 break;
461 case FID_ADDR_MAC:
462 return mac_sd_lookup (&db->sd_mac_table, gid_address_vni (dst),
463 gid_address_sd_dst_mac (dst),
464 gid_address_sd_src_mac (dst));
465 break;
466 default:
467 clib_warning ("Source/Dest address type %d not supported!",
468 gid_address_sd_dst_type (dst));
469 break;
470 }
Filip Tehlar218170b2016-10-26 14:58:18 +0200471 break;
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200472 case GID_ADDR_NSH:
473 return gid_dictionary_lookup (db, dst);
474 break;
Florin Corasdca88042016-09-14 16:01:38 +0200475 default:
476 clib_warning ("address type %d not supported!", gid_address_type (dst));
477 break;
478 }
479 return GID_LOOKUP_MISS;
480}
481
Florin Corase127a7e2016-02-18 22:20:01 +0100482static void
Florin Corasdca88042016-09-14 16:01:38 +0200483ip4_compute_prefix_lengths_in_search_order (gid_ip4_table_t * db)
Florin Corase127a7e2016-02-18 22:20:01 +0100484{
485 int i;
486 vec_reset_length (db->ip4_prefix_lengths_in_search_order);
487 /* Note: bitmap reversed so this is in fact a longest prefix match */
Florin Corasa2157cf2016-08-16 21:09:14 +0200488
489 /* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +0100490 clib_bitmap_foreach (i, db->ip4_non_empty_dst_address_length_bitmap,
491 ({
492 int dst_address_length = 32 - i;
493 vec_add1 (db->ip4_prefix_lengths_in_search_order, dst_address_length);
494 }));
Florin Corasa2157cf2016-08-16 21:09:14 +0200495 /* *INDENT-ON* */
496
Florin Corase127a7e2016-02-18 22:20:01 +0100497}
498
499static u32
Florin Corasdca88042016-09-14 16:01:38 +0200500add_del_ip4_key (gid_ip4_table_t * db, u32 vni, ip_prefix_t * pref, u32 val,
Florin Corasa2157cf2016-08-16 21:09:14 +0200501 u8 is_add)
Florin Corase127a7e2016-02-18 22:20:01 +0100502{
Florin Corasa2157cf2016-08-16 21:09:14 +0200503 BVT (clib_bihash_kv) kv, value;
Florin Corase127a7e2016-02-18 22:20:01 +0100504 u32 old_val = ~0;
505 ip4_address_t key;
506 u8 plen = ip_prefix_len (pref);
507
Florin Corasa2157cf2016-08-16 21:09:14 +0200508 clib_memcpy (&key, &ip_prefix_v4 (pref), sizeof (key));
Florin Corase127a7e2016-02-18 22:20:01 +0100509 key.as_u32 &= db->ip4_fib_masks[plen].as_u32;
510 if (is_add)
511 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200512 db->ip4_non_empty_dst_address_length_bitmap =
513 clib_bitmap_set (db->ip4_non_empty_dst_address_length_bitmap,
514 32 - plen, 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100515 ip4_compute_prefix_lengths_in_search_order (db);
516
517 db->ip4_prefix_len_refcount[plen]++;
518 }
519 else
520 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200521 ASSERT (db->ip4_prefix_len_refcount[plen] != 0);
Florin Corase127a7e2016-02-18 22:20:01 +0100522
523 db->ip4_prefix_len_refcount[plen]--;
524
525 if (db->ip4_prefix_len_refcount[plen] == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200526 {
527 db->ip4_non_empty_dst_address_length_bitmap =
528 clib_bitmap_set (db->ip4_non_empty_dst_address_length_bitmap,
529 32 - plen, 0);
530 ip4_compute_prefix_lengths_in_search_order (db);
531 }
Florin Corase127a7e2016-02-18 22:20:01 +0100532 }
533
534 kv.key[0] = ((u64) vni << 32) | key.as_u32;
535 kv.key[1] = 0;
536 kv.key[2] = 0;
537
Florin Corasa2157cf2016-08-16 21:09:14 +0200538 if (BV (clib_bihash_search) (&db->ip4_lookup_table, &kv, &value) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +0100539 old_val = value.value;
540
541 if (!is_add)
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100542 {
543 BV (clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 0 /* is_add */ );
544 db->count--;
545 }
Florin Corase127a7e2016-02-18 22:20:01 +0100546 else
547 {
548 kv.value = val;
Florin Corasa2157cf2016-08-16 21:09:14 +0200549 BV (clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 1 /* is_add */ );
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100550 db->count++;
Florin Corase127a7e2016-02-18 22:20:01 +0100551 }
552 return old_val;
553}
554
555static void
Florin Corasdca88042016-09-14 16:01:38 +0200556ip4_lookup_init (gid_ip4_table_t * db)
557{
558 uword i;
559
Dave Barachb7b92992018-10-17 10:38:51 -0400560 clib_memset (db->ip4_prefix_len_refcount, 0,
561 sizeof (db->ip4_prefix_len_refcount));
Florin Corasdca88042016-09-14 16:01:38 +0200562
563 for (i = 0; i < ARRAY_LEN (db->ip4_fib_masks); i++)
564 {
565 u32 m;
566
567 if (i < 32)
568 m = pow2_mask (i) << (32 - i);
569 else
570 m = ~0;
571 db->ip4_fib_masks[i].as_u32 = clib_host_to_net_u32 (m);
572 }
573 if (db->ip4_lookup_table_nbuckets == 0)
574 db->ip4_lookup_table_nbuckets = IP4_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
575
576 db->ip4_lookup_table_nbuckets =
577 1 << max_log2 (db->ip4_lookup_table_nbuckets);
578
579 if (db->ip4_lookup_table_size == 0)
580 db->ip4_lookup_table_size = IP4_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
581
582 BV (clib_bihash_init) (&db->ip4_lookup_table, "ip4 lookup table",
583 db->ip4_lookup_table_nbuckets,
584 db->ip4_lookup_table_size);
585}
586
587static u32
588add_del_sd_ip4_key (gid_dictionary_t * db, u32 vni, ip_prefix_t * dst_pref,
589 ip_prefix_t * src_pref, u32 val, u8 is_add)
590{
591 u32 sfi, old_val = ~0;
592 gid_ip4_table_t *sfib;
593
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200594 sfi = ip4_lookup_exact_match (&db->dst_ip4_table, vni, dst_pref);
Florin Corasdca88042016-09-14 16:01:38 +0200595
596 if (is_add)
597 {
598 if (GID_LOOKUP_MISS == sfi)
599 {
600 pool_get (db->src_ip4_table_pool, sfib);
601 ip4_lookup_init (sfib);
602 add_del_ip4_key (&db->dst_ip4_table, vni, dst_pref,
603 sfib - db->src_ip4_table_pool, is_add);
604 if (src_pref)
605 add_del_ip4_key (sfib, 0 /* vni */ , src_pref, val, is_add);
606 else
607 {
608 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400609 clib_memset (&sp, 0, sizeof (sp));
Florin Corasdca88042016-09-14 16:01:38 +0200610 add_del_ip4_key (sfib, 0 /* vni */ , &sp, val, is_add);
611 }
612 }
613 else
614 {
615 ASSERT (!pool_is_free_index (db->src_ip4_table_pool, sfi));
616 sfib = pool_elt_at_index (db->src_ip4_table_pool, sfi);
617 if (src_pref)
618 {
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200619 old_val = ip4_lookup_exact_match (sfib, 0, src_pref);
Florin Corasdca88042016-09-14 16:01:38 +0200620 add_del_ip4_key (sfib, 0 /* vni */ , src_pref, val, is_add);
621 }
622 else
623 {
624 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400625 clib_memset (&sp, 0, sizeof (sp));
Florin Corasdca88042016-09-14 16:01:38 +0200626 old_val =
627 add_del_ip4_key (sfib, 0 /* vni */ , &sp, val, is_add);
628 }
629 }
630 }
631 else
632 {
633 if (GID_LOOKUP_MISS != sfi)
634 {
Florin Corasdca88042016-09-14 16:01:38 +0200635 sfib = pool_elt_at_index (db->src_ip4_table_pool, sfi);
636 if (src_pref)
637 old_val = add_del_ip4_key (sfib, 0, src_pref, 0, is_add);
638 else
639 {
640 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400641 clib_memset (&sp, 0, sizeof (sp));
Florin Corasdca88042016-09-14 16:01:38 +0200642 old_val = add_del_ip4_key (sfib, 0, &sp, 0, is_add);
643 }
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100644
645 if (sfib->count == 0)
646 add_del_ip4_key (&db->dst_ip4_table, vni, dst_pref, 0, is_add);
Florin Corasdca88042016-09-14 16:01:38 +0200647 }
648 else
649 clib_warning ("cannot delete dst mapping %U!", format_ip_prefix,
650 dst_pref);
651 }
652 return old_val;
653}
654
655static void
656ip6_compute_prefix_lengths_in_search_order (gid_ip6_table_t * db)
Florin Corase127a7e2016-02-18 22:20:01 +0100657{
658 int i;
659 vec_reset_length (db->ip6_prefix_lengths_in_search_order);
660 /* Note: bitmap reversed so this is in fact a longest prefix match */
Florin Corasa2157cf2016-08-16 21:09:14 +0200661
662 /* *INDENT-OFF* */
Florin Corase127a7e2016-02-18 22:20:01 +0100663 clib_bitmap_foreach (i, db->ip6_non_empty_dst_address_length_bitmap,
664 ({
665 int dst_address_length = 128 - i;
666 vec_add1 (db->ip6_prefix_lengths_in_search_order, dst_address_length);
667 }));
Florin Corasa2157cf2016-08-16 21:09:14 +0200668 /* *INDENT-ON* */
Florin Corase127a7e2016-02-18 22:20:01 +0100669}
670
671static u32
Florin Corasdca88042016-09-14 16:01:38 +0200672add_del_ip6_key (gid_ip6_table_t * db, u32 vni, ip_prefix_t * pref, u32 val,
Florin Corasa2157cf2016-08-16 21:09:14 +0200673 u8 is_add)
Florin Corase127a7e2016-02-18 22:20:01 +0100674{
Florin Corasa2157cf2016-08-16 21:09:14 +0200675 BVT (clib_bihash_kv) kv, value;
Florin Corase127a7e2016-02-18 22:20:01 +0100676 u32 old_val = ~0;
677 ip6_address_t key;
678 u8 plen = ip_prefix_len (pref);
679
Florin Corasa2157cf2016-08-16 21:09:14 +0200680 clib_memcpy (&key, &ip_prefix_v6 (pref), sizeof (key));
Florin Corase127a7e2016-02-18 22:20:01 +0100681 ip6_address_mask (&key, &db->ip6_fib_masks[plen]);
682 if (is_add)
683 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200684 db->ip6_non_empty_dst_address_length_bitmap =
685 clib_bitmap_set (db->ip6_non_empty_dst_address_length_bitmap,
686 128 - plen, 1);
Florin Corase127a7e2016-02-18 22:20:01 +0100687 ip6_compute_prefix_lengths_in_search_order (db);
688 db->ip6_prefix_len_refcount[plen]++;
689 }
690 else
691 {
Florin Corasa2157cf2016-08-16 21:09:14 +0200692 ASSERT (db->ip6_prefix_len_refcount[plen] != 0);
Florin Corase127a7e2016-02-18 22:20:01 +0100693
694 db->ip6_prefix_len_refcount[plen]--;
695
696 if (db->ip6_prefix_len_refcount[plen] == 0)
Florin Corasa2157cf2016-08-16 21:09:14 +0200697 {
698 db->ip6_non_empty_dst_address_length_bitmap =
699 clib_bitmap_set (db->ip6_non_empty_dst_address_length_bitmap,
700 128 - plen, 0);
701 ip6_compute_prefix_lengths_in_search_order (db);
702 }
Florin Corase127a7e2016-02-18 22:20:01 +0100703 }
704
705 kv.key[0] = key.as_u64[0];
706 kv.key[1] = key.as_u64[1];
707 kv.key[2] = (u64) vni;
708// kv.key[2] = ((u64)((fib - im->fibs))<<32) | ip_prefix_len(key);
709
Florin Corasa2157cf2016-08-16 21:09:14 +0200710 if (BV (clib_bihash_search) (&db->ip6_lookup_table, &kv, &value) == 0)
Florin Corase127a7e2016-02-18 22:20:01 +0100711 old_val = value.value;
712
713 if (!is_add)
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100714 {
715 BV (clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 0 /* is_add */ );
716 db->count--;
717 }
Florin Corase127a7e2016-02-18 22:20:01 +0100718 else
719 {
720 kv.value = val;
Florin Corasa2157cf2016-08-16 21:09:14 +0200721 BV (clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 1 /* is_add */ );
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100722 db->count++;
Florin Corase127a7e2016-02-18 22:20:01 +0100723 }
724 return old_val;
725}
726
727static u32
Florin Corasdca88042016-09-14 16:01:38 +0200728add_del_mac (gid_mac_table_t * db, u32 vni, u8 * dst_mac, u8 * src_mac,
729 u32 val, u8 is_add)
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200730{
Florin Corasa2157cf2016-08-16 21:09:14 +0200731 BVT (clib_bihash_kv) kv, value;
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200732 u32 old_val = ~0;
733
Florin Corasdca88042016-09-14 16:01:38 +0200734 make_mac_sd_key (&kv, vni, src_mac, dst_mac);
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200735
Florin Corasa2157cf2016-08-16 21:09:14 +0200736 if (BV (clib_bihash_search) (&db->mac_lookup_table, &kv, &value) == 0)
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200737 old_val = value.value;
738
739 if (!is_add)
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100740 {
741 BV (clib_bihash_add_del) (&db->mac_lookup_table, &kv, 0 /* is_add */ );
742 db->count--;
743 }
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200744 else
745 {
746 kv.value = val;
Florin Corasa2157cf2016-08-16 21:09:14 +0200747 BV (clib_bihash_add_del) (&db->mac_lookup_table, &kv, 1 /* is_add */ );
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100748 db->count++;
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200749 }
750 return old_val;
751}
752
Florin Corase127a7e2016-02-18 22:20:01 +0100753static void
Florin Corasdca88042016-09-14 16:01:38 +0200754ip6_lookup_init (gid_ip6_table_t * db)
Florin Corase127a7e2016-02-18 22:20:01 +0100755{
756 uword i;
757
Dave Barachb7b92992018-10-17 10:38:51 -0400758 clib_memset (db->ip6_prefix_len_refcount, 0,
759 sizeof (db->ip6_prefix_len_refcount));
Florin Corase127a7e2016-02-18 22:20:01 +0100760
Florin Corasa2157cf2016-08-16 21:09:14 +0200761 for (i = 0; i < ARRAY_LEN (db->ip6_fib_masks); i++)
Florin Corase127a7e2016-02-18 22:20:01 +0100762 {
763 u32 j, i0, i1;
764
765 i0 = i / 32;
766 i1 = i % 32;
767
768 for (j = 0; j < i0; j++)
Florin Corasa2157cf2016-08-16 21:09:14 +0200769 db->ip6_fib_masks[i].as_u32[j] = ~0;
Florin Corase127a7e2016-02-18 22:20:01 +0100770
771 if (i1)
Florin Corasa2157cf2016-08-16 21:09:14 +0200772 db->ip6_fib_masks[i].as_u32[i0] =
773 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
Florin Corase127a7e2016-02-18 22:20:01 +0100774 }
775
776 if (db->ip6_lookup_table_nbuckets == 0)
777 db->ip6_lookup_table_nbuckets = IP6_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
778
Florin Corasa2157cf2016-08-16 21:09:14 +0200779 db->ip6_lookup_table_nbuckets =
780 1 << max_log2 (db->ip6_lookup_table_nbuckets);
Florin Corase127a7e2016-02-18 22:20:01 +0100781
782 if (db->ip6_lookup_table_size == 0)
783 db->ip6_lookup_table_size = IP6_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
784
Florin Corasa2157cf2016-08-16 21:09:14 +0200785 BV (clib_bihash_init) (&db->ip6_lookup_table, "ip6 lookup table",
786 db->ip6_lookup_table_nbuckets,
787 db->ip6_lookup_table_size);
Florin Corase127a7e2016-02-18 22:20:01 +0100788}
789
Florin Corasdca88042016-09-14 16:01:38 +0200790static u32
791add_del_sd_ip6_key (gid_dictionary_t * db, u32 vni, ip_prefix_t * dst_pref,
792 ip_prefix_t * src_pref, u32 val, u8 is_add)
793{
794 u32 sfi, old_val = ~0;
795 gid_ip6_table_t *sfib;
796
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200797 sfi = ip6_lookup_exact_match (&db->dst_ip6_table, vni, dst_pref);
Florin Corasdca88042016-09-14 16:01:38 +0200798
799 if (is_add)
800 {
801 if (GID_LOOKUP_MISS == sfi)
802 {
803 pool_get (db->src_ip6_table_pool, sfib);
804 ip6_lookup_init (sfib);
805 add_del_ip6_key (&db->dst_ip6_table, vni, dst_pref,
806 sfib - db->src_ip6_table_pool, is_add);
807 if (src_pref)
808 add_del_ip6_key (sfib, 0 /* vni */ , src_pref, val, is_add);
809 else
810 {
811 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400812 clib_memset (&sp, 0, sizeof (sp));
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200813 ip_prefix_version (&sp) = IP6;
Florin Corasdca88042016-09-14 16:01:38 +0200814 add_del_ip6_key (sfib, 0 /* vni */ , &sp, val, is_add);
815 }
816 }
817 else
818 {
819 ASSERT (!pool_is_free_index (db->src_ip6_table_pool, sfi));
820 sfib = pool_elt_at_index (db->src_ip6_table_pool, sfi);
821 if (src_pref)
822 {
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200823 old_val = ip6_lookup_exact_match (sfib, 0, src_pref);
Florin Corasdca88042016-09-14 16:01:38 +0200824 add_del_ip6_key (sfib, 0 /* vni */ , src_pref, val, is_add);
825 }
826 else
827 {
828 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400829 clib_memset (&sp, 0, sizeof (sp));
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200830 ip_prefix_version (&sp) = IP6;
Florin Corasdca88042016-09-14 16:01:38 +0200831 old_val =
832 add_del_ip6_key (sfib, 0 /* vni */ , &sp, val, is_add);
833 }
834 }
835 }
836 else
837 {
838 if (GID_LOOKUP_MISS != sfi)
839 {
Florin Corasdca88042016-09-14 16:01:38 +0200840 sfib = pool_elt_at_index (db->src_ip6_table_pool, sfi);
841 if (src_pref)
842 old_val = add_del_ip6_key (sfib, 0, src_pref, 0, is_add);
843 else
844 {
845 ip_prefix_t sp;
Dave Barachb7b92992018-10-17 10:38:51 -0400846 clib_memset (&sp, 0, sizeof (sp));
Filip Tehlarf3e3fd32016-09-30 12:47:59 +0200847 ip_prefix_version (&sp) = IP6;
Florin Corasdca88042016-09-14 16:01:38 +0200848 old_val = add_del_ip6_key (sfib, 0, &sp, 0, is_add);
849 }
Filip Tehlarf3fe8202017-02-15 13:27:08 +0100850
851 if (sfib->count == 0)
852 add_del_ip6_key (&db->dst_ip6_table, vni, dst_pref, 0, is_add);
Florin Corasdca88042016-09-14 16:01:38 +0200853 }
854 else
855 clib_warning ("cannot delete dst mapping %U!", format_ip_prefix,
856 dst_pref);
857 }
858 return old_val;
859}
860
861static u32
862add_del_ip (gid_dictionary_t * db, u32 vni, ip_prefix_t * dst_key,
863 ip_prefix_t * src_key, u32 value, u8 is_add)
864{
865 switch (ip_prefix_version (dst_key))
866 {
867 case IP4:
868 return add_del_sd_ip4_key (db, vni, dst_key, src_key, value, is_add);
869 break;
870 case IP6:
871 return add_del_sd_ip6_key (db, vni, dst_key, src_key, value, is_add);
872 break;
873 default:
874 clib_warning ("address type %d not supported!",
875 ip_prefix_version (dst_key));
876 break;
877 }
878 return ~0;
879}
880
881static u32
882add_del_sd (gid_dictionary_t * db, u32 vni, source_dest_t * key, u32 value,
883 u8 is_add)
884{
885 switch (sd_dst_type (key))
886 {
887 case FID_ADDR_IP_PREF:
888 add_del_ip (db, vni, &sd_dst_ippref (key), &sd_src_ippref (key),
889 value, is_add);
890
891 case FID_ADDR_MAC:
892 return add_del_mac (&db->sd_mac_table, vni, sd_dst_mac (key),
893 sd_src_mac (key), value, is_add);
894
895 default:
Paul Vinciguerrabdc0e6b2018-09-22 05:32:50 -0700896 clib_warning ("SD address type %d not supported!", sd_dst_type (key));
Florin Corasdca88042016-09-14 16:01:38 +0200897 break;
898 }
899
900 return ~0;
901}
902
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200903static u64
Filip Tehlar05879992017-09-05 15:46:09 +0200904add_del_arp_ndp (gid_l2_arp_ndp_table_t * db, u32 bd, ip_address_t * key,
905 u64 value, u8 is_add)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200906{
907 BVT (clib_bihash_kv) kv, result;
908 u32 old_val = ~0;
909
Filip Tehlar05879992017-09-05 15:46:09 +0200910 make_arp_ndp_key (&kv, bd, key);
911 if (BV (clib_bihash_search) (&db->arp_ndp_lookup_table, &kv, &result) == 0)
Filip Tehlard5a65db2017-05-17 17:21:10 +0200912 old_val = result.value;
913
914 if (is_add)
915 {
916 kv.value = value;
Filip Tehlar05879992017-09-05 15:46:09 +0200917 BV (clib_bihash_add_del) (&db->arp_ndp_lookup_table, &kv,
918 1 /* is_add */ );
Filip Tehlard5a65db2017-05-17 17:21:10 +0200919 db->count++;
920 }
921 else
922 {
Filip Tehlar05879992017-09-05 15:46:09 +0200923 BV (clib_bihash_add_del) (&db->arp_ndp_lookup_table, &kv,
924 0 /* is_add */ );
Filip Tehlard5a65db2017-05-17 17:21:10 +0200925 db->count--;
926 }
927 return old_val;
928}
929
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200930static u32
931add_del_nsh (gid_nsh_table_t * db, u32 vni, u32 spi, u8 si, u32 value,
932 u8 is_add)
933{
934 BVT (clib_bihash_kv) kv, result;
935 u32 old_val = ~0;
936
937 make_nsh_key (&kv, vni, spi, si);
938 if (BV (clib_bihash_search) (&db->nsh_lookup_table, &kv, &result) == 0)
939 old_val = result.value;
940
941 if (is_add)
942 {
943 kv.value = value;
944 BV (clib_bihash_add_del) (&db->nsh_lookup_table, &kv, 1 /* is_add */ );
945 db->count++;
946 }
947 else
948 {
949 BV (clib_bihash_add_del) (&db->nsh_lookup_table, &kv, 0 /* is_add */ );
950 db->count--;
951 }
952 return old_val;
953}
954
Florin Corasdca88042016-09-14 16:01:38 +0200955u32
Filip Tehlard5a65db2017-05-17 17:21:10 +0200956gid_dictionary_add_del (gid_dictionary_t * db, gid_address_t * key, u64 value,
Florin Corasdca88042016-09-14 16:01:38 +0200957 u8 is_add)
958{
959 switch (gid_address_type (key))
960 {
961 case GID_ADDR_IP_PREFIX:
962 return add_del_ip (db, gid_address_vni (key), &gid_address_ippref (key),
Filip Tehlard5a65db2017-05-17 17:21:10 +0200963 0, (u32) value, is_add);
Florin Corasdca88042016-09-14 16:01:38 +0200964 case GID_ADDR_MAC:
965 return add_del_mac (&db->sd_mac_table, gid_address_vni (key),
Filip Tehlard5a65db2017-05-17 17:21:10 +0200966 gid_address_mac (key), 0, (u32) value, is_add);
Florin Corasdca88042016-09-14 16:01:38 +0200967 case GID_ADDR_SRC_DST:
968 return add_del_sd (db, gid_address_vni (key), &gid_address_sd (key),
Filip Tehlard5a65db2017-05-17 17:21:10 +0200969 (u32) value, is_add);
970 case GID_ADDR_ARP:
Filip Tehlar05879992017-09-05 15:46:09 +0200971 case GID_ADDR_NDP:
972 return add_del_arp_ndp (&db->arp_ndp_table,
973 gid_address_arp_ndp_bd (key),
974 &gid_address_arp_ndp_ip (key), value, is_add);
Filip Tehlaref2a5bf2017-05-30 07:14:46 +0200975 case GID_ADDR_NSH:
976 return add_del_nsh (&db->nsh_table, gid_address_vni (key),
977 gid_address_nsh_spi (key), gid_address_nsh_si (key),
978 value, is_add);
979
Florin Corasdca88042016-09-14 16:01:38 +0200980 default:
981 clib_warning ("address type %d not supported!", gid_address_type (key));
982 break;
983 }
984 return ~0;
985}
986
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200987static void
Florin Corasdca88042016-09-14 16:01:38 +0200988mac_lookup_init (gid_mac_table_t * db)
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200989{
990 if (db->mac_lookup_table_nbuckets == 0)
991 db->mac_lookup_table_nbuckets = MAC_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
992
Florin Corasa2157cf2016-08-16 21:09:14 +0200993 db->mac_lookup_table_nbuckets =
994 1 << max_log2 (db->mac_lookup_table_nbuckets);
Filip Tehlar8e39bb42016-06-24 14:16:34 +0200995
996 if (db->mac_lookup_table_size == 0)
997 db->mac_lookup_table_size = MAC_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
998
Florin Corasa2157cf2016-08-16 21:09:14 +0200999 BV (clib_bihash_init) (&db->mac_lookup_table, "mac lookup table",
1000 db->mac_lookup_table_nbuckets,
1001 db->mac_lookup_table_size);
Filip Tehlar8e39bb42016-06-24 14:16:34 +02001002}
1003
Filip Tehlard5a65db2017-05-17 17:21:10 +02001004static void
Filip Tehlar05879992017-09-05 15:46:09 +02001005arp_ndp_lookup_init (gid_l2_arp_ndp_table_t * db)
Filip Tehlard5a65db2017-05-17 17:21:10 +02001006{
Filip Tehlar05879992017-09-05 15:46:09 +02001007 if (db->arp_ndp_lookup_table_nbuckets == 0)
1008 db->arp_ndp_lookup_table_nbuckets =
1009 ARP_NDP_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
Filip Tehlard5a65db2017-05-17 17:21:10 +02001010
Filip Tehlar05879992017-09-05 15:46:09 +02001011 db->arp_ndp_lookup_table_nbuckets =
1012 1 << max_log2 (db->arp_ndp_lookup_table_nbuckets);
Filip Tehlard5a65db2017-05-17 17:21:10 +02001013
Filip Tehlar05879992017-09-05 15:46:09 +02001014 if (db->arp_ndp_lookup_table_size == 0)
1015 db->arp_ndp_lookup_table_size = ARP_NDP_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
Filip Tehlard5a65db2017-05-17 17:21:10 +02001016
Filip Tehlar05879992017-09-05 15:46:09 +02001017 BV (clib_bihash_init) (&db->arp_ndp_lookup_table, "arp ndp lookup table",
1018 db->arp_ndp_lookup_table_nbuckets,
1019 db->arp_ndp_lookup_table_size);
Filip Tehlard5a65db2017-05-17 17:21:10 +02001020}
1021
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001022static void
1023nsh_lookup_init (gid_nsh_table_t * db)
1024{
1025 if (db->nsh_lookup_table_nbuckets == 0)
1026 db->nsh_lookup_table_nbuckets = MAC_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
1027
1028 db->nsh_lookup_table_nbuckets =
1029 1 << max_log2 (db->nsh_lookup_table_nbuckets);
1030
1031 if (db->nsh_lookup_table_size == 0)
1032 db->nsh_lookup_table_size = MAC_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
1033
1034 BV (clib_bihash_init) (&db->nsh_lookup_table, "nsh lookup table",
1035 db->nsh_lookup_table_nbuckets,
1036 db->nsh_lookup_table_size);
1037}
1038
Florin Corase127a7e2016-02-18 22:20:01 +01001039void
1040gid_dictionary_init (gid_dictionary_t * db)
1041{
Florin Corasdca88042016-09-14 16:01:38 +02001042 ip4_lookup_init (&db->dst_ip4_table);
1043 ip6_lookup_init (&db->dst_ip6_table);
1044 mac_lookup_init (&db->sd_mac_table);
Filip Tehlar05879992017-09-05 15:46:09 +02001045 arp_ndp_lookup_init (&db->arp_ndp_table);
Filip Tehlaref2a5bf2017-05-30 07:14:46 +02001046 nsh_lookup_init (&db->nsh_table);
Florin Corase127a7e2016-02-18 22:20:01 +01001047}
1048
Florin Corasa2157cf2016-08-16 21:09:14 +02001049/*
1050 * fd.io coding-style-patch-verification: ON
1051 *
1052 * Local Variables:
1053 * eval: (c-set-style "gnu")
1054 * End:
1055 */