Skip to contentSkip to conversation

net/virtio: fix virtio-user init when using existing tap

PATCH Applied David Marchand 4 participants 8 messages Sep 17, 2021 – Sep 30, 2021 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net/virtio: fix virtio-user init when using existing tap

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
  net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

---
 drivers/net/virtio/virtio_user/vhost_kernel.c |  89 +++++----
 .../net/virtio/virtio_user/vhost_kernel_tap.c | 173 +++++++++---------
 .../net/virtio/virtio_user/vhost_kernel_tap.h |  16 +-
 3 files changed, 143 insertions(+), 135 deletions(-)
drivers/net/virtio/virtio_user/vhost_kernel.c +53 -36
index d65f89e1fc..91c523df76 100644
@@ -120,9 +120,9 @@ vhost_kernel_set_owner(struct virtio_user_dev *dev)
 static int
 vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 {
-	int ret;
-	unsigned int tap_features;
 	struct vhost_kernel_data *data = dev->backend_data;
+	unsigned int tap_flags;
+	int ret;
 
 	ret = vhost_kernel_ioctl(data->vhostfds[0], VHOST_GET_FEATURES, features);
 	if (ret < 0) {
@@ -130,7 +130,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 		return -1;
 	}
 
-	ret = tap_support_features(&tap_features);
+	ret = tap_get_flags(data->tapfds[0], &tap_flags);
 	if (ret < 0) {
 		PMD_DRV_LOG(ERR, "Failed to get TAP features");
 		return -1;
@@ -140,7 +140,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 	 * but not claimed by vhost-net, so we add them back when
 	 * reporting to upper layer.
 	 */
-	if (tap_features & IFF_VNET_HDR) {
+	if (tap_flags & IFF_VNET_HDR) {
 		*features |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
 		*features |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
 	}
@@ -148,7 +148,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 	/* vhost_kernel will not declare this feature, but it does
 	 * support multi-queue.
 	 */
-	if (tap_features & IFF_MULTI_QUEUE)
+	if (tap_flags & IFF_MULTI_QUEUE)
 		*features |= (1ull << VIRTIO_NET_F_MQ);
 
 	return 0;
@@ -380,9 +380,20 @@ vhost_kernel_set_status(struct virtio_user_dev *dev __rte_unused, uint8_t status
 static int
 vhost_kernel_setup(struct virtio_user_dev *dev)
 {
-	int vhostfd;
-	uint32_t q, i;
 	struct vhost_kernel_data *data;
+	unsigned int tap_features;
+	unsigned int tap_flags;
+	const char *ifname;
+	uint32_t q, i;
+	int vhostfd;
+
+	if (tap_support_features(&tap_features) < 0)
+		return -1;
+
+	if ((tap_features & IFF_VNET_HDR) == 0) {
+		PMD_INIT_LOG(ERR, "TAP does not support IFF_VNET_HDR");
+		return -1;
+	}
 
 	data = malloc(sizeof(*data));
 	if (!data) {
@@ -414,18 +425,40 @@ vhost_kernel_setup(struct virtio_user_dev *dev)
 			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
 			goto err_tapfds;
 		}
-
 		data->vhostfds[i] = vhostfd;
 	}
 
+	ifname = dev->ifname != NULL ? dev->ifname : "tap%d";
+	if (tap_features & IFF_MULTI_QUEUE)
+		data->tapfds[0] = tap_open(ifname, true);
+	if (data->tapfds[0] < 0)
+		data->tapfds[0] = tap_open(ifname, false);
+	if (data->tapfds[0] < 0)
+		goto err_tapfds;
+	if (tap_get_flags(data->tapfds[0], &tap_flags) < 0)
+		goto err_tapfds;
+	if ((tap_flags & IFF_MULTI_QUEUE) == 0 && dev->max_queue_pairs > 1)
+		goto err_tapfds;
+	if (dev->ifname == NULL && tap_get_name(data->tapfds[0], &dev->ifname) < 0)
+		goto err_tapfds;
+
+	for (i = 1; i < dev->max_queue_pairs; i++) {
+		data->tapfds[i] = tap_open(dev->ifname, true);
+		if (data->tapfds[i] < 0)
+			goto err_tapfds;
+	}
+
 	dev->backend_data = data;
 
 	return 0;
 
 err_tapfds:
-	for (i = 0; i < dev->max_queue_pairs; i++)
+	for (i = 0; i < dev->max_queue_pairs; i++) {
 		if (data->vhostfds[i] >= 0)
 			close(data->vhostfds[i]);
+		if (data->tapfds[i] >= 0)
+			close(data->tapfds[i]);
+	}
 
 	free(data->tapfds);
 err_vhostfds:
@@ -488,58 +521,42 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
 			       uint16_t pair_idx,
 			       int enable)
 {
+	struct vhost_kernel_data *data = dev->backend_data;
 	int hdr_size;
 	int vhostfd;
 	int tapfd;
-	int req_mq = (dev->max_queue_pairs > 1);
-	struct vhost_kernel_data *data = dev->backend_data;
-
-	vhostfd = data->vhostfds[pair_idx];
 
 	if (dev->qp_enabled[pair_idx] == enable)
 		return 0;
 
+	vhostfd = data->vhostfds[pair_idx];
+	tapfd = data->tapfds[pair_idx];
+
 	if (!enable) {
-		tapfd = data->tapfds[pair_idx];
 		if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
 			PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
 			return -1;
 		}
-		if (req_mq && vhost_kernel_tap_set_queue(tapfd, false) < 0) {
-			PMD_DRV_LOG(ERR, "fail to disable tap for vhost kernel");
-			return -1;
-		}
 		dev->qp_enabled[pair_idx] = false;
 		return 0;
 	}
 
-	if (data->tapfds[pair_idx] >= 0) {
-		tapfd = data->tapfds[pair_idx];
-		if (vhost_kernel_tap_set_offload(tapfd, dev->features) == -1)
-			return -1;
-		if (req_mq && vhost_kernel_tap_set_queue(tapfd, true) < 0) {
-			PMD_DRV_LOG(ERR, "fail to enable tap for vhost kernel");
-			return -1;
-		}
-		goto set_backend;
-	}
-
 	if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
 	    (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
 		hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
 	else
 		hdr_size = sizeof(struct virtio_net_hdr);
 
-	tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
-			 (char *)dev->mac_addr, dev->features);
-	if (tapfd < 0) {
-		PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
+	/* Set mac on tap only once when starting */
+	if (!dev->started && pair_idx == 0 &&
+			tap_set_mac(data->tapfds[pair_idx], dev->mac_addr) < 0)
 		return -1;
-	}
 
-	data->tapfds[pair_idx] = tapfd;
+	if (vhost_kernel_tap_setup(tapfd, hdr_size, dev->features) < 0) {
+		PMD_DRV_LOG(ERR, "fail to setup tap for vhost kernel");
+		return -1;
+	}
 
-set_backend:
 	if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
 		PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
 		return -1;
drivers/net/virtio/virtio_user/vhost_kernel_tap.c +84 -89

200 lines not on this page · raw message

drivers/net/virtio/virtio_user/vhost_kernel_tap.h +6 -10
index ed03fce21e..726433c48c 100644
@@ -16,18 +16,12 @@
 #define TUNSETSNDBUF   _IOW('T', 212, int)
 #define TUNGETVNETHDRSZ _IOR('T', 215, int)
 #define TUNSETVNETHDRSZ _IOW('T', 216, int)
-#define TUNSETQUEUE  _IOW('T', 217, int)
-#define TUNSETVNETLE _IOW('T', 220, int)
-#define TUNSETVNETBE _IOW('T', 222, int)
 
 /* TUNSETIFF ifr flags */
 #define IFF_TAP          0x0002
 #define IFF_NO_PI        0x1000
-#define IFF_ONE_QUEUE    0x2000
 #define IFF_VNET_HDR     0x4000
 #define IFF_MULTI_QUEUE  0x0100
-#define IFF_ATTACH_QUEUE 0x0200
-#define IFF_DETACH_QUEUE 0x0400
 
 /* Features for GSO (TUNSETOFFLOAD). */
 #define TUN_F_CSUM	0x01	/* You can hand me unchecksummed packets. */
@@ -39,10 +33,12 @@
 /* Constants */
 #define PATH_NET_TUN	"/dev/net/tun"
 
-int vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
-			 const char *mac, uint64_t features);
-int vhost_kernel_tap_set_offload(int fd, uint64_t features);
-int vhost_kernel_tap_set_queue(int fd, bool attach);
+int vhost_kernel_tap_setup(int tapfd, int hdr_size, uint64_t features);
+
 int tap_support_features(unsigned int *tap_features);
+int tap_open(const char *ifname, bool multi_queue);
+int tap_get_name(int tapfd, char **ifname);
+int tap_get_flags(int tapfd, unsigned int *tap_flags);
+int tap_set_mac(int tapfd, uint8_t *mac);
 
 #endif
Signature · 1 line
2.23.0
#1 lore raw

Hi David,

Quoted 49 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
  net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

Quoted 7 lines

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

It also solves our use-case, which is a bit different:

- our application creates a tap with the IFF_MULTI_QUEUE
- we bind this tap to a virtio-user pmd

Previously, it was not working when passing queues=1 to the pmd, the TUNSETIFF ioctl() in the pmd was failing because the IFF_MULTI_QUEUE flag was not passed.

With your patch, it works fine, thanks.

(...)

drivers/net/virtio/virtio_user/vhost_kernel.c vhost_kernel_setup(struct virtio_user_dev *dev) · line 435

Quoted 13 lines
drivers/net/virtio/virtio_user/vhost_kernel.c +5 -1
@@ -414,18 +425,40 @@ vhost_kernel_setup(struct virtio_user_dev *dev)
 			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
 			goto err_tapfds;
 		}
-
 		data->vhostfds[i] = vhostfd;
 	}
 
