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