Skip to contentSkip to conversation

libqos: add ability to pop buffers from virtqueue

PATCH In review Stefan Hajnoczi 1 participant 7 messages May 5, 2016 – Jun 14, 2016

lore
Share & tools
lore
#0 lore raw

cover libqos: add ability to pop buffers from virtqueue

The libqos virtio framework currently offers no way to pop buffers off the virtqueue. Existing test cases are trivial and therefore get away without reclaiming buffers. More extensive tests really do need to pop buffers and do the housekeeping associated with the 'used' ring.

This patch series adds qvirtqueue_get_buf() and qvirtio_wait_queue_buf() for popping buffers off the virtqueue. The code is based on my recent "[PATCH for-2.7 0/8] libqos: use standard virtio headers" series which has not been merged yet.

I am sending a new test case for the virtio-vsock device in a separate email thread. The virtio-vsock test uses the new qvirtio_wait_queue_buf() function.

Stefan Hajnoczi (5):
libqos: fix virtio descriptor free list
libqos: fix virtio num_free descriptor counter
libqos: add virtio buffer token
libqos: add qvirtqueue_get_buf()
libqos: add qvirtio_wait_queue_buf()

 tests/libqos/virtio.c    | 146 +++++++++++++++++++++++++++++++++++++++++++----
 tests/libqos/virtio.h    |  13 ++++-
 tests/virtio-blk-test.c  |  54 +++++++++---------
 tests/virtio-net-test.c  |   6 +-
 tests/virtio-scsi-test.c |   8 +--
 5 files changed, 181 insertions(+), 46 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/5 libqos: fix virtio descriptor free list

Although qvring_init() chains together all descriptors for the free list, free_head is used as a counter instead of as a free list head.

The test cases appear to work because they are so trivial that the free_head counter never exceeds the vring size. If they added more descriptors free_head would cause an out-of-bounds memory access.

Change free_head to maintain a descriptor free list. The free list is currently never replenished since there is no function to pop buffers from the used ring. This functionality will be added in a later patch.

---
 tests/libqos/virtio.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
tests/libqos/virtio.c +19 -8
index 7910774..d3e4c02 100644
@@ -139,6 +139,7 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
     vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
     vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
         + vq->align - 1) & ~(vq->align - 1));
+    vq->free_head = 0;
 
     for (i = 0; i < vq->size - 1; i++) {
         /* vq->desc[i].addr */
@@ -209,6 +210,8 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
                                                                     bool next)
 {
     uint16_t flags = 0;
+    uint16_t idx = vq->free_head;
+
     vq->num_free--;
 
     if (write) {
@@ -220,17 +223,22 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
     }
 
     /* vq->desc[vq->free_head].addr */
-    writeq(vq->desc + (16 * vq->free_head), data);
+    writeq(vq->desc + (16 * idx), data);
     /* vq->desc[vq->free_head].len */
-    writel(vq->desc + (16 * vq->free_head) + 8, len);
+    writel(vq->desc + (16 * idx) + 8, len);
     /* vq->desc[vq->free_head].flags */
-    writew(vq->desc + (16 * vq->free_head) + 12, flags);
+    writew(vq->desc + (16 * idx) + 12, flags);
 
-    return vq->free_head++; /* Return and increase, in this order */
+    vq->free_head = readw(vq->desc + sizeof(struct vring_desc) * idx +
+                          offsetof(struct vring_desc, next));
+
+    return idx;
 }
 
 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
 {
+    uint16_t idx = vq->free_head;
+
     g_assert(vq->indirect);
     g_assert_cmpint(vq->size, >=, indirect->elem);
     g_assert_cmpint(indirect->index, ==, indirect->elem);
@@ -238,14 +246,17 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
     vq->num_free--;
 
     /* vq->desc[vq->free_head].addr */
-    writeq(vq->desc + (16 * vq->free_head), indirect->desc);
+    writeq(vq->desc + (16 * idx), indirect->desc);
     /* vq->desc[vq->free_head].len */
-    writel(vq->desc + (16 * vq->free_head) + 8,
+    writel(vq->desc + (16 * idx) + 8,
            sizeof(struct vring_desc) * indirect->elem);
     /* vq->desc[vq->free_head].flags */
-    writew(vq->desc + (16 * vq->free_head) + 12, VRING_DESC_F_INDIRECT);
+    writew(vq->desc + (16 * idx) + 12, VRING_DESC_F_INDIRECT);
 
-    return vq->free_head++; /* Return and increase, in this order */
+    vq->free_head = readw(vq->desc + sizeof(struct vring_desc) * idx +
+                          offsetof(struct vring_desc, next));
+
+    return idx;
 }
 
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
Signature · 1 line
2.5.5
#2 lore raw

PATCH 2/5 libqos: fix virtio num_free descriptor counter

In reply to Stefan Hajnoczi

The num_free variable is only ever decremented and never used for anything. Although it is currently useless and could be removed, it will become important once a function is added to pop buffers from a virtqueue.

This patch adds the missing num_free initialization and adds assertions to check that there is enough space to add new descriptors.

---
 tests/libqos/virtio.c | 3 +++
 1 file changed, 3 insertions(+)
tests/libqos/virtio.c +3 -0
index d3e4c02..939e5d3 100644
@@ -140,6 +140,7 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
     vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
         + vq->align - 1) & ~(vq->align - 1));
     vq->free_head = 0;
