blob: a9e253afe4903a688367d8346ed87c1ea8aa12d3 [file] [log] [blame]
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +02001/*
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
Thomas Huthdc0ad022020-06-05 12:02:42 +02008 * License version 2.1 as published by the Free Software Foundation.
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +02009 *
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"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020021#include "qemu/module.h"
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +020022#include "libqos/qgraph.h"
23#include "libqos/virtio-net.h"
24#include "hw/virtio/virtio-net.h"
25
26
27static QGuestAllocator *alloc;
28
29static void virtio_net_cleanup(QVirtioNet *interface)
30{
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +020031 int i;
32
33 for (i = 0; i < interface->n_queues; i++) {
34 qvirtqueue_cleanup(interface->vdev->bus, interface->queues[i], alloc);
35 }
36 g_free(interface->queues);
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +020037}
38
39static void virtio_net_setup(QVirtioNet *interface)
40{
41 QVirtioDevice *vdev = interface->vdev;
42 uint64_t features;
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +020043 int i;
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +020044
45 features = qvirtio_get_features(vdev);
46 features &= ~(QVIRTIO_F_BAD_FEATURE |
Stefan Hajnoczia9340352019-10-23 11:04:12 +010047 (1ull << VIRTIO_RING_F_INDIRECT_DESC) |
48 (1ull << VIRTIO_RING_F_EVENT_IDX));
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +020049 qvirtio_set_features(vdev, features);
50
Stefan Hajnoczia9340352019-10-23 11:04:12 +010051 if (features & (1ull << VIRTIO_NET_F_MQ)) {
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +020052 interface->n_queues = qvirtio_config_readw(vdev, 8) * 2;
53 } else {
54 interface->n_queues = 2;
55 }
Oleinik, Alexander375eae12019-08-05 03:24:15 +000056 interface->n_queues++; /* Account for the ctrl queue */
Paolo Bonzini6bd4a6d2018-10-22 12:37:21 +020057
58 interface->queues = g_new(QVirtQueue *, interface->n_queues);
59 for (i = 0; i < interface->n_queues; i++) {
60 interface->queues[i] = qvirtqueue_setup(vdev, alloc, i);
61 }
Emanuele Giuseppe Esposito583349d2018-07-30 22:21:42 +020062 qvirtio_set_driver_ok(vdev);
63}
64
65/* virtio-net-device */
66static void qvirtio_net_device_destructor(QOSGraphObject *obj)
67{
68 QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
69 virtio_net_cleanup(&v_net->net);
70}
71
72static void qvirtio_net_device_start_hw(QOSGraphObject *obj)
73{
74 QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
75 QVirtioNet *interface = &v_net->net;
76
77 virtio_net_setup(interface);
78}
79
80static void *qvirtio_net_get_driver(QVirtioNet *v_net,
81 const char *interface)
82{
83 if (!g_strcmp0(interface, "virtio-net")) {
84 return v_net;
85 }
86 if (!g_strcmp0(interface, "virtio")) {
87 return v_net->vdev;
88 }
89
90 fprintf(stderr, "%s not present in virtio-net-device\n", interface);
91 g_assert_not_reached();
92}
93
94static void *qvirtio_net_device_get_driver(void *object,
95 const char *interface)
96{
97 QVirtioNetDevice *v_net = object;
98 return qvirtio_net_get_driver(&v_net->net, interface);
99}
100
101static void *virtio_net_device_create(void *virtio_dev,
102 QGuestAllocator *t_alloc,
103 void *addr)
104{
105 QVirtioNetDevice *virtio_ndevice = g_new0(QVirtioNetDevice, 1);
106 QVirtioNet *interface = &virtio_ndevice->net;
107
108 interface->vdev = virtio_dev;
109 alloc = t_alloc;
110
111 virtio_ndevice->obj.destructor = qvirtio_net_device_destructor;
112 virtio_ndevice->obj.get_driver = qvirtio_net_device_get_driver;
113 virtio_ndevice->obj.start_hw = qvirtio_net_device_start_hw;
114
115 return &virtio_ndevice->obj;
116}
117
118/* virtio-net-pci */
119static void qvirtio_net_pci_destructor(QOSGraphObject *obj)
120{
121 QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
122 QVirtioNet *interface = &v_net->net;
123 QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
124
125 virtio_net_cleanup(interface);
126 qvirtio_pci_destructor(pci_vobj);
127}
128
129static void qvirtio_net_pci_start_hw(QOSGraphObject *obj)
130{
131 QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
132 QVirtioNet *interface = &v_net->net;
133 QOSGraphObject *pci_vobj = &v_net->pci_vdev.obj;
134
135 qvirtio_pci_start_hw(pci_vobj);
136 virtio_net_setup(interface);
137}
138
139static void *qvirtio_net_pci_get_driver(void *object,
140 const char *interface)
141{
142 QVirtioNetPCI *v_net = object;
143 if (!g_strcmp0(interface, "pci-device")) {
144 return v_net->pci_vdev.pdev;
145 }
146 return qvirtio_net_get_driver(&v_net->net, interface);
147}
148
149static void *virtio_net_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
150 void *addr)
151{
152 QVirtioNetPCI *virtio_bpci = g_new0(QVirtioNetPCI, 1);
153 QVirtioNet *interface = &virtio_bpci->net;
154 QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
155
156 virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
157 interface->vdev = &virtio_bpci->pci_vdev.vdev;
158 alloc = t_alloc;
159
160 g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_NET);
161
162 obj->destructor = qvirtio_net_pci_destructor;
163 obj->start_hw = qvirtio_net_pci_start_hw;
164 obj->get_driver = qvirtio_net_pci_get_driver;
165
166 return obj;
167}
168
169static void virtio_net_register_nodes(void)
170{
171 /* FIXME: every test using these nodes needs to setup a
172 * -netdev socket,id=hs0 otherwise QEMU is not going to start.
173 * Therefore, we do not include "produces" edge for virtio
174 * and pci-device yet.
175 */
176 QPCIAddress addr = {
177 .devfn = QPCI_DEVFN(4, 0),
178 };
179
180 QOSGraphEdgeOptions opts = { };
181
182 /* virtio-net-device */
183 opts.extra_device_opts = "netdev=hs0";
184 qos_node_create_driver("virtio-net-device",
185 virtio_net_device_create);
186 qos_node_consumes("virtio-net-device", "virtio-bus", &opts);
187 qos_node_produces("virtio-net-device", "virtio-net");
188
189 /* virtio-net-pci */
190 opts.extra_device_opts = "netdev=hs0,addr=04.0";
191 add_qpci_address(&opts, &addr);
192 qos_node_create_driver("virtio-net-pci", virtio_net_pci_create);
193 qos_node_consumes("virtio-net-pci", "pci-bus", &opts);
194 qos_node_produces("virtio-net-pci", "virtio-net");
195}
196
197libqos_init(virtio_net_register_nodes);