]> git.proxmox.com Git - mirror_qemu.git/blob - tests/virtio-net-test.c
tests: introduce basic pci test for virtio-net
[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 <glib.h>
11 #include <string.h>
12 #include "libqtest.h"
13 #include "qemu-common.h"
14 #include "qemu/sockets.h"
15 #include "qemu/osdep.h"
16 #include "qemu/iov.h"
17 #include "libqos/pci-pc.h"
18 #include "libqos/virtio.h"
19 #include "libqos/virtio-pci.h"
20 #include "libqos/malloc.h"
21 #include "libqos/malloc-pc.h"
22 #include "libqos/malloc-generic.h"
23 #include "qemu/bswap.h"
24 #include "hw/virtio/virtio-net.h"
25
26 #define PCI_SLOT_HP 0x06
27 #define PCI_SLOT 0x04
28 #define PCI_FN 0x00
29
30 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
31 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
32
33 static void test_end(void)
34 {
35 qtest_end();
36 }
37
38 #ifndef _WIN32
39
40 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
41 {
42 QVirtioPCIDevice *dev;
43
44 dev = qvirtio_pci_device_find(bus, QVIRTIO_NET_DEVICE_ID);
45 g_assert(dev != NULL);
46 g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_NET_DEVICE_ID);
47
48 qvirtio_pci_device_enable(dev);
49 qvirtio_reset(&qvirtio_pci, &dev->vdev);
50 qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
51 qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
52
53 return dev;
54 }
55
56 static QPCIBus *pci_test_start(int socket)
57 {
58 char *cmdline;
59
60 cmdline = g_strdup_printf("-netdev socket,fd=%d,id=hs0 -device "
61 "virtio-net-pci,netdev=hs0", socket);
62 qtest_start(cmdline);
63 g_free(cmdline);
64
65 return qpci_init_pc();
66 }
67
68 static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
69 {
70 uint32_t features;
71
72 features = qvirtio_get_features(bus, dev);
73 features = features & ~(QVIRTIO_F_BAD_FEATURE |
74 QVIRTIO_F_RING_INDIRECT_DESC |
75 QVIRTIO_F_RING_EVENT_IDX);
76 qvirtio_set_features(bus, dev, features);
77
78 qvirtio_set_driver_ok(bus, dev);
79 }
80
81 static void rx_test(const QVirtioBus *bus, QVirtioDevice *dev,
82 QGuestAllocator *alloc, QVirtQueue *vq,
83 int socket)
84 {
85 uint64_t req_addr;
86 uint32_t free_head;
87 char test[] = "TEST";
88 char buffer[64];
89 int len = htonl(sizeof(test));
90 struct iovec iov[] = {
91 {
92 .iov_base = &len,
93 .iov_len = sizeof(len),
94 }, {
95 .iov_base = test,
96 .iov_len = sizeof(test),
97 },
98 };
99 int ret;
100
101 req_addr = guest_alloc(alloc, 64);
102
103 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
104 qvirtqueue_kick(bus, dev, vq, free_head);
105
106 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
107 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
108
109 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
110 memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
111 g_assert_cmpstr(buffer, ==, "TEST");
112
113 guest_free(alloc, req_addr);
114 }
115
116 static void tx_test(const QVirtioBus *bus, QVirtioDevice *dev,
117 QGuestAllocator *alloc, QVirtQueue *vq,
118 int socket)
119 {
120 uint64_t req_addr;
121 uint32_t free_head;
122 uint32_t len;
123 char buffer[64];
124 int ret;
125
126 req_addr = guest_alloc(alloc, 64);
127 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
128
129 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
130 qvirtqueue_kick(bus, dev, vq, free_head);
131
132 qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
133 guest_free(alloc, req_addr);
134
135 ret = qemu_recv(socket, &len, sizeof(len), 0);
136 g_assert_cmpint(ret, ==, sizeof(len));
137 len = ntohl(len);
138
139 ret = qemu_recv(socket, buffer, len, 0);
140 g_assert_cmpstr(buffer, ==, "TEST");
141 }
142
143 static void send_recv_test(const QVirtioBus *bus, QVirtioDevice *dev,
144 QGuestAllocator *alloc, QVirtQueue *rvq,
145 QVirtQueue *tvq, int socket)
146 {
147 rx_test(bus, dev, alloc, rvq, socket);
148 tx_test(bus, dev, alloc, tvq, socket);
149 }
150
151 static void pci_basic(gconstpointer data)
152 {
153 QVirtioPCIDevice *dev;
154 QPCIBus *bus;
155 QVirtQueuePCI *tx, *rx;
156 QGuestAllocator *alloc;
157 void (*func) (const QVirtioBus *bus,
158 QVirtioDevice *dev,
159 QGuestAllocator *alloc,
160 QVirtQueue *rvq,
161 QVirtQueue *tvq,
162 int socket) = data;
163 int sv[2], ret;
164
165 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
166 g_assert_cmpint(ret, !=, -1);
167
168 bus = pci_test_start(sv[1]);
169 dev = virtio_net_pci_init(bus, PCI_SLOT);
170
171 alloc = pc_alloc_init();
172 rx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
173 alloc, 0);
174 tx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
175 alloc, 1);
176
177 driver_init(&qvirtio_pci, &dev->vdev);
178 func(&qvirtio_pci, &dev->vdev, alloc, &rx->vq, &tx->vq, sv[0]);
179
180 /* End test */
181 close(sv[0]);
182 guest_free(alloc, tx->vq.desc);
183 pc_alloc_uninit(alloc);
184 qvirtio_pci_device_disable(dev);
185 g_free(dev);
186 qpci_free_pc(bus);
187 test_end();
188 }
189 #endif
190
191 static void hotplug(void)
192 {
193 qtest_start("-device virtio-net-pci");
194
195 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
196 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
197
198 test_end();
199 }
200
201 int main(int argc, char **argv)
202 {
203 int ret;
204
205 g_test_init(&argc, &argv, NULL);
206 #ifndef _WIN32
207 qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
208 #endif
209 qtest_add_func("/virtio/net/pci/hotplug", hotplug);
210
211 ret = g_test_run();
212
213 return ret;
214 }