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