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 11 #include "qemu/osdep.h" 12 13 #include "libqtest-single.h" 14 #include "qapi/error.h" 15 #include "qapi/qmp/qdict.h" 16 #include "qemu/config-file.h" 17 #include "qemu/option.h" 18 #include "qemu/range.h" 19 #include "qemu/sockets.h" 20 #include "chardev/char-fe.h" 21 #include "qemu/memfd.h" 22 #include "qemu/module.h" 23 #include "sysemu/sysemu.h" 24 #include "libqos/libqos.h" 25 #include "libqos/pci-pc.h" 26 #include "libqos/virtio-pci.h" 27 28 #include "libqos/malloc-pc.h" 29 #include "libqos/qgraph_internal.h" 30 #include "hw/virtio/virtio-net.h" 31 32 #include "standard-headers/linux/vhost_types.h" 33 #include "standard-headers/linux/virtio_ids.h" 34 #include "standard-headers/linux/virtio_net.h" 35 #include "standard-headers/linux/virtio_gpio.h" 36 #include "standard-headers/linux/virtio_scmi.h" 37 38 #ifdef CONFIG_LINUX 39 #include <sys/vfs.h> 40 #endif 41 42 43 #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \ 44 "mem-path=%s,share=on -numa node,memdev=mem" 45 #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \ 46 " -numa node,memdev=mem" 47 #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s" 48 #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce=on" 49 50 #define HUGETLBFS_MAGIC 0x958458f6 51 52 /*********** FROM hw/virtio/vhost-user.c *************************************/ 53 54 #define VHOST_MEMORY_MAX_NREGIONS 8 55 #define VHOST_MAX_VIRTQUEUES 0x100 56 57 #define VHOST_USER_F_PROTOCOL_FEATURES 30 58 #define VIRTIO_F_VERSION_1 32 59 60 #define VHOST_USER_PROTOCOL_F_MQ 0 61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1 62 #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6 63 #define VHOST_USER_PROTOCOL_F_CONFIG 9 64 65 #define VHOST_LOG_PAGE 0x1000 66 67 typedef enum VhostUserRequest { 68 VHOST_USER_NONE = 0, 69 VHOST_USER_GET_FEATURES = 1, 70 VHOST_USER_SET_FEATURES = 2, 71 VHOST_USER_SET_OWNER = 3, 72 VHOST_USER_RESET_OWNER = 4, 73 VHOST_USER_SET_MEM_TABLE = 5, 74 VHOST_USER_SET_LOG_BASE = 6, 75 VHOST_USER_SET_LOG_FD = 7, 76 VHOST_USER_SET_VRING_NUM = 8, 77 VHOST_USER_SET_VRING_ADDR = 9, 78 VHOST_USER_SET_VRING_BASE = 10, 79 VHOST_USER_GET_VRING_BASE = 11, 80 VHOST_USER_SET_VRING_KICK = 12, 81 VHOST_USER_SET_VRING_CALL = 13, 82 VHOST_USER_SET_VRING_ERR = 14, 83 VHOST_USER_GET_PROTOCOL_FEATURES = 15, 84 VHOST_USER_SET_PROTOCOL_FEATURES = 16, 85 VHOST_USER_GET_QUEUE_NUM = 17, 86 VHOST_USER_SET_VRING_ENABLE = 18, 87 VHOST_USER_GET_CONFIG = 24, 88 VHOST_USER_SET_CONFIG = 25, 89 VHOST_USER_MAX 90 } VhostUserRequest; 91 92 typedef struct VhostUserMemoryRegion { 93 uint64_t guest_phys_addr; 94 uint64_t memory_size; 95 uint64_t userspace_addr; 96 uint64_t mmap_offset; 97 } VhostUserMemoryRegion; 98 99 typedef struct VhostUserMemory { 100 uint32_t nregions; 101 uint32_t padding; 102 VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS]; 103 } VhostUserMemory; 104 105 typedef struct VhostUserLog { 106 uint64_t mmap_size; 107 uint64_t mmap_offset; 108 } VhostUserLog; 109 110 typedef struct VhostUserMsg { 111 VhostUserRequest request; 112 113 #define VHOST_USER_VERSION_MASK (0x3) 114 #define VHOST_USER_REPLY_MASK (0x1<<2) 115 uint32_t flags; 116 uint32_t size; /* the following payload size */ 117 union { 118 #define VHOST_USER_VRING_IDX_MASK (0xff) 119 #define VHOST_USER_VRING_NOFD_MASK (0x1<<8) 120 uint64_t u64; 121 struct vhost_vring_state state; 122 struct vhost_vring_addr addr; 123 VhostUserMemory memory; 124 VhostUserLog log; 125 } payload; 126 } QEMU_PACKED VhostUserMsg; 127 128 static VhostUserMsg m __attribute__ ((unused)); 129 #define VHOST_USER_HDR_SIZE (sizeof(m.request) \ 130 + sizeof(m.flags) \ 131 + sizeof(m.size)) 132 133 #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE) 134 135 /* The version of the protocol we support */ 136 #define VHOST_USER_VERSION (0x1) 137 /*****************************************************************************/ 138 139 enum { 140 TEST_FLAGS_OK, 141 TEST_FLAGS_DISCONNECT, 142 TEST_FLAGS_BAD, 143 TEST_FLAGS_END, 144 }; 145 146 enum { 147 VHOST_USER_NET, 148 VHOST_USER_GPIO, 149 VHOST_USER_SCMI, 150 }; 151 152 typedef struct TestServer { 153 gchar *socket_path; 154 gchar *mig_path; 155 gchar *chr_name; 156 gchar *tmpfs; 157 CharBackend chr; 158 int fds_num; 159 int fds[VHOST_MEMORY_MAX_NREGIONS]; 160 VhostUserMemory memory; 161 GMainContext *context; 162 GMainLoop *loop; 163 GThread *thread; 164 GMutex data_mutex; 165 GCond data_cond; 166 int log_fd; 167 uint64_t rings; 168 bool test_fail; 169 int test_flags; 170 int queues; 171 struct vhost_user_ops *vu_ops; 172 } TestServer; 173 174 struct vhost_user_ops { 175 /* Device types. */ 176 int type; 177 void (*append_opts)(TestServer *s, GString *cmd_line, 178 const char *chr_opts); 179 180 /* VHOST-USER commands. */ 181 uint64_t (*get_features)(TestServer *s); 182 void (*set_features)(TestServer *s, CharBackend *chr, 183 VhostUserMsg *msg); 184 void (*get_protocol_features)(TestServer *s, 185 CharBackend *chr, VhostUserMsg *msg); 186 }; 187 188 static const char *init_hugepagefs(void); 189 static TestServer *test_server_new(const gchar *name, 190 struct vhost_user_ops *ops); 191 static void test_server_free(TestServer *server); 192 static void test_server_listen(TestServer *server); 193 194 enum test_memfd { 195 TEST_MEMFD_AUTO, 196 TEST_MEMFD_YES, 197 TEST_MEMFD_NO, 198 }; 199 200 static void append_vhost_net_opts(TestServer *s, GString *cmd_line, 201 const char *chr_opts) 202 { 203 g_string_append_printf(cmd_line, QEMU_CMD_CHR QEMU_CMD_NETDEV, 204 s->chr_name, s->socket_path, 205 chr_opts, s->chr_name); 206 } 207 208 /* 209 * For GPIO there are no other magic devices we need to add (like 210 * block or netdev) so all we need to worry about is the vhost-user 211 * chardev socket. 212 */ 213 static void append_vhost_gpio_opts(TestServer *s, GString *cmd_line, 214 const char *chr_opts) 215 { 216 g_string_append_printf(cmd_line, QEMU_CMD_CHR, 217 s->chr_name, s->socket_path, 218 chr_opts); 219 } 220 221 static void append_mem_opts(TestServer *server, GString *cmd_line, 222 int size, enum test_memfd memfd) 223 { 224 if (memfd == TEST_MEMFD_AUTO) { 225 memfd = qemu_memfd_check(MFD_ALLOW_SEALING) ? TEST_MEMFD_YES 226 : TEST_MEMFD_NO; 227 } 228 229 if (memfd == TEST_MEMFD_YES) { 230 g_string_append_printf(cmd_line, QEMU_CMD_MEMFD, size, size); 231 } else { 232 const char *root = init_hugepagefs() ? : server->tmpfs; 233 234 g_string_append_printf(cmd_line, QEMU_CMD_MEM, size, size, root); 235 } 236 } 237 238 static bool wait_for_fds(TestServer *s) 239 { 240 gint64 end_time; 241 bool got_region; 242 int i; 243 244 g_mutex_lock(&s->data_mutex); 245 246 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 247 while (!s->fds_num) { 248 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { 249 /* timeout has passed */ 250 g_assert(s->fds_num); 251 break; 252 } 253 } 254 255 /* check for sanity */ 256 g_assert_cmpint(s->fds_num, >, 0); 257 g_assert_cmpint(s->fds_num, ==, s->memory.nregions); 258 259 g_mutex_unlock(&s->data_mutex); 260 261 got_region = false; 262 for (i = 0; i < s->memory.nregions; ++i) { 263 VhostUserMemoryRegion *reg = &s->memory.regions[i]; 264 if (reg->guest_phys_addr == 0) { 265 got_region = true; 266 break; 267 } 268 } 269 if (!got_region) { 270 g_test_skip("No memory at address 0x0"); 271 } 272 return got_region; 273 } 274 275 static void read_guest_mem_server(QTestState *qts, TestServer *s) 276 { 277 uint8_t *guest_mem; 278 int i, j; 279 size_t size; 280 281 g_mutex_lock(&s->data_mutex); 282 283 /* iterate all regions */ 284 for (i = 0; i < s->fds_num; i++) { 285 286 /* We'll check only the region starting at 0x0 */ 287 if (s->memory.regions[i].guest_phys_addr != 0x0) { 288 continue; 289 } 290 291 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); 292 293 size = s->memory.regions[i].memory_size + 294 s->memory.regions[i].mmap_offset; 295 296 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, 297 MAP_SHARED, s->fds[i], 0); 298 299 g_assert(guest_mem != MAP_FAILED); 300 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); 301 302 for (j = 0; j < 1024; j++) { 303 uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j); 304 uint32_t b = guest_mem[j]; 305 306 g_assert_cmpint(a, ==, b); 307 } 308 309 munmap(guest_mem, s->memory.regions[i].memory_size); 310 } 311 312 g_mutex_unlock(&s->data_mutex); 313 } 314 315 static void *thread_function(void *data) 316 { 317 GMainLoop *loop = data; 318 g_main_loop_run(loop); 319 return NULL; 320 } 321 322 static int chr_can_read(void *opaque) 323 { 324 return VHOST_USER_HDR_SIZE; 325 } 326 327 static void chr_read(void *opaque, const uint8_t *buf, int size) 328 { 329 g_autoptr(GError) err = NULL; 330 TestServer *s = opaque; 331 CharBackend *chr = &s->chr; 332 VhostUserMsg msg; 333 uint8_t *p = (uint8_t *) &msg; 334 int fd = -1; 335 336 if (s->test_fail) { 337 qemu_chr_fe_disconnect(chr); 338 /* now switch to non-failure */ 339 s->test_fail = false; 340 } 341 342 if (size != VHOST_USER_HDR_SIZE) { 343 qos_printf("%s: Wrong message size received %d\n", __func__, size); 344 return; 345 } 346 347 g_mutex_lock(&s->data_mutex); 348 memcpy(p, buf, VHOST_USER_HDR_SIZE); 349 350 if (msg.size) { 351 p += VHOST_USER_HDR_SIZE; 352 size = qemu_chr_fe_read_all(chr, p, msg.size); 353 if (size != msg.size) { 354 qos_printf("%s: Wrong message size received %d != %d\n", 355 __func__, size, msg.size); 356 goto out; 357 } 358 } 359 360 switch (msg.request) { 361 case VHOST_USER_GET_FEATURES: 362 /* Mandatory for tests to define get_features */ 363 g_assert(s->vu_ops->get_features); 364 365 /* send back features to qemu */ 366 msg.flags |= VHOST_USER_REPLY_MASK; 367 msg.size = sizeof(m.payload.u64); 368 369 if (s->test_flags >= TEST_FLAGS_BAD) { 370 msg.payload.u64 = 0; 371 s->test_flags = TEST_FLAGS_END; 372 } else { 373 msg.payload.u64 = s->vu_ops->get_features(s); 374 } 375 376 qemu_chr_fe_write_all(chr, (uint8_t *) &msg, 377 VHOST_USER_HDR_SIZE + msg.size); 378 break; 379 380 case VHOST_USER_SET_FEATURES: 381 if (s->vu_ops->set_features) { 382 s->vu_ops->set_features(s, chr, &msg); 383 } 384 break; 385 386 case VHOST_USER_SET_OWNER: 387 /* 388 * We don't need to do anything here, the remote is just 389 * letting us know it is in charge. Just log it. 390 */ 391 qos_printf("set_owner: start of session\n"); 392 break; 393 394 case VHOST_USER_GET_PROTOCOL_FEATURES: 395 if (s->vu_ops->get_protocol_features) { 396 s->vu_ops->get_protocol_features(s, chr, &msg); 397 } 398 break; 399 400 case VHOST_USER_GET_CONFIG: 401 /* 402 * Treat GET_CONFIG as a NOP and just reply and let the guest 403 * consider we have updated its memory. Tests currently don't 404 * require working configs. 405 */ 406 msg.flags |= VHOST_USER_REPLY_MASK; 407 p = (uint8_t *) &msg; 408 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); 409 break; 410 411 case VHOST_USER_SET_PROTOCOL_FEATURES: 412 /* 413 * We did set VHOST_USER_F_PROTOCOL_FEATURES so its valid for 414 * the remote end to send this. There is no handshake reply so 415 * just log the details for debugging. 416 */ 417 qos_printf("set_protocol_features: 0x%"PRIx64 "\n", msg.payload.u64); 418 break; 419 420 /* 421 * A real vhost-user backend would actually set the size and 422 * address of the vrings but we can simply report them. 423 */ 424 case VHOST_USER_SET_VRING_NUM: 425 qos_printf("set_vring_num: %d/%d\n", 426 msg.payload.state.index, msg.payload.state.num); 427 break; 428 case VHOST_USER_SET_VRING_ADDR: 429 qos_printf("set_vring_addr: 0x%"PRIx64"/0x%"PRIx64"/0x%"PRIx64"\n", 430 msg.payload.addr.avail_user_addr, 431 msg.payload.addr.desc_user_addr, 432 msg.payload.addr.used_user_addr); 433 break; 434 435 case VHOST_USER_GET_VRING_BASE: 436 /* send back vring base to qemu */ 437 msg.flags |= VHOST_USER_REPLY_MASK; 438 msg.size = sizeof(m.payload.state); 439 msg.payload.state.num = 0; 440 p = (uint8_t *) &msg; 441 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); 442 443 assert(msg.payload.state.index < s->queues * 2); 444 s->rings &= ~(0x1ULL << msg.payload.state.index); 445 g_cond_broadcast(&s->data_cond); 446 break; 447 448 case VHOST_USER_SET_MEM_TABLE: 449 /* received the mem table */ 450 memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory)); 451 s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds, 452 G_N_ELEMENTS(s->fds)); 453 454 /* signal the test that it can continue */ 455 g_cond_broadcast(&s->data_cond); 456 break; 457 458 case VHOST_USER_SET_VRING_KICK: 459 case VHOST_USER_SET_VRING_CALL: 460 /* consume the fd */ 461 if (!qemu_chr_fe_get_msgfds(chr, &fd, 1) && fd < 0) { 462 qos_printf("call fd: %d, do not set non-blocking\n", fd); 463 break; 464 } 465 /* 466 * This is a non-blocking eventfd. 467 * The receive function forces it to be blocking, 468 * so revert it back to non-blocking. 469 */ 470 g_unix_set_fd_nonblocking(fd, true, &err); 471 g_assert_no_error(err); 472 break; 473 474 case VHOST_USER_SET_LOG_BASE: 475 if (s->log_fd != -1) { 476 close(s->log_fd); 477 s->log_fd = -1; 478 } 479 qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1); 480 msg.flags |= VHOST_USER_REPLY_MASK; 481 msg.size = 0; 482 p = (uint8_t *) &msg; 483 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE); 484 485 g_cond_broadcast(&s->data_cond); 486 break; 487 488 case VHOST_USER_SET_VRING_BASE: 489 assert(msg.payload.state.index < s->queues * 2); 490 s->rings |= 0x1ULL << msg.payload.state.index; 491 g_cond_broadcast(&s->data_cond); 492 break; 493 494 case VHOST_USER_GET_QUEUE_NUM: 495 msg.flags |= VHOST_USER_REPLY_MASK; 496 msg.size = sizeof(m.payload.u64); 497 msg.payload.u64 = s->queues; 498 p = (uint8_t *) &msg; 499 qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size); 500 break; 501 502 case VHOST_USER_SET_VRING_ENABLE: 503 /* 504 * Another case we ignore as we don't need to respond. With a 505 * fully functioning vhost-user we would enable/disable the 506 * vring monitoring. 507 */ 508 qos_printf("set_vring(%d)=%s\n", msg.payload.state.index, 509 msg.payload.state.num ? "enabled" : "disabled"); 510 break; 511 512 default: 513 qos_printf("vhost-user: un-handled message: %d\n", msg.request); 514 break; 515 } 516 517 out: 518 g_mutex_unlock(&s->data_mutex); 519 } 520 521 static const char *init_hugepagefs(void) 522 { 523 #ifdef CONFIG_LINUX 524 static const char *hugepagefs; 525 const char *path = getenv("QTEST_HUGETLBFS_PATH"); 526 struct statfs fs; 527 int ret; 528 529 if (hugepagefs) { 530 return hugepagefs; 531 } 532 if (!path) { 533 return NULL; 534 } 535 536 if (access(path, R_OK | W_OK | X_OK)) { 537 qos_printf("access on path (%s): %s", path, strerror(errno)); 538 g_test_fail(); 539 return NULL; 540 } 541 542 do { 543 ret = statfs(path, &fs); 544 } while (ret != 0 && errno == EINTR); 545 546 if (ret != 0) { 547 qos_printf("statfs on path (%s): %s", path, strerror(errno)); 548 g_test_fail(); 549 return NULL; 550 } 551 552 if (fs.f_type != HUGETLBFS_MAGIC) { 553 qos_printf("Warning: path not on HugeTLBFS: %s", path); 554 g_test_fail(); 555 return NULL; 556 } 557 558 hugepagefs = path; 559 return hugepagefs; 560 #else 561 return NULL; 562 #endif 563 } 564 565 static TestServer *test_server_new(const gchar *name, 566 struct vhost_user_ops *ops) 567 { 568 TestServer *server = g_new0(TestServer, 1); 569 g_autofree const char *tmpfs = NULL; 570 GError *err = NULL; 571 572 server->context = g_main_context_new(); 573 server->loop = g_main_loop_new(server->context, FALSE); 574 575 /* run the main loop thread so the chardev may operate */ 576 server->thread = g_thread_new(NULL, thread_function, server->loop); 577 578 tmpfs = g_dir_make_tmp("vhost-test-XXXXXX", &err); 579 if (!tmpfs) { 580 g_test_message("Can't create temporary directory in %s: %s", 581 g_get_tmp_dir(), err->message); 582 g_error_free(err); 583 } 584 g_assert(tmpfs); 585 586 server->tmpfs = g_strdup(tmpfs); 587 server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name); 588 server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name); 589 server->chr_name = g_strdup_printf("chr-%s", name); 590 591 g_mutex_init(&server->data_mutex); 592 g_cond_init(&server->data_cond); 593 594 server->log_fd = -1; 595 server->queues = 1; 596 server->vu_ops = ops; 597 598 return server; 599 } 600 601 static void chr_event(void *opaque, QEMUChrEvent event) 602 { 603 TestServer *s = opaque; 604 605 if (s->test_flags == TEST_FLAGS_END && 606 event == CHR_EVENT_CLOSED) { 607 s->test_flags = TEST_FLAGS_OK; 608 } 609 } 610 611 static void test_server_create_chr(TestServer *server, const gchar *opt) 612 { 613 g_autofree gchar *chr_path = g_strdup_printf("unix:%s%s", 614 server->socket_path, opt); 615 Chardev *chr; 616 617 chr = qemu_chr_new(server->chr_name, chr_path, server->context); 618 g_assert(chr); 619 620 qemu_chr_fe_init(&server->chr, chr, &error_abort); 621 qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read, 622 chr_event, NULL, server, server->context, true); 623 } 624 625 static void test_server_listen(TestServer *server) 626 { 627 test_server_create_chr(server, ",server=on,wait=off"); 628 } 629 630 static void test_server_free(TestServer *server) 631 { 632 int i, ret; 633 634 /* finish the helper thread and dispatch pending sources */ 635 g_main_loop_quit(server->loop); 636 g_thread_join(server->thread); 637 while (g_main_context_pending(NULL)) { 638 g_main_context_iteration(NULL, TRUE); 639 } 640 641 unlink(server->socket_path); 642 g_free(server->socket_path); 643 644 unlink(server->mig_path); 645 g_free(server->mig_path); 646 647 ret = rmdir(server->tmpfs); 648 if (ret != 0) { 649 g_test_message("unable to rmdir: path (%s): %s", 650 server->tmpfs, strerror(errno)); 651 } 652 g_free(server->tmpfs); 653 654 qemu_chr_fe_deinit(&server->chr, true); 655 656 for (i = 0; i < server->fds_num; i++) { 657 close(server->fds[i]); 658 } 659 660 if (server->log_fd != -1) { 661 close(server->log_fd); 662 } 663 664 g_free(server->chr_name); 665 666 g_main_loop_unref(server->loop); 667 g_main_context_unref(server->context); 668 g_cond_clear(&server->data_cond); 669 g_mutex_clear(&server->data_mutex); 670 g_free(server); 671 } 672 673 static void wait_for_log_fd(TestServer *s) 674 { 675 gint64 end_time; 676 677 g_mutex_lock(&s->data_mutex); 678 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 679 while (s->log_fd == -1) { 680 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { 681 /* timeout has passed */ 682 g_assert(s->log_fd != -1); 683 break; 684 } 685 } 686 687 g_mutex_unlock(&s->data_mutex); 688 } 689 690 static void write_guest_mem(TestServer *s, uint32_t seed) 691 { 692 uint32_t *guest_mem; 693 int i, j; 694 size_t size; 695 696 /* iterate all regions */ 697 for (i = 0; i < s->fds_num; i++) { 698 699 /* We'll write only the region statring at 0x0 */ 700 if (s->memory.regions[i].guest_phys_addr != 0x0) { 701 continue; 702 } 703 704 g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024); 705 706 size = s->memory.regions[i].memory_size + 707 s->memory.regions[i].mmap_offset; 708 709 guest_mem = mmap(0, size, PROT_READ | PROT_WRITE, 710 MAP_SHARED, s->fds[i], 0); 711 712 g_assert(guest_mem != MAP_FAILED); 713 guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem)); 714 715 for (j = 0; j < 256; j++) { 716 guest_mem[j] = seed + j; 717 } 718 719 munmap(guest_mem, s->memory.regions[i].memory_size); 720 break; 721 } 722 } 723 724 static guint64 get_log_size(TestServer *s) 725 { 726 guint64 log_size = 0; 727 int i; 728 729 for (i = 0; i < s->memory.nregions; ++i) { 730 VhostUserMemoryRegion *reg = &s->memory.regions[i]; 731 guint64 last = range_get_last(reg->guest_phys_addr, 732 reg->memory_size); 733 log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1); 734 } 735 736 return log_size; 737 } 738 739 typedef struct TestMigrateSource { 740 GSource source; 741 TestServer *src; 742 TestServer *dest; 743 } TestMigrateSource; 744 745 static gboolean 746 test_migrate_source_check(GSource *source) 747 { 748 TestMigrateSource *t = (TestMigrateSource *)source; 749 gboolean overlap = t->src->rings && t->dest->rings; 750 751 g_assert(!overlap); 752 753 return FALSE; 754 } 755 756 GSourceFuncs test_migrate_source_funcs = { 757 .check = test_migrate_source_check, 758 }; 759 760 static void vhost_user_test_cleanup(void *s) 761 { 762 TestServer *server = s; 763 764 qos_invalidate_command_line(); 765 test_server_free(server); 766 } 767 768 static void *vhost_user_test_setup(GString *cmd_line, void *arg) 769 { 770 TestServer *server = test_server_new("vhost-user-test", arg); 771 test_server_listen(server); 772 773 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_AUTO); 774 server->vu_ops->append_opts(server, cmd_line, ""); 775 776 g_test_queue_destroy(vhost_user_test_cleanup, server); 777 778 return server; 779 } 780 781 static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg) 782 { 783 TestServer *server = test_server_new("vhost-user-test", arg); 784 test_server_listen(server); 785 786 append_mem_opts(server, cmd_line, 256, TEST_MEMFD_YES); 787 server->vu_ops->append_opts(server, cmd_line, ""); 788 789 g_test_queue_destroy(vhost_user_test_cleanup, server); 790 791 return server; 792 } 793 794 static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc) 795 { 796 TestServer *server = arg; 797 798 if (!wait_for_fds(server)) { 799 return; 800 } 801 802 read_guest_mem_server(global_qtest, server); 803 } 804 805 static void test_migrate(void *obj, void *arg, QGuestAllocator *alloc) 806 { 807 TestServer *s = arg; 808 TestServer *dest; 809 GString *dest_cmdline; 810 char *uri; 811 QTestState *to; 812 GSource *source; 813 QDict *rsp; 814 guint8 *log; 815 guint64 size; 816 817 if (!wait_for_fds(s)) { 818 return; 819 } 820 821 dest = test_server_new("dest", s->vu_ops); 822 dest_cmdline = g_string_new(qos_get_current_command_line()); 823 uri = g_strdup_printf("%s%s", "unix:", dest->mig_path); 824 825 size = get_log_size(s); 826 g_assert_cmpint(size, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE * 8)); 827 828 test_server_listen(dest); 829 g_string_append_printf(dest_cmdline, " -incoming %s", uri); 830 append_mem_opts(dest, dest_cmdline, 256, TEST_MEMFD_AUTO); 831 dest->vu_ops->append_opts(dest, dest_cmdline, ""); 832 to = qtest_init(dest_cmdline->str); 833 834 /* This would be where you call qos_allocate_objects(to, NULL), if you want 835 * to talk to the QVirtioNet object on the destination. 836 */ 837 838 source = g_source_new(&test_migrate_source_funcs, 839 sizeof(TestMigrateSource)); 840 ((TestMigrateSource *)source)->src = s; 841 ((TestMigrateSource *)source)->dest = dest; 842 g_source_attach(source, s->context); 843 844 /* slow down migration to have time to fiddle with log */ 845 /* TODO: qtest could learn to break on some places */ 846 rsp = qmp("{ 'execute': 'migrate-set-parameters'," 847 "'arguments': { 'max-bandwidth': 10 } }"); 848 g_assert(qdict_haskey(rsp, "return")); 849 qobject_unref(rsp); 850 851 rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri); 852 g_assert(qdict_haskey(rsp, "return")); 853 qobject_unref(rsp); 854 855 wait_for_log_fd(s); 856 857 log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0); 858 g_assert(log != MAP_FAILED); 859 860 /* modify first page */ 861 write_guest_mem(s, 0x42); 862 log[0] = 1; 863 munmap(log, size); 864 865 /* speed things up */ 866 rsp = qmp("{ 'execute': 'migrate-set-parameters'," 867 "'arguments': { 'max-bandwidth': 0 } }"); 868 g_assert(qdict_haskey(rsp, "return")); 869 qobject_unref(rsp); 870 871 qmp_eventwait("STOP"); 872 qtest_qmp_eventwait(to, "RESUME"); 873 874 g_assert(wait_for_fds(dest)); 875 read_guest_mem_server(to, dest); 876 877 g_source_destroy(source); 878 g_source_unref(source); 879 880 qtest_quit(to); 881 test_server_free(dest); 882 g_free(uri); 883 g_string_free(dest_cmdline, true); 884 } 885 886 static void wait_for_rings_started(TestServer *s, size_t count) 887 { 888 gint64 end_time; 889 890 g_mutex_lock(&s->data_mutex); 891 end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND; 892 while (ctpop64(s->rings) != count) { 893 if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) { 894 /* timeout has passed */ 895 g_assert_cmpint(ctpop64(s->rings), ==, count); 896 break; 897 } 898 } 899 900 g_mutex_unlock(&s->data_mutex); 901 } 902 903 static inline void test_server_connect(TestServer *server) 904 { 905 test_server_create_chr(server, ",reconnect=1"); 906 } 907 908 static gboolean 909 reconnect_cb(gpointer user_data) 910 { 911 TestServer *s = user_data; 912 913 qemu_chr_fe_disconnect(&s->chr); 914 915 return FALSE; 916 } 917 918 static gpointer 919 connect_thread(gpointer data) 920 { 921 TestServer *s = data; 922 923 /* wait for qemu to start before first try, to avoid extra warnings */ 924 g_usleep(G_USEC_PER_SEC); 925 test_server_connect(s); 926 927 return NULL; 928 } 929 930 static void *vhost_user_test_setup_reconnect(GString *cmd_line, void *arg) 931 { 932 TestServer *s = test_server_new("reconnect", arg); 933 934 g_thread_new("connect", connect_thread, s); 935 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO); 936 s->vu_ops->append_opts(s, cmd_line, ",server=on"); 937 938 g_test_queue_destroy(vhost_user_test_cleanup, s); 939 940 return s; 941 } 942 943 static void test_reconnect(void *obj, void *arg, QGuestAllocator *alloc) 944 { 945 TestServer *s = arg; 946 GSource *src; 947 948 if (!wait_for_fds(s)) { 949 return; 950 } 951 952 wait_for_rings_started(s, 2); 953 954 /* reconnect */ 955 s->fds_num = 0; 956 s->rings = 0; 957 src = g_idle_source_new(); 958 g_source_set_callback(src, reconnect_cb, s, NULL); 959 g_source_attach(src, s->context); 960 g_source_unref(src); 961 g_assert(wait_for_fds(s)); 962 wait_for_rings_started(s, 2); 963 } 964 965 static void *vhost_user_test_setup_connect_fail(GString *cmd_line, void *arg) 966 { 967 TestServer *s = test_server_new("connect-fail", arg); 968 969 s->test_fail = true; 970 971 g_thread_new("connect", connect_thread, s); 972 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO); 973 s->vu_ops->append_opts(s, cmd_line, ",server=on"); 974 975 g_test_queue_destroy(vhost_user_test_cleanup, s); 976 977 return s; 978 } 979 980 static void *vhost_user_test_setup_flags_mismatch(GString *cmd_line, void *arg) 981 { 982 TestServer *s = test_server_new("flags-mismatch", arg); 983 984 s->test_flags = TEST_FLAGS_DISCONNECT; 985 986 g_thread_new("connect", connect_thread, s); 987 append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO); 988 s->vu_ops->append_opts(s, cmd_line, ",server=on"); 989 990 g_test_queue_destroy(vhost_user_test_cleanup, s); 991 992 return s; 993 } 994 995 static void test_vhost_user_started(void *obj, void *arg, QGuestAllocator *alloc) 996 { 997 TestServer *s = arg; 998 999 if (!wait_for_fds(s)) { 1000 return; 1001 } 1002 wait_for_rings_started(s, 2); 1003 } 1004 1005 static void *vhost_user_test_setup_multiqueue(GString *cmd_line, void *arg) 1006 { 1007 TestServer *s = vhost_user_test_setup(cmd_line, arg); 1008 1009 s->queues = 2; 1010 g_string_append_printf(cmd_line, 1011 " -set netdev.hs0.queues=%d" 1012 " -global virtio-net-pci.vectors=%d", 1013 s->queues, s->queues * 2 + 2); 1014 1015 return s; 1016 } 1017 1018 static void test_multiqueue(void *obj, void *arg, QGuestAllocator *alloc) 1019 { 1020 TestServer *s = arg; 1021 1022 wait_for_rings_started(s, s->queues * 2); 1023 } 1024 1025 1026 static uint64_t vu_net_get_features(TestServer *s) 1027 { 1028 uint64_t features = 0x1ULL << VHOST_F_LOG_ALL | 1029 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; 1030 1031 if (s->queues > 1) { 1032 features |= 0x1ULL << VIRTIO_NET_F_MQ; 1033 } 1034 1035 return features; 1036 } 1037 1038 static void vu_net_set_features(TestServer *s, CharBackend *chr, 1039 VhostUserMsg *msg) 1040 { 1041 g_assert(msg->payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES)); 1042 if (s->test_flags == TEST_FLAGS_DISCONNECT) { 1043 qemu_chr_fe_disconnect(chr); 1044 s->test_flags = TEST_FLAGS_BAD; 1045 } 1046 } 1047 1048 static void vu_net_get_protocol_features(TestServer *s, CharBackend *chr, 1049 VhostUserMsg *msg) 1050 { 1051 /* send back features to qemu */ 1052 msg->flags |= VHOST_USER_REPLY_MASK; 1053 msg->size = sizeof(m.payload.u64); 1054 msg->payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD; 1055 msg->payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN; 1056 if (s->queues > 1) { 1057 msg->payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ; 1058 } 1059 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size); 1060 } 1061 1062 /* Each VHOST-USER device should have its ops structure defined. */ 1063 static struct vhost_user_ops g_vu_net_ops = { 1064 .type = VHOST_USER_NET, 1065 1066 .append_opts = append_vhost_net_opts, 1067 1068 .get_features = vu_net_get_features, 1069 .set_features = vu_net_set_features, 1070 .get_protocol_features = vu_net_get_protocol_features, 1071 }; 1072 1073 static void register_vhost_user_test(void) 1074 { 1075 QOSGraphTestOptions opts = { 1076 .before = vhost_user_test_setup, 1077 .subprocess = true, 1078 .arg = &g_vu_net_ops, 1079 }; 1080 1081 qemu_add_opts(&qemu_chardev_opts); 1082 1083 qos_add_test("vhost-user/read-guest-mem/memfile", 1084 "virtio-net", 1085 test_read_guest_mem, &opts); 1086 1087 if (qemu_memfd_check(MFD_ALLOW_SEALING)) { 1088 opts.before = vhost_user_test_setup_memfd; 1089 qos_add_test("vhost-user/read-guest-mem/memfd", 1090 "virtio-net", 1091 test_read_guest_mem, &opts); 1092 } 1093 1094 qos_add_test("vhost-user/migrate", 1095 "virtio-net", 1096 test_migrate, &opts); 1097 1098 opts.before = vhost_user_test_setup_reconnect; 1099 qos_add_test("vhost-user/reconnect", "virtio-net", 1100 test_reconnect, &opts); 1101 1102 opts.before = vhost_user_test_setup_connect_fail; 1103 qos_add_test("vhost-user/connect-fail", "virtio-net", 1104 test_vhost_user_started, &opts); 1105 1106 opts.before = vhost_user_test_setup_flags_mismatch; 1107 qos_add_test("vhost-user/flags-mismatch", "virtio-net", 1108 test_vhost_user_started, &opts); 1109 1110 opts.before = vhost_user_test_setup_multiqueue; 1111 opts.edge.extra_device_opts = "mq=on"; 1112 qos_add_test("vhost-user/multiqueue", 1113 "virtio-net", 1114 test_multiqueue, &opts); 1115 } 1116 libqos_init(register_vhost_user_test); 1117 1118 static uint64_t vu_gpio_get_features(TestServer *s) 1119 { 1120 return 0x1ULL << VIRTIO_F_VERSION_1 | 1121 0x1ULL << VIRTIO_GPIO_F_IRQ | 1122 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; 1123 } 1124 1125 /* 1126 * This stub can't handle all the message types but we should reply 1127 * that we support VHOST_USER_PROTOCOL_F_CONFIG as gpio would use it 1128 * talking to a read vhost-user daemon. 1129 */ 1130 static void vu_gpio_get_protocol_features(TestServer *s, CharBackend *chr, 1131 VhostUserMsg *msg) 1132 { 1133 /* send back features to qemu */ 1134 msg->flags |= VHOST_USER_REPLY_MASK; 1135 msg->size = sizeof(m.payload.u64); 1136 msg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_CONFIG; 1137 1138 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size); 1139 } 1140 1141 static struct vhost_user_ops g_vu_gpio_ops = { 1142 .type = VHOST_USER_GPIO, 1143 1144 .append_opts = append_vhost_gpio_opts, 1145 1146 .get_features = vu_gpio_get_features, 1147 .set_features = vu_net_set_features, 1148 .get_protocol_features = vu_gpio_get_protocol_features, 1149 }; 1150 1151 static void register_vhost_gpio_test(void) 1152 { 1153 QOSGraphTestOptions opts = { 1154 .before = vhost_user_test_setup, 1155 .subprocess = true, 1156 .arg = &g_vu_gpio_ops, 1157 }; 1158 1159 qemu_add_opts(&qemu_chardev_opts); 1160 1161 qos_add_test("read-guest-mem/memfile", 1162 "vhost-user-gpio", test_read_guest_mem, &opts); 1163 } 1164 libqos_init(register_vhost_gpio_test); 1165 1166 static uint64_t vu_scmi_get_features(TestServer *s) 1167 { 1168 return 0x1ULL << VIRTIO_F_VERSION_1 | 1169 0x1ULL << VIRTIO_SCMI_F_P2A_CHANNELS | 1170 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES; 1171 } 1172 1173 static void vu_scmi_get_protocol_features(TestServer *s, CharBackend *chr, 1174 VhostUserMsg *msg) 1175 { 1176 msg->flags |= VHOST_USER_REPLY_MASK; 1177 msg->size = sizeof(m.payload.u64); 1178 msg->payload.u64 = 1ULL << VHOST_USER_PROTOCOL_F_MQ; 1179 1180 qemu_chr_fe_write_all(chr, (uint8_t *)msg, VHOST_USER_HDR_SIZE + msg->size); 1181 } 1182 1183 static struct vhost_user_ops g_vu_scmi_ops = { 1184 .type = VHOST_USER_SCMI, 1185 1186 .append_opts = append_vhost_gpio_opts, 1187 1188 .get_features = vu_scmi_get_features, 1189 .set_features = vu_net_set_features, 1190 .get_protocol_features = vu_scmi_get_protocol_features, 1191 }; 1192 1193 static void register_vhost_scmi_test(void) 1194 { 1195 QOSGraphTestOptions opts = { 1196 .before = vhost_user_test_setup, 1197 .subprocess = true, 1198 .arg = &g_vu_scmi_ops, 1199 }; 1200 1201 qemu_add_opts(&qemu_chardev_opts); 1202 1203 qos_add_test("scmi/read-guest-mem/memfile", 1204 "vhost-user-scmi", test_read_guest_mem, &opts); 1205 } 1206 libqos_init(register_vhost_scmi_test); 1207