Skip to contentSkip to conversation

tun: Unify vnet implementation and fill full vnet header

PATCH Superseded Akihiko Odaki 5 participants 36 messages Jan 9, 2025 – Jan 20, 2025

Subsystems (8)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover tun: Unify vnet implementation and fill full vnet header

When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().

This series fills the uninitialized hole. More concretely, the num_buffers field will be initialized with 1, and the other fields will be inialized with 0. Setting the num_buffers field to 1 is mandated by virtio 1.0 [2].

The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.

[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/

---
Changes in v2:
- Fixed num_buffers endian.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (3):
tun: Unify vnet implementation
tun: Pad virtio header with zero
tun: Set num_buffers for virtio 1.0

 MAINTAINERS            |   1 +
 drivers/net/Kconfig    |   5 ++
 drivers/net/Makefile   |   1 +
 drivers/net/tap.c      | 174 ++++++----------------------------------
 drivers/net/tun.c      | 214 +++++++++----------------------------------------
 drivers/net/tun_vnet.c | 191 +++++++++++++++++++++++++++++++++++++++++++
 drivers/net/tun_vnet.h |  24 ++++++
 7 files changed, 283 insertions(+), 327 deletions(-)

---
base-commit: a32e14f8aef69b42826cf0998b068a43d486a9e9
change-id: 20241230-tun-66e10a49b0c7

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v2 1/3 tun: Unify vnet implementation

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
 MAINTAINERS            |   1 +
 drivers/net/Kconfig    |   5 ++
 drivers/net/Makefile   |   1 +
 drivers/net/tap.c      | 172 ++++++----------------------------------
 drivers/net/tun.c      | 208 ++++++++-----------------------------------------
 drivers/net/tun_vnet.c | 186 +++++++++++++++++++++++++++++++++++++++++++
 drivers/net/tun_vnet.h |  24 ++++++
 7 files changed, 273 insertions(+), 324 deletions(-)
MAINTAINERS +1 -0
index 910305c11e8a..1be8a452d11f 100644
@@ -23903,6 +23903,7 @@ F:	Documentation/networking/tuntap.rst
 F:	arch/um/os-Linux/drivers/
 F:	drivers/net/tap.c
 F:	drivers/net/tun.c
+F:	drivers/net/tun_vnet.h
 
 TURBOCHANNEL SUBSYSTEM
 M:	"Maciej W. Rozycki" <[email protected]>
drivers/net/Kconfig +5 -0
index 1fd5acdc73c6..255c8f9f1d7c 100644
@@ -395,6 +395,7 @@ config TUN
 	tristate "Universal TUN/TAP device driver support"
 	depends on INET
 	select CRC32
+	select TUN_VNET
 	help
 	  TUN/TAP provides packet reception and transmission for user space
 	  programs.  It can be viewed as a simple Point-to-Point or Ethernet
@@ -417,10 +418,14 @@ config TUN
 
 config TAP
 	tristate
+	select TUN_VNET
 	help
 	  This option is selected by any driver implementing tap user space
 	  interface for a virtual interface to re-use core tap functionality.
 
+config TUN_VNET
+	tristate
+
 config TUN_VNET_CROSS_LE
 	bool "Support for cross-endian vnet headers on little-endian kernels"
 	default n
drivers/net/Makefile +1 -0
index 13743d0e83b5..bc1f193eccb1 100644
@@ -30,6 +30,7 @@ obj-y += pcs/
 obj-$(CONFIG_RIONET) += rionet.o
 obj-$(CONFIG_NET_TEAM) += team/
 obj-$(CONFIG_TUN) += tun.o
+obj-$(CONFIG_TUN_VNET) += tun_vnet.o
 obj-$(CONFIG_TAP) += tap.o
 obj-$(CONFIG_VETH) += veth.o
 obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
drivers/net/tap.c +23 -149

240 lines not on this page · raw message

drivers/net/tun.c +33 -175

326 lines not on this page · raw message

drivers/net/tun_vnet.c new file +186 -0

186 lines not on this page · raw message

drivers/net/tun_vnet.h new file +24 -0
new file mode 100644
index 000000000000..2dfdbe92bb24
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef TUN_VNET_H
+#define TUN_VNET_H
+
+#include <linux/if_tun.h>
+#include <linux/virtio_net.h>
+
+long tun_vnet_ioctl(int *sz, unsigned int *flags,
+		    unsigned int cmd, int __user *sp);
+
+int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
+		     struct virtio_net_hdr *hdr);
+
+int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
+		     const struct virtio_net_hdr *hdr);
+
+int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
+			const struct virtio_net_hdr *hdr);
+
+int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
+			  const struct sk_buff *skb,
+			  struct virtio_net_hdr *hdr);
+
+#endif /* TUN_VNET_H */
Signature · 1 line
2.47.1
maintainer
#2 lore raw

Re: [PATCH v2 1/3] tun: Unify vnet implementation

Quoted 31 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
 MAINTAINERS            |   1 +
 drivers/net/Kconfig    |   5 ++
 drivers/net/Makefile   |   1 +
 drivers/net/tap.c      | 172 ++++++----------------------------------
 drivers/net/tun.c      | 208 ++++++++-----------------------------------------
 drivers/net/tun_vnet.c | 186 +++++++++++++++++++++++++++++++++++++++++++
 drivers/net/tun_vnet.h |  24 ++++++
 7 files changed, 273 insertions(+), 324 deletions(-)
MAINTAINERS +1 -0
index 910305c11e8a..1be8a452d11f 100644
@@ -23903,6 +23903,7 @@ F:	Documentation/networking/tuntap.rst
 F:	arch/um/os-Linux/drivers/
 F:	drivers/net/tap.c
 F:	drivers/net/tun.c
+F:	drivers/net/tun_vnet.h
 
 TURBOCHANNEL SUBSYSTEM
 M:	"Maciej W. Rozycki" <[email protected]>
drivers/net/Kconfig +0 -0
index 1fd5acdc73c6..255c8f9f1d7c 100644

drivers/net/Kconfig config TUN · line 398

Quoted 4 lines
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN
 	tristate "Universal TUN/TAP device driver support"
 	depends on INET
 	select CRC32
+	select TUN_VNET

No need for this new Kconfig

