qemu/tests/libqos/virtio-net.c
<<
>>
Prefs
   1/*
   2 * libqos driver framework
   3 *
   4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
   5 *
   6 * This library is free software; you can redistribute it and/or
   7 * modify it under the terms of the GNU Lesser General Public
   8 * License version 2 as published by the Free Software Foundation.
   9 *
  10 * This library is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * Lesser General Public License for more details.
  14 *
  15 * You should have received a copy of the GNU Lesser General Public
  16 * License along with this library; if not, see <http://www.gnu.org/licenses/>
  17 */
  18
  19#include "qemu/osdep.h"
  20#include "libqtest.h"
  21#include "libqos/qgraph.h"
  22#include "libqos/virtio-net.h"
  23#include "hw/virtio/virtio-net.h"
  24
  25
  26static QGuestAllocator *alloc;
  27
  28static void virtio_net_cleanup(QVirtioNet *interface)
  29{
  30    int i;
  31
  32    for (i = 0; i < interface->n_queues; i++) {
  33        qvirtqueue_cleanup(interface->vdev->bus, interface->queues[i], alloc);
  34    }
  35    g_free(interface->queues);
  36}
  37
  38static void virtio_net_setup(QVirtioNet *interface)
  39{
  40    QVirtioDevice *vdev = interface->vdev;
  41    uint64_t features;
  42    int i;
  43
  44    features = qvirtio_get_features(vdev);
  45    features &= ~(QVIRTIO_F_BAD_FEATURE |
  46                  (1u << VIRTIO_RING_F_INDIRECT_DESC) |
  47                  (1u << VIRTIO_RING_F_EVENT_IDX));
  48    qvirtio_set_features(vdev, features);
  49
  50    if (features & (1u << VIRTIO_NET_F_MQ)) {
  51        interface->n_queues = qvirtio_config_readw(vdev, 8) * 2;
  52    } else {
  53        interface->n_queues = 2;
  54    }
  55
  56    interface->queues = g_new(QVirtQueue *, interface->n_queues);
  57    for (i = 0; i < interface->n_queues; i++) {
  58        interface->queues[i] = qvirtqueue_setup(vdev, alloc, i);
  59    }
  60    qvirtio_set_driver_ok(vdev);
  61}
  62
  63/* virtio-net-device */
  64static void qvirtio_net_device_destructor(QOSGraphObject *obj)
  65{
  66    QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
  67    virtio_net_cleanup(&v_net->net);
  68}
  69
  70static void qvirtio_net_device_start_hw(QOSGraphObject *obj)
  71{
  72    QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
  73    QVirtioNet *interface = &v_net->net;
  74
  75    virtio_net_setup(interface);
  76}
  77
  78static void *qvirtio_net_get_driver(QVirtioNet *v_net,
  79                                    const char *interface)
  80{
  81    if (!g_strcmp0(interface, "virtio-net")) {
  82        return v_net;
  83    }
  84    if (!g_strcmp0(interface, "virtio")) {
  85        return v_net->vdev;
  86    }
  87
  88    fprintf(stderr, "%s not present in virtio-net-device\n", interface);
  89    g_assert_not_reached();
  90}
  91
  92static void *qvirtio_net_device_get_driver(void *object,
  93                                           const char *interface)
  94{
  95    QVirtioNetDevice *v_net = object;
  96    return qvirtio_net_get_driver(&v_net->net, interface);
  97}
  98
  99static void *virtio_net_device_create(void *virtio_dev,
 100                                          QGuestAllocator *t_alloc,
 101                                          void *addr)
 102{
 103    QVirtioNetDevice *virtio_ndevice = g_new0(QVirtioNetDevice, 1);
 104    QVirtioNet *interface = &virtio_ndevice->net;
 105
 106    interface->vdev = virtio_dev;
 107    alloc = t_alloc;
 108
 109    virtio_ndevice->obj.destructor = qvirtio_net_device_destructor;
 110    virtio_ndevice->obj.get_driver = qvirtio_net_device_get_driver;
 111    virtio_ndevice->obj.start_hw = qvirtio_net_device_start_hw;
 112
 113    return &virtio_ndevice->obj;
 114}
 115
 116/* virtio-net-pci */
 117static void qvirtio_net_pci_destructor(QOSGraphObject *obj)
 118{
 119    QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
 120    QVirtioNet *interface = &v_net->net;
 121    QOSGraphObject *pci_vobj =  &v_net->pci_vdev.obj;
 122
 123    virtio_net_cleanup(interface);
 124    qvirtio_pci_destructor(pci_vobj);
 125}
 126
 127static void qvirtio_net_pci_start_hw(QOSGraphObject *obj)
 128{
 129    QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
 130    QVirtioNet *interface = &v_net->net;
 131    QOSGraphObject *pci_vobj =  &v_net->pci_vdev.obj;
 132
 133    qvirtio_pci_start_hw(pci_vobj);
 134    virtio_net_setup(interface);
 135}
 136
 137static void *qvirtio_net_pci_get_driver(void *object,
 138                                            const char *interface)
 139{
 140    QVirtioNetPCI *v_net = object;
 141    if (!g_strcmp0(interface, "pci-device")) {
 142        return v_net->pci_vdev.pdev;
 143    }
 144    return qvirtio_net_get_driver(&v_net->net, interface);
 145}
 146
 147static void *virtio_net_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
 148                                  void *addr)
 149{
 150    QVirtioNetPCI *virtio_bpci = g_new0(QVirtioNetPCI, 1);
 151    QVirtioNet *interface = &virtio_bpci->net;
 152    QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
 153
 154    virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
 155    interface->vdev = &virtio_bpci->pci_vdev.vdev;
 156    alloc = t_alloc;
 157
 158    g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_NET);
 159
 160    obj->destructor = qvirtio_net_pci_destructor;
 161    obj->start_hw = qvirtio_net_pci_start_hw;
 162    obj->get_driver = qvirtio_net_pci_get_driver;
 163
 164    return obj;
 165}
 166
 167static void virtio_net_register_nodes(void)
 168{
 169    /* FIXME: every test using these nodes needs to setup a
 170     * -netdev socket,id=hs0 otherwise QEMU is not going to start.
 171     * Therefore, we do not include "produces" edge for virtio
 172     * and pci-device yet.
 173     */
 174    QPCIAddress addr = {
 175        .devfn = QPCI_DEVFN(4, 0),
 176    };
 177
 178    QOSGraphEdgeOptions opts = { };
 179
 180    /* virtio-net-device */
 181    opts.extra_device_opts = "netdev=hs0";
 182    qos_node_create_driver("virtio-net-device",
 183                            virtio_net_device_create);
 184    qos_node_consumes("virtio-net-device", "virtio-bus", &opts);
 185    qos_node_produces("virtio-net-device", "virtio-net");
 186
 187    /* virtio-net-pci */
 188    opts.extra_device_opts = "netdev=hs0,addr=04.0";
 189    add_qpci_address(&opts, &addr);
 190    qos_node_create_driver("virtio-net-pci", virtio_net_pci_create);
 191    qos_node_consumes("virtio-net-pci", "pci-bus", &opts);
 192    qos_node_produces("virtio-net-pci", "virtio-net");
 193}
 194
 195libqos_init(virtio_net_register_nodes);
 196