git: 926a8baff0e1 - main - netmap: Handle TX buffers larger than MCLBYTES

From: Ryan Moeller <rmoeller_at_FreeBSD.org>
Date: Sat, 14 Nov 2026 14:30:42 UTC
The branch main has been updated by rmoeller:

URL: https://cgit.FreeBSD.org/src/commit/?id=926a8baff0e1a9d3a0fe66be188c29edf9bd8e90

commit 926a8baff0e1a9d3a0fe66be188c29edf9bd8e90
Author:     Ryan Moeller <rmoeller@FreeBSD.org>
AuthorDate: 2026-11-12 04:54:24 +0000
Commit:     Ryan Moeller <rmoeller@FreeBSD.org>
CommitDate: 2026-11-14 14:26:26 +0000

    netmap: Handle TX buffers larger than MCLBYTES
    
    The generic netmap TX pool always allocates standard clusters
    (zone_clust), even though the buffer size may be larger.
    
    INVARIANTS kernels panic in nm_os_get_mbuf() on first transmit through a
    non-native NIC (e.g. re or epair) using netmap with jumbo frames
    configured.  Release kernels silently truncate frames in
    nm_os_generic_xmit_frame().
    
    Implement cluster zone selection based on the requested size.  Catch
    oversized requests in nm_os_catch_tx() and return EINVAL.  Add a KASSERT
    before the truncation fallback in xmit.  Make nm_os_get_mbuf() take a
    uint32_t size, matching the type of the value passed to it.
    
    Tested using epair, pkt-gen, and tcpdump on release (15.1) and
    INVARIANTS (main) kernels at buffer sizes 2048/4096/9216/16384.
    
    Reviewed by:    vmaffione, kevans, markj
    Approved by:    kevans (mentor)
    Differential Revision:  https://reviews.freebsd.org/D61281
---
 sys/dev/netmap/netmap_freebsd.c | 10 ++++++++++
 sys/dev/netmap/netmap_generic.c |  4 ++--
 sys/dev/netmap/netmap_kern.h    | 30 +++++++++++++++++++++---------
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/sys/dev/netmap/netmap_freebsd.c b/sys/dev/netmap/netmap_freebsd.c
index 409b273e6442..18c623d2ca41 100644
--- a/sys/dev/netmap/netmap_freebsd.c
+++ b/sys/dev/netmap/netmap_freebsd.c
@@ -375,6 +375,13 @@ nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
 	struct netmap_adapter *na = &gna->up.up;
 	if_t ifp = netmap_generic_getifp(gna);
 
