Statistics
| Branch: | Revision:

iof-bird-daemon / nest / rt-attr.c @ d14f8c3c

History | View | Annotate | Download (28.9 KB)

1 2326b001 Martin Mares
/*
2
 *        BIRD -- Route Attribute Cache
3
 *
4 ee76a92a Martin Mares
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 2326b001 Martin Mares
 *
6
 *        Can be freely distributed and used under the terms of the GNU GPL.
7
 */
8
9 3ce8c610 Martin Mares
/**
10
 * DOC: Route attribute cache
11
 *
12
 * Each route entry carries a set of route attributes. Several of them
13
 * vary from route to route, but most attributes are usually common
14
 * for a large number of routes. To conserve memory, we've decided to
15
 * store only the varying ones directly in the &rte and hold the rest
16
 * in a special structure called &rta which is shared among all the
17
 * &rte's with these attributes.
18
 *
19
 * Each &rta contains all the static attributes of the route (i.e.,
20
 * those which are always present) as structure members and a list of
21
 * dynamic attributes represented by a linked list of &ea_list
22
 * structures, each of them consisting of an array of &eattr's containing
23
 * the individual attributes. An attribute can be specified more than once
24 2e9b2421 Martin Mares
 * in the &ea_list chain and in such case the first occurrence overrides
25 3ce8c610 Martin Mares
 * the others. This semantics is used especially when someone (for example
26
 * a filter) wishes to alter values of several dynamic attributes, but
27
 * it wants to preserve the original attribute lists maintained by
28
 * another module.
29
 *
30
 * Each &eattr contains an attribute identifier (split to protocol ID and
31
 * per-protocol attribute ID), protocol dependent flags, a type code (consisting
32
 * of several bit fields describing attribute characteristics) and either an
33
 * embedded 32-bit value or a pointer to a &adata structure holding attribute
34
 * contents.
35
 *
36 2e9b2421 Martin Mares
 * There exist two variants of &rta's -- cached and un-cached ones. Un-cached
37 3ce8c610 Martin Mares
 * &rta's can have arbitrarily complex structure of &ea_list's and they
38
 * can be modified by any module in the route processing chain. Cached
39
 * &rta's have their attribute lists normalized (that means at most one
40
 * &ea_list is present and its values are sorted in order to speed up
41
 * searching), they are stored in a hash table to make fast lookup possible
42
 * and they are provided with a use count to allow sharing.
43
 *
44
 * Routing tables always contain only cached &rta's.
45
 */
46
47 2326b001 Martin Mares
#include "nest/bird.h"
48
#include "nest/route.h"
49
#include "nest/protocol.h"
50 66e53309 Martin Mares
#include "nest/iface.h"
51 730f2e2c Martin Mares
#include "nest/cli.h"
52 c6add07f Martin Mares
#include "nest/attrs.h"
53 10af3676 Ondrej Filip
#include "lib/alloca.h"
54 e7d2ac44 Ondrej Zajicek
#include "lib/hash.h"
55 74c838a8 Ondrej Zajicek (work)
#include "lib/idm.h"
56 2326b001 Martin Mares
#include "lib/resource.h"
57 221135d6 Martin Mares
#include "lib/string.h"
58 2326b001 Martin Mares
59 9a74622c Jan Moskyto Matejka
#include <stddef.h>
60
61 acb60628 Ondrej Zajicek
pool *rta_pool;
62
63 ec5e5d23 Jan Moskyto Matejka
static slab *rta_slab_[4];
64
static slab *nexthop_slab_[4];
65 094d2bdb Ondrej Zajicek
static slab *rte_src_slab;
66
67 74c838a8 Ondrej Zajicek (work)
static struct idm src_ids;
68 e7d2ac44 Ondrej Zajicek
#define SRC_ID_INIT_SIZE 4
69 094d2bdb Ondrej Zajicek
70
/* rte source hash */
71 e7d2ac44 Ondrej Zajicek
72
#define RSH_KEY(n)                n->proto, n->private_id
73
#define RSH_NEXT(n)                n->next
74
#define RSH_EQ(p1,n1,p2,n2)        p1 == p2 && n1 == n2
75
#define RSH_FN(p,n)                p->hash_key ^ u32_hash(n)
76
77
#define RSH_REHASH                rte_src_rehash
78
#define RSH_PARAMS                /2, *2, 1, 1, 8, 20
79
#define RSH_INIT_ORDER                6
80
81
static HASH(struct rte_src) src_hash;
82 2326b001 Martin Mares
83 3991d84e Martin Mares
struct protocol *attr_class_to_protocol[EAP_MAX];
84
85 094d2bdb Ondrej Zajicek
86
static void
87
rte_src_init(void)
88
{
89
  rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
90
91 74c838a8 Ondrej Zajicek (work)
  idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
92 094d2bdb Ondrej Zajicek
93 e7d2ac44 Ondrej Zajicek
  HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
94 094d2bdb Ondrej Zajicek
}
95
96
97 e7d2ac44 Ondrej Zajicek
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
98 094d2bdb Ondrej Zajicek
99
struct rte_src *
100
rt_find_source(struct proto *p, u32 id)
101
{
102 e7d2ac44 Ondrej Zajicek
  return HASH_FIND(src_hash, RSH, p, id);
103 094d2bdb Ondrej Zajicek
}
104
105
struct rte_src *
106
rt_get_source(struct proto *p, u32 id)
107
{
108 e7d2ac44 Ondrej Zajicek
  struct rte_src *src = rt_find_source(p, id);
109 094d2bdb Ondrej Zajicek
110 e7d2ac44 Ondrej Zajicek
  if (src)
111
    return src;
112 094d2bdb Ondrej Zajicek
113
  src = sl_alloc(rte_src_slab);
114
  src->proto = p;
115
  src->private_id = id;
116 74c838a8 Ondrej Zajicek (work)
  src->global_id = idm_alloc(&src_ids);
117 094d2bdb Ondrej Zajicek
  src->uc = 0;
118 d217ba51 Ondrej Zajicek
119 e7d2ac44 Ondrej Zajicek
  HASH_INSERT2(src_hash, RSH, rta_pool, src);
120 094d2bdb Ondrej Zajicek
121
  return src;
122
}
123
124
void
125
rt_prune_sources(void)
126
{
127 e7d2ac44 Ondrej Zajicek
  HASH_WALK_FILTER(src_hash, next, src, sp)
128
  {
129
    if (src->uc == 0)
130 094d2bdb Ondrej Zajicek
    {
131 e7d2ac44 Ondrej Zajicek
      HASH_DO_REMOVE(src_hash, RSH, sp);
132 74c838a8 Ondrej Zajicek (work)
      idm_free(&src_ids, src->global_id);
133 e7d2ac44 Ondrej Zajicek
      sl_free(rte_src_slab, src);
134 094d2bdb Ondrej Zajicek
    }
135 e7d2ac44 Ondrej Zajicek
  }
136
  HASH_WALK_FILTER_END;
137 094d2bdb Ondrej Zajicek
138 e7d2ac44 Ondrej Zajicek
  HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
139 094d2bdb Ondrej Zajicek
}
140
141
142
/*
143
 *        Multipath Next Hop
144
 */
