2014-02-09 07:13:37 +04:00
|
|
|
/*
|
|
|
|
* QTest testcase for VirtIO NIC
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 SUSE LINUX Products GmbH
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:08 +03:00
|
|
|
#include "qemu-common.h"
|
2019-09-03 08:50:26 +03:00
|
|
|
#include "libqtest-single.h"
|
2015-07-17 10:25:53 +03:00
|
|
|
#include "qemu/iov.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2015-07-17 10:25:53 +03:00
|
|
|
#include "hw/virtio/virtio-net.h"
|
2018-07-30 23:22:20 +03:00
|
|
|
#include "libqos/qgraph.h"
|
|
|
|
#include "libqos/virtio-net.h"
|
2014-09-26 13:28:10 +04:00
|
|
|
|
2019-03-12 19:31:30 +03:00
|
|
|
#ifndef ETH_P_RARP
|
|
|
|
#define ETH_P_RARP 0x8035
|
|
|
|
#endif
|
|
|
|
|
2014-09-26 13:28:10 +04:00
|
|
|
#define PCI_SLOT_HP 0x06
|
2015-07-17 10:25:53 +03:00
|
|
|
#define PCI_SLOT 0x04
|
2014-02-09 07:13:37 +04:00
|
|
|
|
2015-07-17 10:25:53 +03:00
|
|
|
#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
|
|
|
|
#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
static void rx_test(QVirtioDevice *dev,
|
2015-07-17 10:25:53 +03:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
2019-05-18 11:23:24 +03:00
|
|
|
QTestState *qts = global_qtest;
|
2015-07-17 10:25:53 +03:00
|
|
|
uint64_t req_addr;
|
|
|
|
uint32_t free_head;
|
|
|
|
char test[] = "TEST";
|
|
|
|
char buffer[64];
|
|
|
|
int len = htonl(sizeof(test));
|
|
|
|
struct iovec iov[] = {
|
|
|
|
{
|
|
|
|
.iov_base = &len,
|
|
|
|
.iov_len = sizeof(len),
|
|
|
|
}, {
|
|
|
|
.iov_base = test,
|
|
|
|
.iov_len = sizeof(test),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
req_addr = guest_alloc(alloc, 64);
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
|
|
|
|
qvirtqueue_kick(qts, dev, vq, free_head);
|
2015-07-17 10:25:53 +03:00
|
|
|
|
|
|
|
ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
|
|
|
|
g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
|
|
|
|
QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 10:25:53 +03:00
|
|
|
memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
|
|
|
|
g_assert_cmpstr(buffer, ==, "TEST");
|
|
|
|
|
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
static void tx_test(QVirtioDevice *dev,
|
2015-07-17 10:25:53 +03:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
2019-05-18 11:23:24 +03:00
|
|
|
QTestState *qts = global_qtest;
|
2015-07-17 10:25:53 +03:00
|
|
|
uint64_t req_addr;
|
|
|
|
uint32_t free_head;
|
|
|
|
uint32_t len;
|
|
|
|
char buffer[64];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
req_addr = guest_alloc(alloc, 64);
|
|
|
|
memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
free_head = qvirtqueue_add(qts, vq, req_addr, 64, false, false);
|
|
|
|
qvirtqueue_kick(qts, dev, vq, free_head);
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
|
|
|
|
QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 10:25:53 +03:00
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
|
|
|
|
ret = qemu_recv(socket, &len, sizeof(len), 0);
|
|
|
|
g_assert_cmpint(ret, ==, sizeof(len));
|
|
|
|
len = ntohl(len);
|
|
|
|
|
|
|
|
ret = qemu_recv(socket, buffer, len, 0);
|
|
|
|
g_assert_cmpstr(buffer, ==, "TEST");
|
|
|
|
}
|
|
|
|
|
2016-10-17 13:30:21 +03:00
|
|
|
static void rx_stop_cont_test(QVirtioDevice *dev,
|
2015-07-17 10:25:54 +03:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
2019-05-18 11:23:24 +03:00
|
|
|
QTestState *qts = global_qtest;
|
2015-07-17 10:25:54 +03:00
|
|
|
uint64_t req_addr;
|
|
|
|
uint32_t free_head;
|
|
|
|
char test[] = "TEST";
|
|
|
|
char buffer[64];
|
|
|
|
int len = htonl(sizeof(test));
|
2016-07-27 00:15:20 +03:00
|
|
|
QDict *rsp;
|
2015-07-17 10:25:54 +03:00
|
|
|
struct iovec iov[] = {
|
|
|
|
{
|
|
|
|
.iov_base = &len,
|
|
|
|
.iov_len = sizeof(len),
|
|
|
|
}, {
|
|
|
|
.iov_base = test,
|
|
|
|
.iov_len = sizeof(test),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
req_addr = guest_alloc(alloc, 64);
|
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
free_head = qvirtqueue_add(qts, vq, req_addr, 64, true, false);
|
|
|
|
qvirtqueue_kick(qts, dev, vq, free_head);
|
2015-07-17 10:25:54 +03:00
|
|
|
|
2016-07-27 00:15:20 +03:00
|
|
|
rsp = qmp("{ 'execute' : 'stop'}");
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(rsp);
|
2015-07-17 10:25:54 +03:00
|
|
|
|
|
|
|
ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
|
|
|
|
g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
|
|
|
|
|
|
|
|
/* We could check the status, but this command is more importantly to
|
|
|
|
* ensure the packet data gets queued in QEMU, before we do 'cont'.
|
|
|
|
*/
|
2016-07-27 00:15:20 +03:00
|
|
|
rsp = qmp("{ 'execute' : 'query-status'}");
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(rsp);
|
2016-07-27 00:15:20 +03:00
|
|
|
rsp = qmp("{ 'execute' : 'cont'}");
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(rsp);
|
2015-07-17 10:25:54 +03:00
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtio_wait_used_elem(qts, dev, vq, free_head, NULL,
|
|
|
|
QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 10:25:54 +03:00
|
|
|
memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
|
|
|
|
g_assert_cmpstr(buffer, ==, "TEST");
|
|
|
|
|
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void send_recv_test(void *obj, void *data, QGuestAllocator *t_alloc)
|
2014-02-09 07:13:37 +04:00
|
|
|
{
|
2018-07-30 23:22:20 +03:00
|
|
|
QVirtioNet *net_if = obj;
|
|
|
|
QVirtioDevice *dev = net_if->vdev;
|
2018-10-22 13:37:21 +03:00
|
|
|
QVirtQueue *rx = net_if->queues[0];
|
|
|
|
QVirtQueue *tx = net_if->queues[1];
|
2018-07-30 23:22:20 +03:00
|
|
|
int *sv = data;
|
|
|
|
|
|
|
|
rx_test(dev, t_alloc, rx, sv[0]);
|
|
|
|
tx_test(dev, t_alloc, tx, sv[0]);
|
2014-02-09 07:13:37 +04:00
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void stop_cont_test(void *obj, void *data, QGuestAllocator *t_alloc)
|
2015-07-17 10:25:54 +03:00
|
|
|
{
|
2018-07-30 23:22:20 +03:00
|
|
|
QVirtioNet *net_if = obj;
|
|
|
|
QVirtioDevice *dev = net_if->vdev;
|
2018-10-22 13:37:21 +03:00
|
|
|
QVirtQueue *rx = net_if->queues[0];
|
2018-07-30 23:22:20 +03:00
|
|
|
int *sv = data;
|
|
|
|
|
|
|
|
rx_stop_cont_test(dev, t_alloc, rx, sv[0]);
|
2015-07-17 10:25:54 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
#endif
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void hotplug(void *obj, void *data, QGuestAllocator *t_alloc)
|
|
|
|
{
|
2019-04-09 15:25:57 +03:00
|
|
|
QVirtioPCIDevice *dev = obj;
|
|
|
|
QTestState *qts = dev->pdev->bus->qts;
|
2018-07-30 23:22:20 +03:00
|
|
|
const char *arch = qtest_get_arch();
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2019-07-22 18:10:55 +03:00
|
|
|
qtest_qmp_device_add(qts, "virtio-net-pci", "net1",
|
2018-07-30 23:22:20 +03:00
|
|
|
"{'addr': %s}", stringify(PCI_SLOT_HP));
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
|
2019-04-09 15:25:57 +03:00
|
|
|
qpci_unplug_acpi_device_test(qts, "net1", PCI_SLOT_HP);
|
2018-07-30 23:22:20 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2019-03-12 19:31:30 +03:00
|
|
|
static void announce_self(void *obj, void *data, QGuestAllocator *t_alloc)
|
|
|
|
{
|
|
|
|
int *sv = data;
|
|
|
|
char buffer[60];
|
|
|
|
int len;
|
|
|
|
QDict *rsp;
|
|
|
|
int ret;
|
|
|
|
uint16_t *proto = (uint16_t *)&buffer[12];
|
2019-06-20 21:47:06 +03:00
|
|
|
size_t total_received = 0;
|
|
|
|
uint64_t start, now, last_rxt, deadline;
|
2019-03-12 19:31:30 +03:00
|
|
|
|
2019-06-20 21:47:06 +03:00
|
|
|
/* Send a set of packets over a few second period */
|
2019-03-12 19:31:30 +03:00
|
|
|
rsp = qmp("{ 'execute' : 'announce-self', "
|
|
|
|
" 'arguments': {"
|
2019-06-20 21:47:06 +03:00
|
|
|
" 'initial': 20, 'max': 100,"
|
|
|
|
" 'rounds': 300, 'step': 10, 'id': 'bob' } }");
|
2019-03-12 19:31:30 +03:00
|
|
|
assert(!qdict_haskey(rsp, "error"));
|
|
|
|
qobject_unref(rsp);
|
|
|
|
|
2019-06-20 21:47:06 +03:00
|
|
|
/* Catch the first packet and make sure it's a RARP */
|
2019-03-12 19:31:30 +03:00
|
|
|
ret = qemu_recv(sv[0], &len, sizeof(len), 0);
|
|
|
|
g_assert_cmpint(ret, ==, sizeof(len));
|
|
|
|
len = ntohl(len);
|
|
|
|
|
|
|
|
ret = qemu_recv(sv[0], buffer, len, 0);
|
|
|
|
g_assert_cmpint(*proto, ==, htons(ETH_P_RARP));
|
2019-06-20 21:47:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop the announcment by settings rounds to 0 on the
|
|
|
|
* existing timer.
|
|
|
|
*/
|
|
|
|
rsp = qmp("{ 'execute' : 'announce-self', "
|
|
|
|
" 'arguments': {"
|
|
|
|
" 'initial': 20, 'max': 100,"
|
|
|
|
" 'rounds': 0, 'step': 10, 'id': 'bob' } }");
|
|
|
|
assert(!qdict_haskey(rsp, "error"));
|
|
|
|
qobject_unref(rsp);
|
|
|
|
|
|
|
|
/* Now make sure the packets stop */
|
|
|
|
|
|
|
|
/* Times are in us */
|
|
|
|
start = g_get_monotonic_time();
|
|
|
|
/* 30 packets, max gap 100ms, * 4 for wiggle */
|
|
|
|
deadline = start + 1000 * (100 * 30 * 4);
|
|
|
|
last_rxt = start;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
int saved_err;
|
|
|
|
ret = qemu_recv(sv[0], buffer, 60, MSG_DONTWAIT);
|
|
|
|
saved_err = errno;
|
|
|
|
now = g_get_monotonic_time();
|
|
|
|
g_assert_cmpint(now, <, deadline);
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
if (ret) {
|
|
|
|
last_rxt = now;
|
|
|
|
}
|
|
|
|
total_received += ret;
|
|
|
|
|
|
|
|
/* Check it's not spewing loads */
|
|
|
|
g_assert_cmpint(total_received, <, 60 * 30 * 2);
|
|
|
|
} else {
|
|
|
|
g_assert_cmpint(saved_err, ==, EAGAIN);
|
|
|
|
|
|
|
|
/* 400ms, i.e. 4 worst case gaps */
|
|
|
|
if ((now - last_rxt) > (1000 * 100 * 4)) {
|
|
|
|
/* Nothings arrived for a while - must have stopped */
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 100ms */
|
|
|
|
g_usleep(1000 * 100);
|
|
|
|
}
|
|
|
|
};
|
2019-03-12 19:31:30 +03:00
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void virtio_net_test_cleanup(void *sockets)
|
|
|
|
{
|
|
|
|
int *sv = sockets;
|
2015-07-17 10:25:53 +03:00
|
|
|
|
|
|
|
close(sv[0]);
|
2018-07-30 23:22:20 +03:00
|
|
|
qos_invalidate_command_line();
|
|
|
|
close(sv[1]);
|
|
|
|
g_free(sv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *virtio_net_test_setup(GString *cmd_line, void *arg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int *sv = g_new(int, 2);
|
|
|
|
|
|
|
|
ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
|
|
|
|
g_assert_cmpint(ret, !=, -1);
|
|
|
|
|
|
|
|
g_string_append_printf(cmd_line, " -netdev socket,fd=%d,id=hs0 ", sv[1]);
|
|
|
|
|
|
|
|
g_test_queue_destroy(virtio_net_test_cleanup, sv);
|
|
|
|
return sv;
|
2015-07-17 10:25:53 +03:00
|
|
|
}
|
2018-12-04 06:53:47 +03:00
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void large_tx(void *obj, void *data, QGuestAllocator *t_alloc)
|
2018-12-04 06:53:47 +03:00
|
|
|
{
|
2018-07-30 23:22:20 +03:00
|
|
|
QVirtioNet *dev = obj;
|
|
|
|
QVirtQueue *vq = dev->queues[1];
|
2018-12-04 06:53:47 +03:00
|
|
|
uint64_t req_addr;
|
|
|
|
uint32_t free_head;
|
|
|
|
size_t alloc_size = (size_t)data / 64;
|
2019-05-18 11:23:24 +03:00
|
|
|
QTestState *qts = global_qtest;
|
2018-12-04 06:53:47 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Bypass the limitation by pointing several descriptors to a single
|
|
|
|
* smaller area */
|
2018-07-30 23:22:20 +03:00
|
|
|
req_addr = guest_alloc(t_alloc, alloc_size);
|
2019-05-18 11:23:24 +03:00
|
|
|
free_head = qvirtqueue_add(qts, vq, req_addr, alloc_size, false, true);
|
2018-12-04 06:53:47 +03:00
|
|
|
|
|
|
|
for (i = 0; i < 64; i++) {
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtqueue_add(qts, vq, req_addr, alloc_size, false, i != 63);
|
2018-12-04 06:53:47 +03:00
|
|
|
}
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtqueue_kick(qts, dev->vdev, vq, free_head);
|
2018-12-04 06:53:47 +03:00
|
|
|
|
2019-05-18 11:23:24 +03:00
|
|
|
qvirtio_wait_used_elem(qts, dev->vdev, vq, free_head, NULL,
|
2018-12-04 06:53:47 +03:00
|
|
|
QVIRTIO_NET_TIMEOUT_US);
|
2018-07-30 23:22:20 +03:00
|
|
|
guest_free(t_alloc, req_addr);
|
2018-12-04 06:53:47 +03:00
|
|
|
}
|
2015-07-17 10:25:53 +03:00
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void *virtio_net_test_setup_nosocket(GString *cmd_line, void *arg)
|
2014-09-26 13:28:10 +04:00
|
|
|
{
|
2018-07-30 23:22:20 +03:00
|
|
|
g_string_append(cmd_line, " -netdev hubport,hubid=0,id=hs0 ");
|
|
|
|
return arg;
|
2014-09-26 13:28:10 +04:00
|
|
|
}
|
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
static void register_virtio_net_test(void)
|
2014-02-09 07:13:37 +04:00
|
|
|
{
|
2018-07-30 23:22:20 +03:00
|
|
|
QOSGraphTestOptions opts = {
|
|
|
|
.before = virtio_net_test_setup,
|
|
|
|
};
|
|
|
|
|
|
|
|
qos_add_test("hotplug", "virtio-pci", hotplug, &opts);
|
2015-07-17 10:25:53 +03:00
|
|
|
#ifndef _WIN32
|
2018-07-30 23:22:20 +03:00
|
|
|
qos_add_test("basic", "virtio-net", send_recv_test, &opts);
|
|
|
|
qos_add_test("rx_stop_cont", "virtio-net", stop_cont_test, &opts);
|
2015-07-17 10:25:53 +03:00
|
|
|
#endif
|
2019-03-12 19:31:30 +03:00
|
|
|
qos_add_test("announce-self", "virtio-net", announce_self, &opts);
|
2014-02-09 07:13:37 +04:00
|
|
|
|
2018-07-30 23:22:20 +03:00
|
|
|
/* These tests do not need a loopback backend. */
|
|
|
|
opts.before = virtio_net_test_setup_nosocket;
|
|
|
|
opts.arg = (gpointer)UINT_MAX;
|
|
|
|
qos_add_test("large_tx/uint_max", "virtio-net", large_tx, &opts);
|
|
|
|
opts.arg = (gpointer)NET_BUFSIZE;
|
|
|
|
qos_add_test("large_tx/net_bufsize", "virtio-net", large_tx, &opts);
|
2014-02-09 07:13:37 +04:00
|
|
|
}
|
2018-07-30 23:22:20 +03:00
|
|
|
|
|
|
|
libqos_init(register_virtio_net_test);
|