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