Skip to contentSkip to conversation

virtio/vsock: add two more queues for datagram types

RFC Superseded Jiang Wang 2 participants 11 messages Jul 6, 2021 – Aug 9, 2021

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Jiang Wang

author
#0 lore raw

RFC v3 1/1 virtio/vsock: add two more queues for datagram types

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

 hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
 hw/virtio/vhost-vsock.c                       |  3 ++
 include/hw/virtio/vhost-vsock-common.h        |  3 +-
 include/hw/virtio/vhost-vsock.h               |  4 ++
 include/standard-headers/linux/virtio_vsock.h |  3 ++
 5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +51 -2
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
     return 0;
 }
 
+static int vhost_vsock_get_max_qps(void)
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
     virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                 sizeof(struct virtio_vsock_config));
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
     vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                        vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
+    }
+
     /* The event queue belongs to QEMU */
     vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                        vhost_vsock_common_handle_output);
 
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
     vvc->post_load_timer = NULL;
 }
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
     virtio_delete_queue(vvc->recv_vq);
     virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)
+        g_free(vvc->vhost_dev.vqs);
+
     virtio_delete_queue(vvc->event_vq);
     virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
     VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
     VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
     VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
     virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
     return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                 requested_features);
 }
include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
     VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
     struct vhost_dev vhost_dev;
 
     VirtQueue *event_vq;
     VirtQueue *recv_vq;
     VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
     QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +4 -0
index 84f4e727c7..e10319785d 100644
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
     uint64_t guest_cid;
     char *vhostfd;
+    bool enable_dgram;
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
     /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +3 -0
index 5eac522ee2..6ff8c5084c 100644
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET supported */
 
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */
+
 struct virtio_vsock_config {
 	uint64_t guest_cid;
 } QEMU_PACKED;
Signature · 1 line
2.11.0
#1 lore raw
Quoted 40 lines · Jiang Wang

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
hw/virtio/vhost-vsock.c                       |  3 ++
include/hw/virtio/vhost-vsock-common.h        |  3 +-
include/hw/virtio/vhost-vsock.h               |  4 ++
include/standard-headers/linux/virtio_vsock.h |  3 ++
5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +3 -0
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
    return 0;
 }
 
+static int vhost_vsock_get_max_qps(void)

hw/virtio/vhost-vsock-common.c int vhost_vsock_common_post_load(void *opaque, int version_id) · line 211

Quoted 10 lines
hw/virtio/vhost-vsock-common.c +10 -0
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }

You should use the vhostfd already opened in vhost_vsock_device_realize(), since QEMU may not have permission to access to the device, and the file descriptor can be passed from the management layer.

Quoted 23 lines
hw/virtio/vhost-vsock-common.c +16 -0
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
    virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                sizeof(struct virtio_vsock_config));

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 241

Quoted 4 lines
hw/virtio/vhost-vsock-common.c +1 -0
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
    vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();

You can't do this here, since the vhost-vsock-common.c functions are used also by vhost-user-vsock, that doesn't use the /dev/vhost-vsock device since the device is emulated in a separate user process.

I think you can use something similar to what you did in v2, where you passed a parameter to vhost_vsock_common_realize() to enable or not the datagram queues.

Quoted 4 lines
hw/virtio/vhost-vsock-common.c +4 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 10 lines
hw/virtio/vhost-vsock-common.c +7 -0
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

Quoted 9 lines
hw/virtio/vhost-vsock-common.c +2 -2
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
    vvc->post_load_timer = NULL;
 }

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_unrealize(VirtIODevice *vdev) · line 276

Quoted 9 lines
hw/virtio/vhost-vsock-common.c +6 -0
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
    virtio_delete_queue(vvc->recv_vq);
    virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)

g_free() already handles NULL pointers, so you can remove this check.

Quoted 50 lines
hw/virtio/vhost-vsock-common.c +2 -0
+        g_free(vvc->vhost_dev.vqs);
+
    virtio_delete_queue(vvc->event_vq);
    virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
    VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
    VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
    virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
    return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                requested_features);
 }
include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
    VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
    struct vhost_dev vhost_dev;
 
    VirtQueue *event_vq;
    VirtQueue *recv_vq;
    VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
    QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +0 -0
index 84f4e727c7..e10319785d 100644

include/hw/virtio/vhost-vsock.h OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK) · line 26

Quoted 4 lines
include/hw/virtio/vhost-vsock.h +1 -0
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
    uint64_t guest_cid;
    char *vhostfd;
+    bool enable_dgram;

Leftover?

Quoted 16 lines
include/hw/virtio/vhost-vsock.h +3 -0
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
    /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +0 -0
index 5eac522ee2..6ff8c5084c 100644

include/standard-headers/linux/virtio_vsock.h line 45

Quoted 5 lines
include/standard-headers/linux/virtio_vsock.h +2 -0
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET supported */
 
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */

Bit 0 is reserved for STREAM, IIRC also in the virtio-spec proposal you used bit 2 for DGRAM.

