]> Git Repo - qemu.git/blob - tests/libqos/virtio-net.c
virtio-pci: Proxy for virtio-pmem
[qemu.git] / tests / libqos / virtio-net.c
1 /*
2  * libqos driver framework
3  *
4  * Copyright (c) 2018 Emanuele Giuseppe Esposito <[email protected]>
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 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 "libqos/qgraph.h"
23 #include "libqos/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                   (1u << VIRTIO_RING_F_INDIRECT_DESC) |
48                   (1u << VIRTIO_RING_F_EVENT_IDX));
49     qvirtio_set_features(vdev, features);
50
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;
55     }
56
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);
60     }
61     qvirtio_set_driver_ok(vdev);
62 }
63
64 /* virtio-net-device */
65 static void qvirtio_net_device_destructor(QOSGraphObject *obj)
66 {
67     QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
68     virtio_net_cleanup(&v_net->net);
69 }
70
71 static void qvirtio_net_device_start_hw(QOSGraphObject *obj)
72 {
73     QVirtioNetDevice *v_net = (QVirtioNetDevice *) obj;
74     QVirtioNet *interface = &v_net->net;
75
76     virtio_net_setup(interface);
77 }
78
79 static void *qvirtio_net_get_driver(QVirtioNet *v_net,
80                                     const char *interface)
81 {
82     if (!g_strcmp0(interface, "virtio-net")) {
83         return v_net;
84     }
85     if (!g_strcmp0(interface, "virtio")) {
86         return v_net->vdev;
87     }
88
89     fprintf(stderr, "%s not present in virtio-net-device\n", interface);
90     g_assert_not_reached();
91 }
92
93 static void *qvirtio_net_device_get_driver(void *object,
94                                            const char *interface)
95 {
96     QVirtioNetDevice *v_net = object;
97     return qvirtio_net_get_driver(&v_net->net, interface);
98 }
99
100 static void *virtio_net_device_create(void *virtio_dev,
101                                           QGuestAllocator *t_alloc,
102                                           void *addr)
103 {
104     QVirtioNetDevice *virtio_ndevice = g_new0(QVirtioNetDevice, 1);
105     QVirtioNet *interface = &virtio_ndevice->net;
106
107     interface->vdev = virtio_dev;
108     alloc = t_alloc;
109
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;
113
114     return &virtio_ndevice->obj;
115 }
116
117 /* virtio-net-pci */
118 static void qvirtio_net_pci_destructor(QOSGraphObject *obj)
119 {
120     QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
121     QVirtioNet *interface = &v_net->net;
122     QOSGraphObject *pci_vobj =  &v_net->pci_vdev.obj;
123
124     virtio_net_cleanup(interface);
125     qvirtio_pci_destructor(pci_vobj);
126 }
127
128 static void qvirtio_net_pci_start_hw(QOSGraphObject *obj)
129 {
130     QVirtioNetPCI *v_net = (QVirtioNetPCI *) obj;
131     QVirtioNet *interface = &v_net->net;
132     QOSGraphObject *pci_vobj =  &v_net->pci_vdev.obj;
133
134     qvirtio_pci_start_hw(pci_vobj);
135     virtio_net_setup(interface);
136 }
137
138 static void *qvirtio_net_pci_get_driver(void *object,
139                                             const char *interface)
140 {
141     QVirtioNetPCI *v_net = object;
142     if (!g_strcmp0(interface, "pci-device")) {
143         return v_net->pci_vdev.pdev;
144     }
145     return qvirtio_net_get_driver(&v_net->net, interface);
146 }
147
148 static void *virtio_net_pci_create(void *pci_bus, QGuestAllocator *t_alloc,
149                                   void *addr)
150 {
151     QVirtioNetPCI *virtio_bpci = g_new0(QVirtioNetPCI, 1);
152     QVirtioNet *interface = &virtio_bpci->net;
153     QOSGraphObject *obj = &virtio_bpci->pci_vdev.obj;
154
155     virtio_pci_init(&virtio_bpci->pci_vdev, pci_bus, addr);
156     interface->vdev = &virtio_bpci->pci_vdev.vdev;
157     alloc = t_alloc;
158
159     g_assert_cmphex(interface->vdev->device_type, ==, VIRTIO_ID_NET);
160
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;
164
165     return obj;
166 }
167
168 static void virtio_net_register_nodes(void)
169 {
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.
174      */
175     QPCIAddress addr = {
176         .devfn = QPCI_DEVFN(4, 0),
177     };
178
179     QOSGraphEdgeOptions opts = { };
180
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");
187
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");
194 }
195
196 libqos_init(virtio_net_register_nodes);
This page took 0.036963 seconds and 4 git commands to generate.