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