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"
19 #define ETH_P_RARP 0x8035
22 #define PCI_SLOT_HP 0x06
25 #define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
26 #define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
30 static void rx_test(QVirtioDevice
*dev
,
31 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
38 int len
= htonl(sizeof(test
));
39 struct iovec iov
[] = {
42 .iov_len
= sizeof(len
),
45 .iov_len
= sizeof(test
),
50 req_addr
= guest_alloc(alloc
, 64);
52 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
53 qvirtqueue_kick(dev
, vq
, free_head
);
55 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
56 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
58 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
59 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
60 g_assert_cmpstr(buffer
, ==, "TEST");
62 guest_free(alloc
, req_addr
);
65 static void tx_test(QVirtioDevice
*dev
,
66 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
75 req_addr
= guest_alloc(alloc
, 64);
76 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
78 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
79 qvirtqueue_kick(dev
, vq
, free_head
);
81 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
82 guest_free(alloc
, req_addr
);
84 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
85 g_assert_cmpint(ret
, ==, sizeof(len
));
88 ret
= qemu_recv(socket
, buffer
, len
, 0);
89 g_assert_cmpstr(buffer
, ==, "TEST");
92 static void rx_stop_cont_test(QVirtioDevice
*dev
,
93 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
100 int len
= htonl(sizeof(test
));
102 struct iovec iov
[] = {
105 .iov_len
= sizeof(len
),
108 .iov_len
= sizeof(test
),
113 req_addr
= guest_alloc(alloc
, 64);
115 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
116 qvirtqueue_kick(dev
, vq
, free_head
);
118 rsp
= qmp("{ 'execute' : 'stop'}");
121 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
122 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
124 /* We could check the status, but this command is more importantly to
125 * ensure the packet data gets queued in QEMU, before we do 'cont'.
127 rsp
= qmp("{ 'execute' : 'query-status'}");
129 rsp
= qmp("{ 'execute' : 'cont'}");
132 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
133 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
134 g_assert_cmpstr(buffer
, ==, "TEST");
136 guest_free(alloc
, req_addr
);
139 static void send_recv_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
141 QVirtioNet
*net_if
= obj
;
142 QVirtioDevice
*dev
= net_if
->vdev
;
143 QVirtQueue
*rx
= net_if
->queues
[0];
144 QVirtQueue
*tx
= net_if
->queues
[1];
147 rx_test(dev
, t_alloc
, rx
, sv
[0]);
148 tx_test(dev
, t_alloc
, tx
, sv
[0]);
151 static void stop_cont_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
153 QVirtioNet
*net_if
= obj
;
154 QVirtioDevice
*dev
= net_if
->vdev
;
155 QVirtQueue
*rx
= net_if
->queues
[0];
158 rx_stop_cont_test(dev
, t_alloc
, rx
, sv
[0]);
163 static void hotplug(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
165 QVirtioPCIDevice
*dev
= obj
;
166 QTestState
*qts
= dev
->pdev
->bus
->qts
;
167 const char *arch
= qtest_get_arch();
169 qtest_qmp_device_add("virtio-net-pci", "net1",
170 "{'addr': %s}", stringify(PCI_SLOT_HP
));
172 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
173 qpci_unplug_acpi_device_test(qts
, "net1", PCI_SLOT_HP
);
177 static void announce_self(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
184 uint16_t *proto
= (uint16_t *)&buffer
[12];
186 rsp
= qmp("{ 'execute' : 'announce-self', "
188 " 'initial': 50, 'max': 550,"
189 " 'rounds': 10, 'step': 50 } }");
190 assert(!qdict_haskey(rsp
, "error"));
193 /* Catch the packet and make sure it's a RARP */
194 ret
= qemu_recv(sv
[0], &len
, sizeof(len
), 0);
195 g_assert_cmpint(ret
, ==, sizeof(len
));
198 ret
= qemu_recv(sv
[0], buffer
, len
, 0);
199 g_assert_cmpint(*proto
, ==, htons(ETH_P_RARP
));
202 static void virtio_net_test_cleanup(void *sockets
)
207 qos_invalidate_command_line();
212 static void *virtio_net_test_setup(GString
*cmd_line
, void *arg
)
215 int *sv
= g_new(int, 2);
217 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
218 g_assert_cmpint(ret
, !=, -1);
220 g_string_append_printf(cmd_line
, " -netdev socket,fd=%d,id=hs0 ", sv
[1]);
222 g_test_queue_destroy(virtio_net_test_cleanup
, sv
);
226 static void large_tx(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
228 QVirtioNet
*dev
= obj
;
229 QVirtQueue
*vq
= dev
->queues
[1];
232 size_t alloc_size
= (size_t)data
/ 64;
235 /* Bypass the limitation by pointing several descriptors to a single
237 req_addr
= guest_alloc(t_alloc
, alloc_size
);
238 free_head
= qvirtqueue_add(vq
, req_addr
, alloc_size
, false, true);
240 for (i
= 0; i
< 64; i
++) {
241 qvirtqueue_add(vq
, req_addr
, alloc_size
, false, i
!= 63);
243 qvirtqueue_kick(dev
->vdev
, vq
, free_head
);
245 qvirtio_wait_used_elem(dev
->vdev
, vq
, free_head
, NULL
,
246 QVIRTIO_NET_TIMEOUT_US
);
247 guest_free(t_alloc
, req_addr
);
250 static void *virtio_net_test_setup_nosocket(GString
*cmd_line
, void *arg
)
252 g_string_append(cmd_line
, " -netdev hubport,hubid=0,id=hs0 ");
256 static void register_virtio_net_test(void)
258 QOSGraphTestOptions opts
= {
259 .before
= virtio_net_test_setup
,
262 qos_add_test("hotplug", "virtio-pci", hotplug
, &opts
);
264 qos_add_test("basic", "virtio-net", send_recv_test
, &opts
);
265 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test
, &opts
);
267 qos_add_test("announce-self", "virtio-net", announce_self
, &opts
);
269 /* These tests do not need a loopback backend. */
270 opts
.before
= virtio_net_test_setup_nosocket
;
271 opts
.arg
= (gpointer
)UINT_MAX
;
272 qos_add_test("large_tx/uint_max", "virtio-net", large_tx
, &opts
);
273 opts
.arg
= (gpointer
)NET_BUFSIZE
;
274 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx
, &opts
);
277 libqos_init(register_virtio_net_test
);