summaryrefslogtreecommitdiffstats
path: root/vnet/vnet/lisp-cp/gid_dictionary.c
blob: 1beac54a5d82819626a65e9b5a39682164840ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <vnet/lisp-cp/gid_dictionary.h>

static u32
mac_lookup (gid_dictionary_t * db, u32 vni, u8 * key)
{
  int rv;
  BVT(clib_bihash_kv) kv, value;

  kv.key[0] = ((u64 *)key)[0];
  kv.key[1] = (u64)vni;
  kv.key[2] = 0;

  rv = BV(clib_bihash_search_inline_2)(&db->mac_lookup_table, &kv, &value);
  if (rv == 0)
    return value.value;

  return GID_LOOKUP_MISS;
}

static u32
ip4_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t *key)
{
  int i, len;
  int rv;
  BVT(clib_bihash_kv) kv, value;

  len = vec_len (db->ip4_prefix_lengths_in_search_order);

  for (i = 0; i < len; i++)
    {
      int dst_address_length = db->ip4_prefix_lengths_in_search_order[i];
      ip4_address_t * mask;

      ASSERT(dst_address_length >= 0 && dst_address_length <= 32);

      mask = &db->ip4_fib_masks[dst_address_length];

      kv.key[0] = ((u64) vni << 32) | (ip_prefix_v4(key).as_u32 & mask->as_u32);
      kv.key[1] = 0;
      kv.key[2] = 0;

      rv = BV(clib_bihash_search_inline_2)(&db->ip4_lookup_table, &kv, &value);
      if (rv == 0)
        return value.value;
    }

  return GID_LOOKUP_MISS;
}

static u32
ip6_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t *key)
{
  int i, len;
  int rv;
  BVT(clib_bihash_kv) kv, value;

  len = vec_len (db->ip6_prefix_lengths_in_search_order);

  for (i = 0; i < len; i++)
    {
      int dst_address_length = db->ip6_prefix_lengths_in_search_order[i];
      ip6_address_t * mask;

      ASSERT(dst_address_length >= 0 && dst_address_length <= 128);

      mask = &db->ip6_fib_masks[dst_address_length];

      kv.key[0] = ip_prefix_v6(key).as_u64[0] & mask->as_u64[0];
      kv.key[1] = ip_prefix_v6(key).as_u64[1] & mask->as_u64[1];
      kv.key[2] = (u64)vni;

      rv = BV(clib_bihash_search_inline_2)(&db->ip6_lookup_table, &kv, &value);
      if (rv == 0)
        return value.value;
    }

  return GID_LOOKUP_MISS;
}

static u32
ip_lookup (gid_dictionary_t * db, u32 vni, ip_prefix_t *key)
{
  /* XXX for now this only works with ip-prefixes, no lcafs */
  switch (ip_prefix_version (key))
    {
    case IP4:
      return ip4_lookup (db, vni, key);
      break;
    case IP6:
      return ip6_lookup (db, vni, key);
      break;
    default:
      clib_warning ("address type %d not supported!", ip_prefix_version(key));
      break;
    }
  return ~0;
}

u32
gid_dictionary_lookup (gid_dictionary_t * db, gid_address_t * key)
{
  /* XXX for now this only works with ip-prefixes, no lcafs */
  switch (gid_address_type (key))
    {
    case GID_ADDR_IP_PREFIX:
      return ip_lookup (db, gid_address_vni(key), &gid_address_ippref(key));
    case GID_ADDR_MAC:
      return mac_lookup (db, gid_address_vni(key), gid_address_mac(key));
    default:
      clib_warning ("address type %d not supported!", gid_address_type(key));
      break;
    }
  return ~0;
}

static void
ip4_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
{
  int i;
  vec_reset_length (db->ip4_prefix_lengths_in_search_order);
  /* Note: bitmap reversed so this is in fact a longest prefix match */
  clib_bitmap_foreach (i, db->ip4_non_empty_dst_address_length_bitmap,
  ({
    int dst_address_length = 32 - i;
    vec_add1 (db->ip4_prefix_lengths_in_search_order, dst_address_length);
  }));
}

