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