Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210330' into...
[qemu/ar7.git] / tests / qtest / virtio-net-test.c
bloba08e2ffe123f89a3f6fdcfd2cd4e4993b6f525a5
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 "qemu-common.h"
12 #include "libqtest-single.h"
13 #include "qemu/iov.h"
14 #include "qemu/module.h"
15 #include "qapi/qmp/qdict.h"
16 #include "hw/virtio/virtio-net.h"
17 #include "libqos/qgraph.h"
18 #include "libqos/virtio-net.h"
20 #ifndef ETH_P_RARP
21 #define ETH_P_RARP 0x8035
22 #endif
24 #define PCI_SLOT_HP 0x06
25 #define PCI_SLOT 0x04
27 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
28 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
30 #ifndef _WIN32
32 static void rx_test(QVirtioDevice *dev,
33 QGuestAllocator *alloc, QVirtQueue *vq,
34 int socket)
36 QTestState *qts = global_qtest;
37 uint64_t req_addr;
38 uint32_t free_head;
39 char test[] = "TEST";
40 char buffer[64];
41 int len = htonl(sizeof(test));
42 struct iovec iov[] = {
44 .iov_base = &len,
45 .iov_len = sizeof(len),
46 }, {
47 .iov_base = test,
48 .iov_len = sizeof(test),
51 int ret;
53 req_addr = guest_alloc(alloc, 64);
55 free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
56 qvirtqueue_kick(qts, dev, vq, free_head);
58 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
59 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
61 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
62 QVIRTIO_NET_TIMEOUT_US);
63 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
64 g_assert_cmpstr(buffer, ==, "TEST");
66 guest_free(alloc, req_addr);
69 static void tx_test(QVirtioDevice *dev,
70 QGuestAllocator *alloc, QVirtQueue *vq,
71 int socket)
73 QTestState *qts = global_qtest;
74 uint64_t req_addr;
75 uint32_t free_head;
76 uint32_t len;
77 char buffer[64];
78 int ret;
80 req_addr = guest_alloc(alloc, 64);
81 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
83 free_head = qvirtqueue_add(qts, vq, req_addr, 64, false, false);
84 qvirtqueue_kick(qts, dev, vq, free_head);
86 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
87 QVIRTIO_NET_TIMEOUT_US);
88 guest_free(alloc, req_addr);
90 ret = qemu_recv(socket, &len, sizeof(len), 0);
91 g_assert_cmpint(ret, ==, sizeof(len));
92 len = ntohl(len);
94 ret = qemu_recv(socket, buffer, len, 0);
95 g_assert_cmpstr(buffer, ==, "TEST");
98 static void rx_stop_cont_test(QVirtioDevice *dev,
99 QGuestAllocator *alloc, QVirtQueue *vq,
100 int socket)
102 QTestState *qts = global_qtest;
103 uint64_t req_addr;
104 uint32_t free_head;
105 char test[] = "TEST";
106 char buffer[64];
107 int len = htonl(sizeof(test));
108 QDict *rsp;
109 struct iovec iov[] = {
111 .iov_base = &len,
112 .iov_len = sizeof(len),
113 }, {
114 .iov_base = test,
115 .iov_len = sizeof(test),
118 int ret;
120 req_addr = guest_alloc(alloc, 64);
122 free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
123 qvirtqueue_kick(qts, dev, vq, free_head);
125 rsp = qmp("{ 'execute' : 'stop'}");
126 qobject_unref(rsp);
128 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
129 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
131 /* We could check the status, but this command is more importantly to
132 * ensure the packet data gets queued in QEMU, before we do 'cont'.
134 rsp = qmp("{ 'execute' : 'query-status'}");
135 qobject_unref(rsp);
136 rsp = qmp("{ 'execute' : 'cont'}");
137 qobject_unref(rsp);
139 qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
140 QVIRTIO_NET_TIMEOUT_US);
141 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
142 g_assert_cmpstr(buffer, ==, "TEST");
144 guest_free(alloc, req_addr);
147 static void send_recv_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 QVirtQueue *tx = net_if->queues[1];
153 int *sv = data;
155 rx_test(dev, t_alloc, rx, sv[0]);
156 tx_test(dev, t_alloc, tx, sv[0]);
159 static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
161 QVirtioNet *net_if = obj;
162 QVirtioDevice *dev = net_if->vdev;
163 QVirtQueue *rx = net_if->queues[0];
164 int *sv = data;
166 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
169 #endif
171 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
173 QVirtioPCIDevice *dev = obj;
174 QTestState *qts = dev->pdev->bus->qts;
175 const char *arch = qtest_get_arch();
177 qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
178 "{'addr': %s}", stringify(PCI_SLOT_HP));
180 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
181 qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
185 static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
187 int *sv = data;
188 char buffer[60];
189 int len;
190 QDict *rsp;
191 int ret;
192 uint16_t *proto = (uint16_t *)&buffer[12];
193 size_t total_received = 0;
194 uint64_t start, now, last_rxt, deadline;
196 /* Send a set of packets over a few second period */
197 rsp = qmp("{ 'execute' : 'announce-self', "
198 " 'arguments': {"
199 " 'initial': 20, 'max': 100,"
200 " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
201 assert(!qdict_haskey(rsp, "error"));
202 qobject_unref(rsp);
204 /* Catch the first packet and make sure it's a RARP */
205 ret = qemu_recv(sv[0], &len, sizeof(len), 0);
206 g_assert_cmpint(ret, ==, sizeof(len));
207 len = ntohl(len);
209 ret = qemu_recv(sv[0], buffer, len, 0);
210 g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
213 * Stop the announcment by settings rounds to 0 on the
214 * existing timer.
216 rsp = qmp("{ 'execute' : 'announce-self', "
217 " 'arguments': {"
218 " 'initial': 20, 'max': 100,"
219 " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
220 assert(!qdict_haskey(rsp, "error"));
221 qobject_unref(rsp);
223 /* Now make sure the packets stop */
225 /* Times are in us */
226 start = g_get_monotonic_time();
227 /* 30 packets, max gap 100ms, * 4 for wiggle */
228 deadline = start + 1000 * (100 * 30 * 4);
229 last_rxt = start;
231 while (true) {
232 int saved_err;
233 ret = qemu_recv(sv[0], buffer, 60, MSG_DONTWAIT);
234 saved_err = errno;
235 now = g_get_monotonic_time();
236 g_assert_cmpint(now, <, deadline);
238 if (ret >= 0) {
239 if (ret) {
240 last_rxt = now;
242 total_received += ret;
244 /* Check it's not spewing loads */
245 g_assert_cmpint(total_received, <, 60 * 30 * 2);
246 } else {
247 g_assert_cmpint(saved_err, ==, EAGAIN);
249 /* 400ms, i.e. 4 worst case gaps */
250 if ((now - last_rxt) > (1000 * 100 * 4)) {
251 /* Nothings arrived for a while - must have stopped */
252 break;
255 /* 100ms */
256 g_usleep(1000 * 100);
261 static void virtio_net_test_cleanup(void *sockets)
263 int *sv = sockets;
265 close(sv[0]);
266 qos_invalidate_command_line();
267 close(sv[1]);
268 g_free(sv);
271 static void *virtio_net_test_setup(GString *cmd_line, void *arg)
273 int ret;
274 int *sv = g_new(int, 2);
276 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
277 g_assert_cmpint(ret, !=, -1);
279 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
281 g_test_queue_destroy(virtio_net_test_cleanup, sv);
282 return sv;
285 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
287 QVirtioNet *dev = obj;
288 QVirtQueue *vq = dev->queues[1];
289 uint64_t req_addr;
290 uint32_t free_head;
291 size_t alloc_size = (size_t)data / 64;
292 QTestState *qts = global_qtest;
293 int i;
295 /* Bypass the limitation by pointing several descriptors to a single
296 * smaller area */
297 req_addr = guest_alloc(t_alloc, alloc_size);
298 free_head = qvirtqueue_add(qts, vq, req_addr, alloc_size, false, true);
300 for (i = 0; i < 64; i++) {
301 qvirtqueue_add(qts, vq, req_addr, alloc_size, false, i != 63);
303 qvirtqueue_kick(qts, dev->vdev, vq, free_head);
305 qvirtio_wait_used_elem(qts, dev->vdev, vq, free_head, NULL,
306 QVIRTIO_NET_TIMEOUT_US);
307 guest_free(t_alloc, req_addr);
310 static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
312 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
313 return arg;
316 static void register_virtio_net_test(void)
318 QOSGraphTestOptions opts = {
319 .before = virtio_net_test_setup,
322 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
323 #ifndef _WIN32
324 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
325 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
326 #endif
327 qos_add_test("announce-self", "virtio-net", announce_self, &opts);
329 /* These tests do not need a loopback backend. */
330 opts.before = virtio_net_test_setup_nosocket;
331 opts.arg = (gpointer)UINT_MAX;
332 qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
333 opts.arg = (gpointer)NET_BUFSIZE;
334 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
337 libqos_init(register_virtio_net_test);