+    vq->num_free = vq->size;
 
     for (i = 0; i < vq->size - 1; i++) {
         /* vq->desc[i].addr */
@@ -212,6 +213,7 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
     uint16_t flags = 0;
     uint16_t idx = vq->free_head;
 
+    g_assert_cmpint(vq->num_free, >=, 1);
     vq->num_free--;
 
     if (write) {
@@ -243,6 +245,7 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
     g_assert_cmpint(vq->size, >=, indirect->elem);
     g_assert_cmpint(indirect->index, ==, indirect->elem);
 
+    g_assert_cmpint(vq->num_free, >=, 1);
     vq->num_free--;
 
     /* vq->desc[vq->free_head].addr */
Signature · 1 line
2.5.5
#3 lore raw

PATCH 3/5 libqos: add virtio buffer token

In reply to Stefan Hajnoczi

Allow the qvirtqueue_add_buf() caller to associate a void *token with the buffer. The next patch adds a qvirtqueue_get_buf() function which returns the token of the buffer that was popped from the virtqueue. This allows users to identify which buffer was popped.

Existing virtio device tests are simple and do not use the token. They pass a NULL token to qvirtqueue_add_buf().

---
 tests/libqos/virtio.c    |  9 ++++++--
 tests/libqos/virtio.h    |  8 +++++--
 tests/virtio-blk-test.c  | 54 ++++++++++++++++++++++++------------------------
 tests/virtio-net-test.c  |  6 +++---
 tests/virtio-scsi-test.c |  8 +++----
 5 files changed, 47 insertions(+), 38 deletions(-)
tests/libqos/virtio.c +7 -2
index 939e5d3..bc9c24c 100644
@@ -147,6 +147,8 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
         writew(vq->desc + (16 * i), 0);
         /* vq->desc[i].next */
         writew(vq->desc + (16 * i) + 14, i + 1);
+
+        vq->tokens[i] = NULL;
     }
 
     /* vq->avail->flags */
@@ -208,7 +210,7 @@ void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
 }
 
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
-                                                                    bool next)
+                        bool next, void *token)
 {
     uint16_t flags = 0;
     uint16_t idx = vq->free_head;
@@ -233,11 +235,13 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
 
     vq->free_head = readw(vq->desc + sizeof(struct vring_desc) * idx +
                           offsetof(struct vring_desc, next));
+    vq->tokens[idx] = token;
 
     return idx;
 }
 
-uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
+uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect,
+                                 void *token)
 {
     uint16_t idx = vq->free_head;
 
@@ -258,6 +262,7 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect)
 
     vq->free_head = readw(vq->desc + sizeof(struct vring_desc) * idx +
                           offsetof(struct vring_desc, next));
+    vq->tokens[idx] = token;
 
     return idx;
 }