Quoted 42 lines
drivers/net/Kconfig +5 -22
 static struct proto tap_proto = {
 	.name = "tap",
@@ -641,10 +576,10 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 	struct sk_buff *skb;
 	struct tap_dev *tap;
 	unsigned long total_len = iov_iter_count(from);
-	unsigned long len = total_len;
+	unsigned long len;
 	int err;
 	struct virtio_net_hdr vnet_hdr = { 0 };
-	int vnet_hdr_len = 0;
+	int hdr_len;
 	int copylen = 0;
 	int depth;
 	bool zerocopy = false;
@@ -652,38 +587,20 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 	enum skb_drop_reason drop_reason;
 
 	if (q->flags & IFF_VNET_HDR) {
-		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
-
-		err = -EINVAL;
-		if (len < vnet_hdr_len)
-			goto err;
-		len -= vnet_hdr_len;
-
-		err = -EFAULT;
-		if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
-			goto err;
-		iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
-		if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
-		     tap16_to_cpu(q, vnet_hdr.csum_start) +
-		     tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
-			     tap16_to_cpu(q, vnet_hdr.hdr_len))
-			vnet_hdr.hdr_len = cpu_to_tap16(q,
-				 tap16_to_cpu(q, vnet_hdr.csum_start) +
-				 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
-		err = -EINVAL;
-		if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
+		hdr_len = tun_vnet_hdr_get(READ_ONCE(q->vnet_hdr_sz), q->flags, from, &vnet_hdr);
+		if (hdr_len < 0) {
+			err = hdr_len;

drivers/net/Kconfig static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, · line 681

Quoted 10 lines
drivers/net/Kconfig +3 -4
 			goto err;
+		}
+	} else {
+		hdr_len = 0;
 	}
 
-	err = -EINVAL;
-	if (unlikely(len < ETH_HLEN))
-		goto err;
-

Is this check removal intentional?

Quoted 48 lines
drivers/net/Kconfig +5 -15
+	len = iov_iter_count(from);
 	if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
 		struct iov_iter i;
 
-		copylen = vnet_hdr.hdr_len ?
-			tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
+		copylen = hdr_len ? hdr_len : GOODCOPY_LEN;
 		if (copylen > good_linear)
 			copylen = good_linear;
 		else if (copylen < ETH_HLEN)
@@ -697,7 +614,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 
 	if (!zerocopy) {
 		copylen = len;
-		linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
+		linear = hdr_len;
 		if (linear > good_linear)
 			linear = good_linear;
 		else if (linear < ETH_HLEN)
@@ -732,9 +649,8 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 	}
 	skb->dev = tap->dev;
 
-	if (vnet_hdr_len) {
-		err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
-					    tap_is_little_endian(q));
+	if (q->flags & IFF_VNET_HDR) {
+		err = tun_vnet_hdr_to_skb(q->flags, skb, &vnet_hdr);
 		if (err) {
 			rcu_read_unlock();
 			drop_reason = SKB_DROP_REASON_DEV_HDR;
@@ -797,23 +713,17 @@ static ssize_t tap_put_user(struct tap_queue *q,
 	int total;
 
 	if (q->flags & IFF_VNET_HDR) {
-		int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
 		struct virtio_net_hdr vnet_hdr;
 
 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
-		if (iov_iter_count(iter) < vnet_hdr_len)
-			return -EINVAL;
-
-		if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
-					    tap_is_little_endian(q), true,
-					    vlan_hlen))
-			BUG();
 
-		if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=

drivers/net/Kconfig static ssize_t tap_put_user(struct tap_queue *q, · line 726

Quoted 10 lines
drivers/net/Kconfig +6 -3
-		    sizeof(vnet_hdr))
-			return -EFAULT;
+		ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+		if (ret < 0)
+			goto done;
 
-		iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
+		ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr);
+		if (ret < 0)
+			goto done;

Please split this patch in to a series of smaller patches.

If feasible:

1. one that move the head of tun.c into tun_vnet.[hc].
2. then one that uses that also in tap.c.
3. then a separate patch for the ioctl changes.
4. then introduce tun_vnet_hdr_from_skb, tun_vnet_hdr_put and friends in (a) follow-up patch(es).

This is subtle code. Please report what tests you ran to ensure that it does not introduce behavioral changes / regressions.

#3 lore raw

Re: [PATCH v2 1/3] tun: Unify vnet implementation

Quoted 25 lines · Willem de Bruijn
Quoted 24 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
  MAINTAINERS            |   1 +
  drivers/net/Kconfig    |   5 ++
  drivers/net/Makefile   |   1 +
  drivers/net/tap.c      | 172 ++++++----------------------------------
  drivers/net/tun.c      | 208 ++++++++-----------------------------------------
  drivers/net/tun_vnet.c | 186 +++++++++++++++++++++++++++++++++++++++++++
  drivers/net/tun_vnet.h |  24 ++++++
  7 files changed, 273 insertions(+), 324 deletions(-)
MAINTAINERS +1 -0
index 910305c11e8a..1be8a452d11f 100644
@@ -23903,6 +23903,7 @@ F:	Documentation/networking/tuntap.rst
  F:	arch/um/os-Linux/drivers/
  F:	drivers/net/tap.c
  F:	drivers/net/tun.c
+F:	drivers/net/tun_vnet.h
 
Quoted 13 lines

drivers/net/Kconfig config TUN · line 398

Quoted 11 lines
MAINTAINERS +0 -0
  TURBOCHANNEL SUBSYSTEM
  M:	"Maciej W. Rozycki" <[email protected]>
drivers/net/Kconfig +1 -0
index 1fd5acdc73c6..255c8f9f1d7c 100644
@@ -395,6 +395,7 @@ config TUN
  	tristate "Universal TUN/TAP device driver support"
  	depends on INET
  	select CRC32
+	select TUN_VNET

No need for this new Kconfig

I will merge tun_vnet.c into TAP.

Quoted 49 lines
Quoted 49 lines
drivers/net/Kconfig +8 -22
  static struct proto tap_proto = {
  	.name = "tap",
@@ -641,10 +576,10 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
  	struct sk_buff *skb;
  	struct tap_dev *tap;
  	unsigned long total_len = iov_iter_count(from);
-	unsigned long len = total_len;
+	unsigned long len;
  	int err;
  	struct virtio_net_hdr vnet_hdr = { 0 };
-	int vnet_hdr_len = 0;
+	int hdr_len;
  	int copylen = 0;
  	int depth;
  	bool zerocopy = false;
@@ -652,38 +587,20 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
  	enum skb_drop_reason drop_reason;
 
  	if (q->flags & IFF_VNET_HDR) {
-		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
-
-		err = -EINVAL;
-		if (len < vnet_hdr_len)
-			goto err;
-		len -= vnet_hdr_len;
-
-		err = -EFAULT;
-		if (!copy_from_iter_full(&vnet_hdr, sizeof(vnet_hdr), from))
-			goto err;
-		iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
-		if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
-		     tap16_to_cpu(q, vnet_hdr.csum_start) +
-		     tap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
-			     tap16_to_cpu(q, vnet_hdr.hdr_len))
-			vnet_hdr.hdr_len = cpu_to_tap16(q,
-				 tap16_to_cpu(q, vnet_hdr.csum_start) +
-				 tap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
-		err = -EINVAL;
-		if (tap16_to_cpu(q, vnet_hdr.hdr_len) > len)
+		hdr_len = tun_vnet_hdr_get(READ_ONCE(q->vnet_hdr_sz), q->flags, from, &vnet_hdr);
+		if (hdr_len < 0) {
+			err = hdr_len;
  			goto err;
+		}
+	} else {
+		hdr_len = 0;
  	}
 
Quoted 6 lines

drivers/net/Kconfig static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, · line 681

Quoted 4 lines
drivers/net/Kconfig +0 -4
-	err = -EINVAL;
-	if (unlikely(len < ETH_HLEN))
-		goto err;
-

Is this check removal intentional?

No, I'm not sure what this check is for, but it is irrlevant with vnet header and shouldn't be modified with this patch. I'll restore the check with the next version.

Quoted 62 lines
Quoted 49 lines
drivers/net/Kconfig +5 -15
+	len = iov_iter_count(from);
  	if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  		struct iov_iter i;
 
-		copylen = vnet_hdr.hdr_len ?
-			tap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
+		copylen = hdr_len ? hdr_len : GOODCOPY_LEN;
  		if (copylen > good_linear)
  			copylen = good_linear;
  		else if (copylen < ETH_HLEN)
@@ -697,7 +614,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
 
  	if (!zerocopy) {
  		copylen = len;
-		linear = tap16_to_cpu(q, vnet_hdr.hdr_len);
+		linear = hdr_len;
  		if (linear > good_linear)
  			linear = good_linear;
  		else if (linear < ETH_HLEN)
@@ -732,9 +649,8 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
  	}
  	skb->dev = tap->dev;
 
-	if (vnet_hdr_len) {
-		err = virtio_net_hdr_to_skb(skb, &vnet_hdr,
-					    tap_is_little_endian(q));
+	if (q->flags & IFF_VNET_HDR) {
+		err = tun_vnet_hdr_to_skb(q->flags, skb, &vnet_hdr);
  		if (err) {
  			rcu_read_unlock();
  			drop_reason = SKB_DROP_REASON_DEV_HDR;
@@ -797,23 +713,17 @@ static ssize_t tap_put_user(struct tap_queue *q,
  	int total;
 
  	if (q->flags & IFF_VNET_HDR) {
-		int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
  		struct virtio_net_hdr vnet_hdr;
 
  		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
-		if (iov_iter_count(iter) < vnet_hdr_len)
-			return -EINVAL;
-
-		if (virtio_net_hdr_from_skb(skb, &vnet_hdr,
-					    tap_is_little_endian(q), true,
-					    vlan_hlen))
-			BUG();
 
-		if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=

drivers/net/Kconfig static ssize_t tap_put_user(struct tap_queue *q, · line 726

Quoted 10 lines
drivers/net/Kconfig +6 -3
-		    sizeof(vnet_hdr))
-			return -EFAULT;
+		ret = tun_vnet_hdr_from_skb(q->flags, NULL, skb, &vnet_hdr);
+		if (ret < 0)
+			goto done;
 
-		iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
+		ret = tun_vnet_hdr_put(vnet_hdr_len, iter, &vnet_hdr);
+		if (ret < 0)
+			goto done;

Please split this patch in to a series of smaller patches.

Quoted 7 lines

If feasible:

1. one that move the head of tun.c into tun_vnet.[hc].
2. then one that uses that also in tap.c.
3. then a separate patch for the ioctl changes.
4. then introduce tun_vnet_hdr_from_skb, tun_vnet_hdr_put and friends in (a) follow-up patch(es).

I will do so.

Quoted 3 lines

This is subtle code. Please report what tests you ran to ensure that it does not introduce behavioral changes / regressions.

I tried:
- curl on QEMU with macvtap (vhost=on)
- curl on QEMU with macvtap (vhost=off)

#4 lore raw

Re: [PATCH v2 1/3] tun: Unify vnet implementation

In reply to Akihiko Odaki
Quoted 32 lines · Akihiko Odaki

Both tun and tap exposes the same set of virtio-net-related features. Unify their implementations to ease future changes.

---
 MAINTAINERS            |   1 +
 drivers/net/Kconfig    |   5 ++
 drivers/net/Makefile   |   1 +
 drivers/net/tap.c      | 172 ++++++----------------------------------
 drivers/net/tun.c      | 208 ++++++++-----------------------------------------
 drivers/net/tun_vnet.c | 186 +++++++++++++++++++++++++++++++++++++++++++
 drivers/net/tun_vnet.h |  24 ++++++
 7 files changed, 273 insertions(+), 324 deletions(-)
MAINTAINERS +1 -0
index 910305c11e8a..1be8a452d11f 100644
@@ -23903,6 +23903,7 @@ F:      Documentation/networking/tuntap.rst
 F:     arch/um/os-Linux/drivers/
 F:     drivers/net/tap.c
 F:     drivers/net/tun.c
+F:     drivers/net/tun_vnet.h
 
 TURBOCHANNEL SUBSYSTEM
 M:     "Maciej W. Rozycki" <[email protected]>
drivers/net/Kconfig +0 -0
index 1fd5acdc73c6..255c8f9f1d7c 100644

drivers/net/Kconfig config TUN · line 398

Quoted 4 lines
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN
        tristate "Universal TUN/TAP device driver support"
        depends on INET
        select CRC32
+       select TUN_VNET

I don't think we need a dedicated Kconfig option here.

Btw, fixes should come first as it simplifies the backporting.

Thanks

#5 lore raw

PATCH v2 2/3 tun: Pad virtio header with zero

In reply to Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

---
 drivers/net/tun_vnet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
 	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
 		return -EFAULT;
 
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
 
 	return 0;
 }
Signature · 1 line
2.47.1
#6 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Quoted 21 lines
---
 drivers/net/tun_vnet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
 	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
 		return -EFAULT;
 
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
 
 	return 0;
 }
Signature · 2 lines
-- 
2.47.1
#7 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 8 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

Quoted 23 lines
Quoted 22 lines
---
  drivers/net/tun_vnet.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
  	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
  		return -EFAULT;
 
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
 
  	return 0;
  }
Signature · 2 lines
-- 
2.47.1
#8 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 20 lines · Akihiko Odaki
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 8 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Quoted 8 lines

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

It's been like this for years, I'd say we wait until we know there's a problem?

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
---
  drivers/net/tun_vnet.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
  	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
  		return -EFAULT;
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
  	return 0;
  }
 
Signature · 1 line
2.47.1
#9 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 26 lines · Michael S. Tsirkin
Quoted 25 lines · Akihiko Odaki
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 8 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

Quoted 5 lines
Quoted 3 lines

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

It's been like this for years, I'd say we wait until we know there's a problem?

Perhaps we can just leave it as is. Let me ask filesystem and networking people:

Is it OK to leave some part of buffer uninitialized with read_iter() or recvmsg()?

Quoted 23 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines

---
   drivers/net/tun_vnet.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
   	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
   		return -EFAULT;
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
   	return 0;
   }
 
Signature · 1 line
2.47.1
reviewer
#10 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 31 lines · Akihiko Odaki
Quoted 30 lines · Michael S. Tsirkin
Quoted 28 lines · Akihiko Odaki
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 8 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

Quoted 7 lines
Quoted 1 line

It's been like this for years, I'd say we wait until we know there's a problem?

Perhaps we can just leave it as is. Let me ask filesystem and networking people:

Is it OK to leave some part of buffer uninitialized with read_iter() or recvmsg()?

I think that leaving part of the IO buffer within returned IO length uninitialized is a very bad practice and I'm not aware of any place in filesystem area that would do that. It makes life unnecessarily harder for userspace and also it is invitation for subtle information leaks (depending on who allocates the buffer and who then gets to read the results). So I think the patch makes sense.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#11 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

In reply to Akihiko Odaki
Quoted 10 lines · Akihiko Odaki
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Zero is a valid hash value, so cannot be used as an indication that hashing is inactive.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

Quoted 7 lines
Quoted 2 lines

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If this is user memory that is ignored by the kernel, just reflected back, then there is no need in general to zero it. There are many such instances, also in msg_control.

If not zeroing leads to ambiguity with the new feature, that would be a reason to add it -- it is always safe to do so.

Quoted 3 lines

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

#12 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 12 lines · Willem de Bruijn
Quoted 10 lines · Akihiko Odaki
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 2 lines

Zero is a valid hash value, so cannot be used as an indication that hashing is inactive.

Zeroing will initialize the hash_report field to VIRTIO_NET_HASH_REPORT_NONE, which tells it does not have a hash value.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Quoted 8 lines
Quoted 4 lines

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If this is user memory that is ignored by the kernel, just reflected back, then there is no need in general to zero it. There are many such instances, also in msg_control.

More specifically, is there any instance of recvmsg() implementation which returns N and does not fill the complete N bytes of msg_iter?

Quoted 7 lines

If not zeroing leads to ambiguity with the new feature, that would be a reason to add it -- it is always safe to do so.

Quoted 3 lines

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

#13 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 34 lines · Akihiko Odaki
Quoted 12 lines · Willem de Bruijn
Quoted 10 lines · Akihiko Odaki
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 2 lines

Zero is a valid hash value, so cannot be used as an indication that hashing is inactive.

Zeroing will initialize the hash_report field to VIRTIO_NET_HASH_REPORT_NONE, which tells it does not have a hash value.

Quoted 15 lines
Quoted 14 lines
Quoted 9 lines
Quoted 6 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

Quoted 6 lines
Quoted 3 lines

If this is user memory that is ignored by the kernel, just reflected back, then there is no need in general to zero it. There are many such instances, also in msg_control.

More specifically, is there any instance of recvmsg() implementation which returns N and does not fill the complete N bytes of msg_iter?

The one in tun. It was a silly idea but it has been here for years now.

Quoted 6 lines
Quoted 6 lines

If not zeroing leads to ambiguity with the new feature, that would be a reason to add it -- it is always safe to do so.

Quoted 3 lines

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

#14 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 35 lines · Michael S. Tsirkin
Quoted 34 lines · Akihiko Odaki
Quoted 12 lines · Willem de Bruijn
Quoted 10 lines · Akihiko Odaki
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 2 lines

Zero is a valid hash value, so cannot be used as an indication that hashing is inactive.

Zeroing will initialize the hash_report field to VIRTIO_NET_HASH_REPORT_NONE, which tells it does not have a hash value.

Quoted 15 lines
Quoted 14 lines
Quoted 9 lines
Quoted 6 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

If this is user memory that is ignored by the kernel, just reflected back, then there is no need in general to zero it. There are many such instances, also in msg_control.

More specifically, is there any instance of recvmsg() implementation which returns N and does not fill the complete N bytes of msg_iter?

The one in tun. It was a silly idea but it has been here for years now.

Except tun. If there is such an example of recvmsg() implementation and it is not accidental and people have agreed to keep it functioning, we can confidently say this construct is safe without fearing pushback from people maintaining filesystem/networking infrastructure. Ultimately I want those people decide if this can be supported for the future or not.

Quoted 9 lines
Quoted 8 lines
Quoted 8 lines

If not zeroing leads to ambiguity with the new feature, that would be a reason to add it -- it is always safe to do so.

Quoted 3 lines

If we are really confident that it will not cause problems, this behavior can be opt-in based on a flag or we can just write some documentation warning userspace programmers to initialize the buffer.

maintainer
#15 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 43 lines · Akihiko Odaki
Quoted 42 lines · Michael S. Tsirkin
Quoted 40 lines · Akihiko Odaki
Quoted 12 lines · Willem de Bruijn
Quoted 10 lines · Akihiko Odaki
Quoted 9 lines · Michael S. Tsirkin
Quoted 8 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 2 lines

Zero is a valid hash value, so cannot be used as an indication that hashing is inactive.

Zeroing will initialize the hash_report field to VIRTIO_NET_HASH_REPORT_NONE, which tells it does not have a hash value.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 6 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

Quoted 8 lines
Quoted 4 lines

Yes. but that means the user expects some part of buffer is not filled after read() or recvmsg(). I'm a bit worried that not filling the buffer may break assumptions others (especially the filesystem and socket infrastructures in the kernel) may have.

If this is user memory that is ignored by the kernel, just reflected back, then there is no need in general to zero it. There are many such instances, also in msg_control.

More specifically, is there any instance of recvmsg() implementation which returns N and does not fill the complete N bytes of msg_iter?

Quoted 7 lines
Quoted 1 line

The one in tun. It was a silly idea but it has been here for years now.

Except tun. If there is such an example of recvmsg() implementation and it is not accidental and people have agreed to keep it functioning, we can confidently say this construct is safe without fearing pushback from people maintaining filesystem/networking infrastructure. Ultimately I want those people decide if this can be supported for the future or not.

It seems preferable to write a value.

Userspace should have not assumption that whatever it writes there will be reflected unmodified. That said, that is the tiny risk of changing this in established code.

If it worked without issue so far, without hashing, then probably the change should only go to net-next.

As said, there are examples in msg_control. I don't immediately have an example where this is the case in msg_data today. A search for iov_iter_advance might show something.

#16 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

In reply to Michael S. Tsirkin
Quoted 10 lines · Michael S. Tsirkin
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 8 lines
Quoted 5 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

But if the user did it, you have just overwritten his value, did you not?

To clearify, I mean if user pre-filled buffer with 1, you have now regressed it. Patch 3 fixes it back, but - not pretty.

Quoted 21 lines
Quoted 21 lines
---
 drivers/net/tun_vnet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +2 -1
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
 	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
 		return -EFAULT;
 
-	iov_iter_advance(iter, sz - sizeof(*hdr));
+	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+		return -EFAULT;
 
 	return 0;
 }
Signature · 2 lines
-- 
2.47.1
#17 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

In reply to Akihiko Odaki
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

I'm not sure I will get here, could we do this in the series of hash reporting?

Quoted 15 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

---
 drivers/net/tun_vnet.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +0 -0
index fe842df9e9ef..ffb2186facd3 100644

drivers/net/tun_vnet.c int tun_vnet_hdr_put(int sz, struct iov_iter *iter, · line 144

Quoted 8 lines
drivers/net/tun_vnet.c +2 -1
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
        if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
                return -EFAULT;
 
-       iov_iter_advance(iter, sz - sizeof(*hdr));
+       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+               return -EFAULT;
 
        return 0;

There're various callers of iov_iter_advance(), do we need to fix them all?

Thanks

Quoted 6 lines
drivers/net/tun_vnet.c +0 -1
 }
 
 
--

2.47.1

#18 lore raw

Re: [PATCH v2 2/3] tun: Pad virtio header with zero

Quoted 11 lines · Jason Wang
Quoted 9 lines · Akihiko Odaki

tun used to simply advance iov_iter when it needs to pad virtio header, which leaves the garbage in the buffer as is. This is especially problematic when tun starts to allow enabling the hash reporting feature; even if the feature is enabled, the packet may lack a hash value and may contain a hole in the virtio header because the packet arrived before the feature gets enabled or does not contain the header fields to be hashed. If the hole is not filled with zero, it is impossible to tell if the packet lacks a hash value.

Quoted 1 line

I'm not sure I will get here, could we do this in the series of hash reporting?

I'll create another series dedicated for this and num_buffers change as suggested by Willem.

Quoted 19 lines
Quoted 19 lines

In theory, a user of tun can fill the buffer with zero before calling read() to avoid such a problem, but leaving the garbage in the buffer is awkward anyway so fill the buffer in tun.

---
  drivers/net/tun_vnet.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun_vnet.c +0 -0
index fe842df9e9ef..ffb2186facd3 100644
@@ -138,7 +138,8 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
         if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
                 return -EFAULT;
 
Quoted 7 lines

drivers/net/tun_vnet.c int tun_vnet_hdr_put(int sz, struct iov_iter *iter, · line 144

Quoted 5 lines
drivers/net/tun_vnet.c +2 -1
-       iov_iter_advance(iter, sz - sizeof(*hdr));
+       if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+               return -EFAULT;
 
         return 0;

There're various callers of iov_iter_advance(), do we need to fix them all?

No. For example, there are iov_iter_advance() calls for SOCK_ZEROCOPY in tun_get_user() and tap_get_user(). They are fine as they are not writing buffers after skipping.

The problem is that read_iter() and recvmsg() says it wrote N bytes but it leaves some of this N bytes uninialized. Such an implementation may be created even without iov_iter_advance() (for example just returning a too big number), and it is equally problematic with the current tun_get_user()/tap_get_user().

Regards,
Akihiko Odaki

Quoted 10 lines

Thanks

Quoted 6 lines
drivers/net/tun_vnet.c +0 -1
  }
 
 
--

2.47.1

#19 lore raw

PATCH v2 3/3 tun: Set num_buffers for virtio 1.0

In reply to Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

---
 drivers/net/tap.c      |  2 +-
 drivers/net/tun.c      |  6 ++++--
 drivers/net/tun_vnet.c | 14 +++++++++-----
 drivers/net/tun_vnet.h |  4 ++--
 4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
 	int total;
 
 	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
 
 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
 	size_t total;
 
 	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
 
 		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
 	}
 
 	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
 
 		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
 		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
 {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
 	if (iov_iter_count(iter) < sz)
 		return -EINVAL;
 
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
 		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
 		return -EFAULT;
 
 	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
 {
 	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
 				    tun_vnet_is_little_endian(flags), true,
 				    vlan_hlen)) {
 		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 		return -EINVAL;
 	}
 
+	hdr->num_buffers = 1;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 		     struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
 
 int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
 			const struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
 
 #endif /* TUN_VNET_H */
Signature · 1 line
2.47.1
#20 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 4 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

How do we know this is v1 and not v0? Confused.

Quoted 115 lines
---
 drivers/net/tap.c      |  2 +-
 drivers/net/tun.c      |  6 ++++--
 drivers/net/tun_vnet.c | 14 +++++++++-----
 drivers/net/tun_vnet.h |  4 ++--
 4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
 	int total;
 
 	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
 
 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
 	size_t total;
 
 	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
 
 		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
 	}
 
 	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
 
 		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
 		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
 {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
 	if (iov_iter_count(iter) < sz)
 		return -EINVAL;
 
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
 		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
 		return -EFAULT;
 
 	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
 {
 	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
 				    tun_vnet_is_little_endian(flags), true,
 				    vlan_hlen)) {
 		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 		return -EINVAL;
 	}
 
