On Wed, Nov 25, 2026 at 11:22:51PM +0100, Alexander Bluhm wrote:
> On Wed, Nov 25, 2026 at 10:34:45PM +0100, Claudio Jeker wrote:
> > On Wed, Nov 25, 2026 at 09:10:50PM +0100, Alexander Bluhm wrote:
> > > On Wed, Nov 25, 2026 at 03:41:05PM +0100, Claudio Jeker wrote:
> > > > With this diff I see both routes of a multipath couple used:
> > > > Destination        Gateway            Flags   Refs      Use   Mtu  Prio 
> > > > Iface
> > > > default            10.83.0.1          UGSP       7      510     -     8 
> > > > re0  
> > > > default            10.83.0.2          UGSP       5      293     -     8 
> > > > re0  
> > > > 
> > > > Before the 2nd one was never used.
> > > 
> > > Better than before.  But the use counter is still not evenly
> > > distributed.  I have updated regress/sys/net/mpath to check that.
> > > 
> > > The route itself has npaths number 1.  Every next route counts one
> > > more.  Diff below fixes distribution in my test.
> > > 
> > > Destination        Gateway            Flags   Refs      Use   Mtu  Prio 
> > > Iface
> > > 10.11.0.0          10.0.11.1          UGHSBP     0       42 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.2          UGHSBP     0       35 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.3          UGHSBP     0       34 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.4          UGHSBP     0       29 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.5          UGHSBP     0       39 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.6          UGHSBP     0       33 32768     8 
> > > lo11
> > > 10.11.0.0          10.0.11.7          UGHSBP     0       38 32768     8 
> > > lo11
> > > 
> > > with that fix, OK bluhm@
> > 
> > I refactored the first while loop this morining and missed that npath
> > starts at 1. So much for last minute change.
> > 
> > What about this version instead?
> 
> That was also my first atempt.  But then you might end up with
> npaths == 0 in threshold = (0xffff / npaths) + 1 and devide by zero.
> My AI says this can only happen during a race, but a potential
> devision by zero looks fragile.

That is only possible if rt->rt_priority changes in the middle of this
function. Now that is RCU violation but I know that our routing code is
sloppy in this regard.

We could adjust the check in the for loop to
        if (mrt != rt && mrt->rt_priority != prio)
but that is about as non-obvious as your version so I adjusted the comment
in your version and moved the npaths = 1 assignment right before the loop.
Hopefully this is clear enough what is going on.

In an ideal world rt->rt_priority would not change and also npaths 
would be precalculated and probably stored inside rt (again immutable).
 
> > Index: rtable.c
> > ===================================================================
> > RCS file: /cvs/src/sys/net/rtable.c,v
> > diff -u -p -r1.96 rtable.c
> > --- rtable.c        4 Nov 2026 08:51:52 -0000       1.96
> > +++ rtable.c        25 Nov 2026 21:32:40 -0000
> > @@ -502,15 +502,15 @@ rtable_match(unsigned int rtableid, cons
> >     /* Gateway selection by Hash-Threshold (RFC 2992) */
> >     if ((hash = rt_hash(rt, dst, src)) != -1) {
> >             struct rtentry          *mrt;
> > -           int                      threshold, npaths = 1;
> > +           int                      threshold, npaths = 0;
> >  
> >             KASSERT(hash <= 0xffff);
> >  
> >             /* Only count nexthops with the same priority. */
> > -           mrt = rt;
> > -           while ((mrt = SMR_PTR_GET(&mrt->rt_next)) != NULL) {
> > -                   if (mrt->rt_priority == prio)
> > -                           npaths++;
> > +           for (mrt = rt; mrt != NULL; mrt = SMR_PTR_GET(&mrt->rt_next)) {
> > +                   if (mrt->rt_priority != prio)
> > +                           break;
> > +                   npaths++;
> >             }
> >  
> >             threshold = (0xffff / npaths) + 1;
> > @@ -522,15 +522,14 @@ rtable_match(unsigned int rtableid, cons
> >              * we'll pick a route that we wouldn't have if we only saw the
> >              * list before or after the change.
> >              */
> > -           mrt = rt;
> > -           while (hash > threshold) {
> > -                   if (mrt->rt_priority == prio) {
> > +           for (mrt = rt; mrt != NULL; mrt = SMR_PTR_GET(&mrt->rt_next)) {
> > +                   if (mrt->rt_priority != prio)
> > +                           break;
> > +                   if (hash <= threshold) {
> >                             rt = mrt;
> > -                           hash -= threshold;
> > -                   }
> > -                   mrt = SMR_PTR_GET(&mrt->rt_next);
> > -                   if (mrt == NULL)
> >                             break;
> > +                   }
> > +                   hash -= threshold;
> >             }
> >     }
> >     rtref(rt);
> > 

-- 
:wq Claudio

Reply via email to