Thanks,
Stefano

#2 lore raw
Quoted 41 lines · Stefano Garzarella
Quoted 39 lines · Jiang Wang

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
hw/virtio/vhost-vsock.c                       |  3 ++
include/hw/virtio/vhost-vsock-common.h        |  3 +-
include/hw/virtio/vhost-vsock.h               |  4 ++
include/standard-headers/linux/virtio_vsock.h |  3 ++
5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +2 -0
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
    return 0;
 }
 
Quoted 17 lines

hw/virtio/vhost-vsock-common.c int vhost_vsock_common_post_load(void *opaque, int version_id) · line 211

Quoted 11 lines
hw/virtio/vhost-vsock-common.c +11 -0
+static int vhost_vsock_get_max_qps(void)
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }

You should use the vhostfd already opened in vhost_vsock_device_realize(), since QEMU may not have permission to access to the device, and the file descriptor can be passed from the management layer.

Sure. Will do.

Quoted 32 lines
Quoted 23 lines
hw/virtio/vhost-vsock-common.c +16 -0
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
    virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                sizeof(struct virtio_vsock_config));

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 241

Quoted 4 lines
hw/virtio/vhost-vsock-common.c +1 -0
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
    vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();

You can't do this here, since the vhost-vsock-common.c functions are used also by vhost-user-vsock, that doesn't use the /dev/vhost-vsock device since the device is emulated in a separate user process.

Quoted 4 lines

I think you can use something similar to what you did in v2, where you passed a parameter to vhost_vsock_common_realize() to enable or not the datagram queues.

Just to make sure, the qemu parameter will only be used for vhost-user-vsock, right? I think for the vhost-vsock kernel module, we will use ioctl and ignore the qemu parameter?

Quoted 9 lines
Quoted 9 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 9 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending on the feature bit.

Quoted 10 lines
Quoted 10 lines
hw/virtio/vhost-vsock-common.c +2 -2
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
    vvc->post_load_timer = NULL;
 }
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
Quoted 11 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_unrealize(VirtIODevice *vdev) · line 276

Quoted 8 lines
hw/virtio/vhost-vsock-common.c +6 -0
    virtio_delete_queue(vvc->recv_vq);
    virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)

g_free() already handles NULL pointers, so you can remove this check.

Got it.

Quoted 43 lines
Quoted 43 lines
hw/virtio/vhost-vsock-common.c +2 -0
+        g_free(vvc->vhost_dev.vqs);
+
    virtio_delete_queue(vvc->event_vq);
    virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
    VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
    VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
    virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
    return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                requested_features);
 }
include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
    VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
    struct vhost_dev vhost_dev;
 
    VirtQueue *event_vq;
    VirtQueue *recv_vq;
    VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
Quoted 14 lines

include/hw/virtio/vhost-vsock.h OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK) · line 26

Quoted 11 lines
include/hw/virtio/vhost-vsock-common.h +0 -0
    QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +1 -0
index 84f4e727c7..e10319785d 100644
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
    uint64_t guest_cid;
    char *vhostfd;
+    bool enable_dgram;

Leftover?

Right, but to support vhost-vsock-user, I think I will add this parameter back?

Quoted 19 lines
Quoted 19 lines
include/hw/virtio/vhost-vsock.h +3 -0
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
    /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +0 -0
index 5eac522ee2..6ff8c5084c 100644
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET supported */
 
Quoted 6 lines

include/standard-headers/linux/virtio_vsock.h line 45

Quoted 2 lines
include/standard-headers/linux/virtio_vsock.h +2 -0
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */

Bit 0 is reserved for STREAM, IIRC also in the virtio-spec proposal you used bit 2 for DGRAM.

My bad, I did not update the code here yet.

Quoted 3 lines

Thanks,
Stefano

#3 lore raw
Quoted 94 lines · Jiang Wang .
Quoted 41 lines · Stefano Garzarella
Quoted 39 lines · Jiang Wang

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
hw/virtio/vhost-vsock.c                       |  3 ++
include/hw/virtio/vhost-vsock-common.h        |  3 +-
include/hw/virtio/vhost-vsock.h               |  4 ++
include/standard-headers/linux/virtio_vsock.h |  3 ++
5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +2 -0
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
    return 0;
 }
 
Quoted 17 lines

hw/virtio/vhost-vsock-common.c int vhost_vsock_common_post_load(void *opaque, int version_id) · line 211

Quoted 11 lines
hw/virtio/vhost-vsock-common.c +11 -0
+static int vhost_vsock_get_max_qps(void)
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }

You should use the vhostfd already opened in vhost_vsock_device_realize(), since QEMU may not have permission to access to the device, and the file descriptor can be passed from the management layer.

Sure. Will do.

Quoted 33 lines
Quoted 23 lines
hw/virtio/vhost-vsock-common.c +16 -0
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
    virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                sizeof(struct virtio_vsock_config));

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice · line 242

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +1 -0
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice 
 *vdev, const char *name)
    vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();

