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 "qemu/bswap.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "standard-headers/linux/virtio_ids.h"
22 #include "standard-headers/linux/virtio_ring.h"
24 #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
);
54 static QOSState
*pci_test_start(int socket
)
56 const char *arch
= qtest_get_arch();
57 const char *cmd
= "-netdev socket,fd=%d,id=hs0 -device "
58 "virtio-net-pci,netdev=hs0";
60 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
61 return qtest_pc_boot(cmd
, socket
);
63 if (strcmp(arch
, "ppc64") == 0) {
64 return qtest_spapr_boot(cmd
, socket
);
66 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
70 static void driver_init(QVirtioDevice
*dev
)
74 features
= qvirtio_get_features(dev
);
75 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
76 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
77 (1u << VIRTIO_RING_F_EVENT_IDX
));
78 qvirtio_set_features(dev
, features
);
80 qvirtio_set_driver_ok(dev
);
83 static void rx_test(QVirtioDevice
*dev
,
84 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
91 int len
= htonl(sizeof(test
));
92 struct iovec iov
[] = {
95 .iov_len
= sizeof(len
),
98 .iov_len
= sizeof(test
),
103 req_addr
= guest_alloc(alloc
, 64);
105 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
106 qvirtqueue_kick(dev
, vq
, free_head
);
108 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
109 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
111 qvirtio_wait_queue_isr(dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
112 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
113 g_assert_cmpstr(buffer
, ==, "TEST");
115 guest_free(alloc
, req_addr
);
118 static void tx_test(QVirtioDevice
*dev
,
119 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
128 req_addr
= guest_alloc(alloc
, 64);
129 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
131 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
132 qvirtqueue_kick(dev
, vq
, free_head
);
134 qvirtio_wait_queue_isr(dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
135 guest_free(alloc
, req_addr
);
137 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
138 g_assert_cmpint(ret
, ==, sizeof(len
));
141 ret
= qemu_recv(socket
, buffer
, len
, 0);
142 g_assert_cmpstr(buffer
, ==, "TEST");
145 static void rx_stop_cont_test(QVirtioDevice
*dev
,
146 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
151 char test
[] = "TEST";
153 int len
= htonl(sizeof(test
));
155 struct iovec iov
[] = {
158 .iov_len
= sizeof(len
),
161 .iov_len
= sizeof(test
),
166 req_addr
= guest_alloc(alloc
, 64);
168 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
169 qvirtqueue_kick(dev
, vq
, free_head
);
171 rsp
= qmp("{ 'execute' : 'stop'}");
174 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
175 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
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'.
180 rsp
= qmp("{ 'execute' : 'query-status'}");
182 rsp
= qmp("{ 'execute' : 'cont'}");
185 qvirtio_wait_queue_isr(dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
186 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
187 g_assert_cmpstr(buffer
, ==, "TEST");
189 guest_free(alloc
, req_addr
);
192 static void send_recv_test(QVirtioDevice
*dev
,
193 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
194 QVirtQueue
*tvq
, int socket
)
196 rx_test(dev
, alloc
, rvq
, socket
);
197 tx_test(dev
, alloc
, tvq
, socket
);
200 static void stop_cont_test(QVirtioDevice
*dev
,
201 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
202 QVirtQueue
*tvq
, int socket
)
204 rx_stop_cont_test(dev
, alloc
, rvq
, socket
);
207 static void pci_basic(gconstpointer data
)
209 QVirtioPCIDevice
*dev
;
211 QVirtQueuePCI
*tx
, *rx
;
212 void (*func
) (QVirtioDevice
*dev
,
213 QGuestAllocator
*alloc
,
219 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
220 g_assert_cmpint(ret
, !=, -1);
222 qs
= pci_test_start(sv
[1]);
223 dev
= virtio_net_pci_init(qs
->pcibus
, PCI_SLOT
);
225 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
226 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 1);
228 driver_init(&dev
->vdev
);
229 func(&dev
->vdev
, qs
->alloc
, &rx
->vq
, &tx
->vq
, sv
[0]);
233 qvirtqueue_cleanup(dev
->vdev
.bus
, &tx
->vq
, qs
->alloc
);
234 qvirtqueue_cleanup(dev
->vdev
.bus
, &rx
->vq
, qs
->alloc
);
235 qvirtio_pci_device_disable(dev
);
242 static void hotplug(void)
244 const char *arch
= qtest_get_arch();
246 qtest_start("-device virtio-net-pci");
248 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP
, NULL
);
250 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
251 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
257 int main(int argc
, char **argv
)
259 g_test_init(&argc
, &argv
, NULL
);
261 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test
, pci_basic
);
262 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
263 stop_cont_test
, pci_basic
);
265 qtest_add_func("/virtio/net/pci/hotplug", hotplug
);