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