You can't do this here, since the vhost-vsock-common.c functions are used also by vhost-user-vsock, that doesn't use the /dev/vhost-vsock device since the device is emulated in a separate user process.

Quoted 7 lines
Quoted 4 lines

I think you can use something similar to what you did in v2, where you passed a parameter to vhost_vsock_common_realize() to enable or not the datagram queues.

Just to make sure, the qemu parameter will only be used for vhost-user-vsock, right? I think for the vhost-vsock kernel module, we will use ioctl and ignore the qemu parameter?

No, I mean a function parameter in vhost_vsock_common_realize() that we set to true when datagram is supported by the backend.

You can move the vhost_vsock_get_max_qps() call in vhost_vsock_device_realize(), just before call vhost_vsock_common_realize() where we can pass a parameter to specify if datagram is supported or not.

For now in vhost-user-vsock you can always pass false. When we will support it, we can add something similar to discover the features.

Just to be clear, we don't need any QEMU command line parameter.

Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 7 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

I'll be off until July 16th, after that I'll check better, but I think there is something wrong here and we should use the 3rd or 5th queue for events only after the guest acked the features.

Quoted 82 lines
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost-vsock-common.c +2 -2
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
    vvc->post_load_timer = NULL;
 }
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
Quoted 11 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_unrealize(VirtIODevice *vdev) · line 276

Quoted 8 lines
hw/virtio/vhost-vsock-common.c +6 -0
    virtio_delete_queue(vvc->recv_vq);
    virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)

g_free() already handles NULL pointers, so you can remove this check.

Got it.

Quoted 44 lines
Quoted 44 lines
hw/virtio/vhost-vsock-common.c +2 -0
+        g_free(vvc->vhost_dev.vqs);
+
    virtio_delete_queue(vvc->event_vq);
    virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
    VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
    VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
    virtio_add_feature(&requested_features, 
    VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
    return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                requested_features);

}

include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
    VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
    struct vhost_dev vhost_dev;
 
    VirtQueue *event_vq;
    VirtQueue *recv_vq;
    VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
Quoted 14 lines

include/hw/virtio/vhost-vsock.h OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK) · line 26

Quoted 11 lines
include/hw/virtio/vhost-vsock-common.h +0 -0
    QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +1 -0
index 84f4e727c7..e10319785d 100644
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
    uint64_t guest_cid;
    char *vhostfd;
+    bool enable_dgram;

Leftover?

Quoted 1 line

Right, but to support vhost-vsock-user, I think I will add this parameter back?

I'm not sure it is needed.

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
include/hw/virtio/vhost-vsock.h +3 -0
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
    /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +0 -0
index 5eac522ee2..6ff8c5084c 100644
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET supported */
 
Quoted 8 lines
Quoted 6 lines

include/standard-headers/linux/virtio_vsock.h line 45

Quoted 2 lines
include/standard-headers/linux/virtio_vsock.h +2 -0
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */

Bit 0 is reserved for STREAM, IIRC also in the virtio-spec proposal you used bit 2 for DGRAM.

My bad, I did not update the code here yet.

No problems :-)

Thanks,
Stefano

#4 lore raw
Quoted 112 lines · Stefano Garzarella
Quoted 94 lines · Jiang Wang .
Quoted 41 lines · Stefano Garzarella
Quoted 39 lines · Jiang Wang

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
hw/virtio/vhost-vsock.c                       |  3 ++
include/hw/virtio/vhost-vsock-common.h        |  3 +-
include/hw/virtio/vhost-vsock.h               |  4 ++
include/standard-headers/linux/virtio_vsock.h |  3 ++
5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +2 -0
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
    return 0;
 }
 
Quoted 17 lines

hw/virtio/vhost-vsock-common.c int vhost_vsock_common_post_load(void *opaque, int version_id) · line 211

Quoted 11 lines
hw/virtio/vhost-vsock-common.c +11 -0
+static int vhost_vsock_get_max_qps(void)
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }

You should use the vhostfd already opened in vhost_vsock_device_realize(), since QEMU may not have permission to access to the device, and the file descriptor can be passed from the management layer.

Sure. Will do.

Quoted 33 lines
Quoted 23 lines
hw/virtio/vhost-vsock-common.c +16 -0
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
    virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                sizeof(struct virtio_vsock_config));

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice · line 242

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +1 -0
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice
 *vdev, const char *name)
    vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();

You can't do this here, since the vhost-vsock-common.c functions are used also by vhost-user-vsock, that doesn't use the /dev/vhost-vsock device since the device is emulated in a separate user process.

Quoted 7 lines
Quoted 4 lines

I think you can use something similar to what you did in v2, where you passed a parameter to vhost_vsock_common_realize() to enable or not the datagram queues.

Just to make sure, the qemu parameter will only be used for vhost-user-vsock, right? I think for the vhost-vsock kernel module, we will use ioctl and ignore the qemu parameter?

No, I mean a function parameter in vhost_vsock_common_realize() that we set to true when datagram is supported by the backend.

