4 * Copyright(c) 2017-2018 Intel Corporation.
5 * Copyright(c) 2020 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
14 #include "hw/virtio/virtio-net.h"
15 #include "net/vhost_net.h"
16 #include "net/vhost-vdpa.h"
17 #include "hw/virtio/vhost-vdpa.h"
18 #include "qemu/config-file.h"
19 #include "qemu/error-report.h"
21 #include "qemu/memalign.h"
22 #include "qemu/option.h"
23 #include "qapi/error.h"
24 #include <linux/vhost.h>
25 #include <sys/ioctl.h>
27 #include "standard-headers/linux/virtio_net.h"
28 #include "monitor/monitor.h"
29 #include "migration/migration.h"
30 #include "migration/misc.h"
31 #include "hw/virtio/vhost.h"
33 /* Todo:need to add the multiqueue support here */
34 typedef struct VhostVDPAState
{
36 struct vhost_vdpa vhost_vdpa
;
37 Notifier migration_state
;
38 VHostNetState
*vhost_net
;
40 /* Control commands shadow buffers */
41 void *cvq_cmd_out_buffer
;
42 virtio_net_ctrl_ack
*status
;
44 /* The device always have SVQ enabled */
49 const int vdpa_feature_bits
[] = {
50 VIRTIO_F_NOTIFY_ON_EMPTY
,
51 VIRTIO_RING_F_INDIRECT_DESC
,
52 VIRTIO_RING_F_EVENT_IDX
,
56 VIRTIO_NET_F_GUEST_CSUM
,
58 VIRTIO_NET_F_GUEST_TSO4
,
59 VIRTIO_NET_F_GUEST_TSO6
,
60 VIRTIO_NET_F_GUEST_ECN
,
61 VIRTIO_NET_F_GUEST_UFO
,
62 VIRTIO_NET_F_HOST_TSO4
,
63 VIRTIO_NET_F_HOST_TSO6
,
64 VIRTIO_NET_F_HOST_ECN
,
65 VIRTIO_NET_F_HOST_UFO
,
66 VIRTIO_NET_F_MRG_RXBUF
,
69 VIRTIO_NET_F_CTRL_RX_EXTRA
,
70 VIRTIO_NET_F_CTRL_VLAN
,
71 VIRTIO_NET_F_CTRL_MAC_ADDR
,
75 VIRTIO_F_IOMMU_PLATFORM
,
79 VIRTIO_NET_F_HASH_REPORT
,
81 VHOST_INVALID_FEATURE_BIT
84 /** Supported device specific feature bits with SVQ */
85 static const uint64_t vdpa_svq_device_features
=
86 BIT_ULL(VIRTIO_NET_F_CSUM
) |
87 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM
) |
88 BIT_ULL(VIRTIO_NET_F_MTU
) |
89 BIT_ULL(VIRTIO_NET_F_MAC
) |
90 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4
) |
91 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6
) |
92 BIT_ULL(VIRTIO_NET_F_GUEST_ECN
) |
93 BIT_ULL(VIRTIO_NET_F_GUEST_UFO
) |
94 BIT_ULL(VIRTIO_NET_F_HOST_TSO4
) |
95 BIT_ULL(VIRTIO_NET_F_HOST_TSO6
) |
96 BIT_ULL(VIRTIO_NET_F_HOST_ECN
) |
97 BIT_ULL(VIRTIO_NET_F_HOST_UFO
) |
98 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF
) |
99 BIT_ULL(VIRTIO_NET_F_STATUS
) |
100 BIT_ULL(VIRTIO_NET_F_CTRL_VQ
) |
101 BIT_ULL(VIRTIO_NET_F_MQ
) |
102 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
103 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
104 /* VHOST_F_LOG_ALL is exposed by SVQ */
105 BIT_ULL(VHOST_F_LOG_ALL
) |
106 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
107 BIT_ULL(VIRTIO_NET_F_STANDBY
);
109 #define VHOST_VDPA_NET_CVQ_ASID 1
111 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
113 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
114 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
118 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
120 uint64_t invalid_dev_features
=
121 features
& ~vdpa_svq_device_features
&
122 /* Transport are all accepted at this point */
123 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
124 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
126 if (invalid_dev_features
) {
127 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
128 invalid_dev_features
);
132 return vhost_svq_valid_features(features
, errp
);
135 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
139 struct vhost_dev
*hdev
;
141 hdev
= (struct vhost_dev
*)&net
->dev
;
142 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
143 if (device_id
!= VIRTIO_ID_NET
) {
149 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
150 int queue_pair_index
, int nvqs
)
152 VhostNetOptions options
;
153 struct vhost_net
*net
= NULL
;
157 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
158 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
159 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
160 options
.net_backend
= ncs
;
162 options
.busyloop_timeout
= 0;
165 net
= vhost_net_init(&options
);
167 error_report("failed to init vhost_net for queue");
171 ret
= vhost_vdpa_net_check_device_id(net
);
177 vhost_net_cleanup(net
);
183 static void vhost_vdpa_cleanup(NetClientState
*nc
)
185 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
187 qemu_vfree(s
->cvq_cmd_out_buffer
);
188 qemu_vfree(s
->status
);
190 vhost_net_cleanup(s
->vhost_net
);
191 g_free(s
->vhost_net
);
194 if (s
->vhost_vdpa
.device_fd
>= 0) {
195 qemu_close(s
->vhost_vdpa
.device_fd
);
196 s
->vhost_vdpa
.device_fd
= -1;
200 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
202 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
207 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
209 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
210 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
211 uint64_t features
= 0;
212 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
213 features
= vhost_net_get_features(s
->vhost_net
, features
);
214 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
218 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
221 const char *driver
= object_class_get_name(oc
);
223 if (!g_str_has_prefix(driver
, "virtio-net-")) {
224 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
231 /** Dummy receive in case qemu falls back to userland tap networking */
232 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
238 /** From any vdpa net client, get the netclient of the first queue pair */
239 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
241 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
242 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
244 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
247 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
249 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
252 int data_queue_pairs
, cvq
, r
;
254 /* We are only called on the first data vqs and only if x-svq is not set */
255 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
260 n
= VIRTIO_NET(vdev
);
261 if (!n
->vhost_started
) {
265 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
266 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
267 n
->max_ncs
- n
->max_queue_pairs
: 0;
269 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
270 * in the future and resume the device if read-only operations between
271 * suspend and reset goes wrong.
273 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
275 /* Start will check migration setup_or_active to configure or not SVQ */
276 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
277 if (unlikely(r
< 0)) {
278 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
282 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
284 MigrationState
*migration
= data
;
285 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
288 if (migration_in_setup(migration
)) {
289 vhost_vdpa_net_log_global_enable(s
, true);
290 } else if (migration_has_failed(migration
)) {
291 vhost_vdpa_net_log_global_enable(s
, false);
295 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
297 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
299 add_migration_state_change_notifier(&s
->migration_state
);
300 if (v
->shadow_vqs_enabled
) {
301 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
306 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
308 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
309 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
311 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
314 migration_is_setup_or_active(migrate_get_current()->state
)) {
315 v
->shadow_vqs_enabled
= true;
316 v
->shadow_data
= true;
318 v
->shadow_vqs_enabled
= false;
319 v
->shadow_data
= false;
323 vhost_vdpa_net_data_start_first(s
);
327 if (v
->shadow_vqs_enabled
) {
328 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
329 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
335 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
337 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
338 struct vhost_dev
*dev
;
340 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
342 if (s
->vhost_vdpa
.index
== 0) {
343 remove_migration_state_change_notifier(&s
->migration_state
);
346 dev
= s
->vhost_vdpa
.dev
;
347 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
348 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
352 static NetClientInfo net_vhost_vdpa_info
= {
353 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
354 .size
= sizeof(VhostVDPAState
),
355 .receive
= vhost_vdpa_receive
,
356 .start
= vhost_vdpa_net_data_start
,
357 .stop
= vhost_vdpa_net_client_stop
,
358 .cleanup
= vhost_vdpa_cleanup
,
359 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
360 .has_ufo
= vhost_vdpa_has_ufo
,
361 .check_peer_type
= vhost_vdpa_check_peer_type
,
364 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
)
366 struct vhost_vring_state state
= {
369 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
371 if (unlikely(r
< 0)) {
372 error_report("Cannot get VQ %u group: %s", vq_index
,
380 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
384 struct vhost_vring_state asid
= {
390 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
391 if (unlikely(r
< 0)) {
392 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
393 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
398 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
400 VhostIOVATree
*tree
= v
->iova_tree
;
403 * No need to specify size or to look for more translations since
404 * this contiguous chunk was allocated by us.
406 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
408 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
411 if (unlikely(!map
)) {
412 error_report("Cannot locate expected map");
416 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
417 if (unlikely(r
!= 0)) {
418 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
421 vhost_iova_tree_remove(tree
, *map
);
424 static size_t vhost_vdpa_net_cvq_cmd_len(void)
427 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
428 * In buffer is always 1 byte, so it should fit here
430 return sizeof(struct virtio_net_ctrl_hdr
) +
431 2 * sizeof(struct virtio_net_ctrl_mac
) +
432 MAC_TABLE_ENTRIES
* ETH_ALEN
;
435 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
437 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
440 /** Map CVQ buffer. */
441 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
447 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
449 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
450 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
451 if (unlikely(r
!= IOVA_OK
)) {
452 error_report("Cannot map injected element");
456 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
457 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
458 if (unlikely(r
< 0)) {
465 vhost_iova_tree_remove(v
->iova_tree
, map
);
469 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
471 VhostVDPAState
*s
, *s0
;
472 struct vhost_vdpa
*v
;
473 uint64_t backend_features
;
477 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
479 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
482 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
483 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
484 v
->shadow_vqs_enabled
= s
->always_svq
;
485 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
487 if (s
->vhost_vdpa
.shadow_data
) {
488 /* SVQ is already configured for all virtqueues */
493 * If we early return in these cases SVQ will not be enabled. The migration
494 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
496 * Calling VHOST_GET_BACKEND_FEATURES as they are not available in v->dev
499 r
= ioctl(v
->device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
500 if (unlikely(r
< 0)) {
501 error_report("Cannot get vdpa backend_features: %s(%d)",
502 g_strerror(errno
), errno
);
505 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
)) ||
506 !vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
511 * Check if all the virtqueues of the virtio device are in a different vq
512 * than the last vq. VQ group of last group passed in cvq_group.
514 cvq_index
= v
->dev
->vq_index_end
- 1;
515 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
, cvq_index
);
516 if (unlikely(cvq_group
< 0)) {
519 for (int i
= 0; i
< cvq_index
; ++i
) {
520 int64_t group
= vhost_vdpa_get_vring_group(v
->device_fd
, i
);
522 if (unlikely(group
< 0)) {
526 if (group
== cvq_group
) {
531 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
532 if (unlikely(r
< 0)) {
536 v
->shadow_vqs_enabled
= true;
537 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
540 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
544 if (s0
->vhost_vdpa
.iova_tree
) {
546 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
547 * simplicity, whether CVQ shares ASID with guest or not, because:
548 * - Memory listener need access to guest's memory addresses allocated
550 * - There should be plenty of IOVA address space for both ASID not to
551 * worry about collisions between them. Guest's translations are
552 * still validated with virtio virtqueue_pop so there is no risk for
553 * the guest to access memory that it shouldn't.
555 * To allocate a iova tree per ASID is doable but it complicates the
556 * code and it is not worth it for the moment.
558 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
560 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
564 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
565 vhost_vdpa_net_cvq_cmd_page_len(), false);
566 if (unlikely(r
< 0)) {
570 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
571 vhost_vdpa_net_cvq_cmd_page_len(), true);
572 if (unlikely(r
< 0)) {
573 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
579 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
581 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
583 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
585 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
586 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
587 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
590 vhost_vdpa_net_client_stop(nc
);
593 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
, size_t out_len
,
596 /* Buffers for the device */
597 const struct iovec out
= {
598 .iov_base
= s
->cvq_cmd_out_buffer
,
601 const struct iovec in
= {
602 .iov_base
= s
->status
,
603 .iov_len
= sizeof(virtio_net_ctrl_ack
),
605 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
608 r
= vhost_svq_add(svq
, &out
, 1, &in
, 1, NULL
);
609 if (unlikely(r
!= 0)) {
610 if (unlikely(r
== -ENOSPC
)) {
611 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
618 * We can poll here since we've had BQL from the time we sent the
619 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
620 * when BQL is released
622 return vhost_svq_poll(svq
);
625 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
, uint8_t class,
626 uint8_t cmd
, const void *data
,
629 const struct virtio_net_ctrl_hdr ctrl
= {
634 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
636 memcpy(s
->cvq_cmd_out_buffer
, &ctrl
, sizeof(ctrl
));
637 memcpy(s
->cvq_cmd_out_buffer
+ sizeof(ctrl
), data
, data_size
);
639 return vhost_vdpa_net_cvq_add(s
, sizeof(ctrl
) + data_size
,
640 sizeof(virtio_net_ctrl_ack
));
643 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
)
645 uint64_t features
= n
->parent_obj
.guest_features
;
646 if (features
& BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
647 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MAC
,
648 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
649 n
->mac
, sizeof(n
->mac
));
650 if (unlikely(dev_written
< 0)) {
654 return *s
->status
!= VIRTIO_NET_OK
;
660 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
663 struct virtio_net_ctrl_mq mq
;
664 uint64_t features
= n
->parent_obj
.guest_features
;
667 if (!(features
& BIT_ULL(VIRTIO_NET_F_MQ
))) {
671 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
672 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MQ
,
673 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
, &mq
,
675 if (unlikely(dev_written
< 0)) {
679 return *s
->status
!= VIRTIO_NET_OK
;
682 static int vhost_vdpa_net_load(NetClientState
*nc
)
684 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
685 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
689 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
691 if (!v
->shadow_vqs_enabled
) {
695 n
= VIRTIO_NET(v
->dev
->vdev
);
696 r
= vhost_vdpa_net_load_mac(s
, n
);
697 if (unlikely(r
< 0)) {
700 r
= vhost_vdpa_net_load_mq(s
, n
);
708 static NetClientInfo net_vhost_vdpa_cvq_info
= {
709 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
710 .size
= sizeof(VhostVDPAState
),
711 .receive
= vhost_vdpa_receive
,
712 .start
= vhost_vdpa_net_cvq_start
,
713 .load
= vhost_vdpa_net_load
,
714 .stop
= vhost_vdpa_net_cvq_stop
,
715 .cleanup
= vhost_vdpa_cleanup
,
716 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
717 .has_ufo
= vhost_vdpa_has_ufo
,
718 .check_peer_type
= vhost_vdpa_check_peer_type
,
722 * Validate and copy control virtqueue commands.
724 * Following QEMU guidelines, we offer a copy of the buffers to the device to
725 * prevent TOCTOU bugs.
727 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
728 VirtQueueElement
*elem
,
731 VhostVDPAState
*s
= opaque
;
733 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
734 /* Out buffer sent to both the vdpa device and the device model */
736 .iov_base
= s
->cvq_cmd_out_buffer
,
738 /* in buffer used for device model */
739 const struct iovec in
= {
741 .iov_len
= sizeof(status
),
743 ssize_t dev_written
= -EINVAL
;
745 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
746 s
->cvq_cmd_out_buffer
,
747 vhost_vdpa_net_cvq_cmd_len());
748 if (*(uint8_t *)s
->cvq_cmd_out_buffer
== VIRTIO_NET_CTRL_ANNOUNCE
) {
750 * Guest announce capability is emulated by qemu, so don't forward to
753 dev_written
= sizeof(status
);
754 *s
->status
= VIRTIO_NET_OK
;
756 dev_written
= vhost_vdpa_net_cvq_add(s
, out
.iov_len
, sizeof(status
));
757 if (unlikely(dev_written
< 0)) {
762 if (unlikely(dev_written
< sizeof(status
))) {
763 error_report("Insufficient written data (%zu)", dev_written
);
767 if (*s
->status
!= VIRTIO_NET_OK
) {
768 return VIRTIO_NET_ERR
;
771 status
= VIRTIO_NET_ERR
;
772 virtio_net_handle_ctrl_iov(svq
->vdev
, &in
, 1, &out
, 1);
773 if (status
!= VIRTIO_NET_OK
) {
774 error_report("Bad CVQ processing in model");
778 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
780 if (unlikely(in_len
< sizeof(status
))) {
781 error_report("Bad device CVQ written length");
783 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
785 return dev_written
< 0 ? dev_written
: 0;
788 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
789 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
792 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
796 int queue_pair_index
,
800 struct vhost_vdpa_iova_range iova_range
,
803 NetClientState
*nc
= NULL
;
808 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
811 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
814 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
815 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
817 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
818 s
->vhost_vdpa
.index
= queue_pair_index
;
820 s
->migration_state
.notify
= vdpa_net_migration_state_notifier
;
821 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
822 s
->vhost_vdpa
.iova_range
= iova_range
;
823 s
->vhost_vdpa
.shadow_data
= svq
;
824 if (queue_pair_index
== 0) {
825 vhost_vdpa_net_valid_svq_features(features
,
826 &s
->vhost_vdpa
.migration_blocker
);
827 } else if (!is_datapath
) {
828 s
->cvq_cmd_out_buffer
= qemu_memalign(qemu_real_host_page_size(),
829 vhost_vdpa_net_cvq_cmd_page_len());
830 memset(s
->cvq_cmd_out_buffer
, 0, vhost_vdpa_net_cvq_cmd_page_len());
831 s
->status
= qemu_memalign(qemu_real_host_page_size(),
832 vhost_vdpa_net_cvq_cmd_page_len());
833 memset(s
->status
, 0, vhost_vdpa_net_cvq_cmd_page_len());
835 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
836 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
839 * TODO: We cannot migrate devices with CVQ as there is no way to set
840 * the device state (MAC, MQ, etc) before starting the datapath.
842 * Migration blocker ownership now belongs to s->vhost_vdpa.
844 error_setg(&s
->vhost_vdpa
.migration_blocker
,
845 "net vdpa cannot migrate with CVQ feature");
847 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
849 qemu_del_net_client(nc
);
855 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
857 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
858 if (unlikely(ret
< 0)) {
859 error_setg_errno(errp
, errno
,
860 "Fail to query features from vhost-vDPA device");
865 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
866 int *has_cvq
, Error
**errp
)
868 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
869 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
870 __virtio16
*max_queue_pairs
;
873 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
879 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
880 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
881 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
882 config
->len
= sizeof(*max_queue_pairs
);
884 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
886 error_setg(errp
, "Fail to get config from vhost-vDPA device");
890 max_queue_pairs
= (__virtio16
*)&config
->buf
;
892 return lduw_le_p(max_queue_pairs
);
898 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
899 NetClientState
*peer
, Error
**errp
)
901 const NetdevVhostVDPAOptions
*opts
;
904 g_autofree NetClientState
**ncs
= NULL
;
905 struct vhost_vdpa_iova_range iova_range
;
907 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
909 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
910 opts
= &netdev
->u
.vhost_vdpa
;
911 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
913 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
917 if (opts
->vhostdev
&& opts
->vhostfd
) {
919 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
923 if (opts
->vhostdev
) {
924 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
925 if (vdpa_device_fd
== -1) {
930 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
931 if (vdpa_device_fd
== -1) {
932 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
937 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
938 if (unlikely(r
< 0)) {
942 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
944 if (queue_pairs
< 0) {
945 qemu_close(vdpa_device_fd
);
949 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
950 if (unlikely(r
< 0)) {
951 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
956 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
960 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
962 for (i
= 0; i
< queue_pairs
; i
++) {
963 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
964 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
965 iova_range
, features
);
971 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
972 vdpa_device_fd
, i
, 1, false,
973 opts
->x_svq
, iova_range
, features
);
982 for (i
--; i
>= 0; i
--) {
983 qemu_del_net_client(ncs
[i
]);
987 qemu_close(vdpa_device_fd
);