]>
Commit | Line | Data |
---|---|---|
b815ec5e AF |
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 | ||
681c28a3 | 10 | #include "qemu/osdep.h" |
b815ec5e | 11 | #include "libqtest.h" |
2af40254 JW |
12 | #include "qemu-common.h" |
13 | #include "qemu/sockets.h" | |
2af40254 | 14 | #include "qemu/iov.h" |
a980f7f2 | 15 | #include "libqos/libqos-pc.h" |
30ca440e | 16 | #include "libqos/libqos-spapr.h" |
2af40254 JW |
17 | #include "libqos/virtio.h" |
18 | #include "libqos/virtio-pci.h" | |
452fcdbc | 19 | #include "qapi/qmp/qdict.h" |
2af40254 JW |
20 | #include "qemu/bswap.h" |
21 | #include "hw/virtio/virtio-net.h" | |
8ac9e205 | 22 | #include "standard-headers/linux/virtio_ids.h" |
ee3b850a | 23 | #include "standard-headers/linux/virtio_ring.h" |
9224709b IM |
24 | |
25 | #define PCI_SLOT_HP 0x06 | |
2af40254 JW |
26 | #define PCI_SLOT 0x04 |
27 | #define PCI_FN 0x00 | |
b815ec5e | 28 | |
2af40254 JW |
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 | ||
8ac9e205 | 43 | dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET); |
2af40254 | 44 | g_assert(dev != NULL); |
8ac9e205 | 45 | g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET); |
2af40254 JW |
46 | |
47 | qvirtio_pci_device_enable(dev); | |
6b9cdf4c LV |
48 | qvirtio_reset(&dev->vdev); |
49 | qvirtio_set_acknowledge(&dev->vdev); | |
50 | qvirtio_set_driver(&dev->vdev); | |
2af40254 JW |
51 | |
52 | return dev; | |
53 | } | |
54 | ||
a980f7f2 | 55 | static QOSState *pci_test_start(int socket) |
2af40254 | 56 | { |
30ca440e | 57 | const char *arch = qtest_get_arch(); |
a980f7f2 LV |
58 | const char *cmd = "-netdev socket,fd=%d,id=hs0 -device " |
59 | "virtio-net-pci,netdev=hs0"; | |
2af40254 | 60 | |
30ca440e LV |
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); | |
2af40254 JW |
69 | } |
70 | ||
6b9cdf4c | 71 | static void driver_init(QVirtioDevice *dev) |
2af40254 JW |
72 | { |
73 | uint32_t features; | |
74 | ||
6b9cdf4c | 75 | features = qvirtio_get_features(dev); |
2af40254 | 76 | features = features & ~(QVIRTIO_F_BAD_FEATURE | |
ee3b850a SH |
77 | (1u << VIRTIO_RING_F_INDIRECT_DESC) | |
78 | (1u << VIRTIO_RING_F_EVENT_IDX)); | |
6b9cdf4c | 79 | qvirtio_set_features(dev, features); |
2af40254 | 80 | |
6b9cdf4c | 81 | qvirtio_set_driver_ok(dev); |
2af40254 JW |
82 | } |
83 | ||
6b9cdf4c | 84 | static void rx_test(QVirtioDevice *dev, |
2af40254 JW |
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); | |
6b9cdf4c | 107 | qvirtqueue_kick(dev, vq, free_head); |
2af40254 JW |
108 | |
109 | ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test)); | |
110 | g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len)); | |
111 | ||
be3a6781 | 112 | qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US); |
2af40254 JW |
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 | ||
6b9cdf4c | 119 | static void tx_test(QVirtioDevice *dev, |
2af40254 JW |
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); | |
6b9cdf4c | 133 | qvirtqueue_kick(dev, vq, free_head); |
2af40254 | 134 | |
be3a6781 | 135 | qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US); |
2af40254 JW |
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 | ||
6b9cdf4c | 146 | static void rx_stop_cont_test(QVirtioDevice *dev, |
8887f84c JW |
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)); | |
1ec3b71c | 155 | QDict *rsp; |
8887f84c JW |
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); | |
6b9cdf4c | 170 | qvirtqueue_kick(dev, vq, free_head); |
8887f84c | 171 | |
1ec3b71c MAL |
172 | rsp = qmp("{ 'execute' : 'stop'}"); |
173 | QDECREF(rsp); | |
8887f84c JW |
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 | */ | |
1ec3b71c MAL |
181 | rsp = qmp("{ 'execute' : 'query-status'}"); |
182 | QDECREF(rsp); | |
183 | rsp = qmp("{ 'execute' : 'cont'}"); | |
184 | QDECREF(rsp); | |
8887f84c | 185 | |
be3a6781 | 186 | qvirtio_wait_used_elem(dev, vq, free_head, NULL, QVIRTIO_NET_TIMEOUT_US); |
8887f84c JW |
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 | ||
6b9cdf4c | 193 | static void send_recv_test(QVirtioDevice *dev, |
2af40254 JW |
194 | QGuestAllocator *alloc, QVirtQueue *rvq, |
195 | QVirtQueue *tvq, int socket) | |
b815ec5e | 196 | { |
6b9cdf4c LV |
197 | rx_test(dev, alloc, rvq, socket); |
198 | tx_test(dev, alloc, tvq, socket); | |
b815ec5e AF |
199 | } |
200 | ||
6b9cdf4c | 201 | static void stop_cont_test(QVirtioDevice *dev, |
8887f84c JW |
202 | QGuestAllocator *alloc, QVirtQueue *rvq, |
203 | QVirtQueue *tvq, int socket) | |
204 | { | |
6b9cdf4c | 205 | rx_stop_cont_test(dev, alloc, rvq, socket); |
8887f84c JW |
206 | } |
207 | ||
2af40254 JW |
208 | static void pci_basic(gconstpointer data) |
209 | { | |
210 | QVirtioPCIDevice *dev; | |
a980f7f2 | 211 | QOSState *qs; |
2af40254 | 212 | QVirtQueuePCI *tx, *rx; |
6b9cdf4c | 213 | void (*func) (QVirtioDevice *dev, |
2af40254 JW |
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 | ||
a980f7f2 LV |
223 | qs = pci_test_start(sv[1]); |
224 | dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT); | |
2af40254 | 225 | |
a980f7f2 LV |
226 | rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0); |
227 | tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1); | |
2af40254 | 228 | |
6b9cdf4c | 229 | driver_init(&dev->vdev); |
a980f7f2 | 230 | func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]); |
2af40254 JW |
231 | |
232 | /* End test */ | |
233 | close(sv[0]); | |
a980f7f2 LV |
234 | qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc); |
235 | qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc); | |
2af40254 | 236 | qvirtio_pci_device_disable(dev); |
1ec3b71c | 237 | g_free(dev->pdev); |
2af40254 | 238 | g_free(dev); |
a980f7f2 | 239 | qtest_shutdown(qs); |
2af40254 JW |
240 | } |
241 | #endif | |
242 | ||
9224709b IM |
243 | static void hotplug(void) |
244 | { | |
30ca440e LV |
245 | const char *arch = qtest_get_arch(); |
246 | ||
2af40254 JW |
247 | qtest_start("-device virtio-net-pci"); |
248 | ||
9224709b | 249 | qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL); |
30ca440e LV |
250 | |
251 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
252 | qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP); | |
253 | } | |
2af40254 JW |
254 | |
255 | test_end(); | |
9224709b IM |
256 | } |
257 | ||
b815ec5e AF |
258 | int main(int argc, char **argv) |
259 | { | |
b815ec5e | 260 | g_test_init(&argc, &argv, NULL); |
2af40254 JW |
261 | #ifndef _WIN32 |
262 | qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic); | |
8887f84c JW |
263 | qtest_add_data_func("/virtio/net/pci/rx_stop_cont", |
264 | stop_cont_test, pci_basic); | |
2af40254 | 265 | #endif |
9224709b | 266 | qtest_add_func("/virtio/net/pci/hotplug", hotplug); |
b815ec5e | 267 | |
9be38598 | 268 | return g_test_run(); |
b815ec5e | 269 | } |