memory: unify loops to sync dirty log bitmap
[qemu/ar7.git] / tests / virtio-net-test.c
blob41148394572a7f5254659cb5570d0b46308eca0a
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/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
26 #define PCI_SLOT 0x04
27 #define PCI_FN 0x00
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)
34 qtest_end();
37 #ifndef _WIN32
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);
52 return dev;
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");
68 exit(EXIT_FAILURE);
71 static void driver_init(QVirtioDevice *dev)
73 uint32_t features;
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,
86 int socket)
88 uint64_t req_addr;
89 uint32_t free_head;
90 char test[] = "TEST";
91 char buffer[64];
92 int len = htonl(sizeof(test));
93 struct iovec iov[] = {
95 .iov_base = &len,
96 .iov_len = sizeof(len),
97 }, {
98 .iov_base = test,
99 .iov_len = sizeof(test),
102 int ret;
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,
121 int socket)
123 uint64_t req_addr;
124 uint32_t free_head;
125 uint32_t len;
126 char buffer[64];
127 int ret;
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));
140 len = ntohl(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,
148 int socket)
150 uint64_t req_addr;
151 uint32_t free_head;
152 char test[] = "TEST";
153 char buffer[64];
154 int len = htonl(sizeof(test));
155 QDict *rsp;
156 struct iovec iov[] = {
158 .iov_base = &len,
159 .iov_len = sizeof(len),
160 }, {
161 .iov_base = test,
162 .iov_len = sizeof(test),
165 int ret;
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'}");
173 QDECREF(rsp);
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'}");
182 QDECREF(rsp);
183 rsp = qmp("{ 'execute' : 'cont'}");
184 QDECREF(rsp);
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;
211 QOSState *qs;
212 QVirtQueuePCI *tx, *rx;
213 void (*func) (QVirtioDevice *dev,
214 QGuestAllocator *alloc,
215 QVirtQueue *rvq,
216 QVirtQueue *tvq,
217 int socket) = data;
218 int sv[2], ret;
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]);
232 /* End test */
233 close(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);
237 g_free(dev->pdev);
238 g_free(dev);
239 qtest_shutdown(qs);
241 #endif
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);
255 test_end();
258 int main(int argc, char **argv)
260 g_test_init(&argc, &argv, NULL);
261 #ifndef _WIN32
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);
265 #endif
266 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
268 return g_test_run();