You can move the vhost_vsock_get_max_qps() call in vhost_vsock_device_realize(), just before call vhost_vsock_common_realize() where we can pass a parameter to specify if datagram is supported or not.

Quoted 5 lines

For now in vhost-user-vsock you can always pass false. When we will support it, we can add something similar to discover the features.

Just to be clear, we don't need any QEMU command line parameter.

Got it. That sounds good.

Quoted 34 lines
Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

Quoted 9 lines

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

I see. I did not test migration, so likely I did not check that code path. Will give it a try.

Quoted 4 lines

I'll be off until July 16th, after that I'll check better, but I think there is something wrong here and we should use the 3rd or 5th queue for events only after the guest acked the features.

Got it.

Quoted 122 lines
Quoted 82 lines
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost-vsock-common.c +2 -2
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
    vvc->post_load_timer = NULL;
 }
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
Quoted 11 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_unrealize(VirtIODevice *vdev) · line 276

Quoted 8 lines
hw/virtio/vhost-vsock-common.c +6 -0
    virtio_delete_queue(vvc->recv_vq);
    virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)

g_free() already handles NULL pointers, so you can remove this check.

Got it.

Quoted 44 lines
Quoted 44 lines
hw/virtio/vhost-vsock-common.c +2 -0
+        g_free(vvc->vhost_dev.vqs);
+
    virtio_delete_queue(vvc->event_vq);
    virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
    VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
    VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
    virtio_add_feature(&requested_features,
    VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
    return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                requested_features);

}

include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
    VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
    struct vhost_dev vhost_dev;
 
    VirtQueue *event_vq;
    VirtQueue *recv_vq;
    VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
Quoted 14 lines

include/hw/virtio/vhost-vsock.h OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK) · line 26

Quoted 11 lines
include/hw/virtio/vhost-vsock-common.h +0 -0
    QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +1 -0
index 84f4e727c7..e10319785d 100644
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
    uint64_t guest_cid;
    char *vhostfd;
+    bool enable_dgram;

Leftover?

Quoted 2 lines

Right, but to support vhost-vsock-user, I think I will add this parameter back?

I'm not sure it is needed.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
include/hw/virtio/vhost-vsock.h +3 -0
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
    /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +0 -0
index 5eac522ee2..6ff8c5084c 100644
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET

supported */

Quoted 8 lines
Quoted 6 lines

include/standard-headers/linux/virtio_vsock.h line 45

Quoted 2 lines
include/standard-headers/linux/virtio_vsock.h +2 -0
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */

Bit 0 is reserved for STREAM, IIRC also in the virtio-spec proposal you used bit 2 for DGRAM.

My bad, I did not update the code here yet.

No problems :-)

Thanks,
Stefano

#5 lore raw
In reply to Stefano Garzarella
Quoted 151 lines · Stefano Garzarella
Quoted 94 lines · Jiang Wang .
Quoted 41 lines · Stefano Garzarella
Quoted 39 lines · Jiang Wang

Datagram sockets are connectionless and unreliable. The sender does not know the capacity of the receiver and may send more packets than the receiver can handle.

Add two more dedicate virtqueues for datagram sockets, so that it will not unfairly steal resources from stream and future connection-oriented sockets.
---
v1 -> v2: use qemu cmd option to control number of queues,
removed configuration settings for dgram.
v2 -> v3: use ioctl to get features and decie numbr of
virt queues, instead of qemu cmd option.

btw: this patch is based on Arseny's SEQPACKET patch.

hw/virtio/vhost-vsock-common.c                | 53 ++++++++++++++++++++++++++-
hw/virtio/vhost-vsock.c                       |  3 ++
include/hw/virtio/vhost-vsock-common.h        |  3 +-
include/hw/virtio/vhost-vsock.h               |  4 ++
include/standard-headers/linux/virtio_vsock.h |  3 ++
5 files changed, 63 insertions(+), 3 deletions(-)
hw/virtio/vhost-vsock-common.c +2 -0
index 4ad6e234ad..8164e09445 100644
@@ -17,6 +17,8 @@
 #include "hw/virtio/vhost-vsock.h"
 #include "qemu/iov.h"
 #include "monitor/monitor.h"
+#include <sys/ioctl.h>
+#include <linux/vhost.h>
 
 int vhost_vsock_common_start(VirtIODevice *vdev)
 {
@@ -196,9 +198,36 @@ int vhost_vsock_common_post_load(void *opaque, int version_id)
    return 0;
 }
 
Quoted 17 lines

hw/virtio/vhost-vsock-common.c int vhost_vsock_common_post_load(void *opaque, int version_id) · line 211

