xref: /openbmc/qemu/tests/qtest/libqos/virtio-net.c (revision 041c2a31)
1 /*
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
8  * License version 2.1 as published by the Free Software Foundation.
9  *
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"
21 #include "qemu/module.h"
22 #include "qgraph.h"
23 #include "virtio-net.h"
24 #include "hw/virtio/virtio-net.h"
25 
26 
27 static QGuestAllocator *alloc;
28 
29 static void virtio_net_cleanup(QVirtioNet *interface)
30 {
31     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);
37 }
38 
39 static void virtio_net_setup(QVirtioNet *interface)
40 {
41     QVirtioDevice *vdev = interface->vdev;
42     uint64_t features;
43     int i;
44 
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);
50 
51     if (features & (1ull << VIRTIO_NET_F_MQ)) {
52         interface->n_queues = qvirtio_config_readw(vdev, 8) * 2;
53     } else {
54         interface->n_queues = 2;
55     }
56     interface->n_queues++; /* Account for the ctrl queue */
57 
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     }
62     qvirtio_set_driver_ok(vdev);
63 }
64 
65 /* virtio-net-device */
66 static void qvirtio_net_device_destructor(QOSGraphObject *obj)
67 {
68     QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
69     virtio_net_cleanup(&v_net->net);
70 }
71 
72 static 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 
80 static 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 
94 static 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 
101 static 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 */
119 static 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 
129 static 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 
139 static 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 
149 static 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 
169 static 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 
197 libqos_init(virtio_net_register_nodes);
198