]> git.proxmox.com Git - mirror_qemu.git/blame - tests/vhost-user-test.c
test-qga: Avoid qobject_from_jsonv("%"PRId64)
[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 13#include "libqtest.h"
5345fdb4 14#include "qapi/error.h"
a77e6b14 15#include "qemu/option.h"
b1819747 16#include "qemu/range.h"
a9c94277 17#include "qemu/sockets.h"
a77e6b14
NN
18#include "sysemu/char.h"
19#include "sysemu/sysemu.h"
cdafe929
EH
20#include "libqos/libqos.h"
21#include "libqos/pci-pc.h"
22#include "libqos/virtio-pci.h"
32a6ebec 23#include "qapi/error.h"
a77e6b14 24
ed0a8d92
MAL
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 143 gchar *chr_name;
32a6ebec 144 CharBackend chr;
ae31fb54
MAL
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);
6b9cdf4c
LV
173 qvirtio_reset(&dev->vdev);
174 qvirtio_set_acknowledge(&dev->vdev);
175 qvirtio_set_driver(&dev->vdev);
cdafe929 176
6b9cdf4c 177 features = qvirtio_get_features(&dev->vdev);
cdafe929 178 features = features & VIRTIO_NET_F_MAC;
6b9cdf4c 179 qvirtio_set_features(&dev->vdev, features);
cdafe929 180
6b9cdf4c 181 qvirtio_set_driver_ok(&dev->vdev);
cdafe929
EH
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 263 TestServer *s = opaque;
32a6ebec 264 CharBackend *chr = &s->chr;
a77e6b14
NN
265 VhostUserMsg msg;
266 uint8_t *p = (uint8_t *) &msg;
267 int fd;
268
5d443f5a 269 if (s->test_fail) {
5345fdb4 270 qemu_chr_fe_disconnect(chr);
5d443f5a
MAL
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;
5345fdb4 285 size = qemu_chr_fe_read_all(chr, p, msg.size);
4616e359
MAL
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 307 p = (uint8_t *) &msg;
5345fdb4 308 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
8a9b6b37
MT
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 314 if (s->test_flags == TEST_FLAGS_DISCONNECT) {
5345fdb4 315 qemu_chr_fe_disconnect(chr);
9294d76c
MAL
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 328 p = (uint8_t *) &msg;
5345fdb4 329 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
a77e6b14
NN
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 337 p = (uint8_t *) &msg;
5345fdb4 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));
5345fdb4 347 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
32a6ebec 348 G_N_ELEMENTS(s->fds));
a77e6b14
NN
349
350 /* signal the test that it can continue */
ae31fb54 351 g_cond_signal(&s->data_cond);
a77e6b14
NN
352 break;
353
354 case VHOST_USER_SET_VRING_KICK:
355 case VHOST_USER_SET_VRING_CALL:
356 /* consume the fd */
5345fdb4 357 qemu_chr_fe_get_msgfds(chr, &fd, 1);
a77e6b14
NN
358 /*
359 * This is a non-blocking eventfd.
360 * The receive function forces it to be blocking,
361 * so revert it back to non-blocking.
362 */
363 qemu_set_nonblock(fd);
364 break;
b1819747
MAL
365
366 case VHOST_USER_SET_LOG_BASE:
367 if (s->log_fd != -1) {
368 close(s->log_fd);
369 s->log_fd = -1;
370 }
5345fdb4 371 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
b1819747
MAL
372 msg.flags |= VHOST_USER_REPLY_MASK;
373 msg.size = 0;
374 p = (uint8_t *) &msg;
5345fdb4 375 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
b1819747
MAL
376
377 g_cond_signal(&s->data_cond);
378 break;
379
d08e42a1 380 case VHOST_USER_SET_VRING_BASE:
ed0a8d92 381 assert(msg.payload.state.index < s->queues * 2);
d08e42a1 382 s->rings |= 0x1ULL << msg.payload.state.index;
1d9edff7
MAL
383 break;
384
ed0a8d92
MAL
385 case VHOST_USER_GET_QUEUE_NUM:
386 msg.flags |= VHOST_USER_REPLY_MASK;
387 msg.size = sizeof(m.payload.u64);
388 msg.payload.u64 = s->queues;
389 p = (uint8_t *) &msg;
5345fdb4 390 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
ed0a8d92
MAL
391 break;
392
a77e6b14
NN
393 default:
394 break;
395 }
ae31fb54
MAL
396
397 g_mutex_unlock(&s->data_mutex);
a77e6b14
NN
398}
399
1b7e1e3b 400static const char *init_hugepagefs(const char *path)
a77e6b14 401{
a77e6b14
NN
402 struct statfs fs;
403 int ret;
404
a77e6b14
NN
405 if (access(path, R_OK | W_OK | X_OK)) {
406 g_test_message("access on path (%s): %s\n", path, strerror(errno));
407 return NULL;
408 }
409
410 do {
411 ret = statfs(path, &fs);
412 } while (ret != 0 && errno == EINTR);
413
414 if (ret != 0) {
415 g_test_message("statfs on path (%s): %s\n", path, strerror(errno));
416 return NULL;
417 }
418
419 if (fs.f_type != HUGETLBFS_MAGIC) {
420 g_test_message("Warning: path not on HugeTLBFS: %s\n", path);
421 return NULL;
422 }
423
424 return path;
425}
426
704b2168 427static TestServer *test_server_new(const gchar *name)
ae31fb54
MAL
428{
429 TestServer *server = g_new0(TestServer, 1);
ae31fb54
MAL
430
431 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
a899b1ea 432 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
ae31fb54 433 server->chr_name = g_strdup_printf("chr-%s", name);
ae31fb54
MAL
434
435 g_mutex_init(&server->data_mutex);
436 g_cond_init(&server->data_cond);
437
b1819747 438 server->log_fd = -1;
ed0a8d92 439 server->queues = 1;
b1819747 440
ae31fb54
MAL
441 return server;
442}
443
9294d76c
MAL
444static void chr_event(void *opaque, int event)
445{
446 TestServer *s = opaque;
447
448 if (s->test_flags == TEST_FLAGS_END &&
449 event == CHR_EVENT_CLOSED) {
450 s->test_flags = TEST_FLAGS_OK;
451 }
452}
453
4616e359
MAL
454static void test_server_create_chr(TestServer *server, const gchar *opt)
455{
456 gchar *chr_path;
32a6ebec 457 CharDriverState *chr;
5345fdb4 458
4616e359 459 chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
32a6ebec 460 chr = qemu_chr_new(server->chr_name, chr_path);
4616e359
MAL
461 g_free(chr_path);
462
5345fdb4
MAL
463 qemu_chr_fe_init(&server->chr, chr, &error_abort);
464 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
39ab61c6 465 chr_event, server, NULL, true);
4616e359
MAL
466}
467
468static void test_server_listen(TestServer *server)
469{
470 test_server_create_chr(server, ",server,nowait");
471}
472
473static inline void test_server_connect(TestServer *server)
474{
475 test_server_create_chr(server, ",reconnect=1");
476}
477
478#define GET_QEMU_CMD(s) \
479 g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \
480 (s)->socket_path, "", (s)->chr_name)
481
482#define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \
483 g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \
484 (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__)
ae31fb54 485
9732baf6 486static gboolean _test_server_free(TestServer *server)
ae31fb54
MAL
487{
488 int i;
5345fdb4 489 CharDriverState *chr = qemu_chr_fe_get_driver(&server->chr);
ae31fb54 490
c39860e6 491 qemu_chr_fe_deinit(&server->chr);
5345fdb4 492 qemu_chr_delete(chr);
ae31fb54
MAL
493
494 for (i = 0; i < server->fds_num; i++) {
495 close(server->fds[i]);
496 }
497
b1819747
MAL
498 if (server->log_fd != -1) {
499 close(server->log_fd);
500 }
501
ae31fb54
MAL
502 unlink(server->socket_path);
503 g_free(server->socket_path);
504
a899b1ea
MAL
505 unlink(server->mig_path);
506 g_free(server->mig_path);
507
b1819747 508 g_free(server->chr_name);
ae31fb54 509 g_free(server);
9732baf6
MAL
510
511 return FALSE;
512}
513
514static void test_server_free(TestServer *server)
515{
516 g_idle_add((GSourceFunc)_test_server_free, server);
ae31fb54
MAL
517}
518
b1819747
MAL
519static void wait_for_log_fd(TestServer *s)
520{
521 gint64 end_time;
522
523 g_mutex_lock(&s->data_mutex);
524 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
525 while (s->log_fd == -1) {
526 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
527 /* timeout has passed */
528 g_assert(s->log_fd != -1);
529 break;
530 }
531 }
532
533 g_mutex_unlock(&s->data_mutex);
534}
535
3a87d009 536static void write_guest_mem(TestServer *s, uint32_t seed)
b1819747
MAL
537{
538 uint32_t *guest_mem;
539 int i, j;
540 size_t size;
541
542 wait_for_fds(s);
543
544 /* iterate all regions */
545 for (i = 0; i < s->fds_num; i++) {
546
547 /* We'll write only the region statring at 0x0 */
548 if (s->memory.regions[i].guest_phys_addr != 0x0) {
549 continue;
550 }
551
552 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
553
554 size = s->memory.regions[i].memory_size +
555 s->memory.regions[i].mmap_offset;
556
557 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
558 MAP_SHARED, s->fds[i], 0);
559
560 g_assert(guest_mem != MAP_FAILED);
561 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
562
563 for (j = 0; j < 256; j++) {
564 guest_mem[j] = seed + j;
565 }
566
567 munmap(guest_mem, s->memory.regions[i].memory_size);
568 break;
569 }
570}
571
572static guint64 get_log_size(TestServer *s)
573{
574 guint64 log_size = 0;
575 int i;
576
577 for (i = 0; i < s->memory.nregions; ++i) {
578 VhostUserMemoryRegion *reg = &s->memory.regions[i];
579 guint64 last = range_get_last(reg->guest_phys_addr,
580 reg->memory_size);
581 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
582 }
583
584 return log_size;
585}
586
1d9edff7
MAL
587typedef struct TestMigrateSource {
588 GSource source;
589 TestServer *src;
590 TestServer *dest;
591} TestMigrateSource;
592
593static gboolean
594test_migrate_source_check(GSource *source)
595{
596 TestMigrateSource *t = (TestMigrateSource *)source;
d08e42a1 597 gboolean overlap = t->src->rings && t->dest->rings;
1d9edff7
MAL
598
599 g_assert(!overlap);
600
601 return FALSE;
602}
603
45ce5126
MAL
604#if !GLIB_CHECK_VERSION(2,36,0)
605/* this callback is unnecessary with glib >2.36, the default
606 * prepare for the source does the same */
607static gboolean
608test_migrate_source_prepare(GSource *source, gint *timeout)
609{
610 *timeout = -1;
611 return FALSE;
612}
613#endif
614
1d9edff7 615GSourceFuncs test_migrate_source_funcs = {
45ce5126
MAL
616#if !GLIB_CHECK_VERSION(2,36,0)
617 .prepare = test_migrate_source_prepare,
618#endif
619 .check = test_migrate_source_check,
1d9edff7
MAL
620};
621
b1819747
MAL
622static void test_migrate(void)
623{
624 TestServer *s = test_server_new("src");
625 TestServer *dest = test_server_new("dest");
a899b1ea 626 char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
b1819747 627 QTestState *global = global_qtest, *from, *to;
1d9edff7 628 GSource *source;
b1819747
MAL
629 gchar *cmd;
630 QDict *rsp;
631 guint8 *log;
632 guint64 size;
633
4616e359
MAL
634 test_server_listen(s);
635 test_server_listen(dest);
636
637 cmd = GET_QEMU_CMDE(s, 2, "", "");
b1819747
MAL
638 from = qtest_start(cmd);
639 g_free(cmd);
640
cdafe929 641 init_virtio_dev(s);
b1819747
MAL
642 wait_for_fds(s);
643 size = get_log_size(s);
644 g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
645
4616e359 646 cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri);
b1819747
MAL
647 to = qtest_init(cmd);
648 g_free(cmd);
649
1d9edff7
MAL
650 source = g_source_new(&test_migrate_source_funcs,
651 sizeof(TestMigrateSource));
652 ((TestMigrateSource *)source)->src = s;
653 ((TestMigrateSource *)source)->dest = dest;
654 g_source_attach(source, NULL);
655
b1819747
MAL
656 /* slow down migration to have time to fiddle with log */
657 /* TODO: qtest could learn to break on some places */
658 rsp = qmp("{ 'execute': 'migrate_set_speed',"
659 "'arguments': { 'value': 10 } }");
660 g_assert(qdict_haskey(rsp, "return"));
661 QDECREF(rsp);
662
663 cmd = g_strdup_printf("{ 'execute': 'migrate',"
664 "'arguments': { 'uri': '%s' } }",
665 uri);
666 rsp = qmp(cmd);
667 g_free(cmd);
668 g_assert(qdict_haskey(rsp, "return"));
669 QDECREF(rsp);
670
671 wait_for_log_fd(s);
672
673 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
674 g_assert(log != MAP_FAILED);
675
676 /* modify first page */
677 write_guest_mem(s, 0x42);
678 log[0] = 1;
679 munmap(log, size);
680
681 /* speed things up */
682 rsp = qmp("{ 'execute': 'migrate_set_speed',"
683 "'arguments': { 'value': 0 } }");
684 g_assert(qdict_haskey(rsp, "return"));
685 QDECREF(rsp);
686
687 qmp_eventwait("STOP");
688
689 global_qtest = to;
690 qmp_eventwait("RESUME");
691
692 read_guest_mem(dest);
693
1d9edff7
MAL
694 g_source_destroy(source);
695 g_source_unref(source);
696
b1819747
MAL
697 qtest_quit(to);
698 test_server_free(dest);
699 qtest_quit(from);
700 test_server_free(s);
a899b1ea 701 g_free(uri);
b1819747
MAL
702
703 global_qtest = global;
704}
705
4616e359
MAL
706static void wait_for_rings_started(TestServer *s, size_t count)
707{
708 gint64 end_time;
709
710 g_mutex_lock(&s->data_mutex);
711 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
712 while (ctpop64(s->rings) != count) {
713 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
714 /* timeout has passed */
715 g_assert_cmpint(ctpop64(s->rings), ==, count);
716 break;
717 }
718 }
719
720 g_mutex_unlock(&s->data_mutex);
721}
722
ed0a8d92 723#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
4616e359
MAL
724static gboolean
725reconnect_cb(gpointer user_data)
726{
727 TestServer *s = user_data;
728
5345fdb4 729 qemu_chr_fe_disconnect(&s->chr);
4616e359
MAL
730
731 return FALSE;
732}
733
734static gpointer
735connect_thread(gpointer data)
736{
737 TestServer *s = data;
738
739 /* wait for qemu to start before first try, to avoid extra warnings */
740 g_usleep(G_USEC_PER_SEC);
741 test_server_connect(s);
742
743 return NULL;
744}
745
746static void test_reconnect_subprocess(void)
747{
748 TestServer *s = test_server_new("reconnect");
749 char *cmd;
750
751 g_thread_new("connect", connect_thread, s);
752 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
753 qtest_start(cmd);
754 g_free(cmd);
755
cdafe929 756 init_virtio_dev(s);
4616e359
MAL
757 wait_for_fds(s);
758 wait_for_rings_started(s, 2);
759
760 /* reconnect */
761 s->fds_num = 0;
762 s->rings = 0;
763 g_idle_add(reconnect_cb, s);
764 wait_for_fds(s);
765 wait_for_rings_started(s, 2);
766
767 qtest_end();
768 test_server_free(s);
769 return;
770}
771
772static void test_reconnect(void)
773{
774 gchar *path = g_strdup_printf("/%s/vhost-user/reconnect/subprocess",
775 qtest_get_arch());
776 g_test_trap_subprocess(path, 0, 0);
777 g_test_trap_assert_passed();
69179fe2 778 g_free(path);
4616e359 779}
5d443f5a
MAL
780
781static void test_connect_fail_subprocess(void)
782{
783 TestServer *s = test_server_new("connect-fail");
784 char *cmd;
785
786 s->test_fail = true;
787 g_thread_new("connect", connect_thread, s);
788 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
789 qtest_start(cmd);
790 g_free(cmd);
791
792 init_virtio_dev(s);
793 wait_for_fds(s);
794 wait_for_rings_started(s, 2);
795
796 qtest_end();
797 test_server_free(s);
798}
799
800static void test_connect_fail(void)
801{
802 gchar *path = g_strdup_printf("/%s/vhost-user/connect-fail/subprocess",
803 qtest_get_arch());
804 g_test_trap_subprocess(path, 0, 0);
805 g_test_trap_assert_passed();
806 g_free(path);
807}
808
9294d76c
MAL
809static void test_flags_mismatch_subprocess(void)
810{
811 TestServer *s = test_server_new("flags-mismatch");
812 char *cmd;
813
814 s->test_flags = TEST_FLAGS_DISCONNECT;
815 g_thread_new("connect", connect_thread, s);
816 cmd = GET_QEMU_CMDE(s, 2, ",server", "");
817 qtest_start(cmd);
818 g_free(cmd);
819
820 init_virtio_dev(s);
821 wait_for_fds(s);
822 wait_for_rings_started(s, 2);
823
824 qtest_end();
825 test_server_free(s);
826}
827
828static void test_flags_mismatch(void)
829{
830 gchar *path = g_strdup_printf("/%s/vhost-user/flags-mismatch/subprocess",
831 qtest_get_arch());
832 g_test_trap_subprocess(path, 0, 0);
833 g_test_trap_assert_passed();
834 g_free(path);
835}
836
4616e359
MAL
837#endif
838
ed0a8d92
MAL
839static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
840{
841 QVirtioPCIDevice *dev;
842
843 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
844 g_assert(dev != NULL);
845 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
846
847 qvirtio_pci_device_enable(dev);
6b9cdf4c
LV
848 qvirtio_reset(&dev->vdev);
849 qvirtio_set_acknowledge(&dev->vdev);
850 qvirtio_set_driver(&dev->vdev);
ed0a8d92
MAL
851
852 return dev;
853}
854
6b9cdf4c 855static void driver_init(QVirtioDevice *dev)
ed0a8d92
MAL
856{
857 uint32_t features;
858
6b9cdf4c 859 features = qvirtio_get_features(dev);
ed0a8d92
MAL
860 features = features & ~(QVIRTIO_F_BAD_FEATURE |
861 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
862 (1u << VIRTIO_RING_F_EVENT_IDX));
6b9cdf4c 863 qvirtio_set_features(dev, features);
ed0a8d92 864
6b9cdf4c 865 qvirtio_set_driver_ok(dev);
ed0a8d92
MAL
866}
867
868#define PCI_SLOT 0x04
869
870static void test_multiqueue(void)
871{
872 const int queues = 2;
873 TestServer *s = test_server_new("mq");
874 QVirtioPCIDevice *dev;
875 QPCIBus *bus;
876 QVirtQueuePCI *vq[queues * 2];
877 QGuestAllocator *alloc;
878 char *cmd;
879 int i;
880
881 s->queues = queues;
882 test_server_listen(s);
883
884 cmd = g_strdup_printf(QEMU_CMD_MEM QEMU_CMD_CHR QEMU_CMD_NETDEV ",queues=%d "
885 "-device virtio-net-pci,netdev=net0,mq=on,vectors=%d",
886 512, 512, root, s->chr_name,
887 s->socket_path, "", s->chr_name,
888 queues, queues * 2 + 2);
889 qtest_start(cmd);
890 g_free(cmd);
891
2ecd7e2f 892 bus = qpci_init_pc(NULL);
ed0a8d92
MAL
893 dev = virtio_net_pci_init(bus, PCI_SLOT);
894
895 alloc = pc_alloc_init();
896 for (i = 0; i < queues * 2; i++) {
6b9cdf4c 897 vq[i] = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, alloc, i);
ed0a8d92
MAL
898 }
899
6b9cdf4c 900 driver_init(&dev->vdev);
ed0a8d92
MAL
901 wait_for_rings_started(s, queues * 2);
902
903 /* End test */
904 for (i = 0; i < queues * 2; i++) {
6b9cdf4c 905 qvirtqueue_cleanup(dev->vdev.bus, &vq[i]->vq, alloc);
ed0a8d92
MAL
906 }
907 pc_alloc_uninit(alloc);
908 qvirtio_pci_device_disable(dev);
909 g_free(dev->pdev);
910 g_free(dev);
911 qpci_free_pc(bus);
912 qtest_end();
913
914 test_server_free(s);
915}
916
a77e6b14
NN
917int main(int argc, char **argv)
918{
919 QTestState *s = NULL;
ae31fb54 920 TestServer *server = NULL;
1b7e1e3b 921 const char *hugefs;
ae31fb54 922 char *qemu_cmd = NULL;
a77e6b14 923 int ret;
1b7e1e3b 924 char template[] = "/tmp/vhost-test-XXXXXX";
9732baf6
MAL
925 GMainLoop *loop;
926 GThread *thread;
a77e6b14
NN
927
928 g_test_init(&argc, &argv, NULL);
929
930 module_call_init(MODULE_INIT_QOM);
ae31fb54 931 qemu_add_opts(&qemu_chardev_opts);
a77e6b14 932
1b7e1e3b
MT
933 tmpfs = mkdtemp(template);
934 if (!tmpfs) {
ae31fb54 935 g_test_message("mkdtemp on path (%s): %s\n", template, strerror(errno));
1b7e1e3b
MT
936 }
937 g_assert(tmpfs);
938
939 hugefs = getenv("QTEST_HUGETLBFS_PATH");
940 if (hugefs) {
941 root = init_hugepagefs(hugefs);
942 g_assert(root);
943 } else {
944 root = tmpfs;
a77e6b14
NN
945 }
946
704b2168 947 server = test_server_new("test");
4616e359 948 test_server_listen(server);
a77e6b14 949
9732baf6 950 loop = g_main_loop_new(NULL, FALSE);
a77e6b14 951 /* run the main loop thread so the chardev may operate */
9732baf6 952 thread = g_thread_new(NULL, thread_function, loop);
a77e6b14 953
704b2168 954 qemu_cmd = GET_QEMU_CMD(server);
ae31fb54 955
a77e6b14
NN
956 s = qtest_start(qemu_cmd);
957 g_free(qemu_cmd);
cdafe929 958 init_virtio_dev(server);
a77e6b14 959
ae31fb54 960 qtest_add_data_func("/vhost-user/read-guest-mem", server, read_guest_mem);
b1819747 961 qtest_add_func("/vhost-user/migrate", test_migrate);
ed0a8d92 962 qtest_add_func("/vhost-user/multiqueue", test_multiqueue);
4616e359
MAL
963#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
964 qtest_add_func("/vhost-user/reconnect/subprocess",
965 test_reconnect_subprocess);
966 qtest_add_func("/vhost-user/reconnect", test_reconnect);
5d443f5a
MAL
967 qtest_add_func("/vhost-user/connect-fail/subprocess",
968 test_connect_fail_subprocess);
969 qtest_add_func("/vhost-user/connect-fail", test_connect_fail);
9294d76c
MAL
970 qtest_add_func("/vhost-user/flags-mismatch/subprocess",
971 test_flags_mismatch_subprocess);
972 qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch);
4616e359 973#endif
a77e6b14
NN
974
975 ret = g_test_run();
976
977 if (s) {
978 qtest_quit(s);
979 }
980
981 /* cleanup */
ae31fb54 982 test_server_free(server);
a77e6b14 983
9732baf6
MAL
984 /* finish the helper thread and dispatch pending sources */
985 g_main_loop_quit(loop);
986 g_thread_join(thread);
987 while (g_main_context_pending(NULL)) {
988 g_main_context_iteration (NULL, TRUE);
989 }
990 g_main_loop_unref(loop);
991
1b7e1e3b
MT
992 ret = rmdir(tmpfs);
993 if (ret != 0) {
994 g_test_message("unable to rmdir: path (%s): %s\n",
995 tmpfs, strerror(errno));
996 }
997 g_assert_cmpint(ret, ==, 0);
998
a77e6b14
NN
999 return ret;
1000}