Skip to contentSkip to conversation

net/packet: support mergeable feature of virtio

PATCH Applied 沈安琪(凛玥) 3 participants 3 messages Apr 19, 2023 – Apr 21, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v9 1/1 net/packet: support mergeable feature of virtio

From: Jianfeng Tan <[email protected]>

Packet sockets, like tap, can be used as the backend for kernel vhost. In packet sockets, virtio net header size is currently hardcoded to be the size of struct virtio_net_hdr, which is 10 bytes; however, it is not always the case: some virtio features, such as mrg_rxbuf, need virtio net header to be 12-byte long.

Mergeable buffers, as a virtio feature, is worthy of supporting: packets that are larger than one-mbuf size will be dropped in vhost worker's handle_rx if mrg_rxbuf feature is not used, but large packets cannot be avoided and increasing mbuf's size is not economical.

With this virtio feature enabled by virtio-user, packet sockets with hardcoded 10-byte virtio net header will parse mac head incorrectly in packet_snd by taking the last two bytes of virtio net header as part of mac header.
This incorrect mac header parsing will cause packet to be dropped due to invalid ether head checking in later under-layer device packet receiving.

By adding extra field vnet_hdr_sz with utilizing holes in struct packet_sock to record currently used virtio net header size and supporting extra sockopt PACKET_VNET_HDR_SZ to set specified vnet_hdr_sz, packet sockets can know the exact length of virtio net header that virtio user gives.
In packet_snd, tpacket_snd and packet_recvmsg, instead of using hardcoded virtio net header size, it can get the exact vnet_hdr_sz from corresponding packet_sock, and parse mac header correctly based on this information to avoid the packets being mistakenly dropped.

---

Changelog

V8 -> V9:
* fix vnet_hdr_sz reset maintain original logic.

V7 -> V8:
* remove redundant variables;
* resolve KCSAN warning.

V6 -> V7:
* addresses coding style comments.

V5 -> V6:
* rebase patch based on 6.3-rc2.

V4 -> V5:
* add READ_ONCE() macro when initializing local vnet_hdr_sz variable;
* fix some nits.

V3 -> V4:
* read po->vnet_hdr_sz once during vnet_hdr_sz and use vnet_hdr_sz locally to avoid race condition;
* modify how to check non-zero po->vnet_hdr_sz;
* separate vnet_hdr_sz as a u8 field in struct packet_sock instead of 8-bit in an int field.

V2 -> V3:
* remove has_vnet_hdr field and use vnet_hdr_sz to indicate whether there is a vnet header;
* refactor PACKET_VNET_HDR and PACKET_VNET_HDR_SZ sockopt to remove redundant code.

V1 -> V2:
* refactor the implementation of PACKET_VNET_HDR and PACKET_VNET_HDR_SZ socketopts to get rid of redundate code;
* amend packet_rcv_vnet in af_packet.c to avoid extra function invocation.

 include/uapi/linux/if_packet.h |  1 +
 net/packet/af_packet.c         | 95 ++++++++++++++++++++--------------
 net/packet/diag.c              |  2 +-
 net/packet/internal.h          |  2 +-
 4 files changed, 60 insertions(+), 40 deletions(-)