+	ifname = dev->ifname != NULL ? dev->ifname : "tap%d";
+	if (tap_features & IFF_MULTI_QUEUE)
+		data->tapfds[0] = tap_open(ifname, true);
+	if (data->tapfds[0] < 0)
+		data->tapfds[0] = tap_open(ifname, false);

As discussed off-list, since tap_open() is called twice, it can in some conditions log failures twice. And since tun/tap multi queue feature is available from 3.8 kernel, we may not want to bother with that.

Olivier

#2 lore raw
Quoted 63 lines · Olivier Matz

Hi David,

Quoted 49 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
  net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

Quoted 7 lines

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

It also solves our use-case, which is a bit different:

Quoted 8 lines

- our application creates a tap with the IFF_MULTI_QUEUE
- we bind this tap to a virtio-user pmd

Previously, it was not working when passing queues=1 to the pmd, the TUNSETIFF ioctl() in the pmd was failing because the IFF_MULTI_QUEUE flag was not passed.

With your patch, it works fine, thanks.

Cool.

Quoted 11 lines

(...)

Quoted 8 lines
drivers/net/virtio/virtio_user/vhost_kernel.c +0 -1
@@ -414,18 +425,40 @@ vhost_kernel_setup(struct virtio_user_dev *dev)
                      PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
                      goto err_tapfds;
              }