+	hdr->num_buffers = 1;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 		     struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
 
 int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
 			const struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
 
 #endif /* TUN_VNET_H */
Signature · 2 lines
-- 
2.47.1
#21 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

How do we know this is v1 and not v0? Confused.

Ah I got it, you assume userspace will over-write it if VIRTIO_NET_F_MRG_RXBUF is set.
If we are leaving this up to userspace, why not let it do it always?

Quoted 115 lines
Quoted 115 lines
---
 drivers/net/tap.c      |  2 +-
 drivers/net/tun.c      |  6 ++++--
 drivers/net/tun_vnet.c | 14 +++++++++-----
 drivers/net/tun_vnet.h |  4 ++--
 4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
 	int total;
 
 	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
 
 		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
 	size_t total;
 
 	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
 
 		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
 		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
 	}
 
 	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
 
 		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
 		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
 {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
 	if (iov_iter_count(iter) < sz)
 		return -EINVAL;
 
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
 		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
 		return -EFAULT;
 
 	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
 {
 	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
 				    tun_vnet_is_little_endian(flags), true,
 				    vlan_hlen)) {
 		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 		return -EINVAL;
 	}
 
+	hdr->num_buffers = 1;
+
 	return 0;
 }
 EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
 		     struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
 
 int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
 			const struct virtio_net_hdr *hdr);
 
 int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
 			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
 
 #endif /* TUN_VNET_H */