Quoted 11 lines
hw/virtio/vhost-vsock-common.c +11 -0
+static int vhost_vsock_get_max_qps(void)
+{
+    uint64_t features;
+    int ret;
+    int fd = -1;
+
+    fd = qemu_open_old("/dev/vhost-vsock", O_RDONLY);
+    if (fd == -1) {
+        error_report("vhost-vsock: failed to open device. %s", strerror(errno));
+        return -1;
+    }

You should use the vhostfd already opened in vhost_vsock_device_realize(), since QEMU may not have permission to access to the device, and the file descriptor can be passed from the management layer.

Sure. Will do.

Quoted 33 lines
Quoted 23 lines
hw/virtio/vhost-vsock-common.c +16 -0
+
+    ret = ioctl(fd, VHOST_GET_FEATURES, &features);
+    if (ret) {
+        error_report("vhost-vsock: failed to read  device. %s", strerror(errno));
+        qemu_close(fd);
+        return ret;
+    }
+
+    qemu_close(fd);
+    if (features & (1 << VIRTIO_VSOCK_F_DGRAM))
+        return MAX_VQS_WITH_DGRAM;
+
+    return MAX_VQS_WITHOUT_DGRAM;
+}
+
 void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name)
 {
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
+    int nvqs = MAX_VQS_WITHOUT_DGRAM;
 
    virtio_init(vdev, name, VIRTIO_ID_VSOCK,
                sizeof(struct virtio_vsock_config));

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice · line 242

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +1 -0
@@ -209,12 +238,24 @@ void vhost_vsock_common_realize(VirtIODevice
 *vdev, const char *name)
    vvc->trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);
 
+    nvqs = vhost_vsock_get_max_qps();

You can't do this here, since the vhost-vsock-common.c functions are used also by vhost-user-vsock, that doesn't use the /dev/vhost-vsock device since the device is emulated in a separate user process.

Quoted 7 lines
Quoted 4 lines

I think you can use something similar to what you did in v2, where you passed a parameter to vhost_vsock_common_realize() to enable or not the datagram queues.

Just to make sure, the qemu parameter will only be used for vhost-user-vsock, right? I think for the vhost-vsock kernel module, we will use ioctl and ignore the qemu parameter?

No, I mean a function parameter in vhost_vsock_common_realize() that we set to true when datagram is supported by the backend.

You can move the vhost_vsock_get_max_qps() call in vhost_vsock_device_realize(), just before call vhost_vsock_common_realize() where we can pass a parameter to specify if datagram is supported or not.

For now in vhost-user-vsock you can always pass false. When we will support it, we can add something similar to discover the features.

Just to be clear, we don't need any QEMU command line parameter.

Quoted 25 lines
Quoted 10 lines
Quoted 10 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

Quoted 9 lines

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

btw, I will address the rest of the comments and send a new version soon.

Quoted 126 lines

I'll be off until July 16th, after that I'll check better, but I think there is something wrong here and we should use the 3rd or 5th queue for events only after the guest acked the features.

Quoted 82 lines
Quoted 11 lines
Quoted 11 lines
hw/virtio/vhost-vsock-common.c +2 -2
-    vvc->vhost_dev.nvqs = ARRAY_SIZE(vvc->vhost_vqs);
-    vvc->vhost_dev.vqs = vvc->vhost_vqs;
+    vvc->vhost_dev.nvqs = nvqs;
+    vvc->vhost_dev.vqs = g_new0(struct vhost_virtqueue, vvc->vhost_dev.nvqs);
 
    vvc->post_load_timer = NULL;
 }
@@ -227,6 +268,14 @@ void vhost_vsock_common_unrealize(VirtIODevice *vdev)
 
Quoted 11 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_unrealize(VirtIODevice *vdev) · line 276

Quoted 8 lines
hw/virtio/vhost-vsock-common.c +6 -0
    virtio_delete_queue(vvc->recv_vq);
    virtio_delete_queue(vvc->trans_vq);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM) {
+        virtio_delete_queue(vvc->dgram_recv_vq);
+        virtio_delete_queue(vvc->dgram_trans_vq);
+    }
+
+    if (vvc->vhost_dev.vqs)

g_free() already handles NULL pointers, so you can remove this check.

Got it.

Quoted 44 lines
Quoted 44 lines
hw/virtio/vhost-vsock-common.c +2 -0
+        g_free(vvc->vhost_dev.vqs);
+
    virtio_delete_queue(vvc->event_vq);
    virtio_cleanup(vdev);
 }
hw/virtio/vhost-vsock.c +3 -0
index 1b1a5c70ed..33bbe16983 100644
@@ -23,6 +23,7 @@
 
 const int feature_bits[] = {
    VIRTIO_VSOCK_F_SEQPACKET,
+    VIRTIO_VSOCK_F_DGRAM,
    VHOST_INVALID_FEATURE_BIT
 };
 
@@ -116,6 +117,8 @@ static uint64_t vhost_vsock_get_features(VirtIODevice *vdev,
    VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(vdev);
 
    virtio_add_feature(&requested_features,
    VIRTIO_VSOCK_F_SEQPACKET);
+    if (vvc->vhost_dev.nvqs == MAX_VQS_WITH_DGRAM)
+        virtio_add_feature(&requested_features, VIRTIO_VSOCK_F_DGRAM);
    return vhost_get_features(&vvc->vhost_dev, feature_bits,
                                requested_features);

}

