]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
The show-route CLI command now uses the route export API
authorMaria Matejka <mq@ucw.cz>
Fri, 24 Jun 2022 13:27:26 +0000 (15:27 +0200)
committerMaria Matejka <mq@ucw.cz>
Mon, 27 Jun 2022 10:32:47 +0000 (12:32 +0200)
In the multithreaded environment, it is not supposed that anybody
traverses the routing table as the CLI show-route was doing. Now the
routing table traversal is gone and CLI won't hold the table locked
while computing filters.

nest/cli.c
nest/cli.h
nest/config.Y
nest/rt-show.c
nest/rt-table.c
nest/rt.h
proto/mrt/mrt.c

index b54a0d76dc26dcaf5cf07bff0b150058a1237061..31deef03f289a482093e4157d6ebdb8429e2870f 100644 (file)
@@ -319,7 +319,6 @@ cli_new(void *priv)
   c->event->data = c;
   c->cont = cli_hello;
   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;
@@ -409,11 +408,14 @@ void
 cli_free(cli *c)
 {
   cli_set_log_echo(c, 0, 0);
+  int defer = 0;
   if (c->cleanup)
-    c->cleanup(c);
+    defer = c->cleanup(c);
   if (c == cmd_reconfig_stored_cli)
     cmd_reconfig_stored_cli = NULL;
-  rfree(c->pool);
+
+  if (!defer)
+    rfree(c->pool);
 }
 
 /**
index 8a3294c52224d5e618219ddd5d77b6f0e7831902..925729683d025a76d8e7f06696d9097d5447d450 100644 (file)
@@ -33,12 +33,12 @@ typedef struct cli {
   struct cli_out *tx_buf, *tx_pos, *tx_write;
   event *event;
   void (*cont)(struct cli *c);
-  void (*cleanup)(struct cli *c);
+  int (*cleanup)(struct cli *c);       /* Return 0 if finished and cli may be freed immediately.
+                                          Otherwise return 1 and call rfree(c->pool) when appropriate. */
   void *rover;                         /* Private to continuation routine */
   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 */
index c3f2532fce6abd807399c2598ec4036ec51a63ee..ec10dd69cb2ddcaab848974d7640d03f57f396fc 100644 (file)
@@ -653,6 +653,7 @@ r_args:
      init_list(&($$->tables));
      $$->filter = FILTER_ACCEPT;
      $$->running_on_config = config;
