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