145
146 04632fd7 Ondrej Zajicek (work)
static inline u32
147 4e276a89 Jan Moskyto Matejka
nexthop_hash(struct nexthop *x)
148 7e95c05d Ondrej Zajicek
{
149 04632fd7 Ondrej Zajicek (work)
  u32 h = 0;
150 7e95c05d Ondrej Zajicek
  for (; x; x = x->next)
151 ec5e5d23 Jan Moskyto Matejka
  {
152
    h ^= ipa_hash(x->gw) ^ (h << 5) ^ (h >> 9);
153
    for (int i=0; i<x->labels; i++)
154
      h ^= x->label[i] ^ (h << 6) ^ (h >> 7);
155
  }
156 7e95c05d Ondrej Zajicek
157
  return h;
158
}
159
160
int
161 4e276a89 Jan Moskyto Matejka
nexthop__same(struct nexthop *x, struct nexthop *y)
162 7e95c05d Ondrej Zajicek
{
163
  for (; x && y; x = x->next, y = y->next)
164 ec5e5d23 Jan Moskyto Matejka
  {
165
    if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight) || (x->labels != y->labels))
166 7e95c05d Ondrej Zajicek
      return 0;
167 ec5e5d23 Jan Moskyto Matejka
    for (int i=0; i<x->labels; i++)
168
      if (x->label[i] != y->label[i])
169
        return 0;
170
  }
171 7e95c05d Ondrej Zajicek
172 ec5e5d23 Jan Moskyto Matejka
  return 1;
173 7e95c05d Ondrej Zajicek
}
174
175 d217ba51 Ondrej Zajicek
static int
176 4e276a89 Jan Moskyto Matejka
nexthop_compare_node(struct nexthop *x, struct nexthop *y)
177 d217ba51 Ondrej Zajicek
{
178
  int r;
179
180
  if (!x)
181
    return 1;
182
183
  if (!y)
184
    return -1;
185
186
  r = ((int) y->weight) - ((int) x->weight);
187
  if (r)
188
    return r;
189
190
  r = ipa_compare(x->gw, y->gw);
191
  if (r)
192
    return r;
193
194 ec5e5d23 Jan Moskyto Matejka
  r = ((int) y->labels) - ((int) x->labels);
195
  if (r)
196
    return r;
197
198
  for (int i=0; i<y->labels; i++)
199
  {
200
    r = ((int) y->label[i]) - ((int) x->label[i]);
201
    if (r)
202
      return r;
203
  }
204
205 d217ba51 Ondrej Zajicek
  return ((int) x->iface->index) - ((int) y->iface->index);
206
}
207
208 4e276a89 Jan Moskyto Matejka
static inline struct nexthop *
209
nexthop_copy_node(const struct nexthop *src, linpool *lp)
210 d217ba51 Ondrej Zajicek
{
211 ec5e5d23 Jan Moskyto Matejka
  struct nexthop *n = lp_alloc(lp, nexthop_size(src));
212
213
  memcpy(n, src, nexthop_size(src));
214 d217ba51 Ondrej Zajicek
  n->next = NULL;
215 ec5e5d23 Jan Moskyto Matejka
216 d217ba51 Ondrej Zajicek
  return n;
217
}
218
219
/**
220 4e276a89 Jan Moskyto Matejka
 * nexthop_merge - merge nexthop lists
221 d217ba51 Ondrej Zajicek
 * @x: list 1
222
 * @y: list 2
223
 * @rx: reusability of list @x
224
 * @ry: reusability of list @y
225
 * @max: max number of nexthops
226
 * @lp: linpool for allocating nexthops
227
 *
228 4e276a89 Jan Moskyto Matejka
 * The nexthop_merge() function takes two nexthop lists @x and @y and merges them,
229 d217ba51 Ondrej Zajicek
 * eliminating possible duplicates. The input lists must be sorted and the
230
 * result is sorted too. The number of nexthops in result is limited by @max.
231
 * New nodes are allocated from linpool @lp.
232
 *
233
 * The arguments @rx and @ry specify whether corresponding input lists may be
234
 * consumed by the function (i.e. their nodes reused in the resulting list), in
235
 * that case the caller should not access these lists after that. To eliminate
236
 * issues with deallocation of these lists, the caller should use some form of
237
 * bulk deallocation (e.g. stack or linpool) to free these nodes when the
238
 * resulting list is no longer needed. When reusability is not set, the
239
 * corresponding lists are not modified nor linked from the resulting list.
240
 */
