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