vhost_net: add version_1 feature
[qemu/ar7.git] / hw / net / vhost_net.c
blobdc48ece695428df6e3a4474f3dcae9d7fdc25d23
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"
42 struct vhost_net {
43 struct vhost_dev dev;
44 struct vhost_virtqueue vqs[2];
45 int backend;
46 NetClientState *nc;
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 VIRTIO_F_VERSION_1,
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_STATUS,
80 VIRTIO_NET_F_CTRL_VQ,
81 VIRTIO_NET_F_CTRL_RX,
82 VIRTIO_NET_F_CTRL_VLAN,
83 VIRTIO_NET_F_CTRL_RX_EXTRA,
84 VIRTIO_NET_F_CTRL_MAC_ADDR,
85 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS,
87 VIRTIO_NET_F_MQ,
89 VHOST_INVALID_FEATURE_BIT
92 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
94 const int *feature_bits = 0;
96 switch (net->nc->info->type) {
97 case NET_CLIENT_OPTIONS_KIND_TAP:
98 feature_bits = kernel_feature_bits;
99 break;
100 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
101 feature_bits = user_feature_bits;
102 break;
103 default:
104 error_report("Feature bits not defined for this type: %d",
105 net->nc->info->type);
106 break;
109 return feature_bits;
112 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
114 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
115 features);
118 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
120 net->dev.acked_features = net->dev.backend_features;
121 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
124 static int vhost_net_get_fd(NetClientState *backend)
126 switch (backend->info->type) {
127 case NET_CLIENT_OPTIONS_KIND_TAP:
128 return tap_get_fd(backend);
129 default:
130 fprintf(stderr, "vhost-net requires tap backend\n");
131 return -EBADFD;
135 struct vhost_net *vhost_net_init(VhostNetOptions *options)
137 int r;
138 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL;
139 struct vhost_net *net = g_malloc(sizeof *net);
141 if (!options->net_backend) {
142 fprintf(stderr, "vhost-net requires net backend to be setup\n");
143 goto fail;
146 if (backend_kernel) {
147 r = vhost_net_get_fd(options->net_backend);
148 if (r < 0) {
149 goto fail;
151 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend)
152 ? 0 : (1 << VHOST_NET_F_VIRTIO_NET_HDR);
153 net->backend = r;
154 } else {
155 net->dev.backend_features = 0;
156 net->backend = -1;
158 net->nc = options->net_backend;
160 net->dev.nvqs = 2;
161 net->dev.vqs = net->vqs;
162 net->dev.vq_index = net->nc->queue_index;
164 r = vhost_dev_init(&net->dev, options->opaque,
165 options->backend_type, options->force);
166 if (r < 0) {
167 goto fail;
169 if (backend_kernel) {
170 if (!qemu_has_vnet_hdr_len(options->net_backend,
171 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
172 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
174 if (~net->dev.features & net->dev.backend_features) {
175 fprintf(stderr, "vhost lacks feature mask %" PRIu64
176 " for backend\n",
177 (uint64_t)(~net->dev.features & net->dev.backend_features));
178 vhost_dev_cleanup(&net->dev);
179 goto fail;
182 /* Set sane init value. Override when guest acks. */
183 vhost_net_ack_features(net, 0);
184 return net;
185 fail:
186 g_free(net);
187 return NULL;
190 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
192 return vhost_dev_query(&net->dev, dev);
195 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
197 net->dev.vq_index = vq_index;
200 static int vhost_net_start_one(struct vhost_net *net,
201 VirtIODevice *dev)
203 struct vhost_vring_file file = { };
204 int r;
206 net->dev.nvqs = 2;
207 net->dev.vqs = net->vqs;
209 r = vhost_dev_enable_notifiers(&net->dev, dev);
210 if (r < 0) {
211 goto fail_notifiers;
214 r = vhost_dev_start(&net->dev, dev);
215 if (r < 0) {
216 goto fail_start;
219 if (net->nc->info->poll) {
220 net->nc->info->poll(net->nc, false);
223 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
224 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
225 file.fd = net->backend;
226 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
227 const VhostOps *vhost_ops = net->dev.vhost_ops;
228 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
229 &file);
230 if (r < 0) {
231 r = -errno;
232 goto fail;
236 return 0;
237 fail:
238 file.fd = -1;
239 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
240 while (file.index-- > 0) {
241 const VhostOps *vhost_ops = net->dev.vhost_ops;
242 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
243 &file);
244 assert(r >= 0);
247 if (net->nc->info->poll) {
248 net->nc->info->poll(net->nc, true);
250 vhost_dev_stop(&net->dev, dev);
251 fail_start:
252 vhost_dev_disable_notifiers(&net->dev, dev);
253 fail_notifiers:
254 return r;
257 static void vhost_net_stop_one(struct vhost_net *net,
258 VirtIODevice *dev)
260 struct vhost_vring_file file = { .fd = -1 };
262 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) {
263 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
264 const VhostOps *vhost_ops = net->dev.vhost_ops;
265 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND,
266 &file);
267 assert(r >= 0);
269 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) {
270 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
271 const VhostOps *vhost_ops = net->dev.vhost_ops;
272 int r = vhost_ops->vhost_call(&net->dev, VHOST_RESET_OWNER,
273 &file);
274 assert(r >= 0);
277 if (net->nc->info->poll) {
278 net->nc->info->poll(net->nc, true);
280 vhost_dev_stop(&net->dev, dev);
281 vhost_dev_disable_notifiers(&net->dev, dev);
284 static bool vhost_net_device_endian_ok(VirtIODevice *vdev)
286 #ifdef TARGET_IS_BIENDIAN
287 #ifdef HOST_WORDS_BIGENDIAN
288 return virtio_is_big_endian(vdev);
289 #else
290 return !virtio_is_big_endian(vdev);
291 #endif
292 #else
293 return true;
294 #endif
297 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
298 int total_queues)
300 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
301 VirtioBusState *vbus = VIRTIO_BUS(qbus);
302 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
303 int r, e, i;
305 if (!vhost_net_device_endian_ok(dev)) {
306 error_report("vhost-net does not support cross-endian");
307 r = -ENOSYS;
308 goto err;
311 if (!k->set_guest_notifiers) {
312 error_report("binding does not support guest notifiers");
313 r = -ENOSYS;
314 goto err;
317 for (i = 0; i < total_queues; i++) {
318 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
321 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
322 if (r < 0) {
323 error_report("Error binding guest notifier: %d", -r);
324 goto err;
327 for (i = 0; i < total_queues; i++) {
328 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
330 if (r < 0) {
331 goto err_start;
335 return 0;
337 err_start:
338 while (--i >= 0) {
339 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
341 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
342 if (e < 0) {
343 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
344 fflush(stderr);
346 err:
347 return r;
350 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
351 int total_queues)
353 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
354 VirtioBusState *vbus = VIRTIO_BUS(qbus);
355 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
356 int i, r;
358 for (i = 0; i < total_queues; i++) {
359 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
362 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
363 if (r < 0) {
364 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
365 fflush(stderr);
367 assert(r >= 0);
370 void vhost_net_cleanup(struct vhost_net *net)
372 vhost_dev_cleanup(&net->dev);
373 g_free(net);
376 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
378 return vhost_virtqueue_pending(&net->dev, idx);
381 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
382 int idx, bool mask)
384 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
387 VHostNetState *get_vhost_net(NetClientState *nc)
389 VHostNetState *vhost_net = 0;
391 if (!nc) {
392 return 0;
395 switch (nc->info->type) {
396 case NET_CLIENT_OPTIONS_KIND_TAP:
397 vhost_net = tap_get_vhost_net(nc);
398 break;
399 case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
400 vhost_net = vhost_user_get_vhost_net(nc);
401 break;
402 default:
403 break;
406 return vhost_net;
408 #else
409 struct vhost_net *vhost_net_init(VhostNetOptions *options)
411 error_report("vhost-net support is not compiled in");
412 return NULL;
415 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
417 return false;
420 int vhost_net_start(VirtIODevice *dev,
421 NetClientState *ncs,
422 int total_queues)
424 return -ENOSYS;
426 void vhost_net_stop(VirtIODevice *dev,
427 NetClientState *ncs,
428 int total_queues)
432 void vhost_net_cleanup(struct vhost_net *net)
436 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
438 return features;
440 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
444 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
446 return false;
449 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
450 int idx, bool mask)
454 VHostNetState *get_vhost_net(NetClientState *nc)
456 return 0;
458 #endif