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