tests/libqos: embed allocators instead of malloc-ing them separately
[qemu/ar7.git] / tests / virtio-net-test.c
blob653148eeabe365ef39ef2c0dc891966593d8b771
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_start_device(&dev->vdev);
49 return dev;
52 GCC_FMT_ATTR(1, 2)
53 static QOSState *pci_test_start(const char *cmd, ...)
55 QOSState *qs;
56 va_list ap;
57 const char *arch = qtest_get_arch();
59 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
60 va_start(ap, cmd);
61 qs = qtest_pc_vboot(cmd, ap);
62 va_end(ap);
63 } else if (strcmp(arch, "ppc64") == 0) {
64 va_start(ap, cmd);
65 qs = qtest_spapr_vboot(cmd, ap);
66 va_end(ap);
67 } else {
68 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
69 exit(EXIT_FAILURE);
71 global_qtest = qs->qts;
72 return qs;
75 static void driver_init(QVirtioDevice *dev)
77 uint32_t features;
79 features = qvirtio_get_features(dev);
80 features = features & ~(QVIRTIO_F_BAD_FEATURE |
81 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
82 (1u << VIRTIO_RING_F_EVENT_IDX));
83 qvirtio_set_features(dev, features);
85 qvirtio_set_driver_ok(dev);
88 static void rx_test(QVirtioDevice *dev,
89 QGuestAllocator *alloc, QVirtQueue *vq,
90 int socket)
92 uint64_t req_addr;
93 uint32_t free_head;
94 char test[] = "TEST";
95 char buffer[64];
96 int len = htonl(sizeof(test));
97 struct iovec iov[] = {
99 .iov_base = &len,
100 .iov_len = sizeof(len),
101 }, {
102 .iov_base = test,
103 .iov_len = sizeof(test),
106 int ret;
108 req_addr = guest_alloc(alloc, 64);
110 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
111 qvirtqueue_kick(dev, vq, free_head);
113 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
114 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
116 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
117 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
118 g_assert_cmpstr(buffer, ==, "TEST");
120 guest_free(alloc, req_addr);
123 static void tx_test(QVirtioDevice *dev,
124 QGuestAllocator *alloc, QVirtQueue *vq,
125 int socket)
127 uint64_t req_addr;
128 uint32_t free_head;
129 uint32_t len;
130 char buffer[64];
131 int ret;
133 req_addr = guest_alloc(alloc, 64);
134 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
136 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
137 qvirtqueue_kick(dev, vq, free_head);
139 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
140 guest_free(alloc, req_addr);
142 ret = qemu_recv(socket, &len, sizeof(len), 0);
143 g_assert_cmpint(ret, ==, sizeof(len));
144 len = ntohl(len);
146 ret = qemu_recv(socket, buffer, len, 0);
147 g_assert_cmpstr(buffer, ==, "TEST");
150 static void rx_stop_cont_test(QVirtioDevice *dev,
151 QGuestAllocator *alloc, QVirtQueue *vq,
152 int socket)
154 uint64_t req_addr;
155 uint32_t free_head;
156 char test[] = "TEST";
157 char buffer[64];
158 int len = htonl(sizeof(test));
159 QDict *rsp;
160 struct iovec iov[] = {
162 .iov_base = &len,
163 .iov_len = sizeof(len),
164 }, {
165 .iov_base = test,
166 .iov_len = sizeof(test),
169 int ret;
171 req_addr = guest_alloc(alloc, 64);
173 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
174 qvirtqueue_kick(dev, vq, free_head);
176 rsp = qmp("{ 'execute' : 'stop'}");
177 qobject_unref(rsp);
179 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
180 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
182 /* We could check the status, but this command is more importantly to
183 * ensure the packet data gets queued in QEMU, before we do 'cont'.
185 rsp = qmp("{ 'execute' : 'query-status'}");
186 qobject_unref(rsp);
187 rsp = qmp("{ 'execute' : 'cont'}");
188 qobject_unref(rsp);
190 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
191 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
192 g_assert_cmpstr(buffer, ==, "TEST");
194 guest_free(alloc, req_addr);
197 static void send_recv_test(QVirtioDevice *dev,
198 QGuestAllocator *alloc, QVirtQueue *rvq,
199 QVirtQueue *tvq, int socket)
201 rx_test(dev, alloc, rvq, socket);
202 tx_test(dev, alloc, tvq, socket);
205 static void stop_cont_test(QVirtioDevice *dev,
206 QGuestAllocator *alloc, QVirtQueue *rvq,
207 QVirtQueue *tvq, int socket)
209 rx_stop_cont_test(dev, alloc, rvq, socket);
212 static void pci_basic(gconstpointer data)
214 QVirtioPCIDevice *dev;
215 QOSState *qs;
216 QVirtQueuePCI *tx, *rx;
217 void (*func) (QVirtioDevice *dev,
218 QGuestAllocator *alloc,
219 QVirtQueue *rvq,
220 QVirtQueue *tvq,
221 int socket) = data;
222 int sv[2], ret;
224 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
225 g_assert_cmpint(ret, !=, -1);
227 qs = pci_test_start("-netdev socket,fd=%d,id=hs0 -device "
228 "virtio-net-pci,netdev=hs0", sv[1]);
229 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
231 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, &qs->alloc, 0);
232 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, &qs->alloc, 1);
234 driver_init(&dev->vdev);
235 func(&dev->vdev, &qs->alloc, &rx->vq, &tx->vq, sv[0]);
237 /* End test */
238 close(sv[0]);
239 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, &qs->alloc);
240 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, &qs->alloc);
241 qvirtio_pci_device_disable(dev);
242 g_free(dev->pdev);
243 g_free(dev);
244 qtest_shutdown(qs);
247 static void large_tx(gconstpointer data)
249 QVirtioPCIDevice *dev;
250 QOSState *qs;
251 QVirtQueuePCI *tx, *rx;
252 QVirtQueue *vq;
253 uint64_t req_addr;
254 uint32_t free_head;
255 size_t alloc_size = (size_t)data / 64;
256 int i;
258 qs = pci_test_start("-netdev hubport,id=hp0,hubid=0 "
259 "-device virtio-net-pci,netdev=hp0");
260 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
262 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
263 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
265 driver_init(&dev->vdev);
266 vq = &tx->vq;
268 /* Bypass the limitation by pointing several descriptors to a single
269 * smaller area */
270 req_addr = guest_alloc(qs->alloc, alloc_size);
271 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
273 for (i = 0; i < 64; i++) {
274 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
276 qvirtqueue_kick(&dev->vdev, vq, free_head);
278 qvirtio_wait_used_elem(&dev->vdev, vq, free_head, NULL,
279 QVIRTIO_NET_TIMEOUT_US);
281 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
282 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
283 qvirtio_pci_device_disable(dev);
284 g_free(dev->pdev);
285 g_free(dev);
286 qtest_shutdown(qs);
288 #endif
290 static void hotplug(void)
292 const char *arch = qtest_get_arch();
294 qtest_start("-device virtio-net-pci");
296 qtest_qmp_device_add("virtio-net-pci", "net1",
297 "{'addr': %s}", stringify(PCI_SLOT_HP));
299 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
300 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
303 test_end();
306 int main(int argc, char **argv)
308 g_test_init(&argc, &argv, NULL);
309 #ifndef _WIN32
310 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
311 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
312 stop_cont_test, pci_basic);
313 qtest_add_data_func("/virtio/net/pci/large_tx_uint_max",
314 (gconstpointer)UINT_MAX, large_tx);
315 qtest_add_data_func("/virtio/net/pci/large_tx_net_bufsize",
316 (gconstpointer)NET_BUFSIZE, large_tx);
317 #endif
318 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
320 return g_test_run();