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