]> Git Repo - qemu.git/blob - tests/virtio-net-test.c
Merge remote-tracking branch 'remotes/famz/tags/pull-docker-20160608' into staging
[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/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"
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, QVIRTIO_NET_DEVICE_ID);
43     g_assert(dev != NULL);
44     g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_NET_DEVICE_ID);
45
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);
50
51     return dev;
52 }
53
54 static QPCIBus *pci_test_start(int socket)
55 {
56     char *cmdline;
57
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);
62
63     return qpci_init_pc();
64 }
65
66 static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
67 {
68     uint32_t features;
69
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);
75
76     qvirtio_set_driver_ok(bus, dev);
77 }
78
79 static void rx_test(const QVirtioBus *bus, QVirtioDevice *dev,
80                     QGuestAllocator *alloc, QVirtQueue *vq,
81                     int socket)
82 {
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[] = {
89         {
90             .iov_base = &len,
91             .iov_len = sizeof(len),
92         }, {
93             .iov_base = test,
94             .iov_len = sizeof(test),
95         },
96     };
97     int ret;
98
99     req_addr = guest_alloc(alloc, 64);
100
101     free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
102     qvirtqueue_kick(bus, dev, vq, free_head);
103
104     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
105     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
106
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");
110
111     guest_free(alloc, req_addr);
112 }
113
114 static void tx_test(const QVirtioBus *bus, QVirtioDevice *dev,
115                     QGuestAllocator *alloc, QVirtQueue *vq,
116                     int socket)
117 {
118     uint64_t req_addr;
119     uint32_t free_head;
120     uint32_t len;
121     char buffer[64];
122     int ret;
123
124     req_addr = guest_alloc(alloc, 64);
125     memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
126
127     free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
128     qvirtqueue_kick(bus, dev, vq, free_head);
129
130     qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
131     guest_free(alloc, req_addr);
132
133     ret = qemu_recv(socket, &len, sizeof(len), 0);
134     g_assert_cmpint(ret, ==, sizeof(len));
135     len = ntohl(len);
136
137     ret = qemu_recv(socket, buffer, len, 0);
138     g_assert_cmpstr(buffer, ==, "TEST");
139 }
140
141 static void rx_stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev,
142                               QGuestAllocator *alloc, QVirtQueue *vq,
143                               int socket)
144 {
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[] = {
151         {
152             .iov_base = &len,
153             .iov_len = sizeof(len),
154         }, {
155             .iov_base = test,
156             .iov_len = sizeof(test),
157         },
158     };
159     int ret;
160
161     req_addr = guest_alloc(alloc, 64);
162
163     free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
164     qvirtqueue_kick(bus, dev, vq, free_head);
165
166     qmp("{ 'execute' : 'stop'}");
167
168     ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
169     g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
170
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'.
173      */
174     qmp("{ 'execute' : 'query-status'}");
175     qmp("{ 'execute' : 'cont'}");
176
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");
180
181     guest_free(alloc, req_addr);
182 }
183
184 static void send_recv_test(const QVirtioBus *bus, QVirtioDevice *dev,
185                            QGuestAllocator *alloc, QVirtQueue *rvq,
186                            QVirtQueue *tvq, int socket)
187 {
188     rx_test(bus, dev, alloc, rvq, socket);
189     tx_test(bus, dev, alloc, tvq, socket);
190 }
191
192 static void stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev,
193                            QGuestAllocator *alloc, QVirtQueue *rvq,
194                            QVirtQueue *tvq, int socket)
195 {
196     rx_stop_cont_test(bus, dev, alloc, rvq, socket);
197 }
198
199 static void pci_basic(gconstpointer data)
200 {
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;
212
213     ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
214     g_assert_cmpint(ret, !=, -1);
215
216     bus = pci_test_start(sv[1]);
217     dev = virtio_net_pci_init(bus, PCI_SLOT);
218
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);
224
225     driver_init(&qvirtio_pci, &dev->vdev);
226     func(&qvirtio_pci, &dev->vdev, alloc, &rx->vq, &tx->vq, sv[0]);
227
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();
236 }
237 #endif
238
239 static void hotplug(void)
240 {
241     qtest_start("-device virtio-net-pci");
242
243     qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
244     qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
245
246     test_end();
247 }
248
249 int main(int argc, char **argv)
250 {
251     int ret;
252
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);
260
261     ret = g_test_run();
262
263     return ret;
264 }
This page took 0.03908 seconds and 4 git commands to generate.