tests/libqos/virtio.h +6 -2
index c73fd8c..b5154a9 100644
@@ -15,6 +15,8 @@
 
 #define QVIRTIO_F_BAD_FEATURE           0x40000000
 
+#define QVIRTIO_QUEUE_MAX 1024 /* descriptors */
+
 typedef struct QVirtioDevice {
     /* Device type */
     uint16_t device_type;
@@ -31,6 +33,7 @@ typedef struct QVirtQueue {
     uint32_t align;
     bool indirect;
     bool event;
+    void *tokens[QVIRTIO_QUEUE_MAX];
 } QVirtQueue;
 
 typedef struct QVRingIndirectDesc {
@@ -125,8 +128,9 @@ QVRingIndirectDesc *qvring_indirect_desc_setup(QVirtioDevice *d,
 void qvring_indirect_desc_add(QVRingIndirectDesc *indirect, uint64_t data,
                                                     uint32_t len, bool write);
 uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
-                                                                    bool next);
-uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect);
+                        bool next, void *token);
+uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect,
+                                 void *token);
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head);
 
tests/virtio-blk-test.c +27 -27

120 lines not on this page · raw message

tests/virtio-net-test.c +3 -3
index 9c8114d..ed21606 100644
@@ -101,7 +101,7 @@ static void rx_test(const QVirtioBus *bus, QVirtioDevice *dev,
 
     req_addr = guest_alloc(alloc, 64);
 
-    free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
+    free_head = qvirtqueue_add(vq, req_addr, 64, true, false, NULL);
     qvirtqueue_kick(bus, dev, vq, free_head);
 
     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
@@ -127,7 +127,7 @@ static void tx_test(const QVirtioBus *bus, QVirtioDevice *dev,
     req_addr = guest_alloc(alloc, 64);
     memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
 
-    free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
+    free_head = qvirtqueue_add(vq, req_addr, 64, false, false, NULL);
     qvirtqueue_kick(bus, dev, vq, free_head);
 
     qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
@@ -163,7 +163,7 @@ static void rx_stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev,
 
     req_addr = guest_alloc(alloc, 64);
 
-    free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
+    free_head = qvirtqueue_add(vq, req_addr, 64, true, false, NULL);
     qvirtqueue_kick(bus, dev, vq, free_head);
 
     qmp("{ 'execute' : 'stop'}");
tests/virtio-scsi-test.c +4 -4
index 71f5aeb..8eb251f 100644
@@ -103,20 +103,20 @@ static uint8_t virtio_scsi_do_command(QVirtIOSCSI *vs, const uint8_t *cdb,
 
     /* Add request header */
     req_addr = qvirtio_scsi_alloc(vs, sizeof(req), &req);
-    free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true);
+    free_head = qvirtqueue_add(vq, req_addr, sizeof(req), false, true, NULL);
 
     if (data_out_len) {
         data_out_addr = qvirtio_scsi_alloc(vs, data_out_len, data_out);
-        qvirtqueue_add(vq, data_out_addr, data_out_len, false, true);
+        qvirtqueue_add(vq, data_out_addr, data_out_len, false, true, NULL);
     }
 
     /* Add response header */
     resp_addr = qvirtio_scsi_alloc(vs, sizeof(resp), &resp);
-    qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len);
+    qvirtqueue_add(vq, resp_addr, sizeof(resp), true, !!data_in_len, NULL);
 
     if (data_in_len) {
         data_in_addr = qvirtio_scsi_alloc(vs, data_in_len, data_in);
-        qvirtqueue_add(vq, data_in_addr, data_in_len, true, false);
+        qvirtqueue_add(vq, data_in_addr, data_in_len, true, false, NULL);
     }
 
     qvirtqueue_kick(&qvirtio_pci, vs->dev, vq, free_head);
Signature · 1 line
2.5.5
#4 lore raw

PATCH 4/5 libqos: add qvirtqueue_get_buf()

In reply to Stefan Hajnoczi

Add a function to pop buffers off the used ring. Up until now the virtio device tests were so simple that they could probe the buffer memory to detect completed requests. That means they never actually reclaimed buffers from the used ring.

The qvirtqueue_get_buf() function will allow more sophisticated test cases to be written without probing bytes in multiple buffers.

---
 tests/libqos/virtio.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio.h |  2 ++
 2 files changed, 81 insertions(+)
tests/libqos/virtio.c +79 -0
index bc9c24c..948d722 100644
@@ -141,6 +141,7 @@ void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
         + vq->align - 1) & ~(vq->align - 1));
     vq->free_head = 0;
     vq->num_free = vq->size;
