2 * QTest testcase for VirtIO NIC
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
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.
10 #include "qemu/osdep.h"
12 #include "qemu-common.h"
13 #include "qemu/sockets.h"
15 #include "libqos/libqos-pc.h"
16 #include "libqos/libqos-spapr.h"
17 #include "libqos/virtio.h"
18 #include "libqos/virtio-pci.h"
19 #include "qapi/qmp/qdict.h"
20 #include "qemu/bswap.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "standard-headers/linux/virtio_ids.h"
23 #include "standard-headers/linux/virtio_ring.h"
25 #define PCI_SLOT_HP 0x06
28 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
29 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31 static void test_end(void)
38 static QVirtioPCIDevice
*virtio_net_pci_init(QPCIBus
*bus
, int slot
)
40 QVirtioPCIDevice
*dev
;
42 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
43 g_assert(dev
!= NULL
);
44 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_NET
);
46 qvirtio_pci_device_enable(dev
);
47 qvirtio_reset(&dev
->vdev
);
48 qvirtio_set_acknowledge(&dev
->vdev
);
49 qvirtio_set_driver(&dev
->vdev
);
55 static QOSState
*pci_test_start(const char *cmd
, ...)
59 const char *arch
= qtest_get_arch();
61 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
63 qs
= qtest_pc_vboot(cmd
, ap
);
65 } else if (strcmp(arch
, "ppc64") == 0) {
67 qs
= qtest_spapr_vboot(cmd
, ap
);
70 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
73 global_qtest
= qs
->qts
;
77 static void driver_init(QVirtioDevice
*dev
)
81 features
= qvirtio_get_features(dev
);
82 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
83 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
84 (1u << VIRTIO_RING_F_EVENT_IDX
));
85 qvirtio_set_features(dev
, features
);
87 qvirtio_set_driver_ok(dev
);
90 static void rx_test(QVirtioDevice
*dev
,
91 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
98 int len
= htonl(sizeof(test
));
99 struct iovec iov
[] = {
102 .iov_len
= sizeof(len
),
105 .iov_len
= sizeof(test
),
110 req_addr
= guest_alloc(alloc
, 64);
112 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
113 qvirtqueue_kick(dev
, vq
, free_head
);
115 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
116 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
118 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
119 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
120 g_assert_cmpstr(buffer
, ==, "TEST");
122 guest_free(alloc
, req_addr
);
125 static void tx_test(QVirtioDevice
*dev
,
126 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
135 req_addr
= guest_alloc(alloc
, 64);
136 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
138 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
139 qvirtqueue_kick(dev
, vq
, free_head
);
141 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
142 guest_free(alloc
, req_addr
);
144 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
145 g_assert_cmpint(ret
, ==, sizeof(len
));
148 ret
= qemu_recv(socket
, buffer
, len
, 0);
149 g_assert_cmpstr(buffer
, ==, "TEST");
152 static void rx_stop_cont_test(QVirtioDevice
*dev
,
153 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
158 char test
[] = "TEST";
160 int len
= htonl(sizeof(test
));
162 struct iovec iov
[] = {
165 .iov_len
= sizeof(len
),
168 .iov_len
= sizeof(test
),
173 req_addr
= guest_alloc(alloc
, 64);
175 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
176 qvirtqueue_kick(dev
, vq
, free_head
);
178 rsp
= qmp("{ 'execute' : 'stop'}");
181 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
182 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
184 /* We could check the status, but this command is more importantly to
185 * ensure the packet data gets queued in QEMU, before we do 'cont'.
187 rsp
= qmp("{ 'execute' : 'query-status'}");
189 rsp
= qmp("{ 'execute' : 'cont'}");
192 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
193 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
194 g_assert_cmpstr(buffer
, ==, "TEST");
196 guest_free(alloc
, req_addr
);
199 static void send_recv_test(QVirtioDevice
*dev
,
200 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
201 QVirtQueue
*tvq
, int socket
)
203 rx_test(dev
, alloc
, rvq
, socket
);
204 tx_test(dev
, alloc
, tvq
, socket
);
207 static void stop_cont_test(QVirtioDevice
*dev
,
208 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
209 QVirtQueue
*tvq
, int socket
)
211 rx_stop_cont_test(dev
, alloc
, rvq
, socket
);
214 static void pci_basic(gconstpointer data
)
216 QVirtioPCIDevice
*dev
;
218 QVirtQueuePCI
*tx
, *rx
;
219 void (*func
) (QVirtioDevice
*dev
,
220 QGuestAllocator
*alloc
,
226 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
227 g_assert_cmpint(ret
, !=, -1);
229 qs
= pci_test_start("-netdev socket,fd=%d,id=hs0 -device "
230 "virtio-net-pci,netdev=hs0", sv
[1]);
231 dev
= virtio_net_pci_init(qs
->pcibus
, PCI_SLOT
);
233 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
234 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 1);
236 driver_init(&dev
->vdev
);
237 func(&dev
->vdev
, qs
->alloc
, &rx
->vq
, &tx
->vq
, sv
[0]);
241 qvirtqueue_cleanup(dev
->vdev
.bus
, &tx
->vq
, qs
->alloc
);
242 qvirtqueue_cleanup(dev
->vdev
.bus
, &rx
->vq
, qs
->alloc
);
243 qvirtio_pci_device_disable(dev
);
249 static void large_tx(gconstpointer data
)
251 QVirtioPCIDevice
*dev
;
253 QVirtQueuePCI
*tx
, *rx
;
257 size_t alloc_size
= (size_t)data
/ 64;
260 qs
= pci_test_start("-netdev hubport,id=hp0,hubid=0 "
261 "-device virtio-net-pci,netdev=hp0");
262 dev
= virtio_net_pci_init(qs
->pcibus
, PCI_SLOT
);
264 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
265 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 1);
267 driver_init(&dev
->vdev
);
270 /* Bypass the limitation by pointing several descriptors to a single
272 req_addr
= guest_alloc(qs
->alloc
, alloc_size
);
273 free_head
= qvirtqueue_add(vq
, req_addr
, alloc_size
, false, true);
275 for (i
= 0; i
< 64; i
++) {
276 qvirtqueue_add(vq
, req_addr
, alloc_size
, false, i
!= 63);
278 qvirtqueue_kick(&dev
->vdev
, vq
, free_head
);
280 qvirtio_wait_used_elem(&dev
->vdev
, vq
, free_head
, NULL
,
281 QVIRTIO_NET_TIMEOUT_US
);
283 qvirtqueue_cleanup(dev
->vdev
.bus
, &tx
->vq
, qs
->alloc
);
284 qvirtqueue_cleanup(dev
->vdev
.bus
, &rx
->vq
, qs
->alloc
);
285 qvirtio_pci_device_disable(dev
);
292 static void hotplug(void)
294 const char *arch
= qtest_get_arch();
296 qtest_start("-device virtio-net-pci");
298 qtest_qmp_device_add("virtio-net-pci", "net1",
299 "{'addr': %s}", stringify(PCI_SLOT_HP
));
301 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
302 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
308 int main(int argc
, char **argv
)
310 g_test_init(&argc
, &argv
, NULL
);
312 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test
, pci_basic
);
313 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
314 stop_cont_test
, pci_basic
);
315 qtest_add_data_func("/virtio/net/pci/large_tx_uint_max",
316 (gconstpointer
)UINT_MAX
, large_tx
);
317 qtest_add_data_func("/virtio/net/pci/large_tx_net_bufsize",
318 (gconstpointer
)NET_BUFSIZE
, large_tx
);
320 qtest_add_func("/virtio/net/pci/hotplug", hotplug
);