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