display: virtio-gpu-3d: check virgl capabilities max_size
[qemu.git] / tests / virtio-net-test.c
blob8f94360480d6d7365d53813b6641a42e819ac97d
1 /*
2 * QTest testcase for VirtIO NIC
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
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 */
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"
24 #define PCI_SLOT_HP 0x06
25 #define PCI_SLOT 0x04
26 #define PCI_FN 0x00
28 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
29 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31 static void test_end(void)
33 qtest_end();
36 #ifndef _WIN32
38 static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
40 QVirtioPCIDevice *dev;
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);
46 qvirtio_pci_device_enable(dev);
47 qvirtio_reset(&dev->vdev);
48 qvirtio_set_acknowledge(&dev->vdev);
49 qvirtio_set_driver(&dev->vdev);
51 return dev;
54 static QOSState *pci_test_start(int socket)
56 const char *arch = qtest_get_arch();
57 const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
58 "virtio-net-pci,netdev=hs0";
60 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
61 return qtest_pc_boot(cmd, socket);
63 if (strcmp(arch, "ppc64") == 0) {
64 return qtest_spapr_boot(cmd, socket);
66 g_printerr("virtio-net tests are only available on x86 or ppc64\n");
67 exit(EXIT_FAILURE);
70 static void driver_init(QVirtioDevice *dev)
72 uint32_t features;
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);
80 qvirtio_set_driver_ok(dev);
83 static void rx_test(QVirtioDevice *dev,
84 QGuestAllocator *alloc, QVirtQueue *vq,
85 int socket)
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[] = {
94 .iov_base = &len,
95 .iov_len = sizeof(len),
96 }, {
97 .iov_base = test,
98 .iov_len = sizeof(test),
101 int ret;
103 req_addr = guest_alloc(alloc, 64);
105 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
106 qvirtqueue_kick(dev, vq, free_head);
108 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
109 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
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");
115 guest_free(alloc, req_addr);
118 static void tx_test(QVirtioDevice *dev,
119 QGuestAllocator *alloc, QVirtQueue *vq,
120 int socket)
122 uint64_t req_addr;
123 uint32_t free_head;
124 uint32_t len;
125 char buffer[64];
126 int ret;
128 req_addr = guest_alloc(alloc, 64);
129 memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
131 free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
132 qvirtqueue_kick(dev, vq, free_head);
134 qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
135 guest_free(alloc, req_addr);
137 ret = qemu_recv(socket, &len, sizeof(len), 0);
138 g_assert_cmpint(ret, ==, sizeof(len));
139 len = ntohl(len);
141 ret = qemu_recv(socket, buffer, len, 0);
142 g_assert_cmpstr(buffer, ==, "TEST");
145 static void rx_stop_cont_test(QVirtioDevice *dev,
146 QGuestAllocator *alloc, QVirtQueue *vq,
147 int socket)
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[] = {
157 .iov_base = &len,
158 .iov_len = sizeof(len),
159 }, {
160 .iov_base = test,
161 .iov_len = sizeof(test),
164 int ret;
166 req_addr = guest_alloc(alloc, 64);
168 free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
169 qvirtqueue_kick(dev, vq, free_head);
171 rsp = qmp("{ 'execute' : 'stop'}");
172 QDECREF(rsp);
174 ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
175 g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
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'.
180 rsp = qmp("{ 'execute' : 'query-status'}");
181 QDECREF(rsp);
182 rsp = qmp("{ 'execute' : 'cont'}");
183 QDECREF(rsp);
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");
189 guest_free(alloc, req_addr);
192 static void send_recv_test(QVirtioDevice *dev,
193 QGuestAllocator *alloc, QVirtQueue *rvq,
194 QVirtQueue *tvq, int socket)
196 rx_test(dev, alloc, rvq, socket);
197 tx_test(dev, alloc, tvq, socket);
200 static void stop_cont_test(QVirtioDevice *dev,
201 QGuestAllocator *alloc, QVirtQueue *rvq,
202 QVirtQueue *tvq, int socket)
204 rx_stop_cont_test(dev, alloc, rvq, socket);
207 static void pci_basic(gconstpointer data)
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;
219 ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
220 g_assert_cmpint(ret, !=, -1);
222 qs = pci_test_start(sv[1]);
223 dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
225 rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
226 tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
228 driver_init(&dev->vdev);
229 func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
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);
240 #endif
242 static void hotplug(void)
244 const char *arch = qtest_get_arch();
246 qtest_start("-device virtio-net-pci");
248 qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
250 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
251 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
254 test_end();
257 int main(int argc, char **argv)
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);
267 return g_test_run();