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
29 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
30 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
32 static void test_end(void)
39 static QVirtioPCIDevice
*virtio_net_pci_init(QPCIBus
*bus
, int slot
)
41 QVirtioPCIDevice
*dev
;
43 dev
= qvirtio_pci_device_find(bus
, VIRTIO_ID_NET
);
44 g_assert(dev
!= NULL
);
45 g_assert_cmphex(dev
->vdev
.device_type
, ==, VIRTIO_ID_NET
);
47 qvirtio_pci_device_enable(dev
);
48 qvirtio_reset(&dev
->vdev
);
49 qvirtio_set_acknowledge(&dev
->vdev
);
50 qvirtio_set_driver(&dev
->vdev
);
55 static QOSState
*pci_test_start(int socket
)
57 const char *arch
= qtest_get_arch();
58 const char *cmd
= "-netdev socket,fd=%d,id=hs0 -device "
59 "virtio-net-pci,netdev=hs0";
61 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
62 return qtest_pc_boot(cmd
, socket
);
64 if (strcmp(arch
, "ppc64") == 0) {
65 return qtest_spapr_boot(cmd
, socket
);
67 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
71 static void driver_init(QVirtioDevice
*dev
)
75 features
= qvirtio_get_features(dev
);
76 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
77 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
78 (1u << VIRTIO_RING_F_EVENT_IDX
));
79 qvirtio_set_features(dev
, features
);
81 qvirtio_set_driver_ok(dev
);
84 static void rx_test(QVirtioDevice
*dev
,
85 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
92 int len
= htonl(sizeof(test
));
93 struct iovec iov
[] = {
96 .iov_len
= sizeof(len
),
99 .iov_len
= sizeof(test
),
104 req_addr
= guest_alloc(alloc
, 64);
106 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
107 qvirtqueue_kick(dev
, vq
, free_head
);
109 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
110 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
112 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
113 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
114 g_assert_cmpstr(buffer
, ==, "TEST");
116 guest_free(alloc
, req_addr
);
119 static void tx_test(QVirtioDevice
*dev
,
120 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
129 req_addr
= guest_alloc(alloc
, 64);
130 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
132 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
133 qvirtqueue_kick(dev
, vq
, free_head
);
135 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
136 guest_free(alloc
, req_addr
);
138 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
139 g_assert_cmpint(ret
, ==, sizeof(len
));
142 ret
= qemu_recv(socket
, buffer
, len
, 0);
143 g_assert_cmpstr(buffer
, ==, "TEST");
146 static void rx_stop_cont_test(QVirtioDevice
*dev
,
147 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
152 char test
[] = "TEST";
154 int len
= htonl(sizeof(test
));
156 struct iovec iov
[] = {
159 .iov_len
= sizeof(len
),
162 .iov_len
= sizeof(test
),
167 req_addr
= guest_alloc(alloc
, 64);
169 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
170 qvirtqueue_kick(dev
, vq
, free_head
);
172 rsp
= qmp("{ 'execute' : 'stop'}");
175 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
176 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
178 /* We could check the status, but this command is more importantly to
179 * ensure the packet data gets queued in QEMU, before we do 'cont'.
181 rsp
= qmp("{ 'execute' : 'query-status'}");
183 rsp
= qmp("{ 'execute' : 'cont'}");
186 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
187 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
188 g_assert_cmpstr(buffer
, ==, "TEST");
190 guest_free(alloc
, req_addr
);
193 static void send_recv_test(QVirtioDevice
*dev
,
194 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
195 QVirtQueue
*tvq
, int socket
)
197 rx_test(dev
, alloc
, rvq
, socket
);
198 tx_test(dev
, alloc
, tvq
, socket
);
201 static void stop_cont_test(QVirtioDevice
*dev
,
202 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
203 QVirtQueue
*tvq
, int socket
)
205 rx_stop_cont_test(dev
, alloc
, rvq
, socket
);
208 static void pci_basic(gconstpointer data
)
210 QVirtioPCIDevice
*dev
;
212 QVirtQueuePCI
*tx
, *rx
;
213 void (*func
) (QVirtioDevice
*dev
,
214 QGuestAllocator
*alloc
,
220 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
221 g_assert_cmpint(ret
, !=, -1);
223 qs
= pci_test_start(sv
[1]);
224 dev
= virtio_net_pci_init(qs
->pcibus
, PCI_SLOT
);
226 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
227 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 1);
229 driver_init(&dev
->vdev
);
230 func(&dev
->vdev
, qs
->alloc
, &rx
->vq
, &tx
->vq
, sv
[0]);
234 qvirtqueue_cleanup(dev
->vdev
.bus
, &tx
->vq
, qs
->alloc
);
235 qvirtqueue_cleanup(dev
->vdev
.bus
, &rx
->vq
, qs
->alloc
);
236 qvirtio_pci_device_disable(dev
);
243 static void hotplug(void)
245 const char *arch
= qtest_get_arch();
247 qtest_start("-device virtio-net-pci");
249 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP
, NULL
);
251 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
252 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
258 int main(int argc
, char **argv
)
260 g_test_init(&argc
, &argv
, NULL
);
262 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test
, pci_basic
);
263 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
264 stop_cont_test
, pci_basic
);
266 qtest_add_func("/virtio/net/pci/hotplug", hotplug
);