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/pci-pc.h"
16 #include "libqos/virtio.h"
17 #include "libqos/virtio-pci.h"
18 #include "libqos/malloc.h"
19 #include "libqos/malloc-pc.h"
20 #include "libqos/malloc-generic.h"
21 #include "qemu/bswap.h"
22 #include "hw/virtio/virtio-net.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
, QVIRTIO_NET_DEVICE_ID
);
43 g_assert(dev
!= NULL
);
44 g_assert_cmphex(dev
->vdev
.device_type
, ==, QVIRTIO_NET_DEVICE_ID
);
46 qvirtio_pci_device_enable(dev
);
47 qvirtio_reset(&qvirtio_pci
, &dev
->vdev
);
48 qvirtio_set_acknowledge(&qvirtio_pci
, &dev
->vdev
);
49 qvirtio_set_driver(&qvirtio_pci
, &dev
->vdev
);
54 static QPCIBus
*pci_test_start(int socket
)
58 cmdline
= g_strdup_printf("-netdev socket,fd=%d,id=hs0 -device "
59 "virtio-net-pci,netdev=hs0", socket
);
63 return qpci_init_pc();
66 static void driver_init(const QVirtioBus
*bus
, QVirtioDevice
*dev
)
70 features
= qvirtio_get_features(bus
, dev
);
71 features
= features
& ~(QVIRTIO_F_BAD_FEATURE
|
72 QVIRTIO_F_RING_INDIRECT_DESC
|
73 QVIRTIO_F_RING_EVENT_IDX
);
74 qvirtio_set_features(bus
, dev
, features
);
76 qvirtio_set_driver_ok(bus
, dev
);
79 static void rx_test(const QVirtioBus
*bus
, QVirtioDevice
*dev
,
80 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
87 int len
= htonl(sizeof(test
));
88 struct iovec iov
[] = {
91 .iov_len
= sizeof(len
),
94 .iov_len
= sizeof(test
),
99 req_addr
= guest_alloc(alloc
, 64);
101 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
102 qvirtqueue_kick(bus
, dev
, vq
, free_head
);
104 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
105 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
107 qvirtio_wait_queue_isr(bus
, dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
108 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
109 g_assert_cmpstr(buffer
, ==, "TEST");
111 guest_free(alloc
, req_addr
);
114 static void tx_test(const QVirtioBus
*bus
, QVirtioDevice
*dev
,
115 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
124 req_addr
= guest_alloc(alloc
, 64);
125 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
127 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
128 qvirtqueue_kick(bus
, dev
, vq
, free_head
);
130 qvirtio_wait_queue_isr(bus
, dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
131 guest_free(alloc
, req_addr
);
133 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
134 g_assert_cmpint(ret
, ==, sizeof(len
));
137 ret
= qemu_recv(socket
, buffer
, len
, 0);
138 g_assert_cmpstr(buffer
, ==, "TEST");
141 static void rx_stop_cont_test(const QVirtioBus
*bus
, QVirtioDevice
*dev
,
142 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
147 char test
[] = "TEST";
149 int len
= htonl(sizeof(test
));
150 struct iovec iov
[] = {
153 .iov_len
= sizeof(len
),
156 .iov_len
= sizeof(test
),
161 req_addr
= guest_alloc(alloc
, 64);
163 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
164 qvirtqueue_kick(bus
, dev
, vq
, free_head
);
166 qmp("{ 'execute' : 'stop'}");
168 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
169 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
171 /* We could check the status, but this command is more importantly to
172 * ensure the packet data gets queued in QEMU, before we do 'cont'.
174 qmp("{ 'execute' : 'query-status'}");
175 qmp("{ 'execute' : 'cont'}");
177 qvirtio_wait_queue_isr(bus
, dev
, vq
, QVIRTIO_NET_TIMEOUT_US
);
178 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
179 g_assert_cmpstr(buffer
, ==, "TEST");
181 guest_free(alloc
, req_addr
);
184 static void send_recv_test(const QVirtioBus
*bus
, QVirtioDevice
*dev
,
185 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
186 QVirtQueue
*tvq
, int socket
)
188 rx_test(bus
, dev
, alloc
, rvq
, socket
);
189 tx_test(bus
, dev
, alloc
, tvq
, socket
);
192 static void stop_cont_test(const QVirtioBus
*bus
, QVirtioDevice
*dev
,
193 QGuestAllocator
*alloc
, QVirtQueue
*rvq
,
194 QVirtQueue
*tvq
, int socket
)
196 rx_stop_cont_test(bus
, dev
, alloc
, rvq
, socket
);
199 static void pci_basic(gconstpointer data
)
201 QVirtioPCIDevice
*dev
;
203 QVirtQueuePCI
*tx
, *rx
;
204 QGuestAllocator
*alloc
;
205 void (*func
) (const QVirtioBus
*bus
,
207 QGuestAllocator
*alloc
,
213 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
214 g_assert_cmpint(ret
, !=, -1);
216 bus
= pci_test_start(sv
[1]);
217 dev
= virtio_net_pci_init(bus
, PCI_SLOT
);
219 alloc
= pc_alloc_init();
220 rx
= (QVirtQueuePCI
*)qvirtqueue_setup(&qvirtio_pci
, &dev
->vdev
,
222 tx
= (QVirtQueuePCI
*)qvirtqueue_setup(&qvirtio_pci
, &dev
->vdev
,
225 driver_init(&qvirtio_pci
, &dev
->vdev
);
226 func(&qvirtio_pci
, &dev
->vdev
, alloc
, &rx
->vq
, &tx
->vq
, sv
[0]);
230 guest_free(alloc
, tx
->vq
.desc
);
231 pc_alloc_uninit(alloc
);
232 qvirtio_pci_device_disable(dev
);
239 static void hotplug(void)
241 qtest_start("-device virtio-net-pci");
243 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP
, NULL
);
244 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
249 int main(int argc
, char **argv
)
253 g_test_init(&argc
, &argv
, NULL
);
255 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test
, pci_basic
);
256 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
257 stop_cont_test
, pci_basic
);
259 qtest_add_func("/virtio/net/pci/hotplug", hotplug
);