static u32
add_del_ip4_key (gid_dictionary_t *db, u32 vni, ip_prefix_t * pref, u32 val,
                 u8 is_add)
{
  BVT(clib_bihash_kv) kv, value;
  u32 old_val = ~0;
  ip4_address_t key;
  u8 plen = ip_prefix_len (pref);

  clib_memcpy (&key, &ip_prefix_v4(pref), sizeof(key));
  key.as_u32 &= db->ip4_fib_masks[plen].as_u32;
  if (is_add)
    {
      db->ip4_non_empty_dst_address_length_bitmap = clib_bitmap_set (
          db->ip4_non_empty_dst_address_length_bitmap, 32 - plen,
          1);
      ip4_compute_prefix_lengths_in_search_order (db);

      db->ip4_prefix_len_refcount[plen]++;
    }
  else
    {
      ASSERT(db->ip4_prefix_len_refcount[plen] != 0);

      db->ip4_prefix_len_refcount[plen]--;

      if (db->ip4_prefix_len_refcount[plen] == 0)
        {
            db->ip4_non_empty_dst_address_length_bitmap = clib_bitmap_set (
                db->ip4_non_empty_dst_address_length_bitmap, 32 - plen,
                0);
            ip4_compute_prefix_lengths_in_search_order (db);
        }
    }

  kv.key[0] = ((u64) vni << 32) | key.as_u32;
  kv.key[1] = 0;
  kv.key[2] = 0;

  if (BV(clib_bihash_search)(&db->ip4_lookup_table, &kv, &value) == 0)
    old_val = value.value;

  if (!is_add)
    BV(clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 0 /* is_add */);
  else
    {
      kv.value = val;
      BV(clib_bihash_add_del) (&db->ip4_lookup_table, &kv, 1 /* is_add */);
    }
  return old_val;
}

static void
ip6_compute_prefix_lengths_in_search_order (gid_dictionary_t * db)
{
  int i;
  vec_reset_length (db->ip6_prefix_lengths_in_search_order);
  /* Note: bitmap reversed so this is in fact a longest prefix match */
  clib_bitmap_foreach (i, db->ip6_non_empty_dst_address_length_bitmap,
  ({
    int dst_address_length = 128 - i;
    vec_add1 (db->ip6_prefix_lengths_in_search_order, dst_address_length);
  }));
}

static u32
add_del_ip6_key (gid_dictionary_t *db, u32 vni, ip_prefix_t *pref, u32 val,
                 u8 is_add)
{
  BVT(clib_bihash_kv) kv, value;
  u32 old_val = ~0;
  ip6_address_t key;
  u8 plen = ip_prefix_len (pref);

  clib_memcpy (&key, &ip_prefix_v6(pref), sizeof(key));
  ip6_address_mask (&key, &db->ip6_fib_masks[plen]);
  if (is_add)
    {
      db->ip6_non_empty_dst_address_length_bitmap = clib_bitmap_set (
          db->ip6_non_empty_dst_address_length_bitmap, 128 - plen, 1);
      ip6_compute_prefix_lengths_in_search_order (db);
      db->ip6_prefix_len_refcount[plen]++;
    }
  else
    {
      ASSERT(db->ip6_prefix_len_refcount[plen] != 0);

      db->ip6_prefix_len_refcount[plen]--;

      if (db->ip6_prefix_len_refcount[plen] == 0)
        {
          db->ip6_non_empty_dst_address_length_bitmap = clib_bitmap_set (
              db->ip6_non_empty_dst_address_length_bitmap, 128 - plen, 0);
          ip6_compute_prefix_lengths_in_search_order (db);
        }
    }

  kv.key[0] = key.as_u64[0];
  kv.key[1] = key.as_u64[1];
  kv.key[2] = (u64) vni;
//  kv.key[2] = ((u64)((fib - im->fibs))<<32) | ip_prefix_len(key);

  if (BV(clib_bihash_search)(&db->ip6_lookup_table, &kv, &value) == 0)
    old_val = value.value;

  if (!is_add)
    BV(clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 0 /* is_add */);
  else
    {
      kv.value = val;
      BV(clib_bihash_add_del) (&db->ip6_lookup_table, &kv, 1 /* is_add */);
    }
  return old_val;
}

static u32
add_del_mac (gid_dictionary_t * db, u32 vni, u8 * mac, u32 val, u8 is_add)
{
  BVT(clib_bihash_kv) kv, value;
  u32 old_val = ~0;

  kv.key[0] = ((u64 *)mac)[0];
  kv.key[1] = (u64)vni;
  kv.key[2] = 0;

  if (BV(clib_bihash_search) (&db->mac_lookup_table, &kv, &value) == 0)
    old_val = value.value;

  if (!is_add)
    BV(clib_bihash_add_del) (&db->mac_lookup_table, &kv, 0 /* is_add */);
  else
    {
      kv.value = val;
      BV(clib_bihash_add_del) (&db->mac_lookup_table, &kv, 1 /* is_add */);
    }
  return old_val;
}