-
              data->vhostfds[i] = vhostfd;
      }
 
Quoted 9 lines

drivers/net/virtio/virtio_user/vhost_kernel.c vhost_kernel_setup(struct virtio_user_dev *dev) · line 435

Quoted 5 lines
drivers/net/virtio/virtio_user/vhost_kernel.c +5 -0
+     ifname = dev->ifname != NULL ? dev->ifname : "tap%d";
+     if (tap_features & IFF_MULTI_QUEUE)
+             data->tapfds[0] = tap_open(ifname, true);
+     if (data->tapfds[0] < 0)
+             data->tapfds[0] = tap_open(ifname, false);

As discussed off-list, since tap_open() is called twice, it can in some conditions log failures twice. And since tun/tap multi queue feature is available from 3.8 kernel, we may not want to bother with that.

Let me relook at this for v2.

Thanks for the test!

Signature · 1 line
David Marchand
#3 lore raw

PATCH v2 1/1 net/virtio: fix virtio-user init when using existing tap

In reply to David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
  net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

---
Changes since v1:
- refactored tap_open() following Olivier comment and updated log messages level accordingly,
- added more error logs,

---

 drivers/net/virtio/virtio_user/vhost_kernel.c |  92 +++++----
 .../net/virtio/virtio_user/vhost_kernel_tap.c | 180 +++++++++---------
 .../net/virtio/virtio_user/vhost_kernel_tap.h |  16 +-
 3 files changed, 153 insertions(+), 135 deletions(-)