241 4e276a89 Jan Moskyto Matejka
struct nexthop *
242
nexthop_merge(struct nexthop *x, struct nexthop *y, int rx, int ry, int max, linpool *lp)
243 d217ba51 Ondrej Zajicek
{
244 4e276a89 Jan Moskyto Matejka
  struct nexthop *root = NULL;
245
  struct nexthop **n = &root;
246 d217ba51 Ondrej Zajicek
247
  while ((x || y) && max--)
248
  {
249 4e276a89 Jan Moskyto Matejka
    int cmp = nexthop_compare_node(x, y);
250 d217ba51 Ondrej Zajicek
    if (cmp < 0)
251
    {
252 4e276a89 Jan Moskyto Matejka
      *n = rx ? x : nexthop_copy_node(x, lp);
253 d217ba51 Ondrej Zajicek
      x = x->next;
254
    }
255
    else if (cmp > 0)
256
    {
257 4e276a89 Jan Moskyto Matejka
      *n = ry ? y : nexthop_copy_node(y, lp);
258 d217ba51 Ondrej Zajicek
      y = y->next;
259
    }
260
    else
261
    {
262 4e276a89 Jan Moskyto Matejka
      *n = rx ? x : (ry ? y : nexthop_copy_node(x, lp));
263 d217ba51 Ondrej Zajicek
      x = x->next;
264
      y = y->next;
265
    }
266
    n = &((*n)->next);
267
  }
268
  *n = NULL;
269
270
  return root;
271
}
272
273 84cac51a Ondrej Zajicek (work)
void
274 4e276a89 Jan Moskyto Matejka
nexthop_insert(struct nexthop *n, struct nexthop *x)
275 84cac51a Ondrej Zajicek (work)
{
276 4e276a89 Jan Moskyto Matejka
  struct nexthop tmp;
277
  memcpy(&tmp, n, sizeof(struct nexthop));
278
  if (nexthop_compare_node(n, x) > 0) /* Insert to the included nexthop */
279 84cac51a Ondrej Zajicek (work)
  {
280 4e276a89 Jan Moskyto Matejka
    memcpy(n, x, sizeof(struct nexthop));
281
    memcpy(x, &tmp, sizeof(struct nexthop));
282
    n->next = x;
283
    return;
284
  }
285
286
  for (struct nexthop **nn = &(n->next); *nn; nn = &((*nn)->next))
287
  {
288
    int cmp = nexthop_compare_node(*nn, x);
289 84cac51a Ondrej Zajicek (work)
290
    if (cmp < 0)
291
      continue;
292 4e276a89 Jan Moskyto Matejka
    
293
    if (cmp > 0)
294
    {
295
      x->next = *nn;
296
      *nn = x;
297
    }
298
    
299
    return;
300 84cac51a Ondrej Zajicek (work)
  }
301
302
}
303
304
int
305 4e276a89 Jan Moskyto Matejka
nexthop_is_sorted(struct nexthop *x)
306 84cac51a Ondrej Zajicek (work)
{
307
  for (; x && x->next; x = x->next)
308 4e276a89 Jan Moskyto Matejka
    if (nexthop_compare_node(x, x->next) >= 0)
309 84cac51a Ondrej Zajicek (work)
      return 0;
310
311
  return 1;
312
}
313 d217ba51 Ondrej Zajicek
314 ec5e5d23 Jan Moskyto Matejka
static inline slab *
315
nexthop_slab(struct nexthop *nh)
316
{
317
  return nexthop_slab_[nh->labels > 2 ? 3 : nh->labels];
318
}
319
320 4e276a89 Jan Moskyto Matejka
static struct nexthop *
321
nexthop_copy(struct nexthop *o)
322 7e95c05d Ondrej Zajicek
{
323 4e276a89 Jan Moskyto Matejka
  struct nexthop *first = NULL;
324
  struct nexthop **last = &first;
325 7e95c05d Ondrej Zajicek
326
  for (; o; o = o->next)
327
    {
328 ec5e5d23 Jan Moskyto Matejka
      struct nexthop *n = sl_alloc(nexthop_slab(o));
329 7e95c05d Ondrej Zajicek
      n->gw = o->gw;
330
      n->iface = o->iface;
331
      n->next = NULL;
332
      n->weight = o->weight;
333 f2010f9c Jan Moskyto Matejka
      n->labels = o->labels;
334
      for (int i=0; i<o->labels; i++)
335
        n->label[i] = o->label[i];
336 7e95c05d Ondrej Zajicek
337
      *last = n;
338
      last = &(n->next);
339
    }
340
341
  return first;
342
}
343
344
static void
345 4e276a89 Jan Moskyto Matejka
nexthop_free(struct nexthop *o)
346 7e95c05d Ondrej Zajicek
{
347 4e276a89 Jan Moskyto Matejka
  struct nexthop *n;
348 7e95c05d Ondrej Zajicek
349
  while (o)
350
    {
351
      n = o->next;
352 ec5e5d23 Jan Moskyto Matejka
      sl_free(nexthop_slab(o), o);
353 7e95c05d Ondrej Zajicek
      o = n;
354
    }
355
}
356
357
358 b77ae37d Martin Mares
/*
359
 *        Extended Attributes
360
 */
361
362 8d24b689 Martin Mares
static inline eattr *
363
ea__find(ea_list *e, unsigned id)
364 b77ae37d Martin Mares
{
365
  eattr *a;
366
  int l, r, m;
367
368
  while (e)
369
    {
370
      if (e->flags & EALF_BISECT)
371
        {
372
          l = 0;
373 fee78355 Ondrej Zajicek
          r = e->count - 1;
374 b77ae37d Martin Mares
          while (l <= r)
375
            {
376
              m = (l+r) / 2;
377
              a = &e->attrs[m];
378
              if (a->id == id)
379
                return a;
380
              else if (a->id < id)
381
                l = m+1;
382
              else
383
                r = m-1;
384
            }
385
        }
386
      else
387
        for(m=0; m<e->count; m++)
388
          if (e->attrs[m].id == id)
389
            return &e->attrs[m];
390
      e = e->next;
391
    }
392
  return NULL;
393
}
394
395 3ce8c610 Martin Mares
/**
396
 * ea_find - find an extended attribute
397
 * @e: attribute list to search in
398
 * @id: attribute ID to search for
399
 *
400
 * Given an extended attribute list, ea_find() searches for a first
401 2e9b2421 Martin Mares
 * occurrence of an attribute with specified ID, returning either a pointer
402 3ce8c610 Martin Mares
 * to its &eattr structure or %NULL if no such attribute exists.
403
 */
404 8d24b689 Martin Mares
eattr *
405
ea_find(ea_list *e, unsigned id)
406
{
407
  eattr *a = ea__find(e, id & EA_CODE_MASK);
408
409
  if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
410
      !(id & EA_ALLOW_UNDEF))
411
    return NULL;
412
  return a;
413
}
414
415 3ce8c610 Martin Mares
/**
416 9fdf9d29 Ondrej Zajicek
 * ea_walk - walk through extended attributes
417
 * @s: walk state structure
418
 * @id: start of attribute ID interval
419
 * @max: length of attribute ID interval
420
 *
421
 * Given an extended attribute list, ea_walk() walks through the list looking
422
 * for first occurrences of attributes with ID in specified interval from @id to
423
 * (@id + @max - 1), returning pointers to found &eattr structures, storing its
424
 * walk state in @s for subsequent calls.
425 8e433d6a Pavel Tvrdik
 *
426 9fdf9d29 Ondrej Zajicek
 * The function ea_walk() is supposed to be called in a loop, with initially
427
 * zeroed walk state structure @s with filled the initial extended attribute
428
 * list, returning one found attribute in each call or %NULL when no other
429
 * attribute exists. The extended attribute list or the arguments should not be
430
 * modified between calls. The maximum value of @max is 128.
431
 */
432
eattr *
433
ea_walk(struct ea_walk_state *s, uint id, uint max)
434
{
435
  ea_list *e = s->eattrs;
436
  eattr *a = s->ea;
437
  eattr *a_max;
438
439
  max = id + max;
440
441
  if (a)
442
    goto step;
443
444
  for (; e; e = e->next)
445
  {
446
    if (e->flags & EALF_BISECT)
447
    {
448
      int l, r, m;
449
450
      l = 0;
451
      r = e->count - 1;
452
      while (l < r)
453
      {
454
        m = (l+r) / 2;
455
        if (e->attrs[m].id < id)
456
          l = m + 1;
457
        else
458
          r = m;
459
      }
460
      a = e->attrs + l;
461
    }
462
    else
463
      a = e->attrs;
464
465
  step:
466
    a_max = e->attrs + e->count;
467
    for (; a < a_max; a++)
468
      if ((a->id >= id) && (a->id < max))
469
      {
470
        int n = a->id - id;
471
472
        if (BIT32_TEST(s->visited, n))
473
          continue;
474
475
        BIT32_SET(s->visited, n);
476
477
        if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
478
          continue;
479
480
        s->eattrs = e;
481
        s->ea = a;
482
        return a;
483
      }
484
      else if (e->flags & EALF_BISECT)
485
        break;
486
  }
487
488
  return NULL;
489
}
490
491
/**
492 3ce8c610 Martin Mares
 * ea_get_int - fetch an integer attribute
493
 * @e: attribute list
494
 * @id: attribute ID
495
 * @def: default value
496
 *
497
 * This function is a shortcut for retrieving a value of an integer attribute
498
 * by calling ea_find() to find the attribute, extracting its value or returning
499
 * a provided default if no such attribute is present.
500
 */