static u32
add_del_ip (gid_dictionary_t *db, u32 vni, ip_prefix_t *key, u32 value,
            u8 is_add)
{
  switch (ip_prefix_version(key))
    {
    case IP4:
      return add_del_ip4_key (db, vni, key, value, is_add);
      break;
    case IP6:
      return add_del_ip6_key (db, vni, key, value, is_add);
      break;
    default:
      clib_warning("address type %d not supported!", ip_prefix_version (key));
      break;
    }
  return ~0;
}

u32
gid_dictionary_add_del (gid_dictionary_t *db, gid_address_t *key, u32 value,
                        u8 is_add)
{
  /* XXX for now this only works with ip-prefixes, no lcafs */
  switch (gid_address_type (key))
    {
    case GID_ADDR_IP_PREFIX:
      return add_del_ip (db, gid_address_vni(key), &gid_address_ippref(key),
                         value, is_add);
    case GID_ADDR_MAC:
      return add_del_mac (db, gid_address_vni(key), gid_address_mac(key),
                          value, is_add);
    default:
      clib_warning ("address type %d not supported!", gid_address_type (key));
      break;
    }
  return ~0;
}

static void
ip4_lookup_init (gid_dictionary_t * db)
{
  uword i;

  memset(db->ip4_prefix_len_refcount, 0, sizeof(db->ip4_prefix_len_refcount));

  for (i = 0; i < ARRAY_LEN (db->ip4_fib_masks); i++)
    {
      u32 m;

      if (i < 32)
        m = pow2_mask (i) << (32 - i);
      else
        m = ~0;
      db->ip4_fib_masks[i].as_u32 = clib_host_to_net_u32 (m);
    }
  if (db->ip4_lookup_table_nbuckets == 0)
    db->ip4_lookup_table_nbuckets = IP4_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;

  db->ip4_lookup_table_nbuckets = 1 << max_log2 (db->ip4_lookup_table_nbuckets);

  if (db->ip4_lookup_table_size == 0)
    db->ip4_lookup_table_size = IP4_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;

  BV(clib_bihash_init) (&db->ip4_lookup_table, "ip4 lookup table",
                         db->ip4_lookup_table_nbuckets, db->ip4_lookup_table_size);
}

static void
ip6_lookup_init (gid_dictionary_t * db)
{
  uword i;

  memset(db->ip6_prefix_len_refcount, 0, sizeof(db->ip6_prefix_len_refcount));

  for (i = 0; i < ARRAY_LEN(db->ip6_fib_masks); i++)
    {
      u32 j, i0, i1;

      i0 = i / 32;
      i1 = i % 32;

      for (j = 0; j < i0; j++)
        db->ip6_fib_masks[i].as_u32[j] = ~0;

      if (i1)
        db->ip6_fib_masks[i].as_u32[i0] = clib_host_to_net_u32 (
            pow2_mask (i1) << (32 - i1));
    }

  if (db->ip6_lookup_table_nbuckets == 0)
    db->ip6_lookup_table_nbuckets = IP6_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;

  db->ip6_lookup_table_nbuckets = 1 << max_log2 (db->ip6_lookup_table_nbuckets);

  if (db->ip6_lookup_table_size == 0)
    db->ip6_lookup_table_size = IP6_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;

  BV(clib_bihash_init) (&db->ip6_lookup_table, "ip6 lookup table",
                         db->ip6_lookup_table_nbuckets, db->ip6_lookup_table_size);
}

static void
mac_lookup_init (gid_dictionary_t * db)
{
  if (db->mac_lookup_table_nbuckets == 0)
    db->mac_lookup_table_nbuckets = MAC_LOOKUP_DEFAULT_HASH_NUM_BUCKETS;

  db->mac_lookup_table_nbuckets = 1 << max_log2 (db->mac_lookup_table_nbuckets);

  if (db->mac_lookup_table_size == 0)
    db->mac_lookup_table_size = MAC_LOOKUP_DEFAULT_HASH_MEMORY_SIZE;

  BV(clib_bihash_init) (&db->mac_lookup_table, "mac lookup table",
                        db->mac_lookup_table_nbuckets,
                        db->mac_lookup_table_size);
}

void
gid_dictionary_init (gid_dictionary_t * db)
{
  ip4_lookup_init (db);
  ip6_lookup_init (db);
  mac_lookup_init (db);
}