hw/arm/virt: introduce DEFINE_VIRT_MACHINE
[qemu/ar7.git] / tests / virtio-net-test.c
blobe5c144818eb5ebcc8c70f61d28e8589284cef9ed
1 /*
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.
8 */
10 #include "qemu/osdep.h"
11 #include "libqtest.h"
12 #include "qemu-common.h"
13 #include "qemu/sockets.h"
14 #include "qemu/iov.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
25 #define PCI_SLOT 0x04
26 #define PCI_FN 0x00
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)
33 qtest_end();
36 #ifndef _WIN32
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);
51 return dev;
54 static QPCIBus *pci_test_start(int socket)
56 char *cmdline;
58 cmdline = g_strdup_printf("-netdev socket,fd=%d,id=hs0 -device "
59 "virtio-net-pci,netdev=hs0", socket);
60 qtest_start(cmdline);
61 g_free(cmdline);
63 return qpci_init_pc();
66 static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
68 uint32_t features;
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,
81 int socket)
83 uint64_t req_addr;
84 uint32_t free_head;
85 char test[] = "TEST";
86 char buffer[64];
87 int len = htonl(sizeof(test));
88 struct iovec iov[] = {
90 .iov_base = &len,
91 .iov_len = sizeof(len),
92 }, {
93 .iov_base = test,
94 .iov_len = sizeof(test),
97 int ret;
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,
116 int socket)
118 uint64_t req_addr;
119 uint32_t free_head;
120 uint32_t len;
121 char buffer[64];
122 int ret;
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));
135 len = ntohl(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,
143 int socket)
145 uint64_t req_addr;
146 uint32_t free_head;
147 char test[] = "TEST";
148 char buffer[64];
149 int len = htonl(sizeof(test));
150 struct iovec iov[] = {
152 .iov_base = &len,
153 .iov_len = sizeof(len),
154 }, {
155 .iov_base = test,
156 .iov_len = sizeof(test),
159 int ret;
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;
202 QPCIBus *bus;
203 QVirtQueuePCI *tx, *rx;
204 QGuestAllocator *alloc;
205 void (*func) (const QVirtioBus *bus,
206 QVirtioDevice *dev,
207 QGuestAllocator *alloc,
208 QVirtQueue *rvq,
209 QVirtQueue *tvq,
210 int socket) = data;
211 int sv[2], ret;
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,
221 alloc, 0);
222 tx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
223 alloc, 1);
225 driver_init(&qvirtio_pci, &dev->vdev);
226 func(&qvirtio_pci, &dev->vdev, alloc, &rx->vq, &tx->vq, sv[0]);
228 /* End test */
229 close(sv[0]);
230 guest_free(alloc, tx->vq.desc);
231 pc_alloc_uninit(alloc);
232 qvirtio_pci_device_disable(dev);
233 g_free(dev);
234 qpci_free_pc(bus);
235 test_end();
237 #endif
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);
246 test_end();
249 int main(int argc, char **argv)
251 int ret;
253 g_test_init(&argc, &argv, NULL);
254 #ifndef _WIN32
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);
258 #endif
259 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
261 ret = g_test_run();
263 return ret;