+     $$->cli = this_cli;
    }
  | r_args net_any {
      $$ = $1;
index 12ddc816b08fc1e5a0dff8a1d61a9221a727463b..1404f7e6129a87e7d085f1e6ab6d93e503c8ff10 100644 (file)
 #include "sysdep/unix/krt.h"
 
 static void
-rt_show_table(struct cli *c, struct rt_show_data *d)
+rt_show_table(struct rt_show_data *d)
 {
+  struct cli *c = d->cli;
+
   /* No table blocks in 'show route count' */
   if (d->stats == 2)
     return;
@@ -70,7 +72,7 @@ rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, int primary
     bsprintf(info, " (%d)", rt_get_preference(e));
 
   if (d->last_table != d->tab)
-    rt_show_table(c, d);
+    rt_show_table(d);
 
   eattr *heea;
   struct hostentry_adata *had = NULL;
@@ -118,32 +120,10 @@ rt_show_rte(struct cli *c, byte *ia, rte *e, struct rt_show_data *d, int primary
     ea_show_list(c, a);
 }
 
-static uint
-rte_feed_count(net *n)
-{
-  uint count = 0;
-  for (struct rte_storage *e = n->routes; e; e = e->next)
-    if (rte_is_valid(RTE_OR_NULL(e)))
-      count++;
-  return count;
-}
-
 static void
-rte_feed_obtain(net *n, rte **feed, uint count)
-{
-  uint i = 0;
-  for (struct rte_storage *e = n->routes; e; e = e->next)
-    if (rte_is_valid(RTE_OR_NULL(e)))
-    {
-      ASSERT_DIE(i < count);
-      feed[i++] = &e->rte;
-    }
-  ASSERT_DIE(i == count);
-}
-
-static void
-rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
+rt_show_net(struct rt_show_data *d, const net_addr *n, rte **feed, uint count)
 {
+  struct cli *c = d->cli;
   byte ia[NET_MAX_TEXT_LENGTH+1];
   struct channel *ec = d->tab->export_channel;
 
@@ -155,9 +135,9 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
   int first_show = 1;
   int pass = 0;
 
-  for (struct rte_storage *er = n->routes; er; er = er->next)
+  for (uint i = 0; i < count; i++)
     {
-      if (!d->tab->prefilter && (rte_is_filtered(&er->rte) != d->filtered))
+      if (!d->tab->prefilter && (rte_is_filtered(feed[i]) != d->filtered))
        continue;
 
       d->rt_counter++;
@@ -167,7 +147,7 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
       if (pass)
        continue;
 
-      struct rte e = er->rte;
+      struct rte e = *feed[i];
       if (d->tab->prefilter)
        if (e.sender != d->tab->prefilter->in_req.hook)
          continue;
@@ -190,13 +170,7 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
        {
          /* Special case for merged export */
          pass = 1;
-         uint count = rte_feed_count(n);
-         if (!count)
-           goto skip;
-
-         rte **feed = alloca(count * sizeof(rte *));
-         rte_feed_obtain(n, feed, count);
-         rte *em = rt_export_merged(ec, feed, count, c->show_pool, 1);
+         rte *em = rt_export_merged(ec, feed, count, tmp_linpool, 1);
 
          if (em)
            e = *em;
@@ -241,159 +215,166 @@ rt_show_net(struct cli *c, net *n, struct rt_show_data *d)
       if (d->stats < 2)
       {
        if (first_show)
-         net_format(n->n.addr, ia, sizeof(ia));
+         net_format(n, ia, sizeof(ia));
        else
          ia[0] = 0;
 
-       rt_show_rte(c, ia, &e, d, !d->tab->prefilter && (n->routes == er));
+       rt_show_rte(c, ia, &e, d, !d->tab->prefilter && !i);
        first_show = 0;
       }
 
       d->show_counter++;
 
     skip:
-      lp_flush(c->show_pool);
-
       if (d->primary_only)
        break;
     }
+
+  if ((d->show_counter - d->show_counter_last_flush) > 64)
+  {
+    d->show_counter_last_flush = d->show_counter;
+    cli_write_trigger(d->cli);
+  }
 }
 
 static void
-rt_show_cleanup(struct cli *c)
+rt_show_net_export_bulk(struct rt_export_request *req, const net_addr *n,
+    struct rt_pending_export *rpe UNUSED, rte **feed, uint count)
 {
-  struct rt_show_data *d = c->rover;
-  struct rt_show_data_rtable *tab;
+  struct rt_show_data *d = SKIP_BACK(struct rt_show_data, req, req);
+  return rt_show_net(d, n, feed, count);
+}
 
-  /* Unlink the iterator */
-  if (d->table_open && !d->trie_walk)
-    fit_get(&d->tab->table->fib, &d->fit);
+static void
+rt_show_export_stopped_cleanup(struct rt_export_request *req)
+{
+  struct rt_show_data *d = SKIP_BACK(struct rt_show_data, req, req);
 
-  if (d->walk_lock)
-    rt_unlock_trie(d->tab->table, d->walk_lock);
+  /* The hook is now invalid */
+  req->hook = NULL;
 
   /* Unlock referenced tables */
+  struct rt_show_data_rtable *tab;
   WALK_LIST(tab, d->tables)
     rt_unlock_table(tab->table);
+
+  /* And free the CLI (deferred) */
+  rfree(d->cli->pool);
 }
 
-static void
-rt_show_cont(struct cli *c)
+static int
+rt_show_cleanup(struct cli *c)
 {
   struct rt_show_data *d = c->rover;
-  struct rtable *tab = d->tab->table;
-#ifdef DEBUGGING
-  unsigned max = 4;
-#else
-  unsigned max = 64;
-#endif
-  struct fib *fib = &tab->fib;
-  struct fib_iterator *it = &d->fit;
 
-  if (d->running_on_config && (d->running_on_config != config))
+  /* Cancel the feed */
+  if (d->req.hook)
   {
-    cli_printf(c, 8004, "Stopped due to reconfiguration");
-    goto done;
+    rt_stop_export(&d->req, rt_show_export_stopped_cleanup);
+    return 1;
   }
+  else
+    return 0;
+}
 
-  if (!d->table_open)
-  {
-    /* We use either trie-based walk or fib-based walk */
-    d->trie_walk = tab->trie &&
-      (d->addr_mode == RSD_ADDR_IN) &&
-      net_val_match(tab->addr_type, NB_IP);
+static void rt_show_export_stopped(struct rt_export_request *req);
 
-    if (d->trie_walk && !d->walk_state)
-      d->walk_state = lp_allocz(c->parser_pool, sizeof (struct f_trie_walk_state));
+static void
+rt_show_log_state_change(struct rt_export_request *req, u8 state)
+{
+  if (state == TES_READY)
+    rt_stop_export(req, rt_show_export_stopped);
+}
 
-    if (d->trie_walk)
-    {
-      d->walk_lock = rt_lock_trie(tab);
-      trie_walk_init(d->walk_state, tab->trie, d->addr);
-    }
-    else
-      FIB_ITERATE_INIT(&d->fit, &tab->fib);
+static void
+rt_show_dump_req(struct rt_export_request *req)
+{
+  debug("  CLI Show Route Feed %p\n", req);
+}
+
+static void
+rt_show_cont(struct rt_show_data *d)
+{
+  struct cli *c = d->cli;
 
-    d->table_open = 1;
-    d->table_counter++;
-    d->kernel = rt_show_get_kernel(d);
+  if (d->running_on_config && (d->running_on_config != config))
+  {
+    cli_printf(c, 8004, "Stopped due to reconfiguration");
 
-    d->show_counter_last = d->show_counter;
-    d->rt_counter_last   = d->rt_counter;
-    d->net_counter_last  = d->net_counter;
+    /* Unlock referenced tables */
+    struct rt_show_data_rtable *tab;
+    WALK_LIST(tab, d->tables)
+      rt_unlock_table(tab->table);
 
-    if (d->tables_defined_by & RSD_TDB_SET)
-      rt_show_table(c, d);
+    /* No more action */
+    c->cleanup = NULL;
+    c->cont = NULL;
+    c->rover = NULL;
+    cli_write_trigger(c);
+    return;
   }
 
-  if (d->trie_walk)
-  {
-    /* Trie-based walk */
-    net_addr addr;
-    while (trie_walk_next(d->walk_state, &addr))
-    {
-      net *n = net_find(tab, &addr);
-      if (!n)
-       continue;
+  d->req = (struct rt_export_request) {
+    .addr_in = (d->addr_mode == RSD_ADDR_IN) ? d->addr : NULL,
+    .name = "CLI Show Route",
+    .export_bulk = rt_show_net_export_bulk,
+    .dump_req = rt_show_dump_req,
+    .log_state_change = rt_show_log_state_change,
+  };
 
-      rt_show_net(c, n, d);
+  d->table_counter++;
+  d->kernel = rt_show_get_kernel(d);
 
-      if (!--max)
-       return;
-    }
+  d->show_counter_last = d->show_counter;
+  d->rt_counter_last   = d->rt_counter;
+  d->net_counter_last  = d->net_counter;
 
-    rt_unlock_trie(tab, d->walk_lock);
-    d->walk_lock = NULL;
-  }
-  else
-  {
-    /* fib-based walk */
-    FIB_ITERATE_START(fib, it, net, n)
-    {
-      if ((d->addr_mode == RSD_ADDR_IN) && (!net_in_netX(n->n.addr, d->addr)))
-       goto next;
+  if (d->tables_defined_by & RSD_TDB_SET)
+    rt_show_table(d);
 
-      if (!max--)
-      {
-       FIB_ITERATE_PUT(it);
-       return;
-      }
-      rt_show_net(c, n, d);
+  rt_request_export(&d->tab->table->exporter, &d->req);
+}
 
-    next:;
-    }
-    FIB_ITERATE_END;
-  }
+static void
+rt_show_export_stopped(struct rt_export_request *req)
+{
+  struct rt_show_data *d = SKIP_BACK(struct rt_show_data, req, req);
+
+  /* The hook is now invalid */
+  req->hook = NULL;
 
   if (d->stats)
   {
     if (d->last_table != d->tab)
-      rt_show_table(c, d);
+      rt_show_table(d);
 
-    cli_printf(c, -1007, "%d of %d routes for %d networks in table %s",
+    cli_printf(d->cli, -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, tab->name);
+              d->net_counter - d->net_counter_last, d->tab->table->name);
   }
 
   d->kernel = NULL;
-  d->table_open = 0;
   d->tab = NODE_NEXT(d->tab);
 
   if (NODE_VALID(d->tab))
-    return;
+    return rt_show_cont(d);
+
+  /* Unlock referenced tables */
+  struct rt_show_data_rtable *tab;
+  WALK_LIST(tab, d->tables)
+    rt_unlock_table(tab->table);
 
+  /* Printout total stats */
   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",
+    if (d->last_table) cli_printf(d->cli, -1007, "");
+    cli_printf(d->cli, 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, "");
+    cli_printf(d->cli, 0, "");
 
-done:
-  rt_show_cleanup(c);
-  c->cont = c->cleanup = NULL;
+  cli_write_trigger(d->cli);
 }
 
 struct rt_show_data_rtable *
@@ -491,6 +472,12 @@ rt_show_prepare_tables(struct rt_show_data *d)
     cf_error("No valid tables");
 }
 
+static void
+rt_show_dummy_cont(struct cli *c UNUSED)
+{
+  /* Explicitly do nothing to prevent CLI from trying to parse another command. */
+}
+
 void
 rt_show(struct rt_show_data *d)
 {
@@ -510,12 +497,16 @@ rt_show(struct rt_show_data *d)
 
     /* 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;
+    this_cli->cont = rt_show_dummy_cont;
+    rt_show_cont(d);
   }
   else
   {
+    uint max = 64;
+    rte **feed = mb_alloc(d->cli->pool, sizeof(rte *) * max);
+
     WALK_LIST(tab, d->tables)
     {
       d->tab = tab;
@@ -526,8 +517,24 @@ rt_show(struct rt_show_data *d)
       else
        n = net_find(tab->table, d->addr);
 
-      if (n)
-       rt_show_net(this_cli, n, d);
+      uint count = 0;
+      for (struct rte_storage *e = n->routes; e; e = e->next)
+       count++;
+
+      if (!count)
+       continue;
+
+      if (count > max)
+      {
+       do max *= 2; while (count > max);
+       feed = mb_realloc(feed, sizeof(rte *) * max);
+      }
+
+      uint i = 0;
+      for (struct rte_storage *e = n->routes; e; e = e->next)
+       feed[i++] = &e->rte;
+
+      rt_show_net(d, n->n.addr, feed, count);
     }
 
     if (d->rt_counter)
index afafef6231bbb71a281b77fd3751e1898f69668b..ed0132ca9feee7255d4f639896992c67bf313854 100644 (file)
@@ -1813,7 +1813,16 @@ rt_table_export_stop(struct rt_export_hook *hook)
   rtable *tab = SKIP_BACK(rtable, exporter, hook->table);
 
   if (hook->export_state == TES_FEEDING)
-    fit_get(&tab->fib, &hook->feed_fit);
+    if (hook->walk_lock)
+    {
+      rt_unlock_trie(tab, hook->walk_lock);
+      hook->walk_lock = NULL;
+
+      mb_free(hook->walk_state);
+      hook->walk_state = NULL;
+    }
+    else
+      fit_get(&tab->fib, &hook->feed_fit);
 }
 
 void
index 57e117da720e745c88bf9eb1f8a8e2593724d314..c959d3586069a30f6c2e68c3ba926a442b377a28 100644 (file)
--- a/nest/rt.h
+++ b/nest/rt.h
@@ -403,13 +403,12 @@ struct rt_show_data_rtable {
 };
 
 struct rt_show_data {
+  struct cli *cli;                     /* Pointer back to the CLI */
   net_addr *addr;
   list tables;
   struct rt_show_data_rtable *tab;     /* Iterator over table list */
   struct rt_show_data_rtable *last_table; /* Last table in output */
-  struct fib_iterator fit;             /* Iterator over networks in table */
-  struct f_trie_walk_state *walk_state;        /* Iterator over networks in trie */
-  struct f_trie *walk_lock;            /* Locked trie for walking */
+  struct rt_export_request req;                /* Export request in use */
   int verbose, tables_defined_by;
   const struct filter *filter;
   struct proto *show_protocol;
@@ -420,10 +419,9 @@ struct rt_show_data {
   struct rt_export_hook *kernel_export_hook;
   int export_mode, addr_mode, primary_only, filtered, stats;
 
-  int table_open;                      /* Iteration (fit) is open */
-  int trie_walk;                       /* Current table is iterated using trie */
   int net_counter, rt_counter, show_counter, table_counter;
   int net_counter_last, rt_counter_last, show_counter_last;
+  int show_counter_last_flush;
 };
 
 void rt_show(struct rt_show_data *);
index fcbe317b4ffe95476cf51c445ab6b9241d4da28f..5ef4cd4464e98aa69444921ec649b74309c5523e 100644 (file)
@@ -703,14 +703,17 @@ mrt_dump_cont(struct cli *c)
 
   cli_printf(c, 0, "");
   mrt_table_dump_free(c->rover);
-  c->cont = c->cleanup = c->rover = NULL;
+  c->cont = NULL;
+  c->cleanup = NULL;
+  c->rover = NULL;
 }
 
-static void
+static int
 mrt_dump_cleanup(struct cli *c)
 {
   mrt_table_dump_free(c->rover);
   c->rover = NULL;
+  return 0;
 }
 
 void