]> git.proxmox.com Git - mirror_qemu.git/blame - tests/virtio-net-test.c
hyperv: rename kvm_hv_sint_route_set_sint
[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
JW
12#include "qemu-common.h"
13#include "qemu/sockets.h"
2af40254 14#include "qemu/iov.h"
a980f7f2 15#include "libqos/libqos-pc.h"
30ca440e 16#include "libqos/libqos-spapr.h"
2af40254
JW
17#include "libqos/virtio.h"
18#include "libqos/virtio-pci.h"
452fcdbc 19#include "qapi/qmp/qdict.h"
2af40254
JW
20#include "qemu/bswap.h"
21#include "hw/virtio/virtio-net.h"
8ac9e205 22#include "standard-headers/linux/virtio_ids.h"
ee3b850a 23#include "standard-headers/linux/virtio_ring.h"
9224709b
IM
24
25#define PCI_SLOT_HP 0x06
2af40254
JW
26#define PCI_SLOT 0x04
27#define PCI_FN 0x00
b815ec5e 28
2af40254
JW
29#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
30#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31
32static void test_end(void)
33{
34 qtest_end();
35}
36
37#ifndef _WIN32
38
39static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
40{
41 QVirtioPCIDevice *dev;
42
8ac9e205 43 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
2af40254 44 g_assert(dev != NULL);
8ac9e205 45 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
2af40254
JW
46
47 qvirtio_pci_device_enable(dev);
6b9cdf4c
LV
48 qvirtio_reset(&dev->vdev);
49 qvirtio_set_acknowledge(&dev->vdev);
50 qvirtio_set_driver(&dev->vdev);
2af40254
JW
51
52 return dev;
53}
54
a980f7f2 55static QOSState *pci_test_start(int socket)
2af40254 56{
3d95fb97 57 QOSState *qs;
30ca440e 58 const char *arch = qtest_get_arch();
a980f7f2
LV
59 const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
60 "virtio-net-pci,netdev=hs0";
2af40254 61
30ca440e 62 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
3d95fb97
EB
63 qs = qtest_pc_boot(cmd, socket);
64 } else if (strcmp(arch, "ppc64") == 0) {
65 qs = qtest_spapr_boot(cmd, socket);
66 } else {
67 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
68 exit(EXIT_FAILURE);
30ca440e 69 }
3d95fb97
EB
70 global_qtest = qs->qts;
71 return qs;
2af40254
JW
72}
73
6b9cdf4c 74static void driver_init(QVirtioDevice *dev)
2af40254
JW
75{
76 uint32_t features;
77
6b9cdf4c 78 features = qvirtio_get_features(dev);
2af40254 79 features = features & ~(QVIRTIO_F_BAD_FEATURE |
ee3b850a
SH
80 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
81 (1u << VIRTIO_RING_F_EVENT_IDX));
6b9cdf4c 82 qvirtio_set_features(dev, features);
2af40254 83
6b9cdf4c 84 qvirtio_set_driver_ok(dev);
2af40254
JW
85}
86
6b9cdf4c 87static void rx_test(QVirtioDevice *dev,
2af40254
JW
88 QGuestAllocator *alloc, QVirtQueue *vq,
89 int socket)
90{
91 uint64_t req_addr;
92 uint32_t free_head;
93 char test[] = "TEST";
94 char buffer[64];
95 int len = htonl(sizeof(test));
96 struct iovec iov[] = {
97 {
98 .iov_base = &len,
99 .iov_len = sizeof(len),
100 }, {
101 .iov_base = test,
102 .iov_len = sizeof(test),
103 },
104 };
105 int ret;
106
107 req_addr = guest_alloc(alloc, 64);
108
109 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
6b9cdf4c 110 qvirtqueue_kick(dev, vq, free_head);
2af40254
JW
111
112 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
113 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
114
be3a6781 115 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
2af40254
JW
116 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
117 g_assert_cmpstr(buffer, ==, "TEST");
118
119 guest_free(alloc, req_addr);
120}
121
6b9cdf4c 122static void tx_test(QVirtioDevice *dev,
2af40254
JW
123 QGuestAllocator *alloc, QVirtQueue *vq,
124 int socket)
125{
126 uint64_t req_addr;
127 uint32_t free_head;
128 uint32_t len;
129 char buffer[64];
130 int ret;
131
132 req_addr = guest_alloc(alloc, 64);
133 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
134
135 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
6b9cdf4c 136 qvirtqueue_kick(dev, vq, free_head);
2af40254 137
be3a6781 138 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
2af40254
JW
139 guest_free(alloc, req_addr);
140
141 ret = qemu_recv(socket, &len, sizeof(len), 0);
142 g_assert_cmpint(ret, ==, sizeof(len));
143 len = ntohl(len);
144
145 ret = qemu_recv(socket, buffer, len, 0);
146 g_assert_cmpstr(buffer, ==, "TEST");
147}
148
6b9cdf4c 149static void rx_stop_cont_test(QVirtioDevice *dev,
8887f84c
JW
150 QGuestAllocator *alloc, QVirtQueue *vq,
151 int socket)
152{
153 uint64_t req_addr;
154 uint32_t free_head;
155 char test[] = "TEST";
156 char buffer[64];
157 int len = htonl(sizeof(test));
1ec3b71c 158 QDict *rsp;
8887f84c
JW
159 struct iovec iov[] = {
160 {
161 .iov_base = &len,
162 .iov_len = sizeof(len),
163 }, {
164 .iov_base = test,
165 .iov_len = sizeof(test),
166 },
167 };
168 int ret;
169
170 req_addr = guest_alloc(alloc, 64);
171
172 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
6b9cdf4c 173 qvirtqueue_kick(dev, vq, free_head);
8887f84c 174
1ec3b71c 175 rsp = qmp("{ 'execute' : 'stop'}");
cb3e7f08 176 qobject_unref(rsp);
8887f84c
JW
177
178 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
179 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
180
181 /* We could check the status, but this command is more importantly to
182 * ensure the packet data gets queued in QEMU, before we do 'cont'.
183 */
1ec3b71c 184 rsp = qmp("{ 'execute' : 'query-status'}");
cb3e7f08 185 qobject_unref(rsp);
1ec3b71c 186 rsp = qmp("{ 'execute' : 'cont'}");
cb3e7f08 187 qobject_unref(rsp);
8887f84c 188
be3a6781 189 qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
8887f84c
JW
190 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
191 g_assert_cmpstr(buffer, ==, "TEST");
192
193 guest_free(alloc, req_addr);
194}
195
6b9cdf4c 196static void send_recv_test(QVirtioDevice *dev,
2af40254
JW
197 QGuestAllocator *alloc, QVirtQueue *rvq,
198 QVirtQueue *tvq, int socket)
b815ec5e 199{
6b9cdf4c
LV
200 rx_test(dev, alloc, rvq, socket);
201 tx_test(dev, alloc, tvq, socket);
b815ec5e
AF
202}
203
6b9cdf4c 204static void stop_cont_test(QVirtioDevice *dev,
8887f84c
JW
205 QGuestAllocator *alloc, QVirtQueue *rvq,
206 QVirtQueue *tvq, int socket)
207{
6b9cdf4c 208 rx_stop_cont_test(dev, alloc, rvq, socket);
8887f84c
JW
209}
210
2af40254
JW
211static void pci_basic(gconstpointer data)
212{
213 QVirtioPCIDevice *dev;
a980f7f2 214 QOSState *qs;
2af40254 215 QVirtQueuePCI *tx, *rx;
6b9cdf4c 216 void (*func) (QVirtioDevice *dev,
2af40254
JW
217 QGuestAllocator *alloc,
218 QVirtQueue *rvq,
219 QVirtQueue *tvq,
220 int socket) = data;
221 int sv[2], ret;
222
223 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
224 g_assert_cmpint(ret, !=, -1);
225
a980f7f2
LV
226 qs = pci_test_start(sv[1]);
227 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
2af40254 228
a980f7f2
LV
229 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
230 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
2af40254 231
6b9cdf4c 232 driver_init(&dev->vdev);
a980f7f2 233 func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
2af40254
JW
234
235 /* End test */
236 close(sv[0]);
a980f7f2
LV
237 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
238 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
2af40254 239 qvirtio_pci_device_disable(dev);
1ec3b71c 240 g_free(dev->pdev);
2af40254 241 g_free(dev);
a980f7f2 242 qtest_shutdown(qs);
2af40254
JW
243}
244#endif
245
9224709b
IM
246static void hotplug(void)
247{
30ca440e
LV
248 const char *arch = qtest_get_arch();
249
2af40254
JW
250 qtest_start("-device virtio-net-pci");
251
82cab70b
MA
252 qtest_qmp_device_add("virtio-net-pci", "net1",
253 "{'addr': %s}", stringify(PCI_SLOT_HP));
30ca440e
LV
254
255 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
256 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
257 }
2af40254
JW
258
259 test_end();
9224709b
IM
260}
261
b815ec5e
AF
262int main(int argc, char **argv)
263{
b815ec5e 264 g_test_init(&argc, &argv, NULL);
2af40254
JW
265#ifndef _WIN32
266 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
8887f84c
JW
267 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
268 stop_cont_test, pci_basic);
2af40254 269#endif
9224709b 270 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
b815ec5e 271
9be38598 272 return g_test_run();
b815ec5e 273}