qemu/tests/libqos/virtio-rng.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-rng.h"
  23
  24/* virtio-rng-device */
  25static void *qvirtio_rng_get_driver(QVirtioRng *v_rng,
  26                                    const char *interface)
  27{
  28    if (!g_strcmp0(interface, "virtio-rng")) {
  29        return v_rng;
  30    }
  31    if (!g_strcmp0(interface, "virtio")) {
  32        return v_rng->vdev;
  33    }
  34
  35    fprintf(stderr, "%s not present in virtio-rng-device\n", interface);
  36    g_assert_not_reached();
  37}
  38
  39static void *qvirtio_rng_device_get_driver(void *object,
  40                                           const char *interface)
  41{
  42    QVirtioRngDevice *v_rng = object;
  43    return qvirtio_rng_get_driver(&v_rng->rng, interface);
  44}
  45
  46static void *virtio_rng_device_create(void *virtio_dev,
  47                                      QGuestAllocator *t_alloc,
  48                                      void *addr)
  49{
  50    QVirtioRngDevice *virtio_rdevice = g_new0(QVirtioRngDevice, 1);
  51    QVirtioRng *interface = &virtio_rdevice->rng;
  52
  53    interface->vdev = virtio_dev;
  54
  55    virtio_rdevice->obj.get_driver = qvirtio_rng_device_get_driver;
  56
  57    return &virtio_rdevice->obj;
  58}
  59
  60/* virtio-rng-pci */
  61static void *qvirtio_rng_pci_get_driver(void *object, const char *interface)
  62{
  63    QVirtioRngPCI *v_rng = object;
  64    if (!g_strcmp0(interface, "pci-device")) {
  65        return v_rng->pci_vdev.pdev;
  66    }
  67    return qvirtio_rng_get_driver(&v_rng->rng, interface);
  68}
  69
  70static void *virtio_rng_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
  71                                   void *addr)
  72{
  73    QVirtioRngPCI *virtio_rpci = g_new0(QVirtioRngPCI, 1);
  74    QVirtioRng *interface = &virtio_rpci->rng;
  75    QOSGraphObject *obj = &virtio_rpci->pci_vdev.obj;
  76
  77    virtio_pci_init(&virtio_rpci->pci_vdev, pci_bus, addr);
  78    interface->vdev = &virtio_rpci->pci_vdev.vdev;
  79
  80    obj->get_driver = qvirtio_rng_pci_get_driver;
  81
  82    return obj;
  83}
  84
  85static void virtio_rng_register_nodes(void)
  86{
  87    QPCIAddress addr = {
  88        .devfn = QPCI_DEVFN(4, 0),
  89    };
  90
  91    QOSGraphEdgeOptions opts = {
  92        .extra_device_opts = "addr=04.0",
  93    };
  94
  95    /* virtio-rng-device */
  96    qos_node_create_driver("virtio-rng-device", virtio_rng_device_create);
  97    qos_node_consumes("virtio-rng-device", "virtio-bus", NULL);
  98    qos_node_produces("virtio-rng-device", "virtio");
  99    qos_node_produces("virtio-rng-device", "virtio-rng");
 100
 101    /* virtio-rng-pci */
 102    add_qpci_address(&opts, &addr);
 103    qos_node_create_driver("virtio-rng-pci", virtio_rng_pci_create);
 104    qos_node_consumes("virtio-rng-pci", "pci-bus", &opts);
 105    qos_node_produces("virtio-rng-pci", "pci-device");
 106    qos_node_produces("virtio-rng-pci", "virtio");
 107    qos_node_produces("virtio-rng-pci", "virtio-rng");
 108}
 109
 110libqos_init(virtio_rng_register_nodes);
 111