+    vq->last_used_idx = 0;
 
     for (i = 0; i < vq->size - 1; i++) {
         /* vq->desc[i].addr */
@@ -267,6 +268,84 @@ uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect,
     return idx;
 }
 
+static uint16_t get_desc_flags(QVirtQueue *vq, uint16_t idx)
+{
+    return readw(vq->desc + idx * sizeof(struct vring_desc) +
+                 offsetof(struct vring_desc, flags));
+}
+
+static uint16_t get_desc_next(QVirtQueue *vq, uint16_t idx)
+{
+    return readw(vq->desc + idx * sizeof(struct vring_desc) +
+                 offsetof(struct vring_desc, next));
+}
+
+static void set_desc_next(QVirtQueue *vq, uint16_t idx, uint16_t val)
+{
+    writew(vq->desc + idx * sizeof(struct vring_desc) +
+           offsetof(struct vring_desc, next), val);
+}
+
+static void free_descs(QVirtQueue *vq, uint16_t head)
+{
+    uint16_t idx = head;
+
+    for (idx = head;
+         get_desc_flags(vq, idx) & VRING_DESC_F_NEXT;
+         idx = get_desc_next(vq, idx)) {
+        vq->num_free++;
+    }
+    vq->num_free++; /* also count the final descriptor */
+
+    /* Add descriptors to free list */
+    set_desc_next(vq, idx, vq->free_head);
+    vq->free_head = head;
+}
+
+/**
+ * qvirtqueue_get_buf:
+ * @vq: the virtqueue
+ * @len: the number of bytes written by the device
+ *
+ * Get the next used buffer from a virtqueue.
+ *
+ * Returns: the token given to qvirtqueue_add()/qvirtqueue_add_indirect() or
+ * NULL if there are no more buffers.
+ */
+void *qvirtqueue_get_buf(QVirtQueue *vq, unsigned int *len)
+{
+    unsigned int head;
+    void *token;
+    /* vq->used->idx */
+    uint16_t idx = readw(vq->used + offsetof(struct vring_used, idx));
+
+    if (vq->last_used_idx == idx) {
+        return NULL;
+    }
+
+    idx = vq->last_used_idx % vq->size;
+
+    /* vq->used->ring[idx].id */
+    head = readl(vq->used + offsetof(struct vring_used, ring) +
+                 sizeof(struct vring_used_elem) * idx);
+    g_assert_cmpint(head, <, vq->size);
+
+    g_assert(vq->tokens[head]);
+    token = vq->tokens[head];
+    vq->tokens[head] = NULL;
+
+    free_descs(vq, head);
+
+    /* vq->used->ring[idx].len */
+    *len = readl(vq->used + offsetof(struct vring_used, ring) +
+                 sizeof(struct vring_used_elem) * idx +
+                 offsetof(struct vring_used_elem, len));
+
+    vq->last_used_idx++;
+
+    return token;
+}
+
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head)
 {
tests/libqos/virtio.h +2 -0
index b5154a9..4fea8ef 100644
@@ -27,6 +27,7 @@ typedef struct QVirtQueue {
     uint64_t avail; /* This points to a struct vring_avail */
     uint64_t used; /* This points to a struct vring_desc */
     uint16_t index;
+    uint16_t last_used_idx;
     uint32_t size;
     uint32_t free_head;
     uint32_t num_free;
@@ -131,6 +132,7 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write,
                         bool next, void *token);
 uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect,
                                  void *token);
+void *qvirtqueue_get_buf(QVirtQueue *vq, unsigned int *len);
 void qvirtqueue_kick(const QVirtioBus *bus, QVirtioDevice *d, QVirtQueue *vq,
                                                             uint32_t free_head);
 
