From f8d44b01df5d93681e116ccbff39cc4618632825 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Mon, 15 May 2017 12:10:51 +0200 Subject: [PATCH 1/4] Nest: split route show into separate file --- nest/Makefile | 2 +- nest/cli.c | 3 +- nest/cli.h | 1 + nest/protocol.h | 8 + nest/rt-show.c | 414 +++++++++++++++++++++++++++++++++++++++++++++++ nest/rt-table.c | 417 +----------------------------------------------- 6 files changed, 429 insertions(+), 416 deletions(-) create mode 100644 nest/rt-show.c diff --git a/nest/Makefile b/nest/Makefile index d673cee5..884d3950 100644 --- a/nest/Makefile +++ b/nest/Makefile @@ -1,4 +1,4 @@ -src := a-path.c a-set.c cli.c cmds.c iface.c locks.c neighbor.c password.c proto.c rt-attr.c rt-dev.c rt-fib.c rt-table.c +src := a-path.c a-set.c cli.c cmds.c iface.c locks.c neighbor.c password.c proto.c rt-attr.c rt-dev.c rt-fib.c rt-show.c rt-table.c obj := $(src-o-files) $(all-daemon) $(cf-local) diff --git a/nest/cli.c b/nest/cli.c index 83e79616..bf8bf127 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -313,7 +313,8 @@ cli_new(void *priv) c->event->hook = cli_event; c->event->data = c; c->cont = cli_hello; - c->parser_pool = lp_new(c->pool, 4096); + c->parser_pool = lp_new(c->pool, 4080); + c->show_pool = lp_new(c->pool, 4080); c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE); ev_schedule(c->event); return c; diff --git a/nest/cli.h b/nest/cli.h index 92f3c3d7..6040be91 100644 --- a/nest/cli.h +++ b/nest/cli.h @@ -38,6 +38,7 @@ typedef struct cli { int last_reply; int restricted; /* CLI is restricted to read-only commands */ struct linpool *parser_pool; /* Pool used during parsing */ + struct linpool *show_pool; /* Pool used during route show */ byte *ring_buf; /* Ring buffer for asynchronous messages */ byte *ring_end, *ring_read, *ring_write; /* Pointers to the ring buffer */ uint ring_overflow; /* Counter of ring overflows */ diff --git a/nest/protocol.h b/nest/protocol.h index f0958678..eed0a291 100644 --- a/nest/protocol.h +++ b/nest/protocol.h @@ -270,6 +270,14 @@ proto_get_router_id(struct proto_config *pc) return pc->router_id ? pc->router_id : pc->global->router_id; } +static inline struct ea_list * +rte_make_tmp_attrs(struct rte *rt, struct linpool *pool) +{ + struct ea_list *(*mta)(struct rte *rt, struct linpool *pool); + mta = rt->attrs->src->proto->make_tmp_attrs; + return mta ? mta(rt, pool) : NULL; +} + /* Moved from route.h to avoid dependency conflicts */ static inline void rte_update(struct proto *p, const net_addr *n, rte *new) { rte_update2(p->main_channel, n, new, p->main_source); } diff --git a/nest/rt-show.c b/nest/rt-show.c new file mode 100644 index 00000000..3f5ef04d --- /dev/null +++ b/nest/rt-show.c @@ -0,0 +1,414 @@ +/* + * BIRD -- Route Display Routines + * + * (c) 1998--2000 Martin Mares + * (c) 2017 Jan Moskyto Matejka + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#undef LOCAL_DEBUG + +#include "nest/bird.h" +#include "nest/route.h" +#include "nest/protocol.h" +#include "nest/cli.h" +#include "nest/iface.h" +#include "filter/filter.h" + +static void +rt_show_table(struct cli *c, struct rt_show_data *d) +{ + /* No table blocks in 'show route count' */ + if (d->stats == 2) + return; + + if (d->last_table) cli_printf(c, -1007, ""); + cli_printf(c, -1007, "Table %s:", d->tab->table->name); + d->last_table = d->tab; +} + +static void +rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa) +{ + byte from[IPA_MAX_TEXT_LENGTH+8]; + byte tm[TM_DATETIME_BUFFER_SIZE], info[256]; + rta *a = e->attrs; + int primary = (e->net->routes == e); + int sync_error = (e->net->n.flags & KRF_SYNC_ERROR); + void (*get_route_info)(struct rte *, byte *buf, struct ea_list *attrs); + struct nexthop *nh; + + tm_format_datetime(tm, &config->tf_route, e->lastmod); + if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->nh.gw)) + bsprintf(from, " from %I", a->from); + else + from[0] = 0; + + get_route_info = a->src->proto->proto->get_route_info; + if (get_route_info || d->verbose) + { + /* Need to normalize the extended attributes */ + ea_list *t = tmpa; + t = ea_append(t, a->eattrs); + tmpa = alloca(ea_scan(t)); + ea_merge(t, tmpa); + ea_sort(tmpa); + } + if (get_route_info) + get_route_info(e, info, tmpa); + else + bsprintf(info, " (%d)", e->pref); + + if (d->last_table != d->tab) + rt_show_table(c, d); + + cli_printf(c, -1007, "%-18s %s [%s %s%s]%s%s", ia, rta_dest_name(a->dest), + a->src->proto->name, tm, from, primary ? (sync_error ? " !" : " *") : "", info); + + if (a->dest == RTD_UNICAST) + for (nh = &(a->nh); nh; nh = nh->next) + { + char mpls[MPLS_MAX_LABEL_STACK*12 + 5], *lsp = mpls; + + if (nh->labels) + { + lsp += bsprintf(lsp, " mpls %d", nh->label[0]); + for (int i=1;ilabels; i++) + lsp += bsprintf(lsp, "/%d", nh->label[i]); + } + *lsp = '\0'; + + if (a->nh.next) + cli_printf(c, -1007, "\tvia %I%s on %s weight %d", nh->gw, mpls, nh->iface->name, nh->weight + 1); + else + cli_printf(c, -1007, "\tvia %I%s on %s", nh->gw, mpls, nh->iface->name); + } + + if (d->verbose) + rta_show(c, a, tmpa); +} + +static void +rt_show_net(struct cli *c, net *n, struct rt_show_data *d) +{ + rte *e, *ee; + byte ia[NET_MAX_TEXT_LENGTH+1]; + struct ea_list *tmpa; + struct channel *ec = d->tab->export_channel; + int first = 1; + int pass = 0; + + bsnprintf(ia, sizeof(ia), "%N", n->n.addr); + + for (e = n->routes; e; e = e->next) + { + if (rte_is_filtered(e) != d->filtered) + continue; + + d->rt_counter++; + d->net_counter += first; + first = 0; + + if (pass) + continue; + + ee = e; + tmpa = rte_make_tmp_attrs(e, c->show_pool); + + /* Export channel is down, do not try to export routes to it */ + if (ec && (ec->export_state == ES_DOWN)) + goto skip; + + /* Special case for merged export */ + if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED)) + { + rte *rt_free; + e = rt_export_merged(ec, n, &rt_free, &tmpa, c->show_pool, 1); + pass = 1; + + if (!e) + { e = ee; goto skip; } + } + else if (d->export_mode) + { + struct proto *ep = ec->proto; + int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, c->show_pool) : 0; + + if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED) + pass = 1; + + if (ic < 0) + goto skip; + + if (d->export_mode > RSEM_PREEXPORT) + { + /* + * FIXME - This shows what should be exported according to current + * filters, but not what was really exported. 'configure soft' + * command may change the export filter and do not update routes. + */ + int do_export = (ic > 0) || + (f_run(ec->out_filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) <= F_ACCEPT); + + if (do_export != (d->export_mode == RSEM_EXPORT)) + goto skip; + + if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED)) + pass = 1; + } + } + + if (d->show_protocol && (d->show_protocol != e->attrs->src->proto)) + goto skip; + + if (f_run(d->filter, &e, &tmpa, c->show_pool, FF_FORCE_TMPATTR) > F_ACCEPT) + goto skip; + + if (d->stats < 2) + rt_show_rte(c, ia, e, d, tmpa); + + d->show_counter++; + ia[0] = 0; + + skip: + if (e != ee) + { + rte_free(e); + e = ee; + } + lp_flush(c->show_pool); + + if (d->primary_only) + break; + } +} + +static void +rt_show_cleanup(struct cli *c) +{ + struct rt_show_data *d = c->rover; + struct rt_show_data_rtable *tab; + + /* Unlink the iterator */ + if (d->table_open) + fit_get(&d->tab->table->fib, &d->fit); + + /* Unlock referenced tables */ + WALK_LIST(tab, d->tables) + rt_unlock_table(tab->table); +} + +static void +rt_show_cont(struct cli *c) +{ + struct rt_show_data *d = c->rover; +#ifdef DEBUGGING + unsigned max = 4; +#else + unsigned max = 64; +#endif + struct fib *fib = &d->tab->table->fib; + struct fib_iterator *it = &d->fit; + + if (d->running_on_config && (d->running_on_config != config)) + { + cli_printf(c, 8004, "Stopped due to reconfiguration"); + goto done; + } + + if (!d->table_open) + { + FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib); + d->table_open = 1; + d->table_counter++; + + d->show_counter_last = d->show_counter; + d->rt_counter_last = d->rt_counter; + d->net_counter_last = d->net_counter; + + if (d->tables_defined_by & RSD_TDB_SET) + rt_show_table(c, d); + } + + FIB_ITERATE_START(fib, it, net, n) + { + if (!max--) + { + FIB_ITERATE_PUT(it); + return; + } + rt_show_net(c, n, d); + } + FIB_ITERATE_END; + + if (d->stats) + { + if (d->last_table != d->tab) + rt_show_table(c, d); + + cli_printf(c, -1007, "%d of %d routes for %d networks in table %s", + d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last, + d->net_counter - d->net_counter_last, d->tab->table->name); + } + + d->table_open = 0; + d->tab = NODE_NEXT(d->tab); + + if (NODE_VALID(d->tab)) + return; + + if (d->stats && (d->table_counter > 1)) + { + if (d->last_table) cli_printf(c, -1007, ""); + cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables", + d->show_counter, d->rt_counter, d->net_counter, d->table_counter); + } + else + cli_printf(c, 0, ""); + +done: + rt_show_cleanup(c); + c->cont = c->cleanup = NULL; +} + +struct rt_show_data_rtable * +rt_show_add_table(struct rt_show_data *d, rtable *t) +{ + struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable)); + tab->table = t; + add_tail(&(d->tables), &(tab->n)); + return tab; +} + +static inline void +rt_show_get_default_tables(struct rt_show_data *d) +{ + struct channel *c; + struct rt_show_data_rtable *tab; + + if (d->export_channel) + { + c = d->export_channel; + tab = rt_show_add_table(d, c->table); + tab->export_channel = c; + return; + } + + if (d->export_protocol) + { + WALK_LIST(c, d->export_protocol->channels) + { + if (c->export_state == ES_DOWN) + continue; + + tab = rt_show_add_table(d, c->table); + tab->export_channel = c; + } + return; + } + + if (d->show_protocol) + { + WALK_LIST(c, d->show_protocol->channels) + rt_show_add_table(d, c->table); + return; + } + + for (int i=1; idef_tables[i]) + rt_show_add_table(d, config->def_tables[i]->table); +} + +static inline void +rt_show_prepare_tables(struct rt_show_data *d) +{ + struct rt_show_data_rtable *tab, *tabx; + + /* Add implicit tables if no table is specified */ + if (EMPTY_LIST(d->tables)) + rt_show_get_default_tables(d); + + WALK_LIST_DELSAFE(tab, tabx, d->tables) + { + /* Ensure there is defined export_channel for each table */ + if (d->export_mode) + { + if (!tab->export_channel && d->export_channel && + (tab->table == d->export_channel->table)) + tab->export_channel = d->export_channel; + + if (!tab->export_channel && d->export_protocol) + tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table); + + if (!tab->export_channel) + { + if (d->tables_defined_by & RSD_TDB_NMN) + cf_error("No export channel for table %s", tab->table->name); + + rem_node(&(tab->n)); + continue; + } + } + + /* Ensure specified network is compatible with each table */ + if (d->addr && (tab->table->addr_type != d->addr->type)) + { + if (d->tables_defined_by & RSD_TDB_NMN) + cf_error("Incompatible type of prefix/ip for table %s", tab->table->name); + + rem_node(&(tab->n)); + continue; + } + } + + /* Ensure there is at least one table */ + if (EMPTY_LIST(d->tables)) + cf_error("No valid tables"); +} + +void +rt_show(struct rt_show_data *d) +{ + struct rt_show_data_rtable *tab; + net *n; + + /* Filtered routes are neither exported nor have sensible ordering */ + if (d->filtered && (d->export_mode || d->primary_only)) + cf_error("Incompatible show route options"); + + rt_show_prepare_tables(d); + + if (!d->addr) + { + WALK_LIST(tab, d->tables) + rt_lock_table(tab->table); + + /* There is at least one table */ + d->tab = HEAD(d->tables); + this_cli->cont = rt_show_cont; + this_cli->cleanup = rt_show_cleanup; + this_cli->rover = d; + } + else + { + WALK_LIST(tab, d->tables) + { + d->tab = tab; + + if (d->show_for) + n = net_route(tab->table, d->addr); + else + n = net_find(tab->table, d->addr); + + if (n) + rt_show_net(this_cli, n, d); + } + + if (d->rt_counter) + cli_msg(0, ""); + else + cli_msg(8001, "Network not found"); + } +} + diff --git a/nest/rt-table.c b/nest/rt-table.c index 67ccc885..bed9036e 100644 --- a/nest/rt-table.c +++ b/nest/rt-table.c @@ -33,7 +33,6 @@ #include "nest/bird.h" #include "nest/route.h" #include "nest/protocol.h" -#include "nest/cli.h" #include "nest/iface.h" #include "lib/resource.h" #include "lib/event.h" @@ -57,15 +56,6 @@ static void rt_next_hop_update(rtable *tab); static inline void rt_prune_table(rtable *tab); -static inline struct ea_list * -make_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - struct ea_list *(*mta)(struct rte *rt, struct linpool *pool); - mta = rt->attrs->src->proto->make_tmp_attrs; - return mta ? mta(rt, pool) : NULL; -} - - /* Like fib_route(), but skips empty net entries */ static inline void * net_route_ip4(rtable *t, net_addr_ip4 *n) @@ -376,7 +366,7 @@ export_filter_(struct channel *c, rte *rt0, rte **rt_free, ea_list **tmpa, linpo if (!tmpa) tmpa = &tmpb; - *tmpa = make_tmp_attrs(rt, pool); + *tmpa = rte_make_tmp_attrs(rt, pool); v = p->import_control ? p->import_control(p, &rt, tmpa, pool) : 0; if (v < 0) @@ -1344,7 +1334,7 @@ rte_update2(struct channel *c, const net_addr *n, rte *new, struct rte_src *src) } else { - tmpa = make_tmp_attrs(new, rte_update_pool); + tmpa = rte_make_tmp_attrs(new, rte_update_pool); if (filter && (filter != FILTER_REJECT)) { ea_list *old_tmpa = tmpa; @@ -1424,7 +1414,7 @@ rt_examine(rtable *t, net_addr *a, struct proto *p, struct filter *filter) rte_update_lock(); /* Rest is stripped down export_filter() */ - ea_list *tmpa = make_tmp_attrs(rt, rte_update_pool); + ea_list *tmpa = rte_make_tmp_attrs(rt, rte_update_pool); int v = p->import_control ? p->import_control(p, &rt, &tmpa, rte_update_pool) : 0; if (v == RIC_PROCESS) v = (f_run(filter, &rt, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT); @@ -2499,407 +2489,6 @@ rt_get_hostentry(rtable *tab, ip_addr a, ip_addr ll, rtable *dep) } -/* - * CLI commands - */ - -static void -rt_show_table(struct cli *c, struct rt_show_data *d) -{ - /* No table blocks in 'show route count' */ - if (d->stats == 2) - return; - - if (d->last_table) cli_printf(c, -1007, ""); - cli_printf(c, -1007, "Table %s:", d->tab->table->name); - d->last_table = d->tab; -} - -static void -rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, ea_list *tmpa) -{ - byte from[IPA_MAX_TEXT_LENGTH+8]; - byte tm[TM_DATETIME_BUFFER_SIZE], info[256]; - rta *a = e->attrs; - int primary = (e->net->routes == e); - int sync_error = (e->net->n.flags & KRF_SYNC_ERROR); - void (*get_route_info)(struct rte *, byte *buf, struct ea_list *attrs); - struct nexthop *nh; - - tm_format_datetime(tm, &config->tf_route, e->lastmod); - if (ipa_nonzero(a->from) && !ipa_equal(a->from, a->nh.gw)) - bsprintf(from, " from %I", a->from); - else - from[0] = 0; - - get_route_info = a->src->proto->proto->get_route_info; - if (get_route_info || d->verbose) - { - /* Need to normalize the extended attributes */ - ea_list *t = tmpa; - t = ea_append(t, a->eattrs); - tmpa = alloca(ea_scan(t)); - ea_merge(t, tmpa); - ea_sort(tmpa); - } - if (get_route_info) - get_route_info(e, info, tmpa); - else - bsprintf(info, " (%d)", e->pref); - - if (d->last_table != d->tab) - rt_show_table(c, d); - - cli_printf(c, -1007, "%-18s %s [%s %s%s]%s%s", ia, rta_dest_name(a->dest), - a->src->proto->name, tm, from, primary ? (sync_error ? " !" : " *") : "", info); - - if (a->dest == RTD_UNICAST) - for (nh = &(a->nh); nh; nh = nh->next) - { - char mpls[MPLS_MAX_LABEL_STACK*12 + 5], *lsp = mpls; - - if (nh->labels) - { - lsp += bsprintf(lsp, " mpls %d", nh->label[0]); - for (int i=1;ilabels; i++) - lsp += bsprintf(lsp, "/%d", nh->label[i]); - } - *lsp = '\0'; - - if (a->nh.next) - cli_printf(c, -1007, "\tvia %I%s on %s weight %d", nh->gw, mpls, nh->iface->name, nh->weight + 1); - else - cli_printf(c, -1007, "\tvia %I%s on %s", nh->gw, mpls, nh->iface->name); - } - - if (d->verbose) - rta_show(c, a, tmpa); -} - -static void -rt_show_net(struct cli *c, net *n, struct rt_show_data *d) -{ - rte *e, *ee; - byte ia[NET_MAX_TEXT_LENGTH+1]; - struct ea_list *tmpa; - struct channel *ec = d->tab->export_channel; - int first = 1; - int pass = 0; - - bsnprintf(ia, sizeof(ia), "%N", n->n.addr); - - for (e = n->routes; e; e = e->next) - { - if (rte_is_filtered(e) != d->filtered) - continue; - - d->rt_counter++; - d->net_counter += first; - first = 0; - - if (pass) - continue; - - ee = e; - rte_update_lock(); /* We use the update buffer for filtering */ - tmpa = make_tmp_attrs(e, rte_update_pool); - - /* Export channel is down, do not try to export routes to it */ - if (ec && (ec->export_state == ES_DOWN)) - goto skip; - - /* Special case for merged export */ - if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_MERGED)) - { - rte *rt_free; - e = rt_export_merged(ec, n, &rt_free, &tmpa, rte_update_pool, 1); - pass = 1; - - if (!e) - { e = ee; goto skip; } - } - else if (d->export_mode) - { - struct proto *ep = ec->proto; - int ic = ep->import_control ? ep->import_control(ep, &e, &tmpa, rte_update_pool) : 0; - - if (ec->ra_mode == RA_OPTIMAL || ec->ra_mode == RA_MERGED) - pass = 1; - - if (ic < 0) - goto skip; - - if (d->export_mode > RSEM_PREEXPORT) - { - /* - * FIXME - This shows what should be exported according to current - * filters, but not what was really exported. 'configure soft' - * command may change the export filter and do not update routes. - */ - int do_export = (ic > 0) || - (f_run(ec->out_filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) <= F_ACCEPT); - - if (do_export != (d->export_mode == RSEM_EXPORT)) - goto skip; - - if ((d->export_mode == RSEM_EXPORT) && (ec->ra_mode == RA_ACCEPTED)) - pass = 1; - } - } - - if (d->show_protocol && (d->show_protocol != e->attrs->src->proto)) - goto skip; - - if (f_run(d->filter, &e, &tmpa, rte_update_pool, FF_FORCE_TMPATTR) > F_ACCEPT) - goto skip; - - if (d->stats < 2) - rt_show_rte(c, ia, e, d, tmpa); - - d->show_counter++; - ia[0] = 0; - - skip: - if (e != ee) - { - rte_free(e); - e = ee; - } - rte_update_unlock(); - - if (d->primary_only) - break; - } -} - -static void -rt_show_cleanup(struct cli *c) -{ - struct rt_show_data *d = c->rover; - struct rt_show_data_rtable *tab; - - /* Unlink the iterator */ - if (d->table_open) - fit_get(&d->tab->table->fib, &d->fit); - - /* Unlock referenced tables */ - WALK_LIST(tab, d->tables) - rt_unlock_table(tab->table); -} - -static void -rt_show_cont(struct cli *c) -{ - struct rt_show_data *d = c->rover; -#ifdef DEBUGGING - unsigned max = 4; -#else - unsigned max = 64; -#endif - struct fib *fib = &d->tab->table->fib; - struct fib_iterator *it = &d->fit; - - if (d->running_on_config && (d->running_on_config != config)) - { - cli_printf(c, 8004, "Stopped due to reconfiguration"); - goto done; - } - - if (!d->table_open) - { - FIB_ITERATE_INIT(&d->fit, &d->tab->table->fib); - d->table_open = 1; - d->table_counter++; - - d->show_counter_last = d->show_counter; - d->rt_counter_last = d->rt_counter; - d->net_counter_last = d->net_counter; - - if (d->tables_defined_by & RSD_TDB_SET) - rt_show_table(c, d); - } - - FIB_ITERATE_START(fib, it, net, n) - { - if (!max--) - { - FIB_ITERATE_PUT(it); - return; - } - rt_show_net(c, n, d); - } - FIB_ITERATE_END; - - if (d->stats) - { - if (d->last_table != d->tab) - rt_show_table(c, d); - - cli_printf(c, -1007, "%d of %d routes for %d networks in table %s", - d->show_counter - d->show_counter_last, d->rt_counter - d->rt_counter_last, - d->net_counter - d->net_counter_last, d->tab->table->name); - } - - d->table_open = 0; - d->tab = NODE_NEXT(d->tab); - - if (NODE_VALID(d->tab)) - return; - - if (d->stats && (d->table_counter > 1)) - { - if (d->last_table) cli_printf(c, -1007, ""); - cli_printf(c, 14, "Total: %d of %d routes for %d networks in %d tables", - d->show_counter, d->rt_counter, d->net_counter, d->table_counter); - } - else - cli_printf(c, 0, ""); - -done: - rt_show_cleanup(c); - c->cont = c->cleanup = NULL; -} - -struct rt_show_data_rtable * -rt_show_add_table(struct rt_show_data *d, rtable *t) -{ - struct rt_show_data_rtable *tab = cfg_allocz(sizeof(struct rt_show_data_rtable)); - tab->table = t; - add_tail(&(d->tables), &(tab->n)); - return tab; -} - -static inline void -rt_show_get_default_tables(struct rt_show_data *d) -{ - struct channel *c; - struct rt_show_data_rtable *tab; - - if (d->export_channel) - { - c = d->export_channel; - tab = rt_show_add_table(d, c->table); - tab->export_channel = c; - return; - } - - if (d->export_protocol) - { - WALK_LIST(c, d->export_protocol->channels) - { - if (c->export_state == ES_DOWN) - continue; - - tab = rt_show_add_table(d, c->table); - tab->export_channel = c; - } - return; - } - - if (d->show_protocol) - { - WALK_LIST(c, d->show_protocol->channels) - rt_show_add_table(d, c->table); - return; - } - - for (int i=1; idef_tables[i]) - rt_show_add_table(d, config->def_tables[i]->table); -} - -static inline void -rt_show_prepare_tables(struct rt_show_data *d) -{ - struct rt_show_data_rtable *tab, *tabx; - - /* Add implicit tables if no table is specified */ - if (EMPTY_LIST(d->tables)) - rt_show_get_default_tables(d); - - WALK_LIST_DELSAFE(tab, tabx, d->tables) - { - /* Ensure there is defined export_channel for each table */ - if (d->export_mode) - { - if (!tab->export_channel && d->export_channel && - (tab->table == d->export_channel->table)) - tab->export_channel = d->export_channel; - - if (!tab->export_channel && d->export_protocol) - tab->export_channel = proto_find_channel_by_table(d->export_protocol, tab->table); - - if (!tab->export_channel) - { - if (d->tables_defined_by & RSD_TDB_NMN) - cf_error("No export channel for table %s", tab->table->name); - - rem_node(&(tab->n)); - continue; - } - } - - /* Ensure specified network is compatible with each table */ - if (d->addr && (tab->table->addr_type != d->addr->type)) - { - if (d->tables_defined_by & RSD_TDB_NMN) - cf_error("Incompatible type of prefix/ip for table %s", tab->table->name); - - rem_node(&(tab->n)); - continue; - } - } - - /* Ensure there is at least one table */ - if (EMPTY_LIST(d->tables)) - cf_error("No valid tables"); -} - -void -rt_show(struct rt_show_data *d) -{ - struct rt_show_data_rtable *tab; - net *n; - - /* Filtered routes are neither exported nor have sensible ordering */ - if (d->filtered && (d->export_mode || d->primary_only)) - cf_error("Incompatible show route options"); - - rt_show_prepare_tables(d); - - if (!d->addr) - { - WALK_LIST(tab, d->tables) - rt_lock_table(tab->table); - - /* There is at least one table */ - d->tab = HEAD(d->tables); - this_cli->cont = rt_show_cont; - this_cli->cleanup = rt_show_cleanup; - this_cli->rover = d; - } - else - { - WALK_LIST(tab, d->tables) - { - d->tab = tab; - - if (d->show_for) - n = net_route(tab->table, d->addr); - else - n = net_find(tab->table, d->addr); - - if (n) - rt_show_net(this_cli, n, d); - } - - if (d->rt_counter) - cli_msg(0, ""); - else - cli_msg(8001, "Network not found"); - } -} - /* * Documentation for functions declared inline in route.h */ From b880e3ffaea12c3231975157bc51b5f90a2f2433 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Tue, 16 May 2017 10:35:10 +0200 Subject: [PATCH 2/4] Bird readline client saves its history. --- client/birdc.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/client/birdc.c b/client/birdc.c index 8aa01c17..18e7c47b 100644 --- a/client/birdc.c +++ b/client/birdc.c @@ -29,6 +29,9 @@ static int prompt_active; extern int _rl_vis_botlin; extern void _rl_move_vert(int); +#define HISTORY "/.birdc_history" +static char *history_file; + static void add_history_dedup(char *cmd) { @@ -137,9 +140,24 @@ input_help(int arg, int key UNUSED) return 0; } +void +history_init(void) +{ + const char *homedir = getenv("HOME"); + if (!homedir) + homedir = "."; + history_file = malloc(strlen(homedir) + sizeof(HISTORY)); + if (!history_file) + die("couldn't alloc enough memory for history file name"); + + sprintf(history_file, "%s%s", homedir, HISTORY); + read_history(history_file); +} + void input_init(void) { + history_init(); rl_readline_name = "birdc"; rl_add_defun("bird-complete", input_complete, '\t'); rl_add_defun("bird-help", input_help, '?'); @@ -217,5 +235,6 @@ cleanup(void) return; input_hide(); + write_history(history_file); rl_callback_handler_remove(); } From 05d47bd53e71480f1b276bd895f1c25088201e48 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Tue, 16 May 2017 14:31:16 +0200 Subject: [PATCH 3/4] Linpool: default allocation size --- conf/conf.c | 2 +- filter/filter_test.c | 2 +- filter/tree_test.c | 2 +- lib/flowspec_test.c | 4 ++-- lib/mempool.c | 2 ++ lib/resource.h | 5 +++++ nest/a-path_test.c | 8 ++++---- nest/a-set_test.c | 4 ++-- nest/cli.c | 4 ++-- nest/rt-table.c | 4 ++-- proto/bgp/bgp.c | 4 ++-- proto/static/static.c | 2 +- sysdep/linux/netlink.c | 2 +- sysdep/unix/krt.c | 2 +- 14 files changed, 27 insertions(+), 20 deletions(-) diff --git a/conf/conf.c b/conf/conf.c index a2837886..9978ef23 100644 --- a/conf/conf.c +++ b/conf/conf.c @@ -89,7 +89,7 @@ struct config * config_alloc(const char *name) { pool *p = rp_new(&root_pool, "Config"); - linpool *l = lp_new(p, 4080); + linpool *l = lp_new_default(p); struct config *c = lp_allocz(l, sizeof(struct config)); /* Duplication of name string in local linear pool */ diff --git a/filter/filter_test.c b/filter/filter_test.c index 82efae0b..be7fd521 100644 --- a/filter/filter_test.c +++ b/filter/filter_test.c @@ -43,7 +43,7 @@ run_function(const void *parsed_fn_def) /* XXX: const -> non-const */ struct f_inst *f = (struct f_inst *) parsed_fn_def; - linpool *tmp = lp_new(&root_pool, 4096); + linpool *tmp = lp_new_default(&root_pool); struct f_val res = f_eval(f, tmp); rfree(tmp); diff --git a/filter/tree_test.c b/filter/tree_test.c index e224a559..5b22a9fe 100644 --- a/filter/tree_test.c +++ b/filter/tree_test.c @@ -20,7 +20,7 @@ start_conf_env(void) bt_bird_init(); pool *p = rp_new(&root_pool, "helper_pool"); - linpool *l = lp_new(p, 4080); + linpool *l = lp_new_default(p); cfg_mem = l; } diff --git a/lib/flowspec_test.c b/lib/flowspec_test.c index 36336104..69bc279d 100644 --- a/lib/flowspec_test.c +++ b/lib/flowspec_test.c @@ -401,7 +401,7 @@ t_builder4(void) resource_init(); struct flow_builder *fb = flow_builder_init(&root_pool); - linpool *lp = lp_new(&root_pool, 4096); + linpool *lp = lp_new_default(&root_pool); /* Expectation */ @@ -482,7 +482,7 @@ t_builder6(void) net_addr_ip6 ip; resource_init(); - linpool *lp = lp_new(&root_pool, 4096); + linpool *lp = lp_new_default(&root_pool); struct flow_builder *fb = flow_builder_init(&root_pool); fb->ipv6 = 1; diff --git a/lib/mempool.c b/lib/mempool.c index a8281041..3cf9c2d3 100644 --- a/lib/mempool.c +++ b/lib/mempool.c @@ -32,6 +32,8 @@ struct lp_chunk { byte data[0]; }; +const int lp_chunk_size = sizeof(struct lp_chunk); + struct linpool { resource r; byte *ptr, *end; diff --git a/lib/resource.h b/lib/resource.h index 1a0568b4..761c6adc 100644 --- a/lib/resource.h +++ b/lib/resource.h @@ -65,6 +65,11 @@ void *lp_allocu(linpool *, unsigned size); /* Unaligned */ void *lp_allocz(linpool *, unsigned size); /* With clear */ void lp_flush(linpool *); /* Free everything, but leave linpool */ +extern const int lp_chunk_size; +#define LP_GAS 1024 +#define LP_GOOD_SIZE(x) (((x + LP_GAS - 1) & (~(LP_GAS - 1))) - lp_chunk_size) +#define lp_new_default(p) lp_new(p, LP_GOOD_SIZE(LP_GAS*4)) + /* Slabs */ typedef struct slab slab; diff --git a/nest/a-path_test.c b/nest/a-path_test.c index fbf0f892..5e122396 100644 --- a/nest/a-path_test.c +++ b/nest/a-path_test.c @@ -32,7 +32,7 @@ t_as_path_match(void) struct adata *as_path = &empty_as_path; u32 first_prepended, last_prepended; first_prepended = last_prepended = 0; - struct linpool *lp = lp_new(&root_pool, 0); + struct linpool *lp = lp_new_default(&root_pool); struct f_path_mask mask[AS_PATH_LENGTH] = {}; int i; @@ -76,7 +76,7 @@ t_path_format(void) struct adata empty_as_path = {}; struct adata *as_path = &empty_as_path; - struct linpool *lp = lp_new(&root_pool, 0); + struct linpool *lp = lp_new_default(&root_pool); uint i; for (i = 4294967285; i <= 4294967294; i++) @@ -122,7 +122,7 @@ t_path_include(void) struct adata empty_as_path = {}; struct adata *as_path = &empty_as_path; - struct linpool *lp = lp_new(&root_pool, 0); + struct linpool *lp = lp_new_default(&root_pool); u32 as_nums[AS_PATH_LENGTH] = {}; int i; @@ -167,7 +167,7 @@ t_as_path_converting(void) struct adata empty_as_path = {}; struct adata *as_path = &empty_as_path; - struct linpool *lp = lp_new(&root_pool, 0); + struct linpool *lp = lp_new_default(&root_pool); #define AS_PATH_LENGTH_FOR_CONVERTING_TEST 10 int i; diff --git a/nest/a-set_test.c b/nest/a-set_test.c index f4588d65..a5081f9f 100644 --- a/nest/a-set_test.c +++ b/nest/a-set_test.c @@ -38,7 +38,7 @@ generate_set_sequence(enum set_type type) { struct adata empty_as_path = {}; set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path; - lp = lp_new(&root_pool, 0); + lp = lp_new_default(&root_pool); int i; for (i = 0; i < SET_SIZE; i++) @@ -205,7 +205,7 @@ t_set_ec_format(void) struct adata empty_as_path = {}; set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path; - lp = lp_new(&root_pool, 0); + lp = lp_new_default(&root_pool); u64 i = 0; set_sequence = ec_set_add(lp, set_sequence, i); diff --git a/nest/cli.c b/nest/cli.c index bf8bf127..aceb5770 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -313,8 +313,8 @@ cli_new(void *priv) c->event->hook = cli_event; c->event->data = c; c->cont = cli_hello; - c->parser_pool = lp_new(c->pool, 4080); - c->show_pool = lp_new(c->pool, 4080); + c->parser_pool = lp_new_default(c->pool); + c->show_pool = lp_new_default(c->pool); c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE); ev_schedule(c->event); return c; diff --git a/nest/rt-table.c b/nest/rt-table.c index bed9036e..85a951b8 100644 --- a/nest/rt-table.c +++ b/nest/rt-table.c @@ -1624,7 +1624,7 @@ rt_init(void) { rta_init(); rt_table_pool = rp_new(&root_pool, "Routing tables"); - rte_update_pool = lp_new(rt_table_pool, 4080); + rte_update_pool = lp_new_default(rt_table_pool); rte_slab = sl_new(rt_table_pool, sizeof(rte)); init_list(&routing_tables); } @@ -2304,7 +2304,7 @@ rt_init_hostcache(rtable *tab) hc_alloc_table(hc, HC_DEF_ORDER); hc->slab = sl_new(rt_table_pool, sizeof(struct hostentry)); - hc->lp = lp_new(rt_table_pool, 1008); + hc->lp = lp_new(rt_table_pool, LP_GOOD_SIZE(1024)); hc->trie = f_new_trie(hc->lp, sizeof(struct f_trie_node)); tab->hostcache = hc; diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index b9a1d157..cccced57 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -185,8 +185,8 @@ bgp_open(struct bgp_proto *p) if (!bgp_linpool) { - bgp_linpool = lp_new(proto_pool, 4080); - bgp_linpool2 = lp_new(proto_pool, 4080); + bgp_linpool = lp_new_default(proto_pool); + bgp_linpool2 = lp_new_default(proto_pool); } return 0; diff --git a/proto/static/static.c b/proto/static/static.c index f74ecee0..bb1501a5 100644 --- a/proto/static/static.c +++ b/proto/static/static.c @@ -410,7 +410,7 @@ static_start(struct proto *P) struct static_route *r; if (!static_lp) - static_lp = lp_new(&root_pool, 1008); + static_lp = lp_new(&root_pool, LP_GOOD_SIZE(1024)); if (p->igp_table_ip4) rt_lock_table(p->igp_table_ip4); diff --git a/sysdep/linux/netlink.c b/sysdep/linux/netlink.c index 083a8d90..8f44b007 100644 --- a/sysdep/linux/netlink.c +++ b/sysdep/linux/netlink.c @@ -1906,7 +1906,7 @@ nl_open_async(void) void krt_sys_io_init(void) { - nl_linpool = lp_new(krt_pool, 4080); + nl_linpool = lp_new_default(krt_pool); HASH_INIT(nl_table_map, krt_pool, 6); } diff --git a/sysdep/unix/krt.c b/sysdep/unix/krt.c index a1b10cea..f0241777 100644 --- a/sysdep/unix/krt.c +++ b/sysdep/unix/krt.c @@ -75,7 +75,7 @@ void krt_io_init(void) { krt_pool = rp_new(&root_pool, "Kernel Syncer"); - krt_filter_lp = lp_new(krt_pool, 4080); + krt_filter_lp = lp_new_default(krt_pool); init_list(&krt_proto_list); krt_sys_io_init(); } From 31874783c44dd59c355921908016f9b42d22ef02 Mon Sep 17 00:00:00 2001 From: Jan Moskyto Matejka Date: Tue, 16 May 2017 15:47:41 +0200 Subject: [PATCH 4/4] Client: manipulate history only if interactive --- client/birdc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/client/birdc.c b/client/birdc.c index 18e7c47b..f1aea2fe 100644 --- a/client/birdc.c +++ b/client/birdc.c @@ -157,7 +157,8 @@ history_init(void) void input_init(void) { - history_init(); + if (interactive) + history_init(); rl_readline_name = "birdc"; rl_add_defun("bird-complete", input_complete, '\t'); rl_add_defun("bird-help", input_help, '?'); @@ -235,6 +236,7 @@ cleanup(void) return; input_hide(); - write_history(history_file); + if (interactive) + write_history(history_file); rl_callback_handler_remove(); }