Noticed that similar to fib_prio the rdomain id is passed around to some
places where it really makes no sense.
First of all kr_nexthop_add() and kr_nexthop_delete() can be simplified.
The imsg peerid is always 0. This was added in the beginning of L3VPN
support without realizing that all bgp nexthops live in the same rdomain.
So simplify this code path.
Then kr_shutdown() should just nuke all tables, passing one for good
measures makes again little sense. Similar reason for kr_dispatch_msg().
Now all those function had this extra variable because of kif_remove().
kif_remove needs to know the ktable where the connected routes live in.
Now this is simply the rdomain id of the interface. No other table should
have a connected route to this interface.
Nexthop lookups need more work but this should work well enough for now.
--
:wq Claudio
Index: bgpd.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/bgpd.c,v
retrieving revision 1.244
diff -u -p -r1.244 bgpd.c
--- bgpd.c 5 Jun 2022 12:43:13 -0000 1.244
+++ bgpd.c 9 Jun 2022 14:18:49 -0000
@@ -388,7 +388,7 @@ BROKEN if (pledge("stdio rpath wpath cpa
}
if (pfd[PFD_SOCK_ROUTE].revents & POLLIN) {
- if (kr_dispatch_msg(conf->default_tableid) == -1)
+ if (kr_dispatch_msg() == -1)
quit = 1;
}
@@ -460,7 +460,7 @@ BROKEN if (pledge("stdio rpath wpath cpa
/* cleanup kernel data structures */
carp_demote_shutdown();
- kr_shutdown(conf->default_tableid);
+ kr_shutdown();
pftable_clear_all();
RB_FOREACH(p, peer_head, &conf->peers)
@@ -800,6 +800,7 @@ dispatch_imsg(struct imsgbuf *ibuf, int
struct peer *p;
struct rtr_config *r;
ssize_t n;
+ u_int rtableid;
int rv, verbose;
rv = 0;
@@ -843,9 +844,11 @@ dispatch_imsg(struct imsgbuf *ibuf, int
else if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct bgpd_addr))
log_warnx("wrong imsg len");
- else if (kr_nexthop_add(imsg.hdr.peerid, imsg.data,
- conf) == -1)
- rv = -1;
+ else {
+ rtableid = conf->default_tableid;
+ if (kr_nexthop_add(rtableid, imsg.data) == -1)
+ rv = -1;
+ }
break;
case IMSG_NEXTHOP_REMOVE:
if (idx != PFD_PIPE_RDE)
@@ -853,9 +856,10 @@ dispatch_imsg(struct imsgbuf *ibuf, int
else if (imsg.hdr.len != IMSG_HEADER_SIZE +
sizeof(struct bgpd_addr))
log_warnx("wrong imsg len");
- else
- kr_nexthop_delete(imsg.hdr.peerid, imsg.data,
- conf);
+ else {
+ rtableid = conf->default_tableid;
+ kr_nexthop_delete(rtableid, imsg.data);
+ }
break;
case IMSG_PFTABLE_ADD:
if (idx != PFD_PIPE_RDE)
Index: bgpd.h
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/bgpd.h,v
retrieving revision 1.427
diff -u -p -r1.427 bgpd.h
--- bgpd.h 7 Jun 2022 15:57:47 -0000 1.427
+++ bgpd.h 9 Jun 2022 14:06:04 -0000
@@ -1291,17 +1291,15 @@ int ktable_exists(u_int, u_int *);
int kr_change(u_int, struct kroute_full *);
int kr_delete(u_int, struct kroute_full *);
int kr_flush(u_int);
-void kr_shutdown(u_int);
+void kr_shutdown(void);
void kr_fib_couple(u_int);
void kr_fib_couple_all(void);
void kr_fib_decouple(u_int);
void kr_fib_decouple_all(void);
void kr_fib_prio_set(uint8_t);
-int kr_dispatch_msg(u_int rdomain);
-int kr_nexthop_add(uint32_t, struct bgpd_addr *,
- struct bgpd_config *);
-void kr_nexthop_delete(uint32_t, struct bgpd_addr *,
- struct bgpd_config *);
+int kr_dispatch_msg(void);
+int kr_nexthop_add(uint32_t, struct bgpd_addr *);
+void kr_nexthop_delete(uint32_t, struct bgpd_addr *);
void kr_show_route(struct imsg *);
void kr_ifinfo(char *);
void kr_net_reload(u_int, uint64_t, struct network_head *);
Index: kroute.c
===================================================================
RCS file: /cvs/src/usr.sbin/bgpd/kroute.c,v
retrieving revision 1.251
diff -u -p -r1.251 kroute.c
--- kroute.c 7 Jun 2022 16:42:07 -0000 1.251
+++ kroute.c 9 Jun 2022 14:17:20 -0000
@@ -152,8 +152,8 @@ void knexthop_clear(struct ktable
*);
struct kif_node *kif_find(int);
int kif_insert(struct kif_node *);
-int kif_remove(struct kif_node *, u_int);
-void kif_clear(u_int);
+int kif_remove(struct kif_node *);
+void kif_clear(void);
int kif_kr_insert(struct kroute_node *);
int kif_kr_remove(struct kroute_node *);
@@ -180,12 +180,12 @@ uint64_t ift2ifm(uint8_t);
const char *get_media_descr(uint64_t);
const char *get_linkstate(uint8_t, int);
void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
-void if_change(u_short, int, struct if_data *, u_int);
-void if_announce(void *, u_int);
+void if_change(u_short, int, struct if_data *);
+void if_announce(void *);
int send_rtmsg(int, int, struct ktable *, struct kroute *);
int send_rt6msg(int, int, struct ktable *, struct kroute6 *);
-int dispatch_rtmsg(u_int);
+int dispatch_rtmsg(void);
int fetchtable(struct ktable *);
int fetchifs(int);
int dispatch_rtmsg_addr(struct rt_msghdr *,
@@ -882,13 +882,13 @@ krVPN6_delete(struct ktable *kt, struct
}
void
-kr_shutdown(u_int rdomain)
+kr_shutdown(void)
{
u_int i;
for (i = krt_size; i > 0; i--)
ktable_free(i - 1);
- kif_clear(rdomain);
+ kif_clear();
free(krt);
}
@@ -969,20 +969,17 @@ kr_fib_prio_set(uint8_t prio)
}
int
-kr_dispatch_msg(u_int rdomain)
+kr_dispatch_msg(void)
{
- return (dispatch_rtmsg(rdomain));
+ return (dispatch_rtmsg());
}
int
-kr_nexthop_add(u_int rtableid, struct bgpd_addr *addr, struct bgpd_config
*conf)
+kr_nexthop_add(u_int rtableid, struct bgpd_addr *addr)
{
struct ktable *kt;
struct knexthop_node *h;
- if (rtableid == 0)
- rtableid = conf->default_tableid;
-
if ((kt = ktable_get(rtableid)) == NULL) {
log_warnx("%s: non-existent rtableid %d", __func__, rtableid);
return (0);
@@ -1005,15 +1002,11 @@ kr_nexthop_add(u_int rtableid, struct bg
}
void
-kr_nexthop_delete(u_int rtableid, struct bgpd_addr *addr,
- struct bgpd_config *conf)
+kr_nexthop_delete(u_int rtableid, struct bgpd_addr *addr)
{
struct ktable *kt;
struct knexthop_node *kn;
- if (rtableid == 0)
- rtableid = conf->default_tableid;
-
if ((kt = ktable_get(rtableid)) == NULL) {
log_warnx("%s: non-existent rtableid %d", __func__,
rtableid);
@@ -2140,7 +2133,7 @@ kif_insert(struct kif_node *kif)
}
int
-kif_remove(struct kif_node *kif, u_int rdomain)
+kif_remove(struct kif_node *kif)
{
struct ktable *kt;
struct kif_kr *kkr;
@@ -2151,7 +2144,7 @@ kif_remove(struct kif_node *kif, u_int r
return (-1);
}
- if ((kt = ktable_get(rdomain)) == NULL)
+ if ((kt = ktable_get(kif->k.rdomain)) == NULL)
goto done;
while ((kkr = LIST_FIRST(&kif->kroute_l)) != NULL) {
@@ -2173,12 +2166,12 @@ done:
}
void
-kif_clear(u_int rdomain)
+kif_clear(void)
{
struct kif_node *kif;
while ((kif = RB_MIN(kif_tree, &kit)) != NULL)
- kif_remove(kif, rdomain);
+ kif_remove(kif);
}
int
@@ -2766,8 +2759,7 @@ get_rtaddrs(int addrs, struct sockaddr *
}
void
-if_change(u_short ifindex, int flags, struct if_data *ifd,
- u_int rdomain)
+if_change(u_short ifindex, int flags, struct if_data *ifd)
{
struct ktable *kt;
struct kif_node *kif;
@@ -2802,7 +2794,7 @@ if_change(u_short ifindex, int flags, st
kif->k.nh_reachable = reachable;
- kt = ktable_get(rdomain);
+ kt = ktable_get(ifd->ifi_rdomain);
LIST_FOREACH(kkr, &kif->kroute_l, entry) {
if (reachable)
@@ -2829,7 +2821,7 @@ if_change(u_short ifindex, int flags, st
}
void
-if_announce(void *msg, u_int rdomain)
+if_announce(void *msg)
{
struct if_announcemsghdr *ifan;
struct kif_node *kif;
@@ -2849,7 +2841,7 @@ if_announce(void *msg, u_int rdomain)
break;
case IFAN_DEPARTURE:
kif = kif_find(ifan->ifan_index);
- kif_remove(kif, rdomain);
+ kif_remove(kif);
break;
}
}
@@ -3447,7 +3439,7 @@ fetchifs(int ifindex)
}
int
-dispatch_rtmsg(u_int rdomain)
+dispatch_rtmsg(void)
{
char buf[RT_BUF_SIZE];
ssize_t n;
@@ -3502,11 +3494,10 @@ dispatch_rtmsg(u_int rdomain)
break;
case RTM_IFINFO:
memcpy(&ifm, next, sizeof(ifm));
- if_change(ifm.ifm_index, ifm.ifm_flags,
- &ifm.ifm_data, rdomain);
+ if_change(ifm.ifm_index, ifm.ifm_flags, &ifm.ifm_data);
break;
case RTM_IFANNOUNCE:
- if_announce(next, rdomain);
+ if_announce(next);
break;
default:
/* ignore for now */