Re: ospfd: fib-priority

2018-12-28 Thread Claudio Jeker
On Fri, Dec 28, 2018 at 02:32:54PM +0100, Remi Locherer wrote:
> ping

OK claudio@
 
> On Mon, Dec 10, 2018 at 10:40:22AM +0100, Remi Locherer wrote:
> > Hi,
> > 
> > below patch adds "fib-priority" to ospfd.conf which allows to set a
> > custom priority to routes. 32 is still the default if not set. Changing
> > the priority with a reload is also supported.
> > 
> > A discussion about the feature can be found here:
> > https://marc.info/?l=openbsd-tech=138360663119816=2
> > 
> > My first idea was to add an additional parameter to the functions that
> > need it. But that that is not practical since then need the event that calls
> > kr_dispatch_msg() needs to be reset. Because of that I added fib_prio to 
> > struct kr_state.
> > 
> > 
> > OK?
> > 
> > Remi
> > 
> > 
> > 
> > cvs diff: Diffing .
> > Index: kroute.c
> > ===
> > RCS file: /cvs/src/usr.sbin/ospfd/kroute.c,v
> > retrieving revision 1.111
> > diff -u -p -r1.111 kroute.c
> > --- kroute.c10 Jul 2018 11:49:04 -  1.111
> > +++ kroute.c9 Dec 2018 21:39:46 -
> > @@ -45,6 +45,7 @@ struct {
> > pid_t   pid;
> > int fib_sync;
> > int fib_serial;
> > +   u_int8_tfib_prio;
> > int fd;
> > struct eventev;
> > struct eventreload;
> > @@ -127,14 +128,15 @@ kif_init(void)
> >  }
> >  
> >  int
> > -kr_init(int fs, u_int rdomain, int redis_label_or_prefix)
> > +kr_init(int fs, u_int rdomain, int redis_label_or_prefix, u_int8_t 
> > fib_prio)
> >  {
> > int opt = 0, rcvbuf, default_rcvbuf;
> > socklen_t   optlen;
> > -   int filter_prio = RTP_OSPF;
> > +   int filter_prio = fib_prio;
> >  
> > kr_state.fib_sync = fs;
> > kr_state.rdomain = rdomain;
> > +   kr_state.fib_prio = fib_prio;
> >  
> > if ((kr_state.fd = socket(AF_ROUTE,
> > SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, AF_INET)) == -1) {
> > @@ -262,7 +264,7 @@ kr_change_fib(struct kroute_node *kr, st
> > kn->r.prefixlen = kroute[i].prefixlen;
> > kn->r.nexthop.s_addr = kroute[i].nexthop.s_addr;
> > kn->r.flags = kroute[i].flags | F_OSPFD_INSERTED;
> > -   kn->r.priority = RTP_OSPF;
> > +   kn->r.priority = kr_state.fib_prio;
> > kn->r.ext_tag = kroute[i].ext_tag;
> > rtlabel_unref(kn->r.rtlabel);   /* for RTM_CHANGE */
> > kn->r.rtlabel = kroute[i].rtlabel;
> > @@ -286,7 +288,8 @@ kr_change(struct kroute *kroute, int krc
> >  
> > kroute->rtlabel = rtlabel_tag2id(kroute->ext_tag);
> >  
> > -   kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen, RTP_OSPF);
> > +   kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
> > +   kr_state.fib_prio);
> > if (kr != NULL && kr->next == NULL && krcount == 1)
> > /* single path OSPF route */
> > action = RTM_CHANGE;
> > @@ -297,7 +300,7 @@ kr_change(struct kroute *kroute, int krc
> >  int
> >  kr_delete_fib(struct kroute_node *kr)
> >  {
> > -   if (kr->r.priority != RTP_OSPF)
> > +   if (kr->r.priority != kr_state.fib_prio)
> > log_warn("kr_delete_fib: %s/%d has wrong priority %d",
> > inet_ntoa(kr->r.prefix), kr->r.prefixlen, kr->r.priority);
> >  
> > @@ -316,7 +319,7 @@ kr_delete(struct kroute *kroute)
> > struct kroute_node  *kr, *nkr;
> >  
> > if ((kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
> > -   RTP_OSPF)) == NULL)
> > +   kr_state.fib_prio)) == NULL)
> > return (0);
> >  
> > while (kr != NULL) {
> > @@ -348,7 +351,7 @@ kr_fib_couple(void)
> > kr_state.fib_sync = 1;
> >  
> > RB_FOREACH(kr, kroute_tree, )
> > -   if (kr->r.priority == RTP_OSPF)
> > +   if (kr->r.priority == kr_state.fib_prio)
> > for (kn = kr; kn != NULL; kn = kn->next)
> > send_rtmsg(kr_state.fd, RTM_ADD, >r);
> >  
> > @@ -365,7 +368,7 @@ kr_fib_decouple(void)
> > return;
> >  
> > RB_FOREACH(kr, kroute_tree, )
> > -   if (kr->r.priority == RTP_OSPF)
> > +   if (kr->r.priority == kr_state.fib_prio)
> > for (kn = kr; kn != NULL; kn = kn->next)
> > send_rtmsg(kr_state.fd, RTM_DELETE, >r);
> >  
> > @@ -418,7 +421,7 @@ kr_fib_reload()
> > kn = kr->next;
> >  
> > if (kr->serial != kr_state.fib_serial) {
> > -   if (kr->r.priority == RTP_OSPF) {
> > +   if (kr->r.priority == kr_state.fib_prio) {
> > kr->serial = kr_state.fib_serial;
> > if (send_rtmsg(kr_state.fd,
> > RTM_ADD, >r) != 0)
> > @@ -431,6 +434,21 @@ kr_fib_reload()
> > }

Re: ospfd: fib-priority

2018-12-28 Thread Remi Locherer
ping

On Mon, Dec 10, 2018 at 10:40:22AM +0100, Remi Locherer wrote:
> Hi,
> 
> below patch adds "fib-priority" to ospfd.conf which allows to set a
> custom priority to routes. 32 is still the default if not set. Changing
> the priority with a reload is also supported.
> 
> A discussion about the feature can be found here:
> https://marc.info/?l=openbsd-tech=138360663119816=2
> 
> My first idea was to add an additional parameter to the functions that
> need it. But that that is not practical since then need the event that calls
> kr_dispatch_msg() needs to be reset. Because of that I added fib_prio to 
> struct kr_state.
> 
> 
> OK?
> 
> Remi
> 
> 
> 
> cvs diff: Diffing .
> Index: kroute.c
> ===
> RCS file: /cvs/src/usr.sbin/ospfd/kroute.c,v
> retrieving revision 1.111
> diff -u -p -r1.111 kroute.c
> --- kroute.c  10 Jul 2018 11:49:04 -  1.111
> +++ kroute.c  9 Dec 2018 21:39:46 -
> @@ -45,6 +45,7 @@ struct {
>   pid_t   pid;
>   int fib_sync;
>   int fib_serial;
> + u_int8_tfib_prio;
>   int fd;
>   struct eventev;
>   struct eventreload;
> @@ -127,14 +128,15 @@ kif_init(void)
>  }
>  
>  int
> -kr_init(int fs, u_int rdomain, int redis_label_or_prefix)
> +kr_init(int fs, u_int rdomain, int redis_label_or_prefix, u_int8_t fib_prio)
>  {
>   int opt = 0, rcvbuf, default_rcvbuf;
>   socklen_t   optlen;
> - int filter_prio = RTP_OSPF;
> + int filter_prio = fib_prio;
>  
>   kr_state.fib_sync = fs;
>   kr_state.rdomain = rdomain;
> + kr_state.fib_prio = fib_prio;
>  
>   if ((kr_state.fd = socket(AF_ROUTE,
>   SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, AF_INET)) == -1) {
> @@ -262,7 +264,7 @@ kr_change_fib(struct kroute_node *kr, st
>   kn->r.prefixlen = kroute[i].prefixlen;
>   kn->r.nexthop.s_addr = kroute[i].nexthop.s_addr;
>   kn->r.flags = kroute[i].flags | F_OSPFD_INSERTED;
> - kn->r.priority = RTP_OSPF;
> + kn->r.priority = kr_state.fib_prio;
>   kn->r.ext_tag = kroute[i].ext_tag;
>   rtlabel_unref(kn->r.rtlabel);   /* for RTM_CHANGE */
>   kn->r.rtlabel = kroute[i].rtlabel;
> @@ -286,7 +288,8 @@ kr_change(struct kroute *kroute, int krc
>  
>   kroute->rtlabel = rtlabel_tag2id(kroute->ext_tag);
>  
> - kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen, RTP_OSPF);
> + kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
> + kr_state.fib_prio);
>   if (kr != NULL && kr->next == NULL && krcount == 1)
>   /* single path OSPF route */
>   action = RTM_CHANGE;
> @@ -297,7 +300,7 @@ kr_change(struct kroute *kroute, int krc
>  int
>  kr_delete_fib(struct kroute_node *kr)
>  {
> - if (kr->r.priority != RTP_OSPF)
> + if (kr->r.priority != kr_state.fib_prio)
>   log_warn("kr_delete_fib: %s/%d has wrong priority %d",
>   inet_ntoa(kr->r.prefix), kr->r.prefixlen, kr->r.priority);
>  
> @@ -316,7 +319,7 @@ kr_delete(struct kroute *kroute)
>   struct kroute_node  *kr, *nkr;
>  
>   if ((kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
> - RTP_OSPF)) == NULL)
> + kr_state.fib_prio)) == NULL)
>   return (0);
>  
>   while (kr != NULL) {
> @@ -348,7 +351,7 @@ kr_fib_couple(void)
>   kr_state.fib_sync = 1;
>  
>   RB_FOREACH(kr, kroute_tree, )
> - if (kr->r.priority == RTP_OSPF)
> + if (kr->r.priority == kr_state.fib_prio)
>   for (kn = kr; kn != NULL; kn = kn->next)
>   send_rtmsg(kr_state.fd, RTM_ADD, >r);
>  
> @@ -365,7 +368,7 @@ kr_fib_decouple(void)
>   return;
>  
>   RB_FOREACH(kr, kroute_tree, )
> - if (kr->r.priority == RTP_OSPF)
> + if (kr->r.priority == kr_state.fib_prio)
>   for (kn = kr; kn != NULL; kn = kn->next)
>   send_rtmsg(kr_state.fd, RTM_DELETE, >r);
>  
> @@ -418,7 +421,7 @@ kr_fib_reload()
>   kn = kr->next;
>  
>   if (kr->serial != kr_state.fib_serial) {
> - if (kr->r.priority == RTP_OSPF) {
> + if (kr->r.priority == kr_state.fib_prio) {
>   kr->serial = kr_state.fib_serial;
>   if (send_rtmsg(kr_state.fd,
>   RTM_ADD, >r) != 0)
> @@ -431,6 +434,21 @@ kr_fib_reload()
>   }
>  }
>  
> +void
> +kr_fib_update_prio(u_int8_t fib_prio)
> +{
> + struct kroute_node  *kr;
> +
> + RB_FOREACH(kr, kroute_tree, )
> + if ((kr->r.flags & F_OSPFD_INSERTED))
> +   

ospfd: fib-priority

2018-12-10 Thread Remi Locherer
Hi,

below patch adds "fib-priority" to ospfd.conf which allows to set a
custom priority to routes. 32 is still the default if not set. Changing
the priority with a reload is also supported.

A discussion about the feature can be found here:
https://marc.info/?l=openbsd-tech=138360663119816=2

My first idea was to add an additional parameter to the functions that
need it. But that that is not practical since then need the event that calls
kr_dispatch_msg() needs to be reset. Because of that I added fib_prio to 
struct kr_state.


OK?

Remi



cvs diff: Diffing .
Index: kroute.c
===
RCS file: /cvs/src/usr.sbin/ospfd/kroute.c,v
retrieving revision 1.111
diff -u -p -r1.111 kroute.c
--- kroute.c10 Jul 2018 11:49:04 -  1.111
+++ kroute.c9 Dec 2018 21:39:46 -
@@ -45,6 +45,7 @@ struct {
pid_t   pid;
int fib_sync;
int fib_serial;
+   u_int8_tfib_prio;
int fd;
struct eventev;
struct eventreload;
@@ -127,14 +128,15 @@ kif_init(void)
 }
 
 int
-kr_init(int fs, u_int rdomain, int redis_label_or_prefix)
+kr_init(int fs, u_int rdomain, int redis_label_or_prefix, u_int8_t fib_prio)
 {
int opt = 0, rcvbuf, default_rcvbuf;
socklen_t   optlen;
-   int filter_prio = RTP_OSPF;
+   int filter_prio = fib_prio;
 
kr_state.fib_sync = fs;
kr_state.rdomain = rdomain;
+   kr_state.fib_prio = fib_prio;
 
if ((kr_state.fd = socket(AF_ROUTE,
SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK, AF_INET)) == -1) {
@@ -262,7 +264,7 @@ kr_change_fib(struct kroute_node *kr, st
kn->r.prefixlen = kroute[i].prefixlen;
kn->r.nexthop.s_addr = kroute[i].nexthop.s_addr;
kn->r.flags = kroute[i].flags | F_OSPFD_INSERTED;
-   kn->r.priority = RTP_OSPF;
+   kn->r.priority = kr_state.fib_prio;
kn->r.ext_tag = kroute[i].ext_tag;
rtlabel_unref(kn->r.rtlabel);   /* for RTM_CHANGE */
kn->r.rtlabel = kroute[i].rtlabel;
@@ -286,7 +288,8 @@ kr_change(struct kroute *kroute, int krc
 
kroute->rtlabel = rtlabel_tag2id(kroute->ext_tag);
 
-   kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen, RTP_OSPF);
+   kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
+   kr_state.fib_prio);
if (kr != NULL && kr->next == NULL && krcount == 1)
/* single path OSPF route */
action = RTM_CHANGE;
@@ -297,7 +300,7 @@ kr_change(struct kroute *kroute, int krc
 int
 kr_delete_fib(struct kroute_node *kr)
 {
-   if (kr->r.priority != RTP_OSPF)
+   if (kr->r.priority != kr_state.fib_prio)
log_warn("kr_delete_fib: %s/%d has wrong priority %d",
inet_ntoa(kr->r.prefix), kr->r.prefixlen, kr->r.priority);
 
@@ -316,7 +319,7 @@ kr_delete(struct kroute *kroute)
struct kroute_node  *kr, *nkr;
 
if ((kr = kroute_find(kroute->prefix.s_addr, kroute->prefixlen,
-   RTP_OSPF)) == NULL)
+   kr_state.fib_prio)) == NULL)
return (0);
 
while (kr != NULL) {
@@ -348,7 +351,7 @@ kr_fib_couple(void)
kr_state.fib_sync = 1;
 
RB_FOREACH(kr, kroute_tree, )
-   if (kr->r.priority == RTP_OSPF)
+   if (kr->r.priority == kr_state.fib_prio)
for (kn = kr; kn != NULL; kn = kn->next)
send_rtmsg(kr_state.fd, RTM_ADD, >r);
 
@@ -365,7 +368,7 @@ kr_fib_decouple(void)
return;
 
RB_FOREACH(kr, kroute_tree, )
-   if (kr->r.priority == RTP_OSPF)
+   if (kr->r.priority == kr_state.fib_prio)
for (kn = kr; kn != NULL; kn = kn->next)
send_rtmsg(kr_state.fd, RTM_DELETE, >r);
 
@@ -418,7 +421,7 @@ kr_fib_reload()
kn = kr->next;
 
if (kr->serial != kr_state.fib_serial) {
-   if (kr->r.priority == RTP_OSPF) {
+   if (kr->r.priority == kr_state.fib_prio) {
kr->serial = kr_state.fib_serial;
if (send_rtmsg(kr_state.fd,
RTM_ADD, >r) != 0)
@@ -431,6 +434,21 @@ kr_fib_reload()
}
 }
 
+void
+kr_fib_update_prio(u_int8_t fib_prio)
+{
+   struct kroute_node  *kr;
+
+   RB_FOREACH(kr, kroute_tree, )
+   if ((kr->r.flags & F_OSPFD_INSERTED))
+   kr->r.priority = fib_prio;
+
+   log_info("fib priority changed from %hhu to %hhu",
+   kr_state.fib_prio, fib_prio);
+
+   kr_state.fib_prio = fib_prio;
+ }
+
 /* ARGSUSED */
 

ospfd: fib-priority

2013-11-07 Thread Loïc Blot
Hi all,
sorry for to be late, but here is my ospfd patch for setting custom
routing priorities on ospfd (based on 5.4 sources)

My parse.y is more precise than Florian's, it allows priorities greater
than static routes (RTP_STATIC) and lower than RTP_MAX (63).

Also, when /etc/rc.d/ospfd reload is launched, fib is decouples,
priority changed and fib recoupled. It works nearly perfect.
The only missing point is that ospfctl sh fib shows both fib with old
and new prio (but real routing table only has new fib priority).

--- ../OpenBSD54/usr.sbin/ospfd/kroute.c2013-07-07 18:26:04.0
+0200
+++ ospfd/kroute.c  2013-11-07 17:26:58.395763302 +0100
@@ -254,7 +254,7 @@
kn-r.prefixlen = kroute[i].prefixlen;
kn-r.nexthop.s_addr = kroute[i].nexthop.s_addr;
kn-r.flags = kroute[i].flags | F_OSPFD_INSERTED;
-   kn-r.priority = RTP_OSPF;
+   kn-r.priority = get_conf()-fib_priority;
kn-r.ext_tag = kroute[i].ext_tag;
rtlabel_unref(kn-r.rtlabel);   /* for RTM_CHANGE */
kn-r.rtlabel = kroute[i].rtlabel;
@@ -278,7 +278,7 @@
 
kroute-rtlabel = rtlabel_tag2id(kroute-ext_tag);
 
-   kr = kroute_find(kroute-prefix.s_addr, kroute-prefixlen, RTP_OSPF);
+   kr = kroute_find(kroute-prefix.s_addr, kroute-prefixlen,
get_conf()-fib_priority);
if (kr != NULL  kr-next == NULL  krcount == 1)
/* single path OSPF route */
action = RTM_CHANGE;
@@ -289,7 +289,7 @@
 int
 kr_delete_fib(struct kroute_node *kr)
 {
-   if (kr-r.priority != RTP_OSPF)
+   if (kr-r.priority != get_conf()-fib_priority)
log_warn(kr_delete_fib: %s/%d has wrong priority %d,
inet_ntoa(kr-r.prefix), kr-r.prefixlen, kr-r.priority);
 
@@ -308,7 +308,7 @@
struct kroute_node  *kr, *nkr;
 
if ((kr = kroute_find(kroute-prefix.s_addr, kroute-prefixlen,
-   RTP_OSPF)) == NULL)
+   get_conf()-fib_priority)) == NULL)
return (0);
 
while (kr != NULL) {
@@ -340,7 +340,7 @@
kr_state.fib_sync = 1;
 
RB_FOREACH(kr, kroute_tree, krt)
-   if (kr-r.priority == RTP_OSPF)
+   if (kr-r.priority == get_conf()-fib_priority)
for (kn = kr; kn != NULL; kn = kn-next)
send_rtmsg(kr_state.fd, RTM_ADD, kn-r);
 
@@ -357,7 +357,7 @@
return;
 
RB_FOREACH(kr, kroute_tree, krt)
-   if (kr-r.priority == RTP_OSPF)
+   if (kr-r.priority == get_conf()-fib_priority)
for (kn = kr; kn != NULL; kn = kn-next)
send_rtmsg(kr_state.fd, RTM_DELETE, kn-r);
 
@@ -410,7 +410,7 @@
kn = kr-next;
 
if (kr-serial != kr_state.fib_serial) {
-   if (kr-r.priority == RTP_OSPF) {
+   if (kr-r.priority == get_conf()-fib_priority) 
{
kr-serial = kr_state.fib_serial;
if (send_rtmsg(kr_state.fd,
RTM_ADD, kr-r) != 0)
@@ -1142,7 +1142,7 @@
bzero(hdr, sizeof(hdr));
hdr.rtm_version = RTM_VERSION;
hdr.rtm_type = action;
-   hdr.rtm_priority = RTP_OSPF;
+   hdr.rtm_priority = get_conf()-fib_priority;
hdr.rtm_tableid = kr_state.rdomain; /* rtableid */
if (action == RTM_CHANGE)
hdr.rtm_fmask = RTF_REJECT|RTF_BLACKHOLE;
@@ -1373,7 +1373,7 @@
if (rtm-rtm_flags  RTF_MPATH)
mpath = 1;
prio = rtm-rtm_priority;
-   flags = (prio == RTP_OSPF) ?
+   flags = (prio == get_conf()-fib_priority) ?
F_OSPFD_INSERTED : F_KERNEL;
 
switch (sa-sa_family) {
@@ -1432,7 +1432,7 @@
!= NULL) {
/* get the correct route */
kr = okr;
-   if ((mpath || prio == RTP_OSPF) 
+   if ((mpath || prio == get_conf()-fib_priority) 

(kr = kroute_matchgw(okr, nexthop)) ==
NULL) {
log_warnx(dispatch_rtmsg 
@@ -1481,7 +1481,7 @@
kr-r.ifindex = ifindex;
kr-r.priority = prio;
 
-   if (rtm-rtm_priority == RTP_OSPF) {
+   if (rtm-rtm_priority == 
get_conf()-fib_priority) {
log_warnx(alien OSPF route %s/%d,
inet_ntoa(prefix), prefixlen);
rv =