]> git.proxmox.com Git - mirror_qemu.git/blame - tests/virtio-net-test.c
minikconf: do not include variables from MINIKCONF_ARGS in config-all-devices.mak
[mirror_qemu.git] / tests / virtio-net-test.c
CommitLineData
b815ec5e
AF
1/*
2 * QTest testcase for VirtIO NIC
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 *
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 */
9
681c28a3 10#include "qemu/osdep.h"
a8d25326 11#include "qemu-common.h"
b815ec5e 12#include "libqtest.h"
2af40254 13#include "qemu/iov.h"
0b8fa32f 14#include "qemu/module.h"
452fcdbc 15#include "qapi/qmp/qdict.h"
2af40254 16#include "hw/virtio/virtio-net.h"
6ae333f9
EGE
17#include "libqos/qgraph.h"
18#include "libqos/virtio-net.h"
9224709b 19
8b159699
PB
20#ifndef ETH_P_RARP
21#define ETH_P_RARP 0x8035
22#endif
23
9224709b 24#define PCI_SLOT_HP 0x06
2af40254 25#define PCI_SLOT 0x04
b815ec5e 26
2af40254
JW
27#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
28#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
29
2af40254
JW
30#ifndef _WIN32
31
6b9cdf4c 32static void rx_test(QVirtioDevice *dev,
2af40254
JW
33 QGuestAllocator *alloc, QVirtQueue *vq,
34 int socket)
35{
36 uint64_t req_addr;
37 uint32_t free_head;
38 char test[] = "TEST";
39 char buffer[64];
40 int len = htonl(sizeof(test));
41 struct iovec iov[] = {
42 {
43 .iov_base = &len,
44 .iov_len = sizeof(len),
45 }, {
46 .iov_base = test,
47 .iov_len = sizeof(test),
48 },
49 };
50 int ret;
51
52 req_addr = guest_alloc(alloc, 64);
53
54 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
6b9cdf4c 55 qvirtqueue_kick(dev, vq, free_head);
2af40254
JW
56
57 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
58 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
59
be3a6781 60 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
2af40254
JW
61 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
62 g_assert_cmpstr(buffer, ==, "TEST");
63
64 guest_free(alloc, req_addr);
65}
66
6b9cdf4c 67static void tx_test(QVirtioDevice *dev,
2af40254
JW
68 QGuestAllocator *alloc, QVirtQueue *vq,
69 int socket)
70{
71 uint64_t req_addr;
72 uint32_t free_head;
73 uint32_t len;
74 char buffer[64];
75 int ret;
76
77 req_addr = guest_alloc(alloc, 64);
78 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
79
80 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
6b9cdf4c 81 qvirtqueue_kick(dev, vq, free_head);
2af40254 82
be3a6781 83 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
2af40254
JW
84 guest_free(alloc, req_addr);
85
86 ret = qemu_recv(socket, &len, sizeof(len), 0);
87 g_assert_cmpint(ret, ==, sizeof(len));
88 len = ntohl(len);
89
90 ret = qemu_recv(socket, buffer, len, 0);
91 g_assert_cmpstr(buffer, ==, "TEST");
92}
93
6b9cdf4c 94static void rx_stop_cont_test(QVirtioDevice *dev,
8887f84c
JW
95 QGuestAllocator *alloc, QVirtQueue *vq,
96 int socket)
97{
98 uint64_t req_addr;
99 uint32_t free_head;
100 char test[] = "TEST";
101 char buffer[64];
102 int len = htonl(sizeof(test));
1ec3b71c 103 QDict *rsp;
8887f84c
JW
104 struct iovec iov[] = {
105 {
106 .iov_base = &len,
107 .iov_len = sizeof(len),
108 }, {
109 .iov_base = test,
110 .iov_len = sizeof(test),
111 },
112 };
113 int ret;
114
115 req_addr = guest_alloc(alloc, 64);
116
117 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
6b9cdf4c 118 qvirtqueue_kick(dev, vq, free_head);
8887f84c 119
1ec3b71c 120 rsp = qmp("{ 'execute' : 'stop'}");
cb3e7f08 121 qobject_unref(rsp);
8887f84c
JW
122
123 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
124 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
125
126 /* We could check the status, but this command is more importantly to
127 * ensure the packet data gets queued in QEMU, before we do 'cont'.
128 */
1ec3b71c 129 rsp = qmp("{ 'execute' : 'query-status'}");
cb3e7f08 130 qobject_unref(rsp);
1ec3b71c 131 rsp = qmp("{ 'execute' : 'cont'}");
cb3e7f08 132 qobject_unref(rsp);
8887f84c 133
be3a6781 134 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
8887f84c
JW
135 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
136 g_assert_cmpstr(buffer, ==, "TEST");
137
138 guest_free(alloc, req_addr);
139}
140
6ae333f9 141static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
b815ec5e 142{
6ae333f9
EGE
143 QVirtioNet *net_if = obj;
144 QVirtioDevice *dev = net_if->vdev;
6bd4a6d4
PB
145 QVirtQueue *rx = net_if->queues[0];
146 QVirtQueue *tx = net_if->queues[1];
6ae333f9
EGE
147 int *sv = data;
148
149 rx_test(dev, t_alloc, rx, sv[0]);
150 tx_test(dev, t_alloc, tx, sv[0]);
b815ec5e
AF
151}
152
6ae333f9 153static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
8887f84c 154{
6ae333f9
EGE
155 QVirtioNet *net_if = obj;
156 QVirtioDevice *dev = net_if->vdev;
6bd4a6d4 157 QVirtQueue *rx = net_if->queues[0];
6ae333f9
EGE
158 int *sv = data;
159
160 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
8887f84c
JW
161}
162
6ae333f9 163#endif
2af40254 164
6ae333f9
EGE
165static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
166{
6ebb8d2a
TH
167 QVirtioPCIDevice *dev = obj;
168 QTestState *qts = dev->pdev->bus->qts;
6ae333f9 169 const char *arch = qtest_get_arch();
2af40254 170
6ae333f9
EGE
171 qtest_qmp_device_add("virtio-net-pci", "net1",
172 "{'addr': %s}", stringify(PCI_SLOT_HP));
2af40254 173
6ae333f9 174 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
6ebb8d2a 175 qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
6ae333f9
EGE
176 }
177}
2af40254 178
8b159699
PB
179static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
180{
181 int *sv = data;
182 char buffer[60];
183 int len;
184 QDict *rsp;
185 int ret;
186 uint16_t *proto = (uint16_t *)&buffer[12];
187
188 rsp = qmp("{ 'execute' : 'announce-self', "
189 " 'arguments': {"
190 " 'initial': 50, 'max': 550,"
191 " 'rounds': 10, 'step': 50 } }");
192 assert(!qdict_haskey(rsp, "error"));
193 qobject_unref(rsp);
194
195 /* Catch the packet and make sure it's a RARP */
196 ret = qemu_recv(sv[0], &len, sizeof(len), 0);
197 g_assert_cmpint(ret, ==, sizeof(len));
198 len = ntohl(len);
199
200 ret = qemu_recv(sv[0], buffer, len, 0);
201 g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
202}
203
6ae333f9
EGE
204static void virtio_net_test_cleanup(void *sockets)
205{
206 int *sv = sockets;
2af40254 207
2af40254 208 close(sv[0]);
6ae333f9
EGE
209 qos_invalidate_command_line();
210 close(sv[1]);
211 g_free(sv);
212}
213
214static void *virtio_net_test_setup(GString *cmd_line, void *arg)
215{
216 int ret;
217 int *sv = g_new(int, 2);
218
219 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
220 g_assert_cmpint(ret, !=, -1);
221
222 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
223
224 g_test_queue_destroy(virtio_net_test_cleanup, sv);
225 return sv;
2af40254 226}
118cafff 227
6ae333f9 228static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
118cafff 229{
6ae333f9
EGE
230 QVirtioNet *dev = obj;
231 QVirtQueue *vq = dev->queues[1];
118cafff
JW
232 uint64_t req_addr;
233 uint32_t free_head;
234 size_t alloc_size = (size_t)data / 64;
235 int i;
236
118cafff
JW
237 /* Bypass the limitation by pointing several descriptors to a single
238 * smaller area */
6ae333f9 239 req_addr = guest_alloc(t_alloc, alloc_size);
118cafff
JW
240 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
241
242 for (i = 0; i < 64; i++) {
243 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
244 }
6ae333f9 245 qvirtqueue_kick(dev->vdev, vq, free_head);
118cafff 246
6ae333f9 247 qvirtio_wait_used_elem(dev->vdev, vq, free_head, NULL,
118cafff 248 QVIRTIO_NET_TIMEOUT_US);
6ae333f9 249 guest_free(t_alloc, req_addr);
118cafff 250}
2af40254 251
6ae333f9 252static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
9224709b 253{
6ae333f9
EGE
254 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
255 return arg;
9224709b
IM
256}
257
6ae333f9 258static void register_virtio_net_test(void)
b815ec5e 259{
6ae333f9
EGE
260 QOSGraphTestOptions opts = {
261 .before = virtio_net_test_setup,
262 };
263
264 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
2af40254 265#ifndef _WIN32
6ae333f9
EGE
266 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
267 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
2af40254 268#endif
8b159699 269 qos_add_test("announce-self", "virtio-net", announce_self, &opts);
b815ec5e 270
6ae333f9
EGE
271 /* These tests do not need a loopback backend. */
272 opts.before = virtio_net_test_setup_nosocket;
273 opts.arg = (gpointer)UINT_MAX;
274 qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
275 opts.arg = (gpointer)NET_BUFSIZE;
276 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
b815ec5e 277}
6ae333f9
EGE
278
279libqos_init(register_virtio_net_test);