xref: /openbmc/qemu/hw/net/vhost_net.c (revision 4f2d31fb)
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 #include "net/vhost-user.h"
19 
20 #include "hw/virtio/virtio-net.h"
21 #include "net/vhost_net.h"
22 #include "qemu/error-report.h"
23 
24 #include "config.h"
25 
26 #ifdef CONFIG_VHOST_NET
27 #include <linux/vhost.h>
28 #include <sys/socket.h>
29 #include <linux/kvm.h>
30 #include <fcntl.h>
31 #include <netpacket/packet.h>
32 #include <net/ethernet.h>
33 #include <net/if.h>
34 #include <netinet/in.h>
35 
36 #include <stdio.h>
37 
38 #include "standard-headers/linux/virtio_ring.h"
39 #include "hw/virtio/vhost.h"
40 #include "hw/virtio/virtio-bus.h"
41 #include "hw/virtio/virtio-access.h"
42 
43 struct vhost_net {
44     struct vhost_dev dev;
45     struct vhost_virtqueue vqs[2];
46     int backend;
47     NetClientState *nc;
48 };
49 
50 /* Features supported by host kernel. */
51 static const int kernel_feature_bits[] = {
52     VIRTIO_F_NOTIFY_ON_EMPTY,
53     VIRTIO_RING_F_INDIRECT_DESC,
54     VIRTIO_RING_F_EVENT_IDX,
55     VIRTIO_NET_F_MRG_RXBUF,
56     VIRTIO_F_VERSION_1,
57     VHOST_INVALID_FEATURE_BIT
58 };
59 
60 /* Features supported by others. */
61 static const int user_feature_bits[] = {
62     VIRTIO_F_NOTIFY_ON_EMPTY,
63     VIRTIO_RING_F_INDIRECT_DESC,
64     VIRTIO_RING_F_EVENT_IDX,
65 
66     VIRTIO_F_ANY_LAYOUT,
67     VIRTIO_F_VERSION_1,
68     VIRTIO_NET_F_CSUM,
69     VIRTIO_NET_F_GUEST_CSUM,
70     VIRTIO_NET_F_GSO,
71     VIRTIO_NET_F_GUEST_TSO4,
72     VIRTIO_NET_F_GUEST_TSO6,
73     VIRTIO_NET_F_GUEST_ECN,
74     VIRTIO_NET_F_GUEST_UFO,
75     VIRTIO_NET_F_HOST_TSO4,
76     VIRTIO_NET_F_HOST_TSO6,
77     VIRTIO_NET_F_HOST_ECN,
78     VIRTIO_NET_F_HOST_UFO,
79     VIRTIO_NET_F_MRG_RXBUF,
80 
81     /* This bit implies RARP isn't sent by QEMU out of band */
82     VIRTIO_NET_F_GUEST_ANNOUNCE,
83 
84     VIRTIO_NET_F_MQ,
85 
86     VHOST_INVALID_FEATURE_BIT
87 };
88 
89 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
90 {
91     const int *feature_bits = 0;
92 
93     switch (net->nc->info->type) {
94     case NET_CLIENT_OPTIONS_KIND_TAP:
95         feature_bits = kernel_feature_bits;
96         break;
97     case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
98         feature_bits = user_feature_bits;
99         break;
100     default:
101         error_report("Feature bits not defined for this type: %d",
102                 net->nc->info->type);
103         break;
104     }
105 
106     return feature_bits;
107 }
108 
109 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
110 {
111     return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
112             features);
113 }
114 
115 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
116 {
117     net->dev.acked_features = net->dev.backend_features;
118     vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
119 }
120 
121 uint64_t vhost_net_get_max_queues(VHostNetState *net)
122 {
123     return net->dev.max_queues;
124 }
125 
126 static int vhost_net_get_fd(NetClientState *backend)
127 {
128     switch (backend->info->type) {
129     case NET_CLIENT_OPTIONS_KIND_TAP:
130         return tap_get_fd(backend);
131     default:
132         fprintf(stderr, "vhost-net requires tap backend\n");
133         return -EBADFD;
134     }
135 }
136 
137 struct vhost_net *vhost_net_init(VhostNetOptions *options)
138 {
139     int r;
140     bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
141     struct vhost_net *net = g_malloc(sizeof *net);
142 
143     if (!options->net_backend) {
144         fprintf(stderr, "vhost-net requires net backend to be setup\n");
145         goto fail;
146     }
147     net->nc = options->net_backend;
148 
149     net->dev.max_queues = 1;
150     net->dev.nvqs = 2;
151     net->dev.vqs = net->vqs;
152 
153     if (backend_kernel) {
154         r = vhost_net_get_fd(options->net_backend);
155         if (r < 0) {
156             goto fail;
157         }
158         net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
159             ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
160         net->backend = r;
161         net->dev.protocol_features = 0;
162     } else {
163         net->dev.backend_features = 0;
164         net->dev.protocol_features = 0;
165         net->backend = -1;
166 
167         /* vhost-user needs vq_index to initiate a specific queue pair */
168         net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
169     }
170 
171     r = vhost_dev_init(&net->dev, options->opaque,
172                        options->backend_type);
173     if (r < 0) {
174         goto fail;
175     }
176     if (backend_kernel) {
177         if (!qemu_has_vnet_hdr_len(options->net_backend,
178                                sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
179             net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
180         }
181         if (~net->dev.features & net->dev.backend_features) {
182             fprintf(stderr, "vhost lacks feature mask %" PRIu64
183                    " for backend\n",
184                    (uint64_t)(~net->dev.features & net->dev.backend_features));
185             vhost_dev_cleanup(&net->dev);
186             goto fail;
187         }
188     }
189     /* Set sane init value. Override when guest acks. */
190     vhost_net_ack_features(net, 0);
191     return net;
192 fail:
193     g_free(net);
194     return NULL;
195 }
196 
197 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
198 {
199     net->dev.vq_index = vq_index;
200 }
201 
202 static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer,
203                                      bool set)
204 {
205     int r = 0;
206 
207     if (virtio_vdev_has_feature(dev, VIRTIO_F_VERSION_1) ||
208         (virtio_legacy_is_cross_endian(dev) && !virtio_is_big_endian(dev))) {
209         r = qemu_set_vnet_le(peer, set);
210         if (r) {
211             error_report("backend does not support LE vnet headers");
212         }
213     } else if (virtio_legacy_is_cross_endian(dev)) {
214         r = qemu_set_vnet_be(peer, set);
215         if (r) {
216             error_report("backend does not support BE vnet headers");
217         }
218     }
219 
220     return r;
221 }
222 
223 static int vhost_net_start_one(struct vhost_net *net,
224                                VirtIODevice *dev)
225 {
226     struct vhost_vring_file file = { };
227     int r;
228 
229     net->dev.nvqs = 2;
230     net->dev.vqs = net->vqs;
231 
232     r = vhost_dev_enable_notifiers(&net->dev, dev);
233     if (r < 0) {
234         goto fail_notifiers;
235     }
236 
237     r = vhost_dev_start(&net->dev, dev);
238     if (r < 0) {
239         goto fail_start;
240     }
241 
242     if (net->nc->info->poll) {
243         net->nc->info->poll(net->nc, false);
244     }
245 
246     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
247         qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
248         file.fd = net->backend;
249         for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
250             const VhostOps *vhost_ops = net->dev.vhost_ops;
251             r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
252             if (r < 0) {
253                 r = -errno;
254                 goto fail;
255             }
256         }
257     }
258     return 0;
259 fail:
260     file.fd = -1;
261     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
262         while (file.index-- > 0) {
263             const VhostOps *vhost_ops = net->dev.vhost_ops;
264             int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
265             assert(r >= 0);
266         }
267     }
268     if (net->nc->info->poll) {
269         net->nc->info->poll(net->nc, true);
270     }
271     vhost_dev_stop(&net->dev, dev);
272 fail_start:
273     vhost_dev_disable_notifiers(&net->dev, dev);
274 fail_notifiers:
275     return r;
276 }
277 
278 static void vhost_net_stop_one(struct vhost_net *net,
279                                VirtIODevice *dev)
280 {
281     struct vhost_vring_file file = { .fd = -1 };
282 
283     if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
284         for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
285             const VhostOps *vhost_ops = net->dev.vhost_ops;
286             int r = vhost_ops->vhost_net_set_backend(&net->dev, &file);
287             assert(r >= 0);
288         }
289     }
290     if (net->nc->info->poll) {
291         net->nc->info->poll(net->nc, true);
292     }
293     vhost_dev_stop(&net->dev, dev);
294     vhost_dev_disable_notifiers(&net->dev, dev);
295 }
296 
297 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
298                     int total_queues)
299 {
300     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
301     VirtioBusState *vbus = VIRTIO_BUS(qbus);
302     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
303     int r, e, i;
304 
305     if (!k->set_guest_notifiers) {
306         error_report("binding does not support guest notifiers");
307         r = -ENOSYS;
308         goto err;
309     }
310 
311     r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true);
312     if (r < 0) {
313         goto err;
314     }
315 
316     for (i = 0; i < total_queues; i++) {
317         vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
318     }
319 
320     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
321     if (r < 0) {
322         error_report("Error binding guest notifier: %d", -r);
323         goto err_endian;
324     }
325 
326     for (i = 0; i < total_queues; i++) {
327         r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
328 
329         if (r < 0) {
330             goto err_start;
331         }
332     }
333 
334     return 0;
335 
336 err_start:
337     while (--i >= 0) {
338         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
339     }
340     e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
341     if (e < 0) {
342         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
343         fflush(stderr);
344     }
345 err_endian:
346     vhost_net_set_vnet_endian(dev, ncs[0].peer, false);
347 err:
348     return r;
349 }
350 
351 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
352                     int total_queues)
353 {
354     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
355     VirtioBusState *vbus = VIRTIO_BUS(qbus);
356     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
357     int i, r;
358 
359     for (i = 0; i < total_queues; i++) {
360         vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
361     }
362 
363     r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
364     if (r < 0) {
365         fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
366         fflush(stderr);
367     }
368     assert(r >= 0);
369 
370     assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0);
371 }
372 
373 void vhost_net_cleanup(struct vhost_net *net)
374 {
375     vhost_dev_cleanup(&net->dev);
376     g_free(net);
377 }
378 
379 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
380 {
381     const VhostOps *vhost_ops = net->dev.vhost_ops;
382     int r = -1;
383 
384     if (vhost_ops->vhost_migration_done) {
385         r = vhost_ops->vhost_migration_done(&net->dev, mac_addr);
386     }
387 
388     return r;
389 }
390 
391 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
392 {
393     return vhost_virtqueue_pending(&net->dev, idx);
394 }
395 
396 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
397                               int idx, bool mask)
398 {
399     vhost_virtqueue_mask(&net->dev, dev, idx, mask);
400 }
401 
402 VHostNetState *get_vhost_net(NetClientState *nc)
403 {
404     VHostNetState *vhost_net = 0;
405 
406     if (!nc) {
407         return 0;
408     }
409 
410     switch (nc->info->type) {
411     case NET_CLIENT_OPTIONS_KIND_TAP:
412         vhost_net = tap_get_vhost_net(nc);
413         break;
414     case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
415         vhost_net = vhost_user_get_vhost_net(nc);
416         break;
417     default:
418         break;
419     }
420 
421     return vhost_net;
422 }
423 
424 int vhost_set_vring_enable(NetClientState *nc, int enable)
425 {
426     VHostNetState *net = get_vhost_net(nc);
427     const VhostOps *vhost_ops = net->dev.vhost_ops;
428 
429     if (vhost_ops->vhost_set_vring_enable) {
430         return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
431     }
432 
433     return 0;
434 }
435 
436 #else
437 uint64_t vhost_net_get_max_queues(VHostNetState *net)
438 {
439     return 1;
440 }
441 
442 struct vhost_net *vhost_net_init(VhostNetOptions *options)
443 {
444     error_report("vhost-net support is not compiled in");
445     return NULL;
446 }
447 
448 int vhost_net_start(VirtIODevice *dev,
449                     NetClientState *ncs,
450                     int total_queues)
451 {
452     return -ENOSYS;
453 }
454 void vhost_net_stop(VirtIODevice *dev,
455                     NetClientState *ncs,
456                     int total_queues)
457 {
458 }
459 
460 void vhost_net_cleanup(struct vhost_net *net)
461 {
462 }
463 
464 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
465 {
466     return features;
467 }
468 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
469 {
470 }
471 
472 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
473 {
474     return false;
475 }
476 
477 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
478                               int idx, bool mask)
479 {
480 }
481 
482 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
483 {
484     return -1;
485 }
486 
487 VHostNetState *get_vhost_net(NetClientState *nc)
488 {
489     return 0;
490 }
491 
492 int vhost_set_vring_enable(NetClientState *nc, int enable)
493 {
494     return 0;
495 }
496 #endif
497