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"
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
,
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(vq
, req_addr
, 64, true, false);
55 qvirtqueue_kick(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(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
61 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
62 g_assert_cmpstr(buffer
, ==, "TEST");
64 guest_free(alloc
, req_addr
);
67 static void tx_test(QVirtioDevice
*dev
,
68 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
77 req_addr
= guest_alloc(alloc
, 64);
78 memwrite(req_addr
+ VNET_HDR_SIZE
, "TEST", 4);
80 free_head
= qvirtqueue_add(vq
, req_addr
, 64, false, false);
81 qvirtqueue_kick(dev
, vq
, free_head
);
83 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
84 guest_free(alloc
, req_addr
);
86 ret
= qemu_recv(socket
, &len
, sizeof(len
), 0);
87 g_assert_cmpint(ret
, ==, sizeof(len
));
90 ret
= qemu_recv(socket
, buffer
, len
, 0);
91 g_assert_cmpstr(buffer
, ==, "TEST");
94 static void rx_stop_cont_test(QVirtioDevice
*dev
,
95 QGuestAllocator
*alloc
, QVirtQueue
*vq
,
100 char test
[] = "TEST";
102 int len
= htonl(sizeof(test
));
104 struct iovec iov
[] = {
107 .iov_len
= sizeof(len
),
110 .iov_len
= sizeof(test
),
115 req_addr
= guest_alloc(alloc
, 64);
117 free_head
= qvirtqueue_add(vq
, req_addr
, 64, true, false);
118 qvirtqueue_kick(dev
, vq
, free_head
);
120 rsp
= qmp("{ 'execute' : 'stop'}");
123 ret
= iov_send(socket
, iov
, 2, 0, sizeof(len
) + sizeof(test
));
124 g_assert_cmpint(ret
, ==, sizeof(test
) + sizeof(len
));
126 /* We could check the status, but this command is more importantly to
127 * ensure the packet data gets queued in QEMU, before we do 'cont'.
129 rsp
= qmp("{ 'execute' : 'query-status'}");
131 rsp
= qmp("{ 'execute' : 'cont'}");
134 qvirtio_wait_used_elem(dev
, vq
, free_head
, NULL
, QVIRTIO_NET_TIMEOUT_US
);
135 memread(req_addr
+ VNET_HDR_SIZE
, buffer
, sizeof(test
));
136 g_assert_cmpstr(buffer
, ==, "TEST");
138 guest_free(alloc
, req_addr
);
141 static void send_recv_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
143 QVirtioNet
*net_if
= obj
;
144 QVirtioDevice
*dev
= net_if
->vdev
;
145 QVirtQueue
*rx
= net_if
->queues
[0];
146 QVirtQueue
*tx
= net_if
->queues
[1];
149 rx_test(dev
, t_alloc
, rx
, sv
[0]);
150 tx_test(dev
, t_alloc
, tx
, sv
[0]);
153 static void stop_cont_test(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
155 QVirtioNet
*net_if
= obj
;
156 QVirtioDevice
*dev
= net_if
->vdev
;
157 QVirtQueue
*rx
= net_if
->queues
[0];
160 rx_stop_cont_test(dev
, t_alloc
, rx
, sv
[0]);
165 static void hotplug(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
167 QVirtioPCIDevice
*dev
= obj
;
168 QTestState
*qts
= dev
->pdev
->bus
->qts
;
169 const char *arch
= qtest_get_arch();
171 qtest_qmp_device_add("virtio-net-pci", "net1",
172 "{'addr': %s}", stringify(PCI_SLOT_HP
));
174 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
175 qpci_unplug_acpi_device_test(qts
, "net1", PCI_SLOT_HP
);
179 static void announce_self(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
186 uint16_t *proto
= (uint16_t *)&buffer
[12];
188 rsp
= qmp("{ 'execute' : 'announce-self', "
190 " 'initial': 50, 'max': 550,"
191 " 'rounds': 10, 'step': 50 } }");
192 assert(!qdict_haskey(rsp
, "error"));
195 /* Catch the packet and make sure it's a RARP */
196 ret
= qemu_recv(sv
[0], &len
, sizeof(len
), 0);
197 g_assert_cmpint(ret
, ==, sizeof(len
));
200 ret
= qemu_recv(sv
[0], buffer
, len
, 0);
201 g_assert_cmpint(*proto
, ==, htons(ETH_P_RARP
));
204 static void virtio_net_test_cleanup(void *sockets
)
209 qos_invalidate_command_line();
214 static void *virtio_net_test_setup(GString
*cmd_line
, void *arg
)
217 int *sv
= g_new(int, 2);
219 ret
= socketpair(PF_UNIX
, SOCK_STREAM
, 0, sv
);
220 g_assert_cmpint(ret
, !=, -1);
222 g_string_append_printf(cmd_line
, " -netdev socket,fd=%d,id=hs0 ", sv
[1]);
224 g_test_queue_destroy(virtio_net_test_cleanup
, sv
);
228 static void large_tx(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
230 QVirtioNet
*dev
= obj
;
231 QVirtQueue
*vq
= dev
->queues
[1];
234 size_t alloc_size
= (size_t)data
/ 64;
237 /* Bypass the limitation by pointing several descriptors to a single
239 req_addr
= guest_alloc(t_alloc
, alloc_size
);
240 free_head
= qvirtqueue_add(vq
, req_addr
, alloc_size
, false, true);
242 for (i
= 0; i
< 64; i
++) {
243 qvirtqueue_add(vq
, req_addr
, alloc_size
, false, i
!= 63);
245 qvirtqueue_kick(dev
->vdev
, vq
, free_head
);
247 qvirtio_wait_used_elem(dev
->vdev
, vq
, free_head
, NULL
,
248 QVIRTIO_NET_TIMEOUT_US
);
249 guest_free(t_alloc
, req_addr
);
252 static void *virtio_net_test_setup_nosocket(GString
*cmd_line
, void *arg
)
254 g_string_append(cmd_line
, " -netdev hubport,hubid=0,id=hs0 ");
258 static void register_virtio_net_test(void)
260 QOSGraphTestOptions opts
= {
261 .before
= virtio_net_test_setup
,
264 qos_add_test("hotplug", "virtio-pci", hotplug
, &opts
);
266 qos_add_test("basic", "virtio-net", send_recv_test
, &opts
);
267 qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test
, &opts
);
269 qos_add_test("announce-self", "virtio-net", announce_self
, &opts
);
271 /* These tests do not need a loopback backend. */
272 opts
.before
= virtio_net_test_setup_nosocket
;
273 opts
.arg
= (gpointer
)UINT_MAX
;
274 qos_add_test("large_tx/uint_max", "virtio-net", large_tx
, &opts
);
275 opts
.arg
= (gpointer
)NET_BUFSIZE
;
276 qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx
, &opts
);
279 libqos_init(register_virtio_net_test
);