On Mon, Nov 16, 2026 at 02:12:07PM +0100, Claudio Jeker wrote:
> On Mon, Nov 16, 2026 at 06:17:59AM -0500, Sergey Andreyev wrote:
> > Hello,
> > 
> > I believe there is a regression in the multipath route selection algorithm
> > in sys/net/rtable.c, introduced by commit bdf12720a479 on July 10, 2025:
> > 
> > https://github.com/openbsd/src/commit/bdf12720a47942798ca6067c46b4be424af5eef5
> > 
> > During the SRP-to-SMR conversion, the selection logic in rtable_match()
> > changed from advancing through the route list and then selecting the
> > resulting entry to assigning the selected route before advancing.
> > 
> > The current code contains:
> > 
> > mrt = rt;
> > while (hash > threshold) {
> > if (mrt->rt_priority == prio) {
> > rt = mrt;
> > hash -= threshold;
> > }
> > mrt = SMR_PTR_GET(&mrt->rt_next);
> > if (mrt == NULL)
> > break;
> > }
> > 
> > With two equal-priority nexthops:
> > 
> >    - hash ranges from 0 to 65535.
> >    - threshold is 32768.
> >    - For hash <= 32768, the loop is not entered.
> >    - For hash > 32768, the first iteration assigns rt = mrt, where mrt
> >    still points to the first nexthop.
> >    - After subtracting 32768, the remaining hash is below the threshold, so
> >    the loop terminates without selecting the second nexthop.
> > 
> > Consequently, the first nexthop is selected for all 65,536 possible hash
> > values, and the second nexthop is never selected.
> > 
> > The same issue affects larger multipath groups, producing an uneven
> > distribution and leaving the last nexthop unreachable by the selection
> > algorithm.
> > 
> > The implementation before commit bdf12720a479 selected the route after
> > advancing through the list.
> > 
> > This appears to be an off-by-one error introduced during the SMR
> > conversion, rather than an intentional change to the Hash-Threshold
> > algorithm (RFC 2992).
> > 
> > The issue appears to affect OpenBSD 7.8, 7.9, and 8.0, as well as the
> > current source tree.
> > 
> > I have verified the selection behavior by examining the source code and
> > exhaustively evaluating the hash range, but I have not tested a patched
> > kernel.
> > 
> > Could you please review the route selection loop in rtable_match()?
>  
> Indeed, it seems the assignment of rt was moved and so the logic
> changed. The diff below restores the old behaviour.

Actually I think I want to commit this version. Using simple for loops
with break conditions makes this less hard on my brain to understand.

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.
-- 
:wq Claudio

Index: net/rtable.c
===================================================================
RCS file: /cvs/src/sys/net/rtable.c,v
diff -u -p -r1.96 rtable.c
--- net/rtable.c        4 Nov 2026 08:51:52 -0000       1.96
+++ net/rtable.c        25 Nov 2026 14:16:55 -0000
@@ -507,10 +507,10 @@ rtable_match(unsigned int rtableid, cons
                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);

Reply via email to