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"
21 #include "libqos/qgraph.h"
22 #include "libqos/virtio-net.h"
23 #include "hw/virtio/virtio-net.h"
26 static QGuestAllocator
*alloc
;
28 static void virtio_net_cleanup(QVirtioNet
*interface
)
32 for (i
= 0; i
< interface
->n_queues
; i
++) {
33 qvirtqueue_cleanup(interface
->vdev
->bus
, interface
->queues
[i
], alloc
);
35 g_free(interface
->queues
);
38 static void virtio_net_setup(QVirtioNet
*interface
)
40 QVirtioDevice
*vdev
= interface
->vdev
;
44 features
= qvirtio_get_features(vdev
);
45 features
&= ~(QVIRTIO_F_BAD_FEATURE
|
46 (1u << VIRTIO_RING_F_INDIRECT_DESC
) |
47 (1u << VIRTIO_RING_F_EVENT_IDX
));
48 qvirtio_set_features(vdev
, features
);
50 if (features
& (1u << VIRTIO_NET_F_MQ
)) {
51 interface
->n_queues
= qvirtio_config_readw(vdev
, 8) * 2;
53 interface
->n_queues
= 2;
56 interface
->queues
= g_new(QVirtQueue
*, interface
->n_queues
);
57 for (i
= 0; i
< interface
->n_queues
; i
++) {
58 interface
->queues
[i
] = qvirtqueue_setup(vdev
, alloc
, i
);
60 qvirtio_set_driver_ok(vdev
);
63 /* virtio-net-device */
64 static void qvirtio_net_device_destructor(QOSGraphObject
*obj
)
66 QVirtioNetDevice
*v_net
= (QVirtioNetDevice
*) obj
;
67 virtio_net_cleanup(&v_net
->net
);
70 static void qvirtio_net_device_start_hw(QOSGraphObject
*obj
)
72 QVirtioNetDevice
*v_net
= (QVirtioNetDevice
*) obj
;
73 QVirtioNet
*interface
= &v_net
->net
;
75 virtio_net_setup(interface
);
78 static void *qvirtio_net_get_driver(QVirtioNet
*v_net
,
79 const char *interface
)
81 if (!g_strcmp0(interface
, "virtio-net")) {
84 if (!g_strcmp0(interface
, "virtio")) {
88 fprintf(stderr
, "%s not present in virtio-net-device\n", interface
);
89 g_assert_not_reached();
92 static void *qvirtio_net_device_get_driver(void *object
,
93 const char *interface
)
95 QVirtioNetDevice
*v_net
= object
;
96 return qvirtio_net_get_driver(&v_net
->net
, interface
);
99 static void *virtio_net_device_create(void *virtio_dev
,
100 QGuestAllocator
*t_alloc
,
103 QVirtioNetDevice
*virtio_ndevice
= g_new0(QVirtioNetDevice
, 1);
104 QVirtioNet
*interface
= &virtio_ndevice
->net
;
106 interface
->vdev
= virtio_dev
;
109 virtio_ndevice
->obj
.destructor
= qvirtio_net_device_destructor
;
110 virtio_ndevice
->obj
.get_driver
= qvirtio_net_device_get_driver
;
111 virtio_ndevice
->obj
.start_hw
= qvirtio_net_device_start_hw
;
113 return &virtio_ndevice
->obj
;
117 static void qvirtio_net_pci_destructor(QOSGraphObject
*obj
)
119 QVirtioNetPCI
*v_net
= (QVirtioNetPCI
*) obj
;
120 QVirtioNet
*interface
= &v_net
->net
;
121 QOSGraphObject
*pci_vobj
= &v_net
->pci_vdev
.obj
;
123 virtio_net_cleanup(interface
);
124 qvirtio_pci_destructor(pci_vobj
);
127 static void qvirtio_net_pci_start_hw(QOSGraphObject
*obj
)
129 QVirtioNetPCI
*v_net
= (QVirtioNetPCI
*) obj
;
130 QVirtioNet
*interface
= &v_net
->net
;
131 QOSGraphObject
*pci_vobj
= &v_net
->pci_vdev
.obj
;
133 qvirtio_pci_start_hw(pci_vobj
);
134 virtio_net_setup(interface
);
137 static void *qvirtio_net_pci_get_driver(void *object
,
138 const char *interface
)
140 QVirtioNetPCI
*v_net
= object
;
141 if (!g_strcmp0(interface
, "pci-device")) {
142 return v_net
->pci_vdev
.pdev
;
144 return qvirtio_net_get_driver(&v_net
->net
, interface
);
147 static void *virtio_net_pci_create(void *pci_bus
, QGuestAllocator
*t_alloc
,
150 QVirtioNetPCI
*virtio_bpci
= g_new0(QVirtioNetPCI
, 1);
151 QVirtioNet
*interface
= &virtio_bpci
->net
;
152 QOSGraphObject
*obj
= &virtio_bpci
->pci_vdev
.obj
;
154 virtio_pci_init(&virtio_bpci
->pci_vdev
, pci_bus
, addr
);
155 interface
->vdev
= &virtio_bpci
->pci_vdev
.vdev
;
158 g_assert_cmphex(interface
->vdev
->device_type
, ==, VIRTIO_ID_NET
);
160 obj
->destructor
= qvirtio_net_pci_destructor
;
161 obj
->start_hw
= qvirtio_net_pci_start_hw
;
162 obj
->get_driver
= qvirtio_net_pci_get_driver
;
167 static void virtio_net_register_nodes(void)
169 /* FIXME: every test using these nodes needs to setup a
170 * -netdev socket,id=hs0 otherwise QEMU is not going to start.
171 * Therefore, we do not include "produces" edge for virtio
172 * and pci-device yet.
175 .devfn
= QPCI_DEVFN(4, 0),
178 QOSGraphEdgeOptions opts
= { };
180 /* virtio-net-device */
181 opts
.extra_device_opts
= "netdev=hs0";
182 qos_node_create_driver("virtio-net-device",
183 virtio_net_device_create
);
184 qos_node_consumes("virtio-net-device", "virtio-bus", &opts
);
185 qos_node_produces("virtio-net-device", "virtio-net");
188 opts
.extra_device_opts
= "netdev=hs0,addr=04.0";
189 add_qpci_address(&opts
, &addr
);
190 qos_node_create_driver("virtio-net-pci", virtio_net_pci_create
);
191 qos_node_consumes("virtio-net-pci", "pci-bus", &opts
);
192 qos_node_produces("virtio-net-pci", "virtio-net");
195 libqos_init(virtio_net_register_nodes
);