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