]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
IGP metric getter refactoring to protocol callback
authorMaria Matejka <mq@ucw.cz>
Sat, 20 Mar 2021 22:18:34 +0000 (23:18 +0100)
committerMaria Matejka <mq@ucw.cz>
Wed, 13 Oct 2021 17:09:04 +0000 (19:09 +0200)
Direct protocol hooks for IGP metric inside nest/rt-table.c make the
protocol API unnecessarily complex. Instead, we use a proper callback.

nest/protocol.h
nest/rt-table.c
proto/babel/babel.c
proto/bgp/attrs.c
proto/bgp/bgp.c
proto/bgp/bgp.h
proto/ospf/ospf.c
proto/rip/rip.c

index 6ee97b7cd2d4fb7935fa91cbb33241d850bc2dfa..62fd2b66ae902d09de074a98da7f2b16a0e2c9ec 100644 (file)
@@ -241,6 +241,7 @@ struct proto {
   struct rte * (*rte_modify)(struct rte *, struct linpool *);
   void (*rte_insert)(struct network *, struct rte *);
   void (*rte_remove)(struct network *, struct rte *);
+  u32 (*rte_igp_metric)(struct rte *);
 
   /* Hic sunt protocol-specific data */
 };
index a869bb1882510c811160a5895369bfea33fb87ab..844c7a683713132ce9a073c9f86d821b48966ee7 100644 (file)
 #include "lib/string.h"
 #include "lib/alloca.h"
 
-#ifdef CONFIG_BGP
-#include "proto/bgp/bgp.h"
-#endif
-
 pool *rt_table_pool;
 
 static slab *rte_slab;
@@ -3022,36 +3018,12 @@ rt_get_igp_metric(rte *rt)
   if (ea)
     return ea->u.data;
 
-  rta *a = rt->attrs;
-
-#ifdef CONFIG_OSPF
-  if ((a->source == RTS_OSPF) ||
-      (a->source == RTS_OSPF_IA) ||
-      (a->source == RTS_OSPF_EXT1))
-    return rt->u.ospf.metric1;
-#endif
-
-#ifdef CONFIG_RIP
-  if (a->source == RTS_RIP)
-    return rt->u.rip.metric;
-#endif
-
-#ifdef CONFIG_BGP
-  if (a->source == RTS_BGP)
-  {
-    u64 metric = bgp_total_aigp_metric(rt);
-    return (u32) MIN(metric, (u64) IGP_METRIC_UNKNOWN);
-  }
-#endif
-
-#ifdef CONFIG_BABEL
-  if (a->source == RTS_BABEL)
-    return rt->u.babel.metric;
-#endif
-
-  if (a->source == RTS_DEVICE)
+  if (rt->attrs->source == RTS_DEVICE)
     return 0;
 
+  if (rt->src->proto->rte_igp_metric)
+    return rt->src->proto->rte_igp_metric(rt);
+
   return IGP_METRIC_UNKNOWN;
 }
 
index 1d23aef79b0b20e0d26710b627d4fce26e237dbc..d17f318be8571b6eeab16cfa19d40f08b836eca1 100644 (file)
@@ -2332,6 +2332,12 @@ babel_rte_same(struct rte *new, struct rte *old)
          (new->u.babel.router_id == old->u.babel.router_id));
 }
 
+static u32
+babel_rte_igp_metric(struct rte *rt)
+{
+  return rt->u.babel.metric;
+}
+
 
 static void
 babel_postconfig(struct proto_config *CF)
@@ -2367,6 +2373,7 @@ babel_init(struct proto_config *CF)
   P->store_tmp_attrs = babel_store_tmp_attrs;
   P->rte_better = babel_rte_better;
   P->rte_same = babel_rte_same;
+  P->rte_igp_metric = babel_rte_igp_metric;
 
   return P;
 }