include/hw/virtio/vhost-vsock-common.h +2 -1
index e412b5ee98..798715241f 100644
@@ -27,12 +27,13 @@ enum {
 struct VHostVSockCommon {
    VirtIODevice parent;
 
-    struct vhost_virtqueue vhost_vqs[2];
    struct vhost_dev vhost_dev;
 
    VirtQueue *event_vq;
    VirtQueue *recv_vq;
    VirtQueue *trans_vq;
+    VirtQueue *dgram_recv_vq;
+    VirtQueue *dgram_trans_vq;
 
Quoted 14 lines

include/hw/virtio/vhost-vsock.h OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK) · line 26

Quoted 11 lines
include/hw/virtio/vhost-vsock-common.h +0 -0
    QEMUTimer *post_load_timer;
 };
include/hw/virtio/vhost-vsock.h +1 -0
index 84f4e727c7..e10319785d 100644
@@ -23,6 +23,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VHostVSock, VHOST_VSOCK)
 typedef struct {
    uint64_t guest_cid;
    char *vhostfd;
+    bool enable_dgram;

Leftover?

Quoted 2 lines

Right, but to support vhost-vsock-user, I think I will add this parameter back?

I'm not sure it is needed.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines
include/hw/virtio/vhost-vsock.h +3 -0
 } VHostVSockConf;
 
 struct VHostVSock {
@@ -33,4 +34,7 @@ struct VHostVSock {
    /*< public >*/
 };
 
+#define MAX_VQS_WITHOUT_DGRAM 2
+#define MAX_VQS_WITH_DGRAM 4
+
 #endif /* QEMU_VHOST_VSOCK_H */
include/standard-headers/linux/virtio_vsock.h +0 -0
index 5eac522ee2..6ff8c5084c 100644
@@ -41,6 +41,9 @@
 /* The feature bitmap for virtio vsock */
 #define VIRTIO_VSOCK_F_SEQPACKET       1       /* SOCK_SEQPACKET

supported */

Quoted 8 lines
Quoted 6 lines

include/standard-headers/linux/virtio_vsock.h line 45

Quoted 2 lines
include/standard-headers/linux/virtio_vsock.h +2 -0
+/* Feature bits */
+#define VIRTIO_VSOCK_F_DGRAM 0 /*Does vsock support dgram */

Bit 0 is reserved for STREAM, IIRC also in the virtio-spec proposal you used bit 2 for DGRAM.

My bad, I did not update the code here yet.

No problems :-)

Thanks,
Stefano

#6 lore raw
Quoted 4 lines · Jiang Wang .
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Jiang Wang .
Quoted 1 line · Stefano Garzarella

On Tue, Jul 06, 2021 at 10:26:07PM +0000, Jiang Wang wrote:

[...]

Quoted 38 lines
Quoted 38 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Quoted 8 lines
Quoted 4 lines

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

I meant a migration of a guest from QEMU without this patch to a QEMU with this patch. Of course in that case testing a socket stream.

Quoted 2 lines

btw, I will address the rest of the comments and send a new version soon.

Great!

Thanks,
Stefano

#7 lore raw
Quoted 51 lines · Stefano Garzarella
Quoted 4 lines · Jiang Wang .
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Jiang Wang .
Quoted 1 line · Stefano Garzarella

On Tue, Jul 06, 2021 at 10:26:07PM +0000, Jiang Wang wrote:

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
hw/virtio/vhost-vsock-common.c +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

hw/virtio/vhost-vsock-common.c void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name) · line 255

Quoted 5 lines
hw/virtio/vhost-vsock-common.c +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

Quoted 8 lines
Quoted 4 lines

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

I meant a migration of a guest from QEMU without this patch to a QEMU with this patch. Of course in that case testing a socket stream.

Sorry, I meant the opposite.

You should try to migrate a guest that does not support dgrams starting from a QEMU with this patch (and kernel that supports dgram, so qemu uses the 5th queue for event), to a QEMU without this patch.

I still think the event queue should remain the third and those dgrams the next 2.

Thanks,
Stefano

#8 lore raw
Quoted 63 lines · Stefano Garzarella
Quoted 51 lines · Stefano Garzarella
Quoted 4 lines · Jiang Wang .
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Jiang Wang .
Quoted 1 line · Stefano Garzarella

On Tue, Jul 06, 2021 at 10:26:07PM +0000, Jiang Wang wrote:

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

Quoted 8 lines
Quoted 4 lines

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

I meant a migration of a guest from QEMU without this patch to a QEMU with this patch. Of course in that case testing a socket stream.

Quoted 6 lines

Sorry, I meant the opposite.

You should try to migrate a guest that does not support dgrams starting from a QEMU with this patch (and kernel that supports dgram, so qemu uses the 5th queue for event), to a QEMU without this patch.

Got it. I tried what you said and saw errors on the destination qemu. Then I moved event queue up to be number 3 (before adding dgram vqs), as the following:

+ /* The event queue belongs to QEMU */
+ vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+ vhost_vsock_common_handle_output);
+

     nvqs = vhost_vsock_get_max_qps(enable_dgram);

