Skip to content

Commit 6f5fd83

Browse files
stefanhaRHmstsirkin
authored andcommitted
libvhost-user: support many virtqueues
Currently libvhost-user is hardcoded to at most 8 virtqueues. The device backend should decide the number of virtqueues, not libvhost-user. This is important for multiqueue device backends where the guest driver needs an accurate number of virtqueues. This change breaks libvhost-user and libvhost-user-glib API stability. There is no stability guarantee yet, so make this change now and update all in-tree library users. This patch touches up vhost-user-blk, vhost-user-gpu, vhost-user-input, vhost-user-scsi, and vhost-user-bridge. If the device has a fixed number of queues that exact number is used. Otherwise the previous default of 8 virtqueues is used. vu_init() and vug_init() can now fail if malloc() returns NULL. I considered aborting with an error in libvhost-user but it should be safe to instantiate new vhost-user instances at runtime without risk of terminating the process. Therefore callers need to handle the vu_init() failure now. vhost-user-blk and vhost-user-scsi duplicate virtqueue index checks that are already performed by libvhost-user. This code would need to be modified to use max_queues but remove it completely instead since it's redundant. Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20190626074815.19994-3-stefanha@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent db68f4f commit 6f5fd83

File tree

9 files changed

+105
-51
lines changed

9 files changed

+105
-51
lines changed

contrib/libvhost-user/libvhost-user-glib.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,24 @@ static void vug_watch(VuDev *dev, int condition, void *data)
131131
}
132132
}
133133

