Merge remote-tracking branch 'remotes/mst/tags/for_upstream' into staging
[qemu.git] / hw / net / vhost_net.c
blob9bd360bd179bab123b3c76c74796cc48590a9a4a
1 /*
2 * vhost-net support
4 * Copyright Red Hat, Inc. 2010
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "net/net.h"
17 #include "net/tap.h"
18 #include "net/vhost-user.h"
20 #include "hw/virtio/virtio-net.h"
21 #include "net/vhost_net.h"
22 #include "qemu/error-report.h"
24 #include "config.h"
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>
36 #include <stdio.h>
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"
43 struct vhost_net {
44 struct vhost_dev dev;
45 struct vhost_virtqueue vqs[2];
46 int backend;
47 NetClientState *nc;
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
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,
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 VIRTIO_NET_F_STATUS,
81 VIRTIO_NET_F_CTRL_VQ,
82 VIRTIO_NET_F_CTRL_RX,
83 VIRTIO_NET_F_CTRL_VLAN,
84 VIRTIO_NET_F_CTRL_RX_EXTRA,
85 VIRTIO_NET_F_CTRL_MAC_ADDR,
86 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
88 VIRTIO_NET_F_MQ,
90 VHOST_INVALID_FEATURE_BIT
93 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
95 const int *feature_bits = 0;
97 switch (net->nc->info->type) {
98 case NET_CLIENT_OPTIONS_KIND_TAP:
99 feature_bits = kernel_feature_bits;
100 break;
101 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
102 feature_bits = user_feature_bits;
103 break;
104 default:
105 error_report("Feature bits not defined for this type: %d",
106 net->nc->info->type);
107 break;
110 return feature_bits;
113 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
115 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
116 features);
119 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
121 net->dev.acked_features = net->dev.backend_features;
122 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
125 static int vhost_net_get_fd(NetClientState *backend)
127 switch (backend->info->type) {
128 case NET_CLIENT_OPTIONS_KIND_TAP:
129 return tap_get_fd(backend);
130 default:
131 fprintf(stderr, "vhost-net requires tap backend\n");
132 return -EBADFD;
136 struct vhost_net *vhost_net_init(VhostNetOptions *options)
138 int r;
139 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
140 struct vhost_net *net = g_malloc(sizeof *net);
142 if (!options->net_backend) {
143 fprintf(stderr, "vhost-net requires net backend to be setup\n");
144 goto fail;
147 if (backend_kernel) {
148 r = vhost_net_get_fd(options->net_backend);
149 if (r < 0) {
150 goto fail;
152 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
153 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
154 net->backend = r;
155 } else {
156 net->dev.backend_features = 0;
157 net->backend = -1;
159 net->nc = options->net_backend;
161 net->dev.nvqs = 2;
162 net->dev.vqs = net->vqs;
163 net->dev.vq_index = net->nc->queue_index;
165 r = vhost_dev_init(&net->dev, options->opaque,
166 options->backend_type);
167 if (r < 0) {
168 goto fail;
170 if (backend_kernel) {
171 if (!qemu_has_vnet_hdr_len(options->net_backend,
172 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
173 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
175 if (~net->dev.features & net->dev.backend_features) {
176 fprintf(stderr, "vhost lacks feature mask %" PRIu64
177 " for backend\n",
178 (uint64_t)(~net->dev.features & net->dev.backend_features));
179 vhost_dev_cleanup(&net->dev);
180 goto fail;
183 /* Set sane init value. Override when guest acks. */
184 vhost_net_ack_features(net, 0);
185 return net;
186 fail:
187 g_free(net);
188 return NULL;
191 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
193 net->dev.vq_index = vq_index;
196 static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer,
197 bool set)
199 int r = 0;
201 if (virtio_has_feature(dev, VIRTIO_F_VERSION_1) ||
202 (virtio_legacy_is_cross_endian(dev) && !virtio_is_big_endian(dev))) {
203 r = qemu_set_vnet_le(peer, set);
204 if (r) {
205 error_report("backend does not support LE vnet headers");
207 } else if (virtio_legacy_is_cross_endian(dev)) {
208 r = qemu_set_vnet_be(peer, set);
209 if (r) {
210 error_report("backend does not support BE vnet headers");
214 return r;
217 static int vhost_net_start_one(struct vhost_net *net,
218 VirtIODevice *dev)
220 struct vhost_vring_file file = { };
221 int r;
223 net->dev.nvqs = 2;
224 net->dev.vqs = net->vqs;
226 r = vhost_dev_enable_notifiers(&net->dev, dev);
227 if (r < 0) {
228 goto fail_notifiers;
231 r = vhost_dev_start(&net->dev, dev);
232 if (r < 0) {
233 goto fail_start;
236 if (net->nc->info->poll) {
237 net->nc->info->poll(net->nc, false);
240 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
241 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
242 file.fd = net->backend;
243 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
244 const VhostOps *vhost_ops = net->dev.vhost_ops;
245 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
246 &file);
247 if (r < 0) {
248 r = -errno;
249 goto fail;
253 return 0;
254 fail:
255 file.fd = -1;
256 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
257 while (file.index-- > 0) {
258 const VhostOps *vhost_ops = net->dev.vhost_ops;
259 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
260 &file);
261 assert(r >= 0);
264 if (net->nc->info->poll) {
265 net->nc->info->poll(net->nc, true);
267 vhost_dev_stop(&net->dev, dev);
268 fail_start:
269 vhost_dev_disable_notifiers(&net->dev, dev);
270 fail_notifiers:
271 return r;
274 static void vhost_net_stop_one(struct vhost_net *net,
275 VirtIODevice *dev)
277 struct vhost_vring_file file = { .fd = -1 };
279 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
280 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
281 const VhostOps *vhost_ops = net->dev.vhost_ops;
282 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
283 &file);
284 assert(r >= 0);
286 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
287 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
288 const VhostOps *vhost_ops = net->dev.vhost_ops;
289 int r = vhost_ops->vhost_call(&net->dev, VHOST_RESET_OWNER,
290 &file);
291 assert(r >= 0);
294 if (net->nc->info->poll) {
295 net->nc->info->poll(net->nc, true);
297 vhost_dev_stop(&net->dev, dev);
298 vhost_dev_disable_notifiers(&net->dev, dev);
301 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
302 int total_queues)
304 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
305 VirtioBusState *vbus = VIRTIO_BUS(qbus);
306 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
307 int r, e, i;
309 if (!k->set_guest_notifiers) {
310 error_report("binding does not support guest notifiers");
311 r = -ENOSYS;
312 goto err;
315 r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true);
316 if (r < 0) {
317 goto err;
320 for (i = 0; i < total_queues; i++) {
321 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
324 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
325 if (r < 0) {
326 error_report("Error binding guest notifier: %d", -r);
327 goto err_endian;
330 for (i = 0; i < total_queues; i++) {
331 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
333 if (r < 0) {
334 goto err_start;
338 return 0;
340 err_start:
341 while (--i >= 0) {
342 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
344 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
345 if (e < 0) {
346 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
347 fflush(stderr);
349 err_endian:
350 vhost_net_set_vnet_endian(dev, ncs[0].peer, false);
351 err:
352 return r;
355 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
356 int total_queues)
358 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
359 VirtioBusState *vbus = VIRTIO_BUS(qbus);
360 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
361 int i, r;
363 for (i = 0; i < total_queues; i++) {
364 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
367 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
368 if (r < 0) {
369 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
370 fflush(stderr);
372 assert(r >= 0);
374 assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0);
377 void vhost_net_cleanup(struct vhost_net *net)
379 vhost_dev_cleanup(&net->dev);
380 g_free(net);
383 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
385 return vhost_virtqueue_pending(&net->dev, idx);
388 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
389 int idx, bool mask)
391 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
394 VHostNetState *get_vhost_net(NetClientState *nc)
396 VHostNetState *vhost_net = 0;
398 if (!nc) {
399 return 0;
402 switch (nc->info->type) {
403 case NET_CLIENT_OPTIONS_KIND_TAP:
404 vhost_net = tap_get_vhost_net(nc);
405 break;
406 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
407 vhost_net = vhost_user_get_vhost_net(nc);
408 break;
409 default:
410 break;
413 return vhost_net;
415 #else
416 struct vhost_net *vhost_net_init(VhostNetOptions *options)
418 error_report("vhost-net support is not compiled in");
419 return NULL;
422 int vhost_net_start(VirtIODevice *dev,
423 NetClientState *ncs,
424 int total_queues)
426 return -ENOSYS;
428 void vhost_net_stop(VirtIODevice *dev,
429 NetClientState *ncs,
430 int total_queues)
434 void vhost_net_cleanup(struct vhost_net *net)
438 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
440 return features;
442 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
446 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
448 return false;
451 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
452 int idx, bool mask)
456 VHostNetState *get_vhost_net(NetClientState *nc)
458 return 0;
460 #endif