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"
11 #include "qemu-common.h"
12 #include "libqtest-single.h"
14 #include "qemu/module.h"
15 #include "qapi/qmp/qdict.h"
16 #include "hw/virtio/virtio-net.h"
17 #include "libqos/qgraph.h"
18 #include "libqos/virtio-net.h"
21 #define ETH_P_RARP 0x8035
24 #define PCI_SLOT_HP 0x06
27 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
28 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
32 static void rx_test(QVirtioDevice
*dev
,
33 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
36 QTestState
*qts
= global_qtest
;
41 int len
= htonl(sizeof(test
));
42 struct iovec iov
[] = {
45 .iov_len
= sizeof(len
),
48 .iov_len
= sizeof(test
),
53 req_addr
= guest_alloc(alloc
, 64);
55 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, 64, true, false);
56 qvirtqueue_kick(qts
, dev
, vq
, free_head
);
58 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
59 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
61 qvirtio_wait_used_elem(qts
, dev
, vq
, free_head
, NULL
,
62 QVIRTIO_NET_TIMEOUT_US
);
63 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
64 g_assert_cmpstr(buffer
, ==, "TEST");
66 guest_free(alloc
, req_addr
);
69 static void tx_test(QVirtioDevice
*dev
,
70 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
73 QTestState
*qts
= global_qtest
;
80 req_addr
= guest_alloc(alloc
, 64);
81 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
83 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, 64, false, false);
84 qvirtqueue_kick(qts
, dev
, vq
, free_head
);
86 qvirtio_wait_used_elem(qts
, dev
, vq
, free_head
, NULL
,
87 QVIRTIO_NET_TIMEOUT_US
);
88 guest_free(alloc
, req_addr
);
90 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
91 g_assert_cmpint(ret
, ==, sizeof(len
));
94 ret
= qemu_recv(socket
, buffer
, len
, 0);
95 g_assert_cmpstr(buffer
, ==, "TEST");
98 static void rx_stop_cont_test(QVirtioDevice
*dev
,
99 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
102 QTestState
*qts
= global_qtest
;
105 char test
[] = "TEST";
107 int len
= htonl(sizeof(test
));
109 struct iovec iov
[] = {
112 .iov_len
= sizeof(len
),
115 .iov_len
= sizeof(test
),
120 req_addr
= guest_alloc(alloc
, 64);
122 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, 64, true, false);
123 qvirtqueue_kick(qts
, dev
, vq
, free_head
);
125 rsp
= qmp("{ 'execute' : 'stop'}");
128 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
129 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
131 /* We could check the status, but this command is more importantly to
132 * ensure the packet data gets queued in QEMU, before we do 'cont'.
134 rsp
= qmp("{ 'execute' : 'query-status'}");
136 rsp
= qmp("{ 'execute' : 'cont'}");
139 qvirtio_wait_used_elem(qts
, dev
, vq
, free_head
, NULL
,
140 QVIRTIO_NET_TIMEOUT_US
);
141 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
142 g_assert_cmpstr(buffer
, ==, "TEST");
144 guest_free(alloc
, req_addr
);
147 static void send_recv_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];
152 QVirtQueue
*tx
= net_if
->queues
[1];
155 rx_test(dev
, t_alloc
, rx
, sv
[0]);
156 tx_test(dev
, t_alloc
, tx
, sv
[0]);
159 static void stop_cont_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
161 QVirtioNet
*net_if
= obj
;
162 QVirtioDevice
*dev
= net_if
->vdev
;
163 QVirtQueue
*rx
= net_if
->queues
[0];
166 rx_stop_cont_test(dev
, t_alloc
, rx
, sv
[0]);
171 static void hotplug(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
173 QVirtioPCIDevice
*dev
= obj
;
174 QTestState
*qts
= dev
->pdev
->bus
->qts
;
175 const char *arch
= qtest_get_arch();
177 qtest_qmp_device_add(qts
, "virtio-net-pci", "net1",
178 "{'addr': %s}", stringify(PCI_SLOT_HP
));
180 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
181 qpci_unplug_acpi_device_test(qts
, "net1", PCI_SLOT_HP
);
185 static void announce_self(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
192 uint16_t *proto
= (uint16_t *)&buffer
[12];
193 size_t total_received
= 0;
194 uint64_t start
, now
, last_rxt
, deadline
;
196 /* Send a set of packets over a few second period */
197 rsp
= qmp("{ 'execute' : 'announce-self', "
199 " 'initial': 20, 'max': 100,"
200 " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
201 assert(!qdict_haskey(rsp
, "error"));
204 /* Catch the first packet and make sure it's a RARP */
205 ret
= qemu_recv(sv
[0], &len
, sizeof(len
), 0);
206 g_assert_cmpint(ret
, ==, sizeof(len
));
209 ret
= qemu_recv(sv
[0], buffer
, len
, 0);
210 g_assert_cmpint(*proto
, ==, htons(ETH_P_RARP
));
213 * Stop the announcment by settings rounds to 0 on the
216 rsp
= qmp("{ 'execute' : 'announce-self', "
218 " 'initial': 20, 'max': 100,"
219 " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
220 assert(!qdict_haskey(rsp
, "error"));
223 /* Now make sure the packets stop */
225 /* Times are in us */
226 start
= g_get_monotonic_time();
227 /* 30 packets, max gap 100ms, * 4 for wiggle */
228 deadline
= start
+ 1000 * (100 * 30 * 4);
233 ret
= qemu_recv(sv
[0], buffer
, 60, MSG_DONTWAIT
);
235 now
= g_get_monotonic_time();
236 g_assert_cmpint(now
, <, deadline
);
242 total_received
+= ret
;
244 /* Check it's not spewing loads */
245 g_assert_cmpint(total_received
, <, 60 * 30 * 2);
247 g_assert_cmpint(saved_err
, ==, EAGAIN
);
249 /* 400ms, i.e. 4 worst case gaps */
250 if ((now
- last_rxt
) > (1000 * 100 * 4)) {
251 /* Nothings arrived for a while - must have stopped */
256 g_usleep(1000 * 100);
261 static void virtio_net_test_cleanup(void *sockets
)
266 qos_invalidate_command_line();
271 static void *virtio_net_test_setup(GString
*cmd_line
, void *arg
)
274 int *sv
= g_new(int, 2);
276 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
277 g_assert_cmpint(ret
, !=, -1);
279 g_string_append_printf(cmd_line
, " -netdev socket,fd=%d,id=hs0 ", sv
[1]);
281 g_test_queue_destroy(virtio_net_test_cleanup
, sv
);
285 static void large_tx(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
287 QVirtioNet
*dev
= obj
;
288 QVirtQueue
*vq
= dev
->queues
[1];
291 size_t alloc_size
= (size_t)data
/ 64;
292 QTestState
*qts
= global_qtest
;
295 /* Bypass the limitation by pointing several descriptors to a single
297 req_addr
= guest_alloc(t_alloc
, alloc_size
);
298 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, alloc_size
, false, true);
300 for (i
= 0; i
< 64; i
++) {
301 qvirtqueue_add(qts
, vq
, req_addr
, alloc_size
, false, i
!= 63);
303 qvirtqueue_kick(qts
, dev
->vdev
, vq
, free_head
);
305 qvirtio_wait_used_elem(qts
, dev
->vdev
, vq
, free_head
, NULL
,
306 QVIRTIO_NET_TIMEOUT_US
);
307 guest_free(t_alloc
, req_addr
);
310 static void *virtio_net_test_setup_nosocket(GString
*cmd_line
, void *arg
)
312 g_string_append(cmd_line
, " -netdev hubport,hubid=0,id=hs0 ");
316 static void register_virtio_net_test(void)
318 QOSGraphTestOptions opts
= {
319 .before
= virtio_net_test_setup
,
322 qos_add_test("hotplug", "virtio-pci", hotplug
, &opts
);
324 qos_add_test("basic", "virtio-net", send_recv_test
, &opts
);
325 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test
, &opts
);
327 qos_add_test("announce-self", "virtio-net", announce_self
, &opts
);
329 /* These tests do not need a loopback backend. */
330 opts
.before
= virtio_net_test_setup_nosocket
;
331 opts
.arg
= (gpointer
)UINT_MAX
;
332 qos_add_test("large_tx/uint_max", "virtio-net", large_tx
, &opts
);
333 opts
.arg
= (gpointer
)NET_BUFSIZE
;
334 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx
, &opts
);
337 libqos_init(register_virtio_net_test
);