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