blob: c58e670e2fe97fa67ac5b5d96697cc33b7a32c8b [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
18#define PCI_SLOT_HP 0x06
Jason Wang2af40252015-07-17 15:25:53 +080019#define PCI_SLOT 0x04
Andreas Färberb815ec52014-02-09 04:13:37 +010020
Jason Wang2af40252015-07-17 15:25:53 +080021#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
22#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
23
Jason Wang2af40252015-07-17 15:25:53 +080024#ifndef _WIN32
25
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020026static void rx_test(QVirtioDevice *dev,
Jason Wang2af40252015-07-17 15:25:53 +080027 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020049 qvirtqueue_kick(dev, vq, free_head);
Jason Wang2af40252015-07-17 15:25:53 +080050
51 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
52 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
53
Greg Kurzbe3a6782018-02-01 21:21:28 +010054 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang2af40252015-07-17 15:25:53 +080055 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
56 g_assert_cmpstr(buffer, ==, "TEST");
57
58 guest_free(alloc, req_addr);
59}
60
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020061static void tx_test(QVirtioDevice *dev,
Jason Wang2af40252015-07-17 15:25:53 +080062 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020075 qvirtqueue_kick(dev, vq, free_head);
Jason Wang2af40252015-07-17 15:25:53 +080076
Greg Kurzbe3a6782018-02-01 21:21:28 +010077 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang2af40252015-07-17 15:25:53 +080078 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
Laurent Vivier6b9cdf42016-10-17 12:30:21 +020088static void rx_stop_cont_test(QVirtioDevice *dev,
Jason Wang8887f842015-07-17 15:25:54 +080089 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));
Marc-André Lureau1ec3b712016-07-27 01:15:20 +040097 QDict *rsp;
Jason Wang8887f842015-07-17 15:25:54 +080098 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);
Laurent Vivier6b9cdf42016-10-17 12:30:21 +0200112 qvirtqueue_kick(dev, vq, free_head);
Jason Wang8887f842015-07-17 15:25:54 +0800113
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400114 rsp = qmp("{ 'execute' : 'stop'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200115 qobject_unref(rsp);
Jason Wang8887f842015-07-17 15:25:54 +0800116
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 */
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400123 rsp = qmp("{ 'execute' : 'query-status'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200124 qobject_unref(rsp);
Marc-André Lureau1ec3b712016-07-27 01:15:20 +0400125 rsp = qmp("{ 'execute' : 'cont'}");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200126 qobject_unref(rsp);
Jason Wang8887f842015-07-17 15:25:54 +0800127
Greg Kurzbe3a6782018-02-01 21:21:28 +0100128 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
Jason Wang8887f842015-07-17 15:25:54 +0800129 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
130 g_assert_cmpstr(buffer, ==, "TEST");
131
132 guest_free(alloc, req_addr);
133}
134
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200135static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
Jason Wang2af40252015-07-17 15:25:53 +0800136{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200137 QVirtioNet *net_if = obj;
138 QVirtioDevice *dev = net_if->vdev;
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +0200139 QVirtQueue *rx = net_if->queues[0];
140 QVirtQueue *tx = net_if->queues[1];
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200141 int *sv = data;
142
143 rx_test(dev, t_alloc, rx, sv[0]);
144 tx_test(dev, t_alloc, tx, sv[0]);
Jason Wang2af40252015-07-17 15:25:53 +0800145}
146
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200147static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
Jason Wang8887f842015-07-17 15:25:54 +0800148{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200149 QVirtioNet *net_if = obj;
150 QVirtioDevice *dev = net_if->vdev;
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +0200151 QVirtQueue *rx = net_if->queues[0];
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200152 int *sv = data;
153
154 rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
Jason Wang8887f842015-07-17 15:25:54 +0800155}
156
Jason Wang2af40252015-07-17 15:25:53 +0800157#endif
158
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200159static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
Igor Mammedov92247092014-09-26 09:28:10 +0000160{
Laurent Vivier30ca4402016-10-17 12:30:24 +0200161 const char *arch = qtest_get_arch();
162
Markus Armbruster82cab702018-08-06 08:53:35 +0200163 qtest_qmp_device_add("virtio-net-pci", "net1",
164 "{'addr': %s}", stringify(PCI_SLOT_HP));
Laurent Vivier30ca4402016-10-17 12:30:24 +0200165
166 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
167 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
168 }
Igor Mammedov92247092014-09-26 09:28:10 +0000169}
170
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200171static void virtio_net_test_cleanup(void *sockets)
Andreas Färberb815ec52014-02-09 04:13:37 +0100172{
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200173 int *sv = sockets;
Andreas Färberb815ec52014-02-09 04:13:37 +0100174
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200175 close(sv[0]);
176 qos_invalidate_command_line();
177 close(sv[1]);
178 g_free(sv);
Andreas Färberb815ec52014-02-09 04:13:37 +0100179}
Emanuele Giuseppe Esposito6ae333f2018-07-30 22:22:20 +0200180
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;
193}
194
195static 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
219static 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
225static 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
245libqos_init(register_virtio_net_test);