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