1998-05-20 19:54:33 +08:00
|
|
|
/*
|
|
|
|
* BIRD -- Route Attribute Cache
|
|
|
|
*
|
2000-03-05 05:09:14 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-20 19:54:33 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* DOC: Route attribute cache
|
|
|
|
*
|
|
|
|
* Each route entry carries a set of route attributes. Several of them
|
|
|
|
* vary from route to route, but most attributes are usually common
|
|
|
|
* for a large number of routes. To conserve memory, we've decided to
|
|
|
|
* store only the varying ones directly in the &rte and hold the rest
|
|
|
|
* in a special structure called &rta which is shared among all the
|
|
|
|
* &rte's with these attributes.
|
|
|
|
*
|
|
|
|
* Each &rta contains all the static attributes of the route (i.e.,
|
|
|
|
* those which are always present) as structure members and a list of
|
|
|
|
* dynamic attributes represented by a linked list of &ea_list
|
|
|
|
* structures, each of them consisting of an array of &eattr's containing
|
|
|
|
* the individual attributes. An attribute can be specified more than once
|
2000-06-07 20:29:08 +08:00
|
|
|
* in the &ea_list chain and in such case the first occurrence overrides
|
2000-06-02 20:29:55 +08:00
|
|
|
* the others. This semantics is used especially when someone (for example
|
|
|
|
* a filter) wishes to alter values of several dynamic attributes, but
|
|
|
|
* it wants to preserve the original attribute lists maintained by
|
|
|
|
* another module.
|
|
|
|
*
|
|
|
|
* Each &eattr contains an attribute identifier (split to protocol ID and
|
|
|
|
* per-protocol attribute ID), protocol dependent flags, a type code (consisting
|
|
|
|
* of several bit fields describing attribute characteristics) and either an
|
|
|
|
* embedded 32-bit value or a pointer to a &adata structure holding attribute
|
|
|
|
* contents.
|
|
|
|
*
|
2000-06-07 20:29:08 +08:00
|
|
|
* There exist two variants of &rta's -- cached and un-cached ones. Un-cached
|
2000-06-02 20:29:55 +08:00
|
|
|
* &rta's can have arbitrarily complex structure of &ea_list's and they
|
|
|
|
* can be modified by any module in the route processing chain. Cached
|
|
|
|
* &rta's have their attribute lists normalized (that means at most one
|
|
|
|
* &ea_list is present and its values are sorted in order to speed up
|
|
|
|
* searching), they are stored in a hash table to make fast lookup possible
|
|
|
|
* and they are provided with a use count to allow sharing.
|
|
|
|
*
|
|
|
|
* Routing tables always contain only cached &rta's.
|
|
|
|
*/
|
|
|
|
|
1998-05-20 19:54:33 +08:00
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "nest/protocol.h"
|
1998-06-05 04:29:05 +08:00
|
|
|
#include "nest/iface.h"
|
1999-12-01 23:10:21 +08:00
|
|
|
#include "nest/cli.h"
|
2000-04-17 18:18:55 +08:00
|
|
|
#include "nest/attrs.h"
|
2004-06-01 01:16:47 +08:00
|
|
|
#include "lib/alloca.h"
|
2013-11-27 05:37:24 +08:00
|
|
|
#include "lib/hash.h"
|
2015-12-29 22:34:48 +08:00
|
|
|
#include "lib/idm.h"
|
1998-05-20 19:54:33 +08:00
|
|
|
#include "lib/resource.h"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.h"
|
1998-05-20 19:54:33 +08:00
|
|
|
|
2016-02-10 20:26:07 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2010-06-03 04:20:40 +08:00
|
|
|
pool *rta_pool;
|
|
|
|
|
1998-05-20 19:54:33 +08:00
|
|
|
static slab *rta_slab;
|
2016-05-06 21:48:35 +08:00
|
|
|
static slab *nexthop_slab;
|
2012-08-14 22:25:22 +08:00
|
|
|
static slab *rte_src_slab;
|
|
|
|
|
2015-12-29 22:34:48 +08:00
|
|
|
static struct idm src_ids;
|
2013-11-27 05:37:24 +08:00
|
|
|
#define SRC_ID_INIT_SIZE 4
|
2012-08-14 22:25:22 +08:00
|
|
|
|
|
|
|
/* rte source hash */
|
2013-11-27 05:37:24 +08:00
|
|
|
|
|
|
|
#define RSH_KEY(n) n->proto, n->private_id
|
|
|
|
#define RSH_NEXT(n) n->next
|
|
|
|
#define RSH_EQ(p1,n1,p2,n2) p1 == p2 && n1 == n2
|
|
|
|
#define RSH_FN(p,n) p->hash_key ^ u32_hash(n)
|
|
|
|
|
|
|
|
#define RSH_REHASH rte_src_rehash
|
|
|
|
#define RSH_PARAMS /2, *2, 1, 1, 8, 20
|
|
|
|
#define RSH_INIT_ORDER 6
|
|
|
|
|
|
|
|
static HASH(struct rte_src) src_hash;
|
1998-05-20 19:54:33 +08:00
|
|
|
|
2000-04-01 18:19:47 +08:00
|
|
|
struct protocol *attr_class_to_protocol[EAP_MAX];
|
|
|
|
|
2012-08-14 22:25:22 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
rte_src_init(void)
|
|
|
|
{
|
|
|
|
rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
|
|
|
|
|
2015-12-29 22:34:48 +08:00
|
|
|
idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
|
2012-08-14 22:25:22 +08:00
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_INIT(src_hash, rta_pool, RSH_INIT_ORDER);
|
2012-08-14 22:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
|
2012-08-14 22:25:22 +08:00
|
|
|
|
|
|
|
struct rte_src *
|
|
|
|
rt_find_source(struct proto *p, u32 id)
|
|
|
|
{
|
2013-11-27 05:37:24 +08:00
|
|
|
return HASH_FIND(src_hash, RSH, p, id);
|
2012-08-14 22:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rte_src *
|
|
|
|
rt_get_source(struct proto *p, u32 id)
|
|
|
|
{
|
2013-11-27 05:37:24 +08:00
|
|
|
struct rte_src *src = rt_find_source(p, id);
|
2012-08-14 22:25:22 +08:00
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
if (src)
|
|
|
|
return src;
|
2012-08-14 22:25:22 +08:00
|
|
|
|
|
|
|
src = sl_alloc(rte_src_slab);
|
|
|
|
src->proto = p;
|
|
|
|
src->private_id = id;
|
2015-12-29 22:34:48 +08:00
|
|
|
src->global_id = idm_alloc(&src_ids);
|
2012-08-14 22:25:22 +08:00
|
|
|
src->uc = 0;
|
2015-06-01 05:25:33 +08:00
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_INSERT2(src_hash, RSH, rta_pool, src);
|
2012-08-14 22:25:22 +08:00
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rt_prune_sources(void)
|
|
|
|
{
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_WALK_FILTER(src_hash, next, src, sp)
|
|
|
|
{
|
|
|
|
if (src->uc == 0)
|
2012-08-14 22:25:22 +08:00
|
|
|
{
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_DO_REMOVE(src_hash, RSH, sp);
|
2015-12-29 22:34:48 +08:00
|
|
|
idm_free(&src_ids, src->global_id);
|
2013-11-27 05:37:24 +08:00
|
|
|
sl_free(rte_src_slab, src);
|
2012-08-14 22:25:22 +08:00
|
|
|
}
|
2013-11-27 05:37:24 +08:00
|
|
|
}
|
|
|
|
HASH_WALK_FILTER_END;
|
2012-08-14 22:25:22 +08:00
|
|
|
|
2013-11-27 05:37:24 +08:00
|
|
|
HASH_MAY_RESIZE_DOWN(src_hash, RSH, rta_pool);
|
2012-08-14 22:25:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multipath Next Hop
|
|
|
|
*/
|
|
|
|
|
2015-12-24 22:52:03 +08:00
|
|
|
static inline u32
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_hash(struct nexthop *x)
|
2010-12-08 06:33:55 +08:00
|
|
|
{
|
2015-12-24 22:52:03 +08:00
|
|
|
u32 h = 0;
|
2010-12-08 06:33:55 +08:00
|
|
|
for (; x; x = x->next)
|
|
|
|
h ^= ipa_hash(x->gw);
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop__same(struct nexthop *x, struct nexthop *y)
|
2010-12-08 06:33:55 +08:00
|
|
|
{
|
|
|
|
for (; x && y; x = x->next, y = y->next)
|
|
|
|
if (!ipa_equal(x->gw, y->gw) || (x->iface != y->iface) || (x->weight != y->weight))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return x == y;
|
|
|
|
}
|
|
|
|
|
2015-06-01 05:25:33 +08:00
|
|
|
static int
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_compare_node(struct nexthop *x, struct nexthop *y)
|
2015-06-01 05:25:33 +08:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!x)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!y)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
r = ((int) y->weight) - ((int) x->weight);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = ipa_compare(x->gw, y->gw);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return ((int) x->iface->index) - ((int) y->iface->index);
|
|
|
|
}
|
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
static inline struct nexthop *
|
|
|
|
nexthop_copy_node(const struct nexthop *src, linpool *lp)
|
2015-06-01 05:25:33 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *n = lp_alloc(lp, sizeof(struct nexthop));
|
2015-06-01 05:25:33 +08:00
|
|
|
n->gw = src->gw;
|
|
|
|
n->iface = src->iface;
|
|
|
|
n->next = NULL;
|
|
|
|
n->weight = src->weight;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-06 21:48:35 +08:00
|
|
|
* nexthop_merge - merge nexthop lists
|
2015-06-01 05:25:33 +08:00
|
|
|
* @x: list 1
|
|
|
|
* @y: list 2
|
|
|
|
* @rx: reusability of list @x
|
|
|
|
* @ry: reusability of list @y
|
|
|
|
* @max: max number of nexthops
|
|
|
|
* @lp: linpool for allocating nexthops
|
|
|
|
*
|
2016-05-06 21:48:35 +08:00
|
|
|
* The nexthop_merge() function takes two nexthop lists @x and @y and merges them,
|
2015-06-01 05:25:33 +08:00
|
|
|
* eliminating possible duplicates. The input lists must be sorted and the
|
|
|
|
* result is sorted too. The number of nexthops in result is limited by @max.
|
|
|
|
* New nodes are allocated from linpool @lp.
|
|
|
|
*
|
|
|
|
* The arguments @rx and @ry specify whether corresponding input lists may be
|
|
|
|
* consumed by the function (i.e. their nodes reused in the resulting list), in
|
|
|
|
* that case the caller should not access these lists after that. To eliminate
|
|
|
|
* issues with deallocation of these lists, the caller should use some form of
|
|
|
|
* bulk deallocation (e.g. stack or linpool) to free these nodes when the
|
|
|
|
* resulting list is no longer needed. When reusability is not set, the
|
|
|
|
* corresponding lists are not modified nor linked from the resulting list.
|
|
|
|
*/
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *
|
|
|
|
nexthop_merge(struct nexthop *x, struct nexthop *y, int rx, int ry, int max, linpool *lp)
|
2015-06-01 05:25:33 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *root = NULL;
|
|
|
|
struct nexthop **n = &root;
|
2015-06-01 05:25:33 +08:00
|
|
|
|
|
|
|
while ((x || y) && max--)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
int cmp = nexthop_compare_node(x, y);
|
2015-06-01 05:25:33 +08:00
|
|
|
if (cmp < 0)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
*n = rx ? x : nexthop_copy_node(x, lp);
|
2015-06-01 05:25:33 +08:00
|
|
|
x = x->next;
|
|
|
|
}
|
|
|
|
else if (cmp > 0)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
*n = ry ? y : nexthop_copy_node(y, lp);
|
2015-06-01 05:25:33 +08:00
|
|
|
y = y->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
*n = rx ? x : (ry ? y : nexthop_copy_node(x, lp));
|
2015-06-01 05:25:33 +08:00
|
|
|
x = x->next;
|
|
|
|
y = y->next;
|
|
|
|
}
|
|
|
|
n = &((*n)->next);
|
|
|
|
}
|
|
|
|
*n = NULL;
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:17:27 +08:00
|
|
|
void
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_insert(struct nexthop *n, struct nexthop *x)
|
2016-08-30 23:17:27 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop tmp;
|
|
|
|
memcpy(&tmp, n, sizeof(struct nexthop));
|
|
|
|
if (nexthop_compare_node(n, x) > 0) /* Insert to the included nexthop */
|
2016-08-30 23:17:27 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
memcpy(n, x, sizeof(struct nexthop));
|
|
|
|
memcpy(x, &tmp, sizeof(struct nexthop));
|
|
|
|
n->next = x;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (struct nexthop **nn = &(n->next); *nn; nn = &((*nn)->next))
|
|
|
|
{
|
|
|
|
int cmp = nexthop_compare_node(*nn, x);
|
2016-08-30 23:17:27 +08:00
|
|
|
|
|
|
|
if (cmp < 0)
|
|
|
|
continue;
|
2016-05-06 21:48:35 +08:00
|
|
|
|
|
|
|
if (cmp > 0)
|
|
|
|
{
|
|
|
|
x->next = *nn;
|
|
|
|
*nn = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2016-08-30 23:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_is_sorted(struct nexthop *x)
|
2016-08-30 23:17:27 +08:00
|
|
|
{
|
|
|
|
for (; x && x->next; x = x->next)
|
2016-05-06 21:48:35 +08:00
|
|
|
if (nexthop_compare_node(x, x->next) >= 0)
|
2016-08-30 23:17:27 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2015-06-01 05:25:33 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
static struct nexthop *
|
|
|
|
nexthop_copy(struct nexthop *o)
|
2010-12-08 06:33:55 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *first = NULL;
|
|
|
|
struct nexthop **last = &first;
|
2010-12-08 06:33:55 +08:00
|
|
|
|
|
|
|
for (; o; o = o->next)
|
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *n = sl_alloc(nexthop_slab);
|
2010-12-08 06:33:55 +08:00
|
|
|
n->gw = o->gw;
|
|
|
|
n->iface = o->iface;
|
|
|
|
n->next = NULL;
|
|
|
|
n->weight = o->weight;
|
|
|
|
|
|
|
|
*last = n;
|
|
|
|
last = &(n->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_free(struct nexthop *o)
|
2010-12-08 06:33:55 +08:00
|
|
|
{
|
2016-05-06 21:48:35 +08:00
|
|
|
struct nexthop *n;
|
2010-12-08 06:33:55 +08:00
|
|
|
|
|
|
|
while (o)
|
|
|
|
{
|
|
|
|
n = o->next;
|
2016-05-06 21:48:35 +08:00
|
|
|
sl_free(nexthop_slab, o);
|
2010-12-08 06:33:55 +08:00
|
|
|
o = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-17 21:09:09 +08:00
|
|
|
/*
|
|
|
|
* Extended Attributes
|
|
|
|
*/
|
|
|
|
|
1999-11-18 22:41:29 +08:00
|
|
|
static inline eattr *
|
|
|
|
ea__find(ea_list *e, unsigned id)
|
1999-03-17 21:09:09 +08:00
|
|
|
{
|
|
|
|
eattr *a;
|
|
|
|
int l, r, m;
|
|
|
|
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
if (e->flags & EALF_BISECT)
|
|
|
|
{
|
|
|
|
l = 0;
|
2009-08-21 15:31:35 +08:00
|
|
|
r = e->count - 1;
|
1999-03-17 21:09:09 +08:00
|
|
|
while (l <= r)
|
|
|
|
{
|
|
|
|
m = (l+r) / 2;
|
|
|
|
a = &e->attrs[m];
|
|
|
|
if (a->id == id)
|
|
|
|
return a;
|
|
|
|
else if (a->id < id)
|
|
|
|
l = m+1;
|
|
|
|
else
|
|
|
|
r = m-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for(m=0; m<e->count; m++)
|
|
|
|
if (e->attrs[m].id == id)
|
|
|
|
return &e->attrs[m];
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_find - find an extended attribute
|
|
|
|
* @e: attribute list to search in
|
|
|
|
* @id: attribute ID to search for
|
|
|
|
*
|
|
|
|
* Given an extended attribute list, ea_find() searches for a first
|
2000-06-07 20:29:08 +08:00
|
|
|
* occurrence of an attribute with specified ID, returning either a pointer
|
2000-06-02 20:29:55 +08:00
|
|
|
* to its &eattr structure or %NULL if no such attribute exists.
|
|
|
|
*/
|
1999-11-18 22:41:29 +08:00
|
|
|
eattr *
|
|
|
|
ea_find(ea_list *e, unsigned id)
|
|
|
|
{
|
|
|
|
eattr *a = ea__find(e, id & EA_CODE_MASK);
|
|
|
|
|
|
|
|
if (a && (a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF &&
|
|
|
|
!(id & EA_ALLOW_UNDEF))
|
|
|
|
return NULL;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
KRT: Add support for plenty of kernel route metrics
Linux kernel route metrics (RTA_METRICS netlink route attribute) are
represented and accessible as new route attributes:
krt_mtu, krt_window, krt_rtt, krt_rttvar, krt_sstresh, krt_cwnd, krt_advmss,
krt_reordering, krt_hoplimit, krt_initcwnd, krt_rto_min, krt_initrwnd,
krt_quickack, krt_lock_mtu, krt_lock_window, krt_lock_rtt, krt_lock_rttvar,
krt_lock_sstresh, krt_lock_cwnd, krt_lock_advmss, krt_lock_reordering,
krt_lock_hoplimit, krt_lock_rto_min, krt_feature_ecn, krt_feature_allfrag
2015-05-12 22:42:22 +08:00
|
|
|
/**
|
|
|
|
* ea_walk - walk through extended attributes
|
|
|
|
* @s: walk state structure
|
|
|
|
* @id: start of attribute ID interval
|
|
|
|
* @max: length of attribute ID interval
|
|
|
|
*
|
|
|
|
* Given an extended attribute list, ea_walk() walks through the list looking
|
|
|
|
* for first occurrences of attributes with ID in specified interval from @id to
|
|
|
|
* (@id + @max - 1), returning pointers to found &eattr structures, storing its
|
|
|
|
* walk state in @s for subsequent calls.
|
2016-05-12 21:49:44 +08:00
|
|
|
*
|
KRT: Add support for plenty of kernel route metrics
Linux kernel route metrics (RTA_METRICS netlink route attribute) are
represented and accessible as new route attributes:
krt_mtu, krt_window, krt_rtt, krt_rttvar, krt_sstresh, krt_cwnd, krt_advmss,
krt_reordering, krt_hoplimit, krt_initcwnd, krt_rto_min, krt_initrwnd,
krt_quickack, krt_lock_mtu, krt_lock_window, krt_lock_rtt, krt_lock_rttvar,
krt_lock_sstresh, krt_lock_cwnd, krt_lock_advmss, krt_lock_reordering,
krt_lock_hoplimit, krt_lock_rto_min, krt_feature_ecn, krt_feature_allfrag
2015-05-12 22:42:22 +08:00
|
|
|
* The function ea_walk() is supposed to be called in a loop, with initially
|
|
|
|
* zeroed walk state structure @s with filled the initial extended attribute
|
|
|
|
* list, returning one found attribute in each call or %NULL when no other
|
|
|
|
* attribute exists. The extended attribute list or the arguments should not be
|
|
|
|
* modified between calls. The maximum value of @max is 128.
|
|
|
|
*/
|
|
|
|
eattr *
|
|
|
|
ea_walk(struct ea_walk_state *s, uint id, uint max)
|
|
|
|
{
|
|
|
|
ea_list *e = s->eattrs;
|
|
|
|
eattr *a = s->ea;
|
|
|
|
eattr *a_max;
|
|
|
|
|
|
|
|
max = id + max;
|
|
|
|
|
|
|
|
if (a)
|
|
|
|
goto step;
|
|
|
|
|
|
|
|
for (; e; e = e->next)
|
|
|
|
{
|
|
|
|
if (e->flags & EALF_BISECT)
|
|
|
|
{
|
|
|
|
int l, r, m;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
r = e->count - 1;
|
|
|
|
while (l < r)
|
|
|
|
{
|
|
|
|
m = (l+r) / 2;
|
|
|
|
if (e->attrs[m].id < id)
|
|
|
|
l = m + 1;
|
|
|
|
else
|
|
|
|
r = m;
|
|
|
|
}
|
|
|
|
a = e->attrs + l;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
a = e->attrs;
|
|
|
|
|
|
|
|
step:
|
|
|
|
a_max = e->attrs + e->count;
|
|
|
|
for (; a < a_max; a++)
|
|
|
|
if ((a->id >= id) && (a->id < max))
|
|
|
|
{
|
|
|
|
int n = a->id - id;
|
|
|
|
|
|
|
|
if (BIT32_TEST(s->visited, n))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BIT32_SET(s->visited, n);
|
|
|
|
|
|
|
|
if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s->eattrs = e;
|
|
|
|
s->ea = a;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
else if (e->flags & EALF_BISECT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_get_int - fetch an integer attribute
|
|
|
|
* @e: attribute list
|
|
|
|
* @id: attribute ID
|
|
|
|
* @def: default value
|
|
|
|
*
|
|
|
|
* This function is a shortcut for retrieving a value of an integer attribute
|
|
|
|
* by calling ea_find() to find the attribute, extracting its value or returning
|
|
|
|
* a provided default if no such attribute is present.
|
|
|
|
*/
|
2000-05-10 14:54:40 +08:00
|
|
|
int
|
|
|
|
ea_get_int(ea_list *e, unsigned id, int def)
|
|
|
|
{
|
|
|
|
eattr *a = ea_find(e, id);
|
|
|
|
if (!a)
|
|
|
|
return def;
|
|
|
|
return a->u.data;
|
|
|
|
}
|
|
|
|
|
1999-03-17 21:09:09 +08:00
|
|
|
static inline void
|
|
|
|
ea_do_sort(ea_list *e)
|
|
|
|
{
|
|
|
|
unsigned n = e->count;
|
|
|
|
eattr *a = e->attrs;
|
|
|
|
eattr *b = alloca(n * sizeof(eattr));
|
|
|
|
unsigned s, ss;
|
|
|
|
|
|
|
|
/* We need to use a stable sorting algorithm, hence mergesort */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
s = ss = 0;
|
|
|
|
while (s < n)
|
|
|
|
{
|
|
|
|
eattr *p, *q, *lo, *hi;
|
|
|
|
p = b;
|
|
|
|
ss = s;
|
|
|
|
*p++ = a[s++];
|
|
|
|
while (s < n && p[-1].id <= a[s].id)
|
|
|
|
*p++ = a[s++];
|
|
|
|
if (s < n)
|
|
|
|
{
|
|
|
|
q = p;
|
|
|
|
*p++ = a[s++];
|
|
|
|
while (s < n && p[-1].id <= a[s].id)
|
|
|
|
*p++ = a[s++];
|
|
|
|
lo = b;
|
|
|
|
hi = q;
|
|
|
|
s = ss;
|
|
|
|
while (lo < q && hi < p)
|
|
|
|
if (lo->id <= hi->id)
|
|
|
|
a[s++] = *lo++;
|
|
|
|
else
|
|
|
|
a[s++] = *hi++;
|
|
|
|
while (lo < q)
|
|
|
|
a[s++] = *lo++;
|
|
|
|
while (hi < p)
|
|
|
|
a[s++] = *hi++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
ea_do_prune(ea_list *e)
|
|
|
|
{
|
2000-04-17 19:23:05 +08:00
|
|
|
eattr *s, *d, *l, *s0;
|
1999-11-18 22:41:29 +08:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
/* Discard duplicates and undefs. Do you remember sorting was stable? */
|
|
|
|
s = d = e->attrs;
|
|
|
|
l = e->attrs + e->count;
|
|
|
|
while (s < l)
|
|
|
|
{
|
2000-04-17 19:23:05 +08:00
|
|
|
s0 = s++;
|
|
|
|
while (s < l && s->id == s[-1].id)
|
|
|
|
s++;
|
|
|
|
/* s0 is the most recent version, s[-1] the oldest one */
|
|
|
|
if ((s0->type & EAF_TYPE_MASK) != EAF_TYPE_UNDEF)
|
1999-11-18 22:41:29 +08:00
|
|
|
{
|
2000-04-17 19:23:05 +08:00
|
|
|
*d = *s0;
|
2016-12-07 21:11:28 +08:00
|
|
|
d->type = (d->type & ~(EAF_ORIGINATED|EAF_FRESH)) | (s[-1].type & EAF_ORIGINATED);
|
2000-04-17 19:23:05 +08:00
|
|
|
d++;
|
1999-11-18 22:41:29 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e->count = i;
|
1999-03-17 21:09:09 +08:00
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_sort - sort an attribute list
|
|
|
|
* @e: list to be sorted
|
|
|
|
*
|
|
|
|
* This function takes a &ea_list chain and sorts the attributes
|
|
|
|
* within each of its entries.
|
|
|
|
*
|
|
|
|
* If an attribute occurs multiple times in a single &ea_list,
|
2000-06-07 20:29:08 +08:00
|
|
|
* ea_sort() leaves only the first (the only significant) occurrence.
|
2000-06-02 20:29:55 +08:00
|
|
|
*/
|
1999-03-17 21:09:09 +08:00
|
|
|
void
|
|
|
|
ea_sort(ea_list *e)
|
|
|
|
{
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
if (!(e->flags & EALF_SORTED))
|
|
|
|
{
|
|
|
|
ea_do_sort(e);
|
|
|
|
ea_do_prune(e);
|
|
|
|
e->flags |= EALF_SORTED;
|
|
|
|
}
|
|
|
|
if (e->count > 5)
|
|
|
|
e->flags |= EALF_BISECT;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_scan - estimate attribute list size
|
|
|
|
* @e: attribute list
|
|
|
|
*
|
|
|
|
* This function calculates an upper bound of the size of
|
|
|
|
* a given &ea_list after merging with ea_merge().
|
|
|
|
*/
|
1999-03-17 21:09:09 +08:00
|
|
|
unsigned
|
|
|
|
ea_scan(ea_list *e)
|
|
|
|
{
|
|
|
|
unsigned cnt = 0;
|
|
|
|
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
cnt += e->count;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
return sizeof(ea_list) + sizeof(eattr)*cnt;
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_merge - merge segments of an attribute list
|
|
|
|
* @e: attribute list
|
|
|
|
* @t: buffer to store the result to
|
|
|
|
*
|
|
|
|
* This function takes a possibly multi-segment attribute list
|
|
|
|
* and merges all of its segments to one.
|
|
|
|
*
|
|
|
|
* The primary use of this function is for &ea_list normalization:
|
|
|
|
* first call ea_scan() to determine how much memory will the result
|
|
|
|
* take, then allocate a buffer (usually using alloca()), merge the
|
|
|
|
* segments with ea_merge() and finally sort and prune the result
|
|
|
|
* by calling ea_sort().
|
|
|
|
*/
|
1999-03-17 21:09:09 +08:00
|
|
|
void
|
|
|
|
ea_merge(ea_list *e, ea_list *t)
|
|
|
|
{
|
|
|
|
eattr *d = t->attrs;
|
|
|
|
|
|
|
|
t->flags = 0;
|
|
|
|
t->count = 0;
|
|
|
|
t->next = NULL;
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
memcpy(d, e->attrs, sizeof(eattr)*e->count);
|
|
|
|
t->count += e->count;
|
|
|
|
d += e->count;
|
|
|
|
e = e->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_same - compare two &ea_list's
|
|
|
|
* @x: attribute list
|
|
|
|
* @y: attribute list
|
|
|
|
*
|
|
|
|
* ea_same() compares two normalized attribute lists @x and @y and returns
|
|
|
|
* 1 if they contain the same attributes, 0 otherwise.
|
|
|
|
*/
|
2000-04-10 20:39:29 +08:00
|
|
|
int
|
1998-05-20 19:54:33 +08:00
|
|
|
ea_same(ea_list *x, ea_list *y)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
1999-03-17 21:09:09 +08:00
|
|
|
if (!x || !y)
|
|
|
|
return x == y;
|
|
|
|
ASSERT(!x->next && !y->next);
|
|
|
|
if (x->count != y->count)
|
|
|
|
return 0;
|
|
|
|
for(c=0; c<x->count; c++)
|
1998-05-20 19:54:33 +08:00
|
|
|
{
|
1999-03-17 21:09:09 +08:00
|
|
|
eattr *a = &x->attrs[c];
|
|
|
|
eattr *b = &y->attrs[c];
|
|
|
|
|
|
|
|
if (a->id != b->id ||
|
|
|
|
a->flags != b->flags ||
|
|
|
|
a->type != b->type ||
|
2013-10-02 20:41:37 +08:00
|
|
|
((a->type & EAF_EMBEDDED) ? a->u.data != b->u.data : !adata_same(a->u.ptr, b->u.ptr)))
|
1998-05-20 19:54:33 +08:00
|
|
|
return 0;
|
1999-03-17 21:09:09 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ea_list *
|
|
|
|
ea_list_copy(ea_list *o)
|
|
|
|
{
|
|
|
|
ea_list *n;
|
|
|
|
unsigned i, len;
|
|
|
|
|
|
|
|
if (!o)
|
|
|
|
return NULL;
|
|
|
|
ASSERT(!o->next);
|
|
|
|
len = sizeof(ea_list) + sizeof(eattr) * o->count;
|
|
|
|
n = mb_alloc(rta_pool, len);
|
|
|
|
memcpy(n, o, len);
|
|
|
|
n->flags |= EALF_CACHED;
|
|
|
|
for(i=0; i<o->count; i++)
|
|
|
|
{
|
|
|
|
eattr *a = &n->attrs[i];
|
2000-04-01 07:21:19 +08:00
|
|
|
if (!(a->type & EAF_EMBEDDED))
|
1999-03-17 21:09:09 +08:00
|
|
|
{
|
|
|
|
unsigned size = sizeof(struct adata) + a->u.ptr->length;
|
|
|
|
struct adata *d = mb_alloc(rta_pool, size);
|
|
|
|
memcpy(d, a->u.ptr, size);
|
|
|
|
a->u.ptr = d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2000-05-08 21:12:14 +08:00
|
|
|
static inline void
|
|
|
|
ea_free(ea_list *o)
|
|
|
|
{
|
2000-05-20 02:05:01 +08:00
|
|
|
int i;
|
|
|
|
|
2000-05-08 21:12:14 +08:00
|
|
|
if (o)
|
|
|
|
{
|
|
|
|
ASSERT(!o->next);
|
2000-05-20 02:05:01 +08:00
|
|
|
for(i=0; i<o->count; i++)
|
|
|
|
{
|
|
|
|
eattr *a = &o->attrs[i];
|
|
|
|
if (!(a->type & EAF_EMBEDDED))
|
|
|
|
mb_free(a->u.ptr);
|
|
|
|
}
|
2000-05-08 21:12:14 +08:00
|
|
|
mb_free(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-02 19:11:53 +08:00
|
|
|
static int
|
|
|
|
get_generic_attr(eattr *a, byte **buf, int buflen UNUSED)
|
|
|
|
{
|
|
|
|
if (a->id == EA_GEN_IGP_METRIC)
|
|
|
|
{
|
|
|
|
*buf += bsprintf(*buf, "igp_metric");
|
|
|
|
return GA_NAME;
|
|
|
|
}
|
2015-06-01 05:25:33 +08:00
|
|
|
|
2010-08-02 19:11:53 +08:00
|
|
|
return GA_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-05-10 00:50:15 +08:00
|
|
|
void
|
|
|
|
ea_format_bitfield(struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
|
|
|
|
{
|
|
|
|
byte *bound = buf + bufsize - 32;
|
|
|
|
u32 data = a->u.data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = min; i < max; i++)
|
|
|
|
if ((data & (1u << i)) && names[i])
|
|
|
|
{
|
|
|
|
if (buf > bound)
|
|
|
|
{
|
|
|
|
strcpy(buf, " ...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf += bsprintf(buf, " %s", names[i]);
|
|
|
|
data &= ~(1u << i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
bsprintf(buf, " %08x", data);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-04 01:43:30 +08:00
|
|
|
static inline void
|
2015-05-19 14:53:34 +08:00
|
|
|
opaque_format(struct adata *ad, byte *buf, uint size)
|
2011-07-04 01:43:30 +08:00
|
|
|
{
|
|
|
|
byte *bound = buf + size - 10;
|
2016-10-14 21:37:04 +08:00
|
|
|
uint i;
|
2011-07-04 01:43:30 +08:00
|
|
|
|
|
|
|
for(i = 0; i < ad->length; i++)
|
|
|
|
{
|
|
|
|
if (buf > bound)
|
|
|
|
{
|
|
|
|
strcpy(buf, " ...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i)
|
|
|
|
*buf++ = ' ';
|
|
|
|
|
|
|
|
buf += bsprintf(buf, "%02x", ad->data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
ea_show_int_set(struct cli *c, struct adata *ad, int way, byte *pos, byte *buf, byte *end)
|
|
|
|
{
|
|
|
|
int i = int_set_format(ad, way, 0, pos, end - pos);
|
2011-08-16 02:54:58 +08:00
|
|
|
cli_printf(c, -1012, "\t%s", buf);
|
2011-07-04 01:43:30 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
i = int_set_format(ad, way, i, buf, end - buf - 1);
|
2011-08-16 02:54:58 +08:00
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
2011-07-04 01:43:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-13 03:03:43 +08:00
|
|
|
static inline void
|
|
|
|
ea_show_ec_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
|
|
|
|
{
|
|
|
|
int i = ec_set_format(ad, 0, pos, end - pos);
|
2011-08-16 02:54:58 +08:00
|
|
|
cli_printf(c, -1012, "\t%s", buf);
|
2011-08-13 03:03:43 +08:00
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
i = ec_set_format(ad, i, buf, end - buf - 1);
|
2011-08-16 02:54:58 +08:00
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
2011-08-13 03:03:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 18:50:29 +08:00
|
|
|
static inline void
|
|
|
|
ea_show_lc_set(struct cli *c, struct adata *ad, byte *pos, byte *buf, byte *end)
|
|
|
|
{
|
|
|
|
int i = lc_set_format(ad, 0, pos, end - pos);
|
|
|
|
cli_printf(c, -1012, "\t%s", buf);
|
|
|
|
while (i)
|
|
|
|
{
|
|
|
|
i = lc_set_format(ad, i, buf, end - buf - 1);
|
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
2011-07-04 01:43:30 +08:00
|
|
|
* ea_show - print an &eattr to CLI
|
|
|
|
* @c: destination CLI
|
|
|
|
* @e: attribute to be printed
|
2000-06-02 20:29:55 +08:00
|
|
|
*
|
2011-07-04 01:43:30 +08:00
|
|
|
* This function takes an extended attribute represented by its &eattr
|
|
|
|
* structure and prints it to the CLI according to the type information.
|
2000-06-02 20:29:55 +08:00
|
|
|
*
|
|
|
|
* If the protocol defining the attribute provides its own
|
|
|
|
* get_attr() hook, it's consulted first.
|
|
|
|
*/
|
2000-04-01 18:19:47 +08:00
|
|
|
void
|
2011-07-04 01:43:30 +08:00
|
|
|
ea_show(struct cli *c, eattr *e)
|
2000-04-01 18:19:47 +08:00
|
|
|
{
|
|
|
|
struct protocol *p;
|
|
|
|
int status = GA_UNKNOWN;
|
|
|
|
struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
|
2011-07-04 01:43:30 +08:00
|
|
|
byte buf[CLI_MSG_SIZE];
|
|
|
|
byte *pos = buf, *end = buf + sizeof(buf);
|
2000-04-01 18:19:47 +08:00
|
|
|
|
|
|
|
if (p = attr_class_to_protocol[EA_PROTO(e->id)])
|
|
|
|
{
|
2011-07-04 01:43:30 +08:00
|
|
|
pos += bsprintf(pos, "%s.", p->name);
|
2000-04-01 18:19:47 +08:00
|
|
|
if (p->get_attr)
|
2011-07-04 01:43:30 +08:00
|
|
|
status = p->get_attr(e, pos, end - pos);
|
|
|
|
pos += strlen(pos);
|
2000-04-01 18:19:47 +08:00
|
|
|
}
|
|
|
|
else if (EA_PROTO(e->id))
|
2011-07-04 01:43:30 +08:00
|
|
|
pos += bsprintf(pos, "%02x.", EA_PROTO(e->id));
|
2015-06-01 05:25:33 +08:00
|
|
|
else
|
2011-07-04 01:43:30 +08:00
|
|
|
status = get_generic_attr(e, &pos, end - pos);
|
2010-08-02 19:11:53 +08:00
|
|
|
|
2000-04-01 18:19:47 +08:00
|
|
|
if (status < GA_NAME)
|
2011-07-04 01:43:30 +08:00
|
|
|
pos += bsprintf(pos, "%02x", EA_ID(e->id));
|
2000-04-01 18:19:47 +08:00
|
|
|
if (status < GA_FULL)
|
|
|
|
{
|
2011-07-04 01:43:30 +08:00
|
|
|
*pos++ = ':';
|
|
|
|
*pos++ = ' ';
|
2000-04-01 18:19:47 +08:00
|
|
|
switch (e->type & EAF_TYPE_MASK)
|
|
|
|
{
|
|
|
|
case EAF_TYPE_INT:
|
2011-07-04 01:43:30 +08:00
|
|
|
bsprintf(pos, "%u", e->u.data);
|
2000-04-01 18:19:47 +08:00
|
|
|
break;
|
|
|
|
case EAF_TYPE_OPAQUE:
|
2011-07-04 01:43:30 +08:00
|
|
|
opaque_format(ad, pos, end - pos);
|
2000-04-01 18:19:47 +08:00
|
|
|
break;
|
|
|
|
case EAF_TYPE_IP_ADDRESS:
|
2011-07-04 01:43:30 +08:00
|
|
|
bsprintf(pos, "%I", *(ip_addr *) ad->data);
|
2000-04-01 18:19:47 +08:00
|
|
|
break;
|
|
|
|
case EAF_TYPE_ROUTER_ID:
|
2011-07-04 01:43:30 +08:00
|
|
|
bsprintf(pos, "%R", e->u.data);
|
2000-04-01 18:19:47 +08:00
|
|
|
break;
|
2000-04-17 18:18:55 +08:00
|
|
|
case EAF_TYPE_AS_PATH:
|
2011-07-04 01:43:30 +08:00
|
|
|
as_path_format(ad, pos, end - pos);
|
2000-04-17 18:18:55 +08:00
|
|
|
break;
|
2015-05-10 00:50:15 +08:00
|
|
|
case EAF_TYPE_BITFIELD:
|
|
|
|
bsprintf(pos, "%08x", e->u.data);
|
|
|
|
break;
|
2000-04-17 18:18:55 +08:00
|
|
|
case EAF_TYPE_INT_SET:
|
2011-07-04 01:43:30 +08:00
|
|
|
ea_show_int_set(c, ad, 1, pos, buf, end);
|
|
|
|
return;
|
2011-08-13 03:03:43 +08:00
|
|
|
case EAF_TYPE_EC_SET:
|
|
|
|
ea_show_ec_set(c, ad, pos, buf, end);
|
|
|
|
return;
|
2016-10-01 18:50:29 +08:00
|
|
|
case EAF_TYPE_LC_SET:
|
|
|
|
ea_show_lc_set(c, ad, pos, buf, end);
|
|
|
|
return;
|
2000-04-01 18:19:47 +08:00
|
|
|
case EAF_TYPE_UNDEF:
|
|
|
|
default:
|
2011-07-04 01:43:30 +08:00
|
|
|
bsprintf(pos, "<type %02x>", e->type);
|
2000-04-01 18:19:47 +08:00
|
|
|
}
|
|
|
|
}
|
2011-08-16 02:54:58 +08:00
|
|
|
cli_printf(c, -1012, "\t%s", buf);
|
2000-04-01 18:19:47 +08:00
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_dump - dump an extended attribute
|
|
|
|
* @e: attribute to be dumped
|
|
|
|
*
|
|
|
|
* ea_dump() dumps contents of the extended attribute given to
|
|
|
|
* the debug output.
|
|
|
|
*/
|
1999-03-17 21:09:09 +08:00
|
|
|
void
|
|
|
|
ea_dump(ea_list *e)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!e)
|
|
|
|
{
|
|
|
|
debug("NONE");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
debug("[%c%c%c]",
|
|
|
|
(e->flags & EALF_SORTED) ? 'S' : 's',
|
|
|
|
(e->flags & EALF_BISECT) ? 'B' : 'b',
|
|
|
|
(e->flags & EALF_CACHED) ? 'C' : 'c');
|
|
|
|
for(i=0; i<e->count; i++)
|
1998-05-20 19:54:33 +08:00
|
|
|
{
|
1999-03-17 21:09:09 +08:00
|
|
|
eattr *a = &e->attrs[i];
|
|
|
|
debug(" %02x:%02x.%02x", EA_PROTO(a->id), EA_ID(a->id), a->flags);
|
2000-03-05 06:30:44 +08:00
|
|
|
if (a->type & EAF_TEMP)
|
|
|
|
debug("T");
|
1999-03-17 21:09:09 +08:00
|
|
|
debug("=%c", "?iO?I?P???S?????" [a->type & EAF_TYPE_MASK]);
|
2000-04-17 19:23:05 +08:00
|
|
|
if (a->type & EAF_ORIGINATED)
|
|
|
|
debug("o");
|
1999-03-17 21:09:09 +08:00
|
|
|
if (a->type & EAF_EMBEDDED)
|
|
|
|
debug(":%08x", a->u.data);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int j, len = a->u.ptr->length;
|
|
|
|
debug("[%d]:", len);
|
|
|
|
for(j=0; j<len; j++)
|
|
|
|
debug("%02x", a->u.ptr->data[j]);
|
|
|
|
}
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
1999-03-17 21:09:09 +08:00
|
|
|
if (e = e->next)
|
|
|
|
debug(" | ");
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_hash - calculate an &ea_list hash key
|
|
|
|
* @e: attribute list
|
|
|
|
*
|
|
|
|
* ea_hash() takes an extended attribute list and calculated a hopefully
|
|
|
|
* uniformly distributed hash value from its contents.
|
|
|
|
*/
|
2015-05-19 14:53:34 +08:00
|
|
|
inline uint
|
2000-03-05 05:09:14 +08:00
|
|
|
ea_hash(ea_list *e)
|
|
|
|
{
|
2016-02-10 20:26:07 +08:00
|
|
|
const u64 mul = 0x68576150f3d6847;
|
|
|
|
u64 h = 0xafcef24eda8b29;
|
2000-03-05 05:09:14 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (e) /* Assuming chain of length 1 */
|
|
|
|
{
|
|
|
|
for(i=0; i<e->count; i++)
|
|
|
|
{
|
|
|
|
struct eattr *a = &e->attrs[i];
|
2016-02-10 20:26:07 +08:00
|
|
|
h ^= a->id; h *= mul;
|
2000-03-05 05:09:14 +08:00
|
|
|
if (a->type & EAF_EMBEDDED)
|
|
|
|
h ^= a->u.data;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct adata *d = a->u.ptr;
|
2016-02-10 20:26:07 +08:00
|
|
|
h ^= mem_hash(d->data, d->length);
|
2000-03-05 05:09:14 +08:00
|
|
|
}
|
2016-02-10 20:26:07 +08:00
|
|
|
h *= mul;
|
2000-03-05 05:09:14 +08:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 20:26:07 +08:00
|
|
|
return (h >> 32) ^ (h & 0xffffffff);
|
2000-03-05 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* ea_append - concatenate &ea_list's
|
|
|
|
* @to: destination list (can be %NULL)
|
|
|
|
* @what: list to be appended (can be %NULL)
|
|
|
|
*
|
|
|
|
* This function appends the &ea_list @what at the end of
|
|
|
|
* &ea_list @to and returns a pointer to the resulting list.
|
|
|
|
*/
|
2000-05-07 06:57:39 +08:00
|
|
|
ea_list *
|
|
|
|
ea_append(ea_list *to, ea_list *what)
|
|
|
|
{
|
|
|
|
ea_list *res;
|
|
|
|
|
|
|
|
if (!to)
|
|
|
|
return what;
|
|
|
|
res = to;
|
|
|
|
while (to->next)
|
|
|
|
to = to->next;
|
|
|
|
to->next = what;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-03-17 21:09:09 +08:00
|
|
|
/*
|
|
|
|
* rta's
|
|
|
|
*/
|
|
|
|
|
2015-05-19 14:53:34 +08:00
|
|
|
static uint rta_cache_count;
|
|
|
|
static uint rta_cache_size = 32;
|
|
|
|
static uint rta_cache_limit;
|
|
|
|
static uint rta_cache_mask;
|
2000-03-05 05:09:14 +08:00
|
|
|
static rta **rta_hash_table;
|
|
|
|
|
|
|
|
static void
|
|
|
|
rta_alloc_hash(void)
|
|
|
|
{
|
2000-05-08 21:12:14 +08:00
|
|
|
rta_hash_table = mb_allocz(rta_pool, sizeof(rta *) * rta_cache_size);
|
2000-03-05 05:09:14 +08:00
|
|
|
if (rta_cache_size < 32768)
|
|
|
|
rta_cache_limit = rta_cache_size * 2;
|
|
|
|
else
|
|
|
|
rta_cache_limit = ~0;
|
|
|
|
rta_cache_mask = rta_cache_size - 1;
|
|
|
|
}
|
|
|
|
|
2015-05-19 14:53:34 +08:00
|
|
|
static inline uint
|
2000-03-05 05:09:14 +08:00
|
|
|
rta_hash(rta *a)
|
|
|
|
{
|
2016-05-13 19:46:46 +08:00
|
|
|
u64 h;
|
2016-05-12 22:16:25 +08:00
|
|
|
mem_hash_init(&h);
|
2016-05-13 19:46:46 +08:00
|
|
|
#define MIX(f) mem_hash_mix(&h, &(a->f), sizeof(a->f));
|
2016-05-12 22:16:25 +08:00
|
|
|
MIX(src);
|
|
|
|
MIX(hostentry);
|
|
|
|
MIX(from);
|
|
|
|
MIX(igp_metric);
|
2016-05-06 21:48:35 +08:00
|
|
|
mem_hash_mix(&h, a->bf, sizeof(u32));
|
2016-05-13 19:46:46 +08:00
|
|
|
#undef MIX
|
2016-05-12 22:16:25 +08:00
|
|
|
|
2016-05-06 21:48:35 +08:00
|
|
|
return mem_hash_value(&h) ^ nexthop_hash(&(a->nh)) ^ ea_hash(a->eattrs);
|
2000-03-05 05:09:14 +08:00
|
|
|
}
|
|
|
|
|
1998-05-20 19:54:33 +08:00
|
|
|
static inline int
|
|
|
|
rta_same(rta *x, rta *y)
|
|
|
|
{
|
2012-08-14 22:25:22 +08:00
|
|
|
return (x->src == y->src &&
|
1998-05-20 19:54:33 +08:00
|
|
|
x->source == y->source &&
|
|
|
|
x->scope == y->scope &&
|
|
|
|
x->cast == y->cast &&
|
|
|
|
x->dest == y->dest &&
|
2010-07-31 07:04:32 +08:00
|
|
|
x->igp_metric == y->igp_metric &&
|
2016-05-06 21:48:35 +08:00
|
|
|
ipa_equal(x->nh.gw, y->nh.gw) &&
|
1998-05-20 19:54:33 +08:00
|
|
|
ipa_equal(x->from, y->from) &&
|
2016-05-06 21:48:35 +08:00
|
|
|
x->nh.iface == y->nh.iface &&
|
2010-07-31 07:04:32 +08:00
|
|
|
x->hostentry == y->hostentry &&
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_same(&(x->nh), &(y->nh)) &&
|
1999-11-04 21:29:43 +08:00
|
|
|
ea_same(x->eattrs, y->eattrs));
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rta *
|
|
|
|
rta_copy(rta *o)
|
|
|
|
{
|
|
|
|
rta *r = sl_alloc(rta_slab);
|
|
|
|
|
|
|
|
memcpy(r, o, sizeof(rta));
|
|
|
|
r->uc = 1;
|
2016-05-06 21:48:35 +08:00
|
|
|
r->nh.next = nexthop_copy(o->nh.next);
|
1999-11-04 21:29:43 +08:00
|
|
|
r->eattrs = ea_list_copy(o->eattrs);
|
1998-05-20 19:54:33 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2000-03-05 05:09:14 +08:00
|
|
|
static inline void
|
|
|
|
rta_insert(rta *r)
|
|
|
|
{
|
2015-05-19 14:53:34 +08:00
|
|
|
uint h = r->hash_key & rta_cache_mask;
|
2000-03-05 05:09:14 +08:00
|
|
|
r->next = rta_hash_table[h];
|
|
|
|
if (r->next)
|
|
|
|
r->next->pprev = &r->next;
|
|
|
|
r->pprev = &rta_hash_table[h];
|
|
|
|
rta_hash_table[h] = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rta_rehash(void)
|
|
|
|
{
|
2015-05-19 14:53:34 +08:00
|
|
|
uint ohs = rta_cache_size;
|
|
|
|
uint h;
|
2000-03-05 05:09:14 +08:00
|
|
|
rta *r, *n;
|
|
|
|
rta **oht = rta_hash_table;
|
|
|
|
|
|
|
|
rta_cache_size = 2*rta_cache_size;
|
|
|
|
DBG("Rehashing rta cache from %d to %d entries.\n", ohs, rta_cache_size);
|
|
|
|
rta_alloc_hash();
|
|
|
|
for(h=0; h<ohs; h++)
|
|
|
|
for(r=oht[h]; r; r=n)
|
|
|
|
{
|
|
|
|
n = r->next;
|
|
|
|
rta_insert(r);
|
|
|
|
}
|
|
|
|
mb_free(oht);
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* rta_lookup - look up a &rta in attribute cache
|
2000-06-07 20:29:08 +08:00
|
|
|
* @o: a un-cached &rta
|
2000-06-02 20:29:55 +08:00
|
|
|
*
|
2000-06-07 20:29:08 +08:00
|
|
|
* rta_lookup() gets an un-cached &rta structure and returns its cached
|
2000-06-02 20:29:55 +08:00
|
|
|
* counterpart. It starts with examining the attribute cache to see whether
|
|
|
|
* there exists a matching entry. If such an entry exists, it's returned and
|
|
|
|
* its use count is incremented, else a new entry is created with use count
|
|
|
|
* set to 1.
|
|
|
|
*
|
|
|
|
* The extended attribute lists attached to the &rta are automatically
|
|
|
|
* converted to the normalized form.
|
|
|
|
*/
|
1998-05-20 19:54:33 +08:00
|
|
|
rta *
|
|
|
|
rta_lookup(rta *o)
|
|
|
|
{
|
|
|
|
rta *r;
|
2015-05-19 14:53:34 +08:00
|
|
|
uint h;
|
1998-05-20 19:54:33 +08:00
|
|
|
|
1999-03-17 21:09:09 +08:00
|
|
|
ASSERT(!(o->aflags & RTAF_CACHED));
|
1999-11-04 21:29:43 +08:00
|
|
|
if (o->eattrs)
|
1999-03-17 21:09:09 +08:00
|
|
|
{
|
1999-11-04 21:29:43 +08:00
|
|
|
if (o->eattrs->next) /* Multiple ea_list's, need to merge them */
|
1999-03-17 21:09:09 +08:00
|
|
|
{
|
1999-11-04 21:29:43 +08:00
|
|
|
ea_list *ml = alloca(ea_scan(o->eattrs));
|
|
|
|
ea_merge(o->eattrs, ml);
|
|
|
|
o->eattrs = ml;
|
1999-03-17 21:09:09 +08:00
|
|
|
}
|
1999-11-04 21:29:43 +08:00
|
|
|
ea_sort(o->eattrs);
|
1999-03-17 21:09:09 +08:00
|
|
|
}
|
|
|
|
|
2000-03-05 05:09:14 +08:00
|
|
|
h = rta_hash(o);
|
|
|
|
for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next)
|
|
|
|
if (r->hash_key == h && rta_same(r, o))
|
1998-05-20 19:54:33 +08:00
|
|
|
return rta_clone(r);
|
1999-03-17 21:09:09 +08:00
|
|
|
|
1998-05-20 19:54:33 +08:00
|
|
|
r = rta_copy(o);
|
2000-03-05 05:09:14 +08:00
|
|
|
r->hash_key = h;
|
1998-12-08 05:59:15 +08:00
|
|
|
r->aflags = RTAF_CACHED;
|
2012-08-14 22:25:22 +08:00
|
|
|
rt_lock_source(r->src);
|
2010-07-05 23:50:19 +08:00
|
|
|
rt_lock_hostentry(r->hostentry);
|
2000-03-05 05:09:14 +08:00
|
|
|
rta_insert(r);
|
|
|
|
|
|
|
|
if (++rta_cache_count > rta_cache_limit)
|
|
|
|
rta_rehash();
|
|
|
|
|
1998-05-20 19:54:33 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-03-17 21:09:09 +08:00
|
|
|
rta__free(rta *a)
|
1998-05-20 19:54:33 +08:00
|
|
|
{
|
2000-03-05 05:09:14 +08:00
|
|
|
ASSERT(rta_cache_count && (a->aflags & RTAF_CACHED));
|
|
|
|
rta_cache_count--;
|
2000-05-08 21:12:14 +08:00
|
|
|
*a->pprev = a->next;
|
|
|
|
if (a->next)
|
|
|
|
a->next->pprev = a->pprev;
|
|
|
|
a->aflags = 0; /* Poison the entry */
|
2010-07-05 23:50:19 +08:00
|
|
|
rt_unlock_hostentry(a->hostentry);
|
2012-08-14 22:25:22 +08:00
|
|
|
rt_unlock_source(a->src);
|
2016-05-06 21:48:35 +08:00
|
|
|
if (a->nh.next)
|
|
|
|
nexthop_free(a->nh.next);
|
2000-05-08 21:12:14 +08:00
|
|
|
ea_free(a->eattrs);
|
|
|
|
sl_free(rta_slab, a);
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
|
|
|
|
2015-06-08 08:20:43 +08:00
|
|
|
rta *
|
|
|
|
rta_do_cow(rta *o, linpool *lp)
|
|
|
|
{
|
|
|
|
rta *r = lp_alloc(lp, sizeof(rta));
|
|
|
|
memcpy(r, o, sizeof(rta));
|
|
|
|
r->aflags = 0;
|
|
|
|
r->uc = 0;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* rta_dump - dump route attributes
|
|
|
|
* @a: attribute structure to dump
|
|
|
|
*
|
|
|
|
* This function takes a &rta and dumps its contents to the debug output.
|
|
|
|
*/
|
1998-05-20 19:54:33 +08:00
|
|
|
void
|
1998-06-05 04:29:05 +08:00
|
|
|
rta_dump(rta *a)
|
1998-05-20 19:54:33 +08:00
|
|
|
{
|
1998-12-09 02:30:35 +08:00
|
|
|
static char *rts[] = { "RTS_DUMMY", "RTS_STATIC", "RTS_INHERIT", "RTS_DEVICE",
|
2000-05-05 05:23:10 +08:00
|
|
|
"RTS_STAT_DEV", "RTS_REDIR", "RTS_RIP",
|
2004-06-26 00:39:53 +08:00
|
|
|
"RTS_OSPF", "RTS_OSPF_IA", "RTS_OSPF_EXT1",
|
2016-07-19 18:16:51 +08:00
|
|
|
"RTS_OSPF_EXT2", "RTS_BGP", "RTS_PIPE", "RTS_BABEL" };
|
1998-06-05 04:29:05 +08:00
|
|
|
static char *rtc[] = { "", " BC", " MC", " AC" };
|
|
|
|
static char *rtd[] = { "", " DEV", " HOLE", " UNREACH", " PROHIBIT" };
|
|
|
|
|
2000-03-05 05:09:14 +08:00
|
|
|
debug("p=%s uc=%d %s %s%s%s h=%04x",
|
2012-08-14 22:25:22 +08:00
|
|
|
a->src->proto->name, a->uc, rts[a->source], ip_scope_text(a->scope), rtc[a->cast],
|
2000-03-05 05:09:14 +08:00
|
|
|
rtd[a->dest], a->hash_key);
|
1998-12-08 05:59:15 +08:00
|
|
|
if (!(a->aflags & RTAF_CACHED))
|
|
|
|
debug(" !CACHED");
|
1998-06-17 22:36:02 +08:00
|
|
|
debug(" <-%I", a->from);
|
2016-05-06 21:48:35 +08:00
|
|
|
if (a->dest == RTD_UNICAST)
|
|
|
|
for (struct nexthop *nh = &(a->nh); nh; nh = nh->next)
|
|
|
|
{
|
|
|
|
if (ipa_nonzero(nh->gw)) debug(" ->%I", nh->gw);
|
|
|
|
debug(" [%s]", nh->iface ? nh->iface->name : "???");
|
|
|
|
}
|
1999-11-04 21:29:43 +08:00
|
|
|
if (a->eattrs)
|
1999-03-17 21:09:09 +08:00
|
|
|
{
|
|
|
|
debug(" EA: ");
|
1999-11-04 21:29:43 +08:00
|
|
|
ea_dump(a->eattrs);
|
1999-03-17 21:09:09 +08:00
|
|
|
}
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* rta_dump_all - dump attribute cache
|
|
|
|
*
|
|
|
|
* This function dumps the whole contents of route attribute cache
|
|
|
|
* to the debug output.
|
|
|
|
*/
|
1998-05-20 19:54:33 +08:00
|
|
|
void
|
|
|
|
rta_dump_all(void)
|
|
|
|
{
|
1998-06-05 04:29:05 +08:00
|
|
|
rta *a;
|
2015-05-19 14:53:34 +08:00
|
|
|
uint h;
|
2000-03-05 05:09:14 +08:00
|
|
|
|
|
|
|
debug("Route attribute cache (%d entries, rehash at %d):\n", rta_cache_count, rta_cache_limit);
|
|
|
|
for(h=0; h<rta_cache_size; h++)
|
|
|
|
for(a=rta_hash_table[h]; a; a=a->next)
|
|
|
|
{
|
|
|
|
debug("%p ", a);
|
|
|
|
rta_dump(a);
|
|
|
|
debug("\n");
|
|
|
|
}
|
1998-06-05 04:29:05 +08:00
|
|
|
debug("\n");
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
|
|
|
|
1999-12-01 23:10:21 +08:00
|
|
|
void
|
2000-05-07 06:57:39 +08:00
|
|
|
rta_show(struct cli *c, rta *a, ea_list *eal)
|
1999-12-01 23:10:21 +08:00
|
|
|
{
|
|
|
|
static char *src_names[] = { "dummy", "static", "inherit", "device", "static-device", "redirect",
|
2011-06-27 04:25:09 +08:00
|
|
|
"RIP", "OSPF", "OSPF-IA", "OSPF-E1", "OSPF-E2", "BGP", "pipe" };
|
1999-12-01 23:10:21 +08:00
|
|
|
static char *cast_names[] = { "unicast", "broadcast", "multicast", "anycast" };
|
2000-04-01 18:19:47 +08:00
|
|
|
int i;
|
1999-12-01 23:10:21 +08:00
|
|
|
|
|
|
|
cli_printf(c, -1008, "\tType: %s %s %s", src_names[a->source], cast_names[a->cast], ip_scope_text(a->scope));
|
2000-05-07 06:57:39 +08:00
|
|
|
if (!eal)
|
|
|
|
eal = a->eattrs;
|
|
|
|
for(; eal; eal=eal->next)
|
2000-04-01 18:19:47 +08:00
|
|
|
for(i=0; i<eal->count; i++)
|
2011-07-04 01:43:30 +08:00
|
|
|
ea_show(c, &eal->attrs[i]);
|
1999-12-01 23:10:21 +08:00
|
|
|
}
|
|
|
|
|
2000-06-02 20:29:55 +08:00
|
|
|
/**
|
|
|
|
* rta_init - initialize route attribute cache
|
|
|
|
*
|
|
|
|
* This function is called during initialization of the routing
|
|
|
|
* table module to set up the internals of the attribute cache.
|
|
|
|
*/
|
1998-05-20 19:54:33 +08:00
|
|
|
void
|
|
|
|
rta_init(void)
|
|
|
|
{
|
1998-05-27 05:37:37 +08:00
|
|
|
rta_pool = rp_new(&root_pool, "Attributes");
|
1998-05-20 19:54:33 +08:00
|
|
|
rta_slab = sl_new(rta_pool, sizeof(rta));
|
2016-05-06 21:48:35 +08:00
|
|
|
nexthop_slab = sl_new(rta_pool, sizeof(struct nexthop));
|
2000-03-05 05:09:14 +08:00
|
|
|
rta_alloc_hash();
|
2012-08-14 22:25:22 +08:00
|
|
|
rte_src_init();
|
1998-05-20 19:54:33 +08:00
|
|
|
}
|
2000-06-02 20:29:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Documentation for functions declared inline in route.h
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rta_clone - clone route attributes
|
|
|
|
* @r: a &rta to be cloned
|
|
|
|
*
|
|
|
|
* rta_clone() takes a cached &rta and returns its identical cached
|
|
|
|
* copy. Currently it works by just returning the original &rta with
|
|
|
|
* its use count incremented.
|
|
|
|
*/
|
|
|
|
static inline rta *rta_clone(rta *r)
|
|
|
|
{ DUMMY; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* rta_free - free route attributes
|
|
|
|
* @r: a &rta to be freed
|
|
|
|
*
|
|
|
|
* If you stop using a &rta (for example when deleting a route which uses
|
|
|
|
* it), you need to call rta_free() to notify the attribute cache the
|
|
|
|
* attribute is no longer in use and can be freed if you were the last
|
|
|
|
* user (which rta_free() tests by inspecting the use count).
|
|
|
|
*/
|
|
|
|
static inline void rta_free(rta *r)
|
|
|
|
{ DUMMY; }
|
|
|
|
|
|
|
|
#endif
|