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