Update OpenBIOS images to 04898e8 built from submodule.
[qemu/ar7.git] / hw / net / vhost_net.c
blob22874a977730918fb3dd464ba4ab87318b230f6c
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 "qemu/osdep.h"
17 #include "net/net.h"
18 #include "net/tap.h"
19 #include "net/vhost-user.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "net/vhost_net.h"
23 #include "qemu/error-report.h"
26 #ifdef CONFIG_VHOST_NET
27 #include <linux/vhost.h>
28 #include <sys/socket.h>
29 #include <linux/kvm.h>
30 #include <netpacket/packet.h>
31 #include <net/ethernet.h>
32 #include <net/if.h>
33 #include <netinet/in.h>
36 #include "standard-headers/linux/virtio_ring.h"
37 #include "hw/virtio/vhost.h"
38 #include "hw/virtio/virtio-bus.h"
40 struct vhost_net {
41 struct vhost_dev dev;
42 struct vhost_virtqueue vqs[2];
43 int backend;
44 NetClientState *nc;
47 /* Features supported by host kernel. */
48 static const int kernel_feature_bits[] = {
49 VIRTIO_F_NOTIFY_ON_EMPTY,
50 VIRTIO_RING_F_INDIRECT_DESC,
51 VIRTIO_RING_F_EVENT_IDX,
52 VIRTIO_NET_F_MRG_RXBUF,
53 VIRTIO_F_VERSION_1,
54 VIRTIO_NET_F_MTU,
55 VIRTIO_F_IOMMU_PLATFORM,
56 VHOST_INVALID_FEATURE_BIT
59 /* Features supported by others. */
60 static const int user_feature_bits[] = {
61 VIRTIO_F_NOTIFY_ON_EMPTY,
62 VIRTIO_RING_F_INDIRECT_DESC,
63 VIRTIO_RING_F_EVENT_IDX,
65 VIRTIO_F_ANY_LAYOUT,
66 VIRTIO_F_VERSION_1,
67 VIRTIO_NET_F_CSUM,
68 VIRTIO_NET_F_GUEST_CSUM,
69 VIRTIO_NET_F_GSO,
70 VIRTIO_NET_F_GUEST_TSO4,
71 VIRTIO_NET_F_GUEST_TSO6,
72 VIRTIO_NET_F_GUEST_ECN,
73 VIRTIO_NET_F_GUEST_UFO,
74 VIRTIO_NET_F_HOST_TSO4,
75 VIRTIO_NET_F_HOST_TSO6,
76 VIRTIO_NET_F_HOST_ECN,
77 VIRTIO_NET_F_HOST_UFO,
78 VIRTIO_NET_F_MRG_RXBUF,
79 VIRTIO_NET_F_MTU,
81 /* This bit implies RARP isn't sent by QEMU out of band */
82 VIRTIO_NET_F_GUEST_ANNOUNCE,
84 VIRTIO_NET_F_MQ,
86 VHOST_INVALID_FEATURE_BIT
89 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
91 const int *feature_bits = 0;
93 switch (net->nc->info->type) {
94 case NET_CLIENT_DRIVER_TAP:
95 feature_bits = kernel_feature_bits;
96 break;
97 case NET_CLIENT_DRIVER_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;
106 return feature_bits;
109 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
111 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
112 features);
115 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
117 net->dev.acked_features = net->dev.backend_features;
118 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
121 uint64_t vhost_net_get_max_queues(VHostNetState *net)
123 return net->dev.max_queues;
126 uint64_t vhost_net_get_acked_features(VHostNetState *net)
128 return net->dev.acked_features;
131 static int vhost_net_get_fd(NetClientState *backend)
133 switch (backend->info->type) {
134 case NET_CLIENT_DRIVER_TAP:
135 return tap_get_fd(backend);
136 default:
137 fprintf(stderr, "vhost-net requires tap backend\n");
138 return -EBADFD;
142 struct vhost_net *vhost_net_init(VhostNetOptions *options)
144 int r;
145 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
146 struct vhost_net *net = g_new0(struct vhost_net, 1);
147 uint64_t features = 0;
149 if (!options->net_backend) {
150 fprintf(stderr, "vhost-net requires net backend to be setup\n");
151 goto fail;
153 net->nc = options->net_backend;
155 net->dev.max_queues = 1;
156 net->dev.nvqs = 2;
157 net->dev.vqs = net->vqs;
159 if (backend_kernel) {
160 r = vhost_net_get_fd(options->net_backend);
161 if (r < 0) {
162 goto fail;
164 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
165 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR);
166 net->backend = r;
167 net->dev.protocol_features = 0;
168 } else {
169 net->dev.backend_features = 0;
170 net->dev.protocol_features = 0;
171 net->backend = -1;
173 /* vhost-user needs vq_index to initiate a specific queue pair */
174 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs;
177 r = vhost_dev_init(&net->dev, options->opaque,
178 options->backend_type, options->busyloop_timeout);
179 if (r < 0) {
180 goto fail;
182 if (backend_kernel) {
183 if (!qemu_has_vnet_hdr_len(options->net_backend,
184 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
185 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF);
187 if (~net->dev.features & net->dev.backend_features) {
188 fprintf(stderr, "vhost lacks feature mask %" PRIu64
189 " for backend\n",
190 (uint64_t)(~net->dev.features & net->dev.backend_features));
191 goto fail;
195 /* Set sane init value. Override when guest acks. */
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;
206 vhost_net_ack_features(net, features);
208 return net;
210 fail:
211 vhost_dev_cleanup(&net->dev);
212 g_free(net);
213 return NULL;
216 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
218 net->dev.vq_index = vq_index;
221 static int vhost_net_start_one(struct vhost_net *net,
222 VirtIODevice *dev)
224 struct vhost_vring_file file = { };
225 int r;
227 net->dev.nvqs = 2;
228 net->dev.vqs = net->vqs;
230 r = vhost_dev_enable_notifiers(&net->dev, dev);
231 if (r < 0) {
232 goto fail_notifiers;
235 r = vhost_dev_start(&net->dev, dev);
236 if (r < 0) {
237 goto fail_start;
240 if (net->nc->info->poll) {
241 net->nc->info->poll(net->nc, false);
244 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
245 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
246 file.fd = net->backend;
247 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
248 r = vhost_net_set_backend(&net->dev, &file);
249 if (r < 0) {
250 r = -errno;
251 goto fail;
255 return 0;
256 fail:
257 file.fd = -1;
258 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
259 while (file.index-- > 0) {
260 int r = vhost_net_set_backend(&net->dev, &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_DRIVER_TAP) {
280 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
281 int r = vhost_net_set_backend(&net->dev, &file);
282 assert(r >= 0);
285 if (net->nc->info->poll) {
286 net->nc->info->poll(net->nc, true);
288 vhost_dev_stop(&net->dev, dev);
289 vhost_dev_disable_notifiers(&net->dev, dev);
292 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
293 int total_queues)
295 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
296 VirtioBusState *vbus = VIRTIO_BUS(qbus);
297 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
298 int r, e, i;
300 if (!k->set_guest_notifiers) {
301 error_report("binding does not support guest notifiers");
302 return -ENOSYS;
305 for (i = 0; i < total_queues; i++) {
306 struct vhost_net *net;
308 net = get_vhost_net(ncs[i].peer);
309 vhost_net_set_vq_index(net, i * 2);
311 /* Suppress the masking guest notifiers on vhost user
312 * because vhost user doesn't interrupt masking/unmasking
313 * properly.
315 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
316 dev->use_guest_notifier_mask = false;
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;
326 for (i = 0; i < total_queues; i++) {
327 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
329 if (r < 0) {
330 goto err_start;
333 if (ncs[i].peer->vring_enable) {
334 /* restore vring enable state */
335 r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable);
337 if (r < 0) {
338 goto err_start;
343 return 0;
345 err_start:
346 while (--i >= 0) {
347 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
349 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
350 if (e < 0) {
351 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
352 fflush(stderr);
354 err:
355 return r;
358 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
359 int total_queues)
361 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
362 VirtioBusState *vbus = VIRTIO_BUS(qbus);
363 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
364 int i, r;
366 for (i = 0; i < total_queues; i++) {
367 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
370 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
371 if (r < 0) {
372 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
373 fflush(stderr);
375 assert(r >= 0);
378 void vhost_net_cleanup(struct vhost_net *net)
380 vhost_dev_cleanup(&net->dev);
383 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
385 const VhostOps *vhost_ops = net->dev.vhost_ops;
387 assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
388 assert(vhost_ops->vhost_migration_done);
390 return vhost_ops->vhost_migration_done(&net->dev, mac_addr);
393 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
395 return vhost_virtqueue_pending(&net->dev, idx);
398 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
399 int idx, bool mask)
401 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
404 VHostNetState *get_vhost_net(NetClientState *nc)
406 VHostNetState *vhost_net = 0;
408 if (!nc) {
409 return 0;
412 switch (nc->info->type) {
413 case NET_CLIENT_DRIVER_TAP:
414 vhost_net = tap_get_vhost_net(nc);
415 break;
416 case NET_CLIENT_DRIVER_VHOST_USER:
417 vhost_net = vhost_user_get_vhost_net(nc);
418 assert(vhost_net);
419 break;
420 default:
421 break;
424 return vhost_net;
427 int vhost_set_vring_enable(NetClientState *nc, int enable)
429 VHostNetState *net = get_vhost_net(nc);
430 const VhostOps *vhost_ops = net->dev.vhost_ops;
432 nc->vring_enable = enable;
434 if (vhost_ops && vhost_ops->vhost_set_vring_enable) {
435 return vhost_ops->vhost_set_vring_enable(&net->dev, enable);
438 return 0;
441 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
443 const VhostOps *vhost_ops = net->dev.vhost_ops;
445 if (!vhost_ops->vhost_net_set_mtu) {
446 return 0;
449 return vhost_ops->vhost_net_set_mtu(&net->dev, mtu);
452 #else
453 uint64_t vhost_net_get_max_queues(VHostNetState *net)
455 return 1;
458 struct vhost_net *vhost_net_init(VhostNetOptions *options)
460 error_report("vhost-net support is not compiled in");
461 return NULL;
464 int vhost_net_start(VirtIODevice *dev,
465 NetClientState *ncs,
466 int total_queues)
468 return -ENOSYS;
470 void vhost_net_stop(VirtIODevice *dev,
471 NetClientState *ncs,
472 int total_queues)
476 void vhost_net_cleanup(struct vhost_net *net)
480 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features)
482 return features;
485 void vhost_net_ack_features(struct vhost_net *net, uint64_t features)
489 uint64_t vhost_net_get_acked_features(VHostNetState *net)
491 return 0;
494 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
496 return false;
499 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
500 int idx, bool mask)
504 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr)
506 return -1;
509 VHostNetState *get_vhost_net(NetClientState *nc)
511 return 0;
514 int vhost_set_vring_enable(NetClientState *nc, int enable)
516 return 0;
519 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu)
521 return 0;
523 #endif