Signature · 2 lines
-- 
2.47.1
#22 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Quoted 6 lines
Quoted 1 line

How do we know this is v1 and not v0? Confused.

Ah I got it, you assume userspace will over-write it if VIRTIO_NET_F_MRG_RXBUF is set.
If we are leaving this up to userspace, why not let it do it always?

tun may be used with vhost_net, which does not set the field.

Quoted 117 lines
Quoted 116 lines
Quoted 116 lines
---
  drivers/net/tap.c      |  2 +-
  drivers/net/tun.c      |  6 ++++--
  drivers/net/tun_vnet.c | 14 +++++++++-----
  drivers/net/tun_vnet.h |  4 ++--
  4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
  	int total;
 
  	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
 
  		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
 
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
  	size_t total;
 
  	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
 
  		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
  	}
 
  	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
 
  		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
  		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
  EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
 
  int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
  {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
  	if (iov_iter_count(iter) < sz)
  		return -EINVAL;
 
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
  		return -EFAULT;
 
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
  		return -EFAULT;
 
  	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
 
  int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
  {
  	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
 
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
  				    tun_vnet_is_little_endian(flags), true,
  				    vlan_hlen)) {
  		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  		return -EINVAL;
  	}
 
+	hdr->num_buffers = 1;
+
  	return 0;
  }
  EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
  		     struct virtio_net_hdr *hdr);
 
  int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
 
  int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
  			const struct virtio_net_hdr *hdr);
 
  int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
 
  #endif /* TUN_VNET_H */
