]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Miscellaneous refactoring
authorMaria Matejka <mq@ucw.cz>
Wed, 31 Aug 2022 09:58:27 +0000 (11:58 +0200)
committerMaria Matejka <mq@ucw.cz>
Thu, 1 Sep 2022 16:46:40 +0000 (18:46 +0200)
nest/proto.c
nest/rt-table.c
nest/rt.h
proto/bgp/attrs.c

index ef3e3f4db4dc00fa6cd92d96e53b8b07414372a6..d2374792b8b0562751b2b63846b2d9c7e6a4519c 100644 (file)
@@ -450,12 +450,8 @@ channel_start_import(struct channel *c)
     return;
   }
 
-  int nlen = strlen(c->name) + strlen(c->proto->name) + 2;
-  char *rn = mb_allocz(c->proto->pool, nlen);
-  bsprintf(rn, "%s.%s", c->proto->name, c->name);
-
   c->in_req = (struct rt_import_request) {
-    .name = rn,
+    .name = mb_sprintf(c->proto->pool, "%s.%s", c->proto->name, c->name),
     .trace_routes = c->debug | c->proto->debug,
     .dump_req = channel_dump_import_req,
     .log_state_change = channel_import_log_state_change,
@@ -483,12 +479,9 @@ channel_start_export(struct channel *c)
   }
 
   ASSERT(c->channel_state == CS_UP);
-  int nlen = strlen(c->name) + strlen(c->proto->name) + 2;
-  char *rn = mb_allocz(c->proto->pool, nlen);
-  bsprintf(rn, "%s.%s", c->proto->name, c->name);
 
   c->out_req = (struct rt_export_request) {
-    .name = rn,
+    .name = mb_sprintf(c->proto->pool, "%s.%s", c->proto->name, c->name),
     .list = proto_work_list(c->proto),
     .addr = c->out_subprefix,
     .addr_mode = c->out_subprefix ? TE_ADDR_IN : TE_ADDR_NONE,
index 7fc0d280d0f1746fdf3a7b4d36ead46960468c22..d4e0d63cf58bd5f6505da0f8cd742e937fee634f 100644 (file)
@@ -908,7 +908,7 @@ channel_rpe_mark_seen(struct rt_export_request *req, struct rt_pending_export *r
 }
 
 void
-rt_notify_accepted(struct rt_export_request *req, const net_addr *n, struct rt_pending_export *rpe,
+rt_notify_accepted(struct rt_export_request *req, const net_addr *n, struct rt_pending_export *first,
     struct rte **feed, uint count)
 {
   struct channel *c = SKIP_BACK(struct channel, out_req, req);
@@ -952,7 +952,7 @@ rt_notify_accepted(struct rt_export_request *req, const net_addr *n, struct rt_p
 
 done:
   /* Check obsolete routes for previously exported */
-  while (rpe)
+  RPE_WALK(first, rpe, NULL)
   {
     channel_rpe_mark_seen(req, rpe);
     if (rpe->old)
@@ -963,7 +963,6 @@ done:
        old_best = &rpe->old->rte;
       }
     }
-    rpe = rpe_next(rpe, NULL);
   }
 
   /* Nothing to export */
@@ -1036,7 +1035,7 @@ rt_export_merged(struct channel *c, struct rte **feed, uint count, linpool *pool
 }
 
 void
-rt_notify_merged(struct rt_export_request *req, const net_addr *n, struct rt_pending_export *rpe,
+rt_notify_merged(struct rt_export_request *req, const net_addr *n, struct rt_pending_export *first,
     struct rte **feed, uint count)
 {
   struct channel *c = SKIP_BACK(struct channel, out_req, req);
@@ -1062,7 +1061,7 @@ rt_notify_merged(struct rt_export_request *req, const net_addr *n, struct rt_pen
     }
 
   /* Check obsolete routes for previously exported */
-  while (rpe)
+  RPE_WALK(first, rpe, NULL)
   {
     channel_rpe_mark_seen(req, rpe);
     if (rpe->old)
@@ -1073,7 +1072,6 @@ rt_notify_merged(struct rt_export_request *req, const net_addr *n, struct rt_pen
        old_best = &rpe->old->rte;
       }
     }
-    rpe = rpe_next(rpe, NULL);
   }
 
   /* Prepare new merged route */
@@ -1084,17 +1082,16 @@ rt_notify_merged(struct rt_export_request *req, const net_addr *n, struct rt_pen
 }
 
 void
-rt_notify_optimal(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *rpe)
+rt_notify_optimal(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *first)
 {
   struct channel *c = SKIP_BACK(struct channel, out_req, req);
-  rte *o = RTE_VALID_OR_NULL(rpe->old_best);
-  struct rte_storage *new_best = rpe->new_best;
+  rte *o = RTE_VALID_OR_NULL(first->old_best);
+  struct rte_storage *new_best = first->new_best;
 
-  while (rpe)
+  RPE_WALK(first, rpe, NULL)
   {
     channel_rpe_mark_seen(req, rpe);
     new_best = rpe->new_best;
-    rpe = rpe_next(rpe, NULL);
   }
 
   rte n0 = RTE_COPY_VALID(new_best);
@@ -1103,27 +1100,26 @@ rt_notify_optimal(struct rt_export_request *req, const net_addr *net, struct rt_
 }
 
 void
-rt_notify_any(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *rpe)
+rt_notify_any(struct rt_export_request *req, const net_addr *net, struct rt_pending_export *first)
 {
   struct channel *c = SKIP_BACK(struct channel, out_req, req);
 
-  rte *n = RTE_VALID_OR_NULL(rpe->new);
-  rte *o = RTE_VALID_OR_NULL(rpe->old);
+  rte *n = RTE_VALID_OR_NULL(first->new);
+  rte *o = RTE_VALID_OR_NULL(first->old);
 
   if (!n && !o)
   {
-    channel_rpe_mark_seen(req, rpe);
+    channel_rpe_mark_seen(req, first);
     return;
   }
 
   struct rte_src *src = n ? n->src : o->src;
-  struct rte_storage *new_latest = rpe->new;
+  struct rte_storage *new_latest = first->new;
 
-  while (rpe)
+  RPE_WALK(first, rpe, src)
   {
     channel_rpe_mark_seen(req, rpe);
     new_latest = rpe->new;
-    rpe = rpe_next(rpe, src);
   }
 
   rte n0 = RTE_COPY_VALID(new_latest);
@@ -1362,7 +1358,7 @@ rte_announce(rtable *tab, net *net, struct rte_storage *new, struct rte_storage
          &net->last->next, &rpenull, rpe,
          memory_order_relaxed,
          memory_order_relaxed));
-    
+
   }
 
   net->last = rpe;
@@ -1918,8 +1914,13 @@ rt_table_export_done(struct rt_export_hook *hook)
   struct rt_exporter *re = hook->table;
   struct rtable *tab = SKIP_BACK(struct rtable, exporter, re);
 
-  rt_unlock_table(tab);
   DBG("Export hook %p in table %s finished uc=%u\n", hook, tab->name, tab->use_count);
+
+  /* Free the hook before unlocking the table */
+  rfree(hook->pool);
+
+  /* Unlock the table; this may free it */
+  rt_unlock_table(tab);
 }
 
 static void
@@ -1935,13 +1936,10 @@ rt_export_stopped(void *data)
   rem_node(&hook->n);
 
   /* Report the channel as stopped. */
-  hook->stopped(hook->req);
+  CALL(hook->stopped, hook->req);
 
   /* Reporting the hook as finished. */
   CALL(tab->done, hook);
-
-  /* Free the hook. */
-  rfree(hook->pool);
 }
 
 static inline void
@@ -1950,8 +1948,7 @@ rt_set_import_state(struct rt_import_hook *hook, u8 state)
   hook->last_state_change = current_time();
   hook->import_state = state;
 
-  if (hook->req->log_state_change)
-    hook->req->log_state_change(hook->req, state);
+  CALL(hook->req->log_state_change, hook->req, state);
 }
 
 void
