11cf4323eSThomas Huth /*
21cf4323eSThomas Huth * libqos driver framework
31cf4323eSThomas Huth *
41cf4323eSThomas Huth * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
51cf4323eSThomas Huth *
61cf4323eSThomas Huth * This library is free software; you can redistribute it and/or
71cf4323eSThomas Huth * modify it under the terms of the GNU Lesser General Public
8dc0ad02dSThomas Huth * License version 2.1 as published by the Free Software Foundation.
91cf4323eSThomas Huth *
101cf4323eSThomas Huth * This library is distributed in the hope that it will be useful,
111cf4323eSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cf4323eSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
131cf4323eSThomas Huth * Lesser General Public License for more details.
141cf4323eSThomas Huth *
151cf4323eSThomas Huth * You should have received a copy of the GNU Lesser General Public
161cf4323eSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>
171cf4323eSThomas Huth */
181cf4323eSThomas Huth
191cf4323eSThomas Huth #include "qemu/osdep.h"
20*907b5105SMarc-André Lureau #include "../libqtest.h"
211cf4323eSThomas Huth #include "qemu/module.h"
22a2ce7dbdSPaolo Bonzini #include "qgraph.h"
23a2ce7dbdSPaolo Bonzini #include "virtio-net.h"
241cf4323eSThomas Huth #include "hw/virtio/virtio-net.h"
251cf4323eSThomas Huth
261cf4323eSThomas Huth
271cf4323eSThomas Huth static QGuestAllocator *alloc;
281cf4323eSThomas Huth
virtio_net_cleanup(QVirtioNet * interface)291cf4323eSThomas Huth static void virtio_net_cleanup(QVirtioNet *interface)
301cf4323eSThomas Huth {
311cf4323eSThomas Huth int i;
321cf4323eSThomas Huth
331cf4323eSThomas Huth for (i = 0; i < interface->n_queues; i++) {
341cf4323eSThomas Huth qvirtqueue_cleanup(interface->vdev->bus, interface->queues[i], alloc);
351cf4323eSThomas Huth }
361cf4323eSThomas Huth g_free(interface->queues);
371cf4323eSThomas Huth }
381cf4323eSThomas Huth
virtio_net_setup(QVirtioNet * interface)391cf4323eSThomas Huth static void virtio_net_setup(QVirtioNet *interface)
401cf4323eSThomas Huth {
411cf4323eSThomas Huth QVirtioDevice *vdev = interface->vdev;
421cf4323eSThomas Huth uint64_t features;
431cf4323eSThomas Huth int i;
441cf4323eSThomas Huth
451cf4323eSThomas Huth features = qvirtio_get_features(vdev);
461cf4323eSThomas Huth features &= ~(QVIRTIO_F_BAD_FEATURE |
471cf4323eSThomas Huth (1ull << VIRTIO_RING_F_INDIRECT_DESC) |
481cf4323eSThomas Huth (1ull << VIRTIO_RING_F_EVENT_IDX));
491cf4323eSThomas Huth qvirtio_set_features(vdev, features);
501cf4323eSThomas Huth
511cf4323eSThomas Huth if (features & (1ull << VIRTIO_NET_F_MQ)) {
521cf4323eSThomas Huth interface->n_queues = qvirtio_config_readw(vdev, 8) * 2;
531cf4323eSThomas Huth } else {
541cf4323eSThomas Huth interface->n_queues = 2;
551cf4323eSThomas Huth }
561cf4323eSThomas Huth interface->n_queues++; /* Account for the ctrl queue */
571cf4323eSThomas Huth
581cf4323eSThomas Huth interface->queues = g_new(QVirtQueue *, interface->n_queues);
591cf4323eSThomas Huth for (i = 0; i < interface->n_queues; i++) {
601cf4323eSThomas Huth interface->queues[i] = qvirtqueue_setup(vdev, alloc, i);
611cf4323eSThomas Huth }
621cf4323eSThomas Huth qvirtio_set_driver_ok(vdev);
631cf4323eSThomas Huth }
641cf4323eSThomas Huth
651cf4323eSThomas Huth /* virtio-net-device */
qvirtio_net_device_destructor(QOSGraphObject * obj)661cf4323eSThomas Huth static void qvirtio_net_device_destructor(QOSGraphObject *obj)
671cf4323eSThomas Huth {
681cf4323eSThomas Huth QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
691cf4323eSThomas Huth virtio_net_cleanup(&v_net->net);
701cf4323eSThomas Huth }
711cf4323eSThomas Huth
qvirtio_net_device_start_hw(QOSGraphObject * obj)721cf4323eSThomas Huth static void qvirtio_net_device_start_hw(QOSGraphObject *obj)
731cf4323eSThomas Huth {
741cf4323eSThomas Huth QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
751cf4323eSThomas Huth QVirtioNet *interface = &v_net->net;
761cf4323eSThomas Huth
771cf4323eSThomas Huth virtio_net_setup(interface);
781cf4323eSThomas Huth }
791cf4323eSThomas Huth
qvirtio_net_get_driver(QVirtioNet * v_net,const char * interface)801cf4323eSThomas Huth static void *qvirtio_net_get_driver(QVirtioNet *v_net,
811cf4323eSThomas Huth const char *interface)
821cf4323eSThomas Huth {
831cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio-net")) {
841cf4323eSThomas Huth return v_net;
851cf4323eSThomas Huth }
861cf4323eSThomas Huth if (!g_strcmp0(interface, "virtio")) {
871cf4323eSThomas Huth return v_net->vdev;
881cf4323eSThomas Huth }
891cf4323eSThomas Huth
901cf4323eSThomas Huth fprintf(stderr, "%s not present in virtio-net-device\n", interface);
911cf4323eSThomas Huth g_assert_not_reached();
921cf4323eSThomas Huth }
931cf4323eSThomas Huth
qvirtio_net_device_get_driver(void * object,const char * interface)941cf4323eSThomas Huth static void *qvirtio_net_device_get_driver(void *object,
951cf4323eSThomas Huth const char *interface)
961cf4323eSThomas Huth {
971cf4323eSThomas Huth QVirtioNetDevice *v_net = object;
981cf4323eSThomas Huth return qvirtio_net_get_driver(&v_net->net, interface);
991cf4323eSThomas Huth }
1001cf4323eSThomas Huth
virtio_net_device_create(void * virtio_dev,QGuestAllocator * t_alloc,void * addr)1011cf4323eSThomas Huth static void *virtio_net_device_create(void *virtio_dev,
1021cf4323eSThomas Huth QGuestAllocator *t_alloc,
1031cf4323eSThomas Huth void *addr)
1041cf4323eSThomas Huth {
1051cf4323eSThomas Huth QVirtioNetDevice *virtio_ndevice = g_new0(QVirtioNetDevice, 1);
1061cf4323eSThomas Huth QVirtioNet *interface = &virtio_ndevice->net;
1071cf4323eSThomas Huth
1081cf4323eSThomas Huth interface->vdev = virtio_dev;
1091cf4323eSThomas Huth alloc = t_alloc;
1101cf4323eSThomas Huth
1111cf4323eSThomas Huth virtio_ndevice->obj.destructor = qvirtio_net_device_destructor;
1121cf4323eSThomas Huth virtio_ndevice->obj.get_driver = qvirtio_net_device_get_driver;
1131cf4323eSThomas Huth virtio_ndevice->obj.start_hw = qvirtio_net_device_start_hw;
1141cf4323eSThomas Huth
1151cf4323eSThomas Huth return &virtio_ndevice->obj;
1161cf4323eSThomas Huth }
1171cf4323eSThomas Huth
1181cf4323eSThomas Huth /* virtio-net-pci */
qvirtio_net_pci_destructor(QOSGraphObject * obj)1191cf4323eSThomas Huth static void qvirtio_net_pci_destructor(QOSGraphObject *obj)
1201cf4323eSThomas Huth {
1211cf4323eSThomas Huth QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
1221cf4323eSThomas Huth QVirtioNet *interface = &v_net->net;
1231cf4323eSThomas Huth QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
1241cf4323eSThomas Huth
1251cf4323eSThomas Huth virtio_net_cleanup(interface);
1261cf4323eSThomas Huth qvirtio_pci_destructor(pci_vobj);
1271cf4323eSThomas Huth }
1281cf4323eSThomas Huth
qvirtio_net_pci_start_hw(QOSGraphObject * obj)1291cf4323eSThomas Huth static void qvirtio_net_pci_start_hw(QOSGraphObject *obj)
1301cf4323eSThomas Huth {
1311cf4323eSThomas Huth QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
1321cf4323eSThomas Huth QVirtioNet *interface = &v_net->net;
1331cf4323eSThomas Huth QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
1341cf4323eSThomas Huth
1351cf4323eSThomas Huth qvirtio_pci_start_hw(pci_vobj);
1361cf4323eSThomas Huth virtio_net_setup(interface);
1371cf4323eSThomas Huth }
1381cf4323eSThomas Huth
qvirtio_net_pci_get_driver(void * object,const char * interface)1391cf4323eSThomas Huth static void *qvirtio_net_pci_get_driver(void *object,
1401cf4323eSThomas Huth const char *interface)
1411cf4323eSThomas Huth {
1421cf4323eSThomas Huth QVirtioNetPCI *v_net = object;
1431cf4323eSThomas Huth if (!g_strcmp0(interface, "pci-device")) {
1441cf4323eSThomas Huth return v_net->pci_vdev.pdev;
1451cf4323eSThomas Huth }
1461cf4323eSThomas Huth return qvirtio_net_get_driver(&v_net->net, interface);
1471cf4323eSThomas Huth }
1481cf4323eSThomas Huth
virtio_net_pci_create(void * pci_bus,QGuestAllocator * t_alloc,void * addr)1491cf4323eSThomas Huth static void *virtio_net_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
1501cf4323eSThomas Huth void *addr)
1511cf4323eSThomas Huth {
1521cf4323eSThomas Huth QVirtioNetPCI *virtio_bpci = g_new0(QVirtioNetPCI, 1);
1531cf4323eSThomas Huth QVirtioNet *interface = &virtio_bpci->net;
1541cf4323eSThomas Huth QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
1551cf4323eSThomas Huth
1561cf4323eSThomas Huth virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
1571cf4323eSThomas Huth interface->vdev = &virtio_bpci->pci_vdev.vdev;
1581cf4323eSThomas Huth alloc = t_alloc;
1591cf4323eSThomas Huth
1601cf4323eSThomas Huth g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_NET);
1611cf4323eSThomas Huth
1621cf4323eSThomas Huth obj->destructor = qvirtio_net_pci_destructor;
1631cf4323eSThomas Huth obj->start_hw = qvirtio_net_pci_start_hw;
1641cf4323eSThomas Huth obj->get_driver = qvirtio_net_pci_get_driver;
1651cf4323eSThomas Huth
1661cf4323eSThomas Huth return obj;
1671cf4323eSThomas Huth }
1681cf4323eSThomas Huth
virtio_net_register_nodes(void)1691cf4323eSThomas Huth static void virtio_net_register_nodes(void)
1701cf4323eSThomas Huth {
1711cf4323eSThomas Huth /* FIXME: every test using these nodes needs to setup a
1721cf4323eSThomas Huth * -netdev socket,id=hs0 otherwise QEMU is not going to start.
1731cf4323eSThomas Huth * Therefore, we do not include "produces" edge for virtio
1741cf4323eSThomas Huth * and pci-device yet.
1751cf4323eSThomas Huth */
1761cf4323eSThomas Huth QPCIAddress addr = {
1771cf4323eSThomas Huth .devfn = QPCI_DEVFN(4, 0),
1781cf4323eSThomas Huth };
1791cf4323eSThomas Huth
1801cf4323eSThomas Huth QOSGraphEdgeOptions opts = { };
1811cf4323eSThomas Huth
1821cf4323eSThomas Huth /* virtio-net-device */
1831cf4323eSThomas Huth opts.extra_device_opts = "netdev=hs0";
1841cf4323eSThomas Huth qos_node_create_driver("virtio-net-device",
1851cf4323eSThomas Huth virtio_net_device_create);
1861cf4323eSThomas Huth qos_node_consumes("virtio-net-device", "virtio-bus", &opts);
1871cf4323eSThomas Huth qos_node_produces("virtio-net-device", "virtio-net");
1881cf4323eSThomas Huth
1891cf4323eSThomas Huth /* virtio-net-pci */
1901cf4323eSThomas Huth opts.extra_device_opts = "netdev=hs0,addr=04.0";
1911cf4323eSThomas Huth add_qpci_address(&opts, &addr);
1921cf4323eSThomas Huth qos_node_create_driver("virtio-net-pci", virtio_net_pci_create);
1931cf4323eSThomas Huth qos_node_consumes("virtio-net-pci", "pci-bus", &opts);
1941cf4323eSThomas Huth qos_node_produces("virtio-net-pci", "virtio-net");
1951cf4323eSThomas Huth }
1961cf4323eSThomas Huth
1971cf4323eSThomas Huth libqos_init(virtio_net_register_nodes);
198