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