include/uapi/linux/if_packet.h +1 -0
index 78c981d6a9d4..9efc42382fdb 100644
@@ -59,6 +59,7 @@ struct sockaddr_ll {
 #define PACKET_ROLLOVER_STATS		21
 #define PACKET_FANOUT_DATA		22
 #define PACKET_IGNORE_OUTGOING		23
+#define PACKET_VNET_HDR_SZ		24
 
 #define PACKET_FANOUT_HASH		0
 #define PACKET_FANOUT_LB		1
net/packet/af_packet.c +57 -38

240 lines not on this page · raw message

net/packet/diag.c +1 -1
index de4ced5cf3e8..d0c4eda4cdc6 100644
@@ -27,7 +27,7 @@ static int pdiag_put_info(const struct packet_sock *po, struct sk_buff *nlskb)
 		pinfo.pdi_flags |= PDI_AUXDATA;
 	if (packet_sock_flag(po, PACKET_SOCK_ORIGDEV))
 		pinfo.pdi_flags |= PDI_ORIGDEV;
-	if (packet_sock_flag(po, PACKET_SOCK_HAS_VNET_HDR))
+	if (READ_ONCE(po->vnet_hdr_sz))
 		pinfo.pdi_flags |= PDI_VNETHDR;
 	if (packet_sock_flag(po, PACKET_SOCK_TP_LOSS))
 		pinfo.pdi_flags |= PDI_LOSS;
net/packet/internal.h +1 -1
index 27930f69f368..63f4865202c1 100644
@@ -118,6 +118,7 @@ struct packet_sock {
 	struct mutex		pg_vec_lock;
 	unsigned long		flags;
 	int			ifindex;	/* bound device		*/
+	u8			vnet_hdr_sz;
 	__be16			num;
 	struct packet_rollover	*rollover;
 	struct packet_mclist	*mclist;
@@ -139,7 +140,6 @@ enum packet_sock_flags {
 	PACKET_SOCK_AUXDATA,
 	PACKET_SOCK_TX_HAS_OFF,
 	PACKET_SOCK_TP_LOSS,
-	PACKET_SOCK_HAS_VNET_HDR,
 	PACKET_SOCK_RUNNING,
 	PACKET_SOCK_PRESSURE,
 	PACKET_SOCK_QDISC_BYPASS,
Signature · 1 line
2.19.1.6.gb485710b
maintainer Reviewed-by on v9 1/1
#1 lore raw
Quoted 30 lines · 沈安琪(凛玥)

From: Jianfeng Tan <[email protected]>

Packet sockets, like tap, can be used as the backend for kernel vhost. In packet sockets, virtio net header size is currently hardcoded to be the size of struct virtio_net_hdr, which is 10 bytes; however, it is not always the case: some virtio features, such as mrg_rxbuf, need virtio net header to be 12-byte long.

Mergeable buffers, as a virtio feature, is worthy of supporting: packets that are larger than one-mbuf size will be dropped in vhost worker's handle_rx if mrg_rxbuf feature is not used, but large packets cannot be avoided and increasing mbuf's size is not economical.

With this virtio feature enabled by virtio-user, packet sockets with hardcoded 10-byte virtio net header will parse mac head incorrectly in packet_snd by taking the last two bytes of virtio net header as part of mac header.
This incorrect mac header parsing will cause packet to be dropped due to invalid ether head checking in later under-layer device packet receiving.

By adding extra field vnet_hdr_sz with utilizing holes in struct packet_sock to record currently used virtio net header size and supporting extra sockopt PACKET_VNET_HDR_SZ to set specified vnet_hdr_sz, packet sockets can know the exact length of virtio net header that virtio user gives.
In packet_snd, tpacket_snd and packet_recvmsg, instead of using hardcoded virtio net header size, it can get the exact vnet_hdr_sz from corresponding packet_sock, and parse mac header correctly based on this information to avoid the packets being mistakenly dropped.

Quoted 3 lines
#2 lore raw
In reply to 沈安琪(凛玥)
This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by David S. Miller <[email protected]>:

Quoted 9 lines · On Wed, 19 Apr 2023 15:24:16 +0800 you wrote:

From: Jianfeng Tan <[email protected]>

Packet sockets, like tap, can be used as the backend for kernel vhost. In packet sockets, virtio net header size is currently hardcoded to be the size of struct virtio_net_hdr, which is 10 bytes; however, it is not always the case: some virtio features, such as mrg_rxbuf, need virtio net header to be 12-byte long.

[...]

Here is the summary with links:
- [v9] net/packet: support mergeable feature of virtio

    https://git.kernel.org/netdev/net-next/c/dfc39d4026fb

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html