virtio-net-test.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * QTest testcase for VirtIO NIC
  3. *
  4. * Copyright (c) 2014 SUSE LINUX Products GmbH
  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. #include "qemu/osdep.h"
  10. #include "qemu-common.h"
  11. #include "libqtest-single.h"
  12. #include "qemu/iov.h"
  13. #include "qemu/module.h"
  14. #include "qapi/qmp/qdict.h"
  15. #include "hw/virtio/virtio-net.h"
  16. #include "libqos/qgraph.h"
  17. #include "libqos/virtio-net.h"
  18. #ifndef ETH_P_RARP
  19. #define ETH_P_RARP 0x8035
  20. #endif
  21. #define PCI_SLOT_HP 0x06
  22. #define PCI_SLOT 0x04
  23. #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
  24. #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
  25. #ifndef _WIN32
  26. static void rx_test(QVirtioDevice *dev,
  27. QGuestAllocator *alloc, QVirtQueue *vq,
  28. int socket)
  29. {
  30. QTestState *qts = global_qtest;
  31. uint64_t req_addr;
  32. uint32_t free_head;
  33. char test[] = "TEST";
  34. char buffer[64];
  35. int len = htonl(sizeof(test));
  36. struct iovec iov[] = {
  37. {
  38. .iov_base = &len,
  39. .iov_len = sizeof(len),
  40. }, {
  41. .iov_base = test,
  42. .iov_len = sizeof(test),
  43. },
  44. };
  45. int ret;
  46. req_addr = guest_alloc(alloc, 64);
  47. free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
  48. qvirtqueue_kick(qts, dev, vq, free_head);
  49. ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
  50. g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
  51. qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
  52. QVIRTIO_NET_TIMEOUT_US);
  53. memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
  54. g_assert_cmpstr(buffer, ==, "TEST");
  55. guest_free(alloc, req_addr);
  56. }
  57. static void tx_test(QVirtioDevice *dev,
  58. QGuestAllocator *alloc, QVirtQueue *vq,
  59. int socket)
  60. {
  61. QTestState *qts = global_qtest;
  62. uint64_t req_addr;
  63. uint32_t free_head;
  64. uint32_t len;
  65. char buffer[64];
  66. int ret;
  67. req_addr = guest_alloc(alloc, 64);
  68. memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
  69. free_head = qvirtqueue_add(qts, vq, req_addr, 64, false, false);
  70. qvirtqueue_kick(qts, dev, vq, free_head);
  71. qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
  72. QVIRTIO_NET_TIMEOUT_US);
  73. guest_free(alloc, req_addr);
  74. ret = qemu_recv(socket, &len, sizeof(len), 0);
  75. g_assert_cmpint(ret, ==, sizeof(len));
  76. len = ntohl(len);
  77. ret = qemu_recv(socket, buffer, len, 0);
  78. g_assert_cmpstr(buffer, ==, "TEST");
  79. }
  80. static void rx_stop_cont_test(QVirtioDevice *dev,
  81. QGuestAllocator *alloc, QVirtQueue *vq,
  82. int socket)
  83. {
  84. QTestState *qts = global_qtest;
  85. uint64_t req_addr;
  86. uint32_t free_head;
  87. char test[] = "TEST";
  88. char buffer[64];
  89. int len = htonl(sizeof(test));
  90. QDict *rsp;
  91. struct iovec iov[] = {
  92. {
  93. .iov_base = &len,
  94. .iov_len = sizeof(len),
  95. }, {
  96. .iov_base = test,
  97. .iov_len = sizeof(test),
  98. },
  99. };
  100. int ret;
  101. req_addr = guest_alloc(alloc, 64);
  102. free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
  103. qvirtqueue_kick(qts, dev, vq, free_head);
  104. rsp = qmp("{ 'execute' : 'stop'}");
  105. qobject_unref(rsp);
  106. ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
  107. g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
  108. /* We could check the status, but this command is more importantly to
  109. * ensure the packet data gets queued in QEMU, before we do 'cont'.
  110. */
  111. rsp = qmp("{ 'execute' : 'query-status'}");
  112. qobject_unref(rsp);
  113. rsp = qmp("{ 'execute' : 'cont'}");
  114. qobject_unref(rsp);
  115. qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
  116. QVIRTIO_NET_TIMEOUT_US);
  117. memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
  118. g_assert_cmpstr(buffer, ==, "TEST");
  119. guest_free(alloc, req_addr);
  120. }
  121. static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
  122. {
  123. QVirtioNet *net_if = obj;
  124. QVirtioDevice *dev = net_if->vdev;
  125. QVirtQueue *rx = net_if->queues[0];
  126. QVirtQueue *tx = net_if->queues[1];
  127. int *sv = data;
  128. rx_test(dev, t_alloc, rx, sv[0]);
  129. tx_test(dev, t_alloc, tx, sv[0]);
  130. }
  131. static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
  132. {
  133. QVirtioNet *net_if = obj;
  134. QVirtioDevice *dev = net_if->vdev;
  135. QVirtQueue *rx = net_if->queues[0];
  136. int *sv = data;
  137. rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
  138. }
  139. #endif
  140. static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
  141. {
  142. QVirtioPCIDevice *dev = obj;
  143. QTestState *qts = dev->pdev->bus->qts;
  144. const char *arch = qtest_get_arch();
  145. qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
  146. "{'addr': %s}", stringify(PCI_SLOT_HP));
  147. if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
  148. qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
  149. }
  150. }
  151. static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
  152. {
  153. int *sv = data;
  154. char buffer[60];
  155. int len;
  156. QDict *rsp;
  157. int ret;
  158. uint16_t *proto = (uint16_t *)&buffer[12];
  159. size_t total_received = 0;
  160. uint64_t start, now, last_rxt, deadline;
  161. /* Send a set of packets over a few second period */
  162. rsp = qmp("{ 'execute' : 'announce-self', "
  163. " 'arguments': {"
  164. " 'initial': 20, 'max': 100,"
  165. " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
  166. assert(!qdict_haskey(rsp, "error"));
  167. qobject_unref(rsp);
  168. /* Catch the first packet and make sure it's a RARP */
  169. ret = qemu_recv(sv[0], &len, sizeof(len), 0);
  170. g_assert_cmpint(ret, ==, sizeof(len));
  171. len = ntohl(len);
  172. ret = qemu_recv(sv[0], buffer, len, 0);
  173. g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
  174. /*
  175. * Stop the announcment by settings rounds to 0 on the
  176. * existing timer.
  177. */
  178. rsp = qmp("{ 'execute' : 'announce-self', "
  179. " 'arguments': {"
  180. " 'initial': 20, 'max': 100,"
  181. " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
  182. assert(!qdict_haskey(rsp, "error"));
  183. qobject_unref(rsp);
  184. /* Now make sure the packets stop */
  185. /* Times are in us */
  186. start = g_get_monotonic_time();
  187. /* 30 packets, max gap 100ms, * 4 for wiggle */
  188. deadline = start + 1000 * (100 * 30 * 4);
  189. last_rxt = start;
  190. while (true) {
  191. int saved_err;
  192. ret = qemu_recv(sv[0], buffer, 60, MSG_DONTWAIT);
  193. saved_err = errno;
  194. now = g_get_monotonic_time();
  195. g_assert_cmpint(now, <, deadline);
  196. if (ret >= 0) {
  197. if (ret) {
  198. last_rxt = now;
  199. }
  200. total_received += ret;
  201. /* Check it's not spewing loads */
  202. g_assert_cmpint(total_received, <, 60 * 30 * 2);
  203. } else {
  204. g_assert_cmpint(saved_err, ==, EAGAIN);
  205. /* 400ms, i.e. 4 worst case gaps */
  206. if ((now - last_rxt) > (1000 * 100 * 4)) {
  207. /* Nothings arrived for a while - must have stopped */
  208. break;
  209. };
  210. /* 100ms */
  211. g_usleep(1000 * 100);
  212. }
  213. };
  214. }
  215. static void virtio_net_test_cleanup(void *sockets)
  216. {
  217. int *sv = sockets;
  218. close(sv[0]);
  219. qos_invalidate_command_line();
  220. close(sv[1]);
  221. g_free(sv);
  222. }
  223. static void *virtio_net_test_setup(GString *cmd_line, void *arg)
  224. {
  225. int ret;
  226. int *sv = g_new(int, 2);
  227. ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
  228. g_assert_cmpint(ret, !=, -1);
  229. g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
  230. g_test_queue_destroy(virtio_net_test_cleanup, sv);
  231. return sv;
  232. }
  233. static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
  234. {
  235. QVirtioNet *dev = obj;
  236. QVirtQueue *vq = dev->queues[1];
  237. uint64_t req_addr;
  238. uint32_t free_head;
  239. size_t alloc_size = (size_t)data / 64;
  240. QTestState *qts = global_qtest;
  241. int i;
  242. /* Bypass the limitation by pointing several descriptors to a single
  243. * smaller area */
  244. req_addr = guest_alloc(t_alloc, alloc_size);
  245. free_head = qvirtqueue_add(qts, vq, req_addr, alloc_size, false, true);
  246. for (i = 0; i < 64; i++) {
  247. qvirtqueue_add(qts, vq, req_addr, alloc_size, false, i != 63);
  248. }
  249. qvirtqueue_kick(qts, dev->vdev, vq, free_head);
  250. qvirtio_wait_used_elem(qts, dev->vdev, vq, free_head, NULL,
  251. QVIRTIO_NET_TIMEOUT_US);
  252. guest_free(t_alloc, req_addr);
  253. }
  254. static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
  255. {
  256. g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
  257. return arg;
  258. }
  259. static void register_virtio_net_test(void)
  260. {
  261. QOSGraphTestOptions opts = {
  262. .before = virtio_net_test_setup,
  263. };
  264. qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
  265. #ifndef _WIN32
  266. qos_add_test("basic", "virtio-net", send_recv_test, &opts);
  267. qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
  268. #endif
  269. qos_add_test("announce-self", "virtio-net", announce_self, &opts);
  270. /* These tests do not need a loopback backend. */
  271. opts.before = virtio_net_test_setup_nosocket;
  272. opts.arg = (gpointer)UINT_MAX;
  273. qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
  274. opts.arg = (gpointer)NET_BUFSIZE;
  275. qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
  276. }
  277. libqos_init(register_virtio_net_test);