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] = ((u64 *)key)[0];
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];
53 kv.key[0] = ((u64) vni << 32) | (ip_prefix_v4(key).as_u32 & mask->as_u32);
57 rv = BV(clib_bihash_search_inline_2)(&db->ip4_lookup_table, &kv, &value);
62 return GID_LOOKUP_MISS;
66 ip6_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t *key)
70 BVT(clib_bihash_kv) kv, value;
72 len = vec_len (db->ip6_prefix_lengths_in_search_order);
74 for (i = 0; i < len; i++)
76 int dst_address_length = db->ip6_prefix_lengths_in_search_order[i];
79 ASSERT(dst_address_length >= 0 && dst_address_length <= 128);
81 mask = &db->ip6_fib_masks[dst_address_length];
83 kv.key[0] = ip_prefix_v6(key).as_u64[0] & mask->as_u64[0];
84 kv.key[1] = ip_prefix_v6(key).as_u64[1] & mask->as_u64[1];
87 rv = BV(clib_bihash_search_inline_2)(&db->ip6_lookup_table, &kv, &value);
92 return GID_LOOKUP_MISS;
96 ip_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t *key)
98 /* XXX for now this only works with ip-prefixes, no lcafs */
99 switch (ip_prefix_version (key))
102 return ip4_lookup (db, vni, key);
105 return ip6_lookup (db, vni, key);
108 clib_warning ("address type %d not supported!", ip_prefix_version(key));
115 gid_dictionary_lookup (gid_dictionary_t * db, gid_address_t * key)
117 /* XXX for now this only works with ip-prefixes, no lcafs */
118 switch (gid_address_type (key))
120 case GID_ADDR_IP_PREFIX:
121 return ip_lookup (db, gid_address_vni(key), &gid_address_ippref(key));
123 return mac_lookup (db, gid_address_vni(key), gid_address_mac(key));
125 clib_warning ("address type %d not supported!", gid_address_type(key));
132 ip4_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
135 vec_reset_length (db->ip4_prefix_lengths_in_search_order);
136 /* Note: bitmap reversed so this is in fact a longest prefix match */
137 clib_bitmap_foreach (i, db->ip4_non_empty_dst_address_length_bitmap,
139 int dst_address_length = 32 - i;
140 vec_add1 (db->ip4_prefix_lengths_in_search_order, dst_address_length);
145 add_del_ip4_key (gid_dictionary_t *db, u32 vni, ip_prefix_t * pref, u32 val,
148 BVT(clib_bihash_kv) kv, value;
151 u8 plen = ip_prefix_len (pref);
153 clib_memcpy (&key, &ip_prefix_v4(pref), sizeof(key));
154 key.as_u32 &= db->ip4_fib_masks[plen].as_u32;
157 db->ip4_non_empty_dst_address_length_bitmap = clib_bitmap_set (
158 db->ip4_non_empty_dst_address_length_bitmap, 32 - plen,
160 ip4_compute_prefix_lengths_in_search_order (db);
162 db->ip4_prefix_len_refcount[plen]++;
166 ASSERT(db->ip4_prefix_len_refcount[plen] != 0);
168 db->ip4_prefix_len_refcount[plen]--;
170 if (db->ip4_prefix_len_refcount[plen] == 0)
172 db->ip4_non_empty_dst_address_length_bitmap = clib_bitmap_set (
173 db->ip4_non_empty_dst_address_length_bitmap, 32 - plen,
175 ip4_compute_prefix_lengths_in_search_order (db);
179 kv.key[0] = ((u64) vni << 32) | key.as_u32;
183 if (BV(clib_bihash_search)(&db->ip4_lookup_table, &kv, &value) == 0)
184 old_val = value.value;
187 BV(clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 0 /* is_add */);
191 BV(clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 1 /* is_add */);
197 ip6_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
200 vec_reset_length (db->ip6_prefix_lengths_in_search_order);
201 /* Note: bitmap reversed so this is in fact a longest prefix match */
202 clib_bitmap_foreach (i, db->ip6_non_empty_dst_address_length_bitmap,
204 int dst_address_length = 128 - i;
205 vec_add1 (db->ip6_prefix_lengths_in_search_order, dst_address_length);
210 add_del_ip6_key (gid_dictionary_t *db, u32 vni, ip_prefix_t *pref, u32 val,
213 BVT(clib_bihash_kv) kv, value;
216 u8 plen = ip_prefix_len (pref);
218 clib_memcpy (&key, &ip_prefix_v6(pref), sizeof(key));
219 ip6_address_mask (&key, &db->ip6_fib_masks[plen]);
222 db->ip6_non_empty_dst_address_length_bitmap = clib_bitmap_set (
223 db->ip6_non_empty_dst_address_length_bitmap, 128 - plen, 1);
224 ip6_compute_prefix_lengths_in_search_order (db);
225 db->ip6_prefix_len_refcount[plen]++;
229 ASSERT(db->ip6_prefix_len_refcount[plen] != 0);
231 db->ip6_prefix_len_refcount[plen]--;
233 if (db->ip6_prefix_len_refcount[plen] == 0)
235 db->ip6_non_empty_dst_address_length_bitmap = clib_bitmap_set (
236 db->ip6_non_empty_dst_address_length_bitmap, 128 - plen, 0);
237 ip6_compute_prefix_lengths_in_search_order (db);
241 kv.key[0] = key.as_u64[0];
242 kv.key[1] = key.as_u64[1];
243 kv.key[2] = (u64) vni;
244 // kv.key[2] = ((u64)((fib - im->fibs))<<32) | ip_prefix_len(key);
246 if (BV(clib_bihash_search)(&db->ip6_lookup_table, &kv, &value) == 0)
247 old_val = value.value;
250 BV(clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 0 /* is_add */);
254 BV(clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 1 /* is_add */);
260 add_del_mac (gid_dictionary_t * db, u32 vni, u8 * mac, u32 val, u8 is_add)
262 BVT(clib_bihash_kv) kv, value;
265 kv.key[0] = ((u64 *)mac)[0];
266 kv.key[1] = (u64)vni;
269 if (BV(clib_bihash_search) (&db->mac_lookup_table, &kv, &value) == 0)
270 old_val = value.value;
273 BV(clib_bihash_add_del) (&db->mac_lookup_table, &kv, 0 /* is_add */);
277 BV(clib_bihash_add_del) (&db->mac_lookup_table, &kv, 1 /* is_add */);
283 add_del_ip (gid_dictionary_t *db, u32 vni, ip_prefix_t *key, u32 value,
286 switch (ip_prefix_version(key))
289 return add_del_ip4_key (db, vni, key, value, is_add);
292 return add_del_ip6_key (db, vni, key, value, is_add);
295 clib_warning("address type %d not supported!", ip_prefix_version (key));
302 gid_dictionary_add_del (gid_dictionary_t *db, gid_address_t *key, u32 value,
305 /* XXX for now this only works with ip-prefixes, no lcafs */
306 switch (gid_address_type (key))
308 case GID_ADDR_IP_PREFIX:
309 return add_del_ip (db, gid_address_vni(key), &gid_address_ippref(key),
312 return add_del_mac (db, gid_address_vni(key), gid_address_mac(key),
315 clib_warning ("address type %d not supported!", gid_address_type (key));
322 ip4_lookup_init (gid_dictionary_t * db)
326 memset(db->ip4_prefix_len_refcount, 0, sizeof(db->ip4_prefix_len_refcount));
328 for (i = 0; i < ARRAY_LEN (db->ip4_fib_masks); i++)
333 m = pow2_mask (i) << (32 - i);
336 db->ip4_fib_masks[i].as_u32 = clib_host_to_net_u32 (m);
338 if (db->ip4_lookup_table_nbuckets == 0)
339 db->ip4_lookup_table_nbuckets = IP4_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
341 db->ip4_lookup_table_nbuckets = 1 << max_log2 (db->ip4_lookup_table_nbuckets);
343 if (db->ip4_lookup_table_size == 0)
344 db->ip4_lookup_table_size = IP4_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
346 BV(clib_bihash_init) (&db->ip4_lookup_table, "ip4 lookup table",
347 db->ip4_lookup_table_nbuckets, db->ip4_lookup_table_size);
351 ip6_lookup_init (gid_dictionary_t * db)
355 memset(db->ip6_prefix_len_refcount, 0, sizeof(db->ip6_prefix_len_refcount));
357 for (i = 0; i < ARRAY_LEN(db->ip6_fib_masks); i++)
364 for (j = 0; j < i0; j++)
365 db->ip6_fib_masks[i].as_u32[j] = ~0;
368 db->ip6_fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (
369 pow2_mask (i1) << (32 - i1));
372 if (db->ip6_lookup_table_nbuckets == 0)
373 db->ip6_lookup_table_nbuckets = IP6_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
375 db->ip6_lookup_table_nbuckets = 1 << max_log2 (db->ip6_lookup_table_nbuckets);
377 if (db->ip6_lookup_table_size == 0)
378 db->ip6_lookup_table_size = IP6_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
380 BV(clib_bihash_init) (&db->ip6_lookup_table, "ip6 lookup table",
381 db->ip6_lookup_table_nbuckets, db->ip6_lookup_table_size);
385 mac_lookup_init (gid_dictionary_t * db)
387 if (db->mac_lookup_table_nbuckets == 0)
388 db->mac_lookup_table_nbuckets = MAC_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;
390 db->mac_lookup_table_nbuckets = 1 << max_log2 (db->mac_lookup_table_nbuckets);
392 if (db->mac_lookup_table_size == 0)
393 db->mac_lookup_table_size = MAC_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;
395 BV(clib_bihash_init) (&db->mac_lookup_table, "mac lookup table",
396 db->mac_lookup_table_nbuckets,
397 db->mac_lookup_table_size);
401 gid_dictionary_init (gid_dictionary_t * db)
403 ip4_lookup_init (db);
404 ip6_lookup_init (db);
405 mac_lookup_init (db);