Signature · 2 lines
-- 
2.47.1
#23 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 9 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

How do we know this is v1 and not v0? Confused.

Ah I got it, you assume userspace will over-write it if VIRTIO_NET_F_MRG_RXBUF is set.
If we are leaving this up to userspace, why not let it do it always?

tun may be used with vhost_net, which does not set the field.

I'd fix that in vhost net.

Quoted 99 lines
Quoted 99 lines
Quoted 98 lines
Quoted 98 lines
---
  drivers/net/tap.c      |  2 +-
  drivers/net/tun.c      |  6 ++++--
  drivers/net/tun_vnet.c | 14 +++++++++-----
  drivers/net/tun_vnet.h |  4 ++--
  4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
  	int total;
  	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
  		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
  	size_t total;
  	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
  		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
  	}
  	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
  		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
  		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
  EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
  int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
  {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
  	if (iov_iter_count(iter) < sz)
  		return -EINVAL;
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
  		return -EFAULT;
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
  		return -EFAULT;
  	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
  int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
  {
  	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
  				    tun_vnet_is_little_endian(flags), true,
  				    vlan_hlen)) {
  		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  		return -EINVAL;
  	}
+	hdr->num_buffers = 1;
+
  	return 0;
  }
  EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
  		     struct virtio_net_hdr *hdr);
  int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
  int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
  			const struct virtio_net_hdr *hdr);
  int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
  			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
  #endif /* TUN_VNET_H */
 
