0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 09:41:54 +00:00

Import tables are stored as an attribute layer inside the main tables.

The separate import tables were too memory-greedy, there is no need for
them being stored as full-sized tables.
This commit is contained in:
Maria Matejka 2022-06-16 23:24:56 +02:00
parent 8c92f47ac7
commit becab5072d
13 changed files with 101 additions and 226 deletions

View File

@ -161,6 +161,7 @@ typedef struct ea_list {
#define EALF_SORTED 1 /* Attributes are sorted by code */
#define EALF_BISECT 2 /* Use interval bisection for searching */
#define EALF_CACHED 4 /* List is cached */
#define EALF_OVERLAY 8 /* List is an overlay in the same table */
struct ea_class {
#define EA_CLASS_INSIDE \
@ -413,7 +414,7 @@ static inline int rte_dest(const rte *r)
}
void rta_init(void);
ea_list *ea_lookup(ea_list *); /* Get a cached (and normalized) variant of this attribute list */
ea_list *ea_lookup(ea_list *, int overlay); /* Get a cached (and normalized) variant of this attribute list */
static inline int ea_is_cached(const ea_list *r) { return r->flags & EALF_CACHED; }
static inline ea_list *ea_clone(ea_list *r) { r->uc++; return r; }
void ea__free(ea_list *r);

View File

@ -311,7 +311,14 @@ channel_item_:
| IMPORT LIMIT limit_spec { this_channel->in_limit = $3; }
| EXPORT LIMIT limit_spec { this_channel->out_limit = $3; }
| PREFERENCE expr { this_channel->preference = $2; check_u16($2); }
| IMPORT KEEP FILTERED bool { this_channel->in_keep_filtered = $4; }
| IMPORT KEEP FILTERED bool {
if ($4)
this_channel->in_keep |= RIK_REJECTED;
else if ((this_channel->in_keep & RIK_PREFILTER) == RIK_PREFILTER)
cf_error("Import keep filtered is implied by the import table.");
else
this_channel->in_keep &= ~RIK_REJECTED;
}
| RPKI RELOAD bool { this_channel->rpki_reload = $3; }
;
@ -674,8 +681,8 @@ r_args:
$$->tables_defined_by = RSD_TDB_ALL;
}
| r_args IMPORT TABLE channel_arg {
if (!$4->in_table) cf_error("No import table in channel %s.%s", $4->proto->name, $4->name);
rt_show_add_table($$, $4->in_table);
if (!($4->in_keep & RIK_PREFILTER)) cf_error("No import table in channel %s.%s", $4->proto->name, $4->name);
rt_show_add_table($$, $4->table)->prefilter = $4;
$$->tables_defined_by = RSD_TDB_DIRECT;
}
| r_args EXPORT TABLE channel_arg {

View File

@ -224,7 +224,7 @@ proto_add_channel(struct proto *p, struct channel_config *cf)
c->preference = cf->preference;
c->debug = cf->debug;
c->merge_limit = cf->merge_limit;
c->in_keep_filtered = cf->in_keep_filtered;
c->in_keep = cf->in_keep;
c->rpki_reload = cf->rpki_reload;
c->channel_state = CS_DOWN;
@ -294,7 +294,7 @@ static void
channel_roa_in_changed(struct rt_subscription *s)
{
struct channel *c = s->data;
int active = c->reload_event && ev_active(c->reload_event);
int active = !!c->reload_req.hook;
CD(c, "Reload triggered by RPKI change%s", active ? " - already active" : "");
@ -379,7 +379,7 @@ channel_roa_subscribe_filter(struct channel *c, int dir)
#ifdef CONFIG_BGP
/* No automatic reload for BGP channels without in_table / out_table */
if (c->channel == &channel_bgp)
valid = dir ? !!c->in_table : !!c->out_table;
valid = dir ? ((c->in_keep & RIK_PREFILTER) == RIK_PREFILTER) : !!c->out_table;
#endif
struct filter_iterator fit;
@ -534,9 +534,6 @@ channel_import_stopped(struct rt_import_request *req)
req->hook = NULL;
if (c->in_table)
rt_prune_sync(c->in_table, 1);
mb_free(c->in_req.name);
c->in_req.name = NULL;
@ -603,43 +600,48 @@ channel_schedule_reload(struct channel *c)
{
ASSERT(c->in_req.hook);
rt_reload_channel_abort(c);
ev_schedule_work(c->reload_event);
rt_request_export(c->table, &c->reload_req);
}
static void
channel_reload_loop(void *ptr)
channel_reload_stopped(struct rt_export_request *req)
{
struct channel *c = ptr;
/* Start reload */
if (!c->reload_active)
c->reload_pending = 0;
if (!rt_reload_channel(c))
{
ev_schedule_work(c->reload_event);
return;
}
struct channel *c = SKIP_BACK(struct channel, reload_req, req);
/* Restart reload */
if (c->reload_pending)
channel_request_reload(c);
}
static void
channel_reload_log_state_change(struct rt_export_request *req, u8 state)
{
if (state == TES_READY)
rt_stop_export(req, channel_reload_stopped);
}
static void
channel_reload_dump_req(struct rt_export_request *req)
{
struct channel *c = SKIP_BACK(struct channel, reload_req, req);
debug(" Channel %s.%s import reload request %p\n", c->proto->name, c->name, req);
}
void channel_reload_export_bulk(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *rpe, rte **feed, uint count);
/* Called by protocol to activate in_table */
void
channel_setup_in_table(struct channel *c)
{
struct rtable_config *cf = mb_allocz(c->proto->pool, sizeof(struct rtable_config));
c->reload_req = (struct rt_export_request) {
.name = mb_sprintf(c->proto->pool, "%s.%s.import", c->proto->name, c->name),
.trace_routes = c->debug | c->proto->debug,
.export_bulk = channel_reload_export_bulk,
.dump_req = channel_reload_dump_req,
.log_state_change = channel_reload_log_state_change,
};
cf->name = "import";
cf->addr_type = c->net_type;
cf->internal = 1;
c->in_table = rt_setup(c->proto->pool, cf);
c->reload_event = ev_new_init(c->proto->pool, channel_reload_loop, c);
c->in_keep |= RIK_PREFILTER;
}
/* Called by protocol to activate out_table */
@ -680,10 +682,10 @@ static void
channel_do_pause(struct channel *c)
{
/* Need to abort feeding */
if (c->reload_event)
if (c->reload_req.hook)
{
ev_postpone(c->reload_event);
rt_reload_channel_abort(c);
c->reload_pending = 0;
rt_stop_export(&c->reload_req, channel_reload_stopped);
}
/* Stop export */
@ -710,15 +712,13 @@ channel_do_stop(struct channel *c)
CALL(c->channel->shutdown, c);
/* This have to be done in here, as channel pool is freed before channel_do_down() */
c->in_table = NULL;
c->reload_event = NULL;
c->out_table = NULL;
}
static void
channel_do_down(struct channel *c)
{
ASSERT(!c->reload_active);
ASSERT(!c->reload_req.hook);
c->proto->active_channels--;
@ -726,8 +726,6 @@ channel_do_down(struct channel *c)
memset(&c->import_stats, 0, sizeof(struct channel_import_stats));
memset(&c->export_stats, 0, sizeof(struct channel_export_stats));
c->in_table = NULL;
c->reload_event = NULL;
c->out_table = NULL;
/* The in_table and out_table are going to be freed by freeing their resource pools. */
@ -922,7 +920,9 @@ int
channel_reconfigure(struct channel *c, struct channel_config *cf)
{
/* FIXME: better handle these changes, also handle in_keep_filtered */
if ((c->table != cf->table->table) || (cf->ra_mode && (c->ra_mode != cf->ra_mode)))
if ((c->table != cf->table->table) ||
(cf->ra_mode && (c->ra_mode != cf->ra_mode)) ||
(cf->in_keep != c->in_keep))
return 0;
/* Note that filter_same() requires arguments in (new, old) order */
@ -949,7 +949,6 @@ channel_reconfigure(struct channel *c, struct channel_config *cf)
c->preference = cf->preference;
c->debug = cf->debug;
c->in_req.trace_routes = c->out_req.trace_routes = c->debug | c->proto->debug;
c->in_keep_filtered = cf->in_keep_filtered;
c->rpki_reload = cf->rpki_reload;
/* Execute channel-specific reconfigure hook */
@ -2099,7 +2098,7 @@ channel_show_stats(struct channel *c)
u32 in_routes = c->in_limit.count;
u32 out_routes = c->out_limit.count;
if (c->in_keep_filtered)
if (c->in_keep)
cli_msg(-1006, " Routes: %u imported, %u filtered, %u exported, %u preferred",
in_routes, (rx_routes - in_routes), out_routes, SRI(pref));
else

View File

@ -454,7 +454,7 @@ struct channel_config {
const struct filter *in_filter, *out_filter; /* Attached filters */
struct channel_limit rx_limit; /* Limit for receiving routes from protocol
(relevant when in_keep_filtered is active) */
(relevant when in_keep & RIK_REJECTED) */
struct channel_limit in_limit; /* Limit for importing routes from protocol */
struct channel_limit out_limit; /* Limit for exporting routes to protocol */
@ -463,7 +463,7 @@ struct channel_config {
u16 preference; /* Default route preference */
u32 debug; /* Debugging flags (D_*) */
u8 merge_limit; /* Maximal number of nexthops for RA_MERGED */
u8 in_keep_filtered; /* Routes rejected in import filter are kept */
u8 in_keep; /* Which states of routes to keep (RIK_*) */
u8 rpki_reload; /* RPKI changes trigger channel reload */
};
@ -480,7 +480,7 @@ struct channel {
struct bmap export_map; /* Keeps track which routes were really exported */
struct bmap export_reject_map; /* Keeps track which routes were rejected by export filter */
struct limit rx_limit; /* Receive limit (for in_keep_filtered) */
struct limit rx_limit; /* Receive limit (for in_keep & RIK_REJECTED) */
struct limit in_limit; /* Input limit */
struct limit out_limit; /* Output limit */
@ -517,7 +517,7 @@ struct channel {
u16 preference; /* Default route preference */
u32 debug; /* Debugging flags (D_*) */
u8 merge_limit; /* Maximal number of nexthops for RA_MERGED */
u8 in_keep_filtered; /* Routes rejected in import filter are kept */
u8 in_keep; /* Which states of routes to keep (RIK_*) */
u8 disabled;
u8 stale; /* Used in reconfiguration */
@ -529,11 +529,7 @@ struct channel {
btime last_state_change; /* Time of last state transition */
struct rtable *in_table; /* Internal table for received routes */
struct event *reload_event; /* Event responsible for reloading from in_table */
struct fib_iterator reload_fit; /* FIB iterator in in_table used during reloading */
struct rte_storage *reload_next_rte; /* Route iterator in in_table used during reloading */
u8 reload_active; /* Iterator reload_fit is linked */
struct rt_export_request reload_req; /* Feeder for import reload */
u8 reload_pending; /* Reloading and another reload is scheduled */
u8 refeed_pending; /* Refeeding and another refeed is scheduled */
@ -544,6 +540,8 @@ struct channel {
list roa_subscriptions; /* List of active ROA table subscriptions based on filters roa_check() */
};
#define RIK_REJECTED 1 /* Routes rejected in import filter are kept */
#define RIK_PREFILTER (2 | RIK_REJECTED) /* All routes' attribute state before import filter is kept */
/*
* Channel states

View File

@ -1310,13 +1310,13 @@ rta_rehash(void)
* converted to the normalized form.
*/
ea_list *
ea_lookup(ea_list *o)
ea_lookup(ea_list *o, int overlay)
{
ea_list *r;
uint h;
ASSERT(!ea_is_cached(o));
o = ea_normalize(o, 1);
o = ea_normalize(o, overlay);
h = ea_hash(o);
for(r=rta_hash_table[h & rta_cache_mask]; r; r=r->next_hash)

View File

@ -26,7 +26,8 @@ rt_show_table(struct cli *c, struct rt_show_data *d)
return;
if (d->last_table) cli_printf(c, -1007, "");
cli_printf(c, -1007, "Table %s:", d->tab->table->name);
cli_printf(c, -1007, "Table %s:",
d->tab->prefilter ? "import" : d->tab->table->name);
d->last_table = d->tab;
}
@ -156,7 +157,7 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
for (struct rte_storage *er = n->routes; er; er = er->next)
{
if (rte_is_filtered(&er->rte) != d->filtered)
if (!d->tab->prefilter && (rte_is_filtered(&er->rte) != d->filtered))
continue;
d->rt_counter++;
@ -167,6 +168,11 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
continue;
struct rte e = er->rte;
if (d->tab->prefilter)
if (e.sender != d->tab->prefilter->in_req.hook)
continue;
else while (e.attrs->next)
e.attrs = e.attrs->next;
/* Export channel is down, do not try to export routes to it */
if (ec && !ec->out_req.hook)
@ -239,7 +245,7 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
else
ia[0] = 0;
rt_show_rte(c, ia, &e, d, (n->routes == er));
rt_show_rte(c, ia, &e, d, !d->tab->prefilter && (n->routes == er));
first_show = 0;
}

View File

@ -606,7 +606,7 @@ rte_store(const rte *r, net *net, rtable *tab)
if (ea_is_cached(e->rte.attrs))
e->rte.attrs = rta_clone(e->rte.attrs);
else
e->rte.attrs = rta_lookup(e->rte.attrs);
e->rte.attrs = rta_lookup(e->rte.attrs, 1);
return e;
}
@ -1513,7 +1513,7 @@ channel_preimport(struct rt_import_request *req, rte *new, rte *old)
if (new_in && !old_in)
if (CHANNEL_LIMIT_PUSH(c, IN))
if (c->in_keep_filtered)
if (c->in_keep & RIK_REJECTED)
{
new->flags |= REF_FILTERED;
return new;
@ -1527,8 +1527,6 @@ channel_preimport(struct rt_import_request *req, rte *new, rte *old)
return new;
}
static void rte_update_direct(struct channel *c, const net_addr *n, rte *new, struct rte_src *src);
void
rte_update(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
{
@ -1537,15 +1535,13 @@ rte_update(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
ASSERT(c->channel_state == CS_UP);
if (c->in_table && !rte_update_in(c, n, new, src))
return;
/* The import reloader requires prefilter routes to be the first layer */
if (new && (c->in_keep & RIK_PREFILTER))
if (ea_is_cached(new->attrs) && !new->attrs->next)
new->attrs = ea_clone(new->attrs);
else
new->attrs = ea_lookup(new->attrs, 0);
return rte_update_direct(c, n, new, src);
}
static void
rte_update_direct(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
{
const struct filter *filter = c->in_filter;
struct channel_import_stats *stats = &c->import_stats;
@ -1563,7 +1559,7 @@ rte_update_direct(struct channel *c, const net_addr *n, rte *new, struct rte_src
stats->updates_filtered++;
channel_rte_trace_in(D_FILTERS, c, new, "filtered out");
if (c->in_keep_filtered)
if (c->in_keep & RIK_REJECTED)
new->flags |= REF_FILTERED;
else
new = NULL;
@ -1588,6 +1584,11 @@ rte_update_direct(struct channel *c, const net_addr *n, rte *new, struct rte_src
rte_import(&c->in_req, n, new, src);
/* Now the route attributes are kept by the in-table cached version
* and we may drop the local handle */
if (new && (c->in_keep & RIK_PREFILTER))
ea_free(new->attrs);
rte_update_unlock();
}
@ -3183,154 +3184,22 @@ done:
* Import table
*/
int
rte_update_in(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
void channel_reload_export_bulk(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *rpe UNUSED, rte **feed, uint count)
{
struct rtable *tab = c->in_table;
net *net;
struct channel *c = SKIP_BACK(struct channel, reload_req, req);
if (new)
net = net_get(tab, n);
else
{
net = net_find(tab, n);
if (!net)
goto drop_withdraw;
}
/* Find the old rte */
struct rte_storage **pos = rte_find(net, src);
if (*pos)
for (uint i=0; i<count; i++)
if (feed[i]->sender == c->in_req.hook)
{
rte *old = &(*pos)->rte;
if (new && rte_same(old, new))
{
/* Refresh the old rte, continue with update to main rtable */
if (old->flags & (REF_STALE | REF_DISCARD | REF_MODIFY))
{
old->flags &= ~(REF_STALE | REF_DISCARD | REF_MODIFY);
return 1;
}
/* Strip the later attribute layers */
rte new = *feed[i];
while (new.attrs->next)
new.attrs = new.attrs->next;
goto drop_update;
}
if (!new)
CHANNEL_LIMIT_POP(c, RX);
/* Move iterator if needed */
if (*pos == c->reload_next_rte)
c->reload_next_rte = (*pos)->next;
/* Remove the old rte */
struct rte_storage *del = *pos;
*pos = (*pos)->next;
rte_free(del);
tab->rt_count--;
/* And reload the route */
rte_update(c, net, &new, new.src);
}
else if (new)
{
if (CHANNEL_LIMIT_PUSH(c, RX))
{
/* Required by rte_trace_in() */
new->net = n;
channel_rte_trace_in(D_FILTERS, c, new, "ignored [limit]");
goto drop_update;
}
}
else
goto drop_withdraw;
if (!new)
{
if (!net->routes)
fib_delete(&tab->fib, net);
return 1;
}
/* Insert the new rte */
struct rte_storage *e = rte_store(new, net, tab);
e->rte.lastmod = current_time();
e->next = *pos;
*pos = e;
tab->rt_count++;
return 1;
drop_update:
c->import_stats.updates_received++;
c->in_req.hook->stats.updates_ignored++;
if (!net->routes)
fib_delete(&tab->fib, net);
return 0;
drop_withdraw:
c->import_stats.withdraws_received++;
c->in_req.hook->stats.withdraws_ignored++;
return 0;
}
int
rt_reload_channel(struct channel *c)
{
struct rtable *tab = c->in_table;
struct fib_iterator *fit = &c->reload_fit;
int max_feed = 64;
ASSERT(c->channel_state == CS_UP);
if (!c->reload_active)
{
FIB_ITERATE_INIT(fit, &tab->fib);
c->reload_active = 1;
}
do {
for (struct rte_storage *e = c->reload_next_rte; e; e = e->next)
{
if (max_feed-- <= 0)
{
c->reload_next_rte = e;
debug("%s channel reload burst split (max_feed=%d)", c->proto->name, max_feed);
return 0;
}
rte r = e->rte;
rte_update_direct(c, r.net, &r, r.src);
}
c->reload_next_rte = NULL;
FIB_ITERATE_START(&tab->fib, fit, net, n)
{
if (c->reload_next_rte = n->routes)
{
FIB_ITERATE_PUT_NEXT(fit, &tab->fib);
break;
}
}
FIB_ITERATE_END;
}
while (c->reload_next_rte);
c->reload_active = 0;
return 1;
}
void
rt_reload_channel_abort(struct channel *c)
{
if (c->reload_active)
{
/* Unlink the iterator */
fit_get(&c->in_table->fib, &c->reload_fit);
c->reload_next_rte = NULL;
c->reload_active = 0;
}
}
void

View File

@ -387,6 +387,7 @@ struct rt_show_data_rtable {
node n;
rtable *table;
struct channel *export_channel;
struct channel *prefilter;
};
struct rt_show_data {

View File

@ -900,9 +900,6 @@ bgp_refresh_begin(struct bgp_channel *c)
c->load_state = BFS_REFRESHING;
rt_refresh_begin(c->c.table, &c->c.in_req);
if (c->c.in_table)
rt_refresh_begin(c->c.in_table, &c->c.in_req);
}
/**
@ -924,9 +921,6 @@ bgp_refresh_end(struct bgp_channel *c)
c->load_state = BFS_NONE;
rt_refresh_end(c->c.table, &c->c.in_req);
if (c->c.in_table)
rt_prune_sync(c->c.in_table, 0);
}
@ -1393,9 +1387,9 @@ bgp_reload_routes(struct channel *C)
struct bgp_proto *p = (void *) C->proto;
struct bgp_channel *c = (void *) C;
ASSERT(p->conn && (p->route_refresh || c->c.in_table));
ASSERT(p->conn && (p->route_refresh || (C->in_keep & RIK_PREFILTER)));
if (c->c.in_table)
if (C->in_keep & RIK_PREFILTER)
channel_schedule_reload(C);
else
bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
@ -2153,7 +2147,7 @@ bgp_channel_reconfigure(struct channel *C, struct channel_config *CC, int *impor
(new->cost != old->cost))
{
/* import_changed itself does not force ROUTE_REFRESH when import_table is active */
if (c->c.in_table && (c->c.channel_state == CS_UP))
if ((c->c.in_keep & RIK_PREFILTER) && (c->c.channel_state == CS_UP))
bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH);
*import_changed = 1;

View File

@ -1392,7 +1392,7 @@ bgp_rte_update(struct bgp_parse_state *s, const net_addr *n, u32 path_id, ea_lis
/* Prepare cached route attributes */
if (s->cached_ea == NULL)
s->cached_ea = ea_lookup(a0);
s->cached_ea = ea_lookup(a0, 0);
rte e0 = {
.attrs = s->cached_ea,

View File

@ -2086,7 +2086,7 @@ again1:
ASSERT_DIE(ARRAY_SIZE(eattrs.a) >= eattrs.l.count);
ea_list *eal = ea_lookup(&eattrs.l);
ea_list *eal = ea_lookup(&eattrs.l, 0);
ea_free(nf->old_ea);
nf->old_ea = eal;

View File

@ -156,7 +156,7 @@ perf_loop(void *data)
ea_set_attr_data(&ea, &ea_gen_nexthop, 0,
&nhad.ad.data, sizeof nhad - sizeof nhad.ad);
p->data[i].a = rta_lookup(ea);
p->data[i].a = rta_lookup(ea, 0);
}
else
p->data[i].a = rta_clone(p->data[i-1].a);

View File

@ -126,7 +126,7 @@ pipe_postconfig(struct proto_config *CF)
if (cc->rx_limit.action)
cf_error("Pipe protocol does not support receive limits");
if (cc->in_keep_filtered)
if (cc->in_keep)
cf_error("Pipe protocol prohibits keeping filtered routes");
cc->debug = cf->c.debug;