@@ -1960,8 +1957,7 @@ rt_set_export_state(struct rt_export_hook *hook, u8 state)
   hook->last_state_change = current_time();
   atomic_store_explicit(&hook->export_state, state, memory_order_release);
 
-  if (hook->req->log_state_change)
-    hook->req->log_state_change(hook->req, state);
+  CALL(hook->req->log_state_change, hook->req, state);
 }
 
 void
@@ -2919,7 +2915,7 @@ rt_export_cleanup(rtable *tab)
     net *net = SKIP_BACK(struct network, n.addr, (net_addr (*)[0]) n);
 
     ASSERT_DIE(net->first == first);
-    
+
     if (first == net->last)
       /* The only export here */
       net->last = net->first = NULL;
@@ -2949,7 +2945,7 @@ rt_export_cleanup(rtable *tab)
     ASSERT_DIE(pos < end);
 
     struct rt_pending_export *next = NULL;
-    
+
     if (++pos < end)
       next = &reb->export[pos];
     else
@@ -3931,9 +3927,7 @@ rt_feed_net(struct rt_export_hook *c, net *n)
     count = 1;
   }
 
-  for (struct rt_pending_export *rpe = n->first; rpe; rpe = rpe_next(rpe, NULL))
-    rpe_mark_seen(c, rpe);
-
+  rpe_mark_seen_all(c, n->first, NULL);
   return count;
 }
 
index 66edf227f1dbf18fd0aa0a8d167bf2789941abf0..e7ba55f60d84b230f85637c38ab11ea7f21d6e63 100644 (file)
--- a/nest/rt.h
+++ b/nest/rt.h
@@ -386,9 +386,16 @@ void rte_import(struct rt_import_request *req, const net_addr *net, rte *new, st
 /* Get next rpe. If src is given, it must match. */
 struct rt_pending_export *rpe_next(struct rt_pending_export *rpe, struct rte_src *src);
 
+/* Walk all rpe's */
+#define RPE_WALK(first, it, src) \
+  for (struct rt_pending_export *it = (first); it; it = rpe_next(it, (src)))
+
 /* Mark the pending export processed */
 void rpe_mark_seen(struct rt_export_hook *hook, struct rt_pending_export *rpe);
 
+#define rpe_mark_seen_all(hook, first, src) \
+  RPE_WALK(first, rpe, src) rpe_mark_seen((hook), rpe)
+
 /* Get pending export seen status */
 int rpe_get_seen(struct rt_export_hook *hook, struct rt_pending_export *rpe);
 
index 2543ee7344b08fd3a8e847bd8ac9317cefcfb459..84a0b023e592491b11d62a66008a47ea0ca0cad5 100644 (file)
@@ -1977,6 +1977,12 @@ bgp_out_table_export_start(struct rt_exporter *re, struct rt_export_request *req
   return hook;
 }
 
+static void
+bgp_out_table_export_done(struct rt_export_hook *hook)
+{
+  rfree(hook->pool);
+}
+
 void
 bgp_setup_out_table(struct bgp_channel *c)
 {
@@ -1985,6 +1991,7 @@ bgp_setup_out_table(struct bgp_channel *c)
   c->prefix_exporter = (struct rt_exporter) {
     .addr_type = c->c.table->addr_type,
     .start = bgp_out_table_export_start,
+    .done = bgp_out_table_export_done,
   };
 
   init_list(&c->prefix_exporter.hooks);