Signature · 1 line
2.47.1
#24 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 12 lines · Michael S. Tsirkin
Quoted 11 lines · Akihiko Odaki
Quoted 10 lines · Michael S. Tsirkin
Quoted 8 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

How do we know this is v1 and not v0? Confused.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Ah I got it, you assume userspace will over-write it if VIRTIO_NET_F_MRG_RXBUF is set.
If we are leaving this up to userspace, why not let it do it always?

tun may be used with vhost_net, which does not set the field.

I'd fix that in vhost net.

Let's see what filesystem and networking people will say for the earlier patch. We can fix num_buffers for free if the earlier patch is getting merged. We will need to come up with another solution otherwise.

Quoted 102 lines
Quoted 101 lines
Quoted 101 lines
Quoted 100 lines
Quoted 100 lines


---
   drivers/net/tap.c      |  2 +-
   drivers/net/tun.c      |  6 ++++--
   drivers/net/tun_vnet.c | 14 +++++++++-----
   drivers/net/tun_vnet.h |  4 ++--
   4 files changed, 16 insertions(+), 10 deletions(-)
drivers/net/tap.c +1 -1
index 60804855510b..fe9554ee5b8b 100644
@@ -713,7 +713,7 @@ static ssize_t tap_put_user(struct tap_queue *q,
   	int total;
   	if (q->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr vnet_hdr;
+		struct virtio_net_hdr_v1 vnet_hdr;
   		vnet_hdr_len = READ_ONCE(q->vnet_hdr_sz);
drivers/net/tun.c +4 -2
index dbf0dee92e93..f211d0580887 100644
@@ -1991,7 +1991,9 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun,
   	size_t total;
   	if (tun->flags & IFF_VNET_HDR) {
-		struct virtio_net_hdr gso = { 0 };
+		struct virtio_net_hdr_v1 gso = {
+			.num_buffers = __virtio16_to_cpu(true, 1)
+		};
   		vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
   		ret = tun_vnet_hdr_put(vnet_hdr_sz, iter, &gso);
@@ -2044,7 +2046,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,
   	}
   	if (vnet_hdr_sz) {
-		struct virtio_net_hdr gso;
+		struct virtio_net_hdr_v1 gso;
   		ret = tun_vnet_hdr_from_skb(tun->flags, tun->dev, skb, &gso);
   		if (ret < 0)
drivers/net/tun_vnet.c +9 -5
index ffb2186facd3..a7a7989fae56 100644
@@ -130,15 +130,17 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
   EXPORT_SYMBOL_GPL(tun_vnet_hdr_get);
   int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr)
+		     const struct virtio_net_hdr_v1 *hdr)
   {
+	int content_sz = MIN(sizeof(*hdr), sz);
+
   	if (iov_iter_count(iter) < sz)
   		return -EINVAL;
-	if (copy_to_iter(hdr, sizeof(*hdr), iter) != sizeof(*hdr))
+	if (copy_to_iter(hdr, content_sz, iter) != content_sz)
   		return -EFAULT;
-	if (iov_iter_zero(sz - sizeof(*hdr), iter) != sz - sizeof(*hdr))
+	if (iov_iter_zero(sz - content_sz, iter) != sz - content_sz)
   		return -EFAULT;
   	return 0;
@@ -154,11 +156,11 @@ EXPORT_SYMBOL_GPL(tun_vnet_hdr_to_skb);
   int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
   			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr)
+			  struct virtio_net_hdr_v1 *hdr)
   {
   	int vlan_hlen = skb_vlan_tag_present(skb) ? VLAN_HLEN : 0;
-	if (virtio_net_hdr_from_skb(skb, hdr,
+	if (virtio_net_hdr_from_skb(skb, (struct virtio_net_hdr *)hdr,
   				    tun_vnet_is_little_endian(flags), true,
   				    vlan_hlen)) {
   		struct skb_shared_info *sinfo = skb_shinfo(skb);
@@ -176,6 +178,8 @@ int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
   		return -EINVAL;
   	}
+	hdr->num_buffers = 1;
+
   	return 0;
   }
   EXPORT_SYMBOL_GPL(tun_vnet_hdr_from_skb);
drivers/net/tun_vnet.h +2 -2
index 2dfdbe92bb24..d8fd94094227 100644
@@ -12,13 +12,13 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,
   		     struct virtio_net_hdr *hdr);
   int tun_vnet_hdr_put(int sz, struct iov_iter *iter,
-		     const struct virtio_net_hdr *hdr);
+		     const struct virtio_net_hdr_v1 *hdr);
   int tun_vnet_hdr_to_skb(unsigned int flags, struct sk_buff *skb,
   			const struct virtio_net_hdr *hdr);
   int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
   			  const struct sk_buff *skb,
