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