xref: /openbmc/qemu/hw/net/vhost_net.c (revision db76ec62)
1 /*
2  * vhost-net support
3  *
4  * Copyright Red Hat, Inc. 2010
5  *
6  * Authors:
7  *  Michael S. Tsirkin <mst@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15 
16 #include "net/net.h"
17 #include "net/tap.h"
18 
19 #include "hw/virtio/virtio-net.h"
20 #include "net/vhost_net.h"
21 #include "qemu/error-report.h"
22 
23 #include "config.h"
24 
25 #ifdef CONFIG_VHOST_NET
26 #include <linux/vhost.h>
27 #include <sys/socket.h>
28 #include <linux/kvm.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <linux/virtio_ring.h>
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
34 #include <net/if.h>
35 #include <netinet/in.h>
36 
37 #include <stdio.h>
38 
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
41 
42 struct vhost_net {
43     struct vhost_dev dev;
44     struct vhost_virtqueue vqs[2];
45     int backend;
46     NetClientState *nc;
47 };
48 
49 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
50 {
51     /* Clear features not supported by host kernel. */
52     if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
53         features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
54     }
55     if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
56         features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
57     }
58     if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
59         features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
60     }
61     if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
62         features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
63     }
64     return features;
65 }
66 
67 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
68 {
69     net->dev.acked_features = net->dev.backend_features;
70     if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
71         net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
72     }
73     if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
74         net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
75     }
76     if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
77         net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
78     }
79     if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
80         net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
81     }
82 }
83 
84 static int vhost_net_get_fd(NetClientState *backend)
85 {
86     switch (backend->info->type) {
87     case NET_CLIENT_OPTIONS_KIND_TAP:
88         return tap_get_fd(backend);
89     default:
90         fprintf(stderr, "vhost-net requires tap backend\n");
91         return -EBADFD;
92     }
93 }
94 
95 struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
96                                  bool force)
97 {
98     int r;
99     struct vhost_net *net = g_malloc(sizeof *net);
100     if (!backend) {
101         fprintf(stderr, "vhost-net requires backend to be setup\n");
102         goto fail;
103     }
104     r = vhost_net_get_fd(backend);
105     if (r < 0) {
106         goto fail;
107     }
108     net->nc = backend;
109     net->dev.backend_features = qemu_has_vnet_hdr(backend) ? 0 :
110         (1 << VHOST_NET_F_VIRTIO_NET_HDR);
111     net->backend = r;
112 
113     net->dev.nvqs = 2;
114     net->dev.vqs = net->vqs;
115 
116     r = vhost_dev_init(&net->dev, devfd, "/dev/vhost-net", force);
117     if (r < 0) {
118         goto fail;
119     }
120     if (!qemu_has_vnet_hdr_len(backend,
121                                sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
122         net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
123     }
124     if (~net->dev.features & net->dev.backend_features) {
125         fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
126                 (uint64_t)(~net->dev.features & net->dev.backend_features));
127         vhost_dev_cleanup(&net->dev);
128         goto fail;
129     }
130 
131     /* Set sane init value. Override when guest acks. */
132     vhost_net_ack_features(net, 0);
133     return net;
134 fail:
135     g_free(net);
136     return NULL;
137 }
138 
139 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
140 {
141     return vhost_dev_query(&net->dev, dev);
142 }
143 
144 static int vhost_net_start_one(struct vhost_net *net,
145                                VirtIODevice *dev,
146                                int vq_index)
147 {
148     struct vhost_vring_file file = { };
149     int r;
150 
151     if (net->dev.started) {
152         return 0;
153     }
154 
155     net->dev.nvqs = 2;
156     net->dev.vqs = net->vqs;
157     net->dev.vq_index = vq_index;
158 
159     r = vhost_dev_enable_notifiers(&net->dev, dev);
160     if (r < 0) {
161         goto fail_notifiers;
162     }
163 
164     r = vhost_dev_start(&net->dev, dev);
165     if (r < 0) {
166         goto fail_start;
167     }
168 
169     net->nc->info->poll(net->nc, false);
170     qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
171     file.fd = net->backend;
172     for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
173         r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
174         if (r < 0) {
175             r = -errno;
176             goto fail;
177         }
178     }
179     return 0;
180 fail:
181     file.fd = -1;
182     while (file.index-- > 0) {
183         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
184         assert(r >= 0);
185     }
186     net->nc->info->poll(net->nc, true);
187     vhost_dev_stop(&net->dev, dev);
188 fail_start:
189     vhost_dev_disable_notifiers(&net->dev, dev);
190 fail_notifiers:
191     return r;
192 }
193 
194 static void vhost_net_stop_one(struct vhost_net *net,
195                                VirtIODevice *dev)
196 {
197     struct vhost_vring_file file = { .fd = -1 };
198 
199     if (!net->dev.started) {
200         return;
201     }
202 
203     for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
204         int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
205         assert(r >= 0);
206     }
207     net->nc->info->poll(net->nc, true);
208     vhost_dev_stop(&net->dev, dev);
209     vhost_dev_disable_notifiers(&net->dev, dev);
210 }
211 
212 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
213                     int total_queues)
214 {
215     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
216     VirtioBusState *vbus = VIRTIO_BUS(qbus);
217     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
218     int r, i = 0;
219 
220     if (!k->set_guest_notifiers) {
221         error_report("binding does not support guest notifiers");
222         r = -ENOSYS;
223         goto err;
224     }
225 
226     for (i = 0; i < total_queues; i++) {
227         r = vhost_net_start_one(tap_get_vhost_net(ncs[i].peer), dev, i * 2);
228 
229         if (r < 0) {
230             goto err;
231         }
232     }
233 
234     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
235     if (r < 0) {
236         error_report("Error binding guest notifier: %d", -r);
237         goto err;
238     }
239 
240     return 0;
241 
242 err:
243     while (--i >= 0) {
244         vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
245     }
246     return r;
247 }
248 
249 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
250                     int total_queues)
251 {
252     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
253     VirtioBusState *vbus = VIRTIO_BUS(qbus);
254     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
255     int i, r;
256 
257     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
258     if (r < 0) {
259         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
260         fflush(stderr);
261     }
262     assert(r >= 0);
263 
264     for (i = 0; i < total_queues; i++) {
265         vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
266     }
267 }
268 
269 void vhost_net_cleanup(struct vhost_net *net)
270 {
271     vhost_dev_cleanup(&net->dev);
272     g_free(net);
273 }
274 
275 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
276 {
277     return vhost_virtqueue_pending(&net->dev, idx);
278 }
279 
280 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
281                               int idx, bool mask)
282 {
283     vhost_virtqueue_mask(&net->dev, dev, idx, mask);
284 }
285 #else
286 struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
287                                  bool force)
288 {
289     error_report("vhost-net support is not compiled in");
290     return NULL;
291 }
292 
293 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
294 {
295     return false;
296 }
297 
298 int vhost_net_start(VirtIODevice *dev,
299                     NetClientState *ncs,
300                     int total_queues)
301 {
302     return -ENOSYS;
303 }
304 void vhost_net_stop(VirtIODevice *dev,
305                     NetClientState *ncs,
306                     int total_queues)
307 {
308 }
309 
310 void vhost_net_cleanup(struct vhost_net *net)
311 {
312 }
313 
314 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
315 {
316     return features;
317 }
318 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
319 {
320 }
321 
322 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
323 {
324     return false;
325 }
326 
327 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
328                               int idx, bool mask)
329 {
330 }
331 #endif
332