501 c0100454 Pavel Machek
int
502
ea_get_int(ea_list *e, unsigned id, int def)
503
{
504
  eattr *a = ea_find(e, id);
505
  if (!a)
506
    return def;
507
  return a->u.data;
508
}
509
510 b77ae37d Martin Mares
static inline void
511
ea_do_sort(ea_list *e)
512
{
513
  unsigned n = e->count;
514
  eattr *a = e->attrs;
515
  eattr *b = alloca(n * sizeof(eattr));
516
  unsigned s, ss;
517
518
  /* We need to use a stable sorting algorithm, hence mergesort */
519
  do
520
    {
521
      s = ss = 0;
522
      while (s < n)
523
        {
524
          eattr *p, *q, *lo, *hi;
525
          p = b;
526
          ss = s;
527
          *p++ = a[s++];
528
          while (s < n && p[-1].id <= a[s].id)
529
            *p++ = a[s++];
530
          if (s < n)
531
            {
532
              q = p;
533
              *p++ = a[s++];
534
              while (s < n && p[-1].id <= a[s].id)
535
                *p++ = a[s++];
536
              lo = b;
537
              hi = q;
538
              s = ss;
539
              while (lo < q && hi < p)
540
                if (lo->id <= hi->id)
541
                  a[s++] = *lo++;
542
                else
543
                  a[s++] = *hi++;
544
              while (lo < q)
545
                a[s++] = *lo++;
546
              while (hi < p)
547
                a[s++] = *hi++;
548
            }
549
        }
550
    }
551
  while (ss);
552
}
553
554
static inline void
555
ea_do_prune(ea_list *e)
556
{
557 51a183af Martin Mares
  eattr *s, *d, *l, *s0;
558 8d24b689 Martin Mares
  int i = 0;
559
560
  /* Discard duplicates and undefs. Do you remember sorting was stable? */
561
  s = d = e->attrs;
562
  l = e->attrs + e->count;
563
  while (s < l)
564
    {
565 51a183af Martin Mares
      s0 = s++;
566
      while (s < l && s->id == s[-1].id)
567
        s++;
568
      /* s0 is the most recent version, s[-1] the oldest one */
569
      if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
570 8d24b689 Martin Mares
        {
571 51a183af Martin Mares
          *d = *s0;
572 d15b0b0a Ondrej Zajicek (work)
          d->type = (d->type & ~(EAF_ORIGINATED|EAF_FRESH)) | (s[-1].type & EAF_ORIGINATED);
573 51a183af Martin Mares
          d++;
574 8d24b689 Martin Mares
          i++;
575
        }
576
    }
577
  e->count = i;
578 b77ae37d Martin Mares
}
579
580 3ce8c610 Martin Mares
/**
581
 * ea_sort - sort an attribute list
582
 * @e: list to be sorted
583
 *
584
 * This function takes a &ea_list chain and sorts the attributes
585
 * within each of its entries.
586
 *
587
 * If an attribute occurs multiple times in a single &ea_list,
588 2e9b2421 Martin Mares
 * ea_sort() leaves only the first (the only significant) occurrence.
589 3ce8c610 Martin Mares
 */
590 b77ae37d Martin Mares
void
591
ea_sort(ea_list *e)
592
{
593
  while (e)
594
    {
595
      if (!(e->flags & EALF_SORTED))
596
        {
597
          ea_do_sort(e);
598
          ea_do_prune(e);
599
          e->flags |= EALF_SORTED;
600
        }
601
      if (e->count > 5)
602
        e->flags |= EALF_BISECT;
603
      e = e->next;
604
    }
605
}
606
607 3ce8c610 Martin Mares
/**
608
 * ea_scan - estimate attribute list size
609
 * @e: attribute list
610
 *
611
 * This function calculates an upper bound of the size of
612
 * a given &ea_list after merging with ea_merge().
613
 */
614 b77ae37d Martin Mares
unsigned
615
ea_scan(ea_list *e)
616
{
617
  unsigned cnt = 0;
618
619
  while (e)
620
    {
621
      cnt += e->count;
622
      e = e->next;
623
    }
624
  return sizeof(ea_list) + sizeof(eattr)*cnt;
625
}
626
627 3ce8c610 Martin Mares
/**
628
 * ea_merge - merge segments of an attribute list
629
 * @e: attribute list
630
 * @t: buffer to store the result to
631
 *
632
 * This function takes a possibly multi-segment attribute list
633
 * and merges all of its segments to one.
634
 *
635
 * The primary use of this function is for &ea_list normalization:
636
 * first call ea_scan() to determine how much memory will the result
637
 * take, then allocate a buffer (usually using alloca()), merge the
638
 * segments with ea_merge() and finally sort and prune the result
639
 * by calling ea_sort().
640
 */
641 b77ae37d Martin Mares
void
642
ea_merge(ea_list *e, ea_list *t)
643
{
644
  eattr *d = t->attrs;
645
646
  t->flags = 0;
647
  t->count = 0;
648
  t->next = NULL;
649
  while (e)
650
    {
651
      memcpy(d, e->attrs, sizeof(eattr)*e->count);
652
      t->count += e->count;
653
      d += e->count;
654
      e = e->next;
655
    }
656
}
657
658 3ce8c610 Martin Mares
/**
659
 * ea_same - compare two &ea_list's
660
 * @x: attribute list
661
 * @y: attribute list
662
 *
663
 * ea_same() compares two normalized attribute lists @x and @y and returns
664
 * 1 if they contain the same attributes, 0 otherwise.
665
 */
