[RFC v2 1/5] virtio/vsock: add VIRTIO_VSOCK_F_DGRAM feature bit

From: Jiang Wang
Date: Tue Sep 14 2021 - 01:55:37 EST


When this feature is enabled, allocate 5 queues,
otherwise, allocate 3 queues to be compatible with
old QEMU versions.

Signed-off-by: Jiang Wang <jiang.wang@xxxxxxxxxxxxx>
---
drivers/vhost/vsock.c | 3 +-
include/linux/virtio_vsock.h | 9 ++++
include/uapi/linux/virtio_vsock.h | 2 +
net/vmw_vsock/virtio_transport.c | 79 +++++++++++++++++++++++++++----
4 files changed, 82 insertions(+), 11 deletions(-)

diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c
index f249622ef11b..c79789af0365 100644
--- a/drivers/vhost/vsock.c
+++ b/drivers/vhost/vsock.c
@@ -32,7 +32,8 @@
enum {
VHOST_VSOCK_FEATURES = VHOST_FEATURES |
(1ULL << VIRTIO_F_ACCESS_PLATFORM) |
- (1ULL << VIRTIO_VSOCK_F_SEQPACKET)
+ (1ULL << VIRTIO_VSOCK_F_SEQPACKET) |
+ (1ULL << VIRTIO_VSOCK_F_DGRAM)
};

enum {
diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h
index 35d7eedb5e8e..87d849aeb3ec 100644
--- a/include/linux/virtio_vsock.h
+++ b/include/linux/virtio_vsock.h
@@ -18,6 +18,15 @@ enum {
VSOCK_VQ_MAX = 3,
};

+enum {
+ VSOCK_VQ_STREAM_RX = 0, /* for host to guest data */
+ VSOCK_VQ_STREAM_TX = 1, /* for guest to host data */
+ VSOCK_VQ_DGRAM_RX = 2,
+ VSOCK_VQ_DGRAM_TX = 3,
+ VSOCK_VQ_EX_EVENT = 4,
+ VSOCK_VQ_EX_MAX = 5,
+};
+
/* Per-socket state (accessed via vsk->trans) */
struct virtio_vsock_sock {
struct vsock_sock *vsk;
diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h
index 3dd3555b2740..cff54ba9b924 100644
--- a/include/uapi/linux/virtio_vsock.h
+++ b/include/uapi/linux/virtio_vsock.h
@@ -40,6 +40,8 @@

/* The feature bitmap for virtio vsock */
#define VIRTIO_VSOCK_F_SEQPACKET 1 /* SOCK_SEQPACKET supported */
+/* The feature bitmap for virtio net */
+#define VIRTIO_VSOCK_F_DGRAM 0 /* Host support dgram vsock */

struct virtio_vsock_config {
__le64 guest_cid;
diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c
index 4f7c99dfd16c..bb89f538f5f3 100644
--- a/net/vmw_vsock/virtio_transport.c
+++ b/net/vmw_vsock/virtio_transport.c
@@ -27,7 +27,8 @@ static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */

struct virtio_vsock {
struct virtio_device *vdev;
- struct virtqueue *vqs[VSOCK_VQ_MAX];
+ struct virtqueue **vqs;
+ bool has_dgram;

/* Virtqueue processing is deferred to a workqueue */
struct work_struct tx_work;
@@ -334,7 +335,10 @@ static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
struct scatterlist sg;
struct virtqueue *vq;

- vq = vsock->vqs[VSOCK_VQ_EVENT];
+ if (vsock->has_dgram)
+ vq = vsock->vqs[VSOCK_VQ_EX_EVENT];
+ else
+ vq = vsock->vqs[VSOCK_VQ_EVENT];

sg_init_one(&sg, event, sizeof(*event));

@@ -352,7 +356,10 @@ static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
virtio_vsock_event_fill_one(vsock, event);
}

- virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
+ if (vsock->has_dgram)
+ virtqueue_kick(vsock->vqs[VSOCK_VQ_EX_EVENT]);
+ else
+ virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
}

static void virtio_vsock_reset_sock(struct sock *sk)
@@ -395,7 +402,10 @@ static void virtio_transport_event_work(struct work_struct *work)
container_of(work, struct virtio_vsock, event_work);
struct virtqueue *vq;

- vq = vsock->vqs[VSOCK_VQ_EVENT];
+ if (vsock->has_dgram)
+ vq = vsock->vqs[VSOCK_VQ_EX_EVENT];
+ else
+ vq = vsock->vqs[VSOCK_VQ_EVENT];

mutex_lock(&vsock->event_lock);

@@ -415,7 +425,10 @@ static void virtio_transport_event_work(struct work_struct *work)
}
} while (!virtqueue_enable_cb(vq));

- virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
+ if (vsock->has_dgram)
+ virtqueue_kick(vsock->vqs[VSOCK_VQ_EX_EVENT]);
+ else
+ virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
out:
mutex_unlock(&vsock->event_lock);
}
@@ -438,6 +451,10 @@ static void virtio_vsock_tx_done(struct virtqueue *vq)
queue_work(virtio_vsock_workqueue, &vsock->tx_work);
}

+static void virtio_vsock_dgram_tx_done(struct virtqueue *vq)
+{
+}
+
static void virtio_vsock_rx_done(struct virtqueue *vq)
{
struct virtio_vsock *vsock = vq->vdev->priv;
@@ -449,6 +466,10 @@ static void virtio_vsock_rx_done(struct virtqueue *vq)

static bool virtio_transport_seqpacket_allow(u32 remote_cid);

+static void virtio_vsock_dgram_rx_done(struct virtqueue *vq)
+{
+}
+
static struct virtio_transport virtio_transport = {
.transport = {
.module = THIS_MODULE,
@@ -571,13 +592,29 @@ static int virtio_vsock_probe(struct virtio_device *vdev)
virtio_vsock_tx_done,
virtio_vsock_event_done,
};
+ vq_callback_t *ex_callbacks[] = {
+ virtio_vsock_rx_done,
+ virtio_vsock_tx_done,
+ virtio_vsock_dgram_rx_done,
+ virtio_vsock_dgram_tx_done,
+ virtio_vsock_event_done,
+ };
+
static const char * const names[] = {
"rx",
"tx",
"event",
};
+ static const char * const ex_names[] = {
+ "rx",
+ "tx",
+ "dgram_rx",
+ "dgram_tx",
+ "event",
+ };
+
struct virtio_vsock *vsock = NULL;
- int ret;
+ int ret, max_vq;

ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
if (ret)
@@ -598,9 +635,30 @@ static int virtio_vsock_probe(struct virtio_device *vdev)

vsock->vdev = vdev;

- ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
- vsock->vqs, callbacks, names,
- NULL);
+ if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_DGRAM))
+ vsock->has_dgram = true;
+
+ if (vsock->has_dgram)
+ max_vq = VSOCK_VQ_EX_MAX;
+ else
+ max_vq = VSOCK_VQ_MAX;
+
+ vsock->vqs = kmalloc_array(max_vq, sizeof(struct virtqueue *), GFP_KERNEL);
+ if (!vsock->vqs) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ if (vsock->has_dgram) {
+ ret = virtio_find_vqs(vsock->vdev, max_vq,
+ vsock->vqs, ex_callbacks, ex_names,
+ NULL);
+ } else {
+ ret = virtio_find_vqs(vsock->vdev, max_vq,
+ vsock->vqs, callbacks, names,
+ NULL);
+ }
+
if (ret < 0)
goto out;

@@ -725,7 +783,8 @@ static struct virtio_device_id id_table[] = {
};

static unsigned int features[] = {
- VIRTIO_VSOCK_F_SEQPACKET
+ VIRTIO_VSOCK_F_SEQPACKET,
+ VIRTIO_VSOCK_F_DGRAM
};

static struct virtio_driver virtio_vsock_driver = {
--
2.20.1