linux-user: stack_base is now mandatory on all targets
[qemu.git] / hw / vhost_net.c
1 /*
2 * vhost-net support
3 *
4 * Copyright Red Hat, Inc. 2010
5 *
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
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.
14 */
15
16 #include "net.h"
17 #include "net/tap.h"
18
19 #include "virtio-net.h"
20 #include "vhost_net.h"
21 #include "qemu-error.h"
22
23 #include "config.h"
24
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>
36
37 #include <stdio.h>
38
39 #include "vhost.h"
40
41 struct vhost_net {
42 struct vhost_dev dev;
43 struct vhost_virtqueue vqs[2];
44 int backend;
45 VLANClientState *vc;
46 };
47
48 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
49 {
50 /* Clear features not supported by host kernel. */
51 if (!(net->dev.features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY))) {
52 features &= ~(1 << VIRTIO_F_NOTIFY_ON_EMPTY);
53 }
54 if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) {
55 features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
56 }
57 if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) {
58 features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
59 }
60 if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) {
61 features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
62 }
63 return features;
64 }
65
66 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
67 {
68 net->dev.acked_features = net->dev.backend_features;
69 if (features & (1 << VIRTIO_F_NOTIFY_ON_EMPTY)) {
70 net->dev.acked_features |= (1 << VIRTIO_F_NOTIFY_ON_EMPTY);
71 }
72 if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) {
73 net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC);
74 }
75 if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) {
76 net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX);
77 }
78 if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
79 net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF);
80 }
81 }
82
83 static int vhost_net_get_fd(VLANClientState *backend)
84 {
85 switch (backend->info->type) {
86 case NET_CLIENT_TYPE_TAP:
87 return tap_get_fd(backend);
88 default:
89 fprintf(stderr, "vhost-net requires tap backend\n");
90 return -EBADFD;
91 }
92 }
93
94 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
95 bool force)
96 {
97 int r;
98 struct vhost_net *net = g_malloc(sizeof *net);
99 if (!backend) {
100 fprintf(stderr, "vhost-net requires backend to be setup\n");
101 goto fail;
102 }
103 r = vhost_net_get_fd(backend);
104 if (r < 0) {
105 goto fail;
106 }
107 net->vc = backend;
108 net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 :
109 (1 << VHOST_NET_F_VIRTIO_NET_HDR);
110 net->backend = r;
111
112 r = vhost_dev_init(&net->dev, devfd, force);
113 if (r < 0) {
114 goto fail;
115 }
116 if (!tap_has_vnet_hdr_len(backend,
117 sizeof(struct virtio_net_hdr_mrg_rxbuf))) {
118 net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF);
119 }
120 if (~net->dev.features & net->dev.backend_features) {
121 fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n",
122 (uint64_t)(~net->dev.features & net->dev.backend_features));
123 vhost_dev_cleanup(&net->dev);
124 goto fail;
125 }
126
127 /* Set sane init value. Override when guest acks. */
128 vhost_net_ack_features(net, 0);
129 return net;
130 fail:
131 g_free(net);
132 return NULL;
133 }
134
135 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
136 {
137 return vhost_dev_query(&net->dev, dev);
138 }
139
140 int vhost_net_start(struct vhost_net *net,
141 VirtIODevice *dev)
142 {
143 struct vhost_vring_file file = { };
144 int r;
145
146 net->dev.nvqs = 2;
147 net->dev.vqs = net->vqs;
148
149 r = vhost_dev_enable_notifiers(&net->dev, dev);
150 if (r < 0) {
151 goto fail_notifiers;
152 }
153 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
154 tap_set_vnet_hdr_len(net->vc,
155 sizeof(struct virtio_net_hdr_mrg_rxbuf));
156 }
157
158 r = vhost_dev_start(&net->dev, dev);
159 if (r < 0) {
160 goto fail_start;
161 }
162
163 net->vc->info->poll(net->vc, false);
164 qemu_set_fd_handler(net->backend, NULL, NULL, NULL);
165 file.fd = net->backend;
166 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
167 r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
168 if (r < 0) {
169 r = -errno;
170 goto fail;
171 }
172 }
173 return 0;
174 fail:
175 file.fd = -1;
176 while (file.index-- > 0) {
177 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
178 assert(r >= 0);
179 }
180 net->vc->info->poll(net->vc, true);
181 vhost_dev_stop(&net->dev, dev);
182 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
183 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
184 }
185 fail_start:
186 vhost_dev_disable_notifiers(&net->dev, dev);
187 fail_notifiers:
188 return r;
189 }
190
191 void vhost_net_stop(struct vhost_net *net,
192 VirtIODevice *dev)
193 {
194 struct vhost_vring_file file = { .fd = -1 };
195
196 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) {
197 int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file);
198 assert(r >= 0);
199 }
200 net->vc->info->poll(net->vc, true);
201 vhost_dev_stop(&net->dev, dev);
202 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
203 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
204 }
205 vhost_dev_disable_notifiers(&net->dev, dev);
206 }
207
208 void vhost_net_cleanup(struct vhost_net *net)
209 {
210 vhost_dev_cleanup(&net->dev);
211 if (net->dev.acked_features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
212 tap_set_vnet_hdr_len(net->vc, sizeof(struct virtio_net_hdr));
213 }
214 g_free(net);
215 }
216 #else
217 struct vhost_net *vhost_net_init(VLANClientState *backend, int devfd,
218 bool force)
219 {
220 error_report("vhost-net support is not compiled in");
221 return NULL;
222 }
223
224 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
225 {
226 return false;
227 }
228
229 int vhost_net_start(struct vhost_net *net,
230 VirtIODevice *dev)
231 {
232 return -ENOSYS;
233 }
234 void vhost_net_stop(struct vhost_net *net,
235 VirtIODevice *dev)
236 {
237 }
238
239 void vhost_net_cleanup(struct vhost_net *net)
240 {
241 }
242
243 unsigned vhost_net_get_features(struct vhost_net *net, unsigned features)
244 {
245 return features;
246 }
247 void vhost_net_ack_features(struct vhost_net *net, unsigned features)
248 {
249 }
250 #endif