1998-05-20 11:54:33 +00:00
|
|
|
/*
|
|
|
|
* BIRD -- Route Attribute Cache
|
|
|
|
*
|
2000-03-04 21:09:14 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-20 11:54:33 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-02 12:29:55 +00: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 12:29:08 +00:00
|
|
|
* in the &ea_list chain and in such case the first occurrence overrides
|
2000-06-02 12:29:55 +00: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 12:29:08 +00:00
|
|
|
* There exist two variants of &rta's -- cached and un-cached ones. Un-cached
|
2000-06-02 12:29:55 +00: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 11:54:33 +00:00
|
|
|
#include "nest/bird.h"
|
2023-10-29 15:25:01 +00:00
|
|
|
#include "nest/route.h"
|
1998-05-20 11:54:33 +00:00
|
|
|
#include "nest/protocol.h"
|
1998-06-04 20:29:05 +00:00
|
|
|
#include "nest/iface.h"
|
1999-12-01 15:10:21 +00:00
|
|
|
#include "nest/cli.h"
|
2022-03-31 17:22:07 +00:00
|
|
|
#include "lib/attrs.h"
|
2004-05-31 17:16:47 +00:00
|
|
|
#include "lib/alloca.h"
|
2013-11-26 21:37:24 +00:00
|
|
|
#include "lib/hash.h"
|
2015-12-29 14:34:48 +00:00
|
|
|
#include "lib/idm.h"
|
1998-05-20 11:54:33 +00:00
|
|
|
#include "lib/resource.h"
|
2000-03-31 23:30:21 +00:00
|
|
|
#include "lib/string.h"
|
1998-05-20 11:54:33 +00:00
|
|
|
|
2016-02-10 12:26:07 +00:00
|
|
|
#include <stddef.h>
|
2022-05-05 16:08:37 +00:00
|
|
|
#include <stdlib.h>
|
2016-02-10 12:26:07 +00:00
|
|
|
|
2018-12-27 13:26:11 +00:00
|
|
|
const adata null_adata; /* adata of length 0 */
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class ea_gen_igp_metric = {
|
|
|
|
.name = "igp_metric",
|
|
|
|
.type = T_INT,
|
|
|
|
};
|
|
|
|
|
2022-04-20 10:24:26 +00:00
|
|
|
struct ea_class ea_gen_preference = {
|
|
|
|
.name = "preference",
|
|
|
|
.type = T_INT,
|
|
|
|
};
|
|
|
|
|
2022-04-20 11:56:04 +00:00
|
|
|
struct ea_class ea_gen_from = {
|
|
|
|
.name = "from",
|
|
|
|
.type = T_IP,
|
|
|
|
};
|
|
|
|
|
2018-11-05 12:56:59 +00:00
|
|
|
const char * const rta_src_names[RTS_MAX] = {
|
|
|
|
[RTS_STATIC] = "static",
|
|
|
|
[RTS_INHERIT] = "inherit",
|
|
|
|
[RTS_DEVICE] = "device",
|
|
|
|
[RTS_STATIC_DEVICE] = "static-device",
|
|
|
|
[RTS_REDIRECT] = "redirect",
|
|
|
|
[RTS_RIP] = "RIP",
|
|
|
|
[RTS_OSPF] = "OSPF",
|
|
|
|
[RTS_OSPF_IA] = "OSPF-IA",
|
|
|
|
[RTS_OSPF_EXT1] = "OSPF-E1",
|
|
|
|
[RTS_OSPF_EXT2] = "OSPF-E2",
|
|
|
|
[RTS_BGP] = "BGP",
|
|
|
|
[RTS_PIPE] = "pipe",
|
|
|
|
[RTS_BABEL] = "Babel",
|
|
|
|
[RTS_RPKI] = "RPKI",
|
2023-06-21 11:15:07 +00:00
|
|
|
[RTS_PERF] = "Perf",
|
2022-10-03 18:06:13 +00:00
|
|
|
[RTS_L3VPN] = "L3VPN",
|
2023-06-21 11:15:07 +00:00
|
|
|
[RTS_AGGREGATED] = "aggregated",
|
2018-11-05 12:56:59 +00:00
|
|
|
};
|
|
|
|
|
2022-05-04 12:41:51 +00:00
|
|
|
static void
|
|
|
|
ea_gen_source_format(const eattr *a, byte *buf, uint size)
|
|
|
|
{
|
|
|
|
if ((a->u.data >= RTS_MAX) || !rta_src_names[a->u.data])
|
|
|
|
bsnprintf(buf, size, "unknown");
|
|
|
|
else
|
|
|
|
bsnprintf(buf, size, "%s", rta_src_names[a->u.data]);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ea_class ea_gen_source = {
|
|
|
|
.name = "source",
|
|
|
|
.type = T_ENUM_RTS,
|
|
|
|
.readonly = 1,
|
|
|
|
.format = ea_gen_source_format,
|
|
|
|
};
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
struct ea_class ea_gen_nexthop = {
|
|
|
|
.name = "nexthop",
|
|
|
|
.type = T_NEXTHOP_LIST,
|
|
|
|
};
|
|
|
|
|
2022-05-15 13:53:35 +00:00
|
|
|
/*
|
|
|
|
* ea_set_hostentry() acquires hostentry from hostcache.
|
|
|
|
* New hostentry has zero use count. Cached rta locks its
|
|
|
|
* hostentry (increases its use count), uncached rta does not lock it.
|
|
|
|
* Hostentry with zero use count is removed asynchronously
|
|
|
|
* during host cache update, therefore it is safe to hold
|
|
|
|
* such hostentry temporarily as long as you hold the table lock.
|
|
|
|
*
|
|
|
|
* There is no need to hold a lock for hostentry->dep table, because that table
|
|
|
|
* contains routes responsible for that hostentry, and therefore is non-empty if
|
|
|
|
* given hostentry has non-zero use count. If the hostentry has zero use count,
|
|
|
|
* the entry is removed before dep is referenced.
|
|
|
|
*
|
|
|
|
* The protocol responsible for routes with recursive next hops should hold a
|
|
|
|
* lock for a 'source' table governing that routes (argument tab),
|
|
|
|
* because its routes reference hostentries related to the governing table.
|
|
|
|
* When all such routes are
|
|
|
|
* removed, rtas are immediately removed achieving zero uc. Then the 'source'
|
|
|
|
* table lock could be immediately released, although hostentries may still
|
|
|
|
* exist - they will be freed together with the 'source' table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ea_gen_hostentry_stored(const eattr *ea)
|
|
|
|
{
|
|
|
|
struct hostentry_adata *had = (struct hostentry_adata *) ea->u.ptr;
|
2024-03-05 13:25:52 +00:00
|
|
|
lfuc_lock(&had->he->uc);
|
2022-05-15 13:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ea_gen_hostentry_freed(const eattr *ea)
|
|
|
|
{
|
|
|
|
struct hostentry_adata *had = (struct hostentry_adata *) ea->u.ptr;
|
2024-03-05 13:25:52 +00:00
|
|
|
lfuc_unlock(&had->he->uc, birdloop_event_list(had->he->owner->loop), had->he->owner->hcu_event);
|
2022-05-15 13:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ea_class ea_gen_hostentry = {
|
|
|
|
.name = "hostentry",
|
|
|
|
.type = T_HOSTENTRY,
|
2022-05-05 17:28:56 +00:00
|
|
|
.readonly = 1,
|
2022-05-15 13:53:35 +00:00
|
|
|
.stored = ea_gen_hostentry_stored,
|
|
|
|
.freed = ea_gen_hostentry_freed,
|
2022-05-05 17:28:56 +00:00
|
|
|
};
|
|
|
|
|
2024-04-04 09:38:52 +00:00
|
|
|
struct ea_class ea_gen_hostentry_version = {
|
|
|
|
.name = "hostentry version",
|
|
|
|
.type = T_INT,
|
|
|
|
.readonly = 1,
|
|
|
|
.hidden = 1,
|
|
|
|
};
|
|
|
|
|
2017-03-08 15:27:18 +00:00
|
|
|
const char * rta_dest_names[RTD_MAX] = {
|
|
|
|
[RTD_NONE] = "",
|
|
|
|
[RTD_UNICAST] = "unicast",
|
|
|
|
[RTD_BLACKHOLE] = "blackhole",
|
|
|
|
[RTD_UNREACHABLE] = "unreachable",
|
|
|
|
[RTD_PROHIBIT] = "prohibited",
|
|
|
|
};
|
|
|
|
|
2022-05-15 16:09:30 +00:00
|
|
|
struct ea_class ea_gen_flowspec_valid = {
|
|
|
|
.name = "flowspec_valid",
|
|
|
|
.type = T_ENUM_FLOWSPEC_VALID,
|
|
|
|
.readonly = 1,
|
|
|
|
};
|
|
|
|
|
2022-06-08 09:47:49 +00:00
|
|
|
const char * flowspec_valid_names[FLOWSPEC__MAX] = {
|
|
|
|
[FLOWSPEC_UNKNOWN] = "unknown",
|
|
|
|
[FLOWSPEC_VALID] = "",
|
|
|
|
[FLOWSPEC_INVALID] = "invalid",
|
|
|
|
};
|
|
|
|
|
2024-11-28 07:22:06 +00:00
|
|
|
static void
|
|
|
|
ea_gen_aspa_providers_format(const eattr *a, byte *buf, uint size)
|
|
|
|
{
|
|
|
|
int_set_format(a->u.ad, ISF_NUMBERS, -1, buf, size - 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ea_class ea_gen_aspa_providers = {
|
|
|
|
.name = "aspa_providers",
|
|
|
|
.type = T_CLIST,
|
|
|
|
.format = ea_gen_aspa_providers_format,
|
|
|
|
};
|
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
DOMAIN(attrs) attrs_domain;
|
|
|
|
|
2010-06-02 20:20:40 +00:00
|
|
|
pool *rta_pool;
|
|
|
|
|
2022-10-12 16:04:39 +00:00
|
|
|
/* Assuming page size of 4096, these are magic values for slab allocation */
|
|
|
|
static const uint ea_slab_sizes[] = { 56, 112, 168, 288, 448, 800, 1344 };
|
|
|
|
static slab *ea_slab[ARRAY_SIZE(ea_slab_sizes)];
|
|
|
|
|
2012-08-14 14:25:22 +00:00
|
|
|
static slab *rte_src_slab;
|
|
|
|
|
2015-12-29 14:34:48 +00:00
|
|
|
static struct idm src_ids;
|
2013-11-26 21:37:24 +00:00
|
|
|
#define SRC_ID_INIT_SIZE 4
|
2012-08-14 14:25:22 +00:00
|
|
|
|
|
|
|
/* rte source hash */
|
2013-11-26 21:37:24 +00:00
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
#define RSH_KEY(n) n->private_id
|
2013-11-26 21:37:24 +00:00
|
|
|
#define RSH_NEXT(n) n->next
|
2021-09-27 14:40:28 +00:00
|
|
|
#define RSH_EQ(n1,n2) n1 == n2
|
2023-11-01 17:08:49 +00:00
|
|
|
#define RSH_FN(n) u64_hash(n)
|
2013-11-26 21:37:24 +00:00
|
|
|
|
|
|
|
#define RSH_REHASH rte_src_rehash
|
|
|
|
#define RSH_PARAMS /2, *2, 1, 1, 8, 20
|
2021-09-27 14:40:28 +00:00
|
|
|
#define RSH_INIT_ORDER 2
|
2023-05-08 11:09:02 +00:00
|
|
|
static struct rte_src * _Atomic * _Atomic rte_src_global;
|
|
|
|
static _Atomic uint rte_src_global_max;
|
1998-05-20 11:54:33 +00:00
|
|
|
|
2012-08-14 14:25:22 +00:00
|
|
|
static void
|
|
|
|
rte_src_init(void)
|
|
|
|
{
|
|
|
|
rte_src_slab = sl_new(rta_pool, sizeof(struct rte_src));
|
2023-05-08 11:09:02 +00:00
|
|
|
|
|
|
|
uint gmax = SRC_ID_INIT_SIZE * 32;
|
|
|
|
struct rte_src * _Atomic *g = mb_alloc(rta_pool, sizeof(struct rte_src * _Atomic) * gmax);
|
|
|
|
for (uint i = 0; i < gmax; i++)
|
|
|
|
atomic_store_explicit(&g[i], NULL, memory_order_relaxed);
|
|
|
|
|
|
|
|
atomic_store_explicit(&rte_src_global, g, memory_order_release);
|
|
|
|
atomic_store_explicit(&rte_src_global_max, gmax, memory_order_release);
|
2012-08-14 14:25:22 +00:00
|
|
|
|
2015-12-29 14:34:48 +00:00
|
|
|
idm_init(&src_ids, rta_pool, SRC_ID_INIT_SIZE);
|
2012-08-14 14:25:22 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 21:37:24 +00:00
|
|
|
HASH_DEFINE_REHASH_FN(RSH, struct rte_src)
|
2012-08-14 14:25:22 +00:00
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
static struct rte_src *
|
|
|
|
rt_find_source(struct rte_owner *p, u32 id)
|
2012-08-14 14:25:22 +00:00
|
|
|
{
|
2021-09-27 14:40:28 +00:00
|
|
|
return HASH_FIND(p->hash, RSH, id);
|
2012-08-14 14:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rte_src *
|
2021-09-27 14:40:28 +00:00
|
|
|
rt_get_source_o(struct rte_owner *p, u32 id)
|
2012-08-14 14:25:22 +00:00
|
|
|
{
|
2021-09-27 14:40:28 +00:00
|
|
|
if (p->stop)
|
|
|
|
bug("Stopping route owner asked for another source.");
|
|
|
|
|
2023-05-08 11:09:02 +00:00
|
|
|
ASSERT_DIE(birdloop_inside(p->list->loop));
|
|
|
|
|
2013-11-26 21:37:24 +00:00
|
|
|
struct rte_src *src = rt_find_source(p, id);
|
2012-08-14 14:25:22 +00:00
|
|
|
|
2013-11-26 21:37:24 +00:00
|
|
|
if (src)
|
2021-09-27 14:40:28 +00:00
|
|
|
{
|
2024-05-02 09:39:34 +00:00
|
|
|
#ifdef RT_SOURCE_DEBUG
|
|
|
|
log(L_INFO "Found source %uG", src->global_id);
|
|
|
|
#endif
|
2023-11-24 07:46:50 +00:00
|
|
|
lfuc_lock_revive(&src->uc);
|
2013-11-26 21:37:24 +00:00
|
|
|
return src;
|
2021-09-27 14:40:28 +00:00
|
|
|
}
|
2012-08-14 14:25:22 +00:00
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
RTA_LOCK;
|
2020-11-24 02:21:44 +00:00
|
|
|
src = sl_allocz(rte_src_slab);
|
2021-09-27 14:40:28 +00:00
|
|
|
src->owner = p;
|
2012-08-14 14:25:22 +00:00
|
|
|
src->private_id = id;
|
2015-12-29 14:34:48 +00:00
|
|
|
src->global_id = idm_alloc(&src_ids);
|
2015-05-31 21:25:33 +00:00
|
|
|
|
2023-11-24 07:46:50 +00:00
|
|
|
lfuc_init(&src->uc);
|
2021-09-27 14:40:28 +00:00
|
|
|
p->uc++;
|
|
|
|
|
|
|
|
HASH_INSERT2(p->hash, RSH, rta_pool, src);
|
2023-12-08 10:33:43 +00:00
|
|
|
if (p->debug & D_ROUTES)
|
|
|
|
log(L_TRACE "%s: new rte_src ID %luL %uG, have %u sources now",
|
2021-09-27 14:40:28 +00:00
|
|
|
p->name, src->private_id, src->global_id, p->uc);
|
|
|
|
|
2023-05-08 11:09:02 +00:00
|
|
|
uint gm = atomic_load_explicit(&rte_src_global_max, memory_order_relaxed);
|
|
|
|
struct rte_src * _Atomic * g = atomic_load_explicit(&rte_src_global, memory_order_relaxed);
|
|
|
|
|
|
|
|
if (src->global_id >= gm)
|
2022-06-20 17:10:49 +00:00
|
|
|
{
|
2023-05-08 11:09:02 +00:00
|
|
|
/* Allocate new block */
|
|
|
|
size_t old_len = sizeof(struct rte_src * _Atomic) * gm;
|
|
|
|
struct rte_src * _Atomic * new_block = mb_alloc(rta_pool, old_len * 2);
|
|
|
|
memcpy(new_block, g, old_len);
|
|
|
|
|
|
|
|
for (uint i = 0; i < gm; i++)
|
|
|
|
atomic_store_explicit(&new_block[gm+i], NULL, memory_order_relaxed);
|
|
|
|
|
|
|
|
/* Update the pointer */
|
|
|
|
atomic_store_explicit(&rte_src_global, new_block, memory_order_release);
|
|
|
|
atomic_store_explicit(&rte_src_global_max, gm * 2, memory_order_release);
|
|
|
|
|
|
|
|
/* Wait for readers */
|
|
|
|
synchronize_rcu();
|
|
|
|
|
|
|
|
/* Free the old block */
|
|
|
|
mb_free(g);
|
|
|
|
g = new_block;
|
2022-06-20 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 11:09:02 +00:00
|
|
|
atomic_store_explicit(&g[src->global_id], src, memory_order_release);
|
2021-09-27 14:40:28 +00:00
|
|
|
RTA_UNLOCK;
|
2012-08-14 14:25:22 +00:00
|
|
|
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2023-05-08 11:09:02 +00:00
|
|
|
/**
|
|
|
|
* Find a rte source by its global ID. Only available for existing and locked
|
|
|
|
* sources stored by their ID. Checking for non-existent or foreign source is unsafe.
|
|
|
|
*
|
|
|
|
* @id: requested global ID
|
|
|
|
*
|
|
|
|
* Returns the found source or dies. Result of this function is guaranteed to
|
|
|
|
* be a valid source as long as the caller owns it.
|
|
|
|
*/
|
2022-06-20 17:10:49 +00:00
|
|
|
struct rte_src *
|
|
|
|
rt_find_source_global(u32 id)
|
|
|
|
{
|
2023-05-08 11:09:02 +00:00
|
|
|
rcu_read_lock();
|
|
|
|
ASSERT_DIE(id < atomic_load_explicit(&rte_src_global_max, memory_order_acquire));
|
|
|
|
|
|
|
|
struct rte_src * _Atomic * g = atomic_load_explicit(&rte_src_global, memory_order_acquire);
|
|
|
|
struct rte_src *src = atomic_load_explicit(&g[id], memory_order_acquire);
|
|
|
|
ASSERT_DIE(src);
|
|
|
|
ASSERT_DIE(src->global_id == id);
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return src;
|
2022-06-20 17:10:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
static inline void
|
|
|
|
rt_done_sources(struct rte_owner *o)
|
|
|
|
{
|
2024-12-13 18:10:55 +00:00
|
|
|
RTA_LOCK;
|
|
|
|
HASH_FREE(o->hash);
|
|
|
|
RTA_UNLOCK;
|
2022-08-02 20:08:59 +00:00
|
|
|
ev_send(o->list, o->stop);
|
2021-09-27 14:40:28 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 14:25:22 +00:00
|
|
|
void
|
2021-09-27 14:40:28 +00:00
|
|
|
rt_prune_sources(void *data)
|
2012-08-14 14:25:22 +00:00
|
|
|
{
|
2021-09-27 14:40:28 +00:00
|
|
|
struct rte_owner *o = data;
|
|
|
|
|
|
|
|
HASH_WALK_FILTER(o->hash, next, src, sp)
|
2013-11-26 21:37:24 +00:00
|
|
|
{
|
2023-11-24 07:46:50 +00:00
|
|
|
if (lfuc_finished(&src->uc))
|
2012-08-14 14:25:22 +00:00
|
|
|
{
|
2021-09-27 14:40:28 +00:00
|
|
|
o->uc--;
|
|
|
|
|
2023-12-08 10:33:43 +00:00
|
|
|
if (o->debug & D_ROUTES)
|
|
|
|
log(L_TRACE "%s: freed rte_src ID %luL %uG, have %u sources now",
|
|
|
|
o->name, src->private_id, src->global_id, o->uc);
|
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
HASH_DO_REMOVE(o->hash, RSH, sp);
|
|
|
|
|
|
|
|
RTA_LOCK;
|
2023-05-08 11:09:02 +00:00
|
|
|
struct rte_src * _Atomic * g = atomic_load_explicit(&rte_src_global, memory_order_acquire);
|
|
|
|
atomic_store_explicit(&g[src->global_id], NULL, memory_order_release);
|
2015-12-29 14:34:48 +00:00
|
|
|
idm_free(&src_ids, src->global_id);
|
2022-04-04 18:31:14 +00:00
|
|
|
sl_free(src);
|
2021-09-27 14:40:28 +00:00
|
|
|
RTA_UNLOCK;
|
2012-08-14 14:25:22 +00:00
|
|
|
}
|
2013-11-26 21:37:24 +00:00
|
|
|
}
|
|
|
|
HASH_WALK_FILTER_END;
|
2012-08-14 14:25:22 +00:00
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
RTA_LOCK;
|
|
|
|
HASH_MAY_RESIZE_DOWN(o->hash, RSH, rta_pool);
|
|
|
|
|
|
|
|
if (o->stop && !o->uc)
|
|
|
|
{
|
|
|
|
rfree(o->prune);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
|
2023-12-08 10:33:43 +00:00
|
|
|
if (o->debug & D_EVENTS)
|
|
|
|
log(L_TRACE "%s: all rte_src's pruned, scheduling stop event", o->name);
|
2021-09-27 14:40:28 +00:00
|
|
|
|
|
|
|
rt_done_sources(o);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
RTA_UNLOCK;
|
2012-08-14 14:25:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-01 17:25:40 +00:00
|
|
|
void
|
2024-12-13 14:23:01 +00:00
|
|
|
rt_dump_sources(struct dump_request *dreq, struct rte_owner *o)
|
2023-11-01 17:25:40 +00:00
|
|
|
{
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("\t%s: hord=%u, uc=%u, cnt=%u prune=%p, stop=%p\n",
|
2023-11-01 17:25:40 +00:00
|
|
|
o->name, o->hash.order, o->uc, o->hash.count, o->prune, o->stop);
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("\tget_route_info=%p, better=%p, mergable=%p, igp_metric=%p, recalculate=%p",
|
2023-11-01 17:25:40 +00:00
|
|
|
o->class->get_route_info, o->class->rte_better, o->class->rte_mergable,
|
|
|
|
o->class->rte_igp_metric, o->rte_recalculate);
|
|
|
|
|
|
|
|
int splitting = 0;
|
|
|
|
HASH_WALK(o->hash, next, src)
|
|
|
|
{
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("%c%c%uL %uG %luU",
|
2023-11-01 17:25:40 +00:00
|
|
|
(splitting % 8) ? ',' : '\n',
|
|
|
|
(splitting % 8) ? ' ' : '\t',
|
|
|
|
src->private_id, src->global_id,
|
2024-01-26 13:50:36 +00:00
|
|
|
atomic_load_explicit(&src->uc.uc, memory_order_relaxed));
|
2023-11-01 17:25:40 +00:00
|
|
|
|
|
|
|
splitting++;
|
|
|
|
}
|
|
|
|
HASH_WALK_END;
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("\n");
|
2023-11-01 17:25:40 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 09:19:14 +00:00
|
|
|
static struct rte_owner_class default_rte_owner_class;
|
|
|
|
|
2021-09-27 14:40:28 +00:00
|
|
|
void
|
|
|
|
rt_init_sources(struct rte_owner *o, const char *name, event_list *list)
|
|
|
|
{
|
|
|
|
RTA_LOCK;
|
|
|
|
HASH_INIT(o->hash, rta_pool, RSH_INIT_ORDER);
|
|
|
|
o->hash_key = random_u32();
|
|
|
|
o->uc = 0;
|
|
|
|
o->name = name;
|
|
|
|
o->prune = ev_new_init(rta_pool, rt_prune_sources, o);
|
|
|
|
o->stop = NULL;
|
|
|
|
o->list = list;
|
2024-06-25 09:19:14 +00:00
|
|
|
if (!o->class)
|
|
|
|
o->class = &default_rte_owner_class;
|
2021-09-27 14:40:28 +00:00
|
|
|
RTA_UNLOCK;
|
2023-12-08 10:33:43 +00:00
|
|
|
if (o->debug & D_EVENTS)
|
|
|
|
log(L_TRACE "%s: initialized rte_src owner", o->name);
|
2021-09-27 14:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rt_destroy_sources(struct rte_owner *o, event *done)
|
|
|
|
{
|
|
|
|
o->stop = done;
|
|
|
|
|
|
|
|
if (!o->uc)
|
|
|
|
{
|
2023-12-08 10:33:43 +00:00
|
|
|
if (o->debug & D_EVENTS)
|
|
|
|
log(L_TRACE "%s: rte_src owner destroy requested, already clean, scheduling stop event", o->name);
|
2021-09-27 14:40:28 +00:00
|
|
|
|
|
|
|
RTA_LOCK;
|
|
|
|
rfree(o->prune);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
|
|
|
|
rt_done_sources(o);
|
|
|
|
}
|
|
|
|
else
|
2023-12-08 10:33:43 +00:00
|
|
|
if (o->debug & D_EVENTS)
|
|
|
|
log(L_TRACE "%s: rte_src owner destroy requested, remaining %u rte_src's to prune.", o->name, o->uc);
|
2021-09-27 14:40:28 +00:00
|
|
|
}
|
2012-08-14 14:25:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Multipath Next Hop
|
|
|
|
*/
|
|
|
|
|
2015-05-31 21:25:33 +00:00
|
|
|
static int
|
2019-08-17 08:20:46 +00:00
|
|
|
nexthop_compare_node(const struct nexthop *x, const struct nexthop *y)
|
2015-05-31 21:25:33 +00:00
|
|
|
{
|
|
|
|
int r;
|
2017-07-04 21:36:21 +00:00
|
|
|
/* Should we also compare flags ? */
|
|
|
|
|
2015-05-31 21:25:33 +00:00
|
|
|
r = ((int) y->weight) - ((int) x->weight);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
r = ipa_compare(x->gw, y->gw);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2016-06-10 12:34:41 +00:00
|
|
|
r = ((int) y->labels) - ((int) x->labels);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
|
2017-02-20 01:26:45 +00:00
|
|
|
for (int i = 0; i < y->labels; i++)
|
2016-06-10 12:34:41 +00:00
|
|
|
{
|
|
|
|
r = ((int) y->label[i]) - ((int) x->label[i]);
|
|
|
|
if (r)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-05-31 21:25:33 +00:00
|
|
|
return ((int) x->iface->index) - ((int) y->iface->index);
|
|
|
|
}
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
static int
|
|
|
|
nexthop_compare_qsort(const void *x, const void *y)
|
2015-05-31 21:25:33 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
return nexthop_compare_node( *(const struct nexthop **) x, *(const struct nexthop **) y );
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-06 13:48:35 +00:00
|
|
|
* nexthop_merge - merge nexthop lists
|
2015-05-31 21:25:33 +00:00
|
|
|
* @x: list 1
|
|
|
|
* @y: list 2
|
|
|
|
* @max: max number of nexthops
|
|
|
|
* @lp: linpool for allocating nexthops
|
|
|
|
*
|
2016-05-06 13:48:35 +00:00
|
|
|
* The nexthop_merge() function takes two nexthop lists @x and @y and merges them,
|
2015-05-31 21:25:33 +00: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.
|
|
|
|
*/
|
2022-05-05 16:08:37 +00:00
|
|
|
struct nexthop_adata *
|
|
|
|
nexthop_merge(struct nexthop_adata *xin, struct nexthop_adata *yin, int max, linpool *lp)
|
2015-05-31 21:25:33 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
uint outlen = ADATA_SIZE(xin->ad.length) + ADATA_SIZE(yin->ad.length);
|
|
|
|
struct nexthop_adata *out = lp_alloc(lp, outlen);
|
|
|
|
out->ad.length = outlen - sizeof (struct adata);
|
|
|
|
|
|
|
|
struct nexthop *x = &xin->nh, *y = &yin->nh, *cur = &out->nh;
|
|
|
|
int xvalid, yvalid;
|
2015-05-31 21:25:33 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
while (max--)
|
2015-05-31 21:25:33 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
xvalid = NEXTHOP_VALID(x, xin);
|
|
|
|
yvalid = NEXTHOP_VALID(y, yin);
|
|
|
|
|
|
|
|
if (!xvalid && !yvalid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
ASSUME(NEXTHOP_VALID(cur, out));
|
|
|
|
|
|
|
|
int cmp = !xvalid ? 1 : !yvalid ? -1 : nexthop_compare_node(x, y);
|
2019-08-17 08:20:46 +00:00
|
|
|
|
2015-05-31 21:25:33 +00:00
|
|
|
if (cmp < 0)
|
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
ASSUME(NEXTHOP_VALID(x, xin));
|
|
|
|
memcpy(cur, x, nexthop_size(x));
|
|
|
|
x = NEXTHOP_NEXT(x);
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
|
|
|
else if (cmp > 0)
|
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
ASSUME(NEXTHOP_VALID(y, yin));
|
|
|
|
memcpy(cur, y, nexthop_size(y));
|
|
|
|
y = NEXTHOP_NEXT(y);
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
ASSUME(NEXTHOP_VALID(x, xin));
|
|
|
|
memcpy(cur, x, nexthop_size(x));
|
|
|
|
x = NEXTHOP_NEXT(x);
|
|
|
|
|
|
|
|
ASSUME(NEXTHOP_VALID(y, yin));
|
|
|
|
y = NEXTHOP_NEXT(y);
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
2022-05-05 16:08:37 +00:00
|
|
|
cur = NEXTHOP_NEXT(cur);
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
out->ad.length = (void *) cur - (void *) out->ad.data;
|
|
|
|
|
|
|
|
return out;
|
2015-05-31 21:25:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
struct nexthop_adata *
|
|
|
|
nexthop_sort(struct nexthop_adata *nhad, linpool *lp)
|
2016-08-30 15:17:27 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
/* Count the nexthops */
|
|
|
|
uint cnt = 0;
|
|
|
|
NEXTHOP_WALK(nh, nhad)
|
|
|
|
cnt++;
|
2016-08-30 15:17:27 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
if (cnt <= 1)
|
|
|
|
return nhad;
|
2016-08-30 15:17:27 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
/* Get pointers to them */
|
|
|
|
struct nexthop **sptr = tmp_alloc(cnt * sizeof(struct nexthop *));
|
2016-08-30 15:17:27 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
uint i = 0;
|
|
|
|
NEXTHOP_WALK(nh, nhad)
|
|
|
|
sptr[i++] = nh;
|
|
|
|
|
|
|
|
/* Sort the pointers */
|
|
|
|
qsort(sptr, cnt, sizeof(struct nexthop *), nexthop_compare_qsort);
|
|
|
|
|
|
|
|
/* Allocate the output */
|
|
|
|
struct nexthop_adata *out = (struct nexthop_adata *) lp_alloc_adata(lp, nhad->ad.length);
|
|
|
|
struct nexthop *dest = &out->nh;
|
2019-07-02 16:23:06 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
/* Deduplicate nexthops while storing them */
|
|
|
|
for (uint i = 0; i < cnt; i++)
|
2019-07-02 16:23:06 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
if (i && !nexthop_compare_node(sptr[i], sptr[i-1]))
|
|
|
|
continue;
|
2019-07-02 16:23:06 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
memcpy(dest, sptr[i], NEXTHOP_SIZE(sptr[i]));
|
|
|
|
dest = NEXTHOP_NEXT(dest);
|
2019-07-02 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
out->ad.length = (void *) dest - (void *) out->ad.data;
|
|
|
|
return out;
|
2019-07-02 16:23:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 15:17:27 +00:00
|
|
|
int
|
2022-05-05 16:08:37 +00:00
|
|
|
nexthop_is_sorted(struct nexthop_adata *nhad)
|
2016-08-30 15:17:27 +00:00
|
|
|
{
|
2022-05-05 16:08:37 +00:00
|
|
|
struct nexthop *prev = NULL;
|
|
|
|
NEXTHOP_WALK(nh, nhad)
|
|
|
|
{
|
|
|
|
if (prev && (nexthop_compare_node(prev, nh) >= 0))
|
2016-08-30 15:17:27 +00:00
|
|
|
return 0;
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
prev = nh;
|
|
|
|
}
|
2010-12-07 22:33:55 +00:00
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
return 1;
|
2010-12-07 22:33:55 +00:00
|
|
|
}
|
|
|
|
|
1999-03-17 13:09:09 +00:00
|
|
|
/*
|
|
|
|
* Extended Attributes
|
|
|
|
*/
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
#define EA_CLASS_INITIAL_MAX 128
|
|
|
|
static struct ea_class **ea_class_global = NULL;
|
|
|
|
static uint ea_class_max;
|
|
|
|
static struct idm ea_class_idm;
|
|
|
|
|
|
|
|
/* Config parser lex register function */
|
|
|
|
void ea_lex_register(struct ea_class *def);
|
|
|
|
|
|
|
|
static void
|
|
|
|
ea_class_free(struct ea_class *cl)
|
|
|
|
{
|
2023-10-29 14:42:46 +00:00
|
|
|
RTA_LOCK;
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
/* No more ea class references. Unregister the attribute. */
|
|
|
|
idm_free(&ea_class_idm, cl->id);
|
|
|
|
ea_class_global[cl->id] = NULL;
|
2023-10-29 14:42:46 +00:00
|
|
|
|
|
|
|
/* When we start supporting full protocol removal, we may need to call
|
|
|
|
* ea_lex_unregister(cl), see where ea_lex_register() is called. */
|
|
|
|
|
|
|
|
RTA_UNLOCK;
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ea_class_ref_free(resource *r)
|
|
|
|
{
|
2024-04-26 10:14:33 +00:00
|
|
|
SKIP_BACK_DECLARE(struct ea_class_ref, ref, r, r);
|
2022-03-19 15:23:42 +00:00
|
|
|
if (!--ref->class->uc)
|
|
|
|
ea_class_free(ref->class);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-12-13 14:23:01 +00:00
|
|
|
ea_class_ref_dump(struct dump_request *dreq, resource *r)
|
2022-03-19 15:23:42 +00:00
|
|
|
{
|
2024-04-26 10:14:33 +00:00
|
|
|
SKIP_BACK_DECLARE(struct ea_class_ref, ref, r, r);
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("name \"%s\", type=%d\n", ref->class->name, ref->class->type);
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct resclass ea_class_ref_class = {
|
|
|
|
.name = "Attribute class reference",
|
|
|
|
.size = sizeof(struct ea_class_ref),
|
|
|
|
.free = ea_class_ref_free,
|
|
|
|
.dump = ea_class_ref_dump,
|
|
|
|
.lookup = NULL,
|
|
|
|
.memsize = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
ea_class_init(void)
|
|
|
|
{
|
2023-01-19 09:56:16 +00:00
|
|
|
ASSERT_DIE(ea_class_global == NULL);
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
idm_init(&ea_class_idm, rta_pool, EA_CLASS_INITIAL_MAX);
|
|
|
|
ea_class_global = mb_allocz(rta_pool,
|
|
|
|
sizeof(*ea_class_global) * (ea_class_max = EA_CLASS_INITIAL_MAX));
|
|
|
|
}
|
|
|
|
|
2023-10-29 14:42:46 +00:00
|
|
|
struct ea_class_ref *
|
2022-03-19 15:23:42 +00:00
|
|
|
ea_ref_class(pool *p, struct ea_class *def)
|
|
|
|
{
|
|
|
|
def->uc++;
|
|
|
|
struct ea_class_ref *ref = ralloc(p, &ea_class_ref_class);
|
|
|
|
ref->class = def;
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ea_class_ref *
|
|
|
|
ea_register(pool *p, struct ea_class *def)
|
|
|
|
{
|
|
|
|
def->id = idm_alloc(&ea_class_idm);
|
|
|
|
|
|
|
|
ASSERT_DIE(ea_class_global);
|
|
|
|
while (def->id >= ea_class_max)
|
|
|
|
ea_class_global = mb_realloc(ea_class_global, sizeof(*ea_class_global) * (ea_class_max *= 2));
|
|
|
|
|
|
|
|
ASSERT_DIE(def->id < ea_class_max);
|
|
|
|
ea_class_global[def->id] = def;
|
|
|
|
|
|
|
|
return ea_ref_class(p, def);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ea_class_ref *
|
|
|
|
ea_register_alloc(pool *p, struct ea_class cl)
|
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
struct ea_class_ref *ref;
|
|
|
|
|
|
|
|
RTA_LOCK;
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class *clp = ea_class_find_by_name(cl.name);
|
|
|
|
if (clp && clp->type == cl.type)
|
2023-04-21 13:26:06 +00:00
|
|
|
{
|
|
|
|
ref = ea_ref_class(p, clp);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
return ref;
|
|
|
|
}
|
2022-03-19 15:23:42 +00:00
|
|
|
|
|
|
|
uint namelen = strlen(cl.name) + 1;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct ea_class cl;
|
|
|
|
char name[0];
|
|
|
|
} *cla = mb_alloc(rta_pool, sizeof(struct ea_class) + namelen);
|
|
|
|
cla->cl = cl;
|
|
|
|
memcpy(cla->name, cl.name, namelen);
|
|
|
|
cla->cl.name = cla->name;
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
ref = ea_register(p, &cla->cl);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
return ref;
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ea_register_init(struct ea_class *clp)
|
|
|
|
{
|
2023-04-21 13:26:06 +00:00
|
|
|
RTA_LOCK;
|
2022-03-19 15:23:42 +00:00
|
|
|
ASSERT_DIE(!ea_class_find_by_name(clp->name));
|
2023-10-29 14:42:46 +00:00
|
|
|
|
|
|
|
struct ea_class *def = ea_register(&root_pool, clp)->class;
|
|
|
|
|
|
|
|
if (!clp->hidden)
|
|
|
|
ea_lex_register(def);
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
RTA_UNLOCK;
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ea_class *
|
|
|
|
ea_class_find_by_id(uint id)
|
|
|
|
{
|
|
|
|
ASSERT_DIE(id < ea_class_max);
|
|
|
|
ASSERT_DIE(ea_class_global[id]);
|
|
|
|
return ea_class_global[id];
|
|
|
|
}
|
|
|
|
|
1999-11-18 14:41:29 +00:00
|
|
|
static inline eattr *
|
|
|
|
ea__find(ea_list *e, unsigned id)
|
1999-03-17 13:09:09 +00:00
|
|
|
{
|
|
|
|
eattr *a;
|
|
|
|
int l, r, m;
|
|
|
|
|
|
|
|
while (e)
|
|
|
|
{
|
|
|
|
if (e->flags & EALF_BISECT)
|
|
|
|
{
|
|
|
|
l = 0;
|
2009-08-21 07:31:35 +00:00
|
|
|
r = e->count - 1;
|
1999-03-17 13:09:09 +00: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 12:29:55 +00: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 12:29:08 +00:00
|
|
|
* occurrence of an attribute with specified ID, returning either a pointer
|
2000-06-02 12:29:55 +00:00
|
|
|
* to its &eattr structure or %NULL if no such attribute exists.
|
|
|
|
*/
|
1999-11-18 14:41:29 +00:00
|
|
|
eattr *
|
2022-03-19 15:23:42 +00:00
|
|
|
ea_find_by_id(ea_list *e, unsigned id)
|
1999-11-18 14:41:29 +00:00
|
|
|
{
|
|
|
|
eattr *a = ea__find(e, id & EA_CODE_MASK);
|
|
|
|
|
2022-03-14 10:13:48 +00:00
|
|
|
if (a && a->undef && !(id & EA_ALLOW_UNDEF))
|
1999-11-18 14:41:29 +00:00
|
|
|
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 14:42:22 +00: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 13:49:44 +00: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 14:42:22 +00: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);
|
|
|
|
|
2022-03-14 10:13:48 +00:00
|
|
|
if (a->undef)
|
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 14:42:22 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
s->eattrs = e;
|
|
|
|
s->ea = a;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
else if (e->flags & EALF_BISECT)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-06-26 15:19:24 +00:00
|
|
|
static bool
|
2024-01-28 12:09:48 +00:00
|
|
|
eattr_same_value(const eattr *a, const eattr *b)
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
a->id != b->id ||
|
|
|
|
a->flags != b->flags ||
|
|
|
|
a->type != b->type ||
|
|
|
|
a->undef != b->undef
|
|
|
|
)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (a->undef)
|
|
|
|
return 1;
|
|
|
|
|
2024-10-15 08:50:29 +00:00
|
|
|
if (a->type == T_PTR)
|
|
|
|
return a->u.v_ptr == b->u.v_ptr;
|
2024-01-28 12:09:48 +00:00
|
|
|
if (a->type & EAF_EMBEDDED)
|
|
|
|
return a->u.data == b->u.data;
|
|
|
|
else
|
|
|
|
return adata_same(a->u.ptr, b->u.ptr);
|
|
|
|
}
|
|
|
|
|
2024-06-26 15:19:24 +00:00
|
|
|
static bool
|
2024-01-28 12:09:48 +00:00
|
|
|
eattr_same(const eattr *a, const eattr *b)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
eattr_same_value(a, b) &&
|
|
|
|
a->originated == b->originated &&
|
|
|
|
a->fresh == b->fresh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-02 12:29:55 +00: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 12:39:29 +00:00
|
|
|
int
|
1998-05-20 11:54:33 +00:00
|
|
|
ea_same(ea_list *x, ea_list *y)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
1999-03-17 13:09:09 +00:00
|
|
|
if (!x || !y)
|
|
|
|
return x == y;
|
2022-06-16 21:24:53 +00:00
|
|
|
if (x->next != y->next)
|
|
|
|
return 0;
|
1999-03-17 13:09:09 +00:00
|
|
|
if (x->count != y->count)
|
|
|
|
return 0;
|
|
|
|
for(c=0; c<x->count; c++)
|
2024-01-28 12:09:48 +00:00
|
|
|
if (!eattr_same(&x->attrs[c], &y->attrs[c]))
|
|
|
|
return 0;
|
1999-03-17 13:09:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-10 12:11:46 +00:00
|
|
|
uint
|
|
|
|
ea_list_size(ea_list *o)
|
1999-03-17 13:09:09 +00:00
|
|
|
{
|
2022-04-10 12:11:46 +00:00
|
|
|
unsigned i, elen;
|
1999-03-17 13:09:09 +00:00
|
|
|
|
2022-04-10 12:11:46 +00:00
|
|
|
ASSERT_DIE(o);
|
|
|
|
elen = BIRD_CPU_ALIGN(sizeof(ea_list) + sizeof(eattr) * o->count);
|
2021-03-30 19:44:35 +00:00
|
|
|
|
|
|
|
for(i=0; i<o->count; i++)
|
|
|
|
{
|
|
|
|
eattr *a = &o->attrs[i];
|
2022-06-16 21:24:53 +00:00
|
|
|
if (!a->undef && !(a->type & EAF_EMBEDDED))
|
2022-04-10 12:11:46 +00:00
|
|
|
elen += ADATA_SIZE(a->u.ptr->length);
|
2021-03-30 19:44:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-10 12:11:46 +00:00
|
|
|
return elen;
|
|
|
|
}
|
|
|
|
|
2024-11-20 15:53:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ea_normalize - create a normalized version of attributes
|
|
|
|
* @e: input attributes
|
|
|
|
* @upto: bitmask of layers which should stay as an underlay
|
|
|
|
*
|
|
|
|
* This function squashes all updates done atop some ea_list
|
|
|
|
* and creates the final structure useful for storage or fast searching.
|
|
|
|
* The method is a bucket sort.
|
|
|
|
*
|
|
|
|
* Returns the final ea_list with some excess memory at the end,
|
|
|
|
* allocated from the tmp_linpool. The adata is linked from the original places.
|
|
|
|
*/
|
|
|
|
ea_list *
|
|
|
|
ea_normalize(ea_list *e, u32 upto)
|
|
|
|
{
|
|
|
|
/* We expect some work to be actually needed. */
|
|
|
|
ASSERT_DIE(!BIT32_TEST(&upto, e->stored));
|
|
|
|
|
|
|
|
/* Allocate the output */
|
|
|
|
ea_list *out = tmp_allocz(ea_class_max * sizeof(eattr) + sizeof(ea_list));
|
|
|
|
*out = (ea_list) {
|
|
|
|
.flags = EALF_SORTED,
|
|
|
|
};
|
|
|
|
|
|
|
|
uint min_id = ~0, max_id = 0;
|
|
|
|
|
|
|
|
eattr *buckets = out->attrs;
|
|
|
|
|
|
|
|
/* Walk the attribute lists, one after another. */
|
|
|
|
for (; e; e = e->next)
|
|
|
|
{
|
|
|
|
if (!out->next && BIT32_TEST(&upto, e->stored))
|
|
|
|
out->next = e;
|
|
|
|
|
|
|
|
for (int i = 0; i < e->count; i++)
|
|
|
|
{
|
|
|
|
uint id = e->attrs[i].id;
|
|
|
|
if (id > max_id)
|
|
|
|
max_id = id;
|
|
|
|
if (id < min_id)
|
|
|
|
min_id = id;
|
|
|
|
|
|
|
|
if (out->next)
|
|
|
|
{
|
|
|
|
/* Underlay: check whether the value is duplicate */
|
|
|
|
if (buckets[id].id && buckets[id].fresh)
|
|
|
|
if (eattr_same_value(&e->attrs[i], &buckets[id]))
|
|
|
|
/* Duplicate value treated as no change at all */
|
|
|
|
buckets[id] = (eattr) {};
|
|
|
|
else
|
|
|
|
/* This value is actually needed */
|
|
|
|
buckets[id].fresh = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Overlay: not seen yet -> copy the eattr */
|
|
|
|
if (!buckets[id].id)
|
|
|
|
{
|
|
|
|
buckets[id] = e->attrs[i];
|
|
|
|
buckets[id].fresh = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The originated information is relevant from the lowermost one */
|
|
|
|
buckets[id].originated = e->attrs[i].originated;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And now we just walk the list from beginning to end and collect
|
|
|
|
* everything to the beginning of the list.
|
|
|
|
* Walking just that part which is inhabited for sure. */
|
|
|
|
for (uint id = min_id; id <= max_id; id++)
|
|
|
|
{
|
|
|
|
/* Nothing to see for this ID */
|
|
|
|
if (!buckets[id].id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Drop unnecessary undefs */
|
|
|
|
if (buckets[id].undef && buckets[id].fresh)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Now the freshness is lost, finally */
|
|
|
|
buckets[id].fresh = 0;
|
|
|
|
|
|
|
|
/* Move the attribute to the beginning */
|
|
|
|
ASSERT_DIE(out->count < id);
|
|
|
|
buckets[out->count++] = buckets[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We want to bisect only if the list is long enough */
|
|
|
|
if (out->count > 5)
|
|
|
|
out->flags |= EALF_BISECT;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 12:11:46 +00:00
|
|
|
void
|
|
|
|
ea_list_copy(ea_list *n, ea_list *o, uint elen)
|
|
|
|
{
|
|
|
|
uint adpos = sizeof(ea_list) + sizeof(eattr) * o->count;
|
2021-03-30 19:44:35 +00:00
|
|
|
memcpy(n, o, adpos);
|
2022-04-10 12:11:46 +00:00
|
|
|
adpos = BIRD_CPU_ALIGN(adpos);
|
|
|
|
|
|
|
|
for(uint i=0; i<o->count; i++)
|
1999-03-17 13:09:09 +00:00
|
|
|
{
|
|
|
|
eattr *a = &n->attrs[i];
|
2022-06-16 21:24:53 +00:00
|
|
|
if (!a->undef && !(a->type & EAF_EMBEDDED))
|
1999-03-17 13:09:09 +00:00
|
|
|
{
|
2022-04-10 12:11:46 +00:00
|
|
|
unsigned size = ADATA_SIZE(a->u.ptr->length);
|
2021-03-30 19:44:35 +00:00
|
|
|
ASSERT_DIE(adpos + size <= elen);
|
|
|
|
|
|
|
|
struct adata *d = ((void *) n) + adpos;
|
1999-03-17 13:09:09 +00:00
|
|
|
memcpy(d, a->u.ptr, size);
|
|
|
|
a->u.ptr = d;
|
2021-03-30 19:44:35 +00:00
|
|
|
|
|
|
|
adpos += size;
|
1999-03-17 13:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-10 12:11:46 +00:00
|
|
|
|
2021-03-30 19:44:35 +00:00
|
|
|
ASSERT_DIE(adpos == elen);
|
1999-03-17 13:09:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
static void
|
|
|
|
ea_list_ref(ea_list *l)
|
2000-05-08 13:12:14 +00:00
|
|
|
{
|
2022-03-19 15:23:42 +00:00
|
|
|
for(uint i=0; i<l->count; i++)
|
2000-05-08 13:12:14 +00:00
|
|
|
{
|
2022-03-19 15:23:42 +00:00
|
|
|
eattr *a = &l->attrs[i];
|
|
|
|
ASSERT_DIE(a->id < ea_class_max);
|
|
|
|
|
2022-06-16 21:24:53 +00:00
|
|
|
if (a->undef)
|
|
|
|
continue;
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class *cl = ea_class_global[a->id];
|
|
|
|
ASSERT_DIE(cl && cl->uc);
|
2022-05-15 13:53:35 +00:00
|
|
|
|
|
|
|
CALL(cl->stored, a);
|
2022-03-19 15:23:42 +00:00
|
|
|
cl->uc++;
|
2000-05-08 13:12:14 +00:00
|
|
|
}
|
2022-06-16 21:24:53 +00:00
|
|
|
|
|
|
|
if (l->next)
|
2024-04-04 10:01:35 +00:00
|
|
|
ea_ref(l->next);
|
2000-05-08 13:12:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
static void
|
|
|
|
ea_list_unref(ea_list *l)
|
2010-08-02 11:11:53 +00:00
|
|
|
{
|
2022-03-19 15:23:42 +00:00
|
|
|
for(uint i=0; i<l->count; i++)
|
2010-08-02 11:11:53 +00:00
|
|
|
{
|
2022-03-19 15:23:42 +00:00
|
|
|
eattr *a = &l->attrs[i];
|
|
|
|
ASSERT_DIE(a->id < ea_class_max);
|
|
|
|
|
2022-06-16 21:24:53 +00:00
|
|
|
if (a->undef)
|
|
|
|
continue;
|
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class *cl = ea_class_global[a->id];
|
|
|
|
ASSERT_DIE(cl && cl->uc);
|
2022-05-15 13:53:35 +00:00
|
|
|
|
|
|
|
CALL(cl->freed, a);
|
2022-03-19 15:23:42 +00:00
|
|
|
if (!--cl->uc)
|
|
|
|
ea_class_free(cl);
|
2010-08-02 11:11:53 +00:00
|
|
|
}
|
2022-06-16 21:24:53 +00:00
|
|
|
|
|
|
|
if (l->next)
|
2024-06-11 11:16:50 +00:00
|
|
|
ea_free_later(l->next);
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
2015-05-31 21:25:33 +00:00
|
|
|
|
2015-05-09 16:50:15 +00:00
|
|
|
void
|
2019-08-17 11:36:36 +00:00
|
|
|
ea_format_bitfield(const struct eattr *a, byte *buf, int bufsize, const char **names, int min, int max)
|
2015-05-09 16:50:15 +00:00
|
|
|
{
|
2024-03-28 18:46:24 +00:00
|
|
|
byte *start = buf;
|
2015-05-09 16:50:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-03-28 18:46:24 +00:00
|
|
|
buf += bsprintf(buf, "%s ", names[i]);
|
2015-05-09 16:50:15 +00:00
|
|
|
data &= ~(1u << i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data)
|
2024-03-28 18:46:24 +00:00
|
|
|
bsprintf(buf, "%08x ", data);
|
2015-05-09 16:50:15 +00:00
|
|
|
|
2024-03-28 18:46:24 +00:00
|
|
|
if (buf != start)
|
|
|
|
buf--;
|
|
|
|
|
|
|
|
*buf = 0;
|
2015-05-09 16:50:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 17:43:30 +00:00
|
|
|
static inline void
|
2018-12-27 13:26:11 +00:00
|
|
|
opaque_format(const struct adata *ad, byte *buf, uint size)
|
2011-07-03 17:43:30 +00:00
|
|
|
{
|
|
|
|
byte *bound = buf + size - 10;
|
2016-10-14 13:37:04 +00:00
|
|
|
uint i;
|
2011-07-03 17:43:30 +00: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
|
2022-06-27 10:14:05 +00:00
|
|
|
ea_show_int_set(struct cli *c, const char *name, const struct adata *ad, int way, byte *buf)
|
2011-07-03 17:43:30 +00:00
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
int nlen = strlen(name);
|
|
|
|
int i = int_set_format(ad, way, 0, buf, CLI_MSG_SIZE - nlen - 3);
|
|
|
|
cli_printf(c, -1012, "\t%s: %s", name, buf);
|
2011-07-03 17:43:30 +00:00
|
|
|
while (i)
|
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
i = int_set_format(ad, way, i, buf, CLI_MSG_SIZE - 1);
|
2011-08-15 18:54:58 +00:00
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
2011-07-03 17:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-12 19:03:43 +00:00
|
|
|
static inline void
|
2022-06-27 10:14:05 +00:00
|
|
|
ea_show_ec_set(struct cli *c, const char *name, const struct adata *ad, byte *buf)
|
2011-08-12 19:03:43 +00:00
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
int nlen = strlen(name);
|
|
|
|
int i = ec_set_format(ad, 0, buf, CLI_MSG_SIZE - nlen - 3);
|
|
|
|
cli_printf(c, -1012, "\t%s: %s", name, buf);
|
2011-08-12 19:03:43 +00:00
|
|
|
while (i)
|
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
i = ec_set_format(ad, i, buf, CLI_MSG_SIZE - 1);
|
2011-08-15 18:54:58 +00:00
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
2011-08-12 19:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-01 10:50:29 +00:00
|
|
|
static inline void
|
2022-06-27 10:14:05 +00:00
|
|
|
ea_show_lc_set(struct cli *c, const char *name, const struct adata *ad, byte *buf)
|
2016-10-01 10:50:29 +00:00
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
int nlen = strlen(name);
|
|
|
|
int i = lc_set_format(ad, 0, buf, CLI_MSG_SIZE - nlen - 3);
|
|
|
|
cli_printf(c, -1012, "\t%s: %s", name, buf);
|
2016-10-01 10:50:29 +00:00
|
|
|
while (i)
|
|
|
|
{
|
2022-06-27 10:14:05 +00:00
|
|
|
i = lc_set_format(ad, i, buf, CLI_MSG_SIZE - 1);
|
2016-10-01 10:50:29 +00:00
|
|
|
cli_printf(c, -1012, "\t\t%s", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 11:39:28 +00:00
|
|
|
void
|
|
|
|
ea_show_nexthop_list(struct cli *c, struct nexthop_adata *nhad)
|
|
|
|
{
|
|
|
|
if (!NEXTHOP_IS_REACHABLE(nhad))
|
|
|
|
return;
|
|
|
|
|
|
|
|
NEXTHOP_WALK(nh, nhad)
|
|
|
|
{
|
|
|
|
char mpls[MPLS_MAX_LABEL_STACK*12 + 5], *lsp = mpls;
|
|
|
|
char *onlink = (nh->flags & RNF_ONLINK) ? " onlink" : "";
|
|
|
|
char weight[16] = "";
|
|
|
|
|
|
|
|
if (nh->labels)
|
|
|
|
{
|
|
|
|
lsp += bsprintf(lsp, " mpls %d", nh->label[0]);
|
|
|
|
for (int i=1;i<nh->labels; i++)
|
|
|
|
lsp += bsprintf(lsp, "/%d", nh->label[i]);
|
|
|
|
}
|
|
|
|
*lsp = '\0';
|
|
|
|
|
|
|
|
if (!NEXTHOP_ONE(nhad))
|
|
|
|
bsprintf(weight, " weight %d", nh->weight + 1);
|
|
|
|
|
|
|
|
if (ipa_nonzero(nh->gw))
|
2022-06-20 17:10:49 +00:00
|
|
|
if (nh->iface)
|
|
|
|
cli_printf(c, -1007, "\tvia %I on %s%s%s%s",
|
|
|
|
nh->gw, nh->iface->name, mpls, onlink, weight);
|
|
|
|
else
|
|
|
|
cli_printf(c, -1007, "\tvia %I", nh->gw);
|
2022-06-27 11:39:28 +00:00
|
|
|
else
|
|
|
|
cli_printf(c, -1007, "\tdev %s%s%s",
|
|
|
|
nh->iface->name, mpls, onlink, weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ea_show_hostentry(const struct adata *ad, byte *buf, uint size)
|
|
|
|
{
|
|
|
|
const struct hostentry_adata *had = (const struct hostentry_adata *) ad;
|
|
|
|
|
2024-01-29 15:56:22 +00:00
|
|
|
uint s = 0;
|
|
|
|
|
2022-06-27 11:39:28 +00:00
|
|
|
if (ipa_nonzero(had->he->link) && !ipa_equal(had->he->link, had->he->addr))
|
2024-03-27 22:16:51 +00:00
|
|
|
s = bsnprintf(buf, size, "via %I %I table %s", had->he->addr, had->he->link, had->he->owner->name);
|
2022-06-27 11:39:28 +00:00
|
|
|
else
|
2024-03-27 22:16:51 +00:00
|
|
|
s = bsnprintf(buf, size, "via %I table %s", had->he->addr, had->he->owner->name);
|
2024-01-29 15:56:22 +00:00
|
|
|
|
|
|
|
uint lc = HOSTENTRY_LABEL_COUNT(had);
|
|
|
|
if (!lc)
|
|
|
|
return;
|
|
|
|
|
2024-03-27 22:26:25 +00:00
|
|
|
s = bsnprintf((buf += s), (size -= s), " mpls");
|
2024-01-29 15:56:22 +00:00
|
|
|
for (uint i=0; i < lc; i++)
|
|
|
|
s = bsnprintf((buf += s), (size -= s), " %u", had->labels[i]);
|
2022-06-27 11:39:28 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 12:29:55 +00:00
|
|
|
/**
|
2011-07-03 17:43:30 +00:00
|
|
|
* ea_show - print an &eattr to CLI
|
|
|
|
* @c: destination CLI
|
|
|
|
* @e: attribute to be printed
|
2000-06-02 12:29:55 +00:00
|
|
|
*
|
2011-07-03 17:43:30 +00: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 12:29:55 +00:00
|
|
|
*
|
|
|
|
* If the protocol defining the attribute provides its own
|
|
|
|
* get_attr() hook, it's consulted first.
|
|
|
|
*/
|
2022-05-30 10:03:03 +00:00
|
|
|
static void
|
2019-08-17 11:36:36 +00:00
|
|
|
ea_show(struct cli *c, const eattr *e)
|
2000-04-01 10:19:47 +00:00
|
|
|
{
|
2018-12-27 13:26:11 +00:00
|
|
|
const struct adata *ad = (e->type & EAF_EMBEDDED) ? NULL : e->u.ptr;
|
2011-07-03 17:43:30 +00:00
|
|
|
byte buf[CLI_MSG_SIZE];
|
|
|
|
byte *pos = buf, *end = buf + sizeof(buf);
|
2000-04-01 10:19:47 +00:00
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
ASSERT_DIE(e->id < ea_class_max);
|
2010-08-02 11:11:53 +00:00
|
|
|
|
2022-03-19 15:23:42 +00:00
|
|
|
struct ea_class *cls = ea_class_global[e->id];
|
|
|
|
ASSERT_DIE(cls);
|
2022-03-14 10:13:48 +00:00
|
|
|
|
2022-06-27 10:14:05 +00:00
|
|
|
if (e->undef || cls->hidden)
|
2022-06-26 12:48:55 +00:00
|
|
|
return;
|
2022-03-19 15:23:42 +00:00
|
|
|
else if (cls->format)
|
|
|
|
cls->format(e, buf, end - buf);
|
|
|
|
else
|
|
|
|
switch (e->type)
|
|
|
|
{
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_INT:
|
2022-06-27 11:39:28 +00:00
|
|
|
if ((cls == &ea_gen_igp_metric) && e->u.data >= IGP_METRIC_UNKNOWN)
|
|
|
|
return;
|
|
|
|
|
2011-07-03 17:43:30 +00:00
|
|
|
bsprintf(pos, "%u", e->u.data);
|
2000-04-01 10:19:47 +00:00
|
|
|
break;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_OPAQUE:
|
2011-07-03 17:43:30 +00:00
|
|
|
opaque_format(ad, pos, end - pos);
|
2000-04-01 10:19:47 +00:00
|
|
|
break;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_IP:
|
2011-07-03 17:43:30 +00:00
|
|
|
bsprintf(pos, "%I", *(ip_addr *) ad->data);
|
2000-04-01 10:19:47 +00:00
|
|
|
break;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_QUAD:
|
2011-07-03 17:43:30 +00:00
|
|
|
bsprintf(pos, "%R", e->u.data);
|
2000-04-01 10:19:47 +00:00
|
|
|
break;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_PATH:
|
2011-07-03 17:43:30 +00:00
|
|
|
as_path_format(ad, pos, end - pos);
|
2000-04-17 10:18:55 +00:00
|
|
|
break;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_CLIST:
|
2024-11-28 07:22:06 +00:00
|
|
|
ea_show_int_set(c, cls->name, ad, ISF_COMMUNITY_LIST, buf);
|
2011-07-03 17:43:30 +00:00
|
|
|
return;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_ECLIST:
|
2022-06-27 10:14:05 +00:00
|
|
|
ea_show_ec_set(c, cls->name, ad, buf);
|
2011-08-12 19:03:43 +00:00
|
|
|
return;
|
2022-03-26 10:56:02 +00:00
|
|
|
case T_LCLIST:
|
2022-06-27 10:14:05 +00:00
|
|
|
ea_show_lc_set(c, cls->name, ad, buf);
|
2016-10-01 10:50:29 +00:00
|
|
|
return;
|
2024-03-28 15:22:23 +00:00
|
|
|
case T_STRING:
|
2024-02-14 12:58:56 +00:00
|
|
|
bsnprintf(pos, end - pos, "%s", (const char *) ad->data);
|
|
|
|
break;
|
2022-06-27 11:39:28 +00:00
|
|
|
case T_NEXTHOP_LIST:
|
|
|
|
ea_show_nexthop_list(c, (struct nexthop_adata *) e->u.ptr);
|
|
|
|
return;
|
|
|
|
case T_HOSTENTRY:
|
|
|
|
ea_show_hostentry(ad, pos, end - pos);
|
|
|
|
break;
|
2000-04-01 10:19:47 +00:00
|
|
|
default:
|
2011-07-03 17:43:30 +00:00
|
|
|
bsprintf(pos, "<type %02x>", e->type);
|
2022-03-19 15:23:42 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:04:57 +00:00
|
|
|
cli_printf(c, -1012, "\t%s: %s", cls->name, buf);
|
2000-04-01 10:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 16:08:37 +00:00
|
|
|
static void
|
2024-12-13 14:23:01 +00:00
|
|
|
nexthop_dump(struct dump_request *dreq, const struct adata *ad)
|
2022-05-05 16:08:37 +00:00
|
|
|
{
|
|
|
|
struct nexthop_adata *nhad = (struct nexthop_adata *) ad;
|
|
|
|
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(":");
|
2022-05-05 16:08:37 +00:00
|
|
|
|
2023-11-01 17:24:32 +00:00
|
|
|
if (!NEXTHOP_IS_REACHABLE(nhad))
|
|
|
|
{
|
|
|
|
const char *name = rta_dest_name(nhad->dest);
|
|
|
|
if (name)
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(" %s", name);
|
2023-11-01 17:24:32 +00:00
|
|
|
else
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(" D%d", nhad->dest);
|
2023-11-01 17:24:32 +00:00
|
|
|
}
|
|
|
|
else NEXTHOP_WALK(nh, nhad)
|
2022-05-05 16:08:37 +00:00
|
|
|
{
|
2024-12-13 14:23:01 +00:00
|
|
|
if (ipa_nonzero(nh->gw)) RDUMP(" ->%I", nh->gw);
|
|
|
|
if (nh->labels) RDUMP(" L %d", nh->label[0]);
|
2022-05-05 16:08:37 +00:00
|
|
|
for (int i=1; i<nh->labels; i++)
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("/%d", nh->label[i]);
|
|
|
|
RDUMP(" [%s]", nh->iface ? nh->iface->name : "???");
|
2022-05-05 16:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 12:29:55 +00: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 13:09:09 +00:00
|
|
|
void
|
2024-11-14 19:43:35 +00:00
|
|
|
ea_dump(struct dump_request *dreq, ea_list *e)
|
1999-03-17 13:09:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!e)
|
|
|
|
{
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("NONE");
|
1999-03-17 13:09:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (e)
|
|
|
|
{
|
2024-04-04 10:01:35 +00:00
|
|
|
struct ea_storage *s = e->stored ? ea_get_storage(e) : NULL;
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("[%c%c] overlay=%d uc=%d h=%08x",
|
1999-03-17 13:09:09 +00:00
|
|
|
(e->flags & EALF_SORTED) ? 'S' : 's',
|
|
|
|
(e->flags & EALF_BISECT) ? 'B' : 'b',
|
2024-04-04 10:01:35 +00:00
|
|
|
e->stored,
|
2024-06-11 08:48:21 +00:00
|
|
|
s ? atomic_load_explicit(&s->uc, memory_order_relaxed) : 0,
|
2024-04-01 14:01:26 +00:00
|
|
|
s ? s->hash_key : 0);
|
1999-03-17 13:09:09 +00:00
|
|
|
for(i=0; i<e->count; i++)
|
1998-05-20 11:54:33 +00:00
|
|
|
{
|
1999-03-17 13:09:09 +00:00
|
|
|
eattr *a = &e->attrs[i];
|
2024-01-28 12:09:48 +00:00
|
|
|
struct ea_class *clp = (a->id < ea_class_max) ? ea_class_global[a->id] : NULL;
|
|
|
|
if (clp)
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(" %s", clp->name);
|
2024-01-28 12:09:48 +00:00
|
|
|
else
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(" 0x%x", a->id);
|
2024-01-28 12:09:48 +00:00
|
|
|
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(".%02x", a->flags);
|
|
|
|
RDUMP("=%c",
|
2022-03-26 11:37:41 +00:00
|
|
|
"?iO?IRP???S??pE?"
|
|
|
|
"??L???N?????????"
|
|
|
|
"?o???r??????????" [a->type]);
|
2022-03-14 09:06:44 +00:00
|
|
|
if (a->originated)
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("o");
|
2024-01-28 12:09:48 +00:00
|
|
|
if (a->undef)
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(":undef");
|
2024-01-28 12:09:48 +00:00
|
|
|
else if (a->type & EAF_EMBEDDED)
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP(":%08x", a->u.data);
|
2022-05-05 16:08:37 +00:00
|
|
|
else if (a->id == ea_gen_nexthop.id)
|
2024-12-13 14:23:01 +00:00
|
|
|
nexthop_dump(dreq, a->u.ptr);
|
1999-03-17 13:09:09 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int j, len = a->u.ptr->length;
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("[%d]:", len);
|
1999-03-17 13:09:09 +00:00
|
|
|
for(j=0; j<len; j++)
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("%02x", a->u.ptr->data[j]);
|
1999-03-17 13:09:09 +00:00
|
|
|
}
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP(" ");
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
1999-03-17 13:09:09 +00:00
|
|
|
if (e = e->next)
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP(" | ");
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-02 12:29:55 +00: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.
|
|
|
|
*/
|
2024-04-04 10:01:35 +00:00
|
|
|
uint
|
2000-03-04 21:09:14 +00:00
|
|
|
ea_hash(ea_list *e)
|
|
|
|
{
|
2016-02-10 12:26:07 +00:00
|
|
|
const u64 mul = 0x68576150f3d6847;
|
|
|
|
u64 h = 0xafcef24eda8b29;
|
2000-03-04 21:09:14 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (e) /* Assuming chain of length 1 */
|
|
|
|
{
|
2022-06-16 21:24:53 +00:00
|
|
|
h ^= mem_hash(&e->next, sizeof(e->next));
|
2000-03-04 21:09:14 +00:00
|
|
|
for(i=0; i<e->count; i++)
|
|
|
|
{
|
|
|
|
struct eattr *a = &e->attrs[i];
|
2016-02-10 12:26:07 +00:00
|
|
|
h ^= a->id; h *= mul;
|
2022-06-16 21:24:53 +00:00
|
|
|
if (a->undef)
|
|
|
|
continue;
|
2000-03-04 21:09:14 +00:00
|
|
|
if (a->type & EAF_EMBEDDED)
|
|
|
|
h ^= a->u.data;
|
|
|
|
else
|
|
|
|
{
|
2018-12-27 13:26:11 +00:00
|
|
|
const struct adata *d = a->u.ptr;
|
2016-02-10 12:26:07 +00:00
|
|
|
h ^= mem_hash(d->data, d->length);
|
2000-03-04 21:09:14 +00:00
|
|
|
}
|
2016-02-10 12:26:07 +00:00
|
|
|
h *= mul;
|
2000-03-04 21:09:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 12:26:07 +00:00
|
|
|
return (h >> 32) ^ (h & 0xffffffff);
|
2000-03-04 21:09:14 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 12:29:55 +00: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-06 22:57:39 +00: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 13:09:09 +00:00
|
|
|
/*
|
|
|
|
* rta's
|
|
|
|
*/
|
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
static SPINHASH(struct ea_storage) rta_hash_table;
|
2000-03-04 21:09:14 +00:00
|
|
|
|
2024-06-04 10:09:41 +00:00
|
|
|
#define RTAH_KEY(a) a->l, a->hash_key
|
|
|
|
#define RTAH_NEXT(a) a->next_hash
|
|
|
|
#define RTAH_EQ(a, b) ((a)->hash_key == (b)->hash_key) && (ea_same((a), (b)))
|
|
|
|
#define RTAH_FN(a, h) h
|
2024-06-08 00:44:09 +00:00
|
|
|
#define RTAH_ORDER 12
|
2000-03-04 21:09:14 +00:00
|
|
|
|
2024-06-04 10:09:41 +00:00
|
|
|
#define RTAH_REHASH rta_rehash
|
|
|
|
#define RTAH_PARAMS /8, *2, 2, 2, 12, 28
|
2000-03-04 21:09:14 +00:00
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
static void RTAH_REHASH(void *_ UNUSED) {
|
|
|
|
int step;
|
|
|
|
|
|
|
|
RTA_LOCK;
|
|
|
|
SPINHASH_REHASH_PREPARE(&rta_hash_table, RTAH, struct ea_storage, step);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
|
|
|
|
if (!step) return;
|
|
|
|
|
|
|
|
if (step > 0) SPINHASH_REHASH_UP (&rta_hash_table, RTAH, struct ea_storage, step);
|
|
|
|
if (step < 0) SPINHASH_REHASH_DOWN(&rta_hash_table, RTAH, struct ea_storage, -step);
|
|
|
|
|
|
|
|
RTA_LOCK;
|
|
|
|
SPINHASH_REHASH_FINISH(&rta_hash_table, RTAH);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ea_list *
|
|
|
|
ea_lookup_existing(ea_list *o, u32 squash_upto, uint h)
|
|
|
|
{
|
|
|
|
struct ea_storage *r = NULL;
|
|
|
|
|
|
|
|
SPINHASH_BEGIN_CHAIN(rta_hash_table, RTAH, read, eap, o, h);
|
|
|
|
for (struct ea_storage *ea; ea = *eap; eap = &RTAH_NEXT(ea))
|
|
|
|
if (
|
|
|
|
(h == ea->hash_key) &&
|
|
|
|
ea_same(o, ea->l) &&
|
|
|
|
BIT32_TEST(&squash_upto, ea->l->stored) &&
|
|
|
|
(!r || r->l->stored > ea->l->stored))
|
|
|
|
r = ea;
|
|
|
|
if (r)
|
2024-06-11 08:48:21 +00:00
|
|
|
atomic_fetch_add_explicit(&r->uc, 1, memory_order_acq_rel);
|
2024-06-08 00:44:09 +00:00
|
|
|
SPINHASH_END_CHAIN(read);
|
|
|
|
|
|
|
|
return r ? r->l : NULL;
|
|
|
|
}
|
|
|
|
|
2000-03-04 21:09:14 +00:00
|
|
|
|
2000-06-02 12:29:55 +00:00
|
|
|
/**
|
|
|
|
* rta_lookup - look up a &rta in attribute cache
|
2000-06-07 12:29:08 +00:00
|
|
|
* @o: a un-cached &rta
|
2000-06-02 12:29:55 +00:00
|
|
|
*
|
2000-06-07 12:29:08 +00:00
|
|
|
* rta_lookup() gets an un-cached &rta structure and returns its cached
|
2000-06-02 12:29:55 +00: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.
|
|
|
|
*/
|
2022-05-30 10:03:03 +00:00
|
|
|
ea_list *
|
2024-04-04 10:01:35 +00:00
|
|
|
ea_lookup_slow(ea_list *o, u32 squash_upto, enum ea_stored oid)
|
1998-05-20 11:54:33 +00:00
|
|
|
{
|
2015-05-19 06:53:34 +00:00
|
|
|
uint h;
|
1998-05-20 11:54:33 +00:00
|
|
|
|
2024-04-04 10:01:35 +00:00
|
|
|
ASSERT(o->stored != oid);
|
|
|
|
ASSERT(oid);
|
|
|
|
o = ea_normalize(o, squash_upto);
|
2022-05-30 10:03:03 +00:00
|
|
|
h = ea_hash(o);
|
|
|
|
|
2024-04-04 10:01:35 +00:00
|
|
|
squash_upto |= BIT32_VAL(oid);
|
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
struct ea_list *rr = ea_lookup_existing(o, squash_upto, h);
|
|
|
|
if (rr) return rr;
|
2021-09-27 15:44:19 +00:00
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
RTA_LOCK;
|
|
|
|
if (rr = ea_lookup_existing(o, squash_upto, oid))
|
2024-06-04 10:09:41 +00:00
|
|
|
{
|
|
|
|
RTA_UNLOCK;
|
2024-06-08 00:44:09 +00:00
|
|
|
return rr;
|
2024-06-04 10:09:41 +00:00
|
|
|
}
|
1999-03-17 13:09:09 +00:00
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
struct ea_storage *r = NULL;
|
2022-05-30 10:03:03 +00:00
|
|
|
uint elen = ea_list_size(o);
|
2022-10-12 16:04:39 +00:00
|
|
|
uint sz = elen + sizeof(struct ea_storage);
|
|
|
|
for (uint i=0; i<ARRAY_SIZE(ea_slab_sizes); i++)
|
|
|
|
if (sz <= ea_slab_sizes[i])
|
|
|
|
{
|
|
|
|
r = sl_alloc(ea_slab[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int huge = r ? 0 : EALF_HUGE;;
|
|
|
|
if (huge)
|
|
|
|
r = mb_alloc(rta_pool, sz);
|
|
|
|
|
2022-06-28 08:51:00 +00:00
|
|
|
ea_list_copy(r->l, o, elen);
|
|
|
|
ea_list_ref(r->l);
|
1999-03-17 13:09:09 +00:00
|
|
|
|
2024-04-04 10:01:35 +00:00
|
|
|
r->l->flags |= huge;
|
|
|
|
r->l->stored = oid;
|
2000-03-04 21:09:14 +00:00
|
|
|
r->hash_key = h;
|
2024-06-11 08:48:21 +00:00
|
|
|
atomic_store_explicit(&r->uc, 1, memory_order_release);
|
2022-05-30 10:03:03 +00:00
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
SPINHASH_INSERT(rta_hash_table, RTAH, r);
|
2000-03-04 21:09:14 +00:00
|
|
|
|
2021-09-27 15:44:19 +00:00
|
|
|
RTA_UNLOCK;
|
2022-06-28 08:51:00 +00:00
|
|
|
return r->l;
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 18:37:21 +00:00
|
|
|
#define EA_UC_SUB (1ULL << 44)
|
|
|
|
#define EA_UC_IN_PROGRESS(x) ((x) >> 44)
|
2024-06-11 11:16:50 +00:00
|
|
|
#define EA_UC_ASSIGNED(x) ((x) & (EA_UC_SUB-1))
|
2024-06-08 00:44:09 +00:00
|
|
|
|
2024-06-11 11:16:50 +00:00
|
|
|
#define EA_UC_DONE(x) (EA_UC_ASSIGNED(x) == EA_UC_IN_PROGRESS(x))
|
2024-06-08 00:44:09 +00:00
|
|
|
|
2024-06-11 08:48:21 +00:00
|
|
|
void
|
|
|
|
ea_free_deferred(struct deferred_call *dc)
|
|
|
|
{
|
|
|
|
struct ea_storage *r = ea_get_storage(SKIP_BACK(struct ea_free_deferred, dc, dc)->attrs);
|
2024-06-11 11:16:50 +00:00
|
|
|
|
2024-06-20 18:37:21 +00:00
|
|
|
/* Indicate free intent */
|
|
|
|
u64 prev = atomic_fetch_add_explicit(&r->uc, EA_UC_SUB, memory_order_acq_rel);
|
2024-06-11 11:16:50 +00:00
|
|
|
prev |= EA_UC_SUB;
|
|
|
|
|
|
|
|
if (!EA_UC_DONE(prev))
|
|
|
|
{
|
|
|
|
/* The easy case: somebody else holds the usecount */
|
|
|
|
ASSERT_DIE(EA_UC_IN_PROGRESS(prev) < EA_UC_ASSIGNED(prev));
|
|
|
|
atomic_fetch_sub_explicit(&r->uc, EA_UC_SUB + 1, memory_order_acq_rel);
|
2024-06-11 08:48:21 +00:00
|
|
|
return;
|
2024-06-11 11:16:50 +00:00
|
|
|
}
|
2015-06-08 00:20:43 +00:00
|
|
|
|
2024-06-20 18:37:21 +00:00
|
|
|
/* Wait for others to finish */
|
|
|
|
while (EA_UC_DONE(prev) && (EA_UC_IN_PROGRESS(prev) > 1))
|
|
|
|
prev = atomic_load_explicit(&r->uc, memory_order_acquire);
|
|
|
|
|
|
|
|
if (!EA_UC_DONE(prev))
|
|
|
|
{
|
|
|
|
/* Somebody else has found us inbetween, no need to free */
|
|
|
|
ASSERT_DIE(EA_UC_IN_PROGRESS(prev) < EA_UC_ASSIGNED(prev));
|
|
|
|
atomic_fetch_sub_explicit(&r->uc, EA_UC_SUB + 1, memory_order_acq_rel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:16:50 +00:00
|
|
|
/* We are the last one to free this ea */
|
2024-06-11 08:48:21 +00:00
|
|
|
RTA_LOCK;
|
2024-06-11 11:16:50 +00:00
|
|
|
|
|
|
|
/* Trying to remove the ea from the hash */
|
|
|
|
SPINHASH_REMOVE(rta_hash_table, RTAH, r);
|
|
|
|
|
|
|
|
/* Somebody has cloned this rta inbetween. This sometimes happens. */
|
|
|
|
prev = atomic_load_explicit(&r->uc, memory_order_acquire);
|
2024-06-20 18:37:21 +00:00
|
|
|
if (!EA_UC_DONE(prev) || (EA_UC_IN_PROGRESS(prev) > 1))
|
2024-06-11 11:16:50 +00:00
|
|
|
{
|
2024-06-20 18:37:21 +00:00
|
|
|
/* Reinsert the ea and go away */
|
2024-06-11 11:16:50 +00:00
|
|
|
SPINHASH_INSERT(rta_hash_table, RTAH, r);
|
|
|
|
atomic_fetch_sub_explicit(&r->uc, EA_UC_SUB + 1, memory_order_acq_rel);
|
|
|
|
RTA_UNLOCK;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-20 18:37:21 +00:00
|
|
|
/* Fine, now everybody is actually done */
|
2024-06-11 11:16:50 +00:00
|
|
|
ASSERT_DIE(atomic_load_explicit(&r->uc, memory_order_acquire) == EA_UC_SUB + 1);
|
|
|
|
|
|
|
|
/* And now we can free the object, finally */
|
|
|
|
ea_list_unref(r->l);
|
|
|
|
if (r->l->flags & EALF_HUGE)
|
|
|
|
mb_free(r);
|
|
|
|
else
|
|
|
|
sl_free(r);
|
|
|
|
|
2022-08-03 15:37:16 +00:00
|
|
|
RTA_UNLOCK;
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 11:16:50 +00:00
|
|
|
|
2000-06-02 12:29:55 +00:00
|
|
|
/**
|
|
|
|
* rta_dump_all - dump attribute cache
|
|
|
|
*
|
|
|
|
* This function dumps the whole contents of route attribute cache
|
|
|
|
* to the debug output.
|
|
|
|
*/
|
1998-05-20 11:54:33 +00:00
|
|
|
void
|
2024-12-13 14:23:01 +00:00
|
|
|
ea_dump_all(struct dump_request *dreq)
|
1998-05-20 11:54:33 +00:00
|
|
|
{
|
2024-12-13 14:23:01 +00:00
|
|
|
RDUMP("Route attribute cache (%d entries, order %d):\n",
|
2024-06-08 00:44:09 +00:00
|
|
|
atomic_load_explicit(&rta_hash_table.count, memory_order_relaxed),
|
2024-06-19 18:53:44 +00:00
|
|
|
atomic_load_explicit(&rta_hash_table.cur, memory_order_relaxed)->order);
|
2024-06-04 10:09:41 +00:00
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
SPINHASH_WALK(rta_hash_table, RTAH, a)
|
2000-03-04 21:09:14 +00:00
|
|
|
{
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("%p ", a);
|
2024-12-13 14:23:01 +00:00
|
|
|
ea_dump(dreq, a->l);
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("\n");
|
2000-03-04 21:09:14 +00:00
|
|
|
}
|
2024-06-08 00:44:09 +00:00
|
|
|
SPINHASH_WALK_END;
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("\n");
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
|
|
|
|
1999-12-01 15:10:21 +00:00
|
|
|
void
|
2022-05-30 10:03:03 +00:00
|
|
|
ea_show_list(struct cli *c, ea_list *eal)
|
1999-12-01 15:10:21 +00:00
|
|
|
{
|
2022-06-27 10:44:11 +00:00
|
|
|
ea_list *n = ea_normalize(eal, 0);
|
|
|
|
for (int i =0; i < n->count; i++)
|
|
|
|
ea_show(c, &n->attrs[i]);
|
1999-12-01 15:10:21 +00:00
|
|
|
}
|
|
|
|
|
2000-06-02 12:29:55 +00: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 11:54:33 +00:00
|
|
|
void
|
|
|
|
rta_init(void)
|
|
|
|
{
|
2024-06-27 06:42:11 +00:00
|
|
|
attrs_domain = DOMAIN_NEW(attrs);
|
2021-09-27 14:40:28 +00:00
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
RTA_LOCK;
|
|
|
|
rta_pool = rp_new(&root_pool, attrs_domain.attrs, "Attributes");
|
2016-06-10 12:34:41 +00:00
|
|
|
|
2022-10-12 16:04:39 +00:00
|
|
|
for (uint i=0; i<ARRAY_SIZE(ea_slab_sizes); i++)
|
|
|
|
ea_slab[i] = sl_new(rta_pool, ea_slab_sizes[i]);
|
|
|
|
|
2024-06-08 00:44:09 +00:00
|
|
|
SPINHASH_INIT(rta_hash_table, RTAH, rta_pool, &global_work_list);
|
2024-06-04 10:09:41 +00:00
|
|
|
|
2012-08-14 14:25:22 +00:00
|
|
|
rte_src_init();
|
2022-03-19 15:23:42 +00:00
|
|
|
ea_class_init();
|
|
|
|
|
2023-04-21 13:26:06 +00:00
|
|
|
RTA_UNLOCK;
|
|
|
|
|
2022-06-27 11:39:28 +00:00
|
|
|
/* These attributes are required to be first for nice "show route" output */
|
|
|
|
ea_register_init(&ea_gen_nexthop);
|
|
|
|
ea_register_init(&ea_gen_hostentry);
|
2024-04-04 09:38:52 +00:00
|
|
|
ea_register_init(&ea_gen_hostentry_version);
|
2022-06-27 11:39:28 +00:00
|
|
|
|
|
|
|
/* Other generic route attributes */
|
2022-04-20 10:24:26 +00:00
|
|
|
ea_register_init(&ea_gen_preference);
|
2022-03-19 15:23:42 +00:00
|
|
|
ea_register_init(&ea_gen_igp_metric);
|
2022-04-20 11:56:04 +00:00
|
|
|
ea_register_init(&ea_gen_from);
|
2022-05-04 12:41:51 +00:00
|
|
|
ea_register_init(&ea_gen_source);
|
2022-05-15 16:09:30 +00:00
|
|
|
ea_register_init(&ea_gen_flowspec_valid);
|
2023-11-08 20:51:46 +00:00
|
|
|
|
|
|
|
/* MPLS route attributes */
|
|
|
|
ea_register_init(&ea_gen_mpls_policy);
|
|
|
|
ea_register_init(&ea_gen_mpls_class);
|
|
|
|
ea_register_init(&ea_gen_mpls_label);
|
2024-11-28 07:22:06 +00:00
|
|
|
|
|
|
|
/* ASPA providers */
|
|
|
|
ea_register_init(&ea_gen_aspa_providers);
|
1998-05-20 11:54:33 +00:00
|
|
|
}
|
2000-06-02 12:29:55 +00: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
|