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.1 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"
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
)
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
;
45 features
= qvirtio_get_features(vdev
);
46 features
&= ~(QVIRTIO_F_BAD_FEATURE
|
47 (1ull << VIRTIO_RING_F_INDIRECT_DESC
) |
48 (1ull << VIRTIO_RING_F_EVENT_IDX
));
49 qvirtio_set_features(vdev
, features
);
51 if (features
& (1ull << VIRTIO_NET_F_MQ
)) {
52 interface
->n_queues
= qvirtio_config_readw(vdev
, 8) * 2;
54 interface
->n_queues
= 2;
56 interface
->n_queues
++; /* Account for the ctrl queue */
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
);
62 qvirtio_set_driver_ok(vdev
);
65 /* virtio-net-device */
66 static void qvirtio_net_device_destructor(QOSGraphObject
*obj
)
68 QVirtioNetDevice
*v_net
= (QVirtioNetDevice
*) obj
;
69 virtio_net_cleanup(&v_net
->net
);
72 static void qvirtio_net_device_start_hw(QOSGraphObject
*obj
)
74 QVirtioNetDevice
*v_net
= (QVirtioNetDevice
*) obj
;
75 QVirtioNet
*interface
= &v_net
->net
;
77 virtio_net_setup(interface
);
80 static void *qvirtio_net_get_driver(QVirtioNet
*v_net
,
81 const char *interface
)
83 if (!g_strcmp0(interface
, "virtio-net")) {
86 if (!g_strcmp0(interface
, "virtio")) {
90 fprintf(stderr
, "%s not present in virtio-net-device\n", interface
);
91 g_assert_not_reached();
94 static void *qvirtio_net_device_get_driver(void *object
,
95 const char *interface
)
97 QVirtioNetDevice
*v_net
= object
;
98 return qvirtio_net_get_driver(&v_net
->net
, interface
);
101 static void *virtio_net_device_create(void *virtio_dev
,
102 QGuestAllocator
*t_alloc
,
105 QVirtioNetDevice
*virtio_ndevice
= g_new0(QVirtioNetDevice
, 1);
106 QVirtioNet
*interface
= &virtio_ndevice
->net
;
108 interface
->vdev
= virtio_dev
;
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
;
115 return &virtio_ndevice
->obj
;
119 static void qvirtio_net_pci_destructor(QOSGraphObject
*obj
)
121 QVirtioNetPCI
*v_net
= (QVirtioNetPCI
*) obj
;
122 QVirtioNet
*interface
= &v_net
->net
;
123 QOSGraphObject
*pci_vobj
= &v_net
->pci_vdev
.obj
;
125 virtio_net_cleanup(interface
);
126 qvirtio_pci_destructor(pci_vobj
);
129 static void qvirtio_net_pci_start_hw(QOSGraphObject
*obj
)
131 QVirtioNetPCI
*v_net
= (QVirtioNetPCI
*) obj
;
132 QVirtioNet
*interface
= &v_net
->net
;
133 QOSGraphObject
*pci_vobj
= &v_net
->pci_vdev
.obj
;
135 qvirtio_pci_start_hw(pci_vobj
);
136 virtio_net_setup(interface
);
139 static void *qvirtio_net_pci_get_driver(void *object
,
140 const char *interface
)
142 QVirtioNetPCI
*v_net
= object
;
143 if (!g_strcmp0(interface
, "pci-device")) {
144 return v_net
->pci_vdev
.pdev
;
146 return qvirtio_net_get_driver(&v_net
->net
, interface
);
149 static void *virtio_net_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
152 QVirtioNetPCI
*virtio_bpci
= g_new0(QVirtioNetPCI
, 1);
153 QVirtioNet
*interface
= &virtio_bpci
->net
;
154 QOSGraphObject
*obj
= &virtio_bpci
->pci_vdev
.obj
;
156 virtio_pci_init(&virtio_bpci
->pci_vdev
, pci_bus
, addr
);
157 interface
->vdev
= &virtio_bpci
->pci_vdev
.vdev
;
160 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_NET
);
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
;
169 static void virtio_net_register_nodes(void)
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.
177 .devfn
= QPCI_DEVFN(4, 0),
180 QOSGraphEdgeOptions opts
= { };
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");
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");
197 libqos_init(virtio_net_register_nodes
);