Merge remote-tracking branch 'remotes/rth/tags/pull-tcg-20190714' into staging
[qemu/ar7.git] / tests / libqos / virtio-net.c
blob66405b646e966fe7ac623f8212ac79af11a95eb5
1 /*
2 * libqos driver framework
4 * Copyright (c) 2018 Emanuele Giuseppe Esposito <e.emanuelegiuseppe@gmail.com>
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.
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.
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/>
19 #include "qemu/osdep.h"
20 #include "libqtest.h"
21 #include "qemu/module.h"
22 #include "libqos/qgraph.h"
23 #include "libqos/virtio-net.h"
24 #include "hw/virtio/virtio-net.h"
27 static QGuestAllocator *alloc;
29 static void virtio_net_cleanup(QVirtioNet *interface)
31 int i;
33 for (i = 0; i < interface->n_queues; i++) {
34 qvirtqueue_cleanup(interface->vdev->bus, interface->queues[i], alloc);
36 g_free(interface->queues);
39 static void virtio_net_setup(QVirtioNet *interface)
41 QVirtioDevice *vdev = interface->vdev;
42 uint64_t features;
43 int i;
45 features = qvirtio_get_features(vdev);
46 features &= ~(QVIRTIO_F_BAD_FEATURE |
47 (1u << VIRTIO_RING_F_INDIRECT_DESC) |
48 (1u << VIRTIO_RING_F_EVENT_IDX));
49 qvirtio_set_features(vdev, features);
51 if (features & (1u << VIRTIO_NET_F_MQ)) {
52 interface->n_queues = qvirtio_config_readw(vdev, 8) * 2;
53 } else {
54 interface->n_queues = 2;
57 interface->queues = g_new(QVirtQueue *, interface->n_queues);
58 for (i = 0; i < interface->n_queues; i++) {
59 interface->queues[i] = qvirtqueue_setup(vdev, alloc, i);
61 qvirtio_set_driver_ok(vdev);
64 /* virtio-net-device */
65 static void qvirtio_net_device_destructor(QOSGraphObject *obj)
67 QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
68 virtio_net_cleanup(&v_net->net);
71 static void qvirtio_net_device_start_hw(QOSGraphObject *obj)
73 QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
74 QVirtioNet *interface = &v_net->net;
76 virtio_net_setup(interface);
79 static void *qvirtio_net_get_driver(QVirtioNet *v_net,
80 const char *interface)
82 if (!g_strcmp0(interface, "virtio-net")) {
83 return v_net;
85 if (!g_strcmp0(interface, "virtio")) {
86 return v_net->vdev;
89 fprintf(stderr, "%s not present in virtio-net-device\n", interface);
90 g_assert_not_reached();
93 static void *qvirtio_net_device_get_driver(void *object,
94 const char *interface)
96 QVirtioNetDevice *v_net = object;
97 return qvirtio_net_get_driver(&v_net->net, interface);
100 static void *virtio_net_device_create(void *virtio_dev,
101 QGuestAllocator *t_alloc,
102 void *addr)
104 QVirtioNetDevice *virtio_ndevice = g_new0(QVirtioNetDevice, 1);
105 QVirtioNet *interface = &virtio_ndevice->net;
107 interface->vdev = virtio_dev;
108 alloc = t_alloc;
110 virtio_ndevice->obj.destructor = qvirtio_net_device_destructor;
111 virtio_ndevice->obj.get_driver = qvirtio_net_device_get_driver;
112 virtio_ndevice->obj.start_hw = qvirtio_net_device_start_hw;
114 return &virtio_ndevice->obj;
117 /* virtio-net-pci */
118 static void qvirtio_net_pci_destructor(QOSGraphObject *obj)
120 QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
121 QVirtioNet *interface = &v_net->net;
122 QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
124 virtio_net_cleanup(interface);
125 qvirtio_pci_destructor(pci_vobj);
128 static void qvirtio_net_pci_start_hw(QOSGraphObject *obj)
130 QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
131 QVirtioNet *interface = &v_net->net;
132 QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
134 qvirtio_pci_start_hw(pci_vobj);
135 virtio_net_setup(interface);
138 static void *qvirtio_net_pci_get_driver(void *object,
139 const char *interface)
141 QVirtioNetPCI *v_net = object;
142 if (!g_strcmp0(interface, "pci-device")) {
143 return v_net->pci_vdev.pdev;
145 return qvirtio_net_get_driver(&v_net->net, interface);
148 static void *virtio_net_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
149 void *addr)
151 QVirtioNetPCI *virtio_bpci = g_new0(QVirtioNetPCI, 1);
152 QVirtioNet *interface = &virtio_bpci->net;
153 QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
155 virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
156 interface->vdev = &virtio_bpci->pci_vdev.vdev;
157 alloc = t_alloc;
159 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_NET);
161 obj->destructor = qvirtio_net_pci_destructor;
162 obj->start_hw = qvirtio_net_pci_start_hw;
163 obj->get_driver = qvirtio_net_pci_get_driver;
165 return obj;
168 static void virtio_net_register_nodes(void)
170 /* FIXME: every test using these nodes needs to setup a
171 * -netdev socket,id=hs0 otherwise QEMU is not going to start.
172 * Therefore, we do not include "produces" edge for virtio
173 * and pci-device yet.
175 QPCIAddress addr = {
176 .devfn = QPCI_DEVFN(4, 0),
179 QOSGraphEdgeOptions opts = { };
181 /* virtio-net-device */
182 opts.extra_device_opts = "netdev=hs0";
183 qos_node_create_driver("virtio-net-device",
184 virtio_net_device_create);
185 qos_node_consumes("virtio-net-device", "virtio-bus", &opts);
186 qos_node_produces("virtio-net-device", "virtio-net");
188 /* virtio-net-pci */
189 opts.extra_device_opts = "netdev=hs0,addr=04.0";
190 add_qpci_address(&opts, &addr);
191 qos_node_create_driver("virtio-net-pci", virtio_net_pci_create);
192 qos_node_consumes("virtio-net-pci", "pci-bus", &opts);
193 qos_node_produces("virtio-net-pci", "virtio-net");
196 libqos_init(virtio_net_register_nodes);