666 6f57dcc0 Martin Mares
int
667 2326b001 Martin Mares
ea_same(ea_list *x, ea_list *y)
668
{
669
  int c;
670
671 b77ae37d Martin Mares
  if (!x || !y)
672
    return x == y;
673
  ASSERT(!x->next && !y->next);
674
  if (x->count != y->count)
675
    return 0;
676
  for(c=0; c<x->count; c++)
677 2326b001 Martin Mares
    {
678 b77ae37d Martin Mares
      eattr *a = &x->attrs[c];
679
      eattr *b = &y->attrs[c];
680
681
      if (a->id != b->id ||
682
          a->flags != b->flags ||
683
          a->type != b->type ||
684 28a10f84 Ondrej Zajicek
          ((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
685 2326b001 Martin Mares
        return 0;
686 b77ae37d Martin Mares
    }
687
  return 1;
688
}
689
690
static inline ea_list *
691
ea_list_copy(ea_list *o)
692
{
693
  ea_list *n;
694
  unsigned i, len;
695
696
  if (!o)
697
    return NULL;
698
  ASSERT(!o->next);
699
  len = sizeof(ea_list) + sizeof(eattr) * o->count;
700
  n = mb_alloc(rta_pool, len);
701
  memcpy(n, o, len);
702
  n->flags |= EALF_CACHED;
703
  for(i=0; i<o->count; i++)
704
    {
705
      eattr *a = &n->attrs[i];
706 08732b71 Martin Mares
      if (!(a->type & EAF_EMBEDDED))
707 b77ae37d Martin Mares
        {
708
          unsigned size = sizeof(struct adata) + a->u.ptr->length;
709
          struct adata *d = mb_alloc(rta_pool, size);
710
          memcpy(d, a->u.ptr, size);
711
          a->u.ptr = d;
712
        }
713
    }
714
  return n;
715
}
716
717 5d86aefb Martin Mares
static inline void
718
ea_free(ea_list *o)
719
{
720 3ced9b34 Martin Mares
  int i;
721
722 5d86aefb Martin Mares
  if (o)
723
    {
724
      ASSERT(!o->next);
725 3ced9b34 Martin Mares
      for(i=0; i<o->count; i++)
726
        {
727
          eattr *a = &o->attrs[i];
728
          if (!(a->type & EAF_EMBEDDED))
729
            mb_free(a->u.ptr);
730
        }
731 5d86aefb Martin Mares
      mb_free(o);
732
    }
733
}
734
735 ba5e5940 Ondrej Zajicek
static int
736
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
737
{
738
  if (a->id == EA_GEN_IGP_METRIC)
739
    {
740
      *buf += bsprintf(*buf, "igp_metric");
741
      return GA_NAME;
742
    }
743 d217ba51 Ondrej Zajicek
744 ba5e5940 Ondrej Zajicek
  return GA_UNKNOWN;
745
}
746
747 315f23a0 Ondrej Zajicek
void
748
ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
749
{
750
  byte *bound = buf + bufsize - 32;
751
  u32 data = a->u.data;
752
  int i;
753
754
  for (i = min; i < max; i++)
755
    if ((data & (1u << i)) && names[i])
756
    {
757
      if (buf > bound)
758
      {
759
        strcpy(buf, " ...");
760
        return;
761
      }
762
763
      buf += bsprintf(buf, " %s", names[i]);
764
      data &= ~(1u << i);
765
    }
766
767
  if (data)
768
    bsprintf(buf, " %08x", data);
769
770
  return;
771
}
772
773 fdf16eb6 Ondrej Zajicek
static inline void
774 ae80a2de Pavel Tvrdík
opaque_format(struct adata *ad, byte *buf, uint size)
775 fdf16eb6 Ondrej Zajicek
{
776
  byte *bound = buf + size - 10;
777 3e236955 Jan Moskyto Matejka
  uint i;
778 fdf16eb6 Ondrej Zajicek
779
  for(i = 0; i < ad->length; i++)
780
    {
781
      if (buf > bound)
782
        {
783
          strcpy(buf, " ...");
784
          return;
785
        }
786
      if (i)
787
        *buf++ = ' ';
788
789
      buf += bsprintf(buf, "%02x", ad->data[i]);
790
    }
791
792
  *buf = 0;
793
  return;
794
}
795
796
static inline void
797
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
798
{
799
  int i = int_set_format(ad, way, 0, pos, end - pos);
800 a52d52fa Ondrej Zajicek
  cli_printf(c, -1012, "\t%s", buf);
801 fdf16eb6 Ondrej Zajicek
  while (i)
802
    {
803
      i = int_set_format(ad, way, i, buf, end - buf - 1);
804 a52d52fa Ondrej Zajicek
      cli_printf(c, -1012, "\t\t%s", buf);
805 fdf16eb6 Ondrej Zajicek
    }
806
}
807
808 42a0c054 Ondrej Zajicek
static inline void
809
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
810
{
811
  int i = ec_set_format(ad, 0, pos, end - pos);
812 a52d52fa Ondrej Zajicek
  cli_printf(c, -1012, "\t%s", buf);
813 42a0c054 Ondrej Zajicek
  while (i)
814
    {
815
      i = ec_set_format(ad, i, buf, end - buf - 1);
816 a52d52fa Ondrej Zajicek
      cli_printf(c, -1012, "\t\t%s", buf);
817 42a0c054 Ondrej Zajicek
    }
818
}
819
820 66dbdbd9 Ondrej Zajicek (work)
static inline void
821
ea_show_lc_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
822
{
823
  int i = lc_set_format(ad, 0, pos, end - pos);
824
  cli_printf(c, -1012, "\t%s", buf);
825
  while (i)
826
    {
827
      i = lc_set_format(ad, i, buf, end - buf - 1);
828
      cli_printf(c, -1012, "\t\t%s", buf);
829
    }
830
}
831
832 3ce8c610 Martin Mares
/**
833 fdf16eb6 Ondrej Zajicek
 * ea_show - print an &eattr to CLI
834
 * @c: destination CLI
835
 * @e: attribute to be printed
836 3ce8c610 Martin Mares
 *
837 fdf16eb6 Ondrej Zajicek
 * This function takes an extended attribute represented by its &eattr
838
 * structure and prints it to the CLI according to the type information.
839 3ce8c610 Martin Mares
 *
840
 * If the protocol defining the attribute provides its own
841
 * get_attr() hook, it's consulted first.
842
 */
843 b77ae37d Martin Mares
void
844 fdf16eb6 Ondrej Zajicek
ea_show(struct cli *c, eattr *e)
845 3991d84e Martin Mares
{
846
  struct protocol *p;
847
  int status = GA_UNKNOWN;
848
  struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
849 fdf16eb6 Ondrej Zajicek
  byte buf[CLI_MSG_SIZE];
850
  byte *pos = buf, *end = buf + sizeof(buf);
851 3991d84e Martin Mares
852
  if (p = attr_class_to_protocol[EA_PROTO(e->id)])
853
    {
854 fdf16eb6 Ondrej Zajicek
      pos += bsprintf(pos, "%s.", p->name);
855 3991d84e Martin Mares
      if (p->get_attr)
856 fdf16eb6 Ondrej Zajicek
        status = p->get_attr(e, pos, end - pos);
857
      pos += strlen(pos);
858 3991d84e Martin Mares
    }
859
  else if (EA_PROTO(e->id))
860 fdf16eb6 Ondrej Zajicek
    pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
861 d217ba51 Ondrej Zajicek
  else
862 fdf16eb6 Ondrej Zajicek
    status = get_generic_attr(e, &pos, end - pos);
863 ba5e5940 Ondrej Zajicek
864 3991d84e Martin Mares
  if (status < GA_NAME)
865 fdf16eb6 Ondrej Zajicek
    pos += bsprintf(pos, "%02x", EA_ID(e->id));
866 3991d84e Martin Mares
  if (status < GA_FULL)
867
    {
868 fdf16eb6 Ondrej Zajicek
      *pos++ = ':';
869
      *pos++ = ' ';
870 3991d84e Martin Mares
      switch (e->type & EAF_TYPE_MASK)
871
        {
872
        case EAF_TYPE_INT:
873 fdf16eb6 Ondrej Zajicek
          bsprintf(pos, "%u", e->u.data);
874 3991d84e Martin Mares
          break;
875
        case EAF_TYPE_OPAQUE:
876 fdf16eb6 Ondrej Zajicek
          opaque_format(ad, pos, end - pos);
877 3991d84e Martin Mares
          break;
878
        case EAF_TYPE_IP_ADDRESS:
879 fdf16eb6 Ondrej Zajicek
          bsprintf(pos, "%I", *(ip_addr *) ad->data);
880 3991d84e Martin Mares
          break;
881
        case EAF_TYPE_ROUTER_ID:
882 fdf16eb6 Ondrej Zajicek
          bsprintf(pos, "%R", e->u.data);
883 3991d84e Martin Mares
          break;
884 c6add07f Martin Mares
        case EAF_TYPE_AS_PATH:
885 fdf16eb6 Ondrej Zajicek
          as_path_format(ad, pos, end - pos);
886 c6add07f Martin Mares
          break;
887 315f23a0 Ondrej Zajicek
        case EAF_TYPE_BITFIELD:
888
          bsprintf(pos, "%08x", e->u.data);
889
          break;
890 c6add07f Martin Mares
        case EAF_TYPE_INT_SET:
891 fdf16eb6 Ondrej Zajicek
          ea_show_int_set(c, ad, 1, pos, buf, end);
892
          return;
893 42a0c054 Ondrej Zajicek
        case EAF_TYPE_EC_SET:
894
          ea_show_ec_set(c, ad, pos, buf, end);
895
          return;
896 66dbdbd9 Ondrej Zajicek (work)
        case EAF_TYPE_LC_SET:
897
          ea_show_lc_set(c, ad, pos, buf, end);
898
          return;
899 3991d84e Martin Mares
        case EAF_TYPE_UNDEF:
900
        default:
901 fdf16eb6 Ondrej Zajicek
          bsprintf(pos, "<type %02x>", e->type);
902 3991d84e Martin Mares
        }
903
    }
904 a52d52fa Ondrej Zajicek
  cli_printf(c, -1012, "\t%s", buf);
905 3991d84e Martin Mares
}
906
907 3ce8c610 Martin Mares
/**
908
 * ea_dump - dump an extended attribute
909
 * @e: attribute to be dumped
910
 *
911
 * ea_dump() dumps contents of the extended attribute given to
912
 * the debug output.
913
 */
914 3991d84e Martin Mares
void
915 b77ae37d Martin Mares
ea_dump(ea_list *e)
916
{
917
  int i;
918
919
  if (!e)
920
    {
921
      debug("NONE");
922
      return;
923
    }
924
  while (e)
925
    {
926
      debug("[%c%c%c]",
927
            (e->flags & EALF_SORTED) ? 'S' : 's',
928
            (e->flags & EALF_BISECT) ? 'B' : 'b',
929
            (e->flags & EALF_CACHED) ? 'C' : 'c');
930
      for(i=0; i<e->count; i++)
931 2326b001 Martin Mares
        {
932 b77ae37d Martin Mares
          eattr *a = &e->attrs[i];
933
          debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
934 9f4929e7 Martin Mares
          if (a->type & EAF_TEMP)
935
            debug("T");
936 b77ae37d Martin Mares
          debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
937 51a183af Martin Mares
          if (a->type & EAF_ORIGINATED)
938
            debug("o");
939 b77ae37d Martin Mares
          if (a->type & EAF_EMBEDDED)
940
            debug(":%08x", a->u.data);
941
          else
942
            {
943
              int j, len = a->u.ptr->length;
944
              debug("[%d]:", len);
945
              for(j=0; j<len; j++)
946
                debug("%02x", a->u.ptr->data[j]);
947
            }
948 2326b001 Martin Mares
        }
949 b77ae37d Martin Mares
      if (e = e->next)
950
        debug(" | ");
951 2326b001 Martin Mares
    }
952
}
953
954 3ce8c610 Martin Mares
/**
955
 * ea_hash - calculate an &ea_list hash key
956
 * @e: attribute list
957
 *
958
 * ea_hash() takes an extended attribute list and calculated a hopefully
959
 * uniformly distributed hash value from its contents.
960
 */
961 ae80a2de Pavel Tvrdík
inline uint
962 ee76a92a Martin Mares
ea_hash(ea_list *e)
963
{
964 9a74622c Jan Moskyto Matejka
  const u64 mul = 0x68576150f3d6847;
965
  u64 h = 0xafcef24eda8b29;
966 ee76a92a Martin Mares
  int i;
967
968
  if (e)                        /* Assuming chain of length 1 */
969
    {
970
      for(i=0; i<e->count; i++)
971
        {
972
          struct eattr *a = &e->attrs[i];
973 9a74622c Jan Moskyto Matejka
          h ^= a->id; h *= mul;
974 ee76a92a Martin Mares
          if (a->type & EAF_EMBEDDED)
975
            h ^= a->u.data;
976
          else
977
            {
978
              struct adata *d = a->u.ptr;
979 9a74622c Jan Moskyto Matejka
              h ^= mem_hash(d->data, d->length);
980 ee76a92a Martin Mares
            }
981 9a74622c Jan Moskyto Matejka
          h *= mul;
982 ee76a92a Martin Mares
        }
983
    }
984 9a74622c Jan Moskyto Matejka
  return (h >> 32) ^ (h & 0xffffffff);
985 ee76a92a Martin Mares
}
986
987 3ce8c610 Martin Mares
/**
988
 * ea_append - concatenate &ea_list's
989
 * @to: destination list (can be %NULL)
990
 * @what: list to be appended (can be %NULL)
991
 *
992
 * This function appends the &ea_list @what at the end of
993
 * &ea_list @to and returns a pointer to the resulting list.
994
 */
995 ce1da96e Martin Mares
ea_list *
996
ea_append(ea_list *to, ea_list *what)
997
{
998
  ea_list *res;
999
1000
  if (!to)
1001
    return what;
1002
  res = to;
1003
  while (to->next)
1004
    to = to->next;
1005
  to->next = what;
1006
  return res;
1007
}
1008
1009 b77ae37d Martin Mares
/*
1010
 *        rta's
1011
 */
1012
1013 ae80a2de Pavel Tvrdík
static uint rta_cache_count;
1014
static uint rta_cache_size = 32;
1015
static uint rta_cache_limit;
1016
static uint rta_cache_mask;
1017 ee76a92a Martin Mares
static rta **rta_hash_table;
1018
1019
static void
1020
rta_alloc_hash(void)
1021
{
1022 5d86aefb Martin Mares
  rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
1023 ee76a92a Martin Mares
  if (rta_cache_size < 32768)
1024
    rta_cache_limit = rta_cache_size * 2;
1025
  else
1026
    rta_cache_limit = ~0;
1027
  rta_cache_mask = rta_cache_size - 1;
1028
}
1029
1030 ae80a2de Pavel Tvrdík
static inline uint
1031 ee76a92a Martin Mares
rta_hash(rta *a)
1032
{
1033 d39d41fb Jan Moskyto Matejka
  u64 h;
1034 54ac0bec Jan Moskyto Matejka
  mem_hash_init(&h);
1035 d39d41fb Jan Moskyto Matejka
#define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f));
1036 54ac0bec Jan Moskyto Matejka
  MIX(src);
1037
  MIX(hostentry);
1038
  MIX(from);
1039
  MIX(igp_metric);
1040 5b208e29 Jan Moskyto Matejka
  MIX(source);
1041
  MIX(scope);
1042
  MIX(dest);
1043 d39d41fb Jan Moskyto Matejka
#undef MIX
1044 54ac0bec Jan Moskyto Matejka
1045 4e276a89 Jan Moskyto Matejka
  return mem_hash_value(&h) ^ nexthop_hash(&(a->nh)) ^ ea_hash(a->eattrs);
1046 ee76a92a Martin Mares
}
1047
1048 2326b001 Martin Mares
static inline int
1049
rta_same(rta *x, rta *y)
1050
{
1051 094d2bdb Ondrej Zajicek
  return (x->src == y->src &&
1052 2326b001 Martin Mares
          x->source == y->source &&
1053
          x->scope == y->scope &&
1054
          x->dest == y->dest &&
1055 d1e146f2 Ondrej Zajicek
          x->igp_metric == y->igp_metric &&
1056 2326b001 Martin Mares
          ipa_equal(x->from, y->from) &&
1057 d1e146f2 Ondrej Zajicek
          x->hostentry == y->hostentry &&
1058 4e276a89 Jan Moskyto Matejka
          nexthop_same(&(x->nh), &(y->nh)) &&
1059 2727bb7c Martin Mares
          ea_same(x->eattrs, y->eattrs));
1060 2326b001 Martin Mares
}
1061
1062 ec5e5d23 Jan Moskyto Matejka
static inline slab *
1063
rta_slab(rta *a)
1064
{
1065
  return rta_slab_[a->nh.labels > 2 ? 3 : a->nh.labels];
1066
}
1067
1068 2326b001 Martin Mares
static rta *
1069
rta_copy(rta *o)
1070
{
1071 ec5e5d23 Jan Moskyto Matejka
  rta *r = sl_alloc(rta_slab(o));
1072 2326b001 Martin Mares
1073 ec5e5d23 Jan Moskyto Matejka
  memcpy(r, o, rta_size(o));
1074 2326b001 Martin Mares
  r->uc = 1;
1075 4e276a89 Jan Moskyto Matejka
  r->nh.next = nexthop_copy(o->nh.next);
1076 2727bb7c Martin Mares
  r->eattrs = ea_list_copy(o->eattrs);
1077 2326b001 Martin Mares
  return r;
1078
}
1079
1080 ee76a92a Martin Mares
static inline void
1081
rta_insert(rta *r)
1082
{
1083 ae80a2de Pavel Tvrdík
  uint h = r->hash_key & rta_cache_mask;
1084 ee76a92a Martin Mares
  r->next = rta_hash_table[h];
1085
  if (r->next)
1086
    r->next->pprev = &r->next;
1087
  r->pprev = &rta_hash_table[h];
1088
  rta_hash_table[h] = r;
1089
}
1090
1091
static void
1092
rta_rehash(void)
1093
{
1094 ae80a2de Pavel Tvrdík
  uint ohs = rta_cache_size;
1095
  uint h;
1096 ee76a92a Martin Mares
  rta *r, *n;
1097
  rta **oht = rta_hash_table;
1098
1099
  rta_cache_size = 2*rta_cache_size;
1100
  DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
1101
  rta_alloc_hash();
1102
  for(h=0; h<ohs; h++)
1103
    for(r=oht[h]; r; r=n)
1104
      {
1105
        n = r->next;
1106
        rta_insert(r);
1107
      }
1108
  mb_free(oht);
1109
}
1110
1111 3ce8c610 Martin Mares
/**
1112
 * rta_lookup - look up a &rta in attribute cache
1113 2e9b2421 Martin Mares
 * @o: a un-cached &rta
1114 3ce8c610 Martin Mares
 *
1115 2e9b2421 Martin Mares
 * rta_lookup() gets an un-cached &rta structure and returns its cached
1116 3ce8c610 Martin Mares
 * counterpart. It starts with examining the attribute cache to see whether
1117
 * there exists a matching entry. If such an entry exists, it's returned and
1118
 * its use count is incremented, else a new entry is created with use count
1119
 * set to 1.
1120
 *
1121
 * The extended attribute lists attached to the &rta are automatically
1122
 * converted to the normalized form.
1123
 */
1124 2326b001 Martin Mares
rta *
1125
rta_lookup(rta *o)
1126
{
1127
  rta *r;
1128 ae80a2de Pavel Tvrdík
  uint h;
1129 2326b001 Martin Mares
1130 b77ae37d Martin Mares
  ASSERT(!(o->aflags & RTAF_CACHED));
1131 2727bb7c Martin Mares
  if (o->eattrs)
1132 b77ae37d Martin Mares
    {
1133 2727bb7c Martin Mares
      if (o->eattrs->next)        /* Multiple ea_list's, need to merge them */
1134 b77ae37d Martin Mares
        {
1135 2727bb7c Martin Mares
          ea_list *ml = alloca(ea_scan(o->eattrs));
1136
          ea_merge(o->eattrs, ml);
1137
          o->eattrs = ml;
1138 b77ae37d Martin Mares
        }
1139 2727bb7c Martin Mares
      ea_sort(o->eattrs);
1140 b77ae37d Martin Mares
    }
1141
1142 ee76a92a Martin Mares
  h = rta_hash(o);
1143
  for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
1144
    if (r->hash_key == h && rta_same(r, o))
1145 2326b001 Martin Mares
      return rta_clone(r);
1146 b77ae37d Martin Mares
1147 2326b001 Martin Mares
  r = rta_copy(o);
1148 ee76a92a Martin Mares
  r->hash_key = h;
1149 04925e90 Martin Mares
  r->aflags = RTAF_CACHED;
1150 094d2bdb Ondrej Zajicek
  rt_lock_source(r->src);
1151 cfe34a31 Ondrej Zajicek
  rt_lock_hostentry(r->hostentry);
1152 ee76a92a Martin Mares
  rta_insert(r);
1153
1154
  if (++rta_cache_count > rta_cache_limit)
1155
    rta_rehash();
1156
1157 2326b001 Martin Mares
  return r;
1158
}
1159
1160
void
1161 b77ae37d Martin Mares
rta__free(rta *a)
1162 2326b001 Martin Mares
{
1163 ee76a92a Martin Mares
  ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
1164
  rta_cache_count--;
1165 5d86aefb Martin Mares
  *a->pprev = a->next;
1166
  if (a->next)
1167
    a->next->pprev = a->pprev;
1168
  a->aflags = 0;                /* Poison the entry */
1169 cfe34a31 Ondrej Zajicek
  rt_unlock_hostentry(a->hostentry);
1170 094d2bdb Ondrej Zajicek
  rt_unlock_source(a->src);
1171 4e276a89 Jan Moskyto Matejka
  if (a->nh.next)
1172
    nexthop_free(a->nh.next);
1173 5d86aefb Martin Mares
  ea_free(a->eattrs);
1174 ec5e5d23 Jan Moskyto Matejka
  sl_free(rta_slab(a), a);
1175 2326b001 Martin Mares
}
1176
1177 8d9eef17 Ondrej Zajicek
rta *
1178
rta_do_cow(rta *o, linpool *lp)
1179
{
1180 ec5e5d23 Jan Moskyto Matejka
  rta *r = lp_alloc(lp, rta_size(o));
1181
  memcpy(r, o, rta_size(o));
1182 f2010f9c Jan Moskyto Matejka
  for (struct nexthop **nhn = &(r->nh.next), *nho = o->nh.next; nho; nho = nho->next)
1183
    {
1184
      *nhn = lp_alloc(lp, nexthop_size(nho));
1185
      memcpy(*nhn, nho, nexthop_size(nho));
1186
      nhn = &((*nhn)->next);
1187
    }
1188 8d9eef17 Ondrej Zajicek
  r->aflags = 0;
1189
  r->uc = 0;
1190
  return r;
1191
}
1192
1193 3ce8c610 Martin Mares
/**
1194
 * rta_dump - dump route attributes
1195
 * @a: attribute structure to dump
1196
 *
1197
 * This function takes a &rta and dumps its contents to the debug output.
1198
 */
1199 2326b001 Martin Mares
void
1200 66e53309 Martin Mares
rta_dump(rta *a)
1201 2326b001 Martin Mares
{
1202 618533af Martin Mares
  static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
1203 beaf86e1 Martin Mares
                         "RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
1204 98ac6176 Ondrej Filip
                         "RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
1205 12640c14 Ondrej Zajicek (work)
                         "RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL" };
1206 66e53309 Martin Mares
  static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
1207
1208 5b208e29 Jan Moskyto Matejka
  debug("p=%s uc=%d %s %s%s h=%04x",
1209
        a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope),
1210 ee76a92a Martin Mares
        rtd[a->dest], a->hash_key);
1211 04925e90 Martin Mares
  if (!(a->aflags & RTAF_CACHED))
1212
    debug(" !CACHED");
1213 962ba482 Martin Mares
  debug(" <-%I", a->from);
1214 4e276a89 Jan Moskyto Matejka
  if (a->dest == RTD_UNICAST)
1215
    for (struct nexthop *nh = &(a->nh); nh; nh = nh->next)
1216
      {
1217
        if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw);
1218 ec5e5d23 Jan Moskyto Matejka
        if (nh->labels) debug(" L %d", nh->label[0]);
1219
        for (int i=1; i<nh->labels; i++)
1220
          debug("/%d", nh->label[i]);
1221 4e276a89 Jan Moskyto Matejka
        debug(" [%s]", nh->iface ? nh->iface->name : "???");
1222
      }
1223 2727bb7c Martin Mares
  if (a->eattrs)
1224 b77ae37d Martin Mares
    {
1225
      debug(" EA: ");
1226 2727bb7c Martin Mares
      ea_dump(a->eattrs);
1227 b77ae37d Martin Mares
    }
1228 2326b001 Martin Mares
}
1229
1230 3ce8c610 Martin Mares
/**
1231
 * rta_dump_all - dump attribute cache
1232
 *
1233
 * This function dumps the whole contents of route attribute cache
1234
 * to the debug output.
1235
 */
