2023-12-04 09:33:30 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Semi-global index of nets
|
|
|
|
*
|
|
|
|
* (c) 2023 Maria Matejka <mq@jmq.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib/birdlib.h"
|
|
|
|
#include "lib/netindex_private.h"
|
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
#define NETINDEX_INIT_BLOCK_SIZE 128
|
|
|
|
|
2023-12-04 09:33:30 +00:00
|
|
|
#define NETINDEX_KEY(n) (n)->hash, (n)->addr
|
|
|
|
#define NETINDEX_NEXT(n) (n)->next
|
|
|
|
#define NETINDEX_EQ(h,n,i,o) ((h == i) && net_equal(n,o))
|
|
|
|
#define NETINDEX_FN(h,n) (h)
|
2024-06-08 16:26:02 +00:00
|
|
|
#define NETINDEX_ORDER 12 /* Initial */
|
2023-12-04 09:33:30 +00:00
|
|
|
|
|
|
|
#define NETINDEX_REHASH netindex_rehash
|
2024-06-08 16:26:02 +00:00
|
|
|
#define NETINDEX_PARAMS /8, *2, 2, 2, 12, 28
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
static void NETINDEX_REHASH(void *_v) {
|
2024-06-11 13:59:19 +00:00
|
|
|
log(L_TRACE "Netindex rehash: begin");
|
2024-06-07 23:37:24 +00:00
|
|
|
netindex_spinhash *v = _v;
|
|
|
|
int step;
|
2024-06-19 18:53:44 +00:00
|
|
|
SPINHASH_REHASH_PREPARE(v,NETINDEX,struct netindex,step);
|
2024-06-07 23:37:24 +00:00
|
|
|
|
2024-06-11 13:59:19 +00:00
|
|
|
log(L_TRACE "Netindex rehash: step=%d", step);
|
2024-06-07 23:37:24 +00:00
|
|
|
if (!step) return;
|
|
|
|
|
|
|
|
if (step > 0) SPINHASH_REHASH_UP(v,NETINDEX,struct netindex,step);
|
|
|
|
if (step < 0) SPINHASH_REHASH_DOWN(v,NETINDEX,struct netindex,-step);
|
|
|
|
|
2024-06-11 13:59:19 +00:00
|
|
|
log(L_TRACE "Netindex rehash: time to finish");
|
2024-06-19 18:53:44 +00:00
|
|
|
SPINHASH_REHASH_FINISH(v,NETINDEX);
|
2024-06-11 13:59:19 +00:00
|
|
|
log(L_TRACE "Netindex rehash: done");
|
2024-06-07 23:37:24 +00:00
|
|
|
}
|
2023-12-04 09:33:30 +00:00
|
|
|
|
|
|
|
static void netindex_hash_cleanup(void *netindex_hash);
|
|
|
|
|
|
|
|
static struct netindex *
|
2024-06-07 23:37:24 +00:00
|
|
|
net_lock_revive_unlock(netindex_hash *h, struct netindex *i)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-04-01 18:04:14 +00:00
|
|
|
if (!i)
|
2023-12-04 09:33:30 +00:00
|
|
|
return NULL;
|
|
|
|
|
2024-04-01 18:04:14 +00:00
|
|
|
lfuc_lock_revive(&i->uc);
|
2024-06-07 23:37:24 +00:00
|
|
|
lfuc_unlock(&i->uc, h->cleanup_list, &h->cleanup_event);
|
2024-04-01 18:04:14 +00:00
|
|
|
return i;
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Index initialization
|
|
|
|
*/
|
|
|
|
netindex_hash *
|
2024-06-05 15:47:32 +00:00
|
|
|
netindex_hash_new(pool *sp, event_list *cleanup_target, u8 type)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-06-10 21:06:43 +00:00
|
|
|
DOMAIN(attrs) dom = DOMAIN_NEW_RCU_SYNC(attrs);
|
2023-12-04 09:33:30 +00:00
|
|
|
LOCK_DOMAIN(attrs, dom);
|
|
|
|
|
|
|
|
pool *p = rp_new(sp, dom.attrs, "Network index");
|
|
|
|
|
|
|
|
struct netindex_hash_private *nh = mb_allocz(p, sizeof *nh);
|
|
|
|
nh->lock = dom;
|
|
|
|
nh->pool = p;
|
2024-06-05 15:47:32 +00:00
|
|
|
nh->net_type = type;
|
|
|
|
|
|
|
|
nh->slab = net_addr_length[type] ? sl_new(nh->pool, sizeof (struct netindex) + net_addr_length[type]) : NULL;
|
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
SPINHASH_INIT(nh->hash, NETINDEX, nh->pool, cleanup_target);
|
2024-06-06 15:45:35 +00:00
|
|
|
atomic_store_explicit(&nh->block_size, NETINDEX_INIT_BLOCK_SIZE, memory_order_release);
|
|
|
|
atomic_store_explicit(&nh->block,
|
|
|
|
mb_allocz(nh->pool, NETINDEX_INIT_BLOCK_SIZE * sizeof *nh->block),
|
|
|
|
memory_order_release);
|
2024-06-05 15:47:32 +00:00
|
|
|
|
|
|
|
hmap_init(&nh->id_map, nh->pool, 128);
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-05-30 20:59:08 +00:00
|
|
|
nh->cleanup_list = cleanup_target;
|
2023-12-04 09:33:30 +00:00
|
|
|
nh->cleanup_event = (event) { .hook = netindex_hash_cleanup, nh };
|
|
|
|
|
|
|
|
UNLOCK_DOMAIN(attrs, dom);
|
|
|
|
return SKIP_BACK(netindex_hash, priv, nh);
|
|
|
|
}
|
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
static uint
|
|
|
|
netindex_hash_cleanup_removed(struct netindex_hash_private *nh, struct netindex * _Atomic *block, struct netindex **removed, uint cnt)
|
|
|
|
{
|
|
|
|
synchronize_rcu();
|
|
|
|
|
|
|
|
uint kept = 0;
|
|
|
|
for (uint q = 0; q < cnt; q++)
|
|
|
|
{
|
|
|
|
struct netindex *ni = removed[q];
|
|
|
|
|
|
|
|
/* Now no reader can possibly still have the old pointer,
|
|
|
|
* unless somebody found it inbetween and ref'd it. */
|
|
|
|
if (!lfuc_finished(&ni->uc))
|
|
|
|
{
|
|
|
|
/* Collision, return the netindex back. */
|
|
|
|
ASSERT_DIE(NULL == atomic_exchange_explicit(&block[ni->index], ni, memory_order_acq_rel));
|
|
|
|
SPINHASH_INSERT(nh->hash, NETINDEX, ni);
|
|
|
|
kept++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now the netindex is definitely obsolete, we can free it */
|
|
|
|
hmap_clear(&nh->id_map, ni->index);
|
|
|
|
|
|
|
|
if (nh->slab)
|
|
|
|
sl_free(ni);
|
|
|
|
else
|
|
|
|
mb_free(ni);
|
|
|
|
}
|
|
|
|
|
|
|
|
return kept;
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:33:30 +00:00
|
|
|
static void
|
|
|
|
netindex_hash_cleanup(void *_nh)
|
|
|
|
{
|
2024-05-30 20:59:08 +00:00
|
|
|
struct netindex_hash_private *nh = _nh;
|
|
|
|
|
|
|
|
DOMAIN(attrs) dom = nh->lock;
|
|
|
|
LOCK_DOMAIN(attrs, dom);
|
|
|
|
|
|
|
|
uint kept = 0;
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
uint bs = atomic_load_explicit(&nh->block_size, memory_order_relaxed);
|
|
|
|
struct netindex * _Atomic *block = atomic_load_explicit(&nh->block, memory_order_relaxed);
|
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
#define REMOVED_MAX 256
|
|
|
|
struct netindex *removed[REMOVED_MAX];
|
|
|
|
uint removed_cnt = 0;
|
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
for (uint i = 0; i < bs; i++)
|
2024-06-05 15:47:32 +00:00
|
|
|
{
|
2024-06-06 15:45:35 +00:00
|
|
|
struct netindex *ni = atomic_load_explicit(&block[i], memory_order_acquire);
|
2024-06-05 15:47:32 +00:00
|
|
|
if (!ni)
|
|
|
|
continue;
|
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
/* We may use the acquired netindex pointer as we are
|
|
|
|
* the only process which deletes them */
|
2024-06-05 15:47:32 +00:00
|
|
|
ASSERT_DIE(i == ni->index);
|
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
/* Check finished */
|
|
|
|
if (!lfuc_finished(&ni->uc))
|
2024-05-30 20:59:08 +00:00
|
|
|
{
|
2024-06-06 15:45:35 +00:00
|
|
|
kept++;
|
|
|
|
continue;
|
2024-05-30 20:59:08 +00:00
|
|
|
}
|
2024-06-06 15:45:35 +00:00
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
/* Looks finished, try dropping */
|
|
|
|
ASSERT_DIE(ni == atomic_exchange_explicit(&block[i], NULL, memory_order_acq_rel));
|
|
|
|
SPINHASH_REMOVE(nh->hash, NETINDEX, ni);
|
2024-06-06 15:45:35 +00:00
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
/* Store into the removed-block */
|
|
|
|
removed[removed_cnt++] = ni;
|
2024-06-06 15:45:35 +00:00
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
/* If removed-block is full, flush it */
|
|
|
|
if (removed_cnt == REMOVED_MAX)
|
2024-06-06 15:45:35 +00:00
|
|
|
{
|
2024-06-12 12:47:20 +00:00
|
|
|
kept += netindex_hash_cleanup_removed(nh, block, removed, removed_cnt);
|
|
|
|
removed_cnt = 0;
|
2024-06-06 15:45:35 +00:00
|
|
|
}
|
2024-06-05 15:47:32 +00:00
|
|
|
}
|
2024-05-30 20:59:08 +00:00
|
|
|
|
2024-06-12 12:47:20 +00:00
|
|
|
/* Flush remaining netindexes */
|
|
|
|
if (removed_cnt)
|
|
|
|
kept += netindex_hash_cleanup_removed(nh, block, removed, removed_cnt);
|
|
|
|
|
|
|
|
/* Return now unless we're deleted */
|
2024-05-30 20:59:08 +00:00
|
|
|
if (kept || !nh->deleted_event)
|
|
|
|
{
|
|
|
|
UNLOCK_DOMAIN(attrs, dom);
|
|
|
|
return;
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 20:59:08 +00:00
|
|
|
ev_postpone(&nh->cleanup_event);
|
|
|
|
|
|
|
|
event *e = nh->deleted_event;
|
|
|
|
event_list *t = nh->deleted_target;
|
|
|
|
|
|
|
|
/* Check cleanliness */
|
2024-06-07 23:37:24 +00:00
|
|
|
SPINHASH_WALK(nh->hash, NETINDEX, i)
|
2024-06-05 15:47:32 +00:00
|
|
|
bug("Stray netindex in deleted hash");
|
2024-06-07 23:37:24 +00:00
|
|
|
SPINHASH_WALK_END;
|
2024-05-30 20:59:08 +00:00
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
/* Cleanup the spinhash itself */
|
|
|
|
SPINHASH_FREE(nh->hash);
|
|
|
|
|
|
|
|
/* Pool free is enough to drop everything else */
|
2024-05-30 20:59:08 +00:00
|
|
|
rp_free(nh->pool);
|
|
|
|
|
|
|
|
/* And only the lock remains */
|
|
|
|
UNLOCK_DOMAIN(attrs, dom);
|
|
|
|
DOMAIN_FREE(attrs, dom);
|
|
|
|
|
|
|
|
/* Notify the requestor */
|
|
|
|
ev_send(t, e);
|
|
|
|
}
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-05-30 20:59:08 +00:00
|
|
|
void
|
|
|
|
netindex_hash_delete(netindex_hash *h, event *e, event_list *t)
|
|
|
|
{
|
|
|
|
NH_LOCK(h, hp);
|
|
|
|
|
|
|
|
hp->deleted_event = e;
|
|
|
|
hp->deleted_target = t;
|
|
|
|
|
|
|
|
ev_send(hp->cleanup_list, &hp->cleanup_event);
|
|
|
|
}
|
|
|
|
|
2023-12-04 09:33:30 +00:00
|
|
|
/*
|
|
|
|
* Private index manipulation
|
|
|
|
*/
|
2024-06-07 23:37:24 +00:00
|
|
|
static struct netindex *
|
|
|
|
net_find_index_fragile(netindex_hash *nh, const net_addr *n)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-06-07 23:37:24 +00:00
|
|
|
ASSERT_DIE(n->type == nh->net_type);
|
2024-05-30 20:59:08 +00:00
|
|
|
|
2023-12-04 09:33:30 +00:00
|
|
|
u32 h = net_hash(n);
|
2024-06-07 23:37:24 +00:00
|
|
|
return SPINHASH_FIND(nh->hash, NETINDEX, h, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static _Bool
|
|
|
|
net_validate_index(netindex_hash *h, struct netindex *ni)
|
|
|
|
{
|
|
|
|
struct netindex * _Atomic *block = atomic_load_explicit(&h->block, memory_order_relaxed);
|
|
|
|
u32 bs = atomic_load_explicit(&h->block_size, memory_order_relaxed);
|
|
|
|
|
|
|
|
ASSERT_DIE(ni->index < bs);
|
|
|
|
struct netindex *bni = atomic_load_explicit(&block[ni->index], memory_order_acquire);
|
2024-06-12 12:47:20 +00:00
|
|
|
return (bni == ni);
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct netindex *
|
2024-04-01 18:04:14 +00:00
|
|
|
net_new_index_locked(struct netindex_hash_private *hp, const net_addr *n)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-05-30 20:59:08 +00:00
|
|
|
ASSERT_DIE(!hp->deleted_event);
|
|
|
|
|
2024-06-05 15:47:32 +00:00
|
|
|
u32 i = hmap_first_zero(&hp->id_map);
|
|
|
|
hmap_set(&hp->id_map, i);
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-06-05 15:47:32 +00:00
|
|
|
struct netindex *ni = hp->slab ?
|
|
|
|
sl_alloc(hp->slab) :
|
2023-12-04 09:33:30 +00:00
|
|
|
mb_alloc(hp->pool, n->length + sizeof *ni);
|
|
|
|
|
|
|
|
*ni = (struct netindex) {
|
|
|
|
.hash = net_hash(n),
|
|
|
|
.index = i,
|
|
|
|
};
|
|
|
|
net_copy(ni->addr, n);
|
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
SPINHASH_INSERT(hp->hash, NETINDEX, ni);
|
2024-06-06 15:45:35 +00:00
|
|
|
|
|
|
|
struct netindex * _Atomic *block = atomic_load_explicit(&hp->block, memory_order_relaxed);
|
|
|
|
u32 bs = atomic_load_explicit(&hp->block_size, memory_order_relaxed);
|
|
|
|
u32 nbs = bs;
|
|
|
|
while (nbs <= i)
|
|
|
|
nbs *= 2;
|
|
|
|
|
|
|
|
if (nbs > bs)
|
2024-05-20 13:57:13 +00:00
|
|
|
{
|
2024-06-06 15:45:35 +00:00
|
|
|
struct netindex * _Atomic *nb = mb_alloc(hp->pool, bs * 2 * sizeof *nb);
|
|
|
|
memcpy(nb, block, bs * sizeof *nb);
|
|
|
|
memset(&nb[bs], 0, (nbs - bs) * sizeof *nb);
|
|
|
|
|
|
|
|
ASSERT_DIE(block == atomic_exchange_explicit(&hp->block, nb, memory_order_acq_rel));
|
|
|
|
ASSERT_DIE(bs == atomic_exchange_explicit(&hp->block_size, nbs, memory_order_acq_rel));
|
|
|
|
synchronize_rcu();
|
2024-05-30 20:59:08 +00:00
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
mb_free(block);
|
|
|
|
block = nb;
|
2024-05-30 20:59:08 +00:00
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
hp->block_epoch++;
|
2024-05-20 13:57:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
ASSERT_DIE(i < nbs);
|
|
|
|
atomic_store_explicit(&block[i], ni, memory_order_release);
|
2023-12-04 09:33:30 +00:00
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
return ni;
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public entry points
|
|
|
|
*/
|
|
|
|
|
|
|
|
void net_lock_index(netindex_hash *h UNUSED, struct netindex *i)
|
|
|
|
{
|
|
|
|
// log(L_TRACE "Lock index %p", i);
|
2024-01-10 08:10:03 +00:00
|
|
|
lfuc_lock(&i->uc);
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void net_unlock_index(netindex_hash *h, struct netindex *i)
|
|
|
|
{
|
|
|
|
// log(L_TRACE "Unlock index %p", i);
|
2024-01-10 08:10:03 +00:00
|
|
|
lfuc_unlock(&i->uc, h->cleanup_list, &h->cleanup_event);
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct netindex *
|
2024-04-01 18:04:14 +00:00
|
|
|
net_find_index(netindex_hash *h, const net_addr *n)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-06-07 23:37:24 +00:00
|
|
|
RCU_ANCHOR(u);
|
|
|
|
struct netindex *ni = net_find_index_fragile(h, n);
|
|
|
|
return (ni && net_validate_index(h, ni)) ? net_lock_revive_unlock(h, ni) : NULL;
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct netindex *
|
2024-04-01 18:04:14 +00:00
|
|
|
net_get_index(netindex_hash *h, const net_addr *n)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-06-07 23:37:24 +00:00
|
|
|
struct netindex *ni = net_find_index(h, n);
|
|
|
|
if (ni) return ni;
|
2024-06-06 15:45:35 +00:00
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
NH_LOCK(h, hp);
|
|
|
|
|
|
|
|
/* Somebody may have added one inbetween */
|
|
|
|
return net_lock_revive_unlock(h,
|
|
|
|
(net_find_index_fragile(h, n) ?:
|
|
|
|
net_new_index_locked(hp, n)));
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|
|
|
|
|
2024-06-10 14:25:53 +00:00
|
|
|
struct netindex net_index_out_of_range;
|
|
|
|
|
2023-12-04 09:33:30 +00:00
|
|
|
struct netindex *
|
2024-06-05 15:47:32 +00:00
|
|
|
net_resolve_index(netindex_hash *h, u32 i)
|
2023-12-04 09:33:30 +00:00
|
|
|
{
|
2024-06-06 15:45:35 +00:00
|
|
|
RCU_ANCHOR(u);
|
2024-05-20 13:57:13 +00:00
|
|
|
|
2024-06-06 15:45:35 +00:00
|
|
|
struct netindex * _Atomic *block = atomic_load_explicit(&h->block, memory_order_relaxed);
|
|
|
|
u32 bs = atomic_load_explicit(&h->block_size, memory_order_relaxed);
|
|
|
|
|
|
|
|
if (i >= bs)
|
2024-06-10 14:25:53 +00:00
|
|
|
return &net_index_out_of_range;
|
2024-06-06 15:45:35 +00:00
|
|
|
|
|
|
|
struct netindex *ni = atomic_load_explicit(&block[i], memory_order_acquire);
|
|
|
|
if (ni == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2024-06-07 23:37:24 +00:00
|
|
|
return net_lock_revive_unlock(h, ni);
|
2023-12-04 09:33:30 +00:00
|
|
|
}
|