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.
10 #include "qemu/osdep.h"
13 #include "qapi/qmp/qdict.h"
14 #include "hw/virtio/virtio-net.h"
15 #include "libqos/qgraph.h"
16 #include "libqos/virtio-net.h"
18 #define PCI_SLOT_HP 0x06
21 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
22 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
26 static void rx_test(QVirtioDevice
*dev
,
27 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
34 int len
= htonl(sizeof(test
));
35 struct iovec iov
[] = {
38 .iov_len
= sizeof(len
),
41 .iov_len
= sizeof(test
),
46 req_addr
= guest_alloc(alloc
, 64);
48 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
49 qvirtqueue_kick(dev
, vq
, free_head
);
51 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
52 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
54 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
55 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
56 g_assert_cmpstr(buffer
, ==, "TEST");
58 guest_free(alloc
, req_addr
);
61 static void tx_test(QVirtioDevice
*dev
,
62 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
71 req_addr
= guest_alloc(alloc
, 64);
72 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
74 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
75 qvirtqueue_kick(dev
, vq
, free_head
);
77 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
78 guest_free(alloc
, req_addr
);
80 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
81 g_assert_cmpint(ret
, ==, sizeof(len
));
84 ret
= qemu_recv(socket
, buffer
, len
, 0);
85 g_assert_cmpstr(buffer
, ==, "TEST");
88 static void rx_stop_cont_test(QVirtioDevice
*dev
,
89 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
96 int len
= htonl(sizeof(test
));
98 struct iovec iov
[] = {
101 .iov_len
= sizeof(len
),
104 .iov_len
= sizeof(test
),
109 req_addr
= guest_alloc(alloc
, 64);
111 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
112 qvirtqueue_kick(dev
, vq
, free_head
);
114 rsp
= qmp("{ 'execute' : 'stop'}");
117 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
118 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
120 /* We could check the status, but this command is more importantly to
121 * ensure the packet data gets queued in QEMU, before we do 'cont'.
123 rsp
= qmp("{ 'execute' : 'query-status'}");
125 rsp
= qmp("{ 'execute' : 'cont'}");
128 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
129 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
130 g_assert_cmpstr(buffer
, ==, "TEST");
132 guest_free(alloc
, req_addr
);
135 static void send_recv_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
137 QVirtioNet
*net_if
= obj
;
138 QVirtioDevice
*dev
= net_if
->vdev
;
139 QVirtQueue
*rx
= net_if
->queues
[0];
140 QVirtQueue
*tx
= net_if
->queues
[1];
143 rx_test(dev
, t_alloc
, rx
, sv
[0]);
144 tx_test(dev
, t_alloc
, tx
, sv
[0]);
147 static void stop_cont_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
149 QVirtioNet
*net_if
= obj
;
150 QVirtioDevice
*dev
= net_if
->vdev
;
151 QVirtQueue
*rx
= net_if
->queues
[0];
154 rx_stop_cont_test(dev
, t_alloc
, rx
, sv
[0]);
159 static void hotplug(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
161 const char *arch
= qtest_get_arch();
163 qtest_qmp_device_add("virtio-net-pci", "net1",
164 "{'addr': %s}", stringify(PCI_SLOT_HP
));
166 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
167 qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP
);
171 static void virtio_net_test_cleanup(void *sockets
)
176 qos_invalidate_command_line();
181 static void *virtio_net_test_setup(GString
*cmd_line
, void *arg
)
184 int *sv
= g_new(int, 2);
186 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
187 g_assert_cmpint(ret
, !=, -1);
189 g_string_append_printf(cmd_line
, " -netdev socket,fd=%d,id=hs0 ", sv
[1]);
191 g_test_queue_destroy(virtio_net_test_cleanup
, sv
);
195 static void large_tx(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
197 QVirtioNet
*dev
= obj
;
198 QVirtQueue
*vq
= dev
->queues
[1];
201 size_t alloc_size
= (size_t)data
/ 64;
204 /* Bypass the limitation by pointing several descriptors to a single
206 req_addr
= guest_alloc(t_alloc
, alloc_size
);
207 free_head
= qvirtqueue_add(vq
, req_addr
, alloc_size
, false, true);
209 for (i
= 0; i
< 64; i
++) {
210 qvirtqueue_add(vq
, req_addr
, alloc_size
, false, i
!= 63);
212 qvirtqueue_kick(dev
->vdev
, vq
, free_head
);
214 qvirtio_wait_used_elem(dev
->vdev
, vq
, free_head
, NULL
,
215 QVIRTIO_NET_TIMEOUT_US
);
216 guest_free(t_alloc
, req_addr
);
219 static void *virtio_net_test_setup_nosocket(GString
*cmd_line
, void *arg
)
221 g_string_append(cmd_line
, " -netdev hubport,hubid=0,id=hs0 ");
225 static void register_virtio_net_test(void)
227 QOSGraphTestOptions opts
= {
228 .before
= virtio_net_test_setup
,
231 qos_add_test("hotplug", "virtio-pci", hotplug
, &opts
);
233 qos_add_test("basic", "virtio-net", send_recv_test
, &opts
);
234 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test
, &opts
);
237 /* These tests do not need a loopback backend. */
238 opts
.before
= virtio_net_test_setup_nosocket
;
239 opts
.arg
= (gpointer
)UINT_MAX
;
240 qos_add_test("large_tx/uint_max", "virtio-net", large_tx
, &opts
);
241 opts
.arg
= (gpointer
)NET_BUFSIZE
;
242 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx
, &opts
);
245 libqos_init(register_virtio_net_test
);