git: b74577a2eaa2 - main - if_epair: Hash the IP header, not the Ethernet header, for fanout

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Wed, 18 Nov 2026 21:20:28 UTC
The branch main has been updated by rcm:

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

commit b74577a2eaa281676f82d4914dc8d5ae54711ed5
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-18 21:17:00 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-18 21:19:34 +0000

    if_epair: Hash the IP header, not the Ethernet header, for fanout
    
    With RSS, epair_select_queue() hashes a packet that carries no RSS hash
    with rss_soft_m2cpuid_v4() or rss_soft_m2cpuid_v6(), which read the IP
    header at the start of the mbuf.  On transmit the mbuf starts with the
    Ethernet header, so the hash covered the MAC addresses, the EtherType
    and the first bytes of the IP header, the IPv4 total length and
    identification among them.  The packets of one flow were spread over the
    queues by their IP ID and delivered out of order, and the value was
    stored in the mbuf as an RSS hash for the receiving side to use.
    
    Step over the Ethernet header, and over inline 802.1Q or 802.1ad tags,
    while the software hash runs.  A tag is inline when vlan(4) or a bridge
    member runs without VLAN_HWTAGGING, always for 802.1ad, and for the
    inner tag of stacked vlans; those frames previously went to the first
    queue unhashed.  Frames with more than two tags still do.
    
    Hash with rss_mbuf_software_hash_v4() and rss_mbuf_software_hash_v6()
    and map the hash with rss_getbucket(), as the hardware hash path does,
    instead of calling the netisr CPU selectors rss_soft_m2cpuid_v4() and
    rss_soft_m2cpuid_v6(): those return NETISR_CPUID_NONE, (u_int)-1, for
    a packet they cannot hash, and that value modulo the queue count picked
    an arbitrary queue rather than the first one.
    
    Reviewed by:            kp
    Fixes:                  24f0bfbad57b ("if_epair: implement fanout")
    MFC after:              2 weeks
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61037
---
 sys/net/if_epair.c | 50 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 41 insertions(+), 9 deletions(-)

diff --git a/sys/net/if_epair.c b/sys/net/if_epair.c
index 5b00da381918..9f6b63f58f94 100644
--- a/sys/net/if_epair.c
+++ b/sys/net/if_epair.c
@@ -202,35 +202,67 @@ epair_tx_start_deferred(void *arg, int pending)
 static struct epair_queue *
 epair_select_queue(struct epair_softc *sc, struct mbuf *m)
 {
-	uint32_t bucket;
+	uint32_t bucket = 0;
 #ifdef RSS
 	struct ether_header *eh;
-	int ret;
+	uint32_t hash, hashtype;
+	uint16_t etype;
+	int i, off, ret;
 
 	ret = rss_m2bucket(m, &bucket);
 	if (ret) {
-		/* Actually hash the packet. */
+		/*
+		 * Actually hash the packet.  The software hash reads the IP
+		 * header at the start of the mbuf, so step over the Ethernet
+		 * header, and over any inline 802.1Q or 802.1ad tags, while
+		 * it runs.  Frames we cannot parse go to the first queue.
+		 */
+		off = ETHER_HDR_LEN;
+		if (m->m_len < off)
+			goto done;
 		eh = mtod(m, struct ether_header *);
+		etype = ntohs(eh->ether_type);
+		for (i = 0; i < 2 && (etype == ETHERTYPE_VLAN ||
+		    etype == ETHERTYPE_QINQ); i++) {
+			if (m->m_len < off + ETHER_VLAN_ENCAP_LEN)
+				goto done;
+			memcpy(&etype, mtod(m, char *) + off +
+			    sizeof(uint16_t), sizeof(etype));
+			etype = ntohs(etype);
+			off += ETHER_VLAN_ENCAP_LEN;
+		}
 
-		switch (ntohs(eh->ether_type)) {
+		m->m_data += off;
+		m->m_len -= off;
+		m->m_pkthdr.len -= off;
+		switch (etype) {
 #ifdef INET
 		case ETHERTYPE_IP:
-			rss_soft_m2cpuid_v4(m, 0, &bucket);
+			ret = rss_mbuf_software_hash_v4(m,
+			    RSS_HASH_PKT_INGRESS, &hash, &hashtype);
 			break;
 #endif
 #ifdef INET6
 		case ETHERTYPE_IPV6:
-			rss_soft_m2cpuid_v6(m, 0, &bucket);
+			ret = rss_mbuf_software_hash_v6(m,
+			    RSS_HASH_PKT_INGRESS, &hash, &hashtype);
 			break;
 #endif
 		default:
-			bucket = 0;
+			ret = -1;
 			break;
 		}
+		m->m_data -= off;
+		m->m_len += off;
+		m->m_pkthdr.len += off;
+		if (ret == 0) {
+			m->m_pkthdr.flowid = hash;
+			M_HASHTYPE_SET(m, hashtype);
+			bucket = rss_getbucket(hash);
+		}
 	}
+done:
 	bucket %= sc->num_queues;
-#else
-	bucket = 0;
 #endif
 	return (&sc->queues[bucket]);
 }