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