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
)
58 const char *arch
= qtest_get_arch();
59 const char *cmd
= "-netdev socket,fd=%d,id=hs0 -device "
60 "virtio-net-pci,netdev=hs0";
62 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
63 qs
= qtest_pc_boot(cmd
, socket
);
64 } else if (strcmp(arch
, "ppc64") == 0) {
65 qs
= qtest_spapr_boot(cmd
, socket
);
67 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
70 global_qtest
= qs
->qts
;
74 static void driver_init(QVirtioDevice
*dev
)
78 features
= qvirtio_get_features(dev
);
79 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
80 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
81 (1u << VIRTIO_RING_F_EVENT_IDX
));
82 qvirtio_set_features(dev
, features
);
84 qvirtio_set_driver_ok(dev
);
87 static void rx_test(QVirtioDevice
*dev
,
88 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
95 int len
= htonl(sizeof(test
));
96 struct iovec iov
[] = {
99 .iov_len
= sizeof(len
),
102 .iov_len
= sizeof(test
),
107 req_addr
= guest_alloc(alloc
, 64);
109 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
110 qvirtqueue_kick(dev
, vq
, free_head
);
112 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
113 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
115 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
116 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
117 g_assert_cmpstr(buffer
, ==, "TEST");
119 guest_free(alloc
, req_addr
);
122 static void tx_test(QVirtioDevice
*dev
,
123 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
132 req_addr
= guest_alloc(alloc
, 64);
133 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
135 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
136 qvirtqueue_kick(dev
, vq
, free_head
);
138 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
139 guest_free(alloc
, req_addr
);
141 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
142 g_assert_cmpint(ret
, ==, sizeof(len
));
145 ret
= qemu_recv(socket
, buffer
, len
, 0);
146 g_assert_cmpstr(buffer
, ==, "TEST");
149 static void rx_stop_cont_test(QVirtioDevice
*dev
,
150 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
155 char test
[] = "TEST";
157 int len
= htonl(sizeof(test
));
159 struct iovec iov
[] = {
162 .iov_len
= sizeof(len
),
165 .iov_len
= sizeof(test
),
170 req_addr
= guest_alloc(alloc
, 64);
172 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
173 qvirtqueue_kick(dev
, vq
, free_head
);
175 rsp
= qmp("{ 'execute' : 'stop'}");
178 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
179 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
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'.
184 rsp
= qmp("{ 'execute' : 'query-status'}");
186 rsp
= qmp("{ 'execute' : 'cont'}");
189 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
190 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
191 g_assert_cmpstr(buffer
, ==, "TEST");
193 guest_free(alloc
, req_addr
);
196 static void send_recv_test(QVirtioDevice
*dev
,
197 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
198 QVirtQueue
*tvq
, int socket
)
200 rx_test(dev
, alloc
, rvq
, socket
);
201 tx_test(dev
, alloc
, tvq
, socket
);
204 static void stop_cont_test(QVirtioDevice
*dev
,
205 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
206 QVirtQueue
*tvq
, int socket
)
208 rx_stop_cont_test(dev
, alloc
, rvq
, socket
);
211 static void pci_basic(gconstpointer data
)
213 QVirtioPCIDevice
*dev
;
215 QVirtQueuePCI
*tx
, *rx
;
216 void (*func
) (QVirtioDevice
*dev
,
217 QGuestAllocator
*alloc
,
223 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
224 g_assert_cmpint(ret
, !=, -1);
226 qs
= pci_test_start(sv
[1]);
227 dev
= virtio_net_pci_init(qs
->pcibus
, PCI_SLOT
);
229 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 0);
230 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&dev
->vdev
, qs
->alloc
, 1);
232 driver_init(&dev
->vdev
);
233 func(&dev
->vdev
, qs
->alloc
, &rx
->vq
, &tx
->vq
, sv
[0]);
237 qvirtqueue_cleanup(dev
->vdev
.bus
, &tx
->vq
, qs
->alloc
);
238 qvirtqueue_cleanup(dev
->vdev
.bus
, &rx
->vq
, qs
->alloc
);
239 qvirtio_pci_device_disable(dev
);
246 static void hotplug(void)
248 const char *arch
= qtest_get_arch();
250 qtest_start("-device virtio-net-pci");
252 qtest_qmp_device_add("virtio-net-pci", "net1",
253 "{'addr': %s}", stringify(PCI_SLOT_HP
));
255 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
256 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
262 int main(int argc
, char **argv
)
264 g_test_init(&argc
, &argv
, NULL
);
266 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test
, pci_basic
);
267 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
268 stop_cont_test
, pci_basic
);
270 qtest_add_func("/virtio/net/pci/hotplug", hotplug
);