-			  struct virtio_net_hdr *hdr);
+			  struct virtio_net_hdr_v1 *hdr);
   #endif /* TUN_VNET_H */
 
Signature · 1 line
2.47.1
#25 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

In reply to Akihiko Odaki
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

#26 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 9 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

My understanding is that we should fix the kernel and QEMU instead. There may be some driver implementations that assumes num_buffers is 1 so the kernel and QEMU should be fixed to be compatible with such potential implementations.

It is also possible to make future drivers with existing kernels and QEMU by ensuring they will not read num_buffers when VIRTIO_NET_F_MRG_RXBUF has not negotiated, and that's what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does.
https://lore.kernel.org/r/[email protected]

Regards,
Akihiko Odaki

#27 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

In reply to Jason Wang
Quoted 11 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

#28 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

#29 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 4 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

Quoted 3 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

Thanks

Quoted 1 line
#30 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 23 lines · Jason Wang
Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

The problem is:
1) On the device side, the num_buffers can be left uninitialized due to bugs
2) On the driver side, the specification allows assuming the num_buffers is set to one.

Relaxing the device requirement will replace "due to bugs" with "according to the specification" in 1). It still contradicts with 2) so does not fix compatibility.

Instead, we should make the driver requirement stricter to change 2). That is what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does:
https://lore.kernel.org/r/[email protected]

Quoted 6 lines
Quoted 4 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

It essentially only requires storing 16 bits. There are details we need to work out, but it should be possible to fix.

Regards,
Akihiko Odaki

#31 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 33 lines · Akihiko Odaki
Quoted 23 lines · Jason Wang
Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

Quoted 8 lines

The problem is:
1) On the device side, the num_buffers can be left uninitialized due to bugs
2) On the driver side, the specification allows assuming the num_buffers is set to one.

Relaxing the device requirement will replace "due to bugs" with "according to the specification" in 1). It still contradicts with 2) so does not fix compatibility.

Just to clarify I meant we can simply remove the following:

"""
The device MUST use only a single descriptor if VIRTIO_NET_F_MRG_RXBUF was not negotiated. Note: This means that num_buffers will always be 1 if VIRTIO_NET_F_MRG_RXBUF is not negotiated.
"""

And

"""
If VIRTIO_NET_F_MRG_RXBUF has not been negotiated, the device MUST set num_buffers to 1.
"""

This seems easier as it reflects the fact where some devices don't set it. And it eases the transitional device as it doesn't need to have any special care.

Then we don't need any driver normative so I don't see any conflict.

Michael suggests we use "SHOULD", but if this is something that the driver needs to be aware of I don't know how "SHOULD" can help a lot or not.

Quoted 7 lines

Instead, we should make the driver requirement stricter to change 2). That is what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does:
https://lore.kernel.org/r/[email protected]

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

It essentially only requires storing 16 bits. There are details we need to work out, but it should be possible to fix.

I meant it's not realistic to fix all the hypervisors. Note that modern devices have been implemented for about a decade so we may have too many versions of various hypervisors. (E.g DPDK seems to stick with the same behaviour of the current kernel).

Quoted 4 lines

Regards,
Akihiko Odaki

Thanks

#32 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 53 lines · Jason Wang
Quoted 33 lines · Akihiko Odaki
Quoted 23 lines · Jason Wang
Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

Quoted 8 lines

The problem is:
1) On the device side, the num_buffers can be left uninitialized due to bugs
2) On the driver side, the specification allows assuming the num_buffers is set to one.

Relaxing the device requirement will replace "due to bugs" with "according to the specification" in 1). It still contradicts with 2) so does not fix compatibility.

Just to clarify I meant we can simply remove the following:

"""
The device MUST use only a single descriptor if VIRTIO_NET_F_MRG_RXBUF was not negotiated. Note: This means that num_buffers will always be 1 if VIRTIO_NET_F_MRG_RXBUF is not negotiated.
"""

And

Quoted 8 lines

"""
If VIRTIO_NET_F_MRG_RXBUF has not been negotiated, the device MUST set num_buffers to 1.
"""

This seems easier as it reflects the fact where some devices don't set it. And it eases the transitional device as it doesn't need to have any special care.

That can potentially break existing drivers that are compliant with the current and assumes the num_buffers is set to 1.

Regards,
Akihiko Odaki

Quoted 33 lines

Then we don't need any driver normative so I don't see any conflict.

Michael suggests we use "SHOULD", but if this is something that the driver needs to be aware of I don't know how "SHOULD" can help a lot or not.

Quoted 7 lines

Instead, we should make the driver requirement stricter to change 2). That is what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does:
https://lore.kernel.org/r/[email protected]

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

It essentially only requires storing 16 bits. There are details we need to work out, but it should be possible to fix.

I meant it's not realistic to fix all the hypervisors. Note that modern devices have been implemented for about a decade so we may have too many versions of various hypervisors. (E.g DPDK seems to stick with the same behaviour of the current kernel).

Quoted 4 lines
Quoted 1 line

Regards,
Akihiko Odaki

Thanks

#33 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Jason Wang
Quoted 33 lines · Akihiko Odaki
Quoted 23 lines · Jason Wang
Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

