xref: /openbmc/qemu/tests/qtest/virtio-net-test.c (revision effd60c8)
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-single.h"
12 #include "qemu/iov.h"
13 #include "qemu/module.h"
14 #include "qapi/qmp/qdict.h"
15 #include "hw/virtio/virtio-net.h"
16 #include "libqos/qgraph.h"
17 #include "libqos/virtio-net.h"
18 
19 #ifndef ETH_P_RARP
20 #define ETH_P_RARP 0x8035
21 #endif
22 
23 #define PCI_SLOT_HP             0x06
24 #define PCI_SLOT                0x04
25 
26 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
27 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
28 
29 #ifndef _WIN32
30 
31 static void rx_test(QVirtioDevice *dev,
32                     QGuestAllocator *alloc, QVirtQueue *vq,
33                     int socket)
34 {
35     QTestState *qts = global_qtest;
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(qts, vq, req_addr, 64, true, false);
55     qvirtqueue_kick(qts, dev, vq, free_head);
56 
57     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
58     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
59 
60     qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
61                            QVIRTIO_NET_TIMEOUT_US);
62     memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
63     g_assert_cmpstr(buffer, ==, "TEST");
64 
65     guest_free(alloc, req_addr);
66 }
67 
68 static void tx_test(QVirtioDevice *dev,
69                     QGuestAllocator *alloc, QVirtQueue *vq,
70                     int socket)
71 {
72     QTestState *qts = global_qtest;
73     uint64_t req_addr;
74     uint32_t free_head;
75     uint32_t len;
76     char buffer[64];
77     int ret;
78 
79     req_addr = guest_alloc(alloc, 64);
80     memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
81 
82     free_head = qvirtqueue_add(qts, vq, req_addr, 64, false, false);
83     qvirtqueue_kick(qts, dev, vq, free_head);
84 
85     qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
86                            QVIRTIO_NET_TIMEOUT_US);
87     guest_free(alloc, req_addr);
88 
89     ret = recv(socket, &len, sizeof(len), 0);
90     g_assert_cmpint(ret, ==, sizeof(len));
91     len = ntohl(len);
92 
93     ret = recv(socket, buffer, len, 0);
94     g_assert_cmpint(ret, ==, len);
95     g_assert_cmpstr(buffer, ==, "TEST");
96 }
97 
98 static void rx_stop_cont_test(QVirtioDevice *dev,
99                               QGuestAllocator *alloc, QVirtQueue *vq,
100                               int socket)
101 {
102     QTestState *qts = global_qtest;
103     uint64_t req_addr;
104     uint32_t free_head;
105     char test[] = "TEST";
106     char buffer[64];
107     int len = htonl(sizeof(test));
108     QDict *rsp;
109     struct iovec iov[] = {
110         {
111             .iov_base = &len,
112             .iov_len = sizeof(len),
113         }, {
114             .iov_base = test,
115             .iov_len = sizeof(test),
116         },
117     };
118     int ret;
119 
120     req_addr = guest_alloc(alloc, 64);
121 
122     free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
123     qvirtqueue_kick(qts, dev, vq, free_head);
124 
125     rsp = qmp("{ 'execute' : 'stop'}");
126     qobject_unref(rsp);
127 
128     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
129     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
130 
131     /* We could check the status, but this command is more importantly to
132      * ensure the packet data gets queued in QEMU, before we do 'cont'.
133      */
134     rsp = qmp("{ 'execute' : 'query-status'}");
135     qobject_unref(rsp);
136     rsp = qmp("{ 'execute' : 'cont'}");
137     qobject_unref(rsp);
138 
139     qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
140                            QVIRTIO_NET_TIMEOUT_US);
141     memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
142     g_assert_cmpstr(buffer, ==, "TEST");
143 
144     guest_free(alloc, req_addr);
145 }
146 
147 static void send_recv_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     QVirtQueue *tx = net_if->queues[1];
153     int *sv = data;
154 
155     rx_test(dev, t_alloc, rx, sv[0]);
156     tx_test(dev, t_alloc, tx, sv[0]);
157 }
158 
159 static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
160 {
161     QVirtioNet *net_if = obj;
162     QVirtioDevice *dev = net_if->vdev;
163     QVirtQueue *rx = net_if->queues[0];
164     int *sv = data;
165 
166     rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
167 }
168 
169 static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
170 {
171     QVirtioPCIDevice *dev = obj;
172     QTestState *qts = dev->pdev->bus->qts;
173     const char *arch = qtest_get_arch();
174 
175     if (dev->pdev->bus->not_hotpluggable) {
176         g_test_skip("pci bus does not support hotplug");
177         return;
178     }
179 
180     qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
181                          "{'addr': %s}", stringify(PCI_SLOT_HP));
182 
183     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
184         qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
185     }
186 }
187 
188 static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
189 {
190     int *sv = data;
191     char buffer[60];
192     int len;
193     QDict *rsp;
194     int ret;
195     uint16_t *proto = (uint16_t *)&buffer[12];
196     size_t total_received = 0;
197     uint64_t start, now, last_rxt, deadline;
198 
199     /* Send a set of packets over a few second period */
200     rsp = qmp("{ 'execute' : 'announce-self', "
201                   " 'arguments': {"
202                       " 'initial': 20, 'max': 100,"
203                       " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
204     assert(!qdict_haskey(rsp, "error"));
205     qobject_unref(rsp);
206 
207     /* Catch the first packet and make sure it's a RARP */
208     ret = recv(sv[0], &len, sizeof(len), 0);
209     g_assert_cmpint(ret, ==,  sizeof(len));
210     len = ntohl(len);
211 
212     ret = recv(sv[0], buffer, len, 0);
213     g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
214 
215     /*
216      * Stop the announcement by settings rounds to 0 on the
217      * existing timer.
218      */
219     rsp = qmp("{ 'execute' : 'announce-self', "
220                   " 'arguments': {"
221                       " 'initial': 20, 'max': 100,"
222                       " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
223     assert(!qdict_haskey(rsp, "error"));
224     qobject_unref(rsp);
225 
226     /* Now make sure the packets stop */
227 
228     /* Times are in us */
229     start = g_get_monotonic_time();
230     /* 30 packets, max gap 100ms, * 4 for wiggle */
231     deadline = start + 1000 * (100 * 30 * 4);
232     last_rxt = start;
233 
234     while (true) {
235         int saved_err;
236         ret = recv(sv[0], buffer, 60, MSG_DONTWAIT);
237         saved_err = errno;
238         now = g_get_monotonic_time();
239         g_assert_cmpint(now, <, deadline);
240 
241         if (ret >= 0) {
242             if (ret) {
243                 last_rxt = now;
244             }
245             total_received += ret;
246 
247             /* Check it's not spewing loads */
248             g_assert_cmpint(total_received, <, 60 * 30 * 2);
249         } else {
250             g_assert_cmpint(saved_err, ==, EAGAIN);
251 
252             /* 400ms, i.e. 4 worst case gaps */
253             if ((now - last_rxt) > (1000 * 100 * 4)) {
254                 /* Nothings arrived for a while - must have stopped */
255                 break;
256             };
257 
258             /* 100ms */
259             g_usleep(1000 * 100);
260         }
261     };
262 }
263 
264 static void virtio_net_test_cleanup(void *sockets)
265 {
266     int *sv = sockets;
267 
268     close(sv[0]);
269     qos_invalidate_command_line();
270     close(sv[1]);
271     g_free(sv);
272 }
273 
274 static void *virtio_net_test_setup(GString *cmd_line, void *arg)
275 {
276     int ret;
277     int *sv = g_new(int, 2);
278 
279     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
280     g_assert_cmpint(ret, !=, -1);
281 
282     g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
283 
284     g_test_queue_destroy(virtio_net_test_cleanup, sv);
285     return sv;
286 }
287 
288 #endif /* _WIN32 */
289 
290 static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
291 {
292     QVirtioNet *dev = obj;
293     QVirtQueue *vq = dev->queues[1];
294     uint64_t req_addr;
295     uint32_t free_head;
296     size_t alloc_size = (size_t)data / 64;
297     QTestState *qts = global_qtest;
298     int i;
299 
300     /* Bypass the limitation by pointing several descriptors to a single
301      * smaller area */
302     req_addr = guest_alloc(t_alloc, alloc_size);
303     free_head = qvirtqueue_add(qts, vq, req_addr, alloc_size, false, true);
304 
305     for (i = 0; i < 64; i++) {
306         qvirtqueue_add(qts, vq, req_addr, alloc_size, false, i != 63);
307     }
308     qvirtqueue_kick(qts, dev->vdev, vq, free_head);
309 
310     qvirtio_wait_used_elem(qts, dev->vdev, vq, free_head, NULL,
311                            QVIRTIO_NET_TIMEOUT_US);
312     guest_free(t_alloc, req_addr);
313 }
314 
315 static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
316 {
317     g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
318     return arg;
319 }
320 
321 static void register_virtio_net_test(void)
322 {
323     QOSGraphTestOptions opts = { 0 };
324 
325 #ifndef _WIN32
326     opts.before = virtio_net_test_setup;
327     qos_add_test("hotplug", "virtio-net-pci", hotplug, &opts);
328     qos_add_test("basic", "virtio-net", send_recv_test, &opts);
329     qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
330     qos_add_test("announce-self", "virtio-net", announce_self, &opts);
331 #endif
332 
333     /* These tests do not need a loopback backend.  */
334     opts.before = virtio_net_test_setup_nosocket;
335     opts.arg = (gpointer)UINT_MAX;
336     qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
337     opts.arg = (gpointer)NET_BUFSIZE;
338     qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
339 }
340 
341 libqos_init(register_virtio_net_test);
342