qcow2: try load bitmaps only once
[qemu/kevin.git] / tests / virtio-net-test.c
blob0a3c5dd2577b82b0af5320a859af17901f1c0f33
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 QOSState *qs;
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);
66 } else {
67 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
68 exit(EXIT_FAILURE);
70 global_qtest = qs->qts;
71 return qs;
74 static void driver_init(QVirtioDevice *dev)
76 uint32_t features;
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,
89 int socket)
91 uint64_t req_addr;
92 uint32_t free_head;
93 char test[] = "TEST";
94 char buffer[64];
95 int len = htonl(sizeof(test));
96 struct iovec iov[] = {
98 .iov_base = &len,
99 .iov_len = sizeof(len),
100 }, {
101 .iov_base = test,
102 .iov_len = sizeof(test),
105 int ret;
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,
124 int socket)
126 uint64_t req_addr;
127 uint32_t free_head;
128 uint32_t len;
129 char buffer[64];
130 int ret;
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));
143 len = ntohl(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,
151 int socket)
153 uint64_t req_addr;
154 uint32_t free_head;
155 char test[] = "TEST";
156 char buffer[64];
157 int len = htonl(sizeof(test));
158 QDict *rsp;
159 struct iovec iov[] = {
161 .iov_base = &len,
162 .iov_len = sizeof(len),
163 }, {
164 .iov_base = test,
165 .iov_len = sizeof(test),
168 int ret;
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'}");
176 QDECREF(rsp);
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'}");
185 QDECREF(rsp);
186 rsp = qmp("{ 'execute' : 'cont'}");
187 QDECREF(rsp);
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;
214 QOSState *qs;
215 QVirtQueuePCI *tx, *rx;
216 void (*func) (QVirtioDevice *dev,
217 QGuestAllocator *alloc,
218 QVirtQueue *rvq,
219 QVirtQueue *tvq,
220 int socket) = data;
221 int sv[2], ret;
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]);
235 /* End test */
236 close(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);
240 g_free(dev->pdev);
241 g_free(dev);
242 qtest_shutdown(qs);
244 #endif
246 static void hotplug(void)
248 const char *arch = qtest_get_arch();
250 qtest_start("-device virtio-net-pci");
252 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
254 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
255 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
258 test_end();
261 int main(int argc, char **argv)
263 g_test_init(&argc, &argv, NULL);
264 #ifndef _WIN32
265 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
266 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
267 stop_cont_test, pci_basic);
268 #endif
269 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
271 return g_test_run();