index 107060884b6773e1f5ce4a56473c2536dcc595bb..18d2985c9978cefa119fabd7440e0364a5545c80 100644 (file)
@@ -371,6 +371,13 @@ bgp_init_aigp_metric(rte *e, u64 *metric, const struct adata **ad)
   return *metric < IGP_METRIC_UNKNOWN;
 }
 
+u32
+bgp_rte_igp_metric(struct rte *rt)
+{
+  u64 metric = bgp_total_aigp_metric(rt);
+  return (u32) MIN(metric, (u64) IGP_METRIC_UNKNOWN);
+}
+
 
 /*
  *     Attribute hooks
index e4d754b1af5e7228f2275da3bf19b5ec6e442473..5c78bfa15a56fe0f487fc5e781597788b83f9abe 100644 (file)
@@ -1694,6 +1694,7 @@ bgp_init(struct proto_config *CF)
   P->rte_mergable = bgp_rte_mergable;
   P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL;
   P->rte_modify = bgp_rte_modify_stale;
+  P->rte_igp_metric = bgp_rte_igp_metric;
 
   p->cf = cf;
   p->is_internal = (cf->local_as == cf->remote_as);
index 20944fe67610e2c402f51dc30fe09e9db8aa8cad..c440c7af0079a5bc5db2d4594720614909207311 100644 (file)
@@ -582,6 +582,7 @@ int bgp_rte_better(struct rte *, struct rte *);
 int bgp_rte_mergable(rte *pri, rte *sec);
 int bgp_rte_recalculate(rtable *table, net *net, rte *new, rte *old, rte *old_best);
 struct rte *bgp_rte_modify_stale(struct rte *r, struct linpool *pool);
+u32 bgp_rte_igp_metric(struct rte *);
 void bgp_rt_notify(struct proto *P, struct channel *C, net *n, rte *new, rte *old);
 int bgp_preexport(struct proto *, struct rte *);
 int bgp_get_attr(const struct eattr *e, byte *buf, int buflen);
index 03b1635019f9b5c8444189c56db4ec798d474b33..43b8f0375e6bfb76c11da05d3431b7c765840f31 100644 (file)
@@ -113,6 +113,7 @@ static void ospf_store_tmp_attrs(struct rte *rt, struct linpool *pool);
 static void ospf_reload_routes(struct channel *C);
 static int ospf_rte_better(struct rte *new, struct rte *old);
 static int ospf_rte_same(struct rte *new, struct rte *old);
+static u32 ospf_rte_igp_metric(struct rte *rt);
 static void ospf_disp(timer *timer);
 
 
@@ -382,6 +383,7 @@ ospf_init(struct proto_config *CF)
   P->store_tmp_attrs = ospf_store_tmp_attrs;
   P->rte_better = ospf_rte_better;
   P->rte_same = ospf_rte_same;
+  P->rte_igp_metric = ospf_rte_igp_metric;
 
   return P;
 }
@@ -419,6 +421,11 @@ ospf_rte_same(struct rte *new, struct rte *old)
     new->u.ospf.router_id == old->u.ospf.router_id;
 }
 
+static u32
+ospf_rte_igp_metric(struct rte *rt)
+{
+  return rt->u.ospf.metric1;
+}
 
 void
 ospf_schedule_rtcalc(struct ospf_proto *p)
index f2e56e93cb862e58662834e3d7f1c9b8b54b2e20..2653b23b434afdef789ee017f7906a2ddb95cd0b 100644 (file)
@@ -1098,6 +1098,11 @@ rip_rte_same(struct rte *new, struct rte *old)
          (new->u.rip.from == old->u.rip.from));
 }
 
+static u32
+rip_rte_igp_metric(struct rte *rt)
+{
+  return rt->u.rip.metric;
+}
 
 static void
 rip_postconfig(struct proto_config *CF)
@@ -1124,6 +1129,7 @@ rip_init(struct proto_config *CF)
   P->store_tmp_attrs = rip_store_tmp_attrs;
   P->rte_better = rip_rte_better;
   P->rte_same = rip_rte_same;
+  P->rte_igp_metric = rip_rte_igp_metric;
 
   return P;
 }