1236 2326b001 Martin Mares
void
1237
rta_dump_all(void)
1238
{
1239 66e53309 Martin Mares
  rta *a;
1240 ae80a2de Pavel Tvrdík
  uint h;
1241 ee76a92a Martin Mares
1242
  debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
1243
  for(h=0; h<rta_cache_size; h++)
1244
    for(a=rta_hash_table[h]; a; a=a->next)
1245
      {
1246
        debug("%p ", a);
1247
        rta_dump(a);
1248
        debug("\n");
1249
      }
1250 66e53309 Martin Mares
  debug("\n");
1251 2326b001 Martin Mares
}
1252
1253
void
1254 ce1da96e Martin Mares
rta_show(struct cli *c, rta *a, ea_list *eal)
1255 730f2e2c Martin Mares
{
1256
  static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
1257 6370d6f6 Ondrej Zajicek
                               "RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
1258 3991d84e Martin Mares
  int i;
1259 730f2e2c Martin Mares
1260 5b208e29 Jan Moskyto Matejka
  cli_printf(c, -1008, "\tType: %s %s", src_names[a->source], ip_scope_text(a->scope));
1261 ce1da96e Martin Mares
  if (!eal)
1262
    eal = a->eattrs;
1263
  for(; eal; eal=eal->next)
1264 3991d84e Martin Mares
    for(i=0; i<eal->count; i++)
1265 fdf16eb6 Ondrej Zajicek
      ea_show(c, &eal->attrs[i]);
1266 730f2e2c Martin Mares
}
1267
1268 3ce8c610 Martin Mares
/**
1269
 * rta_init - initialize route attribute cache
1270
 *
1271
 * This function is called during initialization of the routing
1272
 * table module to set up the internals of the attribute cache.
1273
 */
