]> Git Repo - qemu.git/blob - tests/virtio-net-test.c
Merge remote-tracking branch 'remotes/stsquad/tags/pull-travis-speedup-090218-1'...
[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 "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"
24
25 #define PCI_SLOT_HP             0x06
26 #define PCI_SLOT                0x04
27 #define PCI_FN                  0x00
28
29 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
30 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31
32 static void test_end(void)
33 {
34     qtest_end();
35 }
36
37 #ifndef _WIN32
38
39 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
40 {
41     QVirtioPCIDevice *dev;
42
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);
46
47     qvirtio_pci_device_enable(dev);
48     qvirtio_reset(&dev->vdev);
49     qvirtio_set_acknowledge(&dev->vdev);
50     qvirtio_set_driver(&dev->vdev);
51
52     return dev;
53 }
54
55 static QOSState *pci_test_start(int socket)
56 {
57     const char *arch = qtest_get_arch();
58     const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
59                       "virtio-net-pci,netdev=hs0";
60
61     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
62         return qtest_pc_boot(cmd, socket);
63     }
64     if (strcmp(arch, "ppc64") == 0) {
65         return qtest_spapr_boot(cmd, socket);
66     }
67     g_printerr("virtio-net tests are only available on x86 or ppc64\n");
68     exit(EXIT_FAILURE);
69 }
70
71 static void driver_init(QVirtioDevice *dev)
72 {
73     uint32_t features;
74
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);
80
81     qvirtio_set_driver_ok(dev);
82 }
83
84 static void rx_test(QVirtioDevice *dev,
85                     QGuestAllocator *alloc, QVirtQueue *vq,
86                     int socket)
87 {
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[] = {
94         {
95             .iov_base = &len,
96             .iov_len = sizeof(len),
97         }, {
98             .iov_base = test,
99             .iov_len = sizeof(test),
100         },
101     };
102     int ret;
103
104     req_addr = guest_alloc(alloc, 64);
105
106     free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
107     qvirtqueue_kick(dev, vq, free_head);
108
109     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
110     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
111
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");
115
116     guest_free(alloc, req_addr);
117 }
118
119 static void tx_test(QVirtioDevice *dev,
120                     QGuestAllocator *alloc, QVirtQueue *vq,
121                     int socket)
122 {
123     uint64_t req_addr;
124     uint32_t free_head;
125     uint32_t len;
126     char buffer[64];
127     int ret;
128
129     req_addr = guest_alloc(alloc, 64);
130     memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
131
132     free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
133     qvirtqueue_kick(dev, vq, free_head);
134
135     qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US);
136     guest_free(alloc, req_addr);
137
138     ret = qemu_recv(socket, &len, sizeof(len), 0);
139     g_assert_cmpint(ret, ==, sizeof(len));
140     len = ntohl(len);
141
142     ret = qemu_recv(socket, buffer, len, 0);
143     g_assert_cmpstr(buffer, ==, "TEST");
144 }
145
146 static void rx_stop_cont_test(QVirtioDevice *dev,
147                               QGuestAllocator *alloc, QVirtQueue *vq,
148                               int socket)
149 {
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[] = {
157         {
158             .iov_base = &len,
159             .iov_len = sizeof(len),
160         }, {
161             .iov_base = test,
162             .iov_len = sizeof(test),
163         },
164     };
165     int ret;
166
167     req_addr = guest_alloc(alloc, 64);
168
169     free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
170     qvirtqueue_kick(dev, vq, free_head);
171
172     rsp = qmp("{ 'execute' : 'stop'}");
173     QDECREF(rsp);
174
175     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
176     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
177
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'.
180      */
181     rsp = qmp("{ 'execute' : 'query-status'}");
182     QDECREF(rsp);
183     rsp = qmp("{ 'execute' : 'cont'}");
184     QDECREF(rsp);
185
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");
189
190     guest_free(alloc, req_addr);
191 }
192
193 static void send_recv_test(QVirtioDevice *dev,
194                            QGuestAllocator *alloc, QVirtQueue *rvq,
195                            QVirtQueue *tvq, int socket)
196 {
197     rx_test(dev, alloc, rvq, socket);
198     tx_test(dev, alloc, tvq, socket);
199 }
200
201 static void stop_cont_test(QVirtioDevice *dev,
202                            QGuestAllocator *alloc, QVirtQueue *rvq,
203                            QVirtQueue *tvq, int socket)
204 {
205     rx_stop_cont_test(dev, alloc, rvq, socket);
206 }
207
208 static void pci_basic(gconstpointer data)
209 {
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;
219
220     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
221     g_assert_cmpint(ret, !=, -1);
222
223     qs = pci_test_start(sv[1]);
224     dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
225
226     rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
227     tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
228
229     driver_init(&dev->vdev);
230     func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
231
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);
240 }
241 #endif
242
243 static void hotplug(void)
244 {
245     const char *arch = qtest_get_arch();
246
247     qtest_start("-device virtio-net-pci");
248
249     qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
250
251     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
252         qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
253     }
254
255     test_end();
256 }
257
258 int main(int argc, char **argv)
259 {
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);
267
268     return g_test_run();
269 }
This page took 0.040097 seconds and 4 git commands to generate.