]>
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" |
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" |
a77e6b14 NN |
19 | #include "sysemu/char.h" |
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 | |
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 | ||
704b2168 | 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 | |
cdafe929 | 49 | #define QEMU_CMD QEMU_CMD_MEM QEMU_CMD_CHR \ |
b5c6eaf1 | 50 | QEMU_CMD_NETDEV QEMU_CMD_NET |
a77e6b14 NN |
51 | |
52 | #define HUGETLBFS_MAGIC 0x958458f6 | |
53 | ||
54 | /*********** FROM hw/virtio/vhost-user.c *************************************/ | |
55 | ||
56 | #define VHOST_MEMORY_MAX_NREGIONS 8 | |
57 | ||
8a9b6b37 | 58 | #define VHOST_USER_F_PROTOCOL_FEATURES 30 |
ed0a8d92 | 59 | #define VHOST_USER_PROTOCOL_F_MQ 0 |
b1819747 MAL |
60 | #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 |
61 | ||
62 | #define VHOST_LOG_PAGE 0x1000 | |
8a9b6b37 | 63 | |
a77e6b14 NN |
64 | typedef enum VhostUserRequest { |
65 | VHOST_USER_NONE = 0, | |
66 | VHOST_USER_GET_FEATURES = 1, | |
67 | VHOST_USER_SET_FEATURES = 2, | |
68 | VHOST_USER_SET_OWNER = 3, | |
60915dc4 | 69 | VHOST_USER_RESET_OWNER = 4, |
a77e6b14 NN |
70 | VHOST_USER_SET_MEM_TABLE = 5, |
71 | VHOST_USER_SET_LOG_BASE = 6, | |
72 | VHOST_USER_SET_LOG_FD = 7, | |
73 | VHOST_USER_SET_VRING_NUM = 8, | |
74 | VHOST_USER_SET_VRING_ADDR = 9, | |
75 | VHOST_USER_SET_VRING_BASE = 10, | |
76 | VHOST_USER_GET_VRING_BASE = 11, | |
77 | VHOST_USER_SET_VRING_KICK = 12, | |
78 | VHOST_USER_SET_VRING_CALL = 13, | |
79 | VHOST_USER_SET_VRING_ERR = 14, | |
8a9b6b37 MT |
80 | VHOST_USER_GET_PROTOCOL_FEATURES = 15, |
81 | VHOST_USER_SET_PROTOCOL_FEATURES = 16, | |
ed0a8d92 | 82 | VHOST_USER_GET_QUEUE_NUM = 17, |
87656d50 | 83 | VHOST_USER_SET_VRING_ENABLE = 18, |
a77e6b14 NN |
84 | VHOST_USER_MAX |
85 | } VhostUserRequest; | |
86 | ||
87 | typedef struct VhostUserMemoryRegion { | |
88 | uint64_t guest_phys_addr; | |
89 | uint64_t memory_size; | |
90 | uint64_t userspace_addr; | |
d6970e3b | 91 | uint64_t mmap_offset; |
a77e6b14 NN |
92 | } VhostUserMemoryRegion; |
93 | ||
94 | typedef struct VhostUserMemory { | |
95 | uint32_t nregions; | |
96 | uint32_t padding; | |
97 | VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; | |
98 | } VhostUserMemory; | |
99 | ||
2b8819c6 VK |
100 | typedef struct VhostUserLog { |
101 | uint64_t mmap_size; | |
102 | uint64_t mmap_offset; | |
103 | } VhostUserLog; | |
104 | ||
a77e6b14 NN |
105 | typedef struct VhostUserMsg { |
106 | VhostUserRequest request; | |
107 | ||
108 | #define VHOST_USER_VERSION_MASK (0x3) | |
109 | #define VHOST_USER_REPLY_MASK (0x1<<2) | |
110 | uint32_t flags; | |
111 | uint32_t size; /* the following payload size */ | |
112 | union { | |
2b8819c6 VK |
113 | #define VHOST_USER_VRING_IDX_MASK (0xff) |
114 | #define VHOST_USER_VRING_NOFD_MASK (0x1<<8) | |
a77e6b14 NN |
115 | uint64_t u64; |
116 | struct vhost_vring_state state; | |
117 | struct vhost_vring_addr addr; | |
118 | VhostUserMemory memory; | |
2b8819c6 | 119 | VhostUserLog log; |
12ebf690 | 120 | } payload; |
a77e6b14 NN |
121 | } QEMU_PACKED VhostUserMsg; |
122 | ||
123 | static VhostUserMsg m __attribute__ ((unused)); | |
124 | #define VHOST_USER_HDR_SIZE (sizeof(m.request) \ | |
125 | + sizeof(m.flags) \ | |
126 | + sizeof(m.size)) | |
127 | ||
128 | #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) | |
129 | ||
130 | /* The version of the protocol we support */ | |
131 | #define VHOST_USER_VERSION (0x1) | |
132 | /*****************************************************************************/ | |
133 | ||
9294d76c MAL |
134 | enum { |
135 | TEST_FLAGS_OK, | |
136 | TEST_FLAGS_DISCONNECT, | |
137 | TEST_FLAGS_BAD, | |
138 | TEST_FLAGS_END, | |
139 | }; | |
140 | ||
ae31fb54 | 141 | typedef struct TestServer { |
0c0eb302 | 142 | QPCIBus *bus; |
ae31fb54 | 143 | gchar *socket_path; |
a899b1ea | 144 | gchar *mig_path; |
ae31fb54 | 145 | gchar *chr_name; |
32a6ebec | 146 | CharBackend chr; |
ae31fb54 MAL |
147 | int fds_num; |
148 | int fds[VHOST_MEMORY_MAX_NREGIONS]; | |
149 | VhostUserMemory memory; | |
634d39b4 PB |
150 | CompatGMutex data_mutex; |
151 | CompatGCond data_cond; | |
b1819747 | 152 | int log_fd; |
d08e42a1 | 153 | uint64_t rings; |
5d443f5a | 154 | bool test_fail; |
9294d76c | 155 | int test_flags; |
ed0a8d92 | 156 | int queues; |
ae31fb54 | 157 | } TestServer; |
bd95939f | 158 | |
704b2168 MAL |
159 | static const char *tmpfs; |
160 | static const char *root; | |
161 | ||
cdafe929 EH |
162 | static void init_virtio_dev(TestServer *s) |
163 | { | |
cdafe929 EH |
164 | QVirtioPCIDevice *dev; |
165 | uint32_t features; | |
166 | ||
0c0eb302 MAL |
167 | s->bus = qpci_init_pc(NULL); |
168 | g_assert_nonnull(s->bus); | |
cdafe929 | 169 | |
0c0eb302 | 170 | dev = qvirtio_pci_device_find(s->bus, VIRTIO_ID_NET); |
cdafe929 EH |
171 | g_assert_nonnull(dev); |
172 | ||
173 | qvirtio_pci_device_enable(dev); | |
6b9cdf4c LV |
174 | qvirtio_reset(&dev->vdev); |
175 | qvirtio_set_acknowledge(&dev->vdev); | |
176 | qvirtio_set_driver(&dev->vdev); | |
cdafe929 | 177 | |
6b9cdf4c | 178 | features = qvirtio_get_features(&dev->vdev); |
cdafe929 | 179 | features = features & VIRTIO_NET_F_MAC; |
6b9cdf4c | 180 | qvirtio_set_features(&dev->vdev, features); |
cdafe929 | 181 | |
6b9cdf4c | 182 | qvirtio_set_driver_ok(&dev->vdev); |
0c0eb302 | 183 | qvirtio_pci_device_free(dev); |
cdafe929 EH |
184 | } |
185 | ||
ae31fb54 | 186 | static void wait_for_fds(TestServer *s) |
a77e6b14 | 187 | { |
a77e6b14 | 188 | gint64 end_time; |
a77e6b14 | 189 | |
ae31fb54 | 190 | g_mutex_lock(&s->data_mutex); |
a77e6b14 | 191 | |
ca06d9cc | 192 | end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; |
ae31fb54 MAL |
193 | while (!s->fds_num) { |
194 | if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { | |
a77e6b14 | 195 | /* timeout has passed */ |
ae31fb54 | 196 | g_assert(s->fds_num); |
a77e6b14 NN |
197 | break; |
198 | } | |
199 | } | |
200 | ||
201 | /* check for sanity */ | |
ae31fb54 MAL |
202 | g_assert_cmpint(s->fds_num, >, 0); |
203 | g_assert_cmpint(s->fds_num, ==, s->memory.nregions); | |
a77e6b14 | 204 | |
ae31fb54 | 205 | g_mutex_unlock(&s->data_mutex); |
cf72b57f MAL |
206 | } |
207 | ||
041088c7 | 208 | static void read_guest_mem(const void *data) |
cf72b57f | 209 | { |
041088c7 | 210 | TestServer *s = (void *)data; |
cf72b57f MAL |
211 | uint32_t *guest_mem; |
212 | int i, j; | |
213 | size_t size; | |
214 | ||
ae31fb54 | 215 | wait_for_fds(s); |
cf72b57f | 216 | |
ae31fb54 | 217 | g_mutex_lock(&s->data_mutex); |
cf72b57f | 218 | |
a77e6b14 | 219 | /* iterate all regions */ |
ae31fb54 | 220 | for (i = 0; i < s->fds_num; i++) { |
a77e6b14 NN |
221 | |
222 | /* We'll check only the region statring at 0x0*/ | |
ae31fb54 | 223 | if (s->memory.regions[i].guest_phys_addr != 0x0) { |
a77e6b14 NN |
224 | continue; |
225 | } | |
226 | ||
ae31fb54 | 227 | g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); |
a77e6b14 | 228 | |
ae31fb54 MAL |
229 | size = s->memory.regions[i].memory_size + |
230 | s->memory.regions[i].mmap_offset; | |
d6970e3b NN |
231 | |
232 | guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, | |
ae31fb54 | 233 | MAP_SHARED, s->fds[i], 0); |
d6970e3b NN |
234 | |
235 | g_assert(guest_mem != MAP_FAILED); | |
ae31fb54 | 236 | guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); |
a77e6b14 NN |
237 | |
238 | for (j = 0; j < 256; j++) { | |
ae31fb54 | 239 | uint32_t a = readl(s->memory.regions[i].guest_phys_addr + j*4); |
a77e6b14 NN |
240 | uint32_t b = guest_mem[j]; |
241 | ||
242 | g_assert_cmpint(a, ==, b); | |
243 | } | |
244 | ||
ae31fb54 | 245 | munmap(guest_mem, s->memory.regions[i].memory_size); |
a77e6b14 NN |
246 | } |
247 | ||
ae31fb54 | 248 | g_mutex_unlock(&s->data_mutex); |
a77e6b14 NN |
249 | } |
250 | ||
251 | static void *thread_function(void *data) | |
252 | { | |
9732baf6 | 253 | GMainLoop *loop = data; |
a77e6b14 NN |
254 | g_main_loop_run(loop); |
255 | return NULL; | |
256 | } | |
257 | ||
258 | static int chr_can_read(void *opaque) | |
259 | { | |
260 | return VHOST_USER_HDR_SIZE; | |
261 | } | |
262 | ||
263 | static void chr_read(void *opaque, const uint8_t *buf, int size) | |
264 | { | |
ae31fb54 | 265 | TestServer *s = opaque; |
32a6ebec | 266 | CharBackend *chr = &s->chr; |
a77e6b14 NN |
267 | VhostUserMsg msg; |
268 | uint8_t *p = (uint8_t *) &msg; | |
269 | int fd; | |
270 | ||
5d443f5a | 271 | if (s->test_fail) { |
5345fdb4 | 272 | qemu_chr_fe_disconnect(chr); |
5d443f5a MAL |
273 | /* now switch to non-failure */ |
274 | s->test_fail = false; | |
275 | } | |
276 | ||
a77e6b14 NN |
277 | if (size != VHOST_USER_HDR_SIZE) { |
278 | g_test_message("Wrong message size received %d\n", size); | |
279 | return; | |
280 | } | |
281 | ||
ae31fb54 | 282 | g_mutex_lock(&s->data_mutex); |
a77e6b14 NN |
283 | memcpy(p, buf, VHOST_USER_HDR_SIZE); |
284 | ||
285 | if (msg.size) { | |
286 | p += VHOST_USER_HDR_SIZE; | |
5345fdb4 | 287 | size = qemu_chr_fe_read_all(chr, p, msg.size); |
4616e359 MAL |
288 | if (size != msg.size) { |
289 | g_test_message("Wrong message size received %d != %d\n", | |
290 | size, msg.size); | |
291 | return; | |
292 | } | |
a77e6b14 NN |
293 | } |
294 | ||
295 | switch (msg.request) { | |
296 | case VHOST_USER_GET_FEATURES: | |
8a9b6b37 MT |
297 | /* send back features to qemu */ |
298 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
299 | msg.size = sizeof(m.payload.u64); |
300 | msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL | | |
b1819747 | 301 | 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; |
ed0a8d92 MAL |
302 | if (s->queues > 1) { |
303 | msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ; | |
304 | } | |
9294d76c MAL |
305 | if (s->test_flags >= TEST_FLAGS_BAD) { |
306 | msg.payload.u64 = 0; | |
307 | s->test_flags = TEST_FLAGS_END; | |
308 | } | |
8a9b6b37 | 309 | p = (uint8_t *) &msg; |
5345fdb4 | 310 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); |
8a9b6b37 MT |
311 | break; |
312 | ||
313 | case VHOST_USER_SET_FEATURES: | |
12ebf690 | 314 | g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES), |
8a9b6b37 | 315 | !=, 0ULL); |
9294d76c | 316 | if (s->test_flags == TEST_FLAGS_DISCONNECT) { |
5345fdb4 | 317 | qemu_chr_fe_disconnect(chr); |
9294d76c MAL |
318 | s->test_flags = TEST_FLAGS_BAD; |
319 | } | |
8a9b6b37 MT |
320 | break; |
321 | ||
322 | case VHOST_USER_GET_PROTOCOL_FEATURES: | |
a77e6b14 NN |
323 | /* send back features to qemu */ |
324 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
325 | msg.size = sizeof(m.payload.u64); |
326 | msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD; | |
ed0a8d92 MAL |
327 | if (s->queues > 1) { |
328 | msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ; | |
329 | } | |
a77e6b14 | 330 | p = (uint8_t *) &msg; |
5345fdb4 | 331 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); |
a77e6b14 NN |
332 | break; |
333 | ||
334 | case VHOST_USER_GET_VRING_BASE: | |
335 | /* send back vring base to qemu */ | |
336 | msg.flags |= VHOST_USER_REPLY_MASK; | |
12ebf690 MT |
337 | msg.size = sizeof(m.payload.state); |
338 | msg.payload.state.num = 0; | |
a77e6b14 | 339 | p = (uint8_t *) &msg; |
5345fdb4 | 340 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); |
d08e42a1 | 341 | |
ed0a8d92 | 342 | assert(msg.payload.state.index < s->queues * 2); |
d08e42a1 | 343 | s->rings &= ~(0x1ULL << msg.payload.state.index); |
a77e6b14 NN |
344 | break; |
345 | ||
346 | case VHOST_USER_SET_MEM_TABLE: | |
347 | /* received the mem table */ | |
12ebf690 | 348 | memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory)); |
5345fdb4 | 349 | s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, |
32a6ebec | 350 | G_N_ELEMENTS(s->fds)); |
a77e6b14 NN |
351 | |
352 | /* signal the test that it can continue */ | |
ae31fb54 | 353 | g_cond_signal(&s->data_cond); |
a77e6b14 NN |
354 | break; |
355 | ||
356 | case VHOST_USER_SET_VRING_KICK: | |
357 | case VHOST_USER_SET_VRING_CALL: | |
358 | /* consume the fd */ | |
5345fdb4 | 359 | qemu_chr_fe_get_msgfds(chr, &fd, 1); |
a77e6b14 NN |
360 | /* |
361 | * This is a non-blocking eventfd. | |
362 | * The receive function forces it to be blocking, | |
363 | * so revert it back to non-blocking. | |
364 | */ | |
365 | qemu_set_nonblock(fd); | |
366 | break; | |
b1819747 MAL |
367 | |
368 | case VHOST_USER_SET_LOG_BASE: | |
369 | if (s->log_fd != -1) { | |
370 | close(s->log_fd); | |
371 | s->log_fd = -1; | |
372 | } | |
5345fdb4 | 373 | qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1); |
b1819747 MAL |
374 | msg.flags |= VHOST_USER_REPLY_MASK; |
375 | msg.size = 0; | |
376 | p = (uint8_t *) &msg; | |
5345fdb4 | 377 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE); |
b1819747 MAL |
378 | |
379 | g_cond_signal(&s->data_cond); | |
380 | break; | |
381 | ||
d08e42a1 | 382 | case VHOST_USER_SET_VRING_BASE: |
ed0a8d92 | 383 | assert(msg.payload.state.index < s->queues * 2); |
d08e42a1 | 384 | s->rings |= 0x1ULL << msg.payload.state.index; |
1d9edff7 MAL |
385 | break; |
386 | ||
ed0a8d92 MAL |
387 | case VHOST_USER_GET_QUEUE_NUM: |
388 | msg.flags |= VHOST_USER_REPLY_MASK; | |
389 | msg.size = sizeof(m.payload.u64); | |
390 | msg.payload.u64 = s->queues; | |
391 | p = (uint8_t *) &msg; | |
5345fdb4 | 392 | qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); |
ed0a8d92 MAL |
393 | break; |
394 | ||
a77e6b14 NN |
395 | default: |
396 | break; | |
397 | } | |
ae31fb54 MAL |
398 | |
399 | g_mutex_unlock(&s->data_mutex); | |
a77e6b14 NN |
400 | } |
401 | ||
1b7e1e3b | 402 | static const char *init_hugepagefs(const char *path) |
a77e6b14 | 403 | { |
a77e6b14 NN |
404 | struct statfs fs; |
405 | int ret; | |
406 | ||
a77e6b14 NN |
407 | if (access(path, R_OK | W_OK | X_OK)) { |
408 | g_test_message("access on path (%s): %s\n", path, strerror(errno)); | |
409 | return NULL; | |
410 | } | |
411 | ||
412 | do { | |
413 | ret = statfs(path, &fs); | |
414 | } while (ret != 0 && errno == EINTR); | |
415 | ||
416 | if (ret != 0) { | |
417 | g_test_message("statfs on path (%s): %s\n", path, strerror(errno)); | |
418 | return NULL; | |
419 | } | |
420 | ||
421 | if (fs.f_type != HUGETLBFS_MAGIC) { | |
422 | g_test_message("Warning: path not on HugeTLBFS: %s\n", path); | |
423 | return NULL; | |
424 | } | |
425 | ||
426 | return path; | |
427 | } | |
428 | ||
704b2168 | 429 | static TestServer *test_server_new(const gchar *name) |
ae31fb54 MAL |
430 | { |
431 | TestServer *server = g_new0(TestServer, 1); | |
ae31fb54 MAL |
432 | |
433 | server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name); | |
a899b1ea | 434 | server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name); |
ae31fb54 | 435 | server->chr_name = g_strdup_printf("chr-%s", name); |
ae31fb54 MAL |
436 | |
437 | g_mutex_init(&server->data_mutex); | |
438 | g_cond_init(&server->data_cond); | |
439 | ||
b1819747 | 440 | server->log_fd = -1; |
ed0a8d92 | 441 | server->queues = 1; |
b1819747 | 442 | |
ae31fb54 MAL |
443 | return server; |
444 | } | |
445 | ||
9294d76c MAL |
446 | static void chr_event(void *opaque, int event) |
447 | { | |
448 | TestServer *s = opaque; | |
449 | ||
450 | if (s->test_flags == TEST_FLAGS_END && | |
451 | event == CHR_EVENT_CLOSED) { | |
452 | s->test_flags = TEST_FLAGS_OK; | |
453 | } | |
454 | } | |
455 | ||
4616e359 MAL |
456 | static void test_server_create_chr(TestServer *server, const gchar *opt) |
457 | { | |
458 | gchar *chr_path; | |
0ec7b3e7 | 459 | Chardev *chr; |
5345fdb4 | 460 | |
4616e359 | 461 | chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt); |
32a6ebec | 462 | chr = qemu_chr_new(server->chr_name, chr_path); |
4616e359 MAL |
463 | g_free(chr_path); |
464 | ||
5345fdb4 MAL |
465 | qemu_chr_fe_init(&server->chr, chr, &error_abort); |
466 | qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read, | |
39ab61c6 | 467 | chr_event, server, NULL, true); |
4616e359 MAL |
468 | } |
469 | ||
470 | static void test_server_listen(TestServer *server) | |
471 | { | |
472 | test_server_create_chr(server, ",server,nowait"); | |
473 | } | |
474 | ||
475 | static inline void test_server_connect(TestServer *server) | |
476 | { | |
477 | test_server_create_chr(server, ",reconnect=1"); | |
478 | } | |
479 | ||
480 | #define GET_QEMU_CMD(s) \ | |
481 | g_strdup_printf(QEMU_CMD, 512, 512, (root), (s)->chr_name, \ | |
482 | (s)->socket_path, "", (s)->chr_name) | |
483 | ||
484 | #define GET_QEMU_CMDE(s, mem, chr_opts, extra, ...) \ | |
485 | g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name, \ | |
486 | (s)->socket_path, (chr_opts), (s)->chr_name, ##__VA_ARGS__) | |
ae31fb54 | 487 | |
9732baf6 | 488 | static gboolean _test_server_free(TestServer *server) |
ae31fb54 MAL |
489 | { |
490 | int i; | |
0ec7b3e7 | 491 | Chardev *chr = qemu_chr_fe_get_driver(&server->chr); |
ae31fb54 | 492 | |
c39860e6 | 493 | qemu_chr_fe_deinit(&server->chr); |
5345fdb4 | 494 | qemu_chr_delete(chr); |
ae31fb54 MAL |
495 | |
496 | for (i = 0; i < server->fds_num; i++) { | |
497 | close(server->fds[i]); | |
498 | } | |
499 | ||
b1819747 MAL |
500 | if (server->log_fd != -1) { |
501 | close(server->log_fd); | |
502 | } | |
503 | ||
ae31fb54 MAL |
504 | unlink(server->socket_path); |
505 | g_free(server->socket_path); | |
506 | ||
a899b1ea MAL |
507 | unlink(server->mig_path); |
508 | g_free(server->mig_path); | |
509 | ||
b1819747 | 510 | g_free(server->chr_name); |
0c0eb302 MAL |
511 | qpci_free_pc(server->bus); |
512 | ||
ae31fb54 | 513 | g_free(server); |
9732baf6 MAL |
514 | |
515 | return FALSE; | |
516 | } | |
517 | ||
518 | static void test_server_free(TestServer *server) | |
519 | { | |
520 | g_idle_add((GSourceFunc)_test_server_free, server); | |
ae31fb54 MAL |
521 | } |
522 | ||
b1819747 MAL |
523 | static void wait_for_log_fd(TestServer *s) |
524 | { | |
525 | gint64 end_time; | |
526 | ||
527 | g_mutex_lock(&s->data_mutex); | |
528 | end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; | |
529 | while (s->log_fd == -1) { | |
530 | if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { | |
531 | /* timeout has passed */ | |
532 | g_assert(s->log_fd != -1); | |
533 | break; | |
534 | } | |
535 | } | |
536 | ||
537 | g_mutex_unlock(&s->data_mutex); | |
538 | } | |
539 | ||
3a87d009 | 540 | static void write_guest_mem(TestServer *s, uint32_t seed) |
b1819747 MAL |
541 | { |
542 | uint32_t *guest_mem; | |
543 | int i, j; | |
544 | size_t size; | |
545 | ||
546 | wait_for_fds(s); | |
547 | ||
548 | /* iterate all regions */ | |
549 | for (i = 0; i < s->fds_num; i++) { | |
550 | ||
551 | /* We'll write only the region statring at 0x0 */ | |
552 | if (s->memory.regions[i].guest_phys_addr != 0x0) { | |
553 | continue; | |
554 | } | |
555 | ||
556 | g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); | |
557 | ||
558 | size = s->memory.regions[i].memory_size + | |
559 | s->memory.regions[i].mmap_offset; | |
560 | ||
561 | guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, | |
562 | MAP_SHARED, s->fds[i], 0); | |
563 | ||
564 | g_assert(guest_mem != MAP_FAILED); | |
565 | guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); | |
566 | ||
567 | for (j = 0; j < 256; j++) { | |
568 | guest_mem[j] = seed + j; | |
569 | } | |
570 | ||
571 | munmap(guest_mem, s->memory.regions[i].memory_size); | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | static guint64 get_log_size(TestServer *s) | |
577 | { | |
578 | guint64 log_size = 0; | |
579 | int i; | |
580 | ||
581 | for (i = 0; i < s->memory.nregions; ++i) { | |
582 | VhostUserMemoryRegion *reg = &s->memory.regions[i]; | |
583 | guint64 last = range_get_last(reg->guest_phys_addr, | |
584 | reg->memory_size); | |
585 | log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1); | |
586 | } | |
587 | ||
588 | return log_size; | |
589 | } | |
590 | ||
1d9edff7 MAL |
591 | typedef struct TestMigrateSource { |
592 | GSource source; | |
593 | TestServer *src; | |
594 | TestServer *dest; | |
595 | } TestMigrateSource; | |
596 | ||
597 | static gboolean | |
598 | test_migrate_source_check(GSource *source) | |
599 | { | |
600 | TestMigrateSource *t = (TestMigrateSource *)source; | |
d08e42a1 | 601 | gboolean overlap = t->src->rings && t->dest->rings; |
1d9edff7 MAL |
602 | |
603 | g_assert(!overlap); | |
604 | ||
605 | return FALSE; | |
606 | } | |
607 | ||
45ce5126 MAL |
608 | #if !GLIB_CHECK_VERSION(2,36,0) |
609 | /* this callback is unnecessary with glib >2.36, the default | |
610 | * prepare for the source does the same */ | |
611 | static gboolean | |
612 | test_migrate_source_prepare(GSource *source, gint *timeout) | |
613 | { | |
614 | *timeout = -1; | |
615 | return FALSE; | |
616 | } | |
617 | #endif | |
618 | ||
1d9edff7 | 619 | GSourceFuncs test_migrate_source_funcs = { |
45ce5126 MAL |
620 | #if !GLIB_CHECK_VERSION(2,36,0) |
621 | .prepare = test_migrate_source_prepare, | |
622 | #endif | |
623 | .check = test_migrate_source_check, | |
1d9edff7 MAL |
624 | }; |
625 | ||
b1819747 MAL |
626 | static void test_migrate(void) |
627 | { | |
628 | TestServer *s = test_server_new("src"); | |
629 | TestServer *dest = test_server_new("dest"); | |
a899b1ea | 630 | char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path); |
b1819747 | 631 | QTestState *global = global_qtest, *from, *to; |
1d9edff7 | 632 | GSource *source; |
b1819747 MAL |
633 | gchar *cmd; |
634 | QDict *rsp; | |
635 | guint8 *log; | |
636 | guint64 size; | |
637 | ||
4616e359 MAL |
638 | test_server_listen(s); |
639 | test_server_listen(dest); | |
640 | ||
641 | cmd = GET_QEMU_CMDE(s, 2, "", ""); | |
b1819747 MAL |
642 | from = qtest_start(cmd); |
643 | g_free(cmd); | |
644 | ||
cdafe929 | 645 | init_virtio_dev(s); |
b1819747 MAL |
646 | wait_for_fds(s); |
647 | size = get_log_size(s); | |
648 | g_assert_cmpint(size, ==, (2 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)); | |
649 | ||
4616e359 | 650 | cmd = GET_QEMU_CMDE(dest, 2, "", " -incoming %s", uri); |
b1819747 MAL |
651 | to = qtest_init(cmd); |
652 | g_free(cmd); | |
653 | ||
1d9edff7 MAL |
654 | source = g_source_new(&test_migrate_source_funcs, |
655 | sizeof(TestMigrateSource)); | |
656 | ((TestMigrateSource *)source)->src = s; | |
657 | ((TestMigrateSource *)source)->dest = dest; | |
658 | g_source_attach(source, NULL); | |
659 | ||
b1819747 MAL |
660 | /* slow down migration to have time to fiddle with log */ |
661 | /* TODO: qtest could learn to break on some places */ | |
662 | rsp = qmp("{ 'execute': 'migrate_set_speed'," | |
663 | "'arguments': { 'value': 10 } }"); | |
664 | g_assert(qdict_haskey(rsp, "return")); | |
665 | QDECREF(rsp); | |
666 | ||
667 | cmd = g_strdup_printf("{ 'execute': 'migrate'," | |
668 | "'arguments': { 'uri': '%s' } }", | |
669 | uri); | |
670 | rsp = qmp(cmd); | |
671 | g_free(cmd); | |
672 | g_assert(qdict_haskey(rsp, "return")); | |
673 | QDECREF(rsp); | |
674 | ||
675 | wait_for_log_fd(s); | |
676 | ||
677 | log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0); | |
678 | g_assert(log != MAP_FAILED); | |
679 | ||
680 | /* modify first page */ | |
681 | write_guest_mem(s, 0x42); | |
682 | log[0] = 1; | |
683 | munmap(log, size); | |
684 | ||
685 | /* speed things up */ | |
686 | rsp = qmp("{ 'execute': 'migrate_set_speed'," | |
687 | "'arguments': { 'value': 0 } }"); | |
688 | g_assert(qdict_haskey(rsp, "return")); | |
689 | QDECREF(rsp); | |
690 | ||
691 | qmp_eventwait("STOP"); | |
692 | ||
693 | global_qtest = to; | |
694 | qmp_eventwait("RESUME"); | |
695 | ||
696 | read_guest_mem(dest); | |
697 | ||
1d9edff7 MAL |
698 | g_source_destroy(source); |
699 | g_source_unref(source); | |
700 | ||
b1819747 MAL |
701 | qtest_quit(to); |
702 | test_server_free(dest); | |
703 | qtest_quit(from); | |
704 | test_server_free(s); | |
a899b1ea | 705 | g_free(uri); |
b1819747 MAL |
706 | |
707 | global_qtest = global; | |
708 | } | |
709 | ||
4616e359 MAL |
710 | static void wait_for_rings_started(TestServer *s, size_t count) |
711 | { | |
712 | gint64 end_time; | |
713 | ||
714 | g_mutex_lock(&s->data_mutex); | |
715 | end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; | |
716 | while (ctpop64(s->rings) != count) { | |
717 | if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { | |
718 | /* timeout has passed */ | |
719 | g_assert_cmpint(ctpop64(s->rings), ==, count); | |
720 | break; | |
721 | } | |
722 | } | |
723 | ||
724 | g_mutex_unlock(&s->data_mutex); | |
725 | } | |
726 | ||
ed0a8d92 | 727 | #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS |
4616e359 MAL |
728 | static gboolean |
729 | reconnect_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 | ||
738 | static gpointer | |
739 | connect_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 | ||
750 | static 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 | ||
776 | static 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 | |
785 | static 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 | ||
804 | static 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 |
813 | static 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 | ||
832 | static 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 |
843 | static 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 | 859 | static 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 | ||
874 | static 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 |
921 | int 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); |
4616e359 MAL |
967 | #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS |
968 | qtest_add_func("/vhost-user/reconnect/subprocess", | |
969 | test_reconnect_subprocess); | |
970 | qtest_add_func("/vhost-user/reconnect", test_reconnect); | |
5d443f5a MAL |
971 | qtest_add_func("/vhost-user/connect-fail/subprocess", |
972 | test_connect_fail_subprocess); | |
973 | qtest_add_func("/vhost-user/connect-fail", test_connect_fail); | |
9294d76c MAL |
974 | qtest_add_func("/vhost-user/flags-mismatch/subprocess", |
975 | test_flags_mismatch_subprocess); | |
976 | qtest_add_func("/vhost-user/flags-mismatch", test_flags_mismatch); | |
4616e359 | 977 | #endif |
a77e6b14 NN |
978 | |
979 | ret = g_test_run(); | |
980 | ||
981 | if (s) { | |
982 | qtest_quit(s); | |
983 | } | |
984 | ||
985 | /* cleanup */ | |
ae31fb54 | 986 | test_server_free(server); |
a77e6b14 | 987 | |
9732baf6 MAL |
988 | /* finish the helper thread and dispatch pending sources */ |
989 | g_main_loop_quit(loop); | |
990 | g_thread_join(thread); | |
991 | while (g_main_context_pending(NULL)) { | |
992 | g_main_context_iteration (NULL, TRUE); | |
993 | } | |
994 | g_main_loop_unref(loop); | |
995 | ||
1b7e1e3b MT |
996 | ret = rmdir(tmpfs); |
997 | if (ret != 0) { | |
998 | g_test_message("unable to rmdir: path (%s): %s\n", | |
999 | tmpfs, strerror(errno)); | |
1000 | } | |
1001 | g_assert_cmpint(ret, ==, 0); | |
1002 | ||
a77e6b14 NN |
1003 | return ret; |
1004 | } |