]> git.proxmox.com Git - mirror_qemu.git/blob - tests/virtio-net-test.c
rbd: Reject -blockdev server.*.{numeric, to, ipv4, ipv6}
[mirror_qemu.git] / tests / virtio-net-test.c
1 /*
2 * QTest testcase for VirtIO NIC
3 *
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
5 *
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 */
9
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 "qemu/bswap.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "standard-headers/linux/virtio_ids.h"
22 #include "standard-headers/linux/virtio_ring.h"
23
24 #define PCI_SLOT_HP 0x06
25 #define PCI_SLOT 0x04
26 #define PCI_FN 0x00
27
28 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
29 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
30
31 static void test_end(void)
32 {
33 qtest_end();
34 }
35
36 #ifndef _WIN32
37
38 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
39 {
40 QVirtioPCIDevice *dev;
41
42 dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
43 g_assert(dev != NULL);
44 g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
45
46 qvirtio_pci_device_enable(dev);
47 qvirtio_reset(&dev->vdev);
48 qvirtio_set_acknowledge(&dev->vdev);
49 qvirtio_set_driver(&dev->vdev);
50
51 return dev;
52 }
53
54 static QOSState *pci_test_start(int socket)
55 {
56 const char *arch = qtest_get_arch();
57 const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
58 "virtio-net-pci,netdev=hs0";
59
60 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
61 return qtest_pc_boot(cmd, socket);
62 }
63 if (strcmp(arch, "ppc64") == 0) {
64 return qtest_spapr_boot(cmd, socket);
65 }
66 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
67 exit(EXIT_FAILURE);
68 }
69
70 static void driver_init(QVirtioDevice *dev)
71 {
72 uint32_t features;
73
74 features = qvirtio_get_features(dev);
75 features = features & ~(QVIRTIO_F_BAD_FEATURE |
76 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
77 (1u << VIRTIO_RING_F_EVENT_IDX));
78 qvirtio_set_features(dev, features);
79
80 qvirtio_set_driver_ok(dev);
81 }
82
83 static void rx_test(QVirtioDevice *dev,
84 QGuestAllocator *alloc, QVirtQueue *vq,
85 int socket)
86 {
87 uint64_t req_addr;
88 uint32_t free_head;
89 char test[] = "TEST";
90 char buffer[64];
91 int len = htonl(sizeof(test));
92 struct iovec iov[] = {
93 {
94 .iov_base = &len,
95 .iov_len = sizeof(len),
96 }, {
97 .iov_base = test,
98 .iov_len = sizeof(test),
99 },
100 };
101 int ret;
102
103 req_addr = guest_alloc(alloc, 64);
104
105 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
106 qvirtqueue_kick(dev, vq, free_head);
107
108 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
109 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
110
111 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
112 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
113 g_assert_cmpstr(buffer, ==, "TEST");
114
115 guest_free(alloc, req_addr);
116 }
117
118 static void tx_test(QVirtioDevice *dev,
119 QGuestAllocator *alloc, QVirtQueue *vq,
120 int socket)
121 {
122 uint64_t req_addr;
123 uint32_t free_head;
124 uint32_t len;
125 char buffer[64];
126 int ret;
127
128 req_addr = guest_alloc(alloc, 64);
129 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
130
131 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
132 qvirtqueue_kick(dev, vq, free_head);
133
134 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
135 guest_free(alloc, req_addr);
136
137 ret = qemu_recv(socket, &len, sizeof(len), 0);
138 g_assert_cmpint(ret, ==, sizeof(len));
139 len = ntohl(len);
140
141 ret = qemu_recv(socket, buffer, len, 0);
142 g_assert_cmpstr(buffer, ==, "TEST");
143 }
144
145 static void rx_stop_cont_test(QVirtioDevice *dev,
146 QGuestAllocator *alloc, QVirtQueue *vq,
147 int socket)
148 {
149 uint64_t req_addr;
150 uint32_t free_head;
151 char test[] = "TEST";
152 char buffer[64];
153 int len = htonl(sizeof(test));
154 QDict *rsp;
155 struct iovec iov[] = {
156 {
157 .iov_base = &len,
158 .iov_len = sizeof(len),
159 }, {
160 .iov_base = test,
161 .iov_len = sizeof(test),
162 },
163 };
164 int ret;
165
166 req_addr = guest_alloc(alloc, 64);
167
168 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
169 qvirtqueue_kick(dev, vq, free_head);
170
171 rsp = qmp("{ 'execute' : 'stop'}");
172 QDECREF(rsp);
173
174 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
175 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
176
177 /* We could check the status, but this command is more importantly to
178 * ensure the packet data gets queued in QEMU, before we do 'cont'.
179 */
180 rsp = qmp("{ 'execute' : 'query-status'}");
181 QDECREF(rsp);
182 rsp = qmp("{ 'execute' : 'cont'}");
183 QDECREF(rsp);
184
185 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
186 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
187 g_assert_cmpstr(buffer, ==, "TEST");
188
189 guest_free(alloc, req_addr);
190 }
191
192 static void send_recv_test(QVirtioDevice *dev,
193 QGuestAllocator *alloc, QVirtQueue *rvq,
194 QVirtQueue *tvq, int socket)
195 {
196 rx_test(dev, alloc, rvq, socket);
197 tx_test(dev, alloc, tvq, socket);
198 }
199
200 static void stop_cont_test(QVirtioDevice *dev,
201 QGuestAllocator *alloc, QVirtQueue *rvq,
202 QVirtQueue *tvq, int socket)
203 {
204 rx_stop_cont_test(dev, alloc, rvq, socket);
205 }
206
207 static void pci_basic(gconstpointer data)
208 {
209 QVirtioPCIDevice *dev;
210 QOSState *qs;
211 QVirtQueuePCI *tx, *rx;
212 void (*func) (QVirtioDevice *dev,
213 QGuestAllocator *alloc,
214 QVirtQueue *rvq,
215 QVirtQueue *tvq,
216 int socket) = data;
217 int sv[2], ret;
218
219 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
220 g_assert_cmpint(ret, !=, -1);
221
222 qs = pci_test_start(sv[1]);
223 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
224
225 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
226 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
227
228 driver_init(&dev->vdev);
229 func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
230
231 /* End test */
232 close(sv[0]);
233 qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
234 qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
235 qvirtio_pci_device_disable(dev);
236 g_free(dev->pdev);
237 g_free(dev);
238 qtest_shutdown(qs);
239 }
240 #endif
241
242 static void hotplug(void)
243 {
244 const char *arch = qtest_get_arch();
245
246 qtest_start("-device virtio-net-pci");
247
248 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
249
250 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
251 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
252 }
253
254 test_end();
255 }
256
257 int main(int argc, char **argv)
258 {
259 g_test_init(&argc, &argv, NULL);
260 #ifndef _WIN32
261 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
262 qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
263 stop_cont_test, pci_basic);
264 #endif
265 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
266
267 return g_test_run();
268 }