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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vnet/lisp-cp/gid_dictionary.h>
19 mac_lookup (gid_dictionary_t * db, u32 vni, u8 * key)
22 BVT (clib_bihash_kv) kv, value;
24 kv.key[0] = mac_to_u64 (key);
25 kv.key[1] = (u64) vni;
28 rv = BV (clib_bihash_search_inline_2) (&db->mac_lookup_table, &kv, &value);
32 return GID_LOOKUP_MISS;
36 ip4_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t * key)
40 BVT (clib_bihash_kv) kv, value;
42 len = vec_len (db->ip4_prefix_lengths_in_search_order);
44 for (i = 0; i < len; i++)
46 int dst_address_length = db->ip4_prefix_lengths_in_search_order[i];
49 ASSERT (dst_address_length >= 0 && dst_address_length <= 32);
51 mask = &db->ip4_fib_masks[dst_address_length];
54 ((u64) vni << 32) | (ip_prefix_v4 (key).as_u32 & mask->as_u32);
59 BV (clib_bihash_search_inline_2) (&db->ip4_lookup_table, &kv, &value);
64 return GID_LOOKUP_MISS;
68 ip6_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t * key)
72 BVT (clib_bihash_kv) kv, value;
74 len = vec_len (db->ip6_prefix_lengths_in_search_order);
76 for (i = 0; i < len; i++)
78 int dst_address_length = db->ip6_prefix_lengths_in_search_order[i];
81 ASSERT (dst_address_length >= 0 && dst_address_length <= 128);
83 mask = &db->ip6_fib_masks[dst_address_length];
85 kv.key[0] = ip_prefix_v6 (key).as_u64[0] & mask->as_u64[0];
86 kv.key[1] = ip_prefix_v6 (key).as_u64[1] & mask->as_u64[1];
87 kv.key[2] = (u64) vni;
90 BV (clib_bihash_search_inline_2) (&db->ip6_lookup_table, &kv, &value);
95 return GID_LOOKUP_MISS;
99 ip_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t * key)
101 /* XXX for now this only works with ip-prefixes, no lcafs */
102 switch (ip_prefix_version (key))
105 return ip4_lookup (db, vni, key);
108 return ip6_lookup (db, vni, key);
111 clib_warning ("address type %d not supported!",
112 ip_prefix_version (key));
119 gid_dictionary_lookup (gid_dictionary_t * db, gid_address_t * key)
121 /* XXX for now this only works with ip-prefixes, no lcafs */
122 switch (gid_address_type (key))
124 case GID_ADDR_IP_PREFIX:
125 return ip_lookup (db, gid_address_vni (key), &gid_address_ippref (key));
127 return mac_lookup (db, gid_address_vni (key), gid_address_mac (key));
129 clib_warning ("address type %d not supported!", gid_address_type (key));
132 return GID_LOOKUP_MISS;
136 ip4_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
139 vec_reset_length (db->ip4_prefix_lengths_in_search_order);
140 /* Note: bitmap reversed so this is in fact a longest prefix match */
143 clib_bitmap_foreach (i, db->ip4_non_empty_dst_address_length_bitmap,
145 int dst_address_length = 32 - i;
146 vec_add1 (db->ip4_prefix_lengths_in_search_order, dst_address_length);
153 add_del_ip4_key (gid_dictionary_t * db, u32 vni, ip_prefix_t * pref, u32 val,
156 BVT (clib_bihash_kv) kv, value;
159 u8 plen = ip_prefix_len (pref);
161 clib_memcpy (&key, &ip_prefix_v4 (pref), sizeof (key));
162 key.as_u32 &= db->ip4_fib_masks[plen].as_u32;
165 db->ip4_non_empty_dst_address_length_bitmap =
166 clib_bitmap_set (db->ip4_non_empty_dst_address_length_bitmap,
168 ip4_compute_prefix_lengths_in_search_order (db);
170 db->ip4_prefix_len_refcount[plen]++;
174 ASSERT (db->ip4_prefix_len_refcount[plen] != 0);
176 db->ip4_prefix_len_refcount[plen]--;
178 if (db->ip4_prefix_len_refcount[plen] == 0)
180 db->ip4_non_empty_dst_address_length_bitmap =
181 clib_bitmap_set (db->ip4_non_empty_dst_address_length_bitmap,
183 ip4_compute_prefix_lengths_in_search_order (db);
187 kv.key[0] = ((u64) vni << 32) | key.as_u32;
191 if (BV (clib_bihash_search) (&db->ip4_lookup_table, &kv, &value) == 0)
192 old_val = value.value;
195 BV (clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 0 /* is_add */ );
199 BV (clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 1 /* is_add */ );
205 ip6_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
208 vec_reset_length (db->ip6_prefix_lengths_in_search_order);
209 /* Note: bitmap reversed so this is in fact a longest prefix match */
212 clib_bitmap_foreach (i, db->ip6_non_empty_dst_address_length_bitmap,
214 int dst_address_length = 128 - i;
215 vec_add1 (db->ip6_prefix_lengths_in_search_order, dst_address_length);
221 add_del_ip6_key (gid_dictionary_t * db, u32 vni, ip_prefix_t * pref, u32 val,
224 BVT (clib_bihash_kv) kv, value;
227 u8 plen = ip_prefix_len (pref);
229 clib_memcpy (&key, &ip_prefix_v6 (pref), sizeof (key));
230 ip6_address_mask (&key, &db->ip6_fib_masks[plen]);
233 db->ip6_non_empty_dst_address_length_bitmap =
234 clib_bitmap_set (db->ip6_non_empty_dst_address_length_bitmap,
236 ip6_compute_prefix_lengths_in_search_order (db);
237 db->ip6_prefix_len_refcount[plen]++;
241 ASSERT (db->ip6_prefix_len_refcount[plen] != 0);
243 db->ip6_prefix_len_refcount[plen]--;
245 if (db->ip6_prefix_len_refcount[plen] == 0)
247 db->ip6_non_empty_dst_address_length_bitmap =
248 clib_bitmap_set (db->ip6_non_empty_dst_address_length_bitmap,
250 ip6_compute_prefix_lengths_in_search_order (db);
254 kv.key[0] = key.as_u64[0];
255 kv.key[1] = key.as_u64[1];
256 kv.key[2] = (u64) vni;
257 // kv.key[2] = ((u64)((fib - im->fibs))<<32) | ip_prefix_len(key);
259 if (BV (clib_bihash_search) (&db->ip6_lookup_table, &kv, &value) == 0)
260 old_val = value.value;
263 BV (clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 0 /* is_add */ );
267 BV (clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 1 /* is_add */ );
273 add_del_mac (gid_dictionary_t * db, u32 vni, u8 * mac, u32 val, u8 is_add)
275 BVT (clib_bihash_kv) kv, value;
278 kv.key[0] = ((u64 *) mac)[0];
279 kv.key[1] = (u64) vni;
282 if (BV (clib_bihash_search) (&db->mac_lookup_table, &kv, &value) == 0)
283 old_val = value.value;
286 BV (clib_bihash_add_del) (&db->mac_lookup_table, &kv, 0 /* is_add */ );
290 BV (clib_bihash_add_del) (&db->mac_lookup_table, &kv, 1 /* is_add */ );
296 add_del_ip (gid_dictionary_t * db, u32 vni, ip_prefix_t * key, u32 value,
299 switch (ip_prefix_version (key))
302 return add_del_ip4_key (db, vni, key, value, is_add);
305 return add_del_ip6_key (db, vni, key, value, is_add);
308 clib_warning ("address type %d not supported!",
309 ip_prefix_version (key));
316 gid_dictionary_add_del (gid_dictionary_t * db, gid_address_t * key, u32 value,
319 /* XXX for now this only works with ip-prefixes, no lcafs */
320 switch (gid_address_type (key))
322 case GID_ADDR_IP_PREFIX:
323 return add_del_ip (db, gid_address_vni (key), &gid_address_ippref (key),
326 return add_del_mac (db, gid_address_vni (key), gid_address_mac (key),
329 clib_warning ("address type %d not supported!", gid_address_type (key));
336 ip4_lookup_init (gid_dictionary_t * db)
340 memset (db->ip4_prefix_len_refcount, 0,
341 sizeof (db->ip4_prefix_len_refcount));
343 for (i = 0; i < ARRAY_LEN (db->ip4_fib_masks); i++)
348 m = pow2_mask (i) << (32 - i);
351 db->ip4_fib_masks[i].as_u32 = clib_host_to_net_u32 (m);
353 if (db->ip4_lookup_table_nbuckets == 0)
354 db->ip4_lookup_table_nbuckets = IP4_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
356 db->ip4_lookup_table_nbuckets =
357 1 << max_log2 (db->ip4_lookup_table_nbuckets);
359 if (db->ip4_lookup_table_size == 0)
360 db->ip4_lookup_table_size = IP4_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
362 BV (clib_bihash_init) (&db->ip4_lookup_table, "ip4 lookup table",
363 db->ip4_lookup_table_nbuckets,
364 db->ip4_lookup_table_size);
368 ip6_lookup_init (gid_dictionary_t * db)
372 memset (db->ip6_prefix_len_refcount, 0,
373 sizeof (db->ip6_prefix_len_refcount));
375 for (i = 0; i < ARRAY_LEN (db->ip6_fib_masks); i++)
382 for (j = 0; j < i0; j++)
383 db->ip6_fib_masks[i].as_u32[j] = ~0;
386 db->ip6_fib_masks[i].as_u32[i0] =
387 clib_host_to_net_u32 (pow2_mask (i1) << (32 - i1));
390 if (db->ip6_lookup_table_nbuckets == 0)
391 db->ip6_lookup_table_nbuckets = IP6_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
393 db->ip6_lookup_table_nbuckets =
394 1 << max_log2 (db->ip6_lookup_table_nbuckets);
396 if (db->ip6_lookup_table_size == 0)
397 db->ip6_lookup_table_size = IP6_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
399 BV (clib_bihash_init) (&db->ip6_lookup_table, "ip6 lookup table",
400 db->ip6_lookup_table_nbuckets,
401 db->ip6_lookup_table_size);
405 mac_lookup_init (gid_dictionary_t * db)
407 if (db->mac_lookup_table_nbuckets == 0)
408 db->mac_lookup_table_nbuckets = MAC_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
410 db->mac_lookup_table_nbuckets =
411 1 << max_log2 (db->mac_lookup_table_nbuckets);
413 if (db->mac_lookup_table_size == 0)
414 db->mac_lookup_table_size = MAC_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
416 BV (clib_bihash_init) (&db->mac_lookup_table, "mac lookup table",
417 db->mac_lookup_table_nbuckets,
418 db->mac_lookup_table_size);
422 gid_dictionary_init (gid_dictionary_t * db)
424 ip4_lookup_init (db);
425 ip6_lookup_init (db);
426 mac_lookup_init (db);
430 * fd.io coding-style-patch-verification: ON
433 * eval: (c-set-style "gnu")