git: 57ccd4ea2886 - stable/15 - vtnet: Retry feature negotiation without offloads

From: Kevin Bowling <kbowling_at_FreeBSD.org>
Date: Fri, 30 Oct 2026 17:35:50 UTC
The branch stable/15 has been updated by kbowling:

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

commit 57ccd4ea288601e38e4798999d08e06e2ee9d656
Author:     Faraz Vahedi <kfv@FreeBSD.org>
AuthorDate: 2026-07-12 18:48:36 +0000
Commit:     Kevin Bowling <kbowling@FreeBSD.org>
CommitDate: 2026-10-30 17:35:39 +0000

    vtnet: Retry feature negotiation without offloads
    
    A device is permitted to reject an otherwise valid subset of its
    offered features by refusing to accept FEATURES_OK (VirtIO v1.3,
    2.2.2).  Apple's Virtualization.framework does this in practice;
    it treats the offered CSUM/TSO offloads as all-or-nothing, while
    vtnet's default request contains only part of that group because
    of hw.vtnet.lro_disable that would drop the guest TSO bits, thus
    negotiation fails and the device does not attach.
    
    If FEATURES_OK is rejected, retry the negotiation once with every
    offload-related feature stripped.  Changing the feature set after
    a failed FEATURES_OK requires re-initialising from device reset
    (VirtIO v1.3, 3.1.1), so the retry goes through virtio_reinit().
    
    A NIC without offloads is preferable to no NIC at all.  Devices
    that accept the initial feature set are unaffected, while those
    that also reject the reduced set continue to fail attachment as
    before.
    
    Signed-off-by:  Faraz Vahedi <kfv@kfv.io>
    Reviewed by:    adrian
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/2322
    
    (cherry picked from commit 5481c2d3ac1fc7682cdd184c7a797c0583e391c0)
---
 sys/dev/virtio/network/if_vtnet.c    | 16 ++++++++++++++--
 sys/dev/virtio/network/if_vtnetvar.h | 16 ++++++++++++++++
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/sys/dev/virtio/network/if_vtnet.c b/sys/dev/virtio/network/if_vtnet.c
index da89dea03f87..112edde34b03 100644
--- a/sys/dev/virtio/network/if_vtnet.c
+++ b/sys/dev/virtio/network/if_vtnet.c
@@ -641,7 +641,7 @@ vtnet_negotiate_features(struct vtnet_softc *sc)
 {
 	device_t dev;
 	uint64_t features, negotiated_features;
-	int no_csum;
+	int error, no_csum;
 
 	dev = sc->vtnet_dev;
 	features = virtio_bus_is_modern(dev) ? VTNET_MODERN_FEATURES :
@@ -719,7 +719,19 @@ vtnet_negotiate_features(struct vtnet_softc *sc)
 	sc->vtnet_features = negotiated_features;
 	sc->vtnet_negotiated_features = negotiated_features;
 
-	return (virtio_finalize_features(dev));
+	error = virtio_finalize_features(dev);
+	if (error != 0 && (features & VTNET_OFFLOAD_FEATURES) != 0) {
+		device_printf(dev,
+		    "retrying feature negotiation without offloads\n");
+		features &= ~VTNET_OFFLOAD_FEATURES;
+		negotiated_features &= ~VTNET_OFFLOAD_FEATURES;
+		sc->vtnet_flags &= ~VTNET_FLAG_LRO_NOMRG;
+		sc->vtnet_features = negotiated_features;
+		sc->vtnet_negotiated_features = negotiated_features;
+		error = virtio_reinit(dev, features);
+	}
+
+	return (error);
 }
 
 static int
diff --git a/sys/dev/virtio/network/if_vtnetvar.h b/sys/dev/virtio/network/if_vtnetvar.h
index eb5e6784b07f..237b86fd0244 100644
--- a/sys/dev/virtio/network/if_vtnetvar.h
+++ b/sys/dev/virtio/network/if_vtnetvar.h
@@ -353,6 +353,22 @@ CTASSERT(sizeof(struct vtnet_mac_filter) <= PAGE_SIZE);
 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \
     VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN)
 
+/*
+ * Union of the offload-related features offered by the driver.  As per spec,
+ * a device is permitted to reject an otherwise valid subset of its offered
+ * features by failing FEATURES_OK (v1.3 ยง2.2.2).  Offloads are where this
+ * happens in practice, so feature negotiation retries without this entire
+ * group when the device rejects the first feature set.
+ *
+ * Must cover every offload-related bit in VTNET_COMMON_FEATURES.
+ */
+#define VTNET_OFFLOAD_FEATURES \
+    (VIRTIO_NET_F_CSUM			| \
+     VIRTIO_NET_F_GUEST_CSUM		| \
+     VIRTIO_NET_F_CTRL_GUEST_OFFLOADS	| \
+     VTNET_TSO_FEATURES			| \
+     VTNET_LRO_FEATURES)
+
 #define VTNET_MIN_MTU		68
 #define VTNET_MAX_MTU		65536
 #define VTNET_MAX_RX_SIZE	65550