From d471d5fc7ce587ed836ca7fa10a79331bc181d45 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Sat, 20 Mar 2021 23:18:34 +0100 Subject: IGP metric getter refactoring to protocol callback 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 | 1 + nest/rt-table.c | 36 ++++-------------------------------- proto/babel/babel.c | 7 +++++++ proto/bgp/attrs.c | 7 +++++++ proto/bgp/bgp.c | 1 + proto/bgp/bgp.h | 1 + proto/ospf/ospf.c | 7 +++++++ proto/rip/rip.c | 6 ++++++ 8 files changed, 34 insertions(+), 32 deletions(-) diff --git a/nest/protocol.h b/nest/protocol.h index 6ee97b7c..62fd2b66 100644 --- a/nest/protocol.h +++ b/nest/protocol.h @@ -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 */ }; diff --git a/nest/rt-table.c b/nest/rt-table.c index a869bb18..844c7a68 100644 --- a/nest/rt-table.c +++ b/nest/rt-table.c @@ -45,10 +45,6 @@ #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; } diff --git a/proto/babel/babel.c b/proto/babel/babel.c index 1d23aef7..d17f318b 100644 --- a/proto/babel/babel.c +++ b/proto/babel/babel.c @@ -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; } diff --git a/proto/bgp/attrs.c b/proto/bgp/attrs.c index 10706088..18d2985c 100644 --- a/proto/bgp/attrs.c +++ b/proto/bgp/attrs.c @@ -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 diff --git a/proto/bgp/bgp.c b/proto/bgp/bgp.c index e4d754b1..5c78bfa1 100644 --- a/proto/bgp/bgp.c +++ b/proto/bgp/bgp.c @@ -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); diff --git a/proto/bgp/bgp.h b/proto/bgp/bgp.h index 20944fe6..c440c7af 100644 --- a/proto/bgp/bgp.h +++ b/proto/bgp/bgp.h @@ -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); diff --git a/proto/ospf/ospf.c b/proto/ospf/ospf.c index 03b16350..43b8f037 100644 --- a/proto/ospf/ospf.c +++ b/proto/ospf/ospf.c @@ -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) diff --git a/proto/rip/rip.c b/proto/rip/rip.c index f2e56e93..2653b23b 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -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; } -- cgit v1.2.3 From 6e13df70fdaeb0f08ae5171f62240402501783ea Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 14 Sep 2021 19:53:03 +0200 Subject: Extended route attributes may include also pointers --- nest/route.h | 7 ++++--- nest/rt-attr.c | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/nest/route.h b/nest/route.h index 1dea058f..67df1ce0 100644 --- a/nest/route.h +++ b/nest/route.h @@ -501,8 +501,8 @@ typedef struct eattr { byte flags; /* Protocol-dependent flags */ byte type; /* Attribute type and several flags (EAF_...) */ union { - u32 data; - const struct adata *ptr; /* Attribute data elsewhere */ + uintptr_t data; + const struct adata *ptr; /* Attribute data elsewhere */ } u; } eattr; @@ -533,6 +533,7 @@ const char *ea_custom_name(uint ea); #define EAF_TYPE_AS_PATH 0x06 /* BGP AS path (encoding per RFC 1771:4.3) */ #define EAF_TYPE_BITFIELD 0x09 /* 32-bit embedded bitfield */ #define EAF_TYPE_INT_SET 0x0a /* Set of u32's (e.g., a community list) */ +#define EAF_TYPE_PTR 0x0d /* Pointer to an object */ #define EAF_TYPE_EC_SET 0x0e /* Set of pairs of u32's - ext. community list */ #define EAF_TYPE_LC_SET 0x12 /* Set of triplets of u32's - large community list */ #define EAF_TYPE_UNDEF 0x1f /* `force undefined' entry */ @@ -587,7 +588,7 @@ struct ea_walk_state { eattr *ea_find(ea_list *, unsigned ea); eattr *ea_walk(struct ea_walk_state *s, uint id, uint max); -int ea_get_int(ea_list *, unsigned ea, int def); +uintptr_t ea_get_int(ea_list *, unsigned ea, uintptr_t def); void ea_dump(ea_list *); void ea_sort(ea_list *); /* Sort entries in all sub-lists */ unsigned ea_scan(ea_list *); /* How many bytes do we need for merged ea_list */ diff --git a/nest/rt-attr.c b/nest/rt-attr.c index 4057bf37..00744bea 100644 --- a/nest/rt-attr.c +++ b/nest/rt-attr.c @@ -541,8 +541,8 @@ ea_walk(struct ea_walk_state *s, uint id, uint max) * by calling ea_find() to find the attribute, extracting its value or returning * a provided default if no such attribute is present. */ -int -ea_get_int(ea_list *e, unsigned id, int def) +uintptr_t +ea_get_int(ea_list *e, unsigned id, uintptr_t def) { eattr *a = ea_find(e, id); if (!a) -- cgit v1.2.3 From a0e4c66404681c5613ef7f6e80f8b8c3022446d4 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 14 Sep 2021 20:12:33 +0200 Subject: RIP: convert the rte-local attributes to extended attributes --- nest/route.h | 7 ------ proto/rip/rip.c | 72 ++++++++++++++++++++++++++++----------------------------- proto/rip/rip.h | 1 + 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/nest/route.h b/nest/route.h index 67df1ce0..32200da9 100644 --- a/nest/route.h +++ b/nest/route.h @@ -244,13 +244,6 @@ typedef struct rte { byte pflags; /* Protocol-specific flags */ btime lastmod; /* Last modified */ union { /* Protocol-dependent data (metrics etc.) */ -#ifdef CONFIG_RIP - struct { - struct iface *from; /* Incoming iface */ - u8 metric; /* RIP metric */ - u16 tag; /* External route tag */ - } rip; -#endif #ifdef CONFIG_OSPF struct { u32 metric1, metric2; /* OSPF Type 1 and Type 2 metrics */ diff --git a/proto/rip/rip.c b/proto/rip/rip.c index 2653b23b..0556939a 100644 --- a/proto/rip/rip.c +++ b/proto/rip/rip.c @@ -188,12 +188,28 @@ rip_announce_rte(struct rip_proto *p, struct rip_entry *en) a0.nh.iface = rt->from->ifa->iface; } + a0.eattrs = alloca(sizeof(ea_list) + 3*sizeof(eattr)); + memset(a0.eattrs, 0, sizeof(ea_list)); /* Zero-ing only the ea_list header */ + a0.eattrs->count = 3; + a0.eattrs->attrs[0] = (eattr) { + .id = EA_RIP_METRIC, + .type = EAF_TYPE_INT, + .u.data = rt_metric, + }; + a0.eattrs->attrs[1] = (eattr) { + .id = EA_RIP_TAG, + .type = EAF_TYPE_INT, + .u.data = rt_tag, + }; + a0.eattrs->attrs[2] = (eattr) { + .id = EA_RIP_FROM, + .type = EAF_TYPE_PTR, + .u.data = (uintptr_t) a0.nh.iface, + }; + rta *a = rta_lookup(&a0); rte *e = rte_get_temp(a, p->p.main_source); - e->u.rip.from = a0.nh.iface; - e->u.rip.metric = rt_metric; - e->u.rip.tag = rt_tag; e->pflags = EA_ID_FLAG(EA_RIP_METRIC) | EA_ID_FLAG(EA_RIP_TAG); rte_update(&p->p, en->n.addr, e); @@ -307,8 +323,9 @@ rip_rt_notify(struct proto *P, struct channel *ch UNUSED, struct network *net, s if (new) { /* Update */ - u32 rt_metric = ea_get_int(new->attrs->eattrs, EA_RIP_METRIC, 1); u32 rt_tag = ea_get_int(new->attrs->eattrs, EA_RIP_TAG, 0); + u32 rt_metric = ea_get_int(new->attrs->eattrs, EA_RIP_METRIC, 1); + struct iface *rt_from = (struct iface *) ea_get_int(new->attrs->eattrs, EA_RIP_FROM, 0); if (rt_metric > p->infinity) { @@ -339,7 +356,7 @@ rip_rt_notify(struct proto *P, struct channel *ch UNUSED, struct network *net, s en->valid = RIP_ENTRY_VALID; en->metric = rt_metric; en->tag = rt_tag; - en->from = (new->src->proto == P) ? new->u.rip.from : NULL; + en->from = (new->src->proto == P) ? rt_from : NULL; en->iface = new->attrs->nh.iface; en->next_hop = new->attrs->nh.gw; } @@ -1068,40 +1085,22 @@ rip_reload_routes(struct channel *C) rip_kick_timer(p); } -static void -rip_make_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - rte_init_tmp_attrs(rt, pool, 2); - rte_make_tmp_attr(rt, EA_RIP_METRIC, EAF_TYPE_INT, rt->u.rip.metric); - rte_make_tmp_attr(rt, EA_RIP_TAG, EAF_TYPE_INT, rt->u.rip.tag); -} - -static void -rip_store_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - rte_init_tmp_attrs(rt, pool, 2); - rt->u.rip.metric = rte_store_tmp_attr(rt, EA_RIP_METRIC); - rt->u.rip.tag = rte_store_tmp_attr(rt, EA_RIP_TAG); -} - static int rip_rte_better(struct rte *new, struct rte *old) { - return new->u.rip.metric < old->u.rip.metric; -} + ASSERT_DIE(new->src == old->src); + struct rip_proto *p = (struct rip_proto *) new->src->proto; -static int -rip_rte_same(struct rte *new, struct rte *old) -{ - return ((new->u.rip.metric == old->u.rip.metric) && - (new->u.rip.tag == old->u.rip.tag) && - (new->u.rip.from == old->u.rip.from)); + u32 new_metric = ea_get_int(new->attrs->eattrs, EA_RIP_METRIC, p->infinity); + u32 old_metric = ea_get_int(old->attrs->eattrs, EA_RIP_METRIC, p->infinity); + + return new_metric < old_metric; } static u32 rip_rte_igp_metric(struct rte *rt) { - return rt->u.rip.metric; + return ea_get_int(rt->attrs->eattrs, EA_RIP_METRIC, IGP_METRIC_UNKNOWN); } static void @@ -1125,10 +1124,7 @@ rip_init(struct proto_config *CF) P->rt_notify = rip_rt_notify; P->neigh_notify = rip_neigh_notify; P->reload_routes = rip_reload_routes; - P->make_tmp_attrs = rip_make_tmp_attrs; - 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; @@ -1204,10 +1200,14 @@ rip_reconfigure(struct proto *P, struct proto_config *CF) static void rip_get_route_info(rte *rte, byte *buf) { - buf += bsprintf(buf, " (%d/%d)", rte->attrs->pref, rte->u.rip.metric); + struct rip_proto *p = (struct rip_proto *) rte->src->proto; + u32 rt_metric = ea_get_int(rte->attrs->eattrs, EA_RIP_METRIC, p->infinity); + u32 rt_tag = ea_get_int(rte->attrs->eattrs, EA_RIP_TAG, 0); + + buf += bsprintf(buf, " (%d/%d)", rte->attrs->pref, rt_metric); - if (rte->u.rip.tag) - bsprintf(buf, " [%04x]", rte->u.rip.tag); + if (rt_tag) + bsprintf(buf, " [%04x]", rt_tag); } static int diff --git a/proto/rip/rip.h b/proto/rip/rip.h index 8d347000..f8713c4a 100644 --- a/proto/rip/rip.h +++ b/proto/rip/rip.h @@ -197,6 +197,7 @@ struct rip_rte #define EA_RIP_METRIC EA_CODE(PROTOCOL_RIP, 0) #define EA_RIP_TAG EA_CODE(PROTOCOL_RIP, 1) +#define EA_RIP_FROM EA_CODE(PROTOCOL_RIP, 2) static inline int rip_is_v2(struct rip_proto *p) { return p->rip2; } -- cgit v1.2.3 From 5f0cb61d82315035a3a7f10eef510884da9f94c6 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Tue, 11 Feb 2020 21:24:15 +0100 Subject: OSPF: Convert the rte-local attributes to extended attributes --- nest/route.h | 7 ------ proto/ospf/ospf.c | 71 ++++++++++++++++++------------------------------------- proto/ospf/rt.c | 46 +++++++++++++++++++++++++---------- 3 files changed, 57 insertions(+), 67 deletions(-) diff --git a/nest/route.h b/nest/route.h index 32200da9..1ed40d68 100644 --- a/nest/route.h +++ b/nest/route.h @@ -244,13 +244,6 @@ typedef struct rte { byte pflags; /* Protocol-specific flags */ btime lastmod; /* Last modified */ union { /* Protocol-dependent data (metrics etc.) */ -#ifdef CONFIG_OSPF - struct { - u32 metric1, metric2; /* OSPF Type 1 and Type 2 metrics */ - u32 tag; /* External route tag */ - u32 router_id; /* Router that originated this route */ - } ospf; -#endif #ifdef CONFIG_BGP struct { u8 suppressed; /* Used for deterministic MED comparison */ diff --git a/proto/ospf/ospf.c b/proto/ospf/ospf.c index 43b8f037..f9aa6cd1 100644 --- a/proto/ospf/ospf.c +++ b/proto/ospf/ospf.c @@ -108,11 +108,8 @@ #include "ospf.h" static int ospf_preexport(struct proto *P, rte *new); -static void ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool); -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); @@ -379,10 +376,7 @@ ospf_init(struct proto_config *CF) P->reload_routes = ospf_reload_routes; P->feed_begin = ospf_feed_begin; P->feed_end = ospf_feed_end; - P->make_tmp_attrs = ospf_make_tmp_attrs; - 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; @@ -392,7 +386,9 @@ ospf_init(struct proto_config *CF) static int ospf_rte_better(struct rte *new, struct rte *old) { - if (new->u.ospf.metric1 == LSINFINITY) + u32 new_metric1 = ea_get_int(new->attrs->eattrs, EA_OSPF_METRIC1, LSINFINITY); + + if (new_metric1 == LSINFINITY) return 0; if(new->attrs->source < old->attrs->source) return 1; @@ -400,31 +396,26 @@ ospf_rte_better(struct rte *new, struct rte *old) if(new->attrs->source == RTS_OSPF_EXT2) { - if(new->u.ospf.metric2 < old->u.ospf.metric2) return 1; - if(new->u.ospf.metric2 > old->u.ospf.metric2) return 0; + u32 old_metric2 = ea_get_int(old->attrs->eattrs, EA_OSPF_METRIC2, LSINFINITY); + u32 new_metric2 = ea_get_int(new->attrs->eattrs, EA_OSPF_METRIC2, LSINFINITY); + if(new_metric2 < old_metric2) return 1; + if(new_metric2 > old_metric2) return 0; } - if (new->u.ospf.metric1 < old->u.ospf.metric1) + u32 old_metric1 = ea_get_int(old->attrs->eattrs, EA_OSPF_METRIC1, LSINFINITY); + if (new_metric1 < old_metric1) return 1; return 0; /* Old is shorter or same */ } -static int -ospf_rte_same(struct rte *new, struct rte *old) -{ - /* new->attrs == old->attrs always */ - return - new->u.ospf.metric1 == old->u.ospf.metric1 && - new->u.ospf.metric2 == old->u.ospf.metric2 && - new->u.ospf.tag == old->u.ospf.tag && - new->u.ospf.router_id == old->u.ospf.router_id; -} - static u32 ospf_rte_igp_metric(struct rte *rt) { - return rt->u.ospf.metric1; + if (rt->attrs->source == RTS_OSPF_EXT2) + return IGP_METRIC_UNKNOWN; + + return ea_get_int(rt->attrs->eattrs, EA_OSPF_METRIC1, LSINFINITY); } void @@ -507,26 +498,6 @@ ospf_preexport(struct proto *P, rte *e) return 0; } -static void -ospf_make_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - rte_init_tmp_attrs(rt, pool, 4); - rte_make_tmp_attr(rt, EA_OSPF_METRIC1, EAF_TYPE_INT, rt->u.ospf.metric1); - rte_make_tmp_attr(rt, EA_OSPF_METRIC2, EAF_TYPE_INT, rt->u.ospf.metric2); - rte_make_tmp_attr(rt, EA_OSPF_TAG, EAF_TYPE_INT, rt->u.ospf.tag); - rte_make_tmp_attr(rt, EA_OSPF_ROUTER_ID, EAF_TYPE_ROUTER_ID, rt->u.ospf.router_id); -} - -static void -ospf_store_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - rte_init_tmp_attrs(rt, pool, 4); - rt->u.ospf.metric1 = rte_store_tmp_attr(rt, EA_OSPF_METRIC1); - rt->u.ospf.metric2 = rte_store_tmp_attr(rt, EA_OSPF_METRIC2); - rt->u.ospf.tag = rte_store_tmp_attr(rt, EA_OSPF_TAG); - rt->u.ospf.router_id = rte_store_tmp_attr(rt, EA_OSPF_ROUTER_ID); -} - /** * ospf_shutdown - Finish of OSPF instance * @P: OSPF protocol instance @@ -616,16 +587,20 @@ ospf_get_route_info(rte * rte, byte * buf) } buf += bsprintf(buf, " %s", type); - buf += bsprintf(buf, " (%d/%d", rte->attrs->pref, rte->u.ospf.metric1); + buf += bsprintf(buf, " (%d/%d", rte->attrs->pref, ea_get_int(rte->attrs->eattrs, EA_OSPF_METRIC1, LSINFINITY)); if (rte->attrs->source == RTS_OSPF_EXT2) - buf += bsprintf(buf, "/%d", rte->u.ospf.metric2); + buf += bsprintf(buf, "/%d", ea_get_int(rte->attrs->eattrs, EA_OSPF_METRIC2, LSINFINITY)); buf += bsprintf(buf, ")"); - if ((rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) && rte->u.ospf.tag) + if (rte->attrs->source == RTS_OSPF_EXT1 || rte->attrs->source == RTS_OSPF_EXT2) { - buf += bsprintf(buf, " [%x]", rte->u.ospf.tag); + eattr *ea = ea_find(rte->attrs->eattrs, EA_OSPF_TAG); + if (ea && (ea->u.data > 0)) + buf += bsprintf(buf, " [%x]", ea->u.data); } - if (rte->u.ospf.router_id) - buf += bsprintf(buf, " [%R]", rte->u.ospf.router_id); + + eattr *ea = ea_find(rte->attrs->eattrs, EA_OSPF_ROUTER_ID); + if (ea) + buf += bsprintf(buf, " [%R]", ea->u.data); } static int diff --git a/proto/ospf/rt.c b/proto/ospf/rt.c index cda464e0..91d16dd9 100644 --- a/proto/ospf/rt.c +++ b/proto/ospf/rt.c @@ -2062,23 +2062,45 @@ again1: if (reload || ort_changed(nf, &a0)) { - rta *a = rta_lookup(&a0); - rte *e = rte_get_temp(a, p->p.main_source); + a0.eattrs = alloca(sizeof(ea_list) + 4 * sizeof(eattr)); + memset(a0.eattrs, 0, sizeof(ea_list)); - rta_free(nf->old_rta); - nf->old_rta = rta_clone(a); - e->u.ospf.metric1 = nf->old_metric1 = nf->n.metric1; - e->u.ospf.metric2 = nf->old_metric2 = nf->n.metric2; - e->u.ospf.tag = nf->old_tag = nf->n.tag; - e->u.ospf.router_id = nf->old_rid = nf->n.rid; - e->pflags = EA_ID_FLAG(EA_OSPF_METRIC1) | EA_ID_FLAG(EA_OSPF_ROUTER_ID); + nf->old_metric1 = nf->n.metric1; + nf->old_metric2 = nf->n.metric2; + nf->old_tag = nf->n.tag; + nf->old_rid = nf->n.rid; + + a0.eattrs->attrs[a0.eattrs->count++] = (eattr) { + .id = EA_OSPF_METRIC1, + .type = EAF_TYPE_INT, + .u.data = nf->n.metric1, + }; if (nf->n.type == RTS_OSPF_EXT2) - e->pflags |= EA_ID_FLAG(EA_OSPF_METRIC2); + a0.eattrs->attrs[a0.eattrs->count++] = (eattr) { + .id = EA_OSPF_METRIC2, + .type = EAF_TYPE_INT, + .u.data = nf->n.metric2, + }; - /* Perhaps onfly if tag is non-zero? */ if ((nf->n.type == RTS_OSPF_EXT1) || (nf->n.type == RTS_OSPF_EXT2)) - e->pflags |= EA_ID_FLAG(EA_OSPF_TAG); + a0.eattrs->attrs[a0.eattrs->count++] = (eattr) { + .id = EA_OSPF_TAG, + .type = EAF_TYPE_INT, + .u.data = nf->n.tag, + }; + + a0.eattrs->attrs[a0.eattrs->count++] = (eattr) { + .id = EA_OSPF_ROUTER_ID, + .type = EAF_TYPE_ROUTER_ID, + .u.data = nf->n.rid, + }; + + rta *a = rta_lookup(&a0); + rte *e = rte_get_temp(a, p->p.main_source); + + rta_free(nf->old_rta); + nf->old_rta = rta_clone(a); DBG("Mod rte type %d - %N via %I on iface %s, met %d\n", a0.source, nf->fn.addr, a0.gw, a0.iface ? a0.iface->name : "(none)", nf->n.metric1); -- cgit v1.2.3 From c507fb41bb2e62f336357121598debab95a7c767 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 13 Feb 2020 13:22:15 +0100 Subject: Babel: Convert the rte-local attributes to extended attributes --- nest/route.h | 7 ---- proto/babel/babel.c | 94 +++++++++++++++++++++++++++++------------------------ proto/babel/babel.h | 1 + 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/nest/route.h b/nest/route.h index 1ed40d68..531a4ca8 100644 --- a/nest/route.h +++ b/nest/route.h @@ -249,13 +249,6 @@ typedef struct rte { u8 suppressed; /* Used for deterministic MED comparison */ s8 stale; /* Route is LLGR_STALE, -1 if unknown */ } bgp; -#endif -#ifdef CONFIG_BABEL - struct { - u16 seqno; /* Babel seqno */ - u16 metric; /* Babel metric */ - u64 router_id; /* Babel router id */ - } babel; #endif struct { /* Routes generated by krt sync (both temporary and inherited ones) */ s8 src; /* Alleged route source (see krt.h) */ diff --git a/proto/babel/babel.c b/proto/babel/babel.c index d17f318b..07213c8c 100644 --- a/proto/babel/babel.c +++ b/proto/babel/babel.c @@ -648,6 +648,29 @@ babel_announce_rte(struct babel_proto *p, struct babel_entry *e) .from = r->neigh->addr, .nh.gw = r->next_hop, .nh.iface = r->neigh->ifa->iface, + .eattrs = alloca(sizeof(ea_list) + 3*sizeof(eattr)), + }; + + *a0.eattrs = (ea_list) { .count = 3 }; + a0.eattrs->attrs[0] = (eattr) { + .id = EA_BABEL_METRIC, + .type = EAF_TYPE_INT, + .u.data = r->metric, + }; + + struct adata *ad = alloca(sizeof(struct adata) + sizeof(u64)); + ad->length = sizeof(u64); + memcpy(ad->data, &(r->router_id), sizeof(u64)); + a0.eattrs->attrs[1] = (eattr) { + .id = EA_BABEL_ROUTER_ID, + .type = EAF_TYPE_OPAQUE, + .u.ptr = ad, + }; + + a0.eattrs->attrs[2] = (eattr) { + .id = EA_BABEL_SEQNO, + .type = EAF_TYPE_INT, + .u.data = r->seqno, }; /* @@ -660,10 +683,6 @@ babel_announce_rte(struct babel_proto *p, struct babel_entry *e) rta *a = rta_lookup(&a0); rte *rte = rte_get_temp(a, p->p.main_source); - rte->u.babel.seqno = r->seqno; - rte->u.babel.metric = r->metric; - rte->u.babel.router_id = r->router_id; - rte->pflags = EA_ID_FLAG(EA_BABEL_METRIC) | EA_ID_FLAG(EA_BABEL_ROUTER_ID); e->unreachable = 0; rte_update2(c, e->n.addr, rte, p->p.main_source); @@ -680,7 +699,6 @@ babel_announce_rte(struct babel_proto *p, struct babel_entry *e) rta *a = rta_lookup(&a0); rte *rte = rte_get_temp(a, p->p.main_source); - memset(&rte->u.babel, 0, sizeof(rte->u.babel)); rte->pflags = 0; e->unreachable = 1; @@ -2009,7 +2027,13 @@ babel_dump(struct proto *P) static void babel_get_route_info(rte *rte, byte *buf) { - buf += bsprintf(buf, " (%d/%d) [%lR]", rte->attrs->pref, rte->u.babel.metric, rte->u.babel.router_id); + u64 rid = 0; + eattr *e = ea_find(rte->attrs->eattrs, EA_BABEL_ROUTER_ID); + if (e) + memcpy(&rid, e->u.ptr->data, sizeof(u64)); + + buf += bsprintf(buf, " (%d/%d) [%lR]", rte->attrs->pref, + ea_get_int(rte->attrs->eattrs, EA_BABEL_METRIC, BABEL_INFINITY), rid); } static int @@ -2017,6 +2041,9 @@ babel_get_attr(const eattr *a, byte *buf, int buflen UNUSED) { switch (a->id) { + case EA_BABEL_SEQNO: + return GA_FULL; + case EA_BABEL_METRIC: bsprintf(buf, "metric: %d", a->u.data); return GA_FULL; @@ -2240,27 +2267,6 @@ babel_preexport(struct proto *P, struct rte *new) return 0; } -static void -babel_make_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - struct adata *id = lp_alloc_adata(pool, sizeof(u64)); - memcpy(id->data, &rt->u.babel.router_id, sizeof(u64)); - - rte_init_tmp_attrs(rt, pool, 2); - rte_make_tmp_attr(rt, EA_BABEL_METRIC, EAF_TYPE_INT, rt->u.babel.metric); - rte_make_tmp_attr(rt, EA_BABEL_ROUTER_ID, EAF_TYPE_OPAQUE, (uintptr_t) id); -} - -static void -babel_store_tmp_attrs(struct rte *rt, struct linpool *pool) -{ - rte_init_tmp_attrs(rt, pool, 2); - rt->u.babel.metric = rte_store_tmp_attr(rt, EA_BABEL_METRIC); - - /* EA_BABEL_ROUTER_ID is read-only, we do not really save the value */ - rte_store_tmp_attr(rt, EA_BABEL_ROUTER_ID); -} - /* * babel_rt_notify - core tells us about new route (possibly our own), * so store it into our data structures. @@ -2275,10 +2281,22 @@ babel_rt_notify(struct proto *P, struct channel *c UNUSED, struct network *net, if (new) { /* Update */ - uint internal = (new->src->proto == P); - uint rt_seqno = internal ? new->u.babel.seqno : p->update_seqno; + uint rt_seqno; uint rt_metric = ea_get_int(new->attrs->eattrs, EA_BABEL_METRIC, 0); - u64 rt_router_id = internal ? new->u.babel.router_id : p->router_id; + u64 rt_router_id = 0; + + if (new->src->proto == P) + { + rt_seqno = ea_find(new->attrs->eattrs, EA_BABEL_SEQNO)->u.data; + eattr *e = ea_find(new->attrs->eattrs, EA_BABEL_ROUTER_ID); + if (e) + memcpy(&rt_router_id, e->u.ptr->data, sizeof(u64)); + } + else + { + rt_seqno = p->update_seqno; + rt_router_id = p->router_id; + } if (rt_metric > BABEL_INFINITY) { @@ -2321,21 +2339,16 @@ babel_rt_notify(struct proto *P, struct channel *c UNUSED, struct network *net, static int babel_rte_better(struct rte *new, struct rte *old) { - return new->u.babel.metric < old->u.babel.metric; -} + uint new_metric = ea_find(new->attrs->eattrs, EA_BABEL_SEQNO)->u.data; + uint old_metric = ea_find(old->attrs->eattrs, EA_BABEL_SEQNO)->u.data; -static int -babel_rte_same(struct rte *new, struct rte *old) -{ - return ((new->u.babel.seqno == old->u.babel.seqno) && - (new->u.babel.metric == old->u.babel.metric) && - (new->u.babel.router_id == old->u.babel.router_id)); + return new_metric < old_metric; } static u32 babel_rte_igp_metric(struct rte *rt) { - return rt->u.babel.metric; + return ea_get_int(rt->attrs->eattrs, EA_BABEL_METRIC, BABEL_INFINITY); } @@ -2369,10 +2382,7 @@ babel_init(struct proto_config *CF) P->if_notify = babel_if_notify; P->rt_notify = babel_rt_notify; P->preexport = babel_preexport; - P->make_tmp_attrs = babel_make_tmp_attrs; - 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; diff --git a/proto/babel/babel.h b/proto/babel/babel.h index 84feb085..8b6da3c8 100644 --- a/proto/babel/babel.h +++ b/proto/babel/babel.h @@ -28,6 +28,7 @@ #define EA_BABEL_METRIC EA_CODE(PROTOCOL_BABEL, 0) #define EA_BABEL_ROUTER_ID EA_CODE(PROTOCOL_BABEL, 1) +#define EA_BABEL_SEQNO EA_CODE(PROTOCOL_BABEL, 2) #define BABEL_MAGIC 42 #define BABEL_VERSION 2 -- cgit v1.2.3 From 3660f19dd534224da4870a507efcef5b36794506 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Mon, 17 Feb 2020 11:18:32 +0100 Subject: Dropping the RTS_DUMMY temporary route storage. Kernel route sync is done by other ways now and this code is not used currently. --- doc/bird.sgml | 2 +- filter/test.conf | 1 - nest/config.Y | 2 +- nest/route.h | 1 - nest/rt-attr.c | 3 +-- nest/rt-table.c | 23 ----------------------- proto/ospf/rt.c | 6 +++--- sysdep/linux/netlink.c | 2 -- 8 files changed, 6 insertions(+), 34 deletions(-) diff --git a/doc/bird.sgml b/doc/bird.sgml index ddad4d98..a2138b55 100644 --- a/doc/bird.sgml +++ b/doc/bird.sgml @@ -1691,7 +1691,7 @@ Common route attributes are: