qemu/tests/virtio-net-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for VirtIO NIC
   3 *
   4 * Copyright (c) 2014 SUSE LINUX Products GmbH
   5 *
   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.
   8 */
   9
  10#include "qemu/osdep.h"
  11#include "libqtest.h"
  12#include "qemu-common.h"
  13#include "qemu/sockets.h"
  14#include "qemu/iov.h"
  15#include "libqos/pci-pc.h"
  16#include "libqos/virtio.h"
  17#include "libqos/virtio-pci.h"
  18#include "libqos/malloc.h"
  19#include "libqos/malloc-pc.h"
  20#include "libqos/malloc-generic.h"
  21#include "qemu/bswap.h"
  22#include "hw/virtio/virtio-net.h"
  23#include "standard-headers/linux/virtio_ids.h"
  24#include "standard-headers/linux/virtio_ring.h"
  25
  26#define PCI_SLOT_HP             0x06
  27#define PCI_SLOT                0x04
  28#define PCI_FN                  0x00
  29
  30#define QVIRTIO_NET_TIMEOUT_US (30 * 1000 * 1000)
  31#define VNET_HDR_SIZE sizeof(struct virtio_net_hdr_mrg_rxbuf)
  32
  33static void test_end(void)
  34{
  35    qtest_end();
  36}
  37
  38#ifndef _WIN32
  39
  40static QVirtioPCIDevice *virtio_net_pci_init(QPCIBus *bus, int slot)
  41{
  42    QVirtioPCIDevice *dev;
  43
  44    dev = qvirtio_pci_device_find(bus, VIRTIO_ID_NET);
  45    g_assert(dev != NULL);
  46    g_assert_cmphex(dev->vdev.device_type, ==, VIRTIO_ID_NET);
  47
  48    qvirtio_pci_device_enable(dev);
  49    qvirtio_reset(&qvirtio_pci, &dev->vdev);
  50    qvirtio_set_acknowledge(&qvirtio_pci, &dev->vdev);
  51    qvirtio_set_driver(&qvirtio_pci, &dev->vdev);
  52
  53    return dev;
  54}
  55
  56static QPCIBus *pci_test_start(int socket)
  57{
  58    char *cmdline;
  59
  60    cmdline = g_strdup_printf("-netdev socket,fd=%d,id=hs0 -device "
  61                              "virtio-net-pci,netdev=hs0", socket);
  62    qtest_start(cmdline);
  63    g_free(cmdline);
  64
  65    return qpci_init_pc();
  66}
  67
  68static void driver_init(const QVirtioBus *bus, QVirtioDevice *dev)
  69{
  70    uint32_t features;
  71
  72    features = qvirtio_get_features(bus, dev);
  73    features = features & ~(QVIRTIO_F_BAD_FEATURE |
  74                            (1u << VIRTIO_RING_F_INDIRECT_DESC) |
  75                            (1u << VIRTIO_RING_F_EVENT_IDX));
  76    qvirtio_set_features(bus, dev, features);
  77
  78    qvirtio_set_driver_ok(bus, dev);
  79}
  80
  81static void rx_test(const QVirtioBus *bus, QVirtioDevice *dev,
  82                    QGuestAllocator *alloc, QVirtQueue *vq,
  83                    int socket)
  84{
  85    uint64_t req_addr;
  86    uint32_t free_head;
  87    char test[] = "TEST";
  88    char buffer[64];
  89    int len = htonl(sizeof(test));
  90    struct iovec iov[] = {
  91        {
  92            .iov_base = &len,
  93            .iov_len = sizeof(len),
  94        }, {
  95            .iov_base = test,
  96            .iov_len = sizeof(test),
  97        },
  98    };
  99    int ret;
 100
 101    req_addr = guest_alloc(alloc, 64);
 102
 103    free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
 104    qvirtqueue_kick(bus, dev, vq, free_head);
 105
 106    ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
 107    g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
 108
 109    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
 110    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
 111    g_assert_cmpstr(buffer, ==, "TEST");
 112
 113    guest_free(alloc, req_addr);
 114}
 115
 116static void tx_test(const QVirtioBus *bus, QVirtioDevice *dev,
 117                    QGuestAllocator *alloc, QVirtQueue *vq,
 118                    int socket)
 119{
 120    uint64_t req_addr;
 121    uint32_t free_head;
 122    uint32_t len;
 123    char buffer[64];
 124    int ret;
 125
 126    req_addr = guest_alloc(alloc, 64);
 127    memwrite(req_addr + VNET_HDR_SIZE, "TEST", 4);
 128
 129    free_head = qvirtqueue_add(vq, req_addr, 64, false, false);
 130    qvirtqueue_kick(bus, dev, vq, free_head);
 131
 132    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
 133    guest_free(alloc, req_addr);
 134
 135    ret = qemu_recv(socket, &len, sizeof(len), 0);
 136    g_assert_cmpint(ret, ==, sizeof(len));
 137    len = ntohl(len);
 138
 139    ret = qemu_recv(socket, buffer, len, 0);
 140    g_assert_cmpstr(buffer, ==, "TEST");
 141}
 142
 143static void rx_stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev,
 144                              QGuestAllocator *alloc, QVirtQueue *vq,
 145                              int socket)
 146{
 147    uint64_t req_addr;
 148    uint32_t free_head;
 149    char test[] = "TEST";
 150    char buffer[64];
 151    int len = htonl(sizeof(test));
 152    QDict *rsp;
 153    struct iovec iov[] = {
 154        {
 155            .iov_base = &len,
 156            .iov_len = sizeof(len),
 157        }, {
 158            .iov_base = test,
 159            .iov_len = sizeof(test),
 160        },
 161    };
 162    int ret;
 163
 164    req_addr = guest_alloc(alloc, 64);
 165
 166    free_head = qvirtqueue_add(vq, req_addr, 64, true, false);
 167    qvirtqueue_kick(bus, dev, vq, free_head);
 168
 169    rsp = qmp("{ 'execute' : 'stop'}");
 170    QDECREF(rsp);
 171
 172    ret = iov_send(socket, iov, 2, 0, sizeof(len) + sizeof(test));
 173    g_assert_cmpint(ret, ==, sizeof(test) + sizeof(len));
 174
 175    /* We could check the status, but this command is more importantly to
 176     * ensure the packet data gets queued in QEMU, before we do 'cont'.
 177     */
 178    rsp = qmp("{ 'execute' : 'query-status'}");
 179    QDECREF(rsp);
 180    rsp = qmp("{ 'execute' : 'cont'}");
 181    QDECREF(rsp);
 182
 183    qvirtio_wait_queue_isr(bus, dev, vq, QVIRTIO_NET_TIMEOUT_US);
 184    memread(req_addr + VNET_HDR_SIZE, buffer, sizeof(test));
 185    g_assert_cmpstr(buffer, ==, "TEST");
 186
 187    guest_free(alloc, req_addr);
 188}
 189
 190static void send_recv_test(const QVirtioBus *bus, QVirtioDevice *dev,
 191                           QGuestAllocator *alloc, QVirtQueue *rvq,
 192                           QVirtQueue *tvq, int socket)
 193{
 194    rx_test(bus, dev, alloc, rvq, socket);
 195    tx_test(bus, dev, alloc, tvq, socket);
 196}
 197
 198static void stop_cont_test(const QVirtioBus *bus, QVirtioDevice *dev,
 199                           QGuestAllocator *alloc, QVirtQueue *rvq,
 200                           QVirtQueue *tvq, int socket)
 201{
 202    rx_stop_cont_test(bus, dev, alloc, rvq, socket);
 203}
 204
 205static void pci_basic(gconstpointer data)
 206{
 207    QVirtioPCIDevice *dev;
 208    QPCIBus *bus;
 209    QVirtQueuePCI *tx, *rx;
 210    QGuestAllocator *alloc;
 211    void (*func) (const QVirtioBus *bus,
 212                  QVirtioDevice *dev,
 213                  QGuestAllocator *alloc,
 214                  QVirtQueue *rvq,
 215                  QVirtQueue *tvq,
 216                  int socket) = data;
 217    int sv[2], ret;
 218
 219    ret = socketpair(PF_UNIX, SOCK_STREAM, 0, sv);
 220    g_assert_cmpint(ret, !=, -1);
 221
 222    bus = pci_test_start(sv[1]);
 223    dev = virtio_net_pci_init(bus, PCI_SLOT);
 224
 225    alloc = pc_alloc_init();
 226    rx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
 227                                           alloc, 0);
 228    tx = (QVirtQueuePCI *)qvirtqueue_setup(&qvirtio_pci, &dev->vdev,
 229                                           alloc, 1);
 230
 231    driver_init(&qvirtio_pci, &dev->vdev);
 232    func(&qvirtio_pci, &dev->vdev, alloc, &rx->vq, &tx->vq, sv[0]);
 233
 234    /* End test */
 235    close(sv[0]);
 236    qvirtqueue_cleanup(&qvirtio_pci, &tx->vq, alloc);
 237    qvirtqueue_cleanup(&qvirtio_pci, &rx->vq, alloc);
 238    pc_alloc_uninit(alloc);
 239    qvirtio_pci_device_disable(dev);
 240    g_free(dev->pdev);
 241    g_free(dev);
 242    qpci_free_pc(bus);
 243    test_end();
 244}
 245#endif
 246
 247static void hotplug(void)
 248{
 249    qtest_start("-device virtio-net-pci");
 250
 251    qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
 252    qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
 253
 254    test_end();
 255}
 256
 257int main(int argc, char **argv)
 258{
 259    g_test_init(&argc, &argv, NULL);
 260#ifndef _WIN32
 261    qtest_add_data_func("/virtio/net/pci/basic", send_recv_test, pci_basic);
 262    qtest_add_data_func("/virtio/net/pci/rx_stop_cont",
 263                        stop_cont_test, pci_basic);
 264#endif
 265    qtest_add_func("/virtio/net/pci/hotplug", hotplug);
 266
 267    return g_test_run();
 268}
 269