2014-02-09 04:13:37 +01: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 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2014-02-09 04:13:37 +01:00
|
|
|
#include "libqtest.h"
|
2015-07-17 09:25:53 +02:00
|
|
|
#include "qemu-common.h"
|
|
|
|
#include "qemu/sockets.h"
|
|
|
|
#include "qemu/iov.h"
|
2016-10-17 12:30:23 +02:00
|
|
|
#include "libqos/libqos-pc.h"
|
2015-07-17 09:25:53 +02:00
|
|
|
#include "libqos/virtio.h"
|
|
|
|
#include "libqos/virtio-pci.h"
|
|
|
|
#include "qemu/bswap.h"
|
|
|
|
#include "hw/virtio/virtio-net.h"
|
2016-05-09 14:47:34 +02:00
|
|
|
#include "standard-headers/linux/virtio_ids.h"
|
2016-05-09 14:47:37 +02:00
|
|
|
#include "standard-headers/linux/virtio_ring.h"
|
2014-09-26 11:28:10 +02:00
|
|
|
|
|
|
|
#define PCI_SLOT_HP 0x06
|
2015-07-17 09:25:53 +02:00
|
|
|
#define PCI_SLOT 0x04
|
|
|
|
#define PCI_FN 0x00
|
2014-02-09 04:13:37 +01:00
|
|
|
|
2015-07-17 09:25:53 +02:00
|
|
|
#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
|
|
|
|
#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
|
|
|
|
|
|
|
|
static void test_end(void)
|
|
|
|
{
|
|
|
|
qtest_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
|
|
|
|
static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
|
|
|
|
{
|
|
|
|
QVirtioPCIDevice *dev;
|
|
|
|
|
2016-05-09 14:47:34 +02:00
|
|
|
dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
|
2015-07-17 09:25:53 +02:00
|
|
|
g_assert(dev != NULL);
|
2016-05-09 14:47:34 +02:00
|
|
|
g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
|
|
|
qvirtio_pci_device_enable(dev);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_reset(&dev->vdev);
|
|
|
|
qvirtio_set_acknowledge(&dev->vdev);
|
|
|
|
qvirtio_set_driver(&dev->vdev);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:23 +02:00
|
|
|
static QOSState *pci_test_start(int socket)
|
2015-07-17 09:25:53 +02:00
|
|
|
{
|
2016-10-17 12:30:23 +02:00
|
|
|
const char *cmd = "-netdev socket,fd=%d,id=hs0 -device "
|
|
|
|
"virtio-net-pci,netdev=hs0";
|
2015-07-17 09:25:53 +02:00
|
|
|
|
2016-10-17 12:30:23 +02:00
|
|
|
return qtest_pc_boot(cmd, socket);
|
2015-07-17 09:25:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void driver_init(QVirtioDevice *dev)
|
2015-07-17 09:25:53 +02:00
|
|
|
{
|
|
|
|
uint32_t features;
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
features = qvirtio_get_features(dev);
|
2015-07-17 09:25:53 +02:00
|
|
|
features = features & ~(QVIRTIO_F_BAD_FEATURE |
|
2016-05-09 14:47:37 +02:00
|
|
|
(1u << VIRTIO_RING_F_INDIRECT_DESC) |
|
|
|
|
(1u << VIRTIO_RING_F_EVENT_IDX));
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_features(dev, features);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_set_driver_ok(dev);
|
2015-07-17 09:25:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void rx_test(QVirtioDevice *dev,
|
2015-07-17 09:25:53 +02:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtqueue_kick(dev, vq, free_head);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
|
|
|
ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
|
|
|
|
g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 09:25:53 +02:00
|
|
|
memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
|
|
|
|
g_assert_cmpstr(buffer, ==, "TEST");
|
|
|
|
|
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void tx_test(QVirtioDevice *dev,
|
2015-07-17 09:25:53 +02:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtqueue_kick(dev, vq, free_head);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 09:25:53 +02: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 12:30:21 +02:00
|
|
|
static void rx_stop_cont_test(QVirtioDevice *dev,
|
2015-07-17 09:25:54 +02:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *vq,
|
|
|
|
int socket)
|
|
|
|
{
|
|
|
|
uint64_t req_addr;
|
|
|
|
uint32_t free_head;
|
|
|
|
char test[] = "TEST";
|
|
|
|
char buffer[64];
|
|
|
|
int len = htonl(sizeof(test));
|
2016-07-26 23:15:20 +02:00
|
|
|
QDict *rsp;
|
2015-07-17 09:25:54 +02: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);
|
|
|
|
|
|
|
|
free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtqueue_kick(dev, vq, free_head);
|
2015-07-17 09:25:54 +02:00
|
|
|
|
2016-07-26 23:15:20 +02:00
|
|
|
rsp = qmp("{ 'execute' : 'stop'}");
|
|
|
|
QDECREF(rsp);
|
2015-07-17 09:25:54 +02: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-26 23:15:20 +02:00
|
|
|
rsp = qmp("{ 'execute' : 'query-status'}");
|
|
|
|
QDECREF(rsp);
|
|
|
|
rsp = qmp("{ 'execute' : 'cont'}");
|
|
|
|
QDECREF(rsp);
|
2015-07-17 09:25:54 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
qvirtio_wait_queue_isr(dev, vq, QVIRTIO_NET_TIMEOUT_US);
|
2015-07-17 09:25:54 +02:00
|
|
|
memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
|
|
|
|
g_assert_cmpstr(buffer, ==, "TEST");
|
|
|
|
|
|
|
|
guest_free(alloc, req_addr);
|
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void send_recv_test(QVirtioDevice *dev,
|
2015-07-17 09:25:53 +02:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *rvq,
|
|
|
|
QVirtQueue *tvq, int socket)
|
2014-02-09 04:13:37 +01:00
|
|
|
{
|
2016-10-17 12:30:21 +02:00
|
|
|
rx_test(dev, alloc, rvq, socket);
|
|
|
|
tx_test(dev, alloc, tvq, socket);
|
2014-02-09 04:13:37 +01:00
|
|
|
}
|
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
static void stop_cont_test(QVirtioDevice *dev,
|
2015-07-17 09:25:54 +02:00
|
|
|
QGuestAllocator *alloc, QVirtQueue *rvq,
|
|
|
|
QVirtQueue *tvq, int socket)
|
|
|
|
{
|
2016-10-17 12:30:21 +02:00
|
|
|
rx_stop_cont_test(dev, alloc, rvq, socket);
|
2015-07-17 09:25:54 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 09:25:53 +02:00
|
|
|
static void pci_basic(gconstpointer data)
|
|
|
|
{
|
|
|
|
QVirtioPCIDevice *dev;
|
2016-10-17 12:30:23 +02:00
|
|
|
QOSState *qs;
|
2015-07-17 09:25:53 +02:00
|
|
|
QVirtQueuePCI *tx, *rx;
|
2016-10-17 12:30:21 +02:00
|
|
|
void (*func) (QVirtioDevice *dev,
|
2015-07-17 09:25:53 +02:00
|
|
|
QGuestAllocator *alloc,
|
|
|
|
QVirtQueue *rvq,
|
|
|
|
QVirtQueue *tvq,
|
|
|
|
int socket) = data;
|
|
|
|
int sv[2], ret;
|
|
|
|
|
|
|
|
ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
|
|
|
|
g_assert_cmpint(ret, !=, -1);
|
|
|
|
|
2016-10-17 12:30:23 +02:00
|
|
|
qs = pci_test_start(sv[1]);
|
|
|
|
dev = virtio_net_pci_init(qs->pcibus, PCI_SLOT);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
2016-10-17 12:30:23 +02:00
|
|
|
rx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 0);
|
|
|
|
tx = (QVirtQueuePCI *)qvirtqueue_setup(&dev->vdev, qs->alloc, 1);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
2016-10-17 12:30:21 +02:00
|
|
|
driver_init(&dev->vdev);
|
2016-10-17 12:30:23 +02:00
|
|
|
func(&dev->vdev, qs->alloc, &rx->vq, &tx->vq, sv[0]);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
|
|
|
/* End test */
|
|
|
|
close(sv[0]);
|
2016-10-17 12:30:23 +02:00
|
|
|
qvirtqueue_cleanup(dev->vdev.bus, &tx->vq, qs->alloc);
|
|
|
|
qvirtqueue_cleanup(dev->vdev.bus, &rx->vq, qs->alloc);
|
2015-07-17 09:25:53 +02:00
|
|
|
qvirtio_pci_device_disable(dev);
|
2016-07-26 23:15:20 +02:00
|
|
|
g_free(dev->pdev);
|
2015-07-17 09:25:53 +02:00
|
|
|
g_free(dev);
|
2016-10-17 12:30:23 +02:00
|
|
|
qtest_shutdown(qs);
|
2015-07-17 09:25:53 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-26 11:28:10 +02:00
|
|
|
static void hotplug(void)
|
|
|
|
{
|
2015-07-17 09:25:53 +02:00
|
|
|
qtest_start("-device virtio-net-pci");
|
|
|
|
|
2014-09-26 11:28:10 +02:00
|
|
|
qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
|
|
|
|
qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
|
2015-07-17 09:25:53 +02:00
|
|
|
|
|
|
|
test_end();
|
2014-09-26 11:28:10 +02:00
|
|
|
}
|
|
|
|
|
2014-02-09 04:13:37 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
2015-07-17 09:25:53 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
|
2015-07-17 09:25:54 +02:00
|
|
|
qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
|
|
|
|
stop_cont_test, pci_basic);
|
2015-07-17 09:25:53 +02:00
|
|
|
#endif
|
2014-09-26 11:28:10 +02:00
|
|
|
qtest_add_func("/virtio/net/pci/hotplug", hotplug);
|
2014-02-09 04:13:37 +01:00
|
|
|
|
2016-06-13 23:57:58 +02:00
|
|
|
return g_test_run();
|
2014-02-09 04:13:37 +01:00
|
|
|
}
|