]> git.proxmox.com Git - mirror_qemu.git/blame - tests/vhost-user-test.c
9pfs: use coroutine_fn annotation in hw/9pfs/9p.[ch]
[mirror_qemu.git] / tests / vhost-user-test.c
CommitLineData
a77e6b14
NN
1/*
2 * QTest testcase for the vhost-user
3 *
4 * Copyright (c) 2014 Virtual Open Systems Sarl.
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 *
9 */
10
681c28a3 11#include "qemu/osdep.h"
bd95939f 12
a77e6b14
NN
13#include "libqtest.h"
14#include "qemu/option.h"
b1819747 15#include "qemu/range.h"
a9c94277 16#include "qemu/sockets.h"
a77e6b14
NN
17#include "sysemu/char.h"
18#include "sysemu/sysemu.h"
cdafe929
EH
19#include "libqos/libqos.h"
20#include "libqos/pci-pc.h"
21#include "libqos/virtio-pci.h"
a77e6b14 22
ed0a8d92
MAL
23#include "libqos/pci-pc.h"
24#include "libqos/virtio-pci.h"
25#include "libqos/malloc-pc.h"
26#include "hw/virtio/virtio-net.h"
27
a77e6b14 28#include <linux/vhost.h>
cdafe929
EH
29#include <linux/virtio_ids.h>
30#include <linux/virtio_net.h>
a77e6b14 31#include <sys/vfs.h>
a77e6b14 32
30de46db
GA
33/* GLIB version compatibility flags */
34#if !GLIB_CHECK_VERSION(2, 26, 0)
35#define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
36#endif
37
38#if GLIB_CHECK_VERSION(2, 28, 0)
39#define HAVE_MONOTONIC_TIME
40#endif
41
704b2168 42#define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM,"\
a77e6b14 43 "mem-path=%s,share=on -numa node,memdev=mem"
4616e359 44#define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
704b2168 45#define QEMU_CMD_NETDEV " -netdev vhost-user,id=net0,chardev=%s,vhostforce"
cdafe929 46#define QEMU_CMD_NET " -device virtio-net-pci,netdev=net0"
a77e6b14 47
cdafe929 48#define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \
b5c6eaf1 49 QEMU_CMD_NETDEV QEMU_CMD_NET
a77e6b14
NN
50
51#define HUGETLBFS_MAGIC 0x958458f6
52
53/*********** FROM hw/virtio/vhost-user.c *************************************/
54
55#define VHOST_MEMORY_MAX_NREGIONS 8
56
8a9b6b37 57#define VHOST_USER_F_PROTOCOL_FEATURES 30
ed0a8d92 58#define VHOST_USER_PROTOCOL_F_MQ 0
b1819747
MAL
59#define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
60
61#define VHOST_LOG_PAGE 0x1000
8a9b6b37 62
a77e6b14
NN
63typedef enum VhostUserRequest {
64 VHOST_USER_NONE = 0,
65 VHOST_USER_GET_FEATURES = 1,
66 VHOST_USER_SET_FEATURES = 2,
67 VHOST_USER_SET_OWNER = 3,
60915dc4 68 VHOST_USER_RESET_OWNER = 4,
a77e6b14
NN
69 VHOST_USER_SET_MEM_TABLE = 5,
70 VHOST_USER_SET_LOG_BASE = 6,
71 VHOST_USER_SET_LOG_FD = 7,
72 VHOST_USER_SET_VRING_NUM = 8,
73 VHOST_USER_SET_VRING_ADDR = 9,
74 VHOST_USER_SET_VRING_BASE = 10,
75 VHOST_USER_GET_VRING_BASE = 11,
76 VHOST_USER_SET_VRING_KICK = 12,
77 VHOST_USER_SET_VRING_CALL = 13,
78 VHOST_USER_SET_VRING_ERR = 14,
8a9b6b37
MT
79 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
80 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
ed0a8d92 81 VHOST_USER_GET_QUEUE_NUM = 17,
87656d50 82 VHOST_USER_SET_VRING_ENABLE = 18,
a77e6b14
NN
83 VHOST_USER_MAX
84} VhostUserRequest;
85
86typedef struct VhostUserMemoryRegion {
87 uint64_t guest_phys_addr;
88 uint64_t memory_size;
89 uint64_t userspace_addr;
d6970e3b 90 uint64_t mmap_offset;
a77e6b14
NN
91} VhostUserMemoryRegion;
92
93typedef struct VhostUserMemory {
94 uint32_t nregions;
95 uint32_t padding;
96 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
97} VhostUserMemory;
98
2b8819c6
VK
99typedef struct VhostUserLog {
100 uint64_t mmap_size;
101 uint64_t mmap_offset;
102} VhostUserLog;
103
a77e6b14
NN
104typedef struct VhostUserMsg {
105 VhostUserRequest request;
106
107#define VHOST_USER_VERSION_MASK (0x3)
108#define VHOST_USER_REPLY_MASK (0x1<<2)
109 uint32_t flags;
110 uint32_t size; /* the following payload size */
111 union {
2b8819c6
VK
112#define VHOST_USER_VRING_IDX_MASK (0xff)
113#define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
a77e6b14
NN
114 uint64_t u64;
115 struct vhost_vring_state state;
116 struct vhost_vring_addr addr;
117 VhostUserMemory memory;
2b8819c6 118 VhostUserLog log;
12ebf690 119 } payload;
a77e6b14
NN
120} QEMU_PACKED VhostUserMsg;
121
122static VhostUserMsg m __attribute__ ((unused));
123#define VHOST_USER_HDR_SIZE (sizeof(m.request) \
124 + sizeof(m.flags) \
125 + sizeof(m.size))
126
127#define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
128
129/* The version of the protocol we support */
130#define VHOST_USER_VERSION (0x1)
131/*****************************************************************************/
132
9294d76c
MAL
133enum {
134 TEST_FLAGS_OK,
135 TEST_FLAGS_DISCONNECT,
136 TEST_FLAGS_BAD,
137 TEST_FLAGS_END,
138};
139
ae31fb54
MAL
140typedef struct TestServer {
141 gchar *socket_path;
a899b1ea 142 gchar *mig_path;
ae31fb54
MAL
143 gchar *chr_name;
144 CharDriverState *chr;
145 int fds_num;
146 int fds[VHOST_MEMORY_MAX_NREGIONS];
147 VhostUserMemory memory;
634d39b4
PB
148 CompatGMutex data_mutex;
149 CompatGCond data_cond;
b1819747 150 int log_fd;
d08e42a1 151 uint64_t rings;
5d443f5a 152 bool test_fail;
9294d76c 153 int test_flags;
ed0a8d92 154 int queues;
ae31fb54 155} TestServer;
bd95939f 156
704b2168
MAL
157static const char *tmpfs;
158static const char *root;
159
cdafe929
EH
160static void init_virtio_dev(TestServer *s)
161{
162 QPCIBus *bus;
163 QVirtioPCIDevice *dev;
164 uint32_t features;
165
2ecd7e2f 166 bus = qpci_init_pc(NULL);
cdafe929
EH
167 g_assert_nonnull(bus);
168
169 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
170 g_assert_nonnull(dev);
171
172 qvirtio_pci_device_enable(dev);
173 qvirtio_reset(&qvirtio_pci, &dev->vdev);
174 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
175 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
176
177 features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
178 features = features & VIRTIO_NET_F_MAC;
179 qvirtio_set_features(&qvirtio_pci, &dev->vdev, features);
180
181 qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
182}
183
ae31fb54 184static void wait_for_fds(TestServer *s)
a77e6b14 185{
a77e6b14 186 gint64 end_time;
a77e6b14 187
ae31fb54 188 g_mutex_lock(&s->data_mutex);
a77e6b14 189
ca06d9cc 190 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
ae31fb54
MAL
191 while (!s->fds_num) {
192 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
a77e6b14 193 /* timeout has passed */
ae31fb54 194 g_assert(s->fds_num);
a77e6b14
NN
195 break;
196 }
197 }
198
199 /* check for sanity */
ae31fb54
MAL
200 g_assert_cmpint(s->fds_num, >, 0);
201 g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
a77e6b14 202
ae31fb54 203 g_mutex_unlock(&s->data_mutex);
cf72b57f
MAL
204}
205
041088c7 206static void read_guest_mem(const void *data)
cf72b57f 207{
041088c7 208 TestServer *s = (void *)data;
cf72b57f
MAL
209 uint32_t *guest_mem;
210 int i, j;
211 size_t size;
212
ae31fb54 213 wait_for_fds(s);
cf72b57f 214
ae31fb54 215 g_mutex_lock(&s->data_mutex);
cf72b57f 216
a77e6b14 217 /* iterate all regions */
ae31fb54 218 for (i = 0; i < s->fds_num; i++) {
a77e6b14
NN
219
220 /* We'll check only the region statring at 0x0*/
ae31fb54 221 if (s->memory.regions[i].guest_phys_addr != 0x0) {
a77e6b14
NN
222 continue;
223 }
224
ae31fb54 225 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
a77e6b14 226
ae31fb54
MAL
227 size = s->memory.regions[i].memory_size +
228 s->memory.regions[i].mmap_offset;
d6970e3b
NN
229
230 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
ae31fb54 231 MAP_SHARED, s->fds[i], 0);
d6970e3b
NN
232
233 g_assert(guest_mem != MAP_FAILED);
ae31fb54 234 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
a77e6b14
NN
235
236 for (j = 0; j < 256; j++) {
ae31fb54 237 uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4);
a77e6b14
NN
238 uint32_t b = guest_mem[j];
239
240 g_assert_cmpint(a, ==, b);
241 }
242
ae31fb54 243 munmap(guest_mem, s->memory.regions[i].memory_size);
a77e6b14
NN
244 }
245
ae31fb54 246 g_mutex_unlock(&s->data_mutex);
a77e6b14
NN
247}
248
249static void *thread_function(void *data)
250{
9732baf6 251 GMainLoop *loop = data;
a77e6b14
NN
252 g_main_loop_run(loop);
253 return NULL;
254}
255
256static int chr_can_read(void *opaque)
257{
258 return VHOST_USER_HDR_SIZE;
259}
260
261static void chr_read(void *opaque, const uint8_t *buf, int size)
262{
ae31fb54
MAL
263 TestServer *s = opaque;
264 CharDriverState *chr = s->chr;
a77e6b14
NN
265 VhostUserMsg msg;
266 uint8_t *p = (uint8_t *) &msg;
267 int fd;
268
5d443f5a
MAL
269 if (s->test_fail) {
270 qemu_chr_disconnect(chr);
271 /* now switch to non-failure */
272 s->test_fail = false;
273 }
274
a77e6b14
NN
275 if (size != VHOST_USER_HDR_SIZE) {
276 g_test_message("Wrong message size received %d\n", size);
277 return;
278 }
279
ae31fb54 280 g_mutex_lock(&s->data_mutex);
a77e6b14
NN
281 memcpy(p, buf, VHOST_USER_HDR_SIZE);
282
283 if (msg.size) {
284 p += VHOST_USER_HDR_SIZE;
4616e359
MAL
285 size = qemu_chr_fe_read_all(chr, p, msg.size);
286 if (size != msg.size) {
287 g_test_message("Wrong message size received %d != %d\n",
288 size, msg.size);
289 return;
290 }
a77e6b14
NN
291 }
292
293 switch (msg.request) {
294 case VHOST_USER_GET_FEATURES:
8a9b6b37
MT
295 /* send back features to qemu */
296 msg.flags |= VHOST_USER_REPLY_MASK;
12ebf690
MT
297 msg.size = sizeof(m.payload.u64);
298 msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
b1819747 299 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
ed0a8d92
MAL
300 if (s->queues > 1) {
301 msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
302 }
9294d76c
MAL
303 if (s->test_flags >= TEST_FLAGS_BAD) {
304 msg.payload.u64 = 0;
305 s->test_flags = TEST_FLAGS_END;
306 }
8a9b6b37
MT
307 p = (uint8_t *) &msg;
308 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
309 break;
310
311 case VHOST_USER_SET_FEATURES:
12ebf690 312 g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
8a9b6b37 313 !=, 0ULL);
9294d76c
MAL
314 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
315 qemu_chr_disconnect(chr);
316 s->test_flags = TEST_FLAGS_BAD;
317 }
8a9b6b37
MT
318 break;
319
320 case VHOST_USER_GET_PROTOCOL_FEATURES:
a77e6b14
NN
321 /* send back features to qemu */
322 msg.flags |= VHOST_USER_REPLY_MASK;
12ebf690
MT
323 msg.size = sizeof(m.payload.u64);
324 msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
ed0a8d92
MAL
325 if (s->queues > 1) {
326 msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
327 }
a77e6b14
NN
328 p = (uint8_t *) &msg;
329 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
330 break;
331
332 case VHOST_USER_GET_VRING_BASE:
333 /* send back vring base to qemu */
334 msg.flags |= VHOST_USER_REPLY_MASK;
12ebf690
MT
335 msg.size = sizeof(m.payload.state);
336 msg.payload.state.num = 0;
a77e6b14
NN
337 p = (uint8_t *) &msg;
338 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
d08e42a1 339
ed0a8d92 340 assert(msg.payload.state.index < s->queues * 2);
d08e42a1 341 s->rings &= ~(0x1ULL << msg.payload.state.index);
a77e6b14
NN
342 break;
343
344 case VHOST_USER_SET_MEM_TABLE:
345 /* received the mem table */
12ebf690 346 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
ae31fb54 347 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, G_N_ELEMENTS(s->fds));
a77e6b14
NN
348
349 /* signal the test that it can continue */
ae31fb54 350 g_cond_signal(&s->data_cond);
a77e6b14
NN
351 break;
352
353 case VHOST_USER_SET_VRING_KICK:
354 case VHOST_USER_SET_VRING_CALL:
355 /* consume the fd */
356 qemu_chr_fe_get_msgfds(chr, &fd, 1);
357 /*
358 * This is a non-blocking eventfd.
359 * The receive function forces it to be blocking,
360 * so revert it back to non-blocking.
361 */
362 qemu_set_nonblock(fd);
363 break;
b1819747
MAL
364
365 case VHOST_USER_SET_LOG_BASE:
366 if (s->log_fd != -1) {
367 close(s->log_fd);
368 s->log_fd = -1;
369 }
370 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
371 msg.flags |= VHOST_USER_REPLY_MASK;
372 msg.size = 0;
373 p = (uint8_t *) &msg;
374 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
375
376 g_cond_signal(&s->data_cond);
377 break;
378
d08e42a1 379 case VHOST_USER_SET_VRING_BASE:
ed0a8d92 380 assert(msg.payload.state.index < s->queues * 2);
d08e42a1 381 s->rings |= 0x1ULL << msg.payload.state.index;
1d9edff7
MAL
382 break;
383
ed0a8d92
MAL
384 case VHOST_USER_GET_QUEUE_NUM:
385 msg.flags |= VHOST_USER_REPLY_MASK;
386 msg.size = sizeof(m.payload.u64);
387 msg.payload.u64 = s->queues;
388 p = (uint8_t *) &msg;
389 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
390 break;
391
a77e6b14
NN
392 default:
393 break;
394 }
ae31fb54
MAL
395
396 g_mutex_unlock(&s->data_mutex);
a77e6b14
NN
397}
398
1b7e1e3b 399static const char *init_hugepagefs(const char *path)
a77e6b14 400{
a77e6b14
NN
401 struct statfs fs;
402 int ret;
403
a77e6b14
NN
404 if (access(path, R_OK | W_OK | X_OK)) {
405 g_test_message("access on path (%s): %s\n", path, strerror(errno));
406 return NULL;
407 }
408
409 do {
410 ret = statfs(path, &fs);
411 } while (ret != 0 && errno == EINTR);
412
413 if (ret != 0) {
414 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
415 return NULL;
416 }
417
418 if (fs.f_type != HUGETLBFS_MAGIC) {
419 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
420 return NULL;
421 }
422
423 return path;
424}
425
704b2168 426static TestServer *test_server_new(const gchar *name)
ae31fb54
MAL
427{
428 TestServer *server = g_new0(TestServer, 1);
ae31fb54
MAL
429
430 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
a899b1ea 431 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
ae31fb54 432 server->chr_name = g_strdup_printf("chr-%s", name);
ae31fb54
MAL
433
434 g_mutex_init(&server->data_mutex);
435 g_cond_init(&server->data_cond);
436
b1819747 437 server->log_fd = -1;
ed0a8d92 438 server->queues = 1;
b1819747 439
ae31fb54
MAL
440 return server;
441}
442
9294d76c
MAL
443static void chr_event(void *opaque, int event)
444{
445 TestServer *s = opaque;
446
447 if (s->test_flags == TEST_FLAGS_END &&
448 event == CHR_EVENT_CLOSED) {
449 s->test_flags = TEST_FLAGS_OK;
450 }
451}
452
4616e359
MAL
453static void test_server_create_chr(TestServer *server, const gchar *opt)
454{
455 gchar *chr_path;
ae31fb54 456
4616e359
MAL
457 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
458 server->chr = qemu_chr_new(server->chr_name, chr_path, NULL);
459 g_free(chr_path);
460
9294d76c
MAL
461 qemu_chr_add_handlers(server->chr, chr_can_read, chr_read,
462 chr_event, server);
4616e359
MAL
463}
464
465static void test_server_listen(TestServer *server)
466{
467 test_server_create_chr(server, ",server,nowait");
468}
469
470static inline void test_server_connect(TestServer *server)
471{
472 test_server_create_chr(server, ",reconnect=1");
473}
474
475#define GET_QEMU_CMD(s) \
476 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
477 (s)->socket_path, "", (s)->chr_name)
478
479#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
480 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
481 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
ae31fb54 482
9732baf6 483static gboolean _test_server_free(TestServer *server)
ae31fb54
MAL
484{
485 int i;
486
487 qemu_chr_delete(server->chr);
488
489 for (i = 0; i < server->fds_num; i++) {
490 close(server->fds[i]);
491 }
492
b1819747
MAL
493 if (server->log_fd != -1) {
494 close(server->log_fd);
495 }
496
ae31fb54
MAL
497 unlink(server->socket_path);
498 g_free(server->socket_path);
499
a899b1ea
MAL
500 unlink(server->mig_path);
501 g_free(server->mig_path);
502
b1819747 503 g_free(server->chr_name);
ae31fb54 504 g_free(server);
9732baf6
MAL
505
506 return FALSE;
507}
508
509static void test_server_free(TestServer *server)
510{
511 g_idle_add((GSourceFunc)_test_server_free, server);
ae31fb54
MAL
512}
513
b1819747
MAL
514static void wait_for_log_fd(TestServer *s)
515{
516 gint64 end_time;
517
518 g_mutex_lock(&s->data_mutex);
519 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
520 while (s->log_fd == -1) {
521 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
522 /* timeout has passed */
523 g_assert(s->log_fd != -1);
524 break;
525 }
526 }
527
528 g_mutex_unlock(&s->data_mutex);
529}
530
3a87d009 531static void write_guest_mem(TestServer *s, uint32_t seed)
b1819747
MAL
532{
533 uint32_t *guest_mem;
534 int i, j;
535 size_t size;
536
537 wait_for_fds(s);
538
539 /* iterate all regions */
540 for (i = 0; i < s->fds_num; i++) {
541
542 /* We'll write only the region statring at 0x0 */
543 if (s->memory.regions[i].guest_phys_addr != 0x0) {
544 continue;
545 }
546
547 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
548
549 size = s->memory.regions[i].memory_size +
550 s->memory.regions[i].mmap_offset;
551
552 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
553 MAP_SHARED, s->fds[i], 0);
554
555 g_assert(guest_mem != MAP_FAILED);
556 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
557
558 for (j = 0; j < 256; j++) {
559 guest_mem[j] = seed + j;
560 }
561
562 munmap(guest_mem, s->memory.regions[i].memory_size);
563 break;
564 }
565}
566
567static guint64 get_log_size(TestServer *s)
568{
569 guint64 log_size = 0;
570 int i;
571
572 for (i = 0; i < s->memory.nregions; ++i) {
573 VhostUserMemoryRegion *reg = &s->memory.regions[i];
574 guint64 last = range_get_last(reg->guest_phys_addr,
575 reg->memory_size);
576 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
577 }
578
579 return log_size;
580}
581
1d9edff7
MAL
582typedef struct TestMigrateSource {
583 GSource source;
584 TestServer *src;
585 TestServer *dest;
586} TestMigrateSource;
587
588static gboolean
589test_migrate_source_check(GSource *source)
590{
591 TestMigrateSource *t = (TestMigrateSource *)source;
d08e42a1 592 gboolean overlap = t->src->rings && t->dest->rings;
1d9edff7
MAL
593
594 g_assert(!overlap);
595
596 return FALSE;
597}
598
45ce5126
MAL
599#if !GLIB_CHECK_VERSION(2,36,0)
600/* this callback is unnecessary with glib >2.36, the default
601 * prepare for the source does the same */
602static gboolean
603test_migrate_source_prepare(GSource *source, gint *timeout)
604{
605 *timeout = -1;
606 return FALSE;
607}
608#endif
609
1d9edff7 610GSourceFuncs test_migrate_source_funcs = {
45ce5126
MAL
611#if !GLIB_CHECK_VERSION(2,36,0)
612 .prepare = test_migrate_source_prepare,
613#endif
614 .check = test_migrate_source_check,
1d9edff7
MAL
615};
616
b1819747
MAL
617static void test_migrate(void)
618{
619 TestServer *s = test_server_new("src");
620 TestServer *dest = test_server_new("dest");
a899b1ea 621 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
b1819747 622 QTestState *global = global_qtest, *from, *to;
1d9edff7 623 GSource *source;
b1819747
MAL
624 gchar *cmd;
625 QDict *rsp;
626 guint8 *log;
627 guint64 size;
628
4616e359
MAL
629 test_server_listen(s);
630 test_server_listen(dest);
631
632 cmd = GET_QEMU_CMDE(s, 2, "", "");
b1819747
MAL
633 from = qtest_start(cmd);
634 g_free(cmd);
635
cdafe929 636 init_virtio_dev(s);
b1819747
MAL
637 wait_for_fds(s);
638 size = get_log_size(s);
639 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
640
4616e359 641 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
b1819747
MAL
642 to = qtest_init(cmd);
643 g_free(cmd);
644
1d9edff7
MAL
645 source = g_source_new(&test_migrate_source_funcs,
646 sizeof(TestMigrateSource));
647 ((TestMigrateSource *)source)->src = s;
648 ((TestMigrateSource *)source)->dest = dest;
649 g_source_attach(source, NULL);
650
b1819747
MAL
651 /* slow down migration to have time to fiddle with log */
652 /* TODO: qtest could learn to break on some places */
653 rsp = qmp("{ 'execute': 'migrate_set_speed',"
654 "'arguments': { 'value': 10 } }");
655 g_assert(qdict_haskey(rsp, "return"));
656 QDECREF(rsp);
657
658 cmd = g_strdup_printf("{ 'execute': 'migrate',"
659 "'arguments': { 'uri': '%s' } }",
660 uri);
661 rsp = qmp(cmd);
662 g_free(cmd);
663 g_assert(qdict_haskey(rsp, "return"));
664 QDECREF(rsp);
665
666 wait_for_log_fd(s);
667
668 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
669 g_assert(log != MAP_FAILED);
670
671 /* modify first page */
672 write_guest_mem(s, 0x42);
673 log[0] = 1;
674 munmap(log, size);
675
676 /* speed things up */
677 rsp = qmp("{ 'execute': 'migrate_set_speed',"
678 "'arguments': { 'value': 0 } }");
679 g_assert(qdict_haskey(rsp, "return"));
680 QDECREF(rsp);
681
682 qmp_eventwait("STOP");
683
684 global_qtest = to;
685 qmp_eventwait("RESUME");
686
687 read_guest_mem(dest);
688
1d9edff7
MAL
689 g_source_destroy(source);
690 g_source_unref(source);
691
b1819747
MAL
692 qtest_quit(to);
693 test_server_free(dest);
694 qtest_quit(from);
695 test_server_free(s);
a899b1ea 696 g_free(uri);
b1819747
MAL
697
698 global_qtest = global;
699}
700
4616e359
MAL
701static void wait_for_rings_started(TestServer *s, size_t count)
702{
703 gint64 end_time;
704
705 g_mutex_lock(&s->data_mutex);
706 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
707 while (ctpop64(s->rings) != count) {
708 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
709 /* timeout has passed */
710 g_assert_cmpint(ctpop64(s->rings), ==, count);
711 break;
712 }
713 }
714
715 g_mutex_unlock(&s->data_mutex);
716}
717
ed0a8d92 718#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
4616e359
MAL
719static gboolean
720reconnect_cb(gpointer user_data)
721{
722 TestServer *s = user_data;
723
724 qemu_chr_disconnect(s->chr);
725
726 return FALSE;
727}
728
729static gpointer
730connect_thread(gpointer data)
731{
732 TestServer *s = data;
733
734 /* wait for qemu to start before first try, to avoid extra warnings */
735 g_usleep(G_USEC_PER_SEC);
736 test_server_connect(s);
737
738 return NULL;
739}
740
741static void test_reconnect_subprocess(void)
742{
743 TestServer *s = test_server_new("reconnect");
744 char *cmd;
745
746 g_thread_new("connect", connect_thread, s);
747 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
748 qtest_start(cmd);
749 g_free(cmd);
750
cdafe929 751 init_virtio_dev(s);
4616e359
MAL
752 wait_for_fds(s);
753 wait_for_rings_started(s, 2);
754
755 /* reconnect */
756 s->fds_num = 0;
757 s->rings = 0;
758 g_idle_add(reconnect_cb, s);
759 wait_for_fds(s);
760 wait_for_rings_started(s, 2);
761
762 qtest_end();
763 test_server_free(s);
764 return;
765}
766
767static void test_reconnect(void)
768{
769 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
770 qtest_get_arch());
771 g_test_trap_subprocess(path, 0, 0);
772 g_test_trap_assert_passed();
69179fe2 773 g_free(path);
4616e359 774}
5d443f5a
MAL
775
776static void test_connect_fail_subprocess(void)
777{
778 TestServer *s = test_server_new("connect-fail");
779 char *cmd;
780
781 s->test_fail = true;
782 g_thread_new("connect", connect_thread, s);
783 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
784 qtest_start(cmd);
785 g_free(cmd);
786
787 init_virtio_dev(s);
788 wait_for_fds(s);
789 wait_for_rings_started(s, 2);
790
791 qtest_end();
792 test_server_free(s);
793}
794
795static void test_connect_fail(void)
796{
797 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
798 qtest_get_arch());
799 g_test_trap_subprocess(path, 0, 0);
800 g_test_trap_assert_passed();
801 g_free(path);
802}
803
9294d76c
MAL
804static void test_flags_mismatch_subprocess(void)
805{
806 TestServer *s = test_server_new("flags-mismatch");
807 char *cmd;
808
809 s->test_flags = TEST_FLAGS_DISCONNECT;
810 g_thread_new("connect", connect_thread, s);
811 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
812 qtest_start(cmd);
813 g_free(cmd);
814
815 init_virtio_dev(s);
816 wait_for_fds(s);
817 wait_for_rings_started(s, 2);
818
819 qtest_end();
820 test_server_free(s);
821}
822
823static void test_flags_mismatch(void)
824{
825 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
826 qtest_get_arch());
827 g_test_trap_subprocess(path, 0, 0);
828 g_test_trap_assert_passed();
829 g_free(path);
830}
831
4616e359
MAL
832#endif
833
ed0a8d92
MAL
834static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
835{
836 QVirtioPCIDevice *dev;
837
838 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
839 g_assert(dev != NULL);
840 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
841
842 qvirtio_pci_device_enable(dev);
843 qvirtio_reset(&qvirtio_pci, &dev->vdev);
844 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
845 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
846
847 return dev;
848}
849
850static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
851{
852 uint32_t features;
853
854 features = qvirtio_get_features(bus, dev);
855 features = features & ~(QVIRTIO_F_BAD_FEATURE |
856 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
857 (1u << VIRTIO_RING_F_EVENT_IDX));
858 qvirtio_set_features(bus, dev, features);
859
860 qvirtio_set_driver_ok(bus, dev);
861}
862
863#define PCI_SLOT 0x04
864
865static void test_multiqueue(void)
866{
867 const int queues = 2;
868 TestServer *s = test_server_new("mq");
869 QVirtioPCIDevice *dev;
870 QPCIBus *bus;
871 QVirtQueuePCI *vq[queues * 2];
872 QGuestAllocator *alloc;
873 char *cmd;
874 int i;
875
876 s->queues = queues;
877 test_server_listen(s);
878
879 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
880 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
881 512, 512, root, s->chr_name,
882 s->socket_path, "", s->chr_name,
883 queues, queues * 2 + 2);
884 qtest_start(cmd);
885 g_free(cmd);
886
2ecd7e2f 887 bus = qpci_init_pc(NULL);
ed0a8d92
MAL
888 dev = virtio_net_pci_init(bus, PCI_SLOT);
889
890 alloc = pc_alloc_init();
891 for (i = 0; i < queues * 2; i++) {
892 vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
893 alloc, i);
894 }
895
896 driver_init(&qvirtio_pci, &dev->vdev);
897 wait_for_rings_started(s, queues * 2);
898
899 /* End test */
900 for (i = 0; i < queues * 2; i++) {
901 qvirtqueue_cleanup(&qvirtio_pci, &vq[i]->vq, alloc);
902 }
903 pc_alloc_uninit(alloc);
904 qvirtio_pci_device_disable(dev);
905 g_free(dev->pdev);
906 g_free(dev);
907 qpci_free_pc(bus);
908 qtest_end();
909
910 test_server_free(s);
911}
912
a77e6b14
NN
913int main(int argc, char **argv)
914{
915 QTestState *s = NULL;
ae31fb54 916 TestServer *server = NULL;
1b7e1e3b 917 const char *hugefs;
ae31fb54 918 char *qemu_cmd = NULL;
a77e6b14 919 int ret;
1b7e1e3b 920 char template[] = "/tmp/vhost-test-XXXXXX";
9732baf6
MAL
921 GMainLoop *loop;
922 GThread *thread;
a77e6b14
NN
923
924 g_test_init(&argc, &argv, NULL);
925
926 module_call_init(MODULE_INIT_QOM);
ae31fb54 927 qemu_add_opts(&qemu_chardev_opts);
a77e6b14 928
1b7e1e3b
MT
929 tmpfs = mkdtemp(template);
930 if (!tmpfs) {
ae31fb54 931 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
1b7e1e3b
MT
932 }
933 g_assert(tmpfs);
934
935 hugefs = getenv("QTEST_HUGETLBFS_PATH");
936 if (hugefs) {
937 root = init_hugepagefs(hugefs);
938 g_assert(root);
939 } else {
940 root = tmpfs;
a77e6b14
NN
941 }
942
704b2168 943 server = test_server_new("test");
4616e359 944 test_server_listen(server);
a77e6b14 945
9732baf6 946 loop = g_main_loop_new(NULL, FALSE);
a77e6b14 947 /* run the main loop thread so the chardev may operate */
9732baf6 948 thread = g_thread_new(NULL, thread_function, loop);
a77e6b14 949
704b2168 950 qemu_cmd = GET_QEMU_CMD(server);
ae31fb54 951
a77e6b14
NN
952 s = qtest_start(qemu_cmd);
953 g_free(qemu_cmd);
cdafe929 954 init_virtio_dev(server);
a77e6b14 955
ae31fb54 956 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
b1819747 957 qtest_add_func("/vhost-user/migrate", test_migrate);
ed0a8d92 958 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
4616e359
MAL
959#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
960 qtest_add_func("/vhost-user/reconnect/subprocess",
961 test_reconnect_subprocess);
962 qtest_add_func("/vhost-user/reconnect", test_reconnect);
5d443f5a
MAL
963 qtest_add_func("/vhost-user/connect-fail/subprocess",
964 test_connect_fail_subprocess);
965 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
9294d76c
MAL
966 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
967 test_flags_mismatch_subprocess);
968 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
4616e359 969#endif
a77e6b14
NN
970
971 ret = g_test_run();
972
973 if (s) {
974 qtest_quit(s);
975 }
976
977 /* cleanup */
ae31fb54 978 test_server_free(server);
a77e6b14 979
9732baf6
MAL
980 /* finish the helper thread and dispatch pending sources */
981 g_main_loop_quit(loop);
982 g_thread_join(thread);
983 while (g_main_context_pending(NULL)) {
984 g_main_context_iteration (NULL, TRUE);
985 }
986 g_main_loop_unref(loop);
987
1b7e1e3b
MT
988 ret = rmdir(tmpfs);
989 if (ret != 0) {
990 g_test_message("unable to rmdir: path (%s): %s\n",
991 tmpfs, strerror(errno));
992 }
993 g_assert_cmpint(ret, ==, 0);
994
a77e6b14
NN
995 return ret;
996}