1274 730f2e2c Martin Mares
void
1275 2326b001 Martin Mares
rta_init(void)
1276
{
1277 ed68a5c6 Martin Mares
  rta_pool = rp_new(&root_pool, "Attributes");
1278 ec5e5d23 Jan Moskyto Matejka
1279
  rta_slab_[0] = sl_new(rta_pool, sizeof(rta));
1280
  rta_slab_[1] = sl_new(rta_pool, sizeof(rta) + sizeof(u32));
1281
  rta_slab_[2] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*2);
1282 d14f8c3c Jan Moskyto Matejka
  rta_slab_[3] = sl_new(rta_pool, sizeof(rta) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
1283 ec5e5d23 Jan Moskyto Matejka
1284
  nexthop_slab_[0] = sl_new(rta_pool, sizeof(struct nexthop));
1285
  nexthop_slab_[1] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32));
1286
  nexthop_slab_[2] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*2);
1287 d14f8c3c Jan Moskyto Matejka
  nexthop_slab_[3] = sl_new(rta_pool, sizeof(struct nexthop) + sizeof(u32)*MPLS_MAX_LABEL_STACK);
1288 ec5e5d23 Jan Moskyto Matejka
1289 ee76a92a Martin Mares
  rta_alloc_hash();
1290 094d2bdb Ondrej Zajicek
  rte_src_init();
1291 2326b001 Martin Mares
}
1292 3ce8c610 Martin Mares
1293
/*
1294
 *  Documentation for functions declared inline in route.h
1295
 */
1296
#if 0
1297

1298
/**
1299
 * rta_clone - clone route attributes
1300
 * @r: a &rta to be cloned
1301
 *
1302
 * rta_clone() takes a cached &rta and returns its identical cached
1303
 * copy. Currently it works by just returning the original &rta with
1304
 * its use count incremented.
1305
 */
1306
static inline rta *rta_clone(rta *r)
1307
{ DUMMY; }
1308

1309
/**
1310
 * rta_free - free route attributes
1311
 * @r: a &rta to be freed
1312
 *
1313
 * If you stop using a &rta (for example when deleting a route which uses
1314
 * it), you need to call rta_free() to notify the attribute cache the
1315
 * attribute is no longer in use and can be freed if you were the last
1316
 * user (which rta_free() tests by inspecting the use count).
1317
 */
1318
static inline void rta_free(rta *r)
1319
{ DUMMY; }
1320

1321
#endif