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