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 "libqtest-single.h"
13 #include "qemu/module.h"
14 #include "qapi/qmp/qdict.h"
15 #include "hw/virtio/virtio-net.h"
16 #include "libqos/qgraph.h"
17 #include "libqos/virtio-net.h"
20 #define ETH_P_RARP 0x8035
23 #define PCI_SLOT_HP 0x06
26 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
27 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
31 static void rx_test(QVirtioDevice
*dev
,
32 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
35 QTestState
*qts
= global_qtest
;
40 int len
= htonl(sizeof(test
));
41 struct iovec iov
[] = {
44 .iov_len
= sizeof(len
),
47 .iov_len
= sizeof(test
),
52 req_addr
= guest_alloc(alloc
, 64);
54 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, 64, true, false);
55 qvirtqueue_kick(qts
, dev
, vq
, free_head
);
57 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
58 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
60 qvirtio_wait_used_elem(qts
, dev
, vq
, free_head
, NULL
,
61 QVIRTIO_NET_TIMEOUT_US
);
62 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
63 g_assert_cmpstr(buffer
, ==, "TEST");
65 guest_free(alloc
, req_addr
);
68 static void tx_test(QVirtioDevice
*dev
,
69 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
72 QTestState
*qts
= global_qtest
;
79 req_addr
= guest_alloc(alloc
, 64);
80 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
82 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, 64, false, false);
83 qvirtqueue_kick(qts
, dev
, vq
, free_head
);
85 qvirtio_wait_used_elem(qts
, dev
, vq
, free_head
, NULL
,
86 QVIRTIO_NET_TIMEOUT_US
);
87 guest_free(alloc
, req_addr
);
89 ret
= recv(socket
, &len
, sizeof(len
), 0);
90 g_assert_cmpint(ret
, ==, sizeof(len
));
93 ret
= recv(socket
, buffer
, len
, 0);
94 g_assert_cmpint(ret
, ==, len
);
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]);
169 static void hotplug(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
171 QVirtioPCIDevice
*dev
= obj
;
172 QTestState
*qts
= dev
->pdev
->bus
->qts
;
173 const char *arch
= qtest_get_arch();
175 if (dev
->pdev
->bus
->not_hotpluggable
) {
176 g_test_skip("pci bus does not support hotplug");
180 qtest_qmp_device_add(qts
, "virtio-net-pci", "net1",
181 "{'addr': %s}", stringify(PCI_SLOT_HP
));
183 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
184 qpci_unplug_acpi_device_test(qts
, "net1", PCI_SLOT_HP
);
188 static void announce_self(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
195 uint16_t *proto
= (uint16_t *)&buffer
[12];
196 size_t total_received
= 0;
197 uint64_t start
, now
, last_rxt
, deadline
;
199 /* Send a set of packets over a few second period */
200 rsp
= qmp("{ 'execute' : 'announce-self', "
202 " 'initial': 20, 'max': 100,"
203 " 'rounds': 300, 'step': 10, 'id': 'bob' } }");
204 assert(!qdict_haskey(rsp
, "error"));
207 /* Catch the first packet and make sure it's a RARP */
208 ret
= recv(sv
[0], &len
, sizeof(len
), 0);
209 g_assert_cmpint(ret
, ==, sizeof(len
));
212 ret
= recv(sv
[0], buffer
, len
, 0);
213 g_assert_cmpint(*proto
, ==, htons(ETH_P_RARP
));
216 * Stop the announcement by settings rounds to 0 on the
219 rsp
= qmp("{ 'execute' : 'announce-self', "
221 " 'initial': 20, 'max': 100,"
222 " 'rounds': 0, 'step': 10, 'id': 'bob' } }");
223 assert(!qdict_haskey(rsp
, "error"));
226 /* Now make sure the packets stop */
228 /* Times are in us */
229 start
= g_get_monotonic_time();
230 /* 30 packets, max gap 100ms, * 4 for wiggle */
231 deadline
= start
+ 1000 * (100 * 30 * 4);
236 ret
= recv(sv
[0], buffer
, 60, MSG_DONTWAIT
);
238 now
= g_get_monotonic_time();
239 g_assert_cmpint(now
, <, deadline
);
245 total_received
+= ret
;
247 /* Check it's not spewing loads */
248 g_assert_cmpint(total_received
, <, 60 * 30 * 2);
250 g_assert_cmpint(saved_err
, ==, EAGAIN
);
252 /* 400ms, i.e. 4 worst case gaps */
253 if ((now
- last_rxt
) > (1000 * 100 * 4)) {
254 /* Nothings arrived for a while - must have stopped */
259 g_usleep(1000 * 100);
264 static void virtio_net_test_cleanup(void *sockets
)
269 qos_invalidate_command_line();
274 static void *virtio_net_test_setup(GString
*cmd_line
, void *arg
)
277 int *sv
= g_new(int, 2);
279 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
280 g_assert_cmpint(ret
, !=, -1);
282 g_string_append_printf(cmd_line
, " -netdev socket,fd=%d,id=hs0 ", sv
[1]);
284 g_test_queue_destroy(virtio_net_test_cleanup
, sv
);
290 static void large_tx(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
292 QVirtioNet
*dev
= obj
;
293 QVirtQueue
*vq
= dev
->queues
[1];
296 size_t alloc_size
= (size_t)data
/ 64;
297 QTestState
*qts
= global_qtest
;
300 /* Bypass the limitation by pointing several descriptors to a single
302 req_addr
= guest_alloc(t_alloc
, alloc_size
);
303 free_head
= qvirtqueue_add(qts
, vq
, req_addr
, alloc_size
, false, true);
305 for (i
= 0; i
< 64; i
++) {
306 qvirtqueue_add(qts
, vq
, req_addr
, alloc_size
, false, i
!= 63);
308 qvirtqueue_kick(qts
, dev
->vdev
, vq
, free_head
);
310 qvirtio_wait_used_elem(qts
, dev
->vdev
, vq
, free_head
, NULL
,
311 QVIRTIO_NET_TIMEOUT_US
);
312 guest_free(t_alloc
, req_addr
);
315 static void *virtio_net_test_setup_nosocket(GString
*cmd_line
, void *arg
)
317 g_string_append(cmd_line
, " -netdev hubport,hubid=0,id=hs0 ");
321 static void register_virtio_net_test(void)
323 QOSGraphTestOptions opts
= { 0 };
326 opts
.before
= virtio_net_test_setup
;
327 qos_add_test("hotplug", "virtio-net-pci", hotplug
, &opts
);
328 qos_add_test("basic", "virtio-net", send_recv_test
, &opts
);
329 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test
, &opts
);
330 qos_add_test("announce-self", "virtio-net", announce_self
, &opts
);
333 /* These tests do not need a loopback backend. */
334 opts
.before
= virtio_net_test_setup_nosocket
;
335 opts
.arg
= (gpointer
)UINT_MAX
;
336 qos_add_test("large_tx/uint_max", "virtio-net", large_tx
, &opts
);
337 opts
.arg
= (gpointer
)NET_BUFSIZE
;
338 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx
, &opts
);
341 libqos_init(register_virtio_net_test
);