2
0

vhost-user-test.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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. #include "qemu/osdep.h"
  11. #include "libqtest-single.h"
  12. #include "qapi/error.h"
  13. #include "qapi/qmp/qdict.h"
  14. #include "qemu/config-file.h"
  15. #include "qemu/option.h"
  16. #include "qemu/range.h"
  17. #include "qemu/sockets.h"
  18. #include "chardev/char-fe.h"
  19. #include "qemu/memfd.h"
  20. #include "qemu/module.h"
  21. #include "sysemu/sysemu.h"
  22. #include "libqos/libqos.h"
  23. #include "libqos/pci-pc.h"
  24. #include "libqos/virtio-pci.h"
  25. #include "libqos/malloc-pc.h"
  26. #include "hw/virtio/virtio-net.h"
  27. #include "standard-headers/linux/vhost_types.h"
  28. #include "standard-headers/linux/virtio_ids.h"
  29. #include "standard-headers/linux/virtio_net.h"
  30. #ifdef CONFIG_LINUX
  31. #include <sys/vfs.h>
  32. #endif
  33. #define QEMU_CMD_MEM " -m %d -object memory-backend-file,id=mem,size=%dM," \
  34. "mem-path=%s,share=on -numa node,memdev=mem"
  35. #define QEMU_CMD_MEMFD " -m %d -object memory-backend-memfd,id=mem,size=%dM," \
  36. " -numa node,memdev=mem"
  37. #define QEMU_CMD_CHR " -chardev socket,id=%s,path=%s%s"
  38. #define QEMU_CMD_NETDEV " -netdev vhost-user,id=hs0,chardev=%s,vhostforce"
  39. #define HUGETLBFS_MAGIC 0x958458f6
  40. /*********** FROM hw/virtio/vhost-user.c *************************************/
  41. #define VHOST_MEMORY_MAX_NREGIONS 8
  42. #define VHOST_MAX_VIRTQUEUES 0x100
  43. #define VHOST_USER_F_PROTOCOL_FEATURES 30
  44. #define VHOST_USER_PROTOCOL_F_MQ 0
  45. #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
  46. #define VHOST_USER_PROTOCOL_F_CROSS_ENDIAN 6
  47. #define VHOST_LOG_PAGE 0x1000
  48. typedef enum VhostUserRequest {
  49. VHOST_USER_NONE = 0,
  50. VHOST_USER_GET_FEATURES = 1,
  51. VHOST_USER_SET_FEATURES = 2,
  52. VHOST_USER_SET_OWNER = 3,
  53. VHOST_USER_RESET_OWNER = 4,
  54. VHOST_USER_SET_MEM_TABLE = 5,
  55. VHOST_USER_SET_LOG_BASE = 6,
  56. VHOST_USER_SET_LOG_FD = 7,
  57. VHOST_USER_SET_VRING_NUM = 8,
  58. VHOST_USER_SET_VRING_ADDR = 9,
  59. VHOST_USER_SET_VRING_BASE = 10,
  60. VHOST_USER_GET_VRING_BASE = 11,
  61. VHOST_USER_SET_VRING_KICK = 12,
  62. VHOST_USER_SET_VRING_CALL = 13,
  63. VHOST_USER_SET_VRING_ERR = 14,
  64. VHOST_USER_GET_PROTOCOL_FEATURES = 15,
  65. VHOST_USER_SET_PROTOCOL_FEATURES = 16,
  66. VHOST_USER_GET_QUEUE_NUM = 17,
  67. VHOST_USER_SET_VRING_ENABLE = 18,
  68. VHOST_USER_MAX
  69. } VhostUserRequest;
  70. typedef struct VhostUserMemoryRegion {
  71. uint64_t guest_phys_addr;
  72. uint64_t memory_size;
  73. uint64_t userspace_addr;
  74. uint64_t mmap_offset;
  75. } VhostUserMemoryRegion;
  76. typedef struct VhostUserMemory {
  77. uint32_t nregions;
  78. uint32_t padding;
  79. VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
  80. } VhostUserMemory;
  81. typedef struct VhostUserLog {
  82. uint64_t mmap_size;
  83. uint64_t mmap_offset;
  84. } VhostUserLog;
  85. typedef struct VhostUserMsg {
  86. VhostUserRequest request;
  87. #define VHOST_USER_VERSION_MASK (0x3)
  88. #define VHOST_USER_REPLY_MASK (0x1<<2)
  89. uint32_t flags;
  90. uint32_t size; /* the following payload size */
  91. union {
  92. #define VHOST_USER_VRING_IDX_MASK (0xff)
  93. #define VHOST_USER_VRING_NOFD_MASK (0x1<<8)
  94. uint64_t u64;
  95. struct vhost_vring_state state;
  96. struct vhost_vring_addr addr;
  97. VhostUserMemory memory;
  98. VhostUserLog log;
  99. } payload;
  100. } QEMU_PACKED VhostUserMsg;
  101. static VhostUserMsg m __attribute__ ((unused));
  102. #define VHOST_USER_HDR_SIZE (sizeof(m.request) \
  103. + sizeof(m.flags) \
  104. + sizeof(m.size))
  105. #define VHOST_USER_PAYLOAD_SIZE (sizeof(m) - VHOST_USER_HDR_SIZE)
  106. /* The version of the protocol we support */
  107. #define VHOST_USER_VERSION (0x1)
  108. /*****************************************************************************/
  109. enum {
  110. TEST_FLAGS_OK,
  111. TEST_FLAGS_DISCONNECT,
  112. TEST_FLAGS_BAD,
  113. TEST_FLAGS_END,
  114. };
  115. typedef struct TestServer {
  116. gchar *socket_path;
  117. gchar *mig_path;
  118. gchar *chr_name;
  119. gchar *tmpfs;
  120. CharBackend chr;
  121. int fds_num;
  122. int fds[VHOST_MEMORY_MAX_NREGIONS];
  123. VhostUserMemory memory;
  124. GMainContext *context;
  125. GMainLoop *loop;
  126. GThread *thread;
  127. GMutex data_mutex;
  128. GCond data_cond;
  129. int log_fd;
  130. uint64_t rings;
  131. bool test_fail;
  132. int test_flags;
  133. int queues;
  134. } TestServer;
  135. static const char *init_hugepagefs(void);
  136. static TestServer *test_server_new(const gchar *name);
  137. static void test_server_free(TestServer *server);
  138. static void test_server_listen(TestServer *server);
  139. enum test_memfd {
  140. TEST_MEMFD_AUTO,
  141. TEST_MEMFD_YES,
  142. TEST_MEMFD_NO,
  143. };
  144. static void append_vhost_opts(TestServer *s, GString *cmd_line,
  145. const char *chr_opts)
  146. {
  147. g_string_append_printf(cmd_line, QEMU_CMD_CHR QEMU_CMD_NETDEV,
  148. s->chr_name, s->socket_path,
  149. chr_opts, s->chr_name);
  150. }
  151. static void append_mem_opts(TestServer *server, GString *cmd_line,
  152. int size, enum test_memfd memfd)
  153. {
  154. if (memfd == TEST_MEMFD_AUTO) {
  155. memfd = qemu_memfd_check(MFD_ALLOW_SEALING) ? TEST_MEMFD_YES
  156. : TEST_MEMFD_NO;
  157. }
  158. if (memfd == TEST_MEMFD_YES) {
  159. g_string_append_printf(cmd_line, QEMU_CMD_MEMFD, size, size);
  160. } else {
  161. const char *root = init_hugepagefs() ? : server->tmpfs;
  162. g_string_append_printf(cmd_line, QEMU_CMD_MEM, size, size, root);
  163. }
  164. }
  165. static bool wait_for_fds(TestServer *s)
  166. {
  167. gint64 end_time;
  168. bool got_region;
  169. int i;
  170. g_mutex_lock(&s->data_mutex);
  171. end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
  172. while (!s->fds_num) {
  173. if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
  174. /* timeout has passed */
  175. g_assert(s->fds_num);
  176. break;
  177. }
  178. }
  179. /* check for sanity */
  180. g_assert_cmpint(s->fds_num, >, 0);
  181. g_assert_cmpint(s->fds_num, ==, s->memory.nregions);
  182. g_mutex_unlock(&s->data_mutex);
  183. got_region = false;
  184. for (i = 0; i < s->memory.nregions; ++i) {
  185. VhostUserMemoryRegion *reg = &s->memory.regions[i];
  186. if (reg->guest_phys_addr == 0) {
  187. got_region = true;
  188. break;
  189. }
  190. }
  191. if (!got_region) {
  192. g_test_skip("No memory at address 0x0");
  193. }
  194. return got_region;
  195. }
  196. static void read_guest_mem_server(QTestState *qts, TestServer *s)
  197. {
  198. uint8_t *guest_mem;
  199. int i, j;
  200. size_t size;
  201. g_mutex_lock(&s->data_mutex);
  202. /* iterate all regions */
  203. for (i = 0; i < s->fds_num; i++) {
  204. /* We'll check only the region statring at 0x0*/
  205. if (s->memory.regions[i].guest_phys_addr != 0x0) {
  206. continue;
  207. }
  208. g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
  209. size = s->memory.regions[i].memory_size +
  210. s->memory.regions[i].mmap_offset;
  211. guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
  212. MAP_SHARED, s->fds[i], 0);
  213. g_assert(guest_mem != MAP_FAILED);
  214. guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
  215. for (j = 0; j < 1024; j++) {
  216. uint32_t a = qtest_readb(qts, s->memory.regions[i].guest_phys_addr + j);
  217. uint32_t b = guest_mem[j];
  218. g_assert_cmpint(a, ==, b);
  219. }
  220. munmap(guest_mem, s->memory.regions[i].memory_size);
  221. }
  222. g_mutex_unlock(&s->data_mutex);
  223. }
  224. static void *thread_function(void *data)
  225. {
  226. GMainLoop *loop = data;
  227. g_main_loop_run(loop);
  228. return NULL;
  229. }
  230. static int chr_can_read(void *opaque)
  231. {
  232. return VHOST_USER_HDR_SIZE;
  233. }
  234. static void chr_read(void *opaque, const uint8_t *buf, int size)
  235. {
  236. TestServer *s = opaque;
  237. CharBackend *chr = &s->chr;
  238. VhostUserMsg msg;
  239. uint8_t *p = (uint8_t *) &msg;
  240. int fd = -1;
  241. if (s->test_fail) {
  242. qemu_chr_fe_disconnect(chr);
  243. /* now switch to non-failure */
  244. s->test_fail = false;
  245. }
  246. if (size != VHOST_USER_HDR_SIZE) {
  247. g_test_message("Wrong message size received %d", size);
  248. return;
  249. }
  250. g_mutex_lock(&s->data_mutex);
  251. memcpy(p, buf, VHOST_USER_HDR_SIZE);
  252. if (msg.size) {
  253. p += VHOST_USER_HDR_SIZE;
  254. size = qemu_chr_fe_read_all(chr, p, msg.size);
  255. if (size != msg.size) {
  256. g_test_message("Wrong message size received %d != %d",
  257. size, msg.size);
  258. return;
  259. }
  260. }
  261. switch (msg.request) {
  262. case VHOST_USER_GET_FEATURES:
  263. /* send back features to qemu */
  264. msg.flags |= VHOST_USER_REPLY_MASK;
  265. msg.size = sizeof(m.payload.u64);
  266. msg.payload.u64 = 0x1ULL << VHOST_F_LOG_ALL |
  267. 0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
  268. if (s->queues > 1) {
  269. msg.payload.u64 |= 0x1ULL << VIRTIO_NET_F_MQ;
  270. }
  271. if (s->test_flags >= TEST_FLAGS_BAD) {
  272. msg.payload.u64 = 0;
  273. s->test_flags = TEST_FLAGS_END;
  274. }
  275. p = (uint8_t *) &msg;
  276. qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
  277. break;
  278. case VHOST_USER_SET_FEATURES:
  279. g_assert_cmpint(msg.payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES),
  280. !=, 0ULL);
  281. if (s->test_flags == TEST_FLAGS_DISCONNECT) {
  282. qemu_chr_fe_disconnect(chr);
  283. s->test_flags = TEST_FLAGS_BAD;
  284. }
  285. break;
  286. case VHOST_USER_GET_PROTOCOL_FEATURES:
  287. /* send back features to qemu */
  288. msg.flags |= VHOST_USER_REPLY_MASK;
  289. msg.size = sizeof(m.payload.u64);
  290. msg.payload.u64 = 1 << VHOST_USER_PROTOCOL_F_LOG_SHMFD;
  291. msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_CROSS_ENDIAN;
  292. if (s->queues > 1) {
  293. msg.payload.u64 |= 1 << VHOST_USER_PROTOCOL_F_MQ;
  294. }
  295. p = (uint8_t *) &msg;
  296. qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
  297. break;
  298. case VHOST_USER_GET_VRING_BASE:
  299. /* send back vring base to qemu */
  300. msg.flags |= VHOST_USER_REPLY_MASK;
  301. msg.size = sizeof(m.payload.state);
  302. msg.payload.state.num = 0;
  303. p = (uint8_t *) &msg;
  304. qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
  305. assert(msg.payload.state.index < s->queues * 2);
  306. s->rings &= ~(0x1ULL << msg.payload.state.index);
  307. g_cond_broadcast(&s->data_cond);
  308. break;
  309. case VHOST_USER_SET_MEM_TABLE:
  310. /* received the mem table */
  311. memcpy(&s->memory, &msg.payload.memory, sizeof(msg.payload.memory));
  312. s->fds_num = qemu_chr_fe_get_msgfds(chr, s->fds,
  313. G_N_ELEMENTS(s->fds));
  314. /* signal the test that it can continue */
  315. g_cond_broadcast(&s->data_cond);
  316. break;
  317. case VHOST_USER_SET_VRING_KICK:
  318. case VHOST_USER_SET_VRING_CALL:
  319. /* consume the fd */
  320. qemu_chr_fe_get_msgfds(chr, &fd, 1);
  321. /*
  322. * This is a non-blocking eventfd.
  323. * The receive function forces it to be blocking,
  324. * so revert it back to non-blocking.
  325. */
  326. qemu_set_nonblock(fd);
  327. break;
  328. case VHOST_USER_SET_LOG_BASE:
  329. if (s->log_fd != -1) {
  330. close(s->log_fd);
  331. s->log_fd = -1;
  332. }
  333. qemu_chr_fe_get_msgfds(chr, &s->log_fd, 1);
  334. msg.flags |= VHOST_USER_REPLY_MASK;
  335. msg.size = 0;
  336. p = (uint8_t *) &msg;
  337. qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE);
  338. g_cond_broadcast(&s->data_cond);
  339. break;
  340. case VHOST_USER_SET_VRING_BASE:
  341. assert(msg.payload.state.index < s->queues * 2);
  342. s->rings |= 0x1ULL << msg.payload.state.index;
  343. g_cond_broadcast(&s->data_cond);
  344. break;
  345. case VHOST_USER_GET_QUEUE_NUM:
  346. msg.flags |= VHOST_USER_REPLY_MASK;
  347. msg.size = sizeof(m.payload.u64);
  348. msg.payload.u64 = s->queues;
  349. p = (uint8_t *) &msg;
  350. qemu_chr_fe_write_all(chr, p, VHOST_USER_HDR_SIZE + msg.size);
  351. break;
  352. default:
  353. break;
  354. }
  355. g_mutex_unlock(&s->data_mutex);
  356. }
  357. static const char *init_hugepagefs(void)
  358. {
  359. #ifdef CONFIG_LINUX
  360. static const char *hugepagefs;
  361. const char *path = getenv("QTEST_HUGETLBFS_PATH");
  362. struct statfs fs;
  363. int ret;
  364. if (hugepagefs) {
  365. return hugepagefs;
  366. }
  367. if (!path) {
  368. return NULL;
  369. }
  370. if (access(path, R_OK | W_OK | X_OK)) {
  371. g_test_message("access on path (%s): %s", path, strerror(errno));
  372. g_test_fail();
  373. return NULL;
  374. }
  375. do {
  376. ret = statfs(path, &fs);
  377. } while (ret != 0 && errno == EINTR);
  378. if (ret != 0) {
  379. g_test_message("statfs on path (%s): %s", path, strerror(errno));
  380. g_test_fail();
  381. return NULL;
  382. }
  383. if (fs.f_type != HUGETLBFS_MAGIC) {
  384. g_test_message("Warning: path not on HugeTLBFS: %s", path);
  385. g_test_fail();
  386. return NULL;
  387. }
  388. hugepagefs = path;
  389. return hugepagefs;
  390. #else
  391. return NULL;
  392. #endif
  393. }
  394. static TestServer *test_server_new(const gchar *name)
  395. {
  396. TestServer *server = g_new0(TestServer, 1);
  397. char template[] = "/tmp/vhost-test-XXXXXX";
  398. const char *tmpfs;
  399. server->context = g_main_context_new();
  400. server->loop = g_main_loop_new(server->context, FALSE);
  401. /* run the main loop thread so the chardev may operate */
  402. server->thread = g_thread_new(NULL, thread_function, server->loop);
  403. tmpfs = mkdtemp(template);
  404. if (!tmpfs) {
  405. g_test_message("mkdtemp on path (%s): %s", template, strerror(errno));
  406. }
  407. g_assert(tmpfs);
  408. server->tmpfs = g_strdup(tmpfs);
  409. server->socket_path = g_strdup_printf("%s/%s.sock", tmpfs, name);
  410. server->mig_path = g_strdup_printf("%s/%s.mig", tmpfs, name);
  411. server->chr_name = g_strdup_printf("chr-%s", name);
  412. g_mutex_init(&server->data_mutex);
  413. g_cond_init(&server->data_cond);
  414. server->log_fd = -1;
  415. server->queues = 1;
  416. return server;
  417. }
  418. static void chr_event(void *opaque, int event)
  419. {
  420. TestServer *s = opaque;
  421. if (s->test_flags == TEST_FLAGS_END &&
  422. event == CHR_EVENT_CLOSED) {
  423. s->test_flags = TEST_FLAGS_OK;
  424. }
  425. }
  426. static void test_server_create_chr(TestServer *server, const gchar *opt)
  427. {
  428. gchar *chr_path;
  429. Chardev *chr;
  430. chr_path = g_strdup_printf("unix:%s%s", server->socket_path, opt);
  431. chr = qemu_chr_new(server->chr_name, chr_path, server->context);
  432. g_free(chr_path);
  433. g_assert_nonnull(chr);
  434. qemu_chr_fe_init(&server->chr, chr, &error_abort);
  435. qemu_chr_fe_set_handlers(&server->chr, chr_can_read, chr_read,
  436. chr_event, NULL, server, server->context, true);
  437. }
  438. static void test_server_listen(TestServer *server)
  439. {
  440. test_server_create_chr(server, ",server,nowait");
  441. }
  442. static void test_server_free(TestServer *server)
  443. {
  444. int i, ret;
  445. /* finish the helper thread and dispatch pending sources */
  446. g_main_loop_quit(server->loop);
  447. g_thread_join(server->thread);
  448. while (g_main_context_pending(NULL)) {
  449. g_main_context_iteration(NULL, TRUE);
  450. }
  451. unlink(server->socket_path);
  452. g_free(server->socket_path);
  453. unlink(server->mig_path);
  454. g_free(server->mig_path);
  455. ret = rmdir(server->tmpfs);
  456. if (ret != 0) {
  457. g_test_message("unable to rmdir: path (%s): %s",
  458. server->tmpfs, strerror(errno));
  459. }
  460. g_free(server->tmpfs);
  461. qemu_chr_fe_deinit(&server->chr, true);
  462. for (i = 0; i < server->fds_num; i++) {
  463. close(server->fds[i]);
  464. }
  465. if (server->log_fd != -1) {
  466. close(server->log_fd);
  467. }
  468. g_free(server->chr_name);
  469. g_main_loop_unref(server->loop);
  470. g_main_context_unref(server->context);
  471. g_cond_clear(&server->data_cond);
  472. g_mutex_clear(&server->data_mutex);
  473. g_free(server);
  474. }
  475. static void wait_for_log_fd(TestServer *s)
  476. {
  477. gint64 end_time;
  478. g_mutex_lock(&s->data_mutex);
  479. end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
  480. while (s->log_fd == -1) {
  481. if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
  482. /* timeout has passed */
  483. g_assert(s->log_fd != -1);
  484. break;
  485. }
  486. }
  487. g_mutex_unlock(&s->data_mutex);
  488. }
  489. static void write_guest_mem(TestServer *s, uint32_t seed)
  490. {
  491. uint32_t *guest_mem;
  492. int i, j;
  493. size_t size;
  494. /* iterate all regions */
  495. for (i = 0; i < s->fds_num; i++) {
  496. /* We'll write only the region statring at 0x0 */
  497. if (s->memory.regions[i].guest_phys_addr != 0x0) {
  498. continue;
  499. }
  500. g_assert_cmpint(s->memory.regions[i].memory_size, >, 1024);
  501. size = s->memory.regions[i].memory_size +
  502. s->memory.regions[i].mmap_offset;
  503. guest_mem = mmap(0, size, PROT_READ | PROT_WRITE,
  504. MAP_SHARED, s->fds[i], 0);
  505. g_assert(guest_mem != MAP_FAILED);
  506. guest_mem += (s->memory.regions[i].mmap_offset / sizeof(*guest_mem));
  507. for (j = 0; j < 256; j++) {
  508. guest_mem[j] = seed + j;
  509. }
  510. munmap(guest_mem, s->memory.regions[i].memory_size);
  511. break;
  512. }
  513. }
  514. static guint64 get_log_size(TestServer *s)
  515. {
  516. guint64 log_size = 0;
  517. int i;
  518. for (i = 0; i < s->memory.nregions; ++i) {
  519. VhostUserMemoryRegion *reg = &s->memory.regions[i];
  520. guint64 last = range_get_last(reg->guest_phys_addr,
  521. reg->memory_size);
  522. log_size = MAX(log_size, last / (8 * VHOST_LOG_PAGE) + 1);
  523. }
  524. return log_size;
  525. }
  526. typedef struct TestMigrateSource {
  527. GSource source;
  528. TestServer *src;
  529. TestServer *dest;
  530. } TestMigrateSource;
  531. static gboolean
  532. test_migrate_source_check(GSource *source)
  533. {
  534. TestMigrateSource *t = (TestMigrateSource *)source;
  535. gboolean overlap = t->src->rings && t->dest->rings;
  536. g_assert(!overlap);
  537. return FALSE;
  538. }
  539. GSourceFuncs test_migrate_source_funcs = {
  540. .check = test_migrate_source_check,
  541. };
  542. static void vhost_user_test_cleanup(void *s)
  543. {
  544. TestServer *server = s;
  545. qos_invalidate_command_line();
  546. test_server_free(server);
  547. }
  548. static void *vhost_user_test_setup(GString *cmd_line, void *arg)
  549. {
  550. TestServer *server = test_server_new("vhost-user-test");
  551. test_server_listen(server);
  552. append_mem_opts(server, cmd_line, 256, TEST_MEMFD_AUTO);
  553. append_vhost_opts(server, cmd_line, "");
  554. g_test_queue_destroy(vhost_user_test_cleanup, server);
  555. return server;
  556. }
  557. static void *vhost_user_test_setup_memfd(GString *cmd_line, void *arg)
  558. {
  559. TestServer *server = test_server_new("vhost-user-test");
  560. test_server_listen(server);
  561. append_mem_opts(server, cmd_line, 256, TEST_MEMFD_YES);
  562. append_vhost_opts(server, cmd_line, "");
  563. g_test_queue_destroy(vhost_user_test_cleanup, server);
  564. return server;
  565. }
  566. static void test_read_guest_mem(void *obj, void *arg, QGuestAllocator *alloc)
  567. {
  568. TestServer *server = arg;
  569. if (!wait_for_fds(server)) {
  570. return;
  571. }
  572. read_guest_mem_server(global_qtest, server);
  573. }
  574. static void test_migrate(void *obj, void *arg, QGuestAllocator *alloc)
  575. {
  576. TestServer *s = arg;
  577. TestServer *dest = test_server_new("dest");
  578. GString *dest_cmdline = g_string_new(qos_get_current_command_line());
  579. char *uri = g_strdup_printf("%s%s", "unix:", dest->mig_path);
  580. QTestState *to;
  581. GSource *source;
  582. QDict *rsp;
  583. guint8 *log;
  584. guint64 size;
  585. if (!wait_for_fds(s)) {
  586. return;
  587. }
  588. size = get_log_size(s);
  589. g_assert_cmpint(size, ==, (256 * 1024 * 1024) / (VHOST_LOG_PAGE * 8));
  590. test_server_listen(dest);
  591. g_string_append_printf(dest_cmdline, " -incoming %s", uri);
  592. append_mem_opts(dest, dest_cmdline, 256, TEST_MEMFD_AUTO);
  593. append_vhost_opts(dest, dest_cmdline, "");
  594. to = qtest_init(dest_cmdline->str);
  595. /* This would be where you call qos_allocate_objects(to, NULL), if you want
  596. * to talk to the QVirtioNet object on the destination.
  597. */
  598. source = g_source_new(&test_migrate_source_funcs,
  599. sizeof(TestMigrateSource));
  600. ((TestMigrateSource *)source)->src = s;
  601. ((TestMigrateSource *)source)->dest = dest;
  602. g_source_attach(source, s->context);
  603. /* slow down migration to have time to fiddle with log */
  604. /* TODO: qtest could learn to break on some places */
  605. rsp = qmp("{ 'execute': 'migrate_set_speed',"
  606. "'arguments': { 'value': 10 } }");
  607. g_assert(qdict_haskey(rsp, "return"));
  608. qobject_unref(rsp);
  609. rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
  610. g_assert(qdict_haskey(rsp, "return"));
  611. qobject_unref(rsp);
  612. wait_for_log_fd(s);
  613. log = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, s->log_fd, 0);
  614. g_assert(log != MAP_FAILED);
  615. /* modify first page */
  616. write_guest_mem(s, 0x42);
  617. log[0] = 1;
  618. munmap(log, size);
  619. /* speed things up */
  620. rsp = qmp("{ 'execute': 'migrate_set_speed',"
  621. "'arguments': { 'value': 0 } }");
  622. g_assert(qdict_haskey(rsp, "return"));
  623. qobject_unref(rsp);
  624. qmp_eventwait("STOP");
  625. qtest_qmp_eventwait(to, "RESUME");
  626. g_assert(wait_for_fds(dest));
  627. read_guest_mem_server(to, dest);
  628. g_source_destroy(source);
  629. g_source_unref(source);
  630. qtest_quit(to);
  631. test_server_free(dest);
  632. g_free(uri);
  633. }
  634. static void wait_for_rings_started(TestServer *s, size_t count)
  635. {
  636. gint64 end_time;
  637. g_mutex_lock(&s->data_mutex);
  638. end_time = g_get_monotonic_time() + 5 * G_TIME_SPAN_SECOND;
  639. while (ctpop64(s->rings) != count) {
  640. if (!g_cond_wait_until(&s->data_cond, &s->data_mutex, end_time)) {
  641. /* timeout has passed */
  642. g_assert_cmpint(ctpop64(s->rings), ==, count);
  643. break;
  644. }
  645. }
  646. g_mutex_unlock(&s->data_mutex);
  647. }
  648. static inline void test_server_connect(TestServer *server)
  649. {
  650. test_server_create_chr(server, ",reconnect=1");
  651. }
  652. static gboolean
  653. reconnect_cb(gpointer user_data)
  654. {
  655. TestServer *s = user_data;
  656. qemu_chr_fe_disconnect(&s->chr);
  657. return FALSE;
  658. }
  659. static gpointer
  660. connect_thread(gpointer data)
  661. {
  662. TestServer *s = data;
  663. /* wait for qemu to start before first try, to avoid extra warnings */
  664. g_usleep(G_USEC_PER_SEC);
  665. test_server_connect(s);
  666. return NULL;
  667. }
  668. static void *vhost_user_test_setup_reconnect(GString *cmd_line, void *arg)
  669. {
  670. TestServer *s = test_server_new("reconnect");
  671. g_thread_new("connect", connect_thread, s);
  672. append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
  673. append_vhost_opts(s, cmd_line, ",server");
  674. g_test_queue_destroy(vhost_user_test_cleanup, s);
  675. return s;
  676. }
  677. static void test_reconnect(void *obj, void *arg, QGuestAllocator *alloc)
  678. {
  679. TestServer *s = arg;
  680. GSource *src;
  681. if (!wait_for_fds(s)) {
  682. return;
  683. }
  684. wait_for_rings_started(s, 2);
  685. /* reconnect */
  686. s->fds_num = 0;
  687. s->rings = 0;
  688. src = g_idle_source_new();
  689. g_source_set_callback(src, reconnect_cb, s, NULL);
  690. g_source_attach(src, s->context);
  691. g_source_unref(src);
  692. g_assert(wait_for_fds(s));
  693. wait_for_rings_started(s, 2);
  694. }
  695. static void *vhost_user_test_setup_connect_fail(GString *cmd_line, void *arg)
  696. {
  697. TestServer *s = test_server_new("connect-fail");
  698. s->test_fail = true;
  699. g_thread_new("connect", connect_thread, s);
  700. append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
  701. append_vhost_opts(s, cmd_line, ",server");
  702. g_test_queue_destroy(vhost_user_test_cleanup, s);
  703. return s;
  704. }
  705. static void *vhost_user_test_setup_flags_mismatch(GString *cmd_line, void *arg)
  706. {
  707. TestServer *s = test_server_new("flags-mismatch");
  708. s->test_flags = TEST_FLAGS_DISCONNECT;
  709. g_thread_new("connect", connect_thread, s);
  710. append_mem_opts(s, cmd_line, 256, TEST_MEMFD_AUTO);
  711. append_vhost_opts(s, cmd_line, ",server");
  712. g_test_queue_destroy(vhost_user_test_cleanup, s);
  713. return s;
  714. }
  715. static void test_vhost_user_started(void *obj, void *arg, QGuestAllocator *alloc)
  716. {
  717. TestServer *s = arg;
  718. if (!wait_for_fds(s)) {
  719. return;
  720. }
  721. wait_for_rings_started(s, 2);
  722. }
  723. static void *vhost_user_test_setup_multiqueue(GString *cmd_line, void *arg)
  724. {
  725. TestServer *s = vhost_user_test_setup(cmd_line, arg);
  726. s->queues = 2;
  727. g_string_append_printf(cmd_line,
  728. " -set netdev.hs0.queues=%d"
  729. " -global virtio-net-pci.vectors=%d",
  730. s->queues, s->queues * 2 + 2);
  731. return s;
  732. }
  733. static void test_multiqueue(void *obj, void *arg, QGuestAllocator *alloc)
  734. {
  735. TestServer *s = arg;
  736. wait_for_rings_started(s, s->queues * 2);
  737. }
  738. static void register_vhost_user_test(void)
  739. {
  740. QOSGraphTestOptions opts = {
  741. .before = vhost_user_test_setup,
  742. .subprocess = true,
  743. };
  744. qemu_add_opts(&qemu_chardev_opts);
  745. qos_add_test("vhost-user/read-guest-mem/memfile",
  746. "virtio-net",
  747. test_read_guest_mem, &opts);
  748. if (qemu_memfd_check(MFD_ALLOW_SEALING)) {
  749. opts.before = vhost_user_test_setup_memfd;
  750. qos_add_test("vhost-user/read-guest-mem/memfd",
  751. "virtio-net",
  752. test_read_guest_mem, &opts);
  753. }
  754. qos_add_test("vhost-user/migrate",
  755. "virtio-net",
  756. test_migrate, &opts);
  757. /* keeps failing on build-system since Aug 15 2017 */
  758. if (getenv("QTEST_VHOST_USER_FIXME")) {
  759. opts.before = vhost_user_test_setup_reconnect;
  760. qos_add_test("vhost-user/reconnect", "virtio-net",
  761. test_reconnect, &opts);
  762. opts.before = vhost_user_test_setup_connect_fail;
  763. qos_add_test("vhost-user/connect-fail", "virtio-net",
  764. test_vhost_user_started, &opts);
  765. opts.before = vhost_user_test_setup_flags_mismatch;
  766. qos_add_test("vhost-user/flags-mismatch", "virtio-net",
  767. test_vhost_user_started, &opts);
  768. }
  769. opts.before = vhost_user_test_setup_multiqueue;
  770. opts.edge.extra_device_opts = "mq=on";
  771. qos_add_test("vhost-user/multiqueue",
  772. "virtio-net",
  773. test_multiqueue, &opts);
  774. }
  775. libqos_init(register_vhost_user_test);