git: f2ff4b5edabe - main - ip_forward: Do not reference the nexthop
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 26 Nov 2026 02:14:27 UTC
The branch main has been updated by rcm:
URL: https://cgit.FreeBSD.org/src/commit/?id=f2ff4b5edabe85fa344b30f6495fcaa7dc464c12
commit f2ff4b5edabe85fa344b30f6495fcaa7dc464c12
Author: R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-26 02:09:56 +0000
Commit: R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-26 02:09:56 +0000
ip_forward: Do not reference the nexthop
ip_forward() looked the route up with NHR_REF and released the nexthop
on every exit. The function runs in the network epoch, and nexthops are
destroyed through NET_EPOCH_CALL(), so the nexthop stays valid until
ip_forward() returns without a reference, as ip_tryforward() already
relies on. The reference costs two atomic operations per packet on a
counter that every CPU forwarding to that nexthop shares.
Look the nexthop up without NHR_REF, and set RT_NH_NOREF on the
on-stack route, so that ip_output() neither releases that nexthop nor
references one it looks up itself.
On a four-core router that encrypts what it forwards into one ESP
tunnel, this takes 60-byte packets from 913 to 970 kpps, and the CPU
time for packets dropped by a blackhole route goes down by 18 %.
No functional change intended.
Reviewed by: glebius, pouria, zlei
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")
Differential Revision: https://reviews.freebsd.org/D61385
---
sys/netinet/ip_input.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/sys/netinet/ip_input.c b/sys/netinet/ip_input.c
index 9bc9f7f75e06..bac3ebc73f3c 100644
--- a/sys/netinet/ip_input.c
+++ b/sys/netinet/ip_input.c
@@ -964,17 +964,23 @@ ip_forward(struct mbuf *m, int srcrt)
sin->sin_len = sizeof(*sin);
sin->sin_addr = ip->ip_dst;
flowid = m->m_pkthdr.flowid;
- ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_REF, flowid);
+ /*
+ * The nexthop is not referenced: nexthops are destroyed through
+ * NET_EPOCH_CALL(), so this one stays valid until we return, and
+ * the route does not outlive this function. With RT_NH_NOREF
+ * ip_output() does not reference or release a nexthop of this
+ * route either.
+ */
+ ro.ro_flags = RT_NH_NOREF;
+ ro.ro_nh = fib4_lookup(M_GETFIB(m), ip->ip_dst, 0, NHR_NONE, flowid);
if (ro.ro_nh != NULL) {
if (ro.ro_nh->nh_flags & (NHF_BLACKHOLE | NHF_BROADCAST)) {
IPSTAT_INC(ips_cantforward);
m_freem(m);
- NH_FREE(ro.ro_nh);
return;
}
if (ro.ro_nh->nh_flags & NHF_REJECT) {
IPSTAT_INC(ips_cantforward);
- NH_FREE(ro.ro_nh);
icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
return;
}
@@ -1021,7 +1027,6 @@ ip_forward(struct mbuf *m, int srcrt)
if (IPSEC_ENABLED(ipv4)) {
if ((error = IPSEC_FORWARD(ipv4, m)) != 0) {
/* mbuf consumed by IPsec */
- RO_NHFREE(&ro);
m_freem(mcopy);
if (error != EINPROGRESS)
IPSTAT_INC(ips_cantforward);
@@ -1069,7 +1074,6 @@ ip_forward(struct mbuf *m, int srcrt)
if (error == EMSGSIZE && ro.ro_nh)
mtu = ro.ro_nh->nh_mtu;
- RO_NHFREE(&ro);
if (error)
IPSTAT_INC(ips_cantforward);