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.

To be honest I would write the loop a bit different. Make the outer loop
just walk the list and then do the threshold check in the loop:

                for (mrt = rt; mrt != NULL; mrt = SMR_PTR_GET(&mrt->rt_next)) {
                        if (mrt->rt_priority != prio)
                                break;
                        if (hash <= threshold) {
                                rt = mrt;
                                break;
                        }
                        hash -= threshold;
                }

-- 
:wq Claudio


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    16 Nov 2026 12:43:18 -0000
@@ -524,14 +524,15 @@ rtable_match(unsigned int rtableid, cons
                 */
                mrt = rt;
                while (hash > threshold) {
-                       if (mrt->rt_priority == prio) {
-                               rt = mrt;
+                       if (mrt->rt_priority == prio)
                                hash -= threshold;
-                       }
+
                        mrt = SMR_PTR_GET(&mrt->rt_next);
                        if (mrt == NULL)
                                break;
                }
+               if (mrt != NULL)
+                       rt = mrt;
        }
        rtref(rt);
 out:

Reply via email to