@@ -253,10 +257,6 @@ void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name, bool enabl

vhost_vsock_common_handle_output);
     }

- /* The event queue belongs to QEMU */
- vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
- vhost_vsock_common_handle_output);
-

But I still see following errors on the destination qemu:
qemu-system-x86_64: Error starting vhost vsock: 14

Any idea if my above code change is wrong or missing something?

Take one step back, what should be the host kernel version? With or without dgram support? I tried both. The new dest kernel shows the above error. The old dest kernel shows a msr error probably not related to vsock.

To explain the above qemu 14 error, I think the issue is that the source host kernel
supports dgram by always setting the DGRAM feature bit(in my implementation). Then the source
qemu query the source host kernel, and use 5 for event vq. Even if the source guest kernel does not support dgram, it currently has no way to tell the source host or the source qemu.

On the source machine, when we start qemu process, and the guest VM is still in BIOS or early boot process ( before vsock is initialized), I think at this time, the qemu vhost_vsock_common_realize() is already called. So qemu can only check if the host kernel supports dgram or not, but has no knowledge about the guest kernel. After the guest kernel is fully boot up, it can tell qemu or the host if it supports dgram or not ( or the host or qemu detect for that). But I don't think we will change the vq numbers at that time.

Maybe we should fix that too and change vq numbers ( delete vq and add back?) at a later
time after the guest kernel is fully boot-up?

Quoted 6 lines

I still think the event queue should remain the third and those dgrams the next 2.

Thanks,
Stefano

#9 lore raw
Quoted 95 lines · Jiang Wang .
Quoted 63 lines · Stefano Garzarella
Quoted 51 lines · Stefano Garzarella
Quoted 4 lines · Jiang Wang .
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Jiang Wang .
Quoted 1 line · Stefano Garzarella

On Tue, Jul 06, 2021 at 10:26:07PM +0000, Jiang Wang wrote:

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

Quoted 8 lines
Quoted 4 lines

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

I meant a migration of a guest from QEMU without this patch to a QEMU with this patch. Of course in that case testing a socket stream.

Quoted 6 lines

Sorry, I meant the opposite.

You should try to migrate a guest that does not support dgrams starting from a QEMU with this patch (and kernel that supports dgram, so qemu uses the 5th queue for event), to a QEMU without this patch.

Got it. I tried what you said and saw errors on the destination qemu. Then I moved event queue up to be number 3 (before adding dgram vqs), as the following:

(quoted diff) +4 -4
+    /* The event queue belongs to QEMU */
+    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                       vhost_vsock_common_handle_output);
+
 
    nvqs = vhost_vsock_get_max_qps(enable_dgram);
@@ -253,10 +257,6 @@ void vhost_vsock_common_realize(VirtIODevice
 *vdev, const char *name, bool enabl
 
 vhost_vsock_common_handle_output);
    }
 
-    /* The event queue belongs to QEMU */
-    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
-                                       vhost_vsock_common_handle_output);
-
 

But I still see following errors on the destination qemu:
qemu-system-x86_64: Error starting vhost vsock: 14

Quoted 1 line

Any idea if my above code change is wrong or missing something?

No, sorry.
Do you have some kernel messages in the host?

Quoted 4 lines

Take one step back, what should be the host kernel version? With or without dgram support? I tried both. The new dest kernel shows the above error. The old dest kernel shows a msr error probably not related to vsock.

I think the best is to try the host kernel with DGRAM support, so QEMU will allocate all the queues.

Quoted 8 lines

To explain the above qemu 14 error, I think the issue is that the source host kernel
supports dgram by always setting the DGRAM feature bit(in my implementation). Then the source
qemu query the source host kernel, and use 5 for event vq. Even if the source guest kernel does not support dgram, it currently has no way to tell the source host or the source qemu.

Initially I think is better to try the migration on the same host, so we can exclude other issues. When it works properly, you can try to migrate to a different host kernel.

Quoted 9 lines

On the source machine, when we start qemu process, and the guest VM is still in BIOS or early boot process ( before vsock is initialized), I think at this time, the qemu vhost_vsock_common_realize() is already called. So qemu can only check if the host kernel supports dgram or not, but has no knowledge about the guest kernel. After the guest kernel is fully boot up, it can tell qemu or the host if it supports dgram or not ( or the host or qemu detect for that). But I don't think we will change the vq numbers at that time.

Quoted 3 lines

Maybe we should fix that too and change vq numbers ( delete vq and add back?) at a later
time after the guest kernel is fully boot-up?

IIRC vhost-net allocates the maximum number of queues, and then it uses only the queues supported/requested, so I think we can do something similar.

Allocates 5 queues and, at runtime, we can decide which queue to use.

Thanks,
Stefano

#10 lore raw
Quoted 94 lines · Stefano Garzarella
Quoted 92 lines · Jiang Wang .
Quoted 63 lines · Stefano Garzarella
Quoted 51 lines · Stefano Garzarella
Quoted 4 lines · Jiang Wang .
Quoted 3 lines · Stefano Garzarella
Quoted 2 lines · Jiang Wang .
Quoted 1 line · Stefano Garzarella

