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). With queues=2 and one Rx queue, testpmd receives
22 of the 64 flows that the kernel sends on the TAP interface.

The TAP interface has one queue per queue pair, and the kernel picks
a packet's queue by a hash of its flow. The driver unsets the vhost
backend of a disabled queue pair but leaves its TAP queue attached,
so the kernel still sends flows to it and nothing reads them.

The driver detached such a queue with TUNSETQUEUE until the commit
below, which opens all the TAP queues at initialization.

Attach the TAP queue of a queue pair when the pair is enabled, and
detach it when the pair is disabled or when a later step of the
enable fails. Detach the queues after the first when they are opened.
The first one stays attached, because the interface needs a queue.

TUNSETQUEUE fails on a queue that is already in the requested state.
The existing check of qp_enabled prevents that, because the state of
the TAP queue now follows it.

A memory table change disables the active queue pairs for a moment,
so their TAP queues are 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]>
---
v4: detach the queue again when a later step of the enable fails. One comment 
says the rule.
v3: new patch.

 drivers/net/virtio/virtio_user/vhost_kernel.c | 24 +++++++++++++++++--
 .../net/virtio/virtio_user/vhost_kernel_tap.c | 14 +++++++++++
 .../net/virtio/virtio_user/vhost_kernel_tap.h |  4 ++++
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/drivers/net/virtio/virtio_user/vhost_kernel.c 
b/drivers/net/virtio/virtio_user/vhost_kernel.c
index 7914b517cd..e3b0583232 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;
@@ -541,11 +544,20 @@ vhost_kernel_enable_queue_pair(struct virtio_user_dev 
*dev,
        vhostfd = data->vhostfds[pair_idx];
        tapfd = data->tapfds[pair_idx];
 
+       /*
+        * A queue pair's TAP queue is attached only while the pair is enabled,
+        * so the kernel sends packets only to queues in use. The first queue
+        * stays attached, because the interface needs one. TUNSETQUEUE fails
+        * on a queue already in the requested state, which the check above
+        * prevents.
+        */
        if (!enable) {
                if (vhost_kernel_set_backend(vhostfd, -1) < 0) {
                        PMD_DRV_LOG(ERR, "fail to set backend for vhost 
kernel");
                        return -1;
                }
+               if (pair_idx > 0 && vhost_tap_set_queue(tapfd, false) < 0)
+                       return -1;
                dev->qp_enabled[pair_idx] = false;
                return 0;
        }
@@ -561,18 +573,26 @@ 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;
+               goto err_detach;
        }
 
        if (vhost_kernel_set_backend(vhostfd, tapfd) < 0) {
                PMD_DRV_LOG(ERR, "fail to set backend for vhost kernel");
-               return -1;
+               goto err_detach;
        }
 
        dev->qp_enabled[pair_idx] = true;
        return 0;
+
+err_detach:
+       if (pair_idx > 0)
+               vhost_tap_set_queue(tapfd, false);
+       return -1;
 }
 
 static int
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

Reply via email to