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