vhost_net_init will use VhostNetOptions to get all its arguments
[qemu.git] / hw / net / vhost_net.c
blob7a5523f36af937003fa27fe5e2b3f4f323f63cd4
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 /* 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 VHOST_INVALID_FEATURE_BIT
58 static const int *vhost_net_get_feature_bits(struct vhost_net *net)
60 const int *feature_bits = 0;
62 switch (net->nc->info->type) {
63 case NET_CLIENT_OPTIONS_KIND_TAP:
64 feature_bits = kernel_feature_bits;
65 break;
66 default:
67 error_report("Feature bits not defined for this type: %d",
68 net->nc->info->type);
69 break;
72 return feature_bits;
75 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
77 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net),
78 features);
81 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
83 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features);
86 static int vhost_net_get_fd(NetClientState *backend)
88 switch (backend->info->type) {
89 case NET_CLIENT_OPTIONS_KIND_TAP:
90 return tap_get_fd(backend);
91 default:
92 fprintf(stderr, "vhost-net requires tap backend\n");
93 return -EBADFD;
97 struct vhost_net *vhost_net_init(VhostNetOptions *options)
99 int r;
100 struct vhost_net *net = g_malloc(sizeof *net);
102 if (!options->net_backend) {
103 fprintf(stderr, "vhost-net requires net backend to be setup\n");
104 goto fail;
107 r = vhost_net_get_fd(options->net_backend);
108 if (r < 0) {
109 goto fail;
111 net->nc = options->net_backend;
112 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) ? 0 :
113 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
114 net->backend = r;
116 net->dev.nvqs = 2;
117 net->dev.vqs = net->vqs;
119 r = vhost_dev_init(&net->dev, options->opaque,
120 options->force);
121 if (r < 0) {
122 goto fail;
124 if (!qemu_has_vnet_hdr_len(options->net_backend,
125 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
126 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
128 if (~net->dev.features & net->dev.backend_features) {
129 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
130 (uint64_t)(~net->dev.features & net->dev.backend_features));
131 vhost_dev_cleanup(&net->dev);
132 goto fail;
135 /* Set sane init value. Override when guest acks. */
136 vhost_net_ack_features(net, 0);
137 return net;
138 fail:
139 g_free(net);
140 return NULL;
143 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
145 return vhost_dev_query(&net->dev, dev);
148 static int vhost_net_start_one(struct vhost_net *net,
149 VirtIODevice *dev,
150 int vq_index)
152 struct vhost_vring_file file = { };
153 int r;
155 if (net->dev.started) {
156 return 0;
159 net->dev.nvqs = 2;
160 net->dev.vqs = net->vqs;
161 net->dev.vq_index = vq_index;
163 r = vhost_dev_enable_notifiers(&net->dev, dev);
164 if (r < 0) {
165 goto fail_notifiers;
168 r = vhost_dev_start(&net->dev, dev);
169 if (r < 0) {
170 goto fail_start;
173 if (net->nc->info->poll) {
174 net->nc->info->poll(net->nc, false);
177 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
178 file.fd = net->backend;
179 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
180 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
181 if (r < 0) {
182 r = -errno;
183 goto fail;
186 return 0;
187 fail:
188 file.fd = -1;
189 while (file.index-- > 0) {
190 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
191 assert(r >= 0);
193 if (net->nc->info->poll) {
194 net->nc->info->poll(net->nc, true);
196 vhost_dev_stop(&net->dev, dev);
197 fail_start:
198 vhost_dev_disable_notifiers(&net->dev, dev);
199 fail_notifiers:
200 return r;
203 static void vhost_net_stop_one(struct vhost_net *net,
204 VirtIODevice *dev)
206 struct vhost_vring_file file = { .fd = -1 };
208 if (!net->dev.started) {
209 return;
212 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
213 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
214 assert(r >= 0);
216 if (net->nc->info->poll) {
217 net->nc->info->poll(net->nc, true);
219 vhost_dev_stop(&net->dev, dev);
220 vhost_dev_disable_notifiers(&net->dev, dev);
223 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
224 int total_queues)
226 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
227 VirtioBusState *vbus = VIRTIO_BUS(qbus);
228 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
229 int r, i = 0;
231 if (!k->set_guest_notifiers) {
232 error_report("binding does not support guest notifiers");
233 r = -ENOSYS;
234 goto err;
237 for (i = 0; i < total_queues; i++) {
238 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev, i * 2);
240 if (r < 0) {
241 goto err;
245 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
246 if (r < 0) {
247 error_report("Error binding guest notifier: %d", -r);
248 goto err;
251 return 0;
253 err:
254 while (--i >= 0) {
255 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
257 return r;
260 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
261 int total_queues)
263 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
264 VirtioBusState *vbus = VIRTIO_BUS(qbus);
265 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
266 int i, r;
268 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
269 if (r < 0) {
270 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
271 fflush(stderr);
273 assert(r >= 0);
275 for (i = 0; i < total_queues; i++) {
276 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
280 void vhost_net_cleanup(struct vhost_net *net)
282 vhost_dev_cleanup(&net->dev);
283 g_free(net);
286 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
288 return vhost_virtqueue_pending(&net->dev, idx);
291 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
292 int idx, bool mask)
294 vhost_virtqueue_mask(&net->dev, dev, idx, mask);
297 VHostNetState *get_vhost_net(NetClientState *nc)
299 VHostNetState *vhost_net = 0;
301 if (!nc) {
302 return 0;
305 switch (nc->info->type) {
306 case NET_CLIENT_OPTIONS_KIND_TAP:
307 vhost_net = tap_get_vhost_net(nc);
308 break;
309 default:
310 break;
313 return vhost_net;
315 #else
316 struct vhost_net *vhost_net_init(VhostNetOptions *options)
318 error_report("vhost-net support is not compiled in");
319 return NULL;
322 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
324 return false;
327 int vhost_net_start(VirtIODevice *dev,
328 NetClientState *ncs,
329 int total_queues)
331 return -ENOSYS;
333 void vhost_net_stop(VirtIODevice *dev,
334 NetClientState *ncs,
335 int total_queues)
339 void vhost_net_cleanup(struct vhost_net *net)
343 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
345 return features;
347 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
351 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx)
353 return false;
356 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev,
357 int idx, bool mask)
361 VHostNetState *get_vhost_net(NetClientState *nc)
363 return 0;
365 #endif