From 6c8e05993502f8e077cdbda3bea2f292268e04d8 Mon Sep 17 00:00:00 2001 From: Maria Matejka Date: Thu, 19 Sep 2024 11:26:32 +0200 Subject: [PATCH] more minor fices --- proto/bgp/bgp.h | 3 +- proto/bgp/packets.c | 18 +++--- proto/bmp/bmp.c | 150 +++++++------------------------------------- 3 files changed, 33 insertions(+), 138 deletions(-) diff --git a/proto/bgp/bgp.h b/proto/bgp/bgp.h index 0c31a3a7..fe7afa9b 100644 --- a/proto/bgp/bgp.h +++ b/proto/bgp/bgp.h @@ -698,8 +698,7 @@ static inline struct bgp_proto *bgp_rte_proto(const rte *rte) SKIP_BACK(struct bgp_proto, p.sources, rte->src->owner) : NULL; } -byte * bgp_bmp_encode_rte(ea_list *c, struct bgp_proto *bgp_p, byte *buf, const net_addr *n, - const struct rte *new, const struct rte_src *src); +byte * bgp_bmp_encode_rte(ea_list *c, struct bgp_proto *bgp_p, byte *buf, const struct rte *new); #define BGP_AIGP_METRIC 1 #define BGP_AIGP_MAX U64(0xffffffffffffffff) diff --git a/proto/bgp/packets.c b/proto/bgp/packets.c index 7a2c4c94..8d02f8f6 100644 --- a/proto/bgp/packets.c +++ b/proto/bgp/packets.c @@ -2526,15 +2526,13 @@ bgp_bmp_prepare_bgp_hdr(byte *buf, const u16 msg_size, const u8 msg_type) } byte * -bgp_bmp_encode_rte(ea_list *c, struct bgp_proto *bgp_p, byte *buf, const net_addr *n, - const struct rte *new, const struct rte_src *src) +bgp_bmp_encode_rte(ea_list *c, struct bgp_proto *bgp_p, byte *buf, const struct rte *new) { // struct bgp_proto *p = (void *) c->c.proto; byte *pkt = buf + BGP_HEADER_LENGTH; - ea_list *attrs = new ? new->attrs : NULL; - uint ea_size = new ? (sizeof(ea_list) + attrs->count * sizeof(eattr)) : 0; - uint prefix_size = sizeof(struct bgp_prefix) + n->length; + uint ea_size = new->attrs ? (sizeof(ea_list) + new->attrs->count * sizeof(eattr)) : 0; + uint prefix_size = sizeof(struct bgp_prefix) + new->net->length; struct lp_state *tmpp = lp_save(tmp_linpool); @@ -2543,17 +2541,17 @@ bgp_bmp_encode_rte(ea_list *c, struct bgp_proto *bgp_p, byte *buf, const net_add b->bmp = 1; init_list(&b->prefixes); - if (attrs) - memcpy(b->eattrs, attrs, ea_size); + if (new->attrs) + memcpy(b->eattrs, new->attrs, ea_size); /* Temporary prefix */ struct bgp_prefix *px = tmp_allocz(prefix_size); px->src = tmp_allocz(sizeof(struct rte_src)); - memcpy(px->src, src, sizeof(struct rte_src)); - px->ni = NET_TO_INDEX(n); + memcpy(px->src, new->src, sizeof(struct rte_src)); + px->ni = NET_TO_INDEX(new->net); add_tail(&b->prefixes, &px->buck_node); // why was there _xx ? - byte *end = bgp_create_update_bmp(c, bgp_p, pkt, b, !!new); + byte *end = bgp_create_update_bmp(c, bgp_p, pkt, b, !!new->attrs); if (end) bgp_bmp_prepare_bgp_hdr(buf, end - buf, PKT_UPDATE); diff --git a/proto/bmp/bmp.c b/proto/bmp/bmp.c index f143203f..f36dccd5 100644 --- a/proto/bmp/bmp.c +++ b/proto/bmp/bmp.c @@ -218,7 +218,7 @@ static void bmp_connected(struct birdsock *sk); static void bmp_sock_err(sock *sk, int err); static void bmp_close_socket(struct bmp_proto *p); static void bmp_check_routes(void *bt_); -static void bmp_do_pre_policy(void *bt_); +static void bmp_feed_end(struct rt_export_request *req); static void bmp_send_peer_up_notif_msg(struct bmp_proto *p, ea_list *bgp, @@ -509,33 +509,6 @@ const struct channel_class channel_bmp = { static struct bmp_table * bmp_add_table(struct bmp_proto *p, rtable *tab) -{ - struct bmp_table *bt = mb_allocz(p->p.pool, sizeof(struct bmp_table)); - log("adding table %x", bt); - bt->table = tab; - rt_lock_table(bt->table); - - HASH_INSERT(p->table_map, HASH_TABLE, bt); - - struct channel_config cc = { - .name = "monitor", - //.channel = &channel_basic, - .table = tab->config, - .in_filter = FILTER_REJECT, - .net_type = tab->addr_type, - .ra_mode = RA_ANY, - //.bmp_hack = 1, - .class = &channel_bmp, - }; - - bt->channel = proto_add_channel(&p->p, &cc); - channel_set_state(bt->channel, CS_UP); - - return bt; -} - -static struct bmp_table * -bmp_add_table_exp_req(struct bmp_proto *p, rtable *tab) { struct bmp_table *bt = mb_allocz(p->p.pool, sizeof(struct bmp_table)); log("adding table %x", bt); @@ -557,13 +530,9 @@ bmp_add_table_exp_req(struct bmp_proto *p, rtable *tab) .event = &bt->event, }, .pool = p->p.pool, - //.feeder.prefilter = { - // .mode = c->out_subprefix ? TE_ADDR_IN : TE_ADDR_NONE, - // .addr = c->out_subprefix, - //}, .trace_routes = p->p.debug, - //.dump = channel_dump_export_req, - //.fed = channel_export_fed, + //.dump = channel_dump_export_req, TODO: this will crash on `dump tables` from CLI + .fed = bmp_feed_end, }; rt_export_subscribe(tab, all, &bt->out_req);//} return bt; @@ -622,7 +591,7 @@ struct bmp_table *bmp_get_table(struct bmp_proto *p, rtable *tab) atomic_int i = bt->uc; if (i == 0) { - struct bmp_table *new = bmp_add_table_exp_req(p, tab); + struct bmp_table *new = bmp_add_table(p, tab); bmp_lock_table(p, new); return new; } @@ -630,7 +599,7 @@ struct bmp_table *bmp_get_table(struct bmp_proto *p, rtable *tab) return bt; } } - struct bmp_table *new = bmp_add_table_exp_req(p, tab); + struct bmp_table *new = bmp_add_table(p, tab); bmp_lock_table(p, new); return new; } @@ -967,20 +936,20 @@ bmp_route_monitor_put_update(struct bmp_proto *p, struct bmp_stream *bs, const b } static void -bmp_route_monitor_notify(struct bmp_proto *p, struct bgp_proto *bgp_p, struct bmp_stream *bs, - const net_addr *n, const struct rte *new, const struct rte_src *src) +bmp_route_monitor_notify(struct bmp_proto *p, struct bgp_proto *bgp_p, struct bmp_stream *bs, const struct rte *new) { log("notified"); + bmp_route_monitor_end_of_rib(p, bs); byte buf[BGP_MAX_EXT_MSG_LENGTH]; - byte *end = bgp_bmp_encode_rte(bs->sender, bgp_p, buf, n, new, src); + byte *end = bgp_bmp_encode_rte(bs->sender, bgp_p, buf, new); - btime delta_t = new ? current_time() - new->lastmod : 0; + btime delta_t = new->attrs ? current_time() - new->lastmod : 0; btime timestamp = current_real_time() - delta_t; if (end) bmp_route_monitor_put_update(p, bs, buf, end - buf, timestamp); else - log(L_WARN "%s: Cannot encode update for %N", p->p.name, n); + log(L_WARN "%s: Cannot encode update for %N", p->p.name, new->net); } static void @@ -1172,72 +1141,6 @@ bmp_preexport(struct channel *C UNUSED, rte *e) return 1; } -static void -bmp_rt_notify(struct proto *P, struct channel *c, const net_addr *net, - struct rte *new, const struct rte *old) -{ - return; //TODO The next code is complete mess. It counts with both v2 and v3 functions, does respect only some changes in structs (and retype v3 structs with (void *) just because it was possible in v2), contains forgotten hacks how to make it compilable, uses journal eatters where they (at first or second glance) looked useful and do strange things in general. - struct bmp_proto *p = SKIP_BACK(struct bmp_proto, p, P); - - struct bgp_channel *src = SKIP_BACK(struct bgp_channel, c.in_req, (new ?: old)->sender->req); - struct bgp_proto *bgp = SKIP_BACK(struct bgp_proto, p, src->c.proto); - bool policy = (c->table == src->c.table); - - /* - * We assume that we receive peer_up before the first route and peer_down - * synchronously with BGP session close. So if bmp_stream exists, the related - * BGP session is up and could be accessed. That may not be true in - * multithreaded setup. - */ - - struct bmp_stream *bs = bmp_find_stream(p, bgp, src->afi, policy); - if (!bs) - { - log("bmp stream not found in p %i", p); - return; - } - - log("bmp stream found"); - bmp_route_monitor_notify(p, bgp, bs, net, new, (new ?: old)->src); -} - - -/*static void -bmp_do_pre_policy(void *bt_) -{ - log("do pre policy"); - struct bmp_table *bt = (struct bmp_table *)bt_; - struct bmp_proto *p = bt->p; - - RT_FEED_WALK(&bt->in_req, f) - { - bool seen = 0; - log("count rout %i", f->count_routes); - for (uint i = 0; i < f->count_routes; i++) - { - rte *r = &f->block[i]; - - if (r->flags & REF_OBSOLETE) - break; - - rte new = rte_init_from(r); - - // Strip the later attribute layers - new.attrs = ea_strip_to(new.attrs, BIT32_ALL(EALS_PREIMPORT)); - struct proto *rte_proto = (struct proto*) SKIP_BACK(struct proto, sources, new.src->owner); - if (rte_proto->proto != &proto_bgp || !bgp_next_hop_present(new)) - break; - struct bgp_proto *bgp = (struct bgp_proto *) rte_proto; - struct bgp_channel *bc = (struct bgp_channel *) SKIP_BACK(struct channel, in_req, new.sender->req); - struct bgp_channel *src = SKIP_BACK(struct bgp_channel, c.in_req, new.sender->req); - bool policy = (bc->c.table == src->c.table); //TODO? - struct bmp_stream *bs = bmp_find_stream(p, bgp, src->afi, policy); - - bmp_route_monitor_notify(p, bgp, bs, new.net, &new, new.src); - } - } -}*/ - int bgp_next_hop_present(const rte *n) { @@ -1256,16 +1159,15 @@ bgp_next_hop_present(const rte *n) static void solve_for_post_and_pre(struct bmp_proto *p, const rte *new, const rte *old) { - struct proto *rte_proto = (struct proto*) SKIP_BACK(struct proto, sources, new->src->owner); + rte loc = *(new ?: old); + + struct proto *rte_proto = (struct proto*) SKIP_BACK(struct proto, sources, loc.src->owner); struct bgp_proto *bgp = (struct bgp_proto *) rte_proto; - struct bgp_channel *src_ch = SKIP_BACK(struct bgp_channel, c.in_req, new->sender->req); + struct bgp_channel *src_ch = SKIP_BACK(struct bgp_channel, c.in_req, loc.sender->req); ASSERT_DIE(src_ch->c.proto == rte_proto); /* No pipes supported for now */ log("solve_for_post_and_pre %s", rte_proto->name); - const net_addr *net = (new ?: old)->net; - struct rte_src *src = (new ?: old)->src; - /* Ignore non-BGP routes */ if (rte_proto->proto != &proto_bgp) return; @@ -1276,6 +1178,8 @@ solve_for_post_and_pre(struct bmp_proto *p, const rte *new, const rte *old) /* Compute the pre policy attributes */ ea_list *new_attrs = new ? ea_strip_to(new->attrs, BIT32_ALL(EALS_PREIMPORT)) : NULL; ea_list *old_attrs = old ? ea_strip_to(old->attrs, BIT32_ALL(EALS_PREIMPORT)) : NULL; + + loc.attrs = new_attrs; if (new_attrs != old_attrs) { @@ -1290,7 +1194,7 @@ solve_for_post_and_pre(struct bmp_proto *p, const rte *new, const rte *old) if (bmp_find_peer(p, proto_state_table->attrs[bgp->p.id]) == NULL) bug("not implemented"); - bmp_route_monitor_notify(p, bgp, bs, net, new, src); + bmp_route_monitor_notify(p, bgp, bs, &loc); } } } @@ -1302,6 +1206,8 @@ solve_for_post_and_pre(struct bmp_proto *p, const rte *new, const rte *old) ea_list *new_attrs = new ? ea_normalize(new->attrs, 0) : NULL; ea_list *old_attrs = old ? ea_normalize(old->attrs, 0) : NULL; + loc.attrs = new_attrs; + /* TODO: filter only BGP-relevant attributes */ if ((new_attrs != old_attrs) || ea_same(new_attrs, old_attrs)) @@ -1314,7 +1220,7 @@ solve_for_post_and_pre(struct bmp_proto *p, const rte *new, const rte *old) if (bmp_find_peer(p, proto_state_table->attrs[bgp->p.id]) == NULL) bug("not implemented"); - bmp_route_monitor_notify(p, bgp, bs, net, new, src); + bmp_route_monitor_notify(p, bgp, bs, &loc); } } } @@ -1355,22 +1261,18 @@ bmp_check_routes(void *bt_) log("export update"); solve_for_post_and_pre(p, u->update->new, u->update->old); + break; } } log("end of notify fce"); } static void -bmp_feed_end(struct channel *c) +bmp_feed_end(struct rt_export_request *req) { - struct bmp_proto *p = (void *) c->proto; + SKIP_BACK_DECLARE(struct bmp_table, bt, out_req, req); - struct bmp_table *bt = bmp_find_table(p, c->table); - if (!bt) - { - log("bmp table not found"); - return; - } + struct bmp_proto *p = bt->p; log("bmp table found"); /* @@ -1648,10 +1550,6 @@ bmp_init(struct proto_config *CF) if (!bgp_next_hop_ea_class) bgp_next_hop_ea_class = ea_class_find_by_name("bgp_next_hop"); - P->rt_notify = bmp_rt_notify; - P->preexport = bmp_preexport; - //P->feed_end = bmp_feed_end; I am confused. It looks like feed_end and bmp_feed_end exist only here - p->cf = cf; p->local_addr = cf->local_addr; p->station_ip = cf->station_ip;