vl: Deprecate -virtfs_synth
[qemu/ar7.git] / tests / virtio-net-test.c
blob163126cf07e1aa401d7d91f3bff91a889cec1cf5
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 #ifndef ETH_P_RARP
19 #define ETH_P_RARP 0x8035
20 #endif
22 #define PCI_SLOT_HP 0x06
23 #define PCI_SLOT 0x04
25 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
26 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
28 #ifndef _WIN32
30 static void rx_test(QVirtioDevice *dev,
31 QGuestAllocator *alloc, QVirtQueue *vq,
32 int socket)
34 uint64_t req_addr;
35 uint32_t free_head;
36 char test[] = "TEST";
37 char buffer[64];
38 int len = htonl(sizeof(test));
39 struct iovec iov[] = {
41 .iov_base = &len,
42 .iov_len = sizeof(len),
43 }, {
44 .iov_base = test,
45 .iov_len = sizeof(test),
48 int ret;
50 req_addr = guest_alloc(alloc, 64);
52 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
53 qvirtqueue_kick(dev, vq, free_head);
55 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
56 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
58 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
59 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
60 g_assert_cmpstr(buffer, ==, "TEST");
62 guest_free(alloc, req_addr);
65 static void tx_test(QVirtioDevice *dev,
66 QGuestAllocator *alloc, QVirtQueue *vq,
67 int socket)
69 uint64_t req_addr;
70 uint32_t free_head;
71 uint32_t len;
72 char buffer[64];
73 int ret;
75 req_addr = guest_alloc(alloc, 64);
76 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
78 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
79 qvirtqueue_kick(dev, vq, free_head);
81 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
82 guest_free(alloc, req_addr);
84 ret = qemu_recv(socket, &len, sizeof(len), 0);
85 g_assert_cmpint(ret, ==, sizeof(len));
86 len = ntohl(len);
88 ret = qemu_recv(socket, buffer, len, 0);
89 g_assert_cmpstr(buffer, ==, "TEST");
92 static void rx_stop_cont_test(QVirtioDevice *dev,
93 QGuestAllocator *alloc, QVirtQueue *vq,
94 int socket)
96 uint64_t req_addr;
97 uint32_t free_head;
98 char test[] = "TEST";
99 char buffer[64];
100 int len = htonl(sizeof(test));
101 QDict *rsp;
102 struct iovec iov[] = {
104 .iov_base = &len,
105 .iov_len = sizeof(len),
106 }, {
107 .iov_base = test,
108 .iov_len = sizeof(test),
111 int ret;
113 req_addr = guest_alloc(alloc, 64);
115 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
116 qvirtqueue_kick(dev, vq, free_head);
118 rsp = qmp("{ 'execute' : 'stop'}");
119 qobject_unref(rsp);
121 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
122 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
124 /* We could check the status, but this command is more importantly to
125 * ensure the packet data gets queued in QEMU, before we do 'cont'.
127 rsp = qmp("{ 'execute' : 'query-status'}");
128 qobject_unref(rsp);
129 rsp = qmp("{ 'execute' : 'cont'}");
130 qobject_unref(rsp);
132 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
133 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
134 g_assert_cmpstr(buffer, ==, "TEST");
136 guest_free(alloc, req_addr);
139 static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
141 QVirtioNet *net_if = obj;
142 QVirtioDevice *dev = net_if->vdev;
143 QVirtQueue *rx = net_if->queues[0];
144 QVirtQueue *tx = net_if->queues[1];
145 int *sv = data;
147 rx_test(dev, t_alloc, rx, sv[0]);
148 tx_test(dev, t_alloc, tx, sv[0]);
151 static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
153 QVirtioNet *net_if = obj;
154 QVirtioDevice *dev = net_if->vdev;
155 QVirtQueue *rx = net_if->queues[0];
156 int *sv = data;
158 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
161 #endif
163 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
165 QVirtioPCIDevice *dev = obj;
166 QTestState *qts = dev->pdev->bus->qts;
167 const char *arch = qtest_get_arch();
169 qtest_qmp_device_add("virtio-net-pci", "net1",
170 "{'addr': %s}", stringify(PCI_SLOT_HP));
172 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
173 qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
177 static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
179 int *sv = data;
180 char buffer[60];
181 int len;
182 QDict *rsp;
183 int ret;
184 uint16_t *proto = (uint16_t *)&buffer[12];
186 rsp = qmp("{ 'execute' : 'announce-self', "
187 " 'arguments': {"
188 " 'initial': 50, 'max': 550,"
189 " 'rounds': 10, 'step': 50 } }");
190 assert(!qdict_haskey(rsp, "error"));
191 qobject_unref(rsp);
193 /* Catch the packet and make sure it's a RARP */
194 ret = qemu_recv(sv[0], &len, sizeof(len), 0);
195 g_assert_cmpint(ret, ==, sizeof(len));
196 len = ntohl(len);
198 ret = qemu_recv(sv[0], buffer, len, 0);
199 g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
202 static void virtio_net_test_cleanup(void *sockets)
204 int *sv = sockets;
206 close(sv[0]);
207 qos_invalidate_command_line();
208 close(sv[1]);
209 g_free(sv);
212 static void *virtio_net_test_setup(GString *cmd_line, void *arg)
214 int ret;
215 int *sv = g_new(int, 2);
217 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
218 g_assert_cmpint(ret, !=, -1);
220 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
222 g_test_queue_destroy(virtio_net_test_cleanup, sv);
223 return sv;
226 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
228 QVirtioNet *dev = obj;
229 QVirtQueue *vq = dev->queues[1];
230 uint64_t req_addr;
231 uint32_t free_head;
232 size_t alloc_size = (size_t)data / 64;
233 int i;
235 /* Bypass the limitation by pointing several descriptors to a single
236 * smaller area */
237 req_addr = guest_alloc(t_alloc, alloc_size);
238 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
240 for (i = 0; i < 64; i++) {
241 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
243 qvirtqueue_kick(dev->vdev, vq, free_head);
245 qvirtio_wait_used_elem(dev->vdev, vq, free_head, NULL,
246 QVIRTIO_NET_TIMEOUT_US);
247 guest_free(t_alloc, req_addr);
250 static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
252 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
253 return arg;
256 static void register_virtio_net_test(void)
258 QOSGraphTestOptions opts = {
259 .before = virtio_net_test_setup,
262 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
263 #ifndef _WIN32
264 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
265 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
266 #endif
267 qos_add_test("announce-self", "virtio-net", announce_self, &opts);
269 /* These tests do not need a loopback backend. */
270 opts.before = virtio_net_test_setup_nosocket;
271 opts.arg = (gpointer)UINT_MAX;
272 qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
273 opts.arg = (gpointer)NET_BUFSIZE;
274 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
277 libqos_init(register_virtio_net_test);