Merge remote-tracking branch 'kraxel/usb.16' into staging
[qemu.git] / hw / vhost_net.c
blob420e05f112882ade2aab69c3a1ae5facffa02ebb
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.
13 #include "net.h"
14 #include "net/tap.h"
16 #include "virtio-net.h"
17 #include "vhost_net.h"
19 #include "config.h"
21 #ifdef CONFIG_VHOST_NET
22 #include <linux/vhost.h>
23 #include <sys/socket.h>
24 #include <linux/kvm.h>
25 #include <fcntl.h>
26 #include <sys/ioctl.h>
27 #include <linux/virtio_ring.h>
28 #include <netpacket/packet.h>
29 #include <net/ethernet.h>
30 #include <net/if.h>
31 #include <netinet/in.h>
33 #include <stdio.h>
35 #include "vhost.h"
37 struct vhost_net {
38 struct vhost_dev dev;
39 struct vhost_virtqueue vqs[2];
40 int backend;
41 VLANClientState *vc;
44 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
46 /* Clear features not supported by host kernel. */
47 if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
48 features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
50 if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
51 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
53 if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
54 features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
56 return features;
59 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
61 net->dev.acked_features = net->dev.backend_features;
62 if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
63 net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
65 if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
66 net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
68 if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
69 net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
73 static int vhost_net_get_fd(VLANClientState *backend)
75 switch (backend->info->type) {
76 case NET_CLIENT_TYPE_TAP:
77 return tap_get_fd(backend);
78 default:
79 fprintf(stderr, "vhost-net requires tap backend\n");
80 return -EBADFD;
84 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
85 bool force)
87 int r;
88 struct vhost_net *net = qemu_malloc(sizeof *net);
89 if (!backend) {
90 fprintf(stderr, "vhost-net requires backend to be setup\n");
91 goto fail;
93 r = vhost_net_get_fd(backend);
94 if (r < 0) {
95 goto fail;
97 net->vc = backend;
98 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
99 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
100 net->backend = r;
102 r = vhost_dev_init(&net->dev, devfd, force);
103 if (r < 0) {
104 goto fail;
106 if (!tap_has_vnet_hdr_len(backend,
107 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
108 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
110 if (~net->dev.features & net->dev.backend_features) {
111 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
112 (uint64_t)(~net->dev.features & net->dev.backend_features));
113 vhost_dev_cleanup(&net->dev);
114 goto fail;
117 /* Set sane init value. Override when guest acks. */
118 vhost_net_ack_features(net, 0);
119 return net;
120 fail:
121 qemu_free(net);
122 return NULL;
125 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
127 return vhost_dev_query(&net->dev, dev);
130 int vhost_net_start(struct vhost_net *net,
131 VirtIODevice *dev)
133 struct vhost_vring_file file = { };
134 int r;
135 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
136 tap_set_vnet_hdr_len(net->vc,
137 sizeof(struct virtio_net_hdr_mrg_rxbuf));
140 net->dev.nvqs = 2;
141 net->dev.vqs = net->vqs;
142 r = vhost_dev_start(&net->dev, dev);
143 if (r < 0) {
144 return r;
147 net->vc->info->poll(net->vc, false);
148 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
149 file.fd = net->backend;
150 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
151 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
152 if (r < 0) {
153 r = -errno;
154 goto fail;
157 return 0;
158 fail:
159 file.fd = -1;
160 while (file.index-- > 0) {
161 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
162 assert(r >= 0);
164 net->vc->info->poll(net->vc, true);
165 vhost_dev_stop(&net->dev, dev);
166 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
167 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
169 return r;
172 void vhost_net_stop(struct vhost_net *net,
173 VirtIODevice *dev)
175 struct vhost_vring_file file = { .fd = -1 };
177 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
178 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
179 assert(r >= 0);
181 net->vc->info->poll(net->vc, true);
182 vhost_dev_stop(&net->dev, dev);
183 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
184 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
188 void vhost_net_cleanup(struct vhost_net *net)
190 vhost_dev_cleanup(&net->dev);
191 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
192 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
194 qemu_free(net);
196 #else
197 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
198 bool force)
200 return NULL;
203 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
205 return false;
208 int vhost_net_start(struct vhost_net *net,
209 VirtIODevice *dev)
211 return -ENOSYS;
213 void vhost_net_stop(struct vhost_net *net,
214 VirtIODevice *dev)
218 void vhost_net_cleanup(struct vhost_net *net)
222 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
224 return features;
226 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
229 #endif