drivers/net/virtio/virtio_user/vhost_kernel.c +56 -36
index d65f89e1fc..202a8cdee1 100644
@@ -120,9 +120,9 @@ vhost_kernel_set_owner(struct virtio_user_dev *dev)
 static int
 vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 {
-	int ret;
-	unsigned int tap_features;
 	struct vhost_kernel_data *data = dev->backend_data;
+	unsigned int tap_flags;
+	int ret;
 
 	ret = vhost_kernel_ioctl(data->vhostfds[0], VHOST_GET_FEATURES, features);
 	if (ret < 0) {
@@ -130,7 +130,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 		return -1;
 	}
 
-	ret = tap_support_features(&tap_features);
+	ret = tap_get_flags(data->tapfds[0], &tap_flags);
 	if (ret < 0) {
 		PMD_DRV_LOG(ERR, "Failed to get TAP features");
 		return -1;
@@ -140,7 +140,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 	 * but not claimed by vhost-net, so we add them back when
 	 * reporting to upper layer.
 	 */
-	if (tap_features & IFF_VNET_HDR) {
+	if (tap_flags & IFF_VNET_HDR) {
 		*features |= VHOST_KERNEL_GUEST_OFFLOADS_MASK;
 		*features |= VHOST_KERNEL_HOST_OFFLOADS_MASK;
 	}
@@ -148,7 +148,7 @@ vhost_kernel_get_features(struct virtio_user_dev *dev, uint64_t *features)
 	/* vhost_kernel will not declare this feature, but it does
 	 * support multi-queue.
 	 */
-	if (tap_features & IFF_MULTI_QUEUE)
+	if (tap_flags & IFF_MULTI_QUEUE)
 		*features |= (1ull << VIRTIO_NET_F_MQ);
 
 	return 0;
@@ -380,9 +380,20 @@ vhost_kernel_set_status(struct virtio_user_dev *dev __rte_unused, uint8_t status
 static int
 vhost_kernel_setup(struct virtio_user_dev *dev)
 {
-	int vhostfd;
-	uint32_t q, i;
 	struct vhost_kernel_data *data;
+	unsigned int tap_features;
+	unsigned int tap_flags;
+	const char *ifname;
+	uint32_t q, i;
+	int vhostfd;
+
+	if (tap_support_features(&tap_features) < 0)
+		return -1;
+
+	if ((tap_features & IFF_VNET_HDR) == 0) {
+		PMD_INIT_LOG(ERR, "TAP does not support IFF_VNET_HDR");
+		return -1;
+	}
 
 	data = malloc(sizeof(*data));
 	if (!data) {
@@ -414,18 +425,43 @@ vhost_kernel_setup(struct virtio_user_dev *dev)
 			PMD_DRV_LOG(ERR, "fail to open %s, %s", dev->path, strerror(errno));
 			goto err_tapfds;
 		}
-
 		data->vhostfds[i] = vhostfd;
 	}
 
+	ifname = dev->ifname != NULL ? dev->ifname : "tap%d";
+	data->tapfds[0] = tap_open(ifname, (tap_features & IFF_MULTI_QUEUE) != 0);
+	if (data->tapfds[0] < 0)
+		goto err_tapfds;
+	if (dev->ifname == NULL && tap_get_name(data->tapfds[0], &dev->ifname) < 0) {
+		PMD_DRV_LOG(ERR, "fail to get tap name (%d)", data->tapfds[0]);
+		goto err_tapfds;
+	}
+	if (tap_get_flags(data->tapfds[0], &tap_flags) < 0) {
+		PMD_DRV_LOG(ERR, "fail to get tap flags for tap %s", dev->ifname);
+		goto err_tapfds;
+	}
+	if ((tap_flags & IFF_MULTI_QUEUE) == 0 && dev->max_queue_pairs > 1) {
+		PMD_DRV_LOG(ERR, "tap %s does not support multi queue", dev->ifname);
+		goto err_tapfds;
+	}
+
+	for (i = 1; i < dev->max_queue_pairs; i++) {
+		data->tapfds[i] = tap_open(dev->ifname, true);
+		if (data->tapfds[i] < 0)
+			goto err_tapfds;
+	}
+
 	dev->backend_data = data;
 
 	return 0;
 
 err_tapfds:
-	for (i = 0; i < dev->max_queue_pairs; i++)
+	for (i = 0; i < dev->max_queue_pairs; i++) {
 		if (data->vhostfds[i] >= 0)
 			close(data->vhostfds[i]);
+		if (data->tapfds[i] >= 0)
+			close(data->tapfds[i]);
+	}
 
 	free(data->tapfds);
 err_vhostfds:
@@ -488,58 +524,42 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
 			       uint16_t pair_idx,
 			       int enable)
 {
+	struct vhost_kernel_data *data = dev->backend_data;
 	int hdr_size;
 	int vhostfd;
 	int tapfd;
-	int req_mq = (dev->max_queue_pairs > 1);
-	struct vhost_kernel_data *data = dev->backend_data;
-
-	vhostfd = data->vhostfds[pair_idx];
 
 	if (dev->qp_enabled[pair_idx] == enable)
 		return 0;
 
+	vhostfd = data->vhostfds[pair_idx];
+	tapfd = data->tapfds[pair_idx];
+
 	if (!enable) {
-		tapfd = data->tapfds[pair_idx];
 		if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
 			PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
 			return -1;
 		}
-		if (req_mq && vhost_kernel_tap_set_queue(tapfd, false) < 0) {
-			PMD_DRV_LOG(ERR, "fail to disable tap for vhost kernel");
-			return -1;
-		}
 		dev->qp_enabled[pair_idx] = false;
 		return 0;
 	}
 
-	if (data->tapfds[pair_idx] >= 0) {
-		tapfd = data->tapfds[pair_idx];
-		if (vhost_kernel_tap_set_offload(tapfd, dev->features) == -1)
-			return -1;
-		if (req_mq && vhost_kernel_tap_set_queue(tapfd, true) < 0) {
-			PMD_DRV_LOG(ERR, "fail to enable tap for vhost kernel");
-			return -1;
-		}
-		goto set_backend;
-	}
-
 	if ((dev->features & (1ULL << VIRTIO_NET_F_MRG_RXBUF)) ||
 	    (dev->features & (1ULL << VIRTIO_F_VERSION_1)))
 		hdr_size = sizeof(struct virtio_net_hdr_mrg_rxbuf);
 	else
 		hdr_size = sizeof(struct virtio_net_hdr);
 
-	tapfd = vhost_kernel_open_tap(&dev->ifname, hdr_size, req_mq,
-			 (char *)dev->mac_addr, dev->features);
-	if (tapfd < 0) {
-		PMD_DRV_LOG(ERR, "fail to open tap for vhost kernel");
+	/* Set mac on tap only once when starting */
+	if (!dev->started && pair_idx == 0 &&
+			tap_set_mac(data->tapfds[pair_idx], dev->mac_addr) < 0)
 		return -1;
-	}
 
-	data->tapfds[pair_idx] = tapfd;
+	if (vhost_kernel_tap_setup(tapfd, hdr_size, dev->features) < 0) {
+		PMD_DRV_LOG(ERR, "fail to setup tap for vhost kernel");
+		return -1;
+	}
 
-set_backend:
 	if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
 		PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
 		return -1;
drivers/net/virtio/virtio_user/vhost_kernel_tap.c +91 -89

207 lines not on this page · raw message

drivers/net/virtio/virtio_user/vhost_kernel_tap.h +6 -10
index ed03fce21e..726433c48c 100644
@@ -16,18 +16,12 @@
 #define TUNSETSNDBUF   _IOW('T', 212, int)
 #define TUNGETVNETHDRSZ _IOR('T', 215, int)
 #define TUNSETVNETHDRSZ _IOW('T', 216, int)
-#define TUNSETQUEUE  _IOW('T', 217, int)
-#define TUNSETVNETLE _IOW('T', 220, int)
-#define TUNSETVNETBE _IOW('T', 222, int)
 
 /* TUNSETIFF ifr flags */
 #define IFF_TAP          0x0002
 #define IFF_NO_PI        0x1000
-#define IFF_ONE_QUEUE    0x2000
 #define IFF_VNET_HDR     0x4000
 #define IFF_MULTI_QUEUE  0x0100
-#define IFF_ATTACH_QUEUE 0x0200
-#define IFF_DETACH_QUEUE 0x0400
 
 /* Features for GSO (TUNSETOFFLOAD). */
 #define TUN_F_CSUM	0x01	/* You can hand me unchecksummed packets. */
@@ -39,10 +33,12 @@
 /* Constants */
 #define PATH_NET_TUN	"/dev/net/tun"
 
-int vhost_kernel_open_tap(char **p_ifname, int hdr_size, int req_mq,
-			 const char *mac, uint64_t features);
-int vhost_kernel_tap_set_offload(int fd, uint64_t features);
-int vhost_kernel_tap_set_queue(int fd, bool attach);
+int vhost_kernel_tap_setup(int tapfd, int hdr_size, uint64_t features);
+
 int tap_support_features(unsigned int *tap_features);
+int tap_open(const char *ifname, bool multi_queue);
+int tap_get_name(int tapfd, char **ifname);
+int tap_get_flags(int tapfd, unsigned int *tap_flags);
+int tap_set_mac(int tapfd, uint8_t *mac);
 
 #endif
Signature · 1 line
2.23.0
Reviewed-by on v2 1/1
#4 lore raw
Quoted 62 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
   net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

---
Changes since v1:
- refactored tap_open() following Olivier comment and updated log messages level accordingly,
- added more error logs,

Quoted 7 lines

---

  drivers/net/virtio/virtio_user/vhost_kernel.c |  92 +++++----
  .../net/virtio/virtio_user/vhost_kernel_tap.c | 180 +++++++++---------
  .../net/virtio/virtio_user/vhost_kernel_tap.h |  16 +-
  3 files changed, 153 insertions(+), 135 deletions(-)

Nice, thanks for the detailed commit message.

Thanks,
Maxime

#5 lore raw
In reply to David Marchand
Quoted 49 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
  net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

Quoted 7 lines

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

Do we want to backport this patch? If so can you please provide a fixes tag?

#6 lore raw
Quoted 53 lines · Ferruh Yigit
Quoted 52 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
   net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Quoted 7 lines
Quoted 4 lines

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

Do we want to backport this patch? If so can you please provide a fixes tag?

No, we don't want to backport it.

Thanks,
Maxime

#7 lore raw
In reply to David Marchand
Quoted 55 lines · David Marchand

When attaching to an existing mono queue tap, the virtio-user was not reporting that the virtio device was not properly initialised which prevented from starting the port later.

$ ip tuntap add test mode tap
$ dpdk-testpmd --vdev \
   net_virtio_user0,iface=test,path=/dev/vhost-net,queues=2 -- -i

...
virtio_user_dev_init_mac(): (/dev/vhost-net) No valid MAC in devargs or device, use random
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_user_start_device(): (/dev/vhost-net) Failed to start device
...
Configuring Port 0 (socket 0)
vhost_kernel_open_tap(): TUNSETIFF failed: Invalid argument vhost_kernel_enable_queue_pair(): fail to open tap for vhost kernel virtio_set_multiple_queues(): Multiqueue configured but send command failed, this is too late now...
Fail to start port 0: Invalid argument
Please stop the ports first
Done

The virtio-user with vhost-kernel backend was going through a lot of complications to initialise tap fds only when using them.

For each qp enabled for the first time, a tapfd was created via TUNSETIFF with unneeded additional steps (see below) and then mapped to the right qp in the vhost-net backend.
Unneeded steps (as long as it has been done once for the port):
- tap features were queried while this is a constant on a running system,
- the device name in DPDK was updated,
- the mac address of the tap was set,

On subsequent qps state change, the vhost-net backend fd mapping was updated and the associated queue/tapfd were disabled/enabled via TUNSETQUEUE.

Now, this patch simplifies the whole logic by keeping all tapfds opened and in enabled state (from the tap point of view) at all time.

Unused ioctl defines are removed.

Tap features are validated earlier to fail initialisation asap. Tap name discovery and mac address configuration are moved when configuring qp 0.

To support attaching to mono queue tap, the virtio-user driver now tries to attach in multi queue first, then fallbacks to mono queue.

Finally (but this is more for consistency), VIRTIO_NET_F_MQ feature is exposed only if the underlying tap supports multi queue.

Quoted 7 lines
---
  drivers/net/virtio/virtio_user/vhost_kernel.c |  89 +++++----
  .../net/virtio/virtio_user/vhost_kernel_tap.c | 173 +++++++++---------
  .../net/virtio/virtio_user/vhost_kernel_tap.h |  16 +-
  3 files changed, 143 insertions(+), 135 deletions(-)

Applied to dpdk-next-virtio/main.

Thanks,
Maxime