s390x: upgrade status of KVM cores to "supported"
[qemu/ar7.git] / tests / virtio-net-test.c
blobe9783e67075fe40624884659688e58f92a933510
1 /*
2 * QTest testcase for VirtIO NIC
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
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 */
10 #include "qemu/osdep.h"
11 #include "libqtest.h"
12 #include "qemu-common.h"
13 #include "qemu/sockets.h"
14 #include "qemu/iov.h"
15 #include "libqos/libqos-pc.h"
16 #include "libqos/libqos-spapr.h"
17 #include "libqos/virtio.h"
18 #include "libqos/virtio-pci.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qemu/bswap.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "standard-headers/linux/virtio_ids.h"
23 #include "standard-headers/linux/virtio_ring.h"
25 #define PCI_SLOT_HP 0x06
26 #define PCI_SLOT 0x04
28 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
29 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31 static void test_end(void)
33 qtest_end();
36 #ifndef _WIN32
38 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
40 QVirtioPCIDevice *dev;
42 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
43 g_assert(dev != NULL);
44 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
46 qvirtio_pci_device_enable(dev);
47 qvirtio_reset(&dev->vdev);
48 qvirtio_set_acknowledge(&dev->vdev);
49 qvirtio_set_driver(&dev->vdev);
51 return dev;
54 GCC_FMT_ATTR(1, 2)
55 static QOSState *pci_test_start(const char *cmd, ...)
57 QOSState *qs;
58 va_list ap;
59 const char *arch = qtest_get_arch();
61 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
62 va_start(ap, cmd);
63 qs = qtest_pc_vboot(cmd, ap);
64 va_end(ap);
65 } else if (strcmp(arch, "ppc64") == 0) {
66 va_start(ap, cmd);
67 qs = qtest_spapr_vboot(cmd, ap);
68 va_end(ap);
69 } else {
70 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
71 exit(EXIT_FAILURE);
73 global_qtest = qs->qts;
74 return qs;
77 static void driver_init(QVirtioDevice *dev)
79 uint32_t features;
81 features = qvirtio_get_features(dev);
82 features = features & ~(QVIRTIO_F_BAD_FEATURE |
83 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
84 (1u << VIRTIO_RING_F_EVENT_IDX));
85 qvirtio_set_features(dev, features);
87 qvirtio_set_driver_ok(dev);
90 static void rx_test(QVirtioDevice *dev,
91 QGuestAllocator *alloc, QVirtQueue *vq,
92 int socket)
94 uint64_t req_addr;
95 uint32_t free_head;
96 char test[] = "TEST";
97 char buffer[64];
98 int len = htonl(sizeof(test));
99 struct iovec iov[] = {
101 .iov_base = &len,
102 .iov_len = sizeof(len),
103 }, {
104 .iov_base = test,
105 .iov_len = sizeof(test),
108 int ret;
110 req_addr = guest_alloc(alloc, 64);
112 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
113 qvirtqueue_kick(dev, vq, free_head);
115 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
116 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
118 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
119 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
120 g_assert_cmpstr(buffer, ==, "TEST");
122 guest_free(alloc, req_addr);
125 static void tx_test(QVirtioDevice *dev,
126 QGuestAllocator *alloc, QVirtQueue *vq,
127 int socket)
129 uint64_t req_addr;
130 uint32_t free_head;
131 uint32_t len;
132 char buffer[64];
133 int ret;
135 req_addr = guest_alloc(alloc, 64);
136 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
138 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
139 qvirtqueue_kick(dev, vq, free_head);
141 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
142 guest_free(alloc, req_addr);
144 ret = qemu_recv(socket, &len, sizeof(len), 0);
145 g_assert_cmpint(ret, ==, sizeof(len));
146 len = ntohl(len);
148 ret = qemu_recv(socket, buffer, len, 0);
149 g_assert_cmpstr(buffer, ==, "TEST");
152 static void rx_stop_cont_test(QVirtioDevice *dev,
153 QGuestAllocator *alloc, QVirtQueue *vq,
154 int socket)
156 uint64_t req_addr;
157 uint32_t free_head;
158 char test[] = "TEST";
159 char buffer[64];
160 int len = htonl(sizeof(test));
161 QDict *rsp;
162 struct iovec iov[] = {
164 .iov_base = &len,
165 .iov_len = sizeof(len),
166 }, {
167 .iov_base = test,
168 .iov_len = sizeof(test),
171 int ret;
173 req_addr = guest_alloc(alloc, 64);
175 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
176 qvirtqueue_kick(dev, vq, free_head);
178 rsp = qmp("{ 'execute' : 'stop'}");
179 qobject_unref(rsp);
181 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
182 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
184 /* We could check the status, but this command is more importantly to
185 * ensure the packet data gets queued in QEMU, before we do 'cont'.
187 rsp = qmp("{ 'execute' : 'query-status'}");
188 qobject_unref(rsp);
189 rsp = qmp("{ 'execute' : 'cont'}");
190 qobject_unref(rsp);
192 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
193 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
194 g_assert_cmpstr(buffer, ==, "TEST");
196 guest_free(alloc, req_addr);
199 static void send_recv_test(QVirtioDevice *dev,
200 QGuestAllocator *alloc, QVirtQueue *rvq,
201 QVirtQueue *tvq, int socket)
203 rx_test(dev, alloc, rvq, socket);
204 tx_test(dev, alloc, tvq, socket);
207 static void stop_cont_test(QVirtioDevice *dev,
208 QGuestAllocator *alloc, QVirtQueue *rvq,
209 QVirtQueue *tvq, int socket)
211 rx_stop_cont_test(dev, alloc, rvq, socket);
214 static void pci_basic(gconstpointer data)
216 QVirtioPCIDevice *dev;
217 QOSState *qs;
218 QVirtQueuePCI *tx, *rx;
219 void (*func) (QVirtioDevice *dev,
220 QGuestAllocator *alloc,
221 QVirtQueue *rvq,
222 QVirtQueue *tvq,
223 int socket) = data;
224 int sv[2], ret;
226 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
227 g_assert_cmpint(ret, !=, -1);
229 qs = pci_test_start("-netdev socket,fd=%d,id=hs0 -device "
230 "virtio-net-pci,netdev=hs0", sv[1]);
231 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
233 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
234 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
236 driver_init(&dev->vdev);
237 func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
239 /* End test */
240 close(sv[0]);
241 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
242 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
243 qvirtio_pci_device_disable(dev);
244 g_free(dev->pdev);
245 g_free(dev);
246 qtest_shutdown(qs);
249 static void large_tx(gconstpointer data)
251 QVirtioPCIDevice *dev;
252 QOSState *qs;
253 QVirtQueuePCI *tx, *rx;
254 QVirtQueue *vq;
255 uint64_t req_addr;
256 uint32_t free_head;
257 size_t alloc_size = (size_t)data / 64;
258 int i;
260 qs = pci_test_start("-netdev hubport,id=hp0,hubid=0 "
261 "-device virtio-net-pci,netdev=hp0");
262 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
264 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
265 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
267 driver_init(&dev->vdev);
268 vq = &tx->vq;
270 /* Bypass the limitation by pointing several descriptors to a single
271 * smaller area */
272 req_addr = guest_alloc(qs->alloc, alloc_size);
273 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
275 for (i = 0; i < 64; i++) {
276 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
278 qvirtqueue_kick(&dev->vdev, vq, free_head);
280 qvirtio_wait_used_elem(&dev->vdev, vq, free_head, NULL,
281 QVIRTIO_NET_TIMEOUT_US);
283 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
284 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
285 qvirtio_pci_device_disable(dev);
286 g_free(dev->pdev);
287 g_free(dev);
288 qtest_shutdown(qs);
290 #endif
292 static void hotplug(void)
294 const char *arch = qtest_get_arch();
296 qtest_start("-device virtio-net-pci");
298 qtest_qmp_device_add("virtio-net-pci", "net1",
299 "{'addr': %s}", stringify(PCI_SLOT_HP));
301 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
302 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
305 test_end();
308 int main(int argc, char **argv)
310 g_test_init(&argc, &argv, NULL);
311 #ifndef _WIN32
312 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
313 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
314 stop_cont_test, pci_basic);
315 qtest_add_data_func("/virtio/net/pci/large_tx_uint_max",
316 (gconstpointer)UINT_MAX, large_tx);
317 qtest_add_data_func("/virtio/net/pci/large_tx_net_bufsize",
318 (gconstpointer)NET_BUFSIZE, large_tx);
319 #endif
320 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
322 return g_test_run();