From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out30-119.freemail.mail.aliyun.com (out30-119.freemail.mail.aliyun.com [115.124.30.119]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B707026FDB3 for ; Mon, 13 Oct 2025 02:06:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.119 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760321203; cv=none; b=PqjhO/6YYZrYECBa+m5sCMhuCK9K2BpPIZYfkwklcA8JVnY+mCpq/pmZ6jXsqJ9/hp6370o2tdOwki2x7bEs6sY4sD6UpjfLKBHvgJMmIKBUWC8nK1IfQBO7I4JNvusf1wFxlfuHQdNw6bX+sjD0KfBLZtdrlTLkMo6XPnC91SY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760321203; c=relaxed/simple; bh=DDXgFvsuKRDDaG601HQfe7CytJDlCYnul+8tfVVi5cc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O7gCM0AGrcCszfBDhtCPaGWh3RbufcKni8K7OGohiv3Al60BunRog81tKbP4G5+bm+Cvkbfrs+tkgXTDnWoToMjIqCbn1GlCmoM1lGJiqZZRUw6e35RFK+nRk96KHiMJ0tSDrIabzKtI2sWX7ud9yk4SNkblz2H6B/A2DPH6cFw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=p3B4lQ6I; arc=none smtp.client-ip=115.124.30.119 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="p3B4lQ6I" DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1760321192; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=4/eG72wBc98YTielg9d4qoV6OCuY0hED+6vjM/2rurE=; b=p3B4lQ6I6Rbwav7OJ6yY7DqSFu8s69U5JbOyipYd1vOiC/TLn6KFwm0tVQnZf0md+ehYfShVvWxUnchgASwiHl3iARo3kU9xbchTX/WxWDkr/Wryw2K1DazQ6DFo/qFkXF6jE4gfMU8/zOoNpk0gURy3bFw4hQXXNjpcXRyoWNI= Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0Wpzi6iQ_1760321191 cluster:ay36) by smtp.aliyun-inc.com; Mon, 13 Oct 2025 10:06:31 +0800 From: Xuan Zhuo To: netdev@vger.kernel.org Cc: "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Willem de Bruijn , Jiri Pirko , Alvaro Karsz , Heng Qi , virtualization@lists.linux.dev Subject: [PATCH net v2 2/3] virtio-net: correct hdr_len handling for VIRTIO_NET_F_GUEST_HDRLEN Date: Mon, 13 Oct 2025 10:06:28 +0800 Message-Id: <20251013020629.73902-3-xuanzhuo@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20251013020629.73902-1-xuanzhuo@linux.alibaba.com> References: <20251013020629.73902-1-xuanzhuo@linux.alibaba.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Git-Hash: 06377f1ca66f Content-Transfer-Encoding: 8bit The commit be50da3e9d4a ("net: virtio_net: implement exact header length guest feature") introduces support for the VIRTIO_NET_F_GUEST_HDRLEN feature in virtio-net. This feature requires virtio-net to set hdr_len to the actual header length of the packet when transmitting, the number of bytes from the start of the packet to the beginning of the transport-layer payload. However, in practice, hdr_len was being set using skb_headlen(skb), which is clearly incorrect. This commit fixes that issue. Fixes: be50da3e9d4a ("net: virtio_net: implement exact header length guest feature") Signed-off-by: Xuan Zhuo --- include/linux/virtio_net.h | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/include/linux/virtio_net.h b/include/linux/virtio_net.h index 20e0584db1dd..e059e9c57937 100644 --- a/include/linux/virtio_net.h +++ b/include/linux/virtio_net.h @@ -217,20 +217,34 @@ static inline int virtio_net_hdr_from_skb(const struct sk_buff *skb, if (skb_is_gso(skb)) { struct skb_shared_info *sinfo = skb_shinfo(skb); + u16 hdr_len = 0; + + /* In certain code paths (such as the af_packet.c receive path), + * this function may be called without a transport header. + */ + if (skb_transport_header_was_set(skb)) + hdr_len = skb_transport_offset(skb); - /* This is a hint as to how much should be linear. */ - hdr->hdr_len = __cpu_to_virtio16(little_endian, - skb_headlen(skb)); hdr->gso_size = __cpu_to_virtio16(little_endian, sinfo->gso_size); - if (sinfo->gso_type & SKB_GSO_TCPV4) + if (sinfo->gso_type & SKB_GSO_TCPV4) { hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4; - else if (sinfo->gso_type & SKB_GSO_TCPV6) + if (hdr_len) + hdr_len += tcp_hdrlen(skb); + } else if (sinfo->gso_type & SKB_GSO_TCPV6) { hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6; - else if (sinfo->gso_type & SKB_GSO_UDP_L4) + if (hdr_len) + hdr_len += tcp_hdrlen(skb); + } else if (sinfo->gso_type & SKB_GSO_UDP_L4) { hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP_L4; - else + if (hdr_len) + hdr_len += sizeof(struct udphdr); + } else { return -EINVAL; + } + + hdr->hdr_len = __cpu_to_virtio16(little_endian, hdr_len); + if (sinfo->gso_type & SKB_GSO_TCP_ECN) hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN; } else -- 2.32.0.3.g01195cf9f