]>
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 | |
18658a3c | 21 | #include "standard-headers/linux/vhost_types.h" |
0d09e41a PB |
22 | #include "hw/virtio/virtio-net.h" |
23 | #include "net/vhost_net.h" | |
1de7afc9 | 24 | #include "qemu/error-report.h" |
db725815 | 25 | #include "qemu/main-loop.h" |
d5970055 | 26 | |
d5970055 | 27 | #include <sys/socket.h> |
d5970055 MT |
28 | #include <net/if.h> |
29 | #include <netinet/in.h> | |
30 | ||
d5970055 | 31 | |
4fbe0f32 | 32 | #include "standard-headers/linux/virtio_ring.h" |
0d09e41a | 33 | #include "hw/virtio/vhost.h" |
1c819449 | 34 | #include "hw/virtio/virtio-bus.h" |
d5970055 MT |
35 | |
36 | struct vhost_net { | |
37 | struct vhost_dev dev; | |
38 | struct vhost_virtqueue vqs[2]; | |
39 | int backend; | |
35277d14 | 40 | NetClientState *nc; |
d5970055 MT |
41 | }; |
42 | ||
2e6d46d7 NN |
43 | /* Features supported by host kernel. */ |
44 | static const int kernel_feature_bits[] = { | |
45 | VIRTIO_F_NOTIFY_ON_EMPTY, | |
46 | VIRTIO_RING_F_INDIRECT_DESC, | |
47 | VIRTIO_RING_F_EVENT_IDX, | |
48 | VIRTIO_NET_F_MRG_RXBUF, | |
b1506132 | 49 | VIRTIO_F_VERSION_1, |
45a368ad | 50 | VIRTIO_NET_F_MTU, |
c471ad0e | 51 | VIRTIO_F_IOMMU_PLATFORM, |
2e6d46d7 NN |
52 | VHOST_INVALID_FEATURE_BIT |
53 | }; | |
54 | ||
5f4c01ca | 55 | /* Features supported by others. */ |
d122f1a2 | 56 | static const int user_feature_bits[] = { |
5f4c01ca NN |
57 | VIRTIO_F_NOTIFY_ON_EMPTY, |
58 | VIRTIO_RING_F_INDIRECT_DESC, | |
59 | VIRTIO_RING_F_EVENT_IDX, | |
60 | ||
61 | VIRTIO_F_ANY_LAYOUT, | |
b1506132 | 62 | VIRTIO_F_VERSION_1, |
5f4c01ca NN |
63 | VIRTIO_NET_F_CSUM, |
64 | VIRTIO_NET_F_GUEST_CSUM, | |
65 | VIRTIO_NET_F_GSO, | |
66 | VIRTIO_NET_F_GUEST_TSO4, | |
67 | VIRTIO_NET_F_GUEST_TSO6, | |
68 | VIRTIO_NET_F_GUEST_ECN, | |
69 | VIRTIO_NET_F_GUEST_UFO, | |
70 | VIRTIO_NET_F_HOST_TSO4, | |
71 | VIRTIO_NET_F_HOST_TSO6, | |
72 | VIRTIO_NET_F_HOST_ECN, | |
73 | VIRTIO_NET_F_HOST_UFO, | |
74 | VIRTIO_NET_F_MRG_RXBUF, | |
45a368ad | 75 | VIRTIO_NET_F_MTU, |
6dcdd06e | 76 | VIRTIO_F_IOMMU_PLATFORM, |
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"); | |
af3bba76 | 135 | return -ENOSYS; |
d5970055 MT |
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. */ |
56f41de7 | 193 | #ifdef CONFIG_VHOST_NET_USER |
f394b2e2 | 194 | if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { |
a463215b MAL |
195 | features = vhost_user_get_acked_features(net->nc); |
196 | if (~net->dev.features & features) { | |
197 | fprintf(stderr, "vhost lacks feature mask %" PRIu64 | |
198 | " for backend\n", | |
199 | (uint64_t)(~net->dev.features & features)); | |
a463215b MAL |
200 | goto fail; |
201 | } | |
202 | } | |
56f41de7 | 203 | #endif |
a463215b MAL |
204 | |
205 | vhost_net_ack_features(net, features); | |
206 | ||
d5970055 | 207 | return net; |
f1a0365b | 208 | |
d5970055 | 209 | fail: |
f1a0365b | 210 | vhost_dev_cleanup(&net->dev); |
7267c094 | 211 | g_free(net); |
d5970055 MT |
212 | return NULL; |
213 | } | |
214 | ||
cd7d1d26 JW |
215 | static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index) |
216 | { | |
217 | net->dev.vq_index = vq_index; | |
218 | } | |
219 | ||
a9f98bb5 | 220 | static int vhost_net_start_one(struct vhost_net *net, |
cd7d1d26 | 221 | VirtIODevice *dev) |
d5970055 MT |
222 | { |
223 | struct vhost_vring_file file = { }; | |
224 | int r; | |
b0b3db79 | 225 | |
a9f98bb5 JW |
226 | net->dev.nvqs = 2; |
227 | net->dev.vqs = net->vqs; | |
a9f98bb5 | 228 | |
b0b3db79 MT |
229 | r = vhost_dev_enable_notifiers(&net->dev, dev); |
230 | if (r < 0) { | |
231 | goto fail_notifiers; | |
232 | } | |
d5970055 | 233 | |
d5970055 MT |
234 | r = vhost_dev_start(&net->dev, dev); |
235 | if (r < 0) { | |
b0b3db79 | 236 | goto fail_start; |
d5970055 MT |
237 | } |
238 | ||
212d69f2 NN |
239 | if (net->nc->info->poll) { |
240 | net->nc->info->poll(net->nc, false); | |
241 | } | |
242 | ||
f394b2e2 | 243 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 NN |
244 | qemu_set_fd_handler(net->backend, NULL, NULL, NULL); |
245 | file.fd = net->backend; | |
246 | for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { | |
23bfaf77 JW |
247 | if (!virtio_queue_enabled(dev, net->dev.vq_index + |
248 | file.index)) { | |
249 | /* Queue might not be ready for start */ | |
250 | continue; | |
251 | } | |
950d94ba | 252 | r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
253 | if (r < 0) { |
254 | r = -errno; | |
255 | goto fail; | |
256 | } | |
d5970055 MT |
257 | } |
258 | } | |
259 | return 0; | |
260 | fail: | |
261 | file.fd = -1; | |
f394b2e2 | 262 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 | 263 | while (file.index-- > 0) { |
23bfaf77 JW |
264 | if (!virtio_queue_enabled(dev, net->dev.vq_index + |
265 | file.index)) { | |
266 | /* Queue might not be ready for start */ | |
267 | continue; | |
268 | } | |
950d94ba | 269 | int r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
270 | assert(r >= 0); |
271 | } | |
d5970055 | 272 | } |
212d69f2 NN |
273 | if (net->nc->info->poll) { |
274 | net->nc->info->poll(net->nc, true); | |
275 | } | |
d5970055 | 276 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 MT |
277 | fail_start: |
278 | vhost_dev_disable_notifiers(&net->dev, dev); | |
279 | fail_notifiers: | |
d5970055 MT |
280 | return r; |
281 | } | |
282 | ||
a9f98bb5 JW |
283 | static void vhost_net_stop_one(struct vhost_net *net, |
284 | VirtIODevice *dev) | |
d5970055 MT |
285 | { |
286 | struct vhost_vring_file file = { .fd = -1 }; | |
287 | ||
f394b2e2 | 288 | if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { |
1a1bfac9 | 289 | for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { |
950d94ba | 290 | int r = vhost_net_set_backend(&net->dev, &file); |
1a1bfac9 NN |
291 | assert(r >= 0); |
292 | } | |
d5970055 | 293 | } |
212d69f2 NN |
294 | if (net->nc->info->poll) { |
295 | net->nc->info->poll(net->nc, true); | |
296 | } | |
d5970055 | 297 | vhost_dev_stop(&net->dev, dev); |
b0b3db79 | 298 | vhost_dev_disable_notifiers(&net->dev, dev); |
d5970055 MT |
299 | } |
300 | ||
a9f98bb5 JW |
301 | int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, |
302 | int total_queues) | |
303 | { | |
1c819449 FK |
304 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
305 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
306 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
3154d1e4 | 307 | int r, e, i; |
a9f98bb5 | 308 | |
1c819449 | 309 | if (!k->set_guest_notifiers) { |
312fd5f2 | 310 | error_report("binding does not support guest notifiers"); |
a4076440 | 311 | return -ENOSYS; |
a9f98bb5 JW |
312 | } |
313 | ||
3154d1e4 | 314 | for (i = 0; i < total_queues; i++) { |
5669655a VK |
315 | struct vhost_net *net; |
316 | ||
317 | net = get_vhost_net(ncs[i].peer); | |
318 | vhost_net_set_vq_index(net, i * 2); | |
319 | ||
320 | /* Suppress the masking guest notifiers on vhost user | |
321 | * because vhost user doesn't interrupt masking/unmasking | |
322 | * properly. | |
323 | */ | |
f394b2e2 | 324 | if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { |
01edc230 | 325 | dev->use_guest_notifier_mask = false; |
5669655a VK |
326 | } |
327 | } | |
a9f98bb5 | 328 | |
1c819449 | 329 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); |
a9f98bb5 | 330 | if (r < 0) { |
312fd5f2 | 331 | error_report("Error binding guest notifier: %d", -r); |
3154d1e4 | 332 | goto err; |
a9f98bb5 JW |
333 | } |
334 | ||
cd7d1d26 JW |
335 | for (i = 0; i < total_queues; i++) { |
336 | r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev); | |
337 | ||
338 | if (r < 0) { | |
339 | goto err_start; | |
340 | } | |
bfc6cf31 MAL |
341 | |
342 | if (ncs[i].peer->vring_enable) { | |
343 | /* restore vring enable state */ | |
344 | r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable); | |
345 | ||
346 | if (r < 0) { | |
347 | goto err_start; | |
348 | } | |
349 | } | |
cd7d1d26 JW |
350 | } |
351 | ||
a9f98bb5 JW |
352 | return 0; |
353 | ||
cd7d1d26 | 354 | err_start: |
a9f98bb5 | 355 | while (--i >= 0) { |
ed8b4afe | 356 | vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); |
a9f98bb5 | 357 | } |
cd7d1d26 JW |
358 | e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); |
359 | if (e < 0) { | |
360 | fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); | |
361 | fflush(stderr); | |
362 | } | |
3154d1e4 | 363 | err: |
a9f98bb5 JW |
364 | return r; |
365 | } | |
366 | ||
367 | void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, | |
368 | int total_queues) | |
369 | { | |
1c819449 FK |
370 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); |
371 | VirtioBusState *vbus = VIRTIO_BUS(qbus); | |
372 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); | |
a9f98bb5 JW |
373 | int i, r; |
374 | ||
cd7d1d26 JW |
375 | for (i = 0; i < total_queues; i++) { |
376 | vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); | |
377 | } | |
378 | ||
1c819449 | 379 | r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); |
a9f98bb5 JW |
380 | if (r < 0) { |
381 | fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); | |
382 | fflush(stderr); | |
383 | } | |
384 | assert(r >= 0); | |
a9f98bb5 JW |
385 | } |
386 | ||
d5970055 MT |
387 | void vhost_net_cleanup(struct vhost_net *net) |
388 | { | |
389 | vhost_dev_cleanup(&net->dev); | |
d5970055 | 390 | } |
f56a1247 | 391 | |
3e866365 TC |
392 | int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr) |
393 | { | |
394 | const VhostOps *vhost_ops = net->dev.vhost_ops; | |
3e866365 | 395 | |
51f7aca9 MAL |
396 | assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); |
397 | assert(vhost_ops->vhost_migration_done); | |
3e866365 | 398 | |
51f7aca9 | 399 | return vhost_ops->vhost_migration_done(&net->dev, mac_addr); |
3e866365 TC |
400 | } |
401 | ||
f56a1247 MT |
402 | bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) |
403 | { | |
404 | return vhost_virtqueue_pending(&net->dev, idx); | |
405 | } | |
406 | ||
407 | void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, | |
408 | int idx, bool mask) | |
409 | { | |
410 | vhost_virtqueue_mask(&net->dev, dev, idx, mask); | |
411 | } | |
ed8b4afe NN |
412 | |
413 | VHostNetState *get_vhost_net(NetClientState *nc) | |
414 | { | |
415 | VHostNetState *vhost_net = 0; | |
416 | ||
417 | if (!nc) { | |
418 | return 0; | |
419 | } | |
420 | ||
421 | switch (nc->info->type) { | |
f394b2e2 | 422 | case NET_CLIENT_DRIVER_TAP: |
ed8b4afe NN |
423 | vhost_net = tap_get_vhost_net(nc); |
424 | break; | |
56f41de7 | 425 | #ifdef CONFIG_VHOST_NET_USER |
f394b2e2 | 426 | case NET_CLIENT_DRIVER_VHOST_USER: |
03ce5744 | 427 | vhost_net = vhost_user_get_vhost_net(nc); |
1a5b68ce | 428 | assert(vhost_net); |
03ce5744 | 429 | break; |
56f41de7 | 430 | #endif |
ed8b4afe NN |
431 | default: |
432 | break; | |
433 | } | |
434 | ||
435 | return vhost_net; | |
436 | } | |
7263a0ad CO |
437 | |
438 | int vhost_set_vring_enable(NetClientState *nc, int enable) | |
439 | { | |
440 | VHostNetState *net = get_vhost_net(nc); | |
bb12e761 | 441 | const VhostOps *vhost_ops = net->dev.vhost_ops; |
72b65f92 | 442 | |
bfc6cf31 MAL |
443 | nc->vring_enable = enable; |
444 | ||
ca10203c | 445 | if (vhost_ops && vhost_ops->vhost_set_vring_enable) { |
21e70425 | 446 | return vhost_ops->vhost_set_vring_enable(&net->dev, enable); |
7263a0ad CO |
447 | } |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
45a368ad MC |
452 | int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu) |
453 | { | |
454 | const VhostOps *vhost_ops = net->dev.vhost_ops; | |
455 | ||
456 | if (!vhost_ops->vhost_net_set_mtu) { | |
457 | return 0; | |
458 | } | |
459 | ||
460 | return vhost_ops->vhost_net_set_mtu(&net->dev, mtu); | |
461 | } |