]>
Commit | Line | Data |
---|---|---|
d5970055 MT |
1 | /* |
2 | * vhost-net support | |
3 | * | |
4 | * Copyright Red Hat, Inc. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Michael S. Tsirkin <[email protected]> | |
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. | |
6b620ca3 PB |
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. | |
d5970055 MT |
14 | */ |
15 | ||
1422e32d | 16 | #include "net/net.h" |
d5970055 MT |
17 | #include "net/tap.h" |
18 | ||
0d09e41a PB |
19 | #include "hw/virtio/virtio-net.h" |
20 | #include "net/vhost_net.h" | |
1de7afc9 | 21 | #include "qemu/error-report.h" |
d5970055 MT |
22 | |
23 | #include "config.h" | |
24 | ||
25 | #ifdef CONFIG_VHOST_NET | |
26 | #include <linux/vhost.h> | |
d5970055 MT |
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 | ||
0d09e41a | 39 | #include "hw/virtio/vhost.h" |
1c819449 | 40 | #include "hw/virtio/virtio-bus.h" |
d5970055 MT |
41 | |
42 | struct vhost_net { | |
43 | struct vhost_dev dev; | |
44 | struct vhost_virtqueue vqs[2]; | |
45 | int backend; | |
35277d14 | 46 | NetClientState *nc; |
d5970055 MT |
47 | }; |
48 | ||
49 | unsigned vhost_net_get_features(struct vhost_net *net, unsigned features) | |
50 | { | |
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); | |
54 | } | |
55 | if (!(net->dev.features & (1 << VIRTIO_RING_F_INDIRECT_DESC))) { | |
56 | features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC); | |
57 | } | |
bcbabae8 MT |
58 | if (!(net->dev.features & (1 << VIRTIO_RING_F_EVENT_IDX))) { |
59 | features &= ~(1 << VIRTIO_RING_F_EVENT_IDX); | |
60 | } | |
ca736c8e MT |
61 | if (!(net->dev.features & (1 << VIRTIO_NET_F_MRG_RXBUF))) { |
62 | features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF); | |
63 | } | |
d5970055 MT |
64 | return features; |
65 | } | |
66 | ||
67 | void vhost_net_ack_features(struct vhost_net *net, unsigned features) | |
68 | { | |
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); | |
72 | } | |
73 | if (features & (1 << VIRTIO_RING_F_INDIRECT_DESC)) { | |
74 | net->dev.acked_features |= (1 << VIRTIO_RING_F_INDIRECT_DESC); | |
75 | } | |
bcbabae8 MT |
76 | if (features & (1 << VIRTIO_RING_F_EVENT_IDX)) { |
77 | net->dev.acked_features |= (1 << VIRTIO_RING_F_EVENT_IDX); | |
78 | } | |
ca736c8e MT |
79 | if (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) { |
80 | net->dev.acked_features |= (1 << VIRTIO_NET_F_MRG_RXBUF); | |
81 | } | |
d5970055 MT |
82 | } |
83 | ||
4e68f7a0 | 84 | static int vhost_net_get_fd(NetClientState *backend) |
d5970055 MT |
85 | { |
86 | switch (backend->info->type) { | |
2be64a68 | 87 | case NET_CLIENT_OPTIONS_KIND_TAP: |
d5970055 MT |
88 | return tap_get_fd(backend); |
89 | default: | |
90 | fprintf(stderr, "vhost-net requires tap backend\n"); | |
91 | return -EBADFD; | |
92 | } | |
93 | } | |
94 | ||
4e68f7a0 | 95 | struct vhost_net *vhost_net_init(NetClientState *backend, int devfd, |
5430a28f | 96 | bool force) |
d5970055 MT |
97 | { |
98 | int r; | |
7267c094 | 99 | struct vhost_net *net = g_malloc(sizeof *net); |
d5970055 MT |
100 | if (!backend) { |
101 | fprintf(stderr, "vhost-net requires backend to be setup\n"); | |
102 | goto fail; | |
103 | } | |
104 | r = vhost_net_get_fd(backend); | |
105 | if (r < 0) { | |
106 | goto fail; | |
107 | } | |
35277d14 | 108 | net->nc = backend; |
d5970055 MT |
109 | net->dev.backend_features = tap_has_vnet_hdr(backend) ? 0 : |
110 | (1 << VHOST_NET_F_VIRTIO_NET_HDR); | |
111 | net->backend = r; | |
112 | ||
f56a1247 MT |
113 | net->dev.nvqs = 2; |
114 | net->dev.vqs = net->vqs; | |
115 | ||
1241ed94 | 116 | r = vhost_dev_init(&net->dev, devfd, "/dev/vhost-net", force); |
d5970055 MT |
117 | if (r < 0) { |
118 | goto fail; | |
119 | } | |
ca736c8e MT |
120 | if (!tap_has_vnet_hdr_len(backend, |
121 | sizeof(struct virtio_net_hdr_mrg_rxbuf))) { | |
122 | net->dev.features &= ~(1 << VIRTIO_NET_F_MRG_RXBUF); | |
123 | } | |
d5970055 | 124 | if (~net->dev.features & net->dev.backend_features) { |
0bfcd599 | 125 | fprintf(stderr, "vhost lacks feature mask %" PRIu64 " for backend\n", |
29f91781 | 126 | (uint64_t)(~net->dev.features & net->dev.backend_features)); |
d5970055 MT |
127 | vhost_dev_cleanup(&net->dev); |
128 | goto fail; | |
129 | } | |
130 | ||
131 | /* Set sane init value. Override when guest acks. */ | |
132 | vhost_net_ack_features(net, 0); | |
133 | return net; | |
134 | fail: | |
7267c094 | 135 | g_free(net); |
d5970055 MT |
136 | return NULL; |
137 | } | |
138 | ||
5430a28f MT |
139 | bool vhost_net_query(VHostNetState *net, VirtIODevice *dev) |
140 | { | |
141 | return vhost_dev_query(&net->dev, dev); | |
142 | } | |
143 | ||
a9f98bb5 JW |
144 | static int vhost_net_start_one(struct vhost_net *net, |
145 | VirtIODevice *dev, | |
146 | int vq_index) | |
d5970055 MT |
147 | { |
148 | struct vhost_vring_file file = { }; | |
149 | int r; | |
b0b3db79 | 150 | |
a9f98bb5 JW |
151 | if (net->dev.started) { |
152 | return 0; | |
153 | } | |
154 | ||
155 | net->dev.nvqs = 2; | |
156 | net->dev.vqs = net->vqs; | |
157 | net->dev.vq_index = vq_index; | |
158 | ||
b0b3db79 MT |
159 | r = vhost_dev_enable_notifiers(&net->dev, dev); |
160 | if (r < 0) { | |
161 | goto fail_notifiers; | |
162 | } | |
d5970055 | 163 | |
d5970055 MT |
164 | r = vhost_dev_start(&net->dev, dev); |
165 | if (r < 0) { | |
b0b3db79 | 166 | goto fail_start; |
d5970055 MT |
167 | } |
168 | ||
35277d14 | 169 | net->nc->info->poll(net->nc, false); |
d5970055 MT |
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; | |
177 | } | |
178 | } | |
179 | return 0; | |
180 | fail: | |
181 | file.fd = -1; | |
6b37c87c | 182 | while (file.index-- > 0) { |
d5970055 MT |
183 | int r = ioctl(net->dev.control, VHOST_NET_SET_BACKEND, &file); |
184 | assert(r >= 0); | |
185 | } | |
35277d14 | 186 | net->nc->info->poll(net->nc, true); |
d5970055 | 187 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 MT |
188 | fail_start: |
189 | vhost_dev_disable_notifiers(&net->dev, dev); | |
190 | fail_notifiers: | |
d5970055 MT |
191 | return r; |
192 | } | |
193 | ||
a9f98bb5 JW |
194 | static void vhost_net_stop_one(struct vhost_net *net, |
195 | VirtIODevice *dev) | |
d5970055 MT |
196 | { |
197 | struct vhost_vring_file file = { .fd = -1 }; | |
198 | ||
a9f98bb5 JW |
199 | if (!net->dev.started) { |
200 | return; | |
201 | } | |
202 | ||
d5970055 MT |
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); | |
206 | } | |
35277d14 | 207 | net->nc->info->poll(net->nc, true); |
d5970055 | 208 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 | 209 | vhost_dev_disable_notifiers(&net->dev, dev); |
d5970055 MT |
210 | } |
211 | ||
a9f98bb5 JW |
212 | int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, |
213 | int total_queues) | |
214 | { | |
1c819449 FK |
215 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
216 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
217 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
a9f98bb5 JW |
218 | int r, i = 0; |
219 | ||
1c819449 | 220 | if (!k->set_guest_notifiers) { |
312fd5f2 | 221 | error_report("binding does not support guest notifiers"); |
a9f98bb5 JW |
222 | r = -ENOSYS; |
223 | goto err; | |
224 | } | |
225 | ||
226 | for (i = 0; i < total_queues; i++) { | |
227 | r = vhost_net_start_one(tap_get_vhost_net(ncs[i].peer), dev, i * 2); | |
228 | ||
229 | if (r < 0) { | |
230 | goto err; | |
231 | } | |
232 | } | |
233 | ||
1c819449 | 234 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); |
a9f98bb5 | 235 | if (r < 0) { |
312fd5f2 | 236 | error_report("Error binding guest notifier: %d", -r); |
a9f98bb5 JW |
237 | goto err; |
238 | } | |
239 | ||
240 | return 0; | |
241 | ||
242 | err: | |
243 | while (--i >= 0) { | |
244 | vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev); | |
245 | } | |
246 | return r; | |
247 | } | |
248 | ||
249 | void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, | |
250 | int total_queues) | |
251 | { | |
1c819449 FK |
252 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
253 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
254 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
a9f98bb5 JW |
255 | int i, r; |
256 | ||
1c819449 | 257 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); |
a9f98bb5 JW |
258 | if (r < 0) { |
259 | fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); | |
260 | fflush(stderr); | |
261 | } | |
262 | assert(r >= 0); | |
263 | ||
264 | for (i = 0; i < total_queues; i++) { | |
265 | vhost_net_stop_one(tap_get_vhost_net(ncs[i].peer), dev); | |
266 | } | |
267 | } | |
268 | ||
d5970055 MT |
269 | void vhost_net_cleanup(struct vhost_net *net) |
270 | { | |
271 | vhost_dev_cleanup(&net->dev); | |
7267c094 | 272 | g_free(net); |
d5970055 | 273 | } |
f56a1247 MT |
274 | |
275 | bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) | |
276 | { | |
277 | return vhost_virtqueue_pending(&net->dev, idx); | |
278 | } | |
279 | ||
280 | void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, | |
281 | int idx, bool mask) | |
282 | { | |
283 | vhost_virtqueue_mask(&net->dev, dev, idx, mask); | |
284 | } | |
d5970055 | 285 | #else |
4e68f7a0 | 286 | struct vhost_net *vhost_net_init(NetClientState *backend, int devfd, |
5430a28f MT |
287 | bool force) |
288 | { | |
35f75462 | 289 | error_report("vhost-net support is not compiled in"); |
5430a28f MT |
290 | return NULL; |
291 | } | |
292 | ||
293 | bool vhost_net_query(VHostNetState *net, VirtIODevice *dev) | |
d5970055 | 294 | { |
5430a28f | 295 | return false; |
d5970055 MT |
296 | } |
297 | ||
a9f98bb5 JW |
298 | int vhost_net_start(VirtIODevice *dev, |
299 | NetClientState *ncs, | |
300 | int total_queues) | |
d5970055 | 301 | { |
5430a28f | 302 | return -ENOSYS; |
d5970055 | 303 | } |
a9f98bb5 JW |
304 | void vhost_net_stop(VirtIODevice *dev, |
305 | NetClientState *ncs, | |
306 | int total_queues) | |
d5970055 MT |
307 | { |
308 | } | |
309 | ||
310 | void vhost_net_cleanup(struct vhost_net *net) | |
311 | { | |
312 | } | |
313 | ||
314 | unsigned vhost_net_get_features(struct vhost_net *net, unsigned features) | |
315 | { | |
5430a28f | 316 | return features; |
d5970055 MT |
317 | } |
318 | void vhost_net_ack_features(struct vhost_net *net, unsigned features) | |
319 | { | |
320 | } | |
f56a1247 MT |
321 | |
322 | bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) | |
323 | { | |
324 | return -ENOSYS; | |
325 | } | |
326 | ||
327 | void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, | |
328 | int idx, bool mask) | |
329 | { | |
330 | } | |
d5970055 | 331 | #endif |