Quoted 8 lines

The problem is:
1) On the device side, the num_buffers can be left uninitialized due to bugs
2) On the driver side, the specification allows assuming the num_buffers is set to one.

Relaxing the device requirement will replace "due to bugs" with "according to the specification" in 1). It still contradicts with 2) so does not fix compatibility.

Just to clarify I meant we can simply remove the following:

"""
The device MUST use only a single descriptor if VIRTIO_NET_F_MRG_RXBUF was not negotiated. Note: This means that num_buffers will always be 1 if VIRTIO_NET_F_MRG_RXBUF is not negotiated.
"""

And

"""
If VIRTIO_NET_F_MRG_RXBUF has not been negotiated, the device MUST set num_buffers to 1.
"""

Quoted 6 lines
Quoted 3 lines

This seems easier as it reflects the fact where some devices don't set it. And it eases the transitional device as it doesn't need to have any special care.

That can potentially break existing drivers that are compliant with the current and assumes the num_buffers is set to 1.

Those drivers are already 'broken'. Aren't they?

Thanks

Quoted 38 lines

Regards,
Akihiko Odaki

Quoted 33 lines

Then we don't need any driver normative so I don't see any conflict.

Michael suggests we use "SHOULD", but if this is something that the driver needs to be aware of I don't know how "SHOULD" can help a lot or not.

Quoted 7 lines

Instead, we should make the driver requirement stricter to change 2). That is what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does:
https://lore.kernel.org/r/[email protected]

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

It essentially only requires storing 16 bits. There are details we need to work out, but it should be possible to fix.

I meant it's not realistic to fix all the hypervisors. Note that modern devices have been implemented for about a decade so we may have too many versions of various hypervisors. (E.g DPDK seems to stick with the same behaviour of the current kernel).

Quoted 4 lines
Quoted 1 line

Regards,
Akihiko Odaki

Thanks

#34 lore raw

Re: [PATCH v2 3/3] tun: Set num_buffers for virtio 1.0

Quoted 61 lines · Jason Wang
Quoted 60 lines · Akihiko Odaki
Quoted 58 lines · Jason Wang
Quoted 33 lines · Akihiko Odaki
Quoted 23 lines · Jason Wang
Quoted 10 lines · Akihiko Odaki
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · Jason Wang
Quoted 3 lines · Akihiko Odaki

The specification says the device MUST set num_buffers to 1 if VIRTIO_NET_F_MRG_RXBUF has not been negotiated.

Have we agreed on how to fix the spec or not?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I replied in the spec patch, if we just remove this "MUST", it looks like we are all fine?

Thanks

We should replace MUST with SHOULD but it is not all fine, ignoring SHOULD is a quality of implementation issue.

So is this something that the driver should notice?

Quoted 1 line
Quoted 6 lines
Quoted 3 lines

Should we really replace it? It would mean that a driver conformant with the current specification may not be compatible with a device conformant with the future specification.

I don't get this. We are talking about devices and we want to relax so it should compatibile.

Quoted 8 lines

The problem is:
1) On the device side, the num_buffers can be left uninitialized due to bugs
2) On the driver side, the specification allows assuming the num_buffers is set to one.

Relaxing the device requirement will replace "due to bugs" with "according to the specification" in 1). It still contradicts with 2) so does not fix compatibility.

Just to clarify I meant we can simply remove the following:

"""
The device MUST use only a single descriptor if VIRTIO_NET_F_MRG_RXBUF was not negotiated. Note: This means that num_buffers will always be 1 if VIRTIO_NET_F_MRG_RXBUF is not negotiated.
"""

And

"""
If VIRTIO_NET_F_MRG_RXBUF has not been negotiated, the device MUST set num_buffers to 1.
"""

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

This seems easier as it reflects the fact where some devices don't set it. And it eases the transitional device as it doesn't need to have any special care.

That can potentially break existing drivers that are compliant with the current and assumes the num_buffers is set to 1.

Those drivers are already 'broken'. Aren't they?

The drivers are not broken, but vhost_net is. The driver works fine as long as it's used with a device compliant with the specification. If we relax the device requirement in the future specification, the drivers may not work with devices compliant with the revised specification.

Regards,
Akihiko Odaki

Quoted 42 lines

Thanks

Quoted 38 lines

Regards,
Akihiko Odaki

Quoted 33 lines

Then we don't need any driver normative so I don't see any conflict.

Michael suggests we use "SHOULD", but if this is something that the driver needs to be aware of I don't know how "SHOULD" can help a lot or not.

Quoted 7 lines

Instead, we should make the driver requirement stricter to change 2). That is what "[PATCH v3] virtio-net: Ignore num_buffers when unused" does:
https://lore.kernel.org/r/[email protected]

Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We are going to fix all implementations known to buggy (QEMU and Linux) anyway so I think it's just fine to leave that part of specification as is.

I don't think we can fix it all.

It essentially only requires storing 16 bits. There are details we need to work out, but it should be possible to fix.

I meant it's not realistic to fix all the hypervisors. Note that modern devices have been implemented for about a decade so we may have too many versions of various hypervisors. (E.g DPDK seems to stick with the same behaviour of the current kernel).

Quoted 4 lines
Quoted 1 line

Regards,
Akihiko Odaki

Thanks

maintainer
#35 lore raw
In reply to Akihiko Odaki
Quoted 22 lines · Akihiko Odaki

When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().

This series fills the uninitialized hole. More concretely, the num_buffers field will be initialized with 1, and the other fields will be inialized with 0. Setting the num_buffers field to 1 is mandated by virtio 1.0 [2].

The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.

[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/

---
Changes in v2:
- Fixed num_buffers endian.
- Link to v1: https://lore.kernel.org/r/[email protected]

Quoted 5 lines

---
Akihiko Odaki (3):
tun: Unify vnet implementation
tun: Pad virtio header with zero
tun: Set num_buffers for virtio 1.0

Patches should explicitly to net or net-next.

In this case if the undefined data would be a bug, that would target net. It sounds as if this is only relevant with the upcoming hash changes, so then it too can target net-next. If needed at all.

The first patch is clearly net-next material.

I would prefer to work on that independent from the rest. I'm in favor of deduplicating logic across tun/tap/pf_packet. Have taken a stab, but haven't gotten to a concrete series. This indeed a valid deduplication effort.

We have to make sure that the code is identical between tun and tap, or where it isn't (due to one of the two having received a change to such code, but the other not) explicitly note that in the commit message. As then it is a behavioral change.

Anyway, let's send the undefined data, hash and dedup changes independently. And preferably one after the other, rather than having concurrent conversations across threads.