On Tue, Jul 06, 2021 at 10:26:07PM +0000, Jiang Wang wrote:

[...]

Quoted 42 lines
Quoted 42 lines
Quoted 24 lines
Quoted 9 lines
Quoted 9 lines
(quoted diff) +9 -0
+
+    if (nvqs < 0)
+        nvqs = MAX_VQS_WITHOUT_DGRAM;
+
+    if (nvqs == MAX_VQS_WITH_DGRAM) {
+        vvc->dgram_recv_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                              vhost_vsock_common_handle_output);
+        vvc->dgram_trans_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                               vhost_vsock_common_handle_output);
Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+    }
+
    /* The event queue belongs to QEMU */
    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
                                       vhost_vsock_common_handle_output);

Did you do a test with a guest that doesn't support datagram with QEMU and hosts that do?

Yes, and it works.

Quoted 4 lines

I repost my thoughts that I had on v2:

What happen if the guest doesn't support dgram?

Quoted 8 lines
Quoted 5 lines

I think we should dynamically use the 3rd queue or the 5th queue for the events at runtime after the guest acked the features.

Maybe better to switch to an array of VirtQueue.

I think in current V3, it already dynamically use 3rd or 5th queue depending
on the feature bit.

I'm not sure. IIUC when vhost_vsock_common_realize() is called, we don't know the features acked by the guest, so how can it be dynamic?

Here we should know only if the host kernel supports it.

Maybe it works, because in QEMU we use the event queue only after a migration to send a reset event, so you can try to migrate a guest to check this path.

Quoted 8 lines
Quoted 4 lines

I tried VM migration and didn't see any problems. The migration looks fine and vsock dgram still works after migration. Is there any more specific test you want to run to check for this code path?

I meant a migration of a guest from QEMU without this patch to a QEMU with this patch. Of course in that case testing a socket stream.

Quoted 6 lines

Sorry, I meant the opposite.

You should try to migrate a guest that does not support dgrams starting from a QEMU with this patch (and kernel that supports dgram, so qemu uses the 5th queue for event), to a QEMU without this patch.

Got it. I tried what you said and saw errors on the destination qemu. Then I moved event queue up to be number 3 (before adding dgram vqs), as the following:

(quoted diff) +4 -4
+    /* The event queue belongs to QEMU */
+    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
+                                       vhost_vsock_common_handle_output);
+
 
    nvqs = vhost_vsock_get_max_qps(enable_dgram);
@@ -253,10 +257,6 @@ void vhost_vsock_common_realize(VirtIODevice
 *vdev, const char *name, bool enabl
 
 vhost_vsock_common_handle_output);
    }
 
-    /* The event queue belongs to QEMU */
-    vvc->event_vq = virtio_add_queue(vdev, VHOST_VSOCK_QUEUE_SIZE,
-                                       vhost_vsock_common_handle_output);
-
 
Quoted 8 lines
Quoted 4 lines

But I still see following errors on the destination qemu:
qemu-system-x86_64: Error starting vhost vsock: 14

Any idea if my above code change is wrong or missing something?

No, sorry.
Do you have some kernel messages in the host?

I checked dmesg but did not find any errors. I will debug more.

Quoted 17 lines
Quoted 4 lines

Take one step back, what should be the host kernel version? With or without dgram support? I tried both. The new dest kernel shows the above error. The old dest kernel shows a msr error probably not related to vsock.

I think the best is to try the host kernel with DGRAM support, so QEMU will allocate all the queues.

Quoted 8 lines

To explain the above qemu 14 error, I think the issue is that the source host kernel
supports dgram by always setting the DGRAM feature bit(in my implementation). Then the source
qemu query the source host kernel, and use 5 for event vq. Even if the source guest kernel does not support dgram, it currently has no way to tell the source host or the source qemu.

Quoted 4 lines

Initially I think is better to try the migration on the same host, so we can exclude other issues. When it works properly, you can try to migrate to a different host kernel.

Got it.

Quoted 13 lines
Quoted 9 lines

On the source machine, when we start qemu process, and the guest VM is still in BIOS or early boot process ( before vsock is initialized), I think at this time, the qemu vhost_vsock_common_realize() is already called. So qemu can only check if the host kernel supports dgram or not, but has no knowledge about the guest kernel. After the guest kernel is fully boot up, it can tell qemu or the host if it supports dgram or not ( or the host or qemu detect for that). But I don't think we will change the vq numbers at that time.

Quoted 3 lines

Maybe we should fix that too and change vq numbers ( delete vq and add back?) at a later
time after the guest kernel is fully boot-up?

Quoted 6 lines

IIRC vhost-net allocates the maximum number of queues, and then it uses only the queues supported/requested, so I think we can do something similar.

Allocates 5 queues and, at runtime, we can decide which queue to use.

I see. Will dig more. thanks.

Quoted 3 lines

Thanks,
Stefano