sysemu.h: Document what MAX_CPUMASK_BITS really limits
[qemu/kevin.git] / hw / net / vhost_net.c
bloba1de2f43a0f911ced788b3e8b42c2c9606a6565d
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"
19 #include "hw/virtio/virtio-net.h"
20 #include "net/vhost_net.h"
21 #include "qemu/error-report.h"
23 #include "config.h"
25 #ifdef CONFIG_VHOST_NET
26 #include <linux/vhost.h>
27 #include <sys/socket.h>
28 #include <linux/kvm.h>
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <linux/virtio_ring.h>
32 #include <netpacket/packet.h>
33 #include <net/ethernet.h>
34 #include <net/if.h>
35 #include <netinet/in.h>
37 #include <stdio.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 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
51 /* Clear features not supported by host kernel. */
52 if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
53 features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
55 if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
56 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
58 if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
59 features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
61 if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
62 features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
64 return features;
67 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
69 net->dev.acked_features = net->dev.backend_features;
70 if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
71 net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
73 if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
74 net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
76 if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
77 net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
79 if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
80 net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
84 static int vhost_net_get_fd(NetClientState *backend)
86 switch (backend->info->type) {
87 case NET_CLIENT_OPTIONS_KIND_TAP:
88 return tap_get_fd(backend);
89 default:
90 fprintf(stderr, "vhost-net requires tap backend\n");
91 return -EBADFD;
95 struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
96 bool force)
98 int r;
99 struct vhost_net *net = g_malloc(sizeof *net);
100 if (!backend) {
101 fprintf(stderr, "vhost-net requires backend to be setup\n");
102 goto fail;
104 r = vhost_net_get_fd(backend);
105 if (r < 0) {
106 goto fail;
108 net->nc = backend;
109 net->dev.backend_features = qemu_has_vnet_hdr(backend) ? 0 :
110 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
111 net->backend = r;
113 net->dev.nvqs = 2;
114 net->dev.vqs = net->vqs;
116 r = vhost_dev_init(&net->dev, devfd, "/dev/vhost-net", force);
117 if (r < 0) {
118 goto fail;
120 if (!qemu_has_vnet_hdr_len(backend,
121 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
122 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
124 if (~net->dev.features & net->dev.backend_features) {
125 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
126 (uint64_t)(~net->dev.features & net->dev.backend_features));
127 vhost_dev_cleanup(&net->dev);
128 goto fail;
131 /* Set sane init value. Override when guest acks. */
132 vhost_net_ack_features(net, 0);
133 return net;
134 fail:
135 g_free(net);
136 return NULL;
139 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
141 return vhost_dev_query(&net->dev, dev);
144 static int vhost_net_start_one(struct vhost_net *net,
145 VirtIODevice *dev,
146 int vq_index)
148 struct vhost_vring_file file = { };
149 int r;
151 if (net->dev.started) {
152 return 0;
155 net->dev.nvqs = 2;
156 net->dev.vqs = net->vqs;
157 net->dev.vq_index = vq_index;
159 r = vhost_dev_enable_notifiers(&net->dev, dev);
160 if (r < 0) {
161 goto fail_notifiers;
164 r = vhost_dev_start(&net->dev, dev);
165 if (r < 0) {
166 goto fail_start;
169 net->nc->info->poll(net->nc, false);
170 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
171 file.fd = net->backend;
172 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
173 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
174 if (r < 0) {
175 r = -errno;
176 goto fail;
179 return 0;
180 fail:
181 file.fd = -1;
182 while (file.index-- > 0) {
183 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
184 assert(r >= 0);
186 net->nc->info->poll(net->nc, true);
187 vhost_dev_stop(&net->dev, dev);
188 fail_start:
189 vhost_dev_disable_notifiers(&net->dev, dev);
190 fail_notifiers:
191 return r;
194 static void vhost_net_stop_one(struct vhost_net *net,
195 VirtIODevice *dev)
197 struct vhost_vring_file file = { .fd = -1 };
199 if (!net->dev.started) {
200 return;
203 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
204 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
205 assert(r >= 0);
207 net->nc->info->poll(net->nc, true);
208 vhost_dev_stop(&net->dev, dev);
209 vhost_dev_disable_notifiers(&net->dev, dev);
212 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
213 int total_queues)
215 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
216 VirtioBusState *vbus = VIRTIO_BUS(qbus);
217 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
218 int r, i = 0;
220 if (!k->set_guest_notifiers) {
221 error_report("binding does not support guest notifiers");
222 r = -ENOSYS;
223 goto err;
226 for (i = 0; i < total_queues; i++) {
227 r = vhost_net_start_one(tap_get_vhost_net(ncs[i].peer), dev, i * 2);
229 if (r < 0) {
230 goto err;
234 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
235 if (r < 0) {
236 error_report("Error binding guest notifier: %d", -r);
237 goto err;
240 return 0;
242 err:
243 while (--i >= 0) {
244 vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
246 return r;
249 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
250 int total_queues)
252 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
253 VirtioBusState *vbus = VIRTIO_BUS(qbus);
254 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
255 int i, r;
257 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
258 if (r < 0) {
259 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
260 fflush(stderr);
262 assert(r >= 0);
264 for (i = 0; i < total_queues; i++) {
265 vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev);
269 void vhost_net_cleanup(struct vhost_net *net)
271 vhost_dev_cleanup(&net->dev);
272 g_free(net);
275 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
277 return vhost_virtqueue_pending(&net->dev, idx);
280 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
281 int idx, bool mask)
283 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
285 #else
286 struct vhost_net *vhost_net_init(NetClientState *backend, int devfd,
287 bool force)
289 error_report("vhost-net support is not compiled in");
290 return NULL;
293 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
295 return false;
298 int vhost_net_start(VirtIODevice *dev,
299 NetClientState *ncs,
300 int total_queues)
302 return -ENOSYS;
304 void vhost_net_stop(VirtIODevice *dev,
305 NetClientState *ncs,
306 int total_queues)
310 void vhost_net_cleanup(struct vhost_net *net)
314 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
316 return features;
318 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
322 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
324 return false;
327 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
328 int idx, bool mask)
331 #endif