blob: 163126cf07e1aa401d7d91f3bff91a889cec1cf5 [file] [log] [blame]
Andreas Färberb815ec52014-02-09 04:13:37 +01001/*
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
Peter Maydell681c28a2016-02-08 18:08:51 +000010#include "qemu/osdep.h"
Andreas Färberb815ec52014-02-09 04:13:37 +010011#include "libqtest.h"
Jason Wang2af40252015-07-17 15:25:53 +080012#include "qemu/iov.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010013#include "qapi/qmp/qdict.h"
Jason Wang2af40252015-07-17 15:25:53 +080014#include "hw/virtio/virtio-net.h"
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +020015#include "libqos/qgraph.h"
16#include "libqos/virtio-net.h"
Igor Mammedov92247092014-09-26 09:28:10 +000017
Paolo Bonzini8b159692019-03-12 17:31:30 +010018#ifndef ETH_P_RARP
19#define ETH_P_RARP 0x8035
20#endif
21
Igor Mammedov92247092014-09-26 09:28:10 +000022#define PCI_SLOT_HP 0x06
Jason Wang2af40252015-07-17 15:25:53 +080023#define PCI_SLOT 0x04
Andreas Färberb815ec52014-02-09 04:13:37 +010024
Jason Wang2af40252015-07-17 15:25:53 +080025#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
26#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
27
Jason Wang2af40252015-07-17 15:25:53 +080028#ifndef _WIN32
29
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020030static void rx_test(QVirtioDevice *dev,
Jason Wang2af40252015-07-17 15:25:53 +080031 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020053 qvirtqueue_kick(dev, vq, free_head);
Jason Wang2af40252015-07-17 15:25:53 +080054
55 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
56 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
57
Greg Kurzbe3a6782018-02-01 21:21:28 +010058 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang2af40252015-07-17 15:25:53 +080059 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
60 g_assert_cmpstr(buffer, ==, "TEST");
61
62 guest_free(alloc, req_addr);
63}
64
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020065static void tx_test(QVirtioDevice *dev,
Jason Wang2af40252015-07-17 15:25:53 +080066 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020079 qvirtqueue_kick(dev, vq, free_head);
Jason Wang2af40252015-07-17 15:25:53 +080080
Greg Kurzbe3a6782018-02-01 21:21:28 +010081 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang2af40252015-07-17 15:25:53 +080082 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
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020092static void rx_stop_cont_test(QVirtioDevice *dev,
Jason Wang8887f842015-07-17 15:25:54 +080093 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));
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400101 QDict *rsp;
Jason Wang8887f842015-07-17 15:25:54 +0800102 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +0200116 qvirtqueue_kick(dev, vq, free_head);
Jason Wang8887f842015-07-17 15:25:54 +0800117
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400118 rsp = qmp("{ 'execute' : 'stop'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200119 qobject_unref(rsp);
Jason Wang8887f842015-07-17 15:25:54 +0800120
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 */
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400127 rsp = qmp("{ 'execute' : 'query-status'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200128 qobject_unref(rsp);
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400129 rsp = qmp("{ 'execute' : 'cont'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200130 qobject_unref(rsp);
Jason Wang8887f842015-07-17 15:25:54 +0800131
Greg Kurzbe3a6782018-02-01 21:21:28 +0100132 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang8887f842015-07-17 15:25:54 +0800133 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
134 g_assert_cmpstr(buffer, ==, "TEST");
135
136 guest_free(alloc, req_addr);
137}
138
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200139static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
Jason Wang2af40252015-07-17 15:25:53 +0800140{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200141 QVirtioNet *net_if = obj;
142 QVirtioDevice *dev = net_if->vdev;
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +0200143 QVirtQueue *rx = net_if->queues[0];
144 QVirtQueue *tx = net_if->queues[1];
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200145 int *sv = data;
146
147 rx_test(dev, t_alloc, rx, sv[0]);
148 tx_test(dev, t_alloc, tx, sv[0]);
Jason Wang2af40252015-07-17 15:25:53 +0800149}
150
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200151static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
Jason Wang8887f842015-07-17 15:25:54 +0800152{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200153 QVirtioNet *net_if = obj;
154 QVirtioDevice *dev = net_if->vdev;
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +0200155 QVirtQueue *rx = net_if->queues[0];
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200156 int *sv = data;
157
158 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
Jason Wang8887f842015-07-17 15:25:54 +0800159}
160
Jason Wang2af40252015-07-17 15:25:53 +0800161#endif
162
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200163static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
Igor Mammedov92247092014-09-26 09:28:10 +0000164{
Thomas Huth6ebb8d22019-04-09 14:25:57 +0200165 QVirtioPCIDevice *dev = obj;
166 QTestState *qts = dev->pdev->bus->qts;
Laurent Vivier30ca4402016-10-17 12:30:24 +0200167 const char *arch = qtest_get_arch();
168
Markus Armbruster82cab702018-08-06 08:53:35 +0200169 qtest_qmp_device_add("virtio-net-pci", "net1",
170 "{'addr': %s}", stringify(PCI_SLOT_HP));
Laurent Vivier30ca4402016-10-17 12:30:24 +0200171
172 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
Thomas Huth6ebb8d22019-04-09 14:25:57 +0200173 qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
Laurent Vivier30ca4402016-10-17 12:30:24 +0200174 }
Igor Mammedov92247092014-09-26 09:28:10 +0000175}
176
Paolo Bonzini8b159692019-03-12 17:31:30 +0100177static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
178{
179 int *sv = data;
180 char buffer[60];
181 int len;
182 QDict *rsp;
183 int ret;
184 uint16_t *proto = (uint16_t *)&buffer[12];
185
186 rsp = qmp("{ 'execute' : 'announce-self', "
187 " 'arguments': {"
188 " 'initial': 50, 'max': 550,"
189 " 'rounds': 10, 'step': 50 } }");
190 assert(!qdict_haskey(rsp, "error"));
191 qobject_unref(rsp);
192
193 /* Catch the packet and make sure it's a RARP */
194 ret = qemu_recv(sv[0], &len, sizeof(len), 0);
195 g_assert_cmpint(ret, ==, sizeof(len));
196 len = ntohl(len);
197
198 ret = qemu_recv(sv[0], buffer, len, 0);
199 g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
200}
201
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200202static void virtio_net_test_cleanup(void *sockets)
Andreas Färberb815ec52014-02-09 04:13:37 +0100203{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200204 int *sv = sockets;
Andreas Färberb815ec52014-02-09 04:13:37 +0100205
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200206 close(sv[0]);
207 qos_invalidate_command_line();
208 close(sv[1]);
209 g_free(sv);
Andreas Färberb815ec52014-02-09 04:13:37 +0100210}
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200211
212static void *virtio_net_test_setup(GString *cmd_line, void *arg)
213{
214 int ret;
215 int *sv = g_new(int, 2);
216
217 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
218 g_assert_cmpint(ret, !=, -1);
219
220 g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
221
222 g_test_queue_destroy(virtio_net_test_cleanup, sv);
223 return sv;
224}
225
226static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
227{
228 QVirtioNet *dev = obj;
229 QVirtQueue *vq = dev->queues[1];
230 uint64_t req_addr;
231 uint32_t free_head;
232 size_t alloc_size = (size_t)data / 64;
233 int i;
234
235 /* Bypass the limitation by pointing several descriptors to a single
236 * smaller area */
237 req_addr = guest_alloc(t_alloc, alloc_size);
238 free_head = qvirtqueue_add(vq, req_addr, alloc_size, false, true);
239
240 for (i = 0; i < 64; i++) {
241 qvirtqueue_add(vq, req_addr, alloc_size, false, i != 63);
242 }
243 qvirtqueue_kick(dev->vdev, vq, free_head);
244
245 qvirtio_wait_used_elem(dev->vdev, vq, free_head, NULL,
246 QVIRTIO_NET_TIMEOUT_US);
247 guest_free(t_alloc, req_addr);
248}
249
250static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
251{
252 g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
253 return arg;
254}
255
256static void register_virtio_net_test(void)
257{
258 QOSGraphTestOptions opts = {
259 .before = virtio_net_test_setup,
260 };
261
262 qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
263#ifndef _WIN32
264 qos_add_test("basic", "virtio-net", send_recv_test, &opts);
265 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
266#endif
Paolo Bonzini8b159692019-03-12 17:31:30 +0100267 qos_add_test("announce-self", "virtio-net", announce_self, &opts);
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200268
269 /* These tests do not need a loopback backend. */
270 opts.before = virtio_net_test_setup_nosocket;
271 opts.arg = (gpointer)UINT_MAX;
272 qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
273 opts.arg = (gpointer)NET_BUFSIZE;
274 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
275}
276
277libqos_init(register_virtio_net_test);