134-
void
135-
vug_init(VugDev *dev, int socket,
134+
bool
135+
vug_init(VugDev *dev, uint16_t max_queues, int socket,
136136
vu_panic_cb panic, const VuDevIface *iface)
137137
{
138138
g_assert(dev);
139139
g_assert(iface);
140140

141-
vu_init(&dev->parent, socket, panic, set_watch, remove_watch, iface);
141+
if (!vu_init(&dev->parent, max_queues, socket, panic, set_watch,
142+
remove_watch, iface)) {
143+
return false;
144+
}
145+
142146
dev->fdmap = g_hash_table_new_full(NULL, NULL, NULL,
143147
(GDestroyNotify) g_source_destroy);
144148

145149
dev->src = vug_source_new(dev, socket, G_IO_IN, vug_watch, NULL);
150+
151+
return true;
146152
}
147153

148154
void

contrib/libvhost-user/libvhost-user-glib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef struct VugDev {
2525
GSource *src;
2626
} VugDev;
2727

28-
void vug_init(VugDev *dev, int socket,
28+
bool vug_init(VugDev *dev, uint16_t max_queues, int socket,
2929
vu_panic_cb panic, const VuDevIface *iface);
3030
void vug_deinit(VugDev *dev);
3131

contrib/libvhost-user/libvhost-user.c

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,9 @@ vu_get_features_exec(VuDev *dev, VhostUserMsg *vmsg)
493493
static void
494494
vu_set_enable_all_rings(VuDev *dev, bool enabled)
495495
{
496-
int i;
496+
uint16_t i;
497497

498-
for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
498+
for (i = 0; i < dev->max_queues; i++) {
499499
dev->vq[i].enable = enabled;
500500
}
501501
}
@@ -916,7 +916,7 @@ vu_check_queue_msg_file(VuDev *dev, VhostUserMsg *vmsg)
916916
{
917917
int index = vmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
918918

919-
if (index >= VHOST_MAX_NR_VIRTQUEUE) {
919+
if (index >= dev->max_queues) {
920920
vmsg_close_fds(vmsg);
921921
vu_panic(dev, "Invalid queue index: %u", index);
922922
return false;
@@ -1213,7 +1213,7 @@ vu_set_vring_enable_exec(VuDev *dev, VhostUserMsg *vmsg)
12131213
DPRINT("State.index: %d\n", index);
12141214
DPRINT("State.enable: %d\n", enable);
12151215

1216-
if (index >= VHOST_MAX_NR_VIRTQUEUE) {
1216+
if (index >= dev->max_queues) {
12171217
vu_panic(dev, "Invalid vring_enable index: %u", index);
12181218
return false;
12191219
}
@@ -1582,7 +1582,7 @@ vu_deinit(VuDev *dev)
15821582
}
15831583
dev->nregions = 0;
15841584

1585-
for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
1585+
for (i = 0; i < dev->max_queues; i++) {
15861586
VuVirtq *vq = &dev->vq[i];
15871587

15881588
if (vq->call_fd != -1) {
@@ -1627,18 +1627,23 @@ vu_deinit(VuDev *dev)
16271627
if (dev->sock != -1) {
16281628
close(dev->sock);
16291629
}
1630+
1631+
free(dev->vq);
1632+
dev->vq = NULL;
16301633
}
16311634

1632-
void
1635+
bool
16331636
vu_init(VuDev *dev,
1637+
uint16_t max_queues,
16341638
int socket,
16351639
vu_panic_cb panic,
16361640
vu_set_watch_cb set_watch,
16371641
vu_remove_watch_cb remove_watch,
16381642
const VuDevIface *iface)
16391643
{
1640-
int i;
1644+
uint16_t i;
16411645

1646+
assert(max_queues > 0);
16421647
assert(socket >= 0);
16431648
assert(set_watch);
16441649
assert(remove_watch);
@@ -1654,18 +1659,28 @@ vu_init(VuDev *dev,
16541659
dev->iface = iface;
16551660
dev->log_call_fd = -1;
16561661
dev->slave_fd = -1;
1657-
for (i = 0; i < VHOST_MAX_NR_VIRTQUEUE; i++) {
1662+
dev->max_queues = max_queues;
1663+
1664+
dev->vq = malloc(max_queues * sizeof(dev->vq[0]));
1665+
if (!dev->vq) {
1666+
DPRINT("%s: failed to malloc virtqueues\n", __func__);
1667+
return false;
1668+
}
1669+
1670+
for (i = 0; i < max_queues; i++) {
16581671
dev->vq[i] = (VuVirtq) {
16591672
.call_fd = -1, .kick_fd = -1, .err_fd = -1,
16601673
.notification = true,
16611674
};
16621675
}
1676+
1677+
return true;
16631678
}
16641679

16651680
VuVirtq *
16661681
vu_get_queue(VuDev *dev, int qidx)
16671682
{
1668-
assert(qidx < VHOST_MAX_NR_VIRTQUEUE);
1683+
assert(qidx < dev->max_queues);
16691684
return &dev->vq[qidx];
16701685
}
16711686

contrib/libvhost-user/libvhost-user.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#define VHOST_USER_F_PROTOCOL_FEATURES 30
2626
#define VHOST_LOG_PAGE 4096
2727

28-
#define VHOST_MAX_NR_VIRTQUEUE 8
2928
#define VIRTQUEUE_MAX_SIZE 1024
3029

3130
#define VHOST_MEMORY_MAX_NREGIONS 8
@@ -353,7 +352,7 @@ struct VuDev {
353352
int sock;
354353
uint32_t nregions;
355354
VuDevRegion regions[VHOST_MEMORY_MAX_NREGIONS];
356-
VuVirtq vq[VHOST_MAX_NR_VIRTQUEUE];
355+
VuVirtq *vq;
357356
VuDevInflightInfo inflight_info;
358357
int log_call_fd;
359358
int slave_fd;
@@ -362,6 +361,7 @@ struct VuDev {
362361
uint64_t features;
363362
uint64_t protocol_features;
364363
bool broken;
364+
uint16_t max_queues;
365365

366366
/* @set_watch: add or update the given fd to the watch set,
367367
* call cb when condition is met */
@@ -391,15 +391,19 @@ typedef struct VuVirtqElement {
391391
/**
392392
* vu_init:
393393
* @dev: a VuDev context
394+
* @max_queues: maximum number of virtqueues
394395
* @socket: the socket connected to vhost-user master
395396
* @panic: a panic callback
396397
* @set_watch: a set_watch callback
397398
* @remove_watch: a remove_watch callback
398399
* @iface: a VuDevIface structure with vhost-user device callbacks
399400
*
400401
* Intializes a VuDev vhost-user context.
402+
*
403+
* Returns: true on success, false on failure.
401404
**/
402-
void vu_init(VuDev *dev,
405+
bool vu_init(VuDev *dev,
406+
uint16_t max_queues,
403407
int socket,
404408
vu_panic_cb panic,
405409
vu_set_watch_cb set_watch,

contrib/vhost-user-blk/vhost-user-blk.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include <sys/ioctl.h>
2626
#endif
2727

28+
enum {
29+
VHOST_USER_BLK_MAX_QUEUES = 8,
30+
};
31+
2832
struct virtio_blk_inhdr {
2933
unsigned char status;
3034
};
@@ -334,12 +338,6 @@ static void vub_process_vq(VuDev *vu_dev, int idx)
334338
VuVirtq *vq;
335339
int ret;
336340

337-
if ((idx < 0) || (idx >= VHOST_MAX_NR_VIRTQUEUE)) {
338-
fprintf(stderr, "VQ Index out of range: %d\n", idx);
339-
vub_panic_cb(vu_dev, NULL);
340-
return;
341-
}
342-
343341
gdev = container_of(vu_dev, VugDev, parent);
344342
vdev_blk = container_of(gdev, VubDev, parent);
345343
assert(vdev_blk);
@@ -631,7 +629,11 @@ int main(int argc, char **argv)
631629
vdev_blk->enable_ro = true;
632630
}
633631

634-
vug_init(&vdev_blk->parent, csock, vub_panic_cb, &vub_iface);
632+
if (!vug_init(&vdev_blk->parent, VHOST_USER_BLK_MAX_QUEUES, csock,
633+
vub_panic_cb, &vub_iface)) {
634+
fprintf(stderr, "Failed to initialized libvhost-user-glib\n");
635+
goto err;
636+
}
635637

636638
g_main_loop_run(vdev_blk->loop);
637639

contrib/vhost-user-gpu/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
#include "virgl.h"
2626
#include "vugbm.h"
2727

28+
enum {
29+
VHOST_USER_GPU_MAX_QUEUES = 2,
30+
};
31+
2832
struct virtio_gpu_simple_resource {
2933
uint32_t resource_id;
3034
uint32_t width;
@@ -1169,7 +1173,10 @@ main(int argc, char *argv[])
11691173
exit(EXIT_FAILURE);
11701174
}
11711175

1172-
vug_init(&g.dev, fd, vg_panic, &vuiface);
1176+
if (!vug_init(&g.dev, VHOST_USER_GPU_MAX_QUEUES, fd, vg_panic, &vuiface)) {
1177+
g_printerr("Failed to initialize libvhost-user-glib.\n");
1178+
exit(EXIT_FAILURE);
1179+
}
11731180

11741181
loop = g_main_loop_new(NULL, FALSE);
11751182
g_main_loop_run(loop);

contrib/vhost-user-input/main.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "standard-headers/linux/virtio_input.h"
1818
#include "qapi/error.h"
1919

20+
enum {
21+
VHOST_USER_INPUT_MAX_QUEUES = 2,
22+
};
23+
2024
typedef struct virtio_input_event virtio_input_event;
2125
typedef struct virtio_input_config virtio_input_config;
2226

@@ -384,7 +388,12 @@ main(int argc, char *argv[])
384388
g_printerr("Invalid vhost-user socket.\n");
385389
exit(EXIT_FAILURE);
386390
}
387-
vug_init(&vi.dev, fd, vi_panic, &vuiface);
391+
392+
if (!vug_init(&vi.dev, VHOST_USER_INPUT_MAX_QUEUES, fd, vi_panic,
393+
&vuiface)) {
394+
g_printerr("Failed to initialize libvhost-user-glib.\n");
395+
exit(EXIT_FAILURE);
396+
}
388397

389398
loop = g_main_loop_new(NULL, FALSE);
390399
g_main_loop_run(loop);

contrib/vhost-user-scsi/vhost-user-scsi.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
#define VUS_ISCSI_INITIATOR "iqn.2016-11.com.nutanix:vhost-user-scsi"
2121

22+
enum {
23+
VHOST_USER_SCSI_MAX_QUEUES = 8,
24+
};
25+
2226
typedef struct VusIscsiLun {
2327
struct iscsi_context *iscsi_ctx;
2428
int iscsi_lun;
@@ -231,11 +235,6 @@ static void vus_proc_req(VuDev *vu_dev, int idx)
231235

232236
gdev = container_of(vu_dev, VugDev, parent);
233237
vdev_scsi = container_of(gdev, VusDev, parent);
234-
if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
235-
g_warning("VQ Index out of range: %d", idx);
236-
vus_panic_cb(vu_dev, NULL);
237-
return;
238-
}
239238

240239
vq = vu_get_queue(vu_dev, idx);
241240
if (!vq) {
@@ -295,12 +294,6 @@ static void vus_queue_set_started(VuDev *vu_dev, int idx, bool started)
295294

296295
assert(vu_dev);
297296

298-
if (idx < 0 || idx >= VHOST_MAX_NR_VIRTQUEUE) {
299-
g_warning("VQ Index out of range: %d", idx);
300-
vus_panic_cb(vu_dev, NULL);
301-
return;
302-
}
303-
304297
vq = vu_get_queue(vu_dev, idx);
305298

306299
if (idx == 0 || idx == 1) {
@@ -398,7 +391,11 @@ int main(int argc, char **argv)
398391
goto err;
399392
}
400393

401-
vug_init(&vdev_scsi->parent, csock, vus_panic_cb, &vus_iface);
394+
if (!vug_init(&vdev_scsi->parent, VHOST_USER_SCSI_MAX_QUEUES, csock,
395+
vus_panic_cb, &vus_iface)) {
396+
g_printerr("Failed to initialize libvhost-user-glib\n");
397+
goto err;
398+
}
402399

403400
g_main_loop_run(vdev_scsi->loop);
404401

tests/vhost-user-bridge.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
} \
4646
} while (0)
4747

48+
enum {
49+
VHOST_USER_BRIDGE_MAX_QUEUES = 8,
50+
};
51+
4852
typedef void (*CallbackFunc)(int sock, void *ctx);
4953

5054
typedef struct Event {
@@ -512,12 +516,16 @@ vubr_accept_cb(int sock, void *ctx)
512516
}
513517
DPRINT("Got connection from remote peer on sock %d\n", conn_fd);
514518

515-
vu_init(&dev->vudev,
516-
conn_fd,
517-
vubr_panic,
518-
vubr_set_watch,
519-
vubr_remove_watch,
520-
&vuiface);
519+
if (!vu_init(&dev->vudev,
520+
VHOST_USER_BRIDGE_MAX_QUEUES,
521+
conn_fd,
522+
vubr_panic,
523+
vubr_set_watch,
524+
vubr_remove_watch,
525+
&vuiface)) {
526+
fprintf(stderr, "Failed to initialize libvhost-user\n");
527+
exit(1);
528+
}
521529

522530
dispatcher_add(&dev->dispatcher, conn_fd, ctx, vubr_receive_cb);
523531
dispatcher_remove(&dev->dispatcher, sock);
@@ -560,12 +568,18 @@ vubr_new(const char *path, bool client)
560568
if (connect(dev->sock, (struct sockaddr *)&un, len) == -1) {
561569
vubr_die("connect");
562570
}
563-
vu_init(&dev->vudev,
564-
dev->sock,
565-
vubr_panic,
566-
vubr_set_watch,
567-
vubr_remove_watch,
568-
&vuiface);
571+
572+
if (!vu_init(&dev->vudev,
573+
VHOST_USER_BRIDGE_MAX_QUEUES,
574+
dev->sock,
575+
vubr_panic,
576+
vubr_set_watch,
577+
vubr_remove_watch,
578+
&vuiface)) {
579+
fprintf(stderr, "Failed to initialize libvhost-user\n");
580+
exit(1);
581+
}
582+
569583
cb = vubr_receive_cb;
570584
}
571585

@@ -584,7 +598,7 @@ static void *notifier_thread(void *arg)
584598
int qidx;
585599

586600
while (true) {
587-
for (qidx = 0; qidx < VHOST_MAX_NR_VIRTQUEUE; qidx++) {
601+
for (qidx = 0; qidx < VHOST_USER_BRIDGE_MAX_QUEUES; qidx++) {
588602
uint16_t *n = vubr->notifier.addr + pagesize * qidx;
589603

590604
if (*n == qidx) {
@@ -616,7 +630,7 @@ vubr_host_notifier_setup(VubrDev *dev)
616630
void *addr;
617631
int fd;
618632

619-
length = getpagesize() * VHOST_MAX_NR_VIRTQUEUE;
633+
length = getpagesize() * VHOST_USER_BRIDGE_MAX_QUEUES;
620634

621635
fd = mkstemp(template);
622636
if (fd < 0) {

0 commit comments

Comments
 (0)