+	/* Each tx slot is copied into a single mbuf cluster. */
+	if (intercept && NETMAP_BUF_SIZE(na) > NM_GENERIC_MAX_BUF_SIZE) {
+		nm_prerr("%s: buf size %u exceeds emulated mode max %d",
+		    na->name, NETMAP_BUF_SIZE(na), NM_GENERIC_MAX_BUF_SIZE);
+		return (EINVAL);
+	}
+
 	nm_os_ifnet_lock();
 	if (intercept) {
 		na->if_transmit = if_gettransmitfn(ifp);
@@ -420,6 +427,9 @@ nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
 	M_ASSERTPKTHDR(m);
 	KASSERT((m->m_flags & M_EXT) != 0,
 	    ("%s: mbuf %p has no cluster", __func__, m));
+	KASSERT(m->m_ext.ext_size >= len,
+	    ("%s: mbuf %p cluster size %u < len %u", __func__, m,
+	    m->m_ext.ext_size, len));
 
 	if (MBUF_REFCNT(m) != 1) {
 		nm_prerr("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
diff --git a/sys/dev/netmap/netmap_generic.c b/sys/dev/netmap/netmap_generic.c
index 9b1218043a88..a9f7d044a42a 100644
--- a/sys/dev/netmap/netmap_generic.c
+++ b/sys/dev/netmap/netmap_generic.c
@@ -102,9 +102,9 @@
 #include <linux/hrtimer.h>
 
 static inline struct mbuf *
-nm_os_get_mbuf(struct ifnet *ifp, int len)
+nm_os_get_mbuf(struct ifnet *ifp, uint32_t size)
 {
-	return alloc_skb(LL_RESERVED_SPACE(ifp) + len +
+	return alloc_skb(LL_RESERVED_SPACE(ifp) + size +
 			 ifp->needed_tailroom, GFP_ATOMIC);
 }
 
diff --git a/sys/dev/netmap/netmap_kern.h b/sys/dev/netmap/netmap_kern.h
index 704b880b3232..f9cc7adb985c 100644
--- a/sys/dev/netmap/netmap_kern.h
+++ b/sys/dev/netmap/netmap_kern.h
@@ -2414,10 +2414,12 @@ ptnet_sync_tail(struct nm_csb_ktoa *ktoa, struct netmap_kring *kring)
  * has a KASSERT(), checking that the mbuf dtor function is not NULL.
  */
 
+#define NM_GENERIC_MAX_BUF_SIZE MJUM16BYTES
+
 static inline void
 nm_generic_mbuf_dtor(struct mbuf *m)
 {
-	uma_zfree(zone_clust, m->m_ext.ext_buf);
+	uma_zfree(m_getzone(m->m_ext.ext_size), m->m_ext.ext_buf);
 }
 
 #define SET_MBUF_DESTRUCTOR(m, fn, na)	do {		\
@@ -2427,22 +2429,33 @@ nm_generic_mbuf_dtor(struct mbuf *m)
 } while (0)
 
 static inline struct mbuf *
-nm_os_get_mbuf(if_t ifp __unused, int len)
+nm_os_get_mbuf(if_t ifp __unused, uint32_t size)
 {
 	struct mbuf *m;
 	void *buf;
 
-	KASSERT(len <= MCLBYTES, ("%s: len %d", __func__, len));
+	KASSERT(size <= NM_GENERIC_MAX_BUF_SIZE,
+	    ("%s: size %u > %d", __func__, size, NM_GENERIC_MAX_BUF_SIZE));
 
 	m = m_gethdr(M_NOWAIT, MT_DATA);
 	if (__predict_false(m == NULL))
 		return (NULL);
-	buf = uma_zalloc(zone_clust, M_NOWAIT);
+	if (size <= MCLBYTES)
+		size = MCLBYTES;
+#if MJUMPAGESIZE != MCLBYTES
+	else if (size <= MJUMPAGESIZE)
+		size = MJUMPAGESIZE;
+#endif
+	else if (size <= MJUM9BYTES)
+		size = MJUM9BYTES;
+	else
+		size = MJUM16BYTES;
+	buf = uma_zalloc(m_getzone(size), M_NOWAIT);
 	if (__predict_false(buf == NULL)) {
 		m_free(m);
 		return (NULL);
 	}
-	m_extadd(m, buf, MCLBYTES, nm_generic_mbuf_dtor, NULL, NULL, 0,
+	m_extadd(m, buf, size, nm_generic_mbuf_dtor, NULL, NULL, 0,
 	    EXT_NET_DRV);
 	return (m);
 }
@@ -2451,16 +2464,15 @@ static inline void
 nm_os_mbuf_reinit(struct mbuf *m)
 {
 	void *buf;
+	uint32_t size;
 
 	KASSERT((m->m_flags & M_EXT) != 0,
 	    ("%s: mbuf %p has no external storage", __func__, m));
-	KASSERT(m->m_ext.ext_size == MCLBYTES,
-	    ("%s: mbuf %p has wrong external storage size %u", __func__, m,
-	    m->m_ext.ext_size));
 
 	buf = m->m_ext.ext_buf;
+	size = m->m_ext.ext_size;
 	m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
-	m_extadd(m, buf, MCLBYTES, nm_generic_mbuf_dtor, NULL, NULL, 0,
+	m_extadd(m, buf, size, nm_generic_mbuf_dtor, NULL, NULL, 0,
 	    EXT_NET_DRV);
 }