]> git.proxmox.com Git - mirror_qemu.git/blob - tests/virtio-net-test.c
Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2019-03-08' into...
[mirror_qemu.git] / tests / virtio-net-test.c
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
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"
17
18 #define PCI_SLOT_HP 0x06
19 #define PCI_SLOT 0x04
20
21 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
22 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
23
24 #ifndef _WIN32
25
26 static void rx_test(QVirtioDevice *dev,
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);
49 qvirtqueue_kick(dev, vq, free_head);
50
51 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
52 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
53
54 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
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
61 static void tx_test(QVirtioDevice *dev,
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);
75 qvirtqueue_kick(dev, vq, free_head);
76
77 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
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
88 static void rx_stop_cont_test(QVirtioDevice *dev,
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));
97 QDict *rsp;
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);
112 qvirtqueue_kick(dev, vq, free_head);
113
114 rsp = qmp("{ 'execute' : 'stop'}");
115 qobject_unref(rsp);
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 */
123 rsp = qmp("{ 'execute' : 'query-status'}");
124 qobject_unref(rsp);
125 rsp = qmp("{ 'execute' : 'cont'}");
126 qobject_unref(rsp);
127
128 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
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
135 static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
136 {
137 QVirtioNet *net_if = obj;
138 QVirtioDevice *dev = net_if->vdev;
139 QVirtQueue *rx = net_if->queues[0];
140 QVirtQueue *tx = net_if->queues[1];
141 int *sv = data;
142
143 rx_test(dev, t_alloc, rx, sv[0]);
144 tx_test(dev, t_alloc, tx, sv[0]);
145 }
146
147 static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
148 {
149 QVirtioNet *net_if = obj;
150 QVirtioDevice *dev = net_if->vdev;
151 QVirtQueue *rx = net_if->queues[0];
152 int *sv = data;
153
154 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
155 }
156
157 #endif
158
159 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
160 {
161 const char *arch = qtest_get_arch();
162
163 qtest_qmp_device_add("virtio-net-pci", "net1",
164 "{'addr': %s}", stringify(PCI_SLOT_HP));
165
166 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
167 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
168 }
169 }
170
171 static void virtio_net_test_cleanup(void *sockets)
172 {
173 int *sv = sockets;
174
175 close(sv[0]);
176 qos_invalidate_command_line();
177 close(sv[1]);
178 g_free(sv);
179 }
180
181 static 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;
193 }
194
195 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
196 {
197 QVirtioNet *dev = obj;
198 QVirtQueue *vq = dev->queues[1];
199 uint64_t req_addr;
200 uint32_t free_head;
201 size_t alloc_size = (size_t)data / 64;
202 int i;
203
204 /* Bypass the limitation by pointing several descriptors to a single
205 * smaller area */
206 req_addr = guest_alloc(t_alloc, alloc_size);
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 }
212 qvirtqueue_kick(dev->vdev, vq, free_head);
213
214 qvirtio_wait_used_elem(dev->vdev, vq, free_head, NULL,
215 QVIRTIO_NET_TIMEOUT_US);
216 guest_free(t_alloc, req_addr);
217 }
218
219 static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
220 {
221 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
222 return arg;
223 }
224
225 static void register_virtio_net_test(void)
226 {
227 QOSGraphTestOptions opts = {
228 .before = virtio_net_test_setup,
229 };
230
231 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
232 #ifndef _WIN32
233 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
234 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
235 #endif
236
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);
243 }
244
245 libqos_init(register_virtio_net_test);