qga-win: fix VSS build breakage due to unintended gnu99 C++ flag
[qemu/ar7.git] / tests / virtio-net-test.c
blobc58e670e2fe97fa67ac5b5d96697cc33b7a32c8b
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/iov.h"
13 #include "qapi/qmp/qdict.h"
14 #include "hw/virtio/virtio-net.h"
15 #include "libqos/qgraph.h"
16 #include "libqos/virtio-net.h"
18 #define PCI_SLOT_HP 0x06
19 #define PCI_SLOT 0x04
21 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
22 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
24 #ifndef _WIN32
26 static void rx_test(QVirtioDevice *dev,
27 QGuestAllocator *alloc, QVirtQueue *vq,
28 int socket)
30 uint64_t req_addr;
31 uint32_t free_head;
32 char test[] = "TEST";
33 char buffer[64];
34 int len = htonl(sizeof(test));
35 struct iovec iov[] = {
37 .iov_base = &len,
38 .iov_len = sizeof(len),
39 }, {
40 .iov_base = test,
41 .iov_len = sizeof(test),
44 int ret;
46 req_addr = guest_alloc(alloc, 64);
48 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
49 qvirtqueue_kick(dev, vq, free_head);
51 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
52 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
54 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
55 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
56 g_assert_cmpstr(buffer, ==, "TEST");
58 guest_free(alloc, req_addr);
61 static void tx_test(QVirtioDevice *dev,
62 QGuestAllocator *alloc, QVirtQueue *vq,
63 int socket)
65 uint64_t req_addr;
66 uint32_t free_head;
67 uint32_t len;
68 char buffer[64];
69 int ret;
71 req_addr = guest_alloc(alloc, 64);
72 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
74 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
75 qvirtqueue_kick(dev, vq, free_head);
77 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
78 guest_free(alloc, req_addr);
80 ret = qemu_recv(socket, &len, sizeof(len), 0);
81 g_assert_cmpint(ret, ==, sizeof(len));
82 len = ntohl(len);
84 ret = qemu_recv(socket, buffer, len, 0);
85 g_assert_cmpstr(buffer, ==, "TEST");
88 static void rx_stop_cont_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 QDict *rsp;
98 struct iovec iov[] = {
100 .iov_base = &len,
101 .iov_len = sizeof(len),
102 }, {
103 .iov_base = test,
104 .iov_len = sizeof(test),
107 int ret;
109 req_addr = guest_alloc(alloc, 64);
111 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
112 qvirtqueue_kick(dev, vq, free_head);
114 rsp = qmp("{ 'execute' : 'stop'}");
115 qobject_unref(rsp);
117 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
118 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
120 /* We could check the status, but this command is more importantly to
121 * ensure the packet data gets queued in QEMU, before we do 'cont'.
123 rsp = qmp("{ 'execute' : 'query-status'}");
124 qobject_unref(rsp);
125 rsp = qmp("{ 'execute' : 'cont'}");
126 qobject_unref(rsp);
128 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
129 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
130 g_assert_cmpstr(buffer, ==, "TEST");
132 guest_free(alloc, req_addr);
135 static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
137 QVirtioNet *net_if = obj;
138 QVirtioDevice *dev = net_if->vdev;
139 QVirtQueue *rx = net_if->queues[0];
140 QVirtQueue *tx = net_if->queues[1];
141 int *sv = data;
143 rx_test(dev, t_alloc, rx, sv[0]);
144 tx_test(dev, t_alloc, tx, sv[0]);
147 static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
149 QVirtioNet *net_if = obj;
150 QVirtioDevice *dev = net_if->vdev;
151 QVirtQueue *rx = net_if->queues[0];
152 int *sv = data;
154 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
157 #endif
159 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
161 const char *arch = qtest_get_arch();
163 qtest_qmp_device_add("virtio-net-pci", "net1",
164 "{'addr': %s}", stringify(PCI_SLOT_HP));
166 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
167 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
171 static void virtio_net_test_cleanup(void *sockets)
173 int *sv = sockets;
175 close(sv[0]);
176 qos_invalidate_command_line();
177 close(sv[1]);
178 g_free(sv);
181 static void *virtio_net_test_setup(GString *cmd_line, void *arg)
183 int ret;
184 int *sv = g_new(int, 2);
186 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
187 g_assert_cmpint(ret, !=, -1);
189 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
191 g_test_queue_destroy(virtio_net_test_cleanup, sv);
192 return sv;
195 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
197 QVirtioNet *dev = obj;
198 QVirtQueue *vq = dev->queues[1];
199 uint64_t req_addr;
200 uint32_t free_head;
201 size_t alloc_size = (size_t)data / 64;
202 int i;
204 /* Bypass the limitation by pointing several descriptors to a single
205 * smaller area */
206 req_addr = guest_alloc(t_alloc, alloc_size);
207 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
209 for (i = 0; i < 64; i++) {
210 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
212 qvirtqueue_kick(dev->vdev, vq, free_head);
214 qvirtio_wait_used_elem(dev->vdev, vq, free_head, NULL,
215 QVIRTIO_NET_TIMEOUT_US);
216 guest_free(t_alloc, req_addr);
219 static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
221 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
222 return arg;
225 static void register_virtio_net_test(void)
227 QOSGraphTestOptions opts = {
228 .before = virtio_net_test_setup,
231 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
232 #ifndef _WIN32
233 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
234 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
235 #endif
237 /* These tests do not need a loopback backend. */
238 opts.before = virtio_net_test_setup_nosocket;
239 opts.arg = (gpointer)UINT_MAX;
240 qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
241 opts.arg = (gpointer)NET_BUFSIZE;
242 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
245 libqos_init(register_virtio_net_test);