A virtio-user port on vhost-net loses received packets when the
application configures fewer Rx queues than the port has queue pairs
(the queues devarg). For example, with queues=2 and one Rx queue,
testpmd receives 22 of 64 flows that the kernel sends on the TAP
interface.
The TAP interface has one queue per queue pair, and the kernel picks
the queue of a packet by a hash of its flow. The driver unsets the
vhost backend of a disabled queue pair, but the TAP queue stays
attached. So the kernel still sends flows to it, and nothing reads
them.
The driver used to detach such a queue with TUNSETQUEUE. The commit
below removed that when it moved the opening of the TAP queues to
the initialization.
Detach the TAP queue of a queue pair when it is disabled, and attach
it when it is enabled. The queues after the first one are detached
when they are opened, because a queue pair is disabled until the
driver enables it. The first queue is never detached, because the
interface needs a queue.
A queue pair is also disabled for a moment when the memory table
changes. Its TAP queue is then detached and attached again.
Fixes: b72099be7f27 ("net/virtio-user: fix init when using existing tap")
Cc: [email protected]
Signed-off-by: Isaac Boukris <[email protected]>
---
v3: new patch.
drivers/net/virtio/virtio_user/vhost_kernel.c | 12 ++++++++++++
drivers/net/virtio/virtio_user/vhost_kernel_tap.c | 14 ++++++++++++++
drivers/net/virtio/virtio_user/vhost_kernel_tap.h | 4 ++++
3 files changed, 30 insertions(+)
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c
b/drivers/net/virtio/virtio_user/vhost_kernel.c
index 7914b517cd..5b70999393 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel.c
@@ -455,6 +455,9 @@ vhost_kernel_setup(struct virtio_user_dev *dev)
data->tapfds[i] = vhost_tap_open(dev->ifname, r_flags, true);
if (data->tapfds[i] < 0)
goto err_tapfds;
+ /* The queue stays detached until its queue pair is enabled. */
+ if (vhost_tap_set_queue(data->tapfds[i], false) < 0)
+ goto err_tapfds;
}
dev->backend_data = data;
@@ -546,6 +549,12 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
PMD_DRV_LOG(ERR, "fail to set backend for vhost
kernel");
return -1;
}
+ /*
+ * Detach the queue, so that the kernel sends its packets to the
+ * other queues. The interface keeps its first queue.
+ */
+ if (pair_idx > 0 && vhost_tap_set_queue(tapfd, false) < 0)
+ return -1;
dev->qp_enabled[pair_idx] = false;
return 0;
}
@@ -561,6 +570,9 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev *dev,
vhost_tap_set_mac(data->tapfds[pair_idx], dev->mac_addr) < 0)
return -1;
+ if (pair_idx > 0 && vhost_tap_set_queue(tapfd, true) < 0)
+ return -1;
+
if (vhost_kernel_tap_setup(tapfd, hdr_size, dev->features) < 0) {
PMD_DRV_LOG(ERR, "fail to setup tap for vhost kernel");
return -1;
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
index da55aba505..bfdc6f46c0 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.c
@@ -126,6 +126,20 @@ vhost_tap_set_mac(int tapfd, uint8_t *mac)
return 0;
}
+int
+vhost_tap_set_queue(int tapfd, bool attach)
+{
+ struct ifreq ifr;
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = attach ? IFF_ATTACH_QUEUE : IFF_DETACH_QUEUE;
+ if (ioctl(tapfd, TUNSETQUEUE, (void *)&ifr) == -1) {
+ PMD_DRV_LOG(ERR, "TUNSETQUEUE failed: %s", strerror(errno));
+ return -1;
+ }
+ return 0;
+}
+
static int
vhost_kernel_tap_set_offload(int fd, uint64_t features)
{
diff --git a/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
b/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
index 1e026d7488..05fa8a9244 100644
--- a/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
+++ b/drivers/net/virtio/virtio_user/vhost_kernel_tap.h
@@ -16,12 +16,15 @@
#define TUNSETSNDBUF _IOW('T', 212, int)
#define TUNGETVNETHDRSZ _IOR('T', 215, int)
#define TUNSETVNETHDRSZ _IOW('T', 216, int)
+#define TUNSETQUEUE _IOW('T', 217, int)
/* TUNSETIFF ifr flags */
#define IFF_TAP 0x0002
#define IFF_NO_PI 0x1000
#define IFF_VNET_HDR 0x4000
#define IFF_MULTI_QUEUE 0x0100
+#define IFF_ATTACH_QUEUE 0x0200
+#define IFF_DETACH_QUEUE 0x0400
#define IFF_NAPI 0x0010
/* Features for GSO (TUNSETOFFLOAD). */
@@ -41,5 +44,6 @@ int vhost_tap_open(const char *ifname, unsigned int r_flags,
bool multi_queue);
int vhost_tap_get_name(int tapfd, char **ifname);
int vhost_tap_get_flags(int tapfd, unsigned int *tap_flags);
int vhost_tap_set_mac(int tapfd, uint8_t *mac);
+int vhost_tap_set_queue(int tapfd, bool attach);
#endif
--
2.56.1