4 * Copyright Red Hat, Inc. 2010
7 * Michael S. Tsirkin <mst@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
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.
16 #include "qemu/osdep.h"
19 #include "net/vhost-user.h"
20 #include "net/vhost-vdpa.h"
22 #include "standard-headers/linux/vhost_types.h"
23 #include "hw/virtio/virtio-net.h"
24 #include "net/vhost_net.h"
25 #include "qapi/error.h"
26 #include "qemu/error-report.h"
27 #include "qemu/main-loop.h"
29 #include <sys/socket.h>
31 #include <netinet/in.h>
34 #include "standard-headers/linux/virtio_ring.h"
35 #include "hw/virtio/vhost.h"
36 #include "hw/virtio/virtio-bus.h"
37 #include "linux-headers/linux/vhost.h"
40 /* Features supported by host kernel. */
41 static const int kernel_feature_bits
[] = {
42 VIRTIO_F_NOTIFY_ON_EMPTY
,
43 VIRTIO_RING_F_INDIRECT_DESC
,
44 VIRTIO_RING_F_EVENT_IDX
,
45 VIRTIO_NET_F_MRG_RXBUF
,
48 VIRTIO_F_IOMMU_PLATFORM
,
51 VIRTIO_F_NOTIFICATION_DATA
,
52 VIRTIO_NET_F_HASH_REPORT
,
53 VHOST_INVALID_FEATURE_BIT
56 /* Features supported by others. */
57 static const int user_feature_bits
[] = {
58 VIRTIO_F_NOTIFY_ON_EMPTY
,
59 VIRTIO_F_NOTIFICATION_DATA
,
60 VIRTIO_RING_F_INDIRECT_DESC
,
61 VIRTIO_RING_F_EVENT_IDX
,
66 VIRTIO_NET_F_GUEST_CSUM
,
68 VIRTIO_NET_F_GUEST_TSO4
,
69 VIRTIO_NET_F_GUEST_TSO6
,
70 VIRTIO_NET_F_GUEST_ECN
,
71 VIRTIO_NET_F_GUEST_UFO
,
72 VIRTIO_NET_F_HOST_TSO4
,
73 VIRTIO_NET_F_HOST_TSO6
,
74 VIRTIO_NET_F_HOST_ECN
,
75 VIRTIO_NET_F_HOST_UFO
,
76 VIRTIO_NET_F_MRG_RXBUF
,
78 VIRTIO_F_IOMMU_PLATFORM
,
82 VIRTIO_NET_F_HASH_REPORT
,
83 VIRTIO_NET_F_GUEST_USO4
,
84 VIRTIO_NET_F_GUEST_USO6
,
85 VIRTIO_NET_F_HOST_USO
,
87 /* This bit implies RARP isn't sent by QEMU out of band */
88 VIRTIO_NET_F_GUEST_ANNOUNCE
,
92 VHOST_INVALID_FEATURE_BIT
95 static const int *vhost_net_get_feature_bits(struct vhost_net
*net
)
97 const int *feature_bits
= 0;
99 switch (net
->nc
->info
->type
) {
100 case NET_CLIENT_DRIVER_TAP
:
101 feature_bits
= kernel_feature_bits
;
103 case NET_CLIENT_DRIVER_VHOST_USER
:
104 feature_bits
= user_feature_bits
;
106 #ifdef CONFIG_VHOST_NET_VDPA
107 case NET_CLIENT_DRIVER_VHOST_VDPA
:
108 feature_bits
= vdpa_feature_bits
;
112 error_report("Feature bits not defined for this type: %d",
113 net
->nc
->info
->type
);
120 uint64_t vhost_net_get_features(struct vhost_net
*net
, uint64_t features
)
122 return vhost_get_features(&net
->dev
, vhost_net_get_feature_bits(net
),
125 int vhost_net_get_config(struct vhost_net
*net
, uint8_t *config
,
128 return vhost_dev_get_config(&net
->dev
, config
, config_len
, NULL
);
130 int vhost_net_set_config(struct vhost_net
*net
, const uint8_t *data
,
131 uint32_t offset
, uint32_t size
, uint32_t flags
)
133 return vhost_dev_set_config(&net
->dev
, data
, offset
, size
, flags
);
136 void vhost_net_ack_features(struct vhost_net
*net
, uint64_t features
)
138 net
->dev
.acked_features
= net
->dev
.backend_features
;
139 vhost_ack_features(&net
->dev
, vhost_net_get_feature_bits(net
), features
);
142 uint64_t vhost_net_get_max_queues(VHostNetState
*net
)
144 return net
->dev
.max_queues
;
147 uint64_t vhost_net_get_acked_features(VHostNetState
*net
)
149 return net
->dev
.acked_features
;
152 void vhost_net_save_acked_features(NetClientState
*nc
)
154 #ifdef CONFIG_VHOST_NET_USER
155 if (nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
156 vhost_user_save_acked_features(nc
);
161 static int vhost_net_get_fd(NetClientState
*backend
)
163 switch (backend
->info
->type
) {
164 case NET_CLIENT_DRIVER_TAP
:
165 return tap_get_fd(backend
);
167 fprintf(stderr
, "vhost-net requires tap backend\n");
172 struct vhost_net
*vhost_net_init(VhostNetOptions
*options
)
175 bool backend_kernel
= options
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
;
176 struct vhost_net
*net
= g_new0(struct vhost_net
, 1);
177 uint64_t features
= 0;
178 Error
*local_err
= NULL
;
180 if (!options
->net_backend
) {
181 fprintf(stderr
, "vhost-net requires net backend to be setup\n");
184 net
->nc
= options
->net_backend
;
185 net
->dev
.nvqs
= options
->nvqs
;
187 net
->dev
.max_queues
= 1;
188 net
->dev
.vqs
= net
->vqs
;
190 if (backend_kernel
) {
191 r
= vhost_net_get_fd(options
->net_backend
);
195 net
->dev
.backend_features
= qemu_has_vnet_hdr(options
->net_backend
)
196 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
);
198 net
->dev
.protocol_features
= 0;
200 net
->dev
.backend_features
= 0;
201 net
->dev
.protocol_features
= 0;
204 /* vhost-user needs vq_index to initiate a specific queue pair */
205 net
->dev
.vq_index
= net
->nc
->queue_index
* net
->dev
.nvqs
;
208 r
= vhost_dev_init(&net
->dev
, options
->opaque
,
209 options
->backend_type
, options
->busyloop_timeout
,
212 error_report_err(local_err
);
215 if (backend_kernel
) {
216 if (!qemu_has_vnet_hdr_len(options
->net_backend
,
217 sizeof(struct virtio_net_hdr_mrg_rxbuf
))) {
218 net
->dev
.features
&= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF
);
220 if (~net
->dev
.features
& net
->dev
.backend_features
) {
221 fprintf(stderr
, "vhost lacks feature mask 0x%" PRIx64
223 (uint64_t)(~net
->dev
.features
& net
->dev
.backend_features
));
228 /* Set sane init value. Override when guest acks. */
229 #ifdef CONFIG_VHOST_NET_USER
230 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
231 features
= vhost_user_get_acked_features(net
->nc
);
232 if (~net
->dev
.features
& features
) {
233 fprintf(stderr
, "vhost lacks feature mask 0x%" PRIx64
235 (uint64_t)(~net
->dev
.features
& features
));
241 vhost_net_ack_features(net
, features
);
246 vhost_dev_cleanup(&net
->dev
);
251 static void vhost_net_set_vq_index(struct vhost_net
*net
, int vq_index
,
254 net
->dev
.vq_index
= vq_index
;
255 net
->dev
.vq_index_end
= vq_index_end
;
258 static int vhost_net_start_one(struct vhost_net
*net
,
261 struct vhost_vring_file file
= { };
264 if (net
->nc
->info
->start
) {
265 r
= net
->nc
->info
->start(net
->nc
);
271 r
= vhost_dev_enable_notifiers(&net
->dev
, dev
);
276 r
= vhost_dev_start(&net
->dev
, dev
, false);
281 if (net
->nc
->info
->poll
) {
282 net
->nc
->info
->poll(net
->nc
, false);
285 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
286 qemu_set_fd_handler(net
->backend
, NULL
, NULL
, NULL
);
287 file
.fd
= net
->backend
;
288 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
289 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
291 /* Queue might not be ready for start */
294 r
= vhost_net_set_backend(&net
->dev
, &file
);
302 if (net
->nc
->info
->load
) {
303 r
= net
->nc
->info
->load(net
->nc
);
311 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
312 while (file
.index
-- > 0) {
313 if (!virtio_queue_enabled(dev
, net
->dev
.vq_index
+
315 /* Queue might not be ready for start */
318 int ret
= vhost_net_set_backend(&net
->dev
, &file
);
322 if (net
->nc
->info
->poll
) {
323 net
->nc
->info
->poll(net
->nc
, true);
325 vhost_dev_stop(&net
->dev
, dev
, false);
327 vhost_dev_disable_notifiers(&net
->dev
, dev
);
332 static void vhost_net_stop_one(struct vhost_net
*net
,
335 struct vhost_vring_file file
= { .fd
= -1 };
337 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
338 for (file
.index
= 0; file
.index
< net
->dev
.nvqs
; ++file
.index
) {
339 int r
= vhost_net_set_backend(&net
->dev
, &file
);
343 if (net
->nc
->info
->poll
) {
344 net
->nc
->info
->poll(net
->nc
, true);
346 vhost_dev_stop(&net
->dev
, dev
, false);
347 if (net
->nc
->info
->stop
) {
348 net
->nc
->info
->stop(net
->nc
);
350 vhost_dev_disable_notifiers(&net
->dev
, dev
);
353 int vhost_net_start(VirtIODevice
*dev
, NetClientState
*ncs
,
354 int data_queue_pairs
, int cvq
)
356 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
357 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
358 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
359 int total_notifiers
= data_queue_pairs
* 2 + cvq
;
360 VirtIONet
*n
= VIRTIO_NET(dev
);
361 int nvhosts
= data_queue_pairs
+ cvq
;
362 struct vhost_net
*net
;
363 int r
, e
, i
, index_end
= data_queue_pairs
* 2;
364 NetClientState
*peer
;
370 if (!k
->set_guest_notifiers
) {
371 error_report("binding does not support guest notifiers");
375 for (i
= 0; i
< nvhosts
; i
++) {
377 if (i
< data_queue_pairs
) {
378 peer
= qemu_get_peer(ncs
, i
);
379 } else { /* Control Virtqueue */
380 peer
= qemu_get_peer(ncs
, n
->max_queue_pairs
);
383 net
= get_vhost_net(peer
);
384 vhost_net_set_vq_index(net
, i
* 2, index_end
);
386 /* Suppress the masking guest notifiers on vhost user
387 * because vhost user doesn't interrupt masking/unmasking
390 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_USER
) {
391 dev
->use_guest_notifier_mask
= false;
395 r
= k
->set_guest_notifiers(qbus
->parent
, total_notifiers
, true);
397 error_report("Error binding guest notifier: %d", -r
);
401 for (i
= 0; i
< nvhosts
; i
++) {
402 if (i
< data_queue_pairs
) {
403 peer
= qemu_get_peer(ncs
, i
);
405 peer
= qemu_get_peer(ncs
, n
->max_queue_pairs
);
408 if (peer
->vring_enable
) {
409 /* restore vring enable state */
410 r
= vhost_set_vring_enable(peer
, peer
->vring_enable
);
417 r
= vhost_net_start_one(get_vhost_net(peer
), dev
);
427 peer
= qemu_get_peer(ncs
, i
< data_queue_pairs
?
428 i
: n
->max_queue_pairs
);
429 vhost_net_stop_one(get_vhost_net(peer
), dev
);
431 e
= k
->set_guest_notifiers(qbus
->parent
, total_notifiers
, false);
433 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", e
);
440 void vhost_net_stop(VirtIODevice
*dev
, NetClientState
*ncs
,
441 int data_queue_pairs
, int cvq
)
443 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(dev
)));
444 VirtioBusState
*vbus
= VIRTIO_BUS(qbus
);
445 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(vbus
);
446 VirtIONet
*n
= VIRTIO_NET(dev
);
447 NetClientState
*peer
;
448 int total_notifiers
= data_queue_pairs
* 2 + cvq
;
449 int nvhosts
= data_queue_pairs
+ cvq
;
452 for (i
= 0; i
< nvhosts
; i
++) {
453 if (i
< data_queue_pairs
) {
454 peer
= qemu_get_peer(ncs
, i
);
456 peer
= qemu_get_peer(ncs
, n
->max_queue_pairs
);
458 vhost_net_stop_one(get_vhost_net(peer
), dev
);
461 r
= k
->set_guest_notifiers(qbus
->parent
, total_notifiers
, false);
463 fprintf(stderr
, "vhost guest notifier cleanup failed: %d\n", r
);
469 void vhost_net_cleanup(struct vhost_net
*net
)
471 vhost_dev_cleanup(&net
->dev
);
474 int vhost_net_notify_migration_done(struct vhost_net
*net
, char* mac_addr
)
476 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
478 assert(vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_USER
);
479 assert(vhost_ops
->vhost_migration_done
);
481 return vhost_ops
->vhost_migration_done(&net
->dev
, mac_addr
);
484 bool vhost_net_virtqueue_pending(VHostNetState
*net
, int idx
)
486 return vhost_virtqueue_pending(&net
->dev
, idx
);
489 void vhost_net_virtqueue_mask(VHostNetState
*net
, VirtIODevice
*dev
,
492 vhost_virtqueue_mask(&net
->dev
, dev
, idx
, mask
);
495 bool vhost_net_config_pending(VHostNetState
*net
)
497 return vhost_config_pending(&net
->dev
);
500 void vhost_net_config_mask(VHostNetState
*net
, VirtIODevice
*dev
, bool mask
)
502 vhost_config_mask(&net
->dev
, dev
, mask
);
504 VHostNetState
*get_vhost_net(NetClientState
*nc
)
506 VHostNetState
*vhost_net
= 0;
512 switch (nc
->info
->type
) {
513 case NET_CLIENT_DRIVER_TAP
:
514 vhost_net
= tap_get_vhost_net(nc
);
516 * tap_get_vhost_net() can return NULL if a tap net-device backend is
517 * created with 'vhost=off' option, 'vhostforce=off' or no vhost or
518 * vhostforce or vhostfd options at all. Please see net_init_tap_one().
519 * Hence, we omit the assertion here.
522 #ifdef CONFIG_VHOST_NET_USER
523 case NET_CLIENT_DRIVER_VHOST_USER
:
524 vhost_net
= vhost_user_get_vhost_net(nc
);
528 #ifdef CONFIG_VHOST_NET_VDPA
529 case NET_CLIENT_DRIVER_VHOST_VDPA
:
530 vhost_net
= vhost_vdpa_get_vhost_net(nc
);
541 int vhost_set_vring_enable(NetClientState
*nc
, int enable
)
543 VHostNetState
*net
= get_vhost_net(nc
);
544 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
547 * vhost-vdpa network devices need to enable dataplane virtqueues after
548 * DRIVER_OK, so they can recover device state before starting dataplane.
549 * Because of that, we don't enable virtqueues here and leave it to
552 if (nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
) {
556 nc
->vring_enable
= enable
;
558 if (vhost_ops
&& vhost_ops
->vhost_set_vring_enable
) {
559 return vhost_ops
->vhost_set_vring_enable(&net
->dev
, enable
);
565 int vhost_net_set_mtu(struct vhost_net
*net
, uint16_t mtu
)
567 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
569 if (!vhost_ops
->vhost_net_set_mtu
) {
573 return vhost_ops
->vhost_net_set_mtu(&net
->dev
, mtu
);
576 void vhost_net_virtqueue_reset(VirtIODevice
*vdev
, NetClientState
*nc
,
579 VHostNetState
*net
= get_vhost_net(nc
->peer
);
580 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
581 struct vhost_vring_file file
= { .fd
= -1 };
584 /* should only be called after backend is connected */
587 idx
= vhost_ops
->vhost_get_vq_index(&net
->dev
, vq_index
);
589 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
591 int r
= vhost_net_set_backend(&net
->dev
, &file
);
595 vhost_virtqueue_stop(&net
->dev
,
598 net
->dev
.vq_index
+ idx
);
601 int vhost_net_virtqueue_restart(VirtIODevice
*vdev
, NetClientState
*nc
,
604 VHostNetState
*net
= get_vhost_net(nc
->peer
);
605 const VhostOps
*vhost_ops
= net
->dev
.vhost_ops
;
606 struct vhost_vring_file file
= { };
609 if (!net
->dev
.started
) {
613 /* should only be called after backend is connected */
616 idx
= vhost_ops
->vhost_get_vq_index(&net
->dev
, vq_index
);
618 r
= vhost_virtqueue_start(&net
->dev
,
621 net
->dev
.vq_index
+ idx
);
626 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
628 file
.fd
= net
->backend
;
629 r
= vhost_net_set_backend(&net
->dev
, &file
);
639 error_report("Error when restarting the queue.");
641 if (net
->nc
->info
->type
== NET_CLIENT_DRIVER_TAP
) {
642 file
.fd
= VHOST_FILE_UNBIND
;
644 int ret
= vhost_net_set_backend(&net
->dev
, &file
);
648 vhost_dev_stop(&net
->dev
, vdev
, false);