]>
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 | ||
e8d40465 | 16 | #include "qemu/osdep.h" |
1422e32d | 17 | #include "net/net.h" |
d5970055 | 18 | #include "net/tap.h" |
03ce5744 | 19 | #include "net/vhost-user.h" |
d5970055 | 20 | |
0d09e41a PB |
21 | #include "hw/virtio/virtio-net.h" |
22 | #include "net/vhost_net.h" | |
1de7afc9 | 23 | #include "qemu/error-report.h" |
d5970055 | 24 | |
d5970055 MT |
25 | |
26 | #ifdef CONFIG_VHOST_NET | |
27 | #include <linux/vhost.h> | |
d5970055 MT |
28 | #include <sys/socket.h> |
29 | #include <linux/kvm.h> | |
d5970055 MT |
30 | #include <netpacket/packet.h> |
31 | #include <net/ethernet.h> | |
32 | #include <net/if.h> | |
33 | #include <netinet/in.h> | |
34 | ||
d5970055 | 35 | |
4fbe0f32 | 36 | #include "standard-headers/linux/virtio_ring.h" |
0d09e41a | 37 | #include "hw/virtio/vhost.h" |
1c819449 | 38 | #include "hw/virtio/virtio-bus.h" |
d5970055 MT |
39 | |
40 | struct vhost_net { | |
41 | struct vhost_dev dev; | |
42 | struct vhost_virtqueue vqs[2]; | |
43 | int backend; | |
35277d14 | 44 | NetClientState *nc; |
d5970055 MT |
45 | }; |
46 | ||
2e6d46d7 NN |
47 | /* Features supported by host kernel. */ |
48 | static const int kernel_feature_bits[] = { | |
49 | VIRTIO_F_NOTIFY_ON_EMPTY, | |
50 | VIRTIO_RING_F_INDIRECT_DESC, | |
51 | VIRTIO_RING_F_EVENT_IDX, | |
52 | VIRTIO_NET_F_MRG_RXBUF, | |
b1506132 | 53 | VIRTIO_F_VERSION_1, |
2e6d46d7 NN |
54 | VHOST_INVALID_FEATURE_BIT |
55 | }; | |
56 | ||
5f4c01ca | 57 | /* Features supported by others. */ |
d122f1a2 | 58 | static const int user_feature_bits[] = { |
5f4c01ca NN |
59 | VIRTIO_F_NOTIFY_ON_EMPTY, |
60 | VIRTIO_RING_F_INDIRECT_DESC, | |
61 | VIRTIO_RING_F_EVENT_IDX, | |
62 | ||
63 | VIRTIO_F_ANY_LAYOUT, | |
b1506132 | 64 | VIRTIO_F_VERSION_1, |
5f4c01ca NN |
65 | VIRTIO_NET_F_CSUM, |
66 | VIRTIO_NET_F_GUEST_CSUM, | |
67 | VIRTIO_NET_F_GSO, | |
68 | VIRTIO_NET_F_GUEST_TSO4, | |
69 | VIRTIO_NET_F_GUEST_TSO6, | |
70 | VIRTIO_NET_F_GUEST_ECN, | |
71 | VIRTIO_NET_F_GUEST_UFO, | |
72 | VIRTIO_NET_F_HOST_TSO4, | |
73 | VIRTIO_NET_F_HOST_TSO6, | |
74 | VIRTIO_NET_F_HOST_ECN, | |
75 | VIRTIO_NET_F_HOST_UFO, | |
76 | VIRTIO_NET_F_MRG_RXBUF, | |
5f4c01ca | 77 | |
72018d1e | 78 | /* This bit implies RARP isn't sent by QEMU out of band */ |
f6f56291 TC |
79 | VIRTIO_NET_F_GUEST_ANNOUNCE, |
80 | ||
5f4c01ca NN |
81 | VIRTIO_NET_F_MQ, |
82 | ||
83 | VHOST_INVALID_FEATURE_BIT | |
84 | }; | |
85 | ||
2e6d46d7 | 86 | static const int *vhost_net_get_feature_bits(struct vhost_net *net) |
d5970055 | 87 | { |
2e6d46d7 NN |
88 | const int *feature_bits = 0; |
89 | ||
90 | switch (net->nc->info->type) { | |
f394b2e2 | 91 | case NET_CLIENT_DRIVER_TAP: |
2e6d46d7 NN |
92 | feature_bits = kernel_feature_bits; |
93 | break; | |
f394b2e2 | 94 | case NET_CLIENT_DRIVER_VHOST_USER: |
5f4c01ca NN |
95 | feature_bits = user_feature_bits; |
96 | break; | |
2e6d46d7 NN |
97 | default: |
98 | error_report("Feature bits not defined for this type: %d", | |
99 | net->nc->info->type); | |
100 | break; | |
ca736c8e | 101 | } |
2e6d46d7 NN |
102 | |
103 | return feature_bits; | |
104 | } | |
105 | ||
9a2ba823 | 106 | uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) |
2e6d46d7 NN |
107 | { |
108 | return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net), | |
109 | features); | |
d5970055 MT |
110 | } |
111 | ||
9a2ba823 | 112 | void vhost_net_ack_features(struct vhost_net *net, uint64_t features) |
d5970055 | 113 | { |
b49ae913 | 114 | net->dev.acked_features = net->dev.backend_features; |
2e6d46d7 | 115 | vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features); |
d5970055 MT |
116 | } |
117 | ||
e2051e9e YL |
118 | uint64_t vhost_net_get_max_queues(VHostNetState *net) |
119 | { | |
120 | return net->dev.max_queues; | |
121 | } | |
122 | ||
a463215b MAL |
123 | uint64_t vhost_net_get_acked_features(VHostNetState *net) |
124 | { | |
125 | return net->dev.acked_features; | |
126 | } | |
127 | ||
4e68f7a0 | 128 | static int vhost_net_get_fd(NetClientState *backend) |
d5970055 MT |
129 | { |
130 | switch (backend->info->type) { | |
f394b2e2 | 131 | case NET_CLIENT_DRIVER_TAP: |
d5970055 MT |
132 | return tap_get_fd(backend); |
133 | default: | |
134 | fprintf(stderr, "vhost-net requires tap backend\n"); | |
135 | return -EBADFD; | |
136 | } | |
137 | } | |
138 | ||
81647a65 | 139 | struct vhost_net *vhost_net_init(VhostNetOptions *options) |
d5970055 MT |
140 | { |
141 | int r; | |
1a1bfac9 | 142 | bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL; |
f1a0365b | 143 | struct vhost_net *net = g_new0(struct vhost_net, 1); |
a463215b | 144 | uint64_t features = 0; |
81647a65 NN |
145 | |
146 | if (!options->net_backend) { | |
147 | fprintf(stderr, "vhost-net requires net backend to be setup\n"); | |
d5970055 MT |
148 | goto fail; |
149 | } | |
b931bfbf | 150 | net->nc = options->net_backend; |
81647a65 | 151 | |
e2051e9e | 152 | net->dev.max_queues = 1; |
b931bfbf CO |
153 | net->dev.nvqs = 2; |
154 | net->dev.vqs = net->vqs; | |
e2051e9e | 155 | |
1a1bfac9 NN |
156 | if (backend_kernel) { |
157 | r = vhost_net_get_fd(options->net_backend); | |
158 | if (r < 0) { | |
159 | goto fail; | |
160 | } | |
161 | net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) | |
9a2ba823 | 162 | ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR); |
1a1bfac9 | 163 | net->backend = r; |
dcb10c00 | 164 | net->dev.protocol_features = 0; |
1a1bfac9 NN |
165 | } else { |
166 | net->dev.backend_features = 0; | |
dcb10c00 | 167 | net->dev.protocol_features = 0; |
1a1bfac9 | 168 | net->backend = -1; |
d5970055 | 169 | |
b931bfbf CO |
170 | /* vhost-user needs vq_index to initiate a specific queue pair */ |
171 | net->dev.vq_index = net->nc->queue_index * net->dev.nvqs; | |
172 | } | |
f56a1247 | 173 | |
81647a65 | 174 | r = vhost_dev_init(&net->dev, options->opaque, |
69e87b32 | 175 | options->backend_type, options->busyloop_timeout); |
d5970055 MT |
176 | if (r < 0) { |
177 | goto fail; | |
178 | } | |
1a1bfac9 | 179 | if (backend_kernel) { |
d8e80ae3 DM |
180 | if (!qemu_has_vnet_hdr_len(options->net_backend, |
181 | sizeof(struct virtio_net_hdr_mrg_rxbuf))) { | |
9a2ba823 | 182 | net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF); |
d8e80ae3 | 183 | } |
1a1bfac9 NN |
184 | if (~net->dev.features & net->dev.backend_features) { |
185 | fprintf(stderr, "vhost lacks feature mask %" PRIu64 | |
186 | " for backend\n", | |
187 | (uint64_t)(~net->dev.features & net->dev.backend_features)); | |
1a1bfac9 NN |
188 | goto fail; |
189 | } | |
d5970055 | 190 | } |
a463215b | 191 | |
d5970055 | 192 | /* Set sane init value. Override when guest acks. */ |
f394b2e2 | 193 | if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { |
a463215b MAL |
194 | features = vhost_user_get_acked_features(net->nc); |
195 | if (~net->dev.features & features) { | |
196 | fprintf(stderr, "vhost lacks feature mask %" PRIu64 | |
197 | " for backend\n", | |
198 | (uint64_t)(~net->dev.features & features)); | |
a463215b MAL |
199 | goto fail; |
200 | } | |
201 | } | |
202 | ||
203 | vhost_net_ack_features(net, features); | |
204 | ||
d5970055 | 205 | return net; |
f1a0365b | 206 | |
d5970055 | 207 | fail: |
f1a0365b | 208 | vhost_dev_cleanup(&net->dev); |
7267c094 | 209 | g_free(net); |
d5970055 MT |
210 | return NULL; |
211 | } | |
212 | ||
cd7d1d26 JW |
213 | static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index) |
214 | { | |
215 | net->dev.vq_index = vq_index; | |
216 | } | |
217 | ||
a9f98bb5 | 218 | static int vhost_net_start_one(struct vhost_net *net, |
cd7d1d26 | 219 | VirtIODevice *dev) |
d5970055 MT |
220 | { |
221 | struct vhost_vring_file file = { }; | |
222 | int r; | |
b0b3db79 | 223 | |
a9f98bb5 JW |
224 | net->dev.nvqs = 2; |
225 | net->dev.vqs = net->vqs; | |
a9f98bb5 | 226 | |
b0b3db79 MT |
227 | r = vhost_dev_enable_notifiers(&net->dev, dev); |
228 | if (r < 0) { | |
229 | goto fail_notifiers; | |
230 | } | |
d5970055 | 231 | |
d5970055 MT |
232 | r = vhost_dev_start(&net->dev, dev); |
233 | if (r < 0) { | |
b0b3db79 | 234 | goto fail_start; |
d5970055 MT |
235 | } |
236 | ||
212d69f2 NN |
237 | if (net->nc->info->poll) { |
238 | net->nc->info->poll(net->nc, false); | |
239 | } | |
240 | ||
f394b2e2 | 241 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 NN |
242 | qemu_set_fd_handler(net->backend, NULL, NULL, NULL); |
243 | file.fd = net->backend; | |
244 | for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { | |
950d94ba | 245 | r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
246 | if (r < 0) { |
247 | r = -errno; | |
248 | goto fail; | |
249 | } | |
d5970055 MT |
250 | } |
251 | } | |
252 | return 0; | |
253 | fail: | |
254 | file.fd = -1; | |
f394b2e2 | 255 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 | 256 | while (file.index-- > 0) { |
950d94ba | 257 | int r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
258 | assert(r >= 0); |
259 | } | |
d5970055 | 260 | } |
212d69f2 NN |
261 | if (net->nc->info->poll) { |
262 | net->nc->info->poll(net->nc, true); | |
263 | } | |
d5970055 | 264 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 MT |
265 | fail_start: |
266 | vhost_dev_disable_notifiers(&net->dev, dev); | |
267 | fail_notifiers: | |
d5970055 MT |
268 | return r; |
269 | } | |
270 | ||
a9f98bb5 JW |
271 | static void vhost_net_stop_one(struct vhost_net *net, |
272 | VirtIODevice *dev) | |
d5970055 MT |
273 | { |
274 | struct vhost_vring_file file = { .fd = -1 }; | |
275 | ||
f394b2e2 | 276 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 | 277 | for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { |
950d94ba | 278 | int r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
279 | assert(r >= 0); |
280 | } | |
d5970055 | 281 | } |
212d69f2 NN |
282 | if (net->nc->info->poll) { |
283 | net->nc->info->poll(net->nc, true); | |
284 | } | |
d5970055 | 285 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 | 286 | vhost_dev_disable_notifiers(&net->dev, dev); |
d5970055 MT |
287 | } |
288 | ||
a9f98bb5 JW |
289 | int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, |
290 | int total_queues) | |
291 | { | |
1c819449 FK |
292 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
293 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
294 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
3154d1e4 | 295 | int r, e, i; |
a9f98bb5 | 296 | |
1c819449 | 297 | if (!k->set_guest_notifiers) { |
312fd5f2 | 298 | error_report("binding does not support guest notifiers"); |
a4076440 | 299 | return -ENOSYS; |
a9f98bb5 JW |
300 | } |
301 | ||
3154d1e4 | 302 | for (i = 0; i < total_queues; i++) { |
5669655a VK |
303 | struct vhost_net *net; |
304 | ||
305 | net = get_vhost_net(ncs[i].peer); | |
306 | vhost_net_set_vq_index(net, i * 2); | |
307 | ||
308 | /* Suppress the masking guest notifiers on vhost user | |
309 | * because vhost user doesn't interrupt masking/unmasking | |
310 | * properly. | |
311 | */ | |
f394b2e2 | 312 | if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { |
01edc230 | 313 | dev->use_guest_notifier_mask = false; |
5669655a VK |
314 | } |
315 | } | |
a9f98bb5 | 316 | |
1c819449 | 317 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); |
a9f98bb5 | 318 | if (r < 0) { |
312fd5f2 | 319 | error_report("Error binding guest notifier: %d", -r); |
3154d1e4 | 320 | goto err; |
a9f98bb5 JW |
321 | } |
322 | ||
cd7d1d26 JW |
323 | for (i = 0; i < total_queues; i++) { |
324 | r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev); | |
325 | ||
326 | if (r < 0) { | |
327 | goto err_start; | |
328 | } | |
bfc6cf31 MAL |
329 | |
330 | if (ncs[i].peer->vring_enable) { | |
331 | /* restore vring enable state */ | |
332 | r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable); | |
333 | ||
334 | if (r < 0) { | |
335 | goto err_start; | |
336 | } | |
337 | } | |
cd7d1d26 JW |
338 | } |
339 | ||
a9f98bb5 JW |
340 | return 0; |
341 | ||
cd7d1d26 | 342 | err_start: |
a9f98bb5 | 343 | while (--i >= 0) { |
ed8b4afe | 344 | vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); |
a9f98bb5 | 345 | } |
cd7d1d26 JW |
346 | e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); |
347 | if (e < 0) { | |
348 | fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); | |
349 | fflush(stderr); | |
350 | } | |
3154d1e4 | 351 | err: |
a9f98bb5 JW |
352 | return r; |
353 | } | |
354 | ||
355 | void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, | |
356 | int total_queues) | |
357 | { | |
1c819449 FK |
358 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
359 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
360 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
a9f98bb5 JW |
361 | int i, r; |
362 | ||
cd7d1d26 JW |
363 | for (i = 0; i < total_queues; i++) { |
364 | vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); | |
365 | } | |
366 | ||
1c819449 | 367 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); |
a9f98bb5 JW |
368 | if (r < 0) { |
369 | fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); | |
370 | fflush(stderr); | |
371 | } | |
372 | assert(r >= 0); | |
a9f98bb5 JW |
373 | } |
374 | ||
d5970055 MT |
375 | void vhost_net_cleanup(struct vhost_net *net) |
376 | { | |
377 | vhost_dev_cleanup(&net->dev); | |
d5970055 | 378 | } |
f56a1247 | 379 | |
3e866365 TC |
380 | int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr) |
381 | { | |
382 | const VhostOps *vhost_ops = net->dev.vhost_ops; | |
3e866365 | 383 | |
51f7aca9 MAL |
384 | assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
385 | assert(vhost_ops->vhost_migration_done); | |
3e866365 | 386 | |
51f7aca9 | 387 | return vhost_ops->vhost_migration_done(&net->dev, mac_addr); |
3e866365 TC |
388 | } |
389 | ||
f56a1247 MT |
390 | bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) |
391 | { | |
392 | return vhost_virtqueue_pending(&net->dev, idx); | |
393 | } | |
394 | ||
395 | void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, | |
396 | int idx, bool mask) | |
397 | { | |
398 | vhost_virtqueue_mask(&net->dev, dev, idx, mask); | |
399 | } | |
ed8b4afe NN |
400 | |
401 | VHostNetState *get_vhost_net(NetClientState *nc) | |
402 | { | |
403 | VHostNetState *vhost_net = 0; | |
404 | ||
405 | if (!nc) { | |
406 | return 0; | |
407 | } | |
408 | ||
409 | switch (nc->info->type) { | |
f394b2e2 | 410 | case NET_CLIENT_DRIVER_TAP: |
ed8b4afe NN |
411 | vhost_net = tap_get_vhost_net(nc); |
412 | break; | |
f394b2e2 | 413 | case NET_CLIENT_DRIVER_VHOST_USER: |
03ce5744 | 414 | vhost_net = vhost_user_get_vhost_net(nc); |
1a5b68ce | 415 | assert(vhost_net); |
03ce5744 | 416 | break; |
ed8b4afe NN |
417 | default: |
418 | break; | |
419 | } | |
420 | ||
421 | return vhost_net; | |
422 | } | |
7263a0ad CO |
423 | |
424 | int vhost_set_vring_enable(NetClientState *nc, int enable) | |
425 | { | |
426 | VHostNetState *net = get_vhost_net(nc); | |
bb12e761 | 427 | const VhostOps *vhost_ops = net->dev.vhost_ops; |
72b65f92 | 428 | |
bfc6cf31 MAL |
429 | nc->vring_enable = enable; |
430 | ||
21e70425 MAL |
431 | if (vhost_ops->vhost_set_vring_enable) { |
432 | return vhost_ops->vhost_set_vring_enable(&net->dev, enable); | |
7263a0ad CO |
433 | } |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
d5970055 | 438 | #else |
e2051e9e YL |
439 | uint64_t vhost_net_get_max_queues(VHostNetState *net) |
440 | { | |
441 | return 1; | |
442 | } | |
443 | ||
81647a65 | 444 | struct vhost_net *vhost_net_init(VhostNetOptions *options) |
5430a28f | 445 | { |
35f75462 | 446 | error_report("vhost-net support is not compiled in"); |
5430a28f MT |
447 | return NULL; |
448 | } | |
449 | ||
a9f98bb5 JW |
450 | int vhost_net_start(VirtIODevice *dev, |
451 | NetClientState *ncs, | |
452 | int total_queues) | |
d5970055 | 453 | { |
5430a28f | 454 | return -ENOSYS; |
d5970055 | 455 | } |
a9f98bb5 JW |
456 | void vhost_net_stop(VirtIODevice *dev, |
457 | NetClientState *ncs, | |
458 | int total_queues) | |
d5970055 MT |
459 | { |
460 | } | |
461 | ||
462 | void vhost_net_cleanup(struct vhost_net *net) | |
463 | { | |
464 | } | |
465 | ||
9a2ba823 | 466 | uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) |
d5970055 | 467 | { |
5430a28f | 468 | return features; |
d5970055 | 469 | } |
a463215b | 470 | |
9a2ba823 | 471 | void vhost_net_ack_features(struct vhost_net *net, uint64_t features) |
d5970055 MT |
472 | { |
473 | } | |
f56a1247 | 474 | |
a463215b MAL |
475 | uint64_t vhost_net_get_acked_features(VHostNetState *net) |
476 | { | |
477 | return 0; | |
478 | } | |
479 | ||
f56a1247 MT |
480 | bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) |
481 | { | |
4dd72e04 | 482 | return false; |
f56a1247 MT |
483 | } |
484 | ||
485 | void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, | |
486 | int idx, bool mask) | |
487 | { | |
488 | } | |
ed8b4afe | 489 | |
3e866365 TC |
490 | int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr) |
491 | { | |
492 | return -1; | |
493 | } | |
494 | ||
ed8b4afe NN |
495 | VHostNetState *get_vhost_net(NetClientState *nc) |
496 | { | |
497 | return 0; | |
498 | } | |
7263a0ad CO |
499 | |
500 | int vhost_set_vring_enable(NetClientState *nc, int enable) | |
501 | { | |
502 | return 0; | |
503 | } | |
d5970055 | 504 | #endif |