Signature · 1 line
2.5.5
#5 lore raw

PATCH 5/5 libqos: add qvirtio_wait_queue_buf()

In reply to Stefan Hajnoczi

Like qvirtqueue_get_buf() but blocking and checks for the expected buffer token. New virtio device tests are expected to use this function as a convenient way to pop buffers off the used ring.

---
 tests/libqos/virtio.c | 28 ++++++++++++++++++++++++++++
 tests/libqos/virtio.h |  3 +++
 2 files changed, 31 insertions(+)
tests/libqos/virtio.c +28 -0
index 948d722..b1591db 100644
@@ -131,6 +131,34 @@ void qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
     }
 }
 
+/**
+ * qvirtio_wait_queue_buf:
+ * @bus: the virtio bus
+ * @d: the virtio device
+ * @vq: the virtqueue
+ *
+ * Wait for the next buffer and check that it has the given token.
+ *
+ * Returns: the number of bytes written by the device.
+ */
+unsigned int qvirtio_wait_queue_buf(const QVirtioBus *bus, QVirtioDevice *d,
+                                    QVirtQueue *vq, void *token,
+                                    gint64 timeout_us)
+{
+    unsigned int len;
+    void *actual_token;
+
+    actual_token = qvirtqueue_get_buf(vq, &len);
+    if (!actual_token) {
+        qvirtio_wait_queue_isr(bus, d, vq, timeout_us);
+        actual_token = qvirtqueue_get_buf(vq, &len);
+    }
+    g_assert(actual_token != NULL);
+    g_assert(actual_token == token);
+
+    return len;
+}
+
 void qvring_init(const QGuestAllocator *alloc, QVirtQueue *vq, uint64_t addr)
 {
     int i;
tests/libqos/virtio.h +3 -0
index 4fea8ef..5d40b23 100644
@@ -120,6 +120,9 @@ uint8_t qvirtio_wait_status_byte_no_isr(const QVirtioBus *bus,
                                         gint64 timeout_us);
 void qvirtio_wait_config_isr(const QVirtioBus *bus, QVirtioDevice *d,
                              gint64 timeout_us);
+unsigned int qvirtio_wait_queue_buf(const QVirtioBus *bus, QVirtioDevice *d,
+                                    QVirtQueue *vq, void *token,
+                                    gint64 timeout_us);
 QVirtQueue *qvirtqueue_setup(const QVirtioBus *bus, QVirtioDevice *d,
                                         QGuestAllocator *alloc, uint16_t index);
 
Signature · 1 line
2.5.5
#6 lore raw
In reply to Stefan Hajnoczi
Quoted 20 lines · Stefan Hajnoczi

The libqos virtio framework currently offers no way to pop buffers off the virtqueue. Existing test cases are trivial and therefore get away without reclaiming buffers. More extensive tests really do need to pop buffers and do the housekeeping associated with the 'used' ring.

This patch series adds qvirtqueue_get_buf() and qvirtio_wait_queue_buf() for popping buffers off the virtqueue. The code is based on my recent "[PATCH for-2.7 0/8] libqos: use standard virtio headers" series which has not been merged yet.

I am sending a new test case for the virtio-vsock device in a separate email thread. The virtio-vsock test uses the new qvirtio_wait_queue_buf() function.

Stefan Hajnoczi (5):
libqos: fix virtio descriptor free list
libqos: fix virtio num_free descriptor counter
libqos: add virtio buffer token
libqos: add qvirtqueue_get_buf()
libqos: add qvirtio_wait_queue_buf()

Quoted 6 lines
 tests/libqos/virtio.c    | 146 +++++++++++++++++++++++++++++++++++++++++++----
 tests/libqos/virtio.h    |  13 ++++-
 tests/virtio-blk-test.c  |  54 +++++++++---------
 tests/virtio-net-test.c  |   6 +-
 tests/virtio-scsi-test.c |   8 +--
 5 files changed, 181 insertions(+), 46 deletions(-)

I'll resend this again in the future.

For the time being there is no qvirtio_wait_queue_buf() user in the source tree so let's wait. My vhost-vsock tests will use this and any more advanced virtio device tests will need it too.