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 */
47 /* The device can isolate CVQ in its own ASID */
54 * The array is sorted alphabetically in ascending order,
55 * with the exception of VHOST_INVALID_FEATURE_BIT,
56 * which should always be the last entry.
58 const int vdpa_feature_bits
[] = {
60 VIRTIO_F_IOMMU_PLATFORM
,
61 VIRTIO_F_NOTIFY_ON_EMPTY
,
66 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
,
67 VIRTIO_NET_F_CTRL_MAC_ADDR
,
69 VIRTIO_NET_F_CTRL_RX_EXTRA
,
70 VIRTIO_NET_F_CTRL_VLAN
,
73 VIRTIO_NET_F_GUEST_CSUM
,
74 VIRTIO_NET_F_GUEST_ECN
,
75 VIRTIO_NET_F_GUEST_TSO4
,
76 VIRTIO_NET_F_GUEST_TSO6
,
77 VIRTIO_NET_F_GUEST_UFO
,
78 VIRTIO_NET_F_GUEST_USO4
,
79 VIRTIO_NET_F_GUEST_USO6
,
80 VIRTIO_NET_F_HASH_REPORT
,
81 VIRTIO_NET_F_HOST_ECN
,
82 VIRTIO_NET_F_HOST_TSO4
,
83 VIRTIO_NET_F_HOST_TSO6
,
84 VIRTIO_NET_F_HOST_UFO
,
85 VIRTIO_NET_F_HOST_USO
,
87 VIRTIO_NET_F_MRG_RXBUF
,
91 VIRTIO_RING_F_EVENT_IDX
,
92 VIRTIO_RING_F_INDIRECT_DESC
,
94 /* VHOST_INVALID_FEATURE_BIT should always be the last entry */
95 VHOST_INVALID_FEATURE_BIT
98 /** Supported device specific feature bits with SVQ */
99 static const uint64_t vdpa_svq_device_features
=
100 BIT_ULL(VIRTIO_NET_F_CSUM
) |
101 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM
) |
102 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
) |
103 BIT_ULL(VIRTIO_NET_F_MTU
) |
104 BIT_ULL(VIRTIO_NET_F_MAC
) |
105 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4
) |
106 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6
) |
107 BIT_ULL(VIRTIO_NET_F_GUEST_ECN
) |
108 BIT_ULL(VIRTIO_NET_F_GUEST_UFO
) |
109 BIT_ULL(VIRTIO_NET_F_HOST_TSO4
) |
110 BIT_ULL(VIRTIO_NET_F_HOST_TSO6
) |
111 BIT_ULL(VIRTIO_NET_F_HOST_ECN
) |
112 BIT_ULL(VIRTIO_NET_F_HOST_UFO
) |
113 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF
) |
114 BIT_ULL(VIRTIO_NET_F_STATUS
) |
115 BIT_ULL(VIRTIO_NET_F_CTRL_VQ
) |
116 BIT_ULL(VIRTIO_NET_F_CTRL_RX
) |
117 BIT_ULL(VIRTIO_NET_F_CTRL_VLAN
) |
118 BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA
) |
119 BIT_ULL(VIRTIO_NET_F_MQ
) |
120 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
121 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
122 /* VHOST_F_LOG_ALL is exposed by SVQ */
123 BIT_ULL(VHOST_F_LOG_ALL
) |
124 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
125 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
126 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
128 #define VHOST_VDPA_NET_CVQ_ASID 1
130 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
132 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
133 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
137 static size_t vhost_vdpa_net_cvq_cmd_len(void)
140 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
141 * In buffer is always 1 byte, so it should fit here
143 return sizeof(struct virtio_net_ctrl_hdr
) +
144 2 * sizeof(struct virtio_net_ctrl_mac
) +
145 MAC_TABLE_ENTRIES
* ETH_ALEN
;
148 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
150 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
153 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
155 uint64_t invalid_dev_features
=
156 features
& ~vdpa_svq_device_features
&
157 /* Transport are all accepted at this point */
158 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
159 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
161 if (invalid_dev_features
) {
162 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
163 invalid_dev_features
);
167 return vhost_svq_valid_features(features
, errp
);
170 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
174 struct vhost_dev
*hdev
;
176 hdev
= (struct vhost_dev
*)&net
->dev
;
177 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
178 if (device_id
!= VIRTIO_ID_NET
) {
184 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
185 int queue_pair_index
, int nvqs
)
187 VhostNetOptions options
;
188 struct vhost_net
*net
= NULL
;
192 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
193 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
194 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
195 options
.net_backend
= ncs
;
197 options
.busyloop_timeout
= 0;
200 net
= vhost_net_init(&options
);
202 error_report("failed to init vhost_net for queue");
206 ret
= vhost_vdpa_net_check_device_id(net
);
212 vhost_net_cleanup(net
);
218 static void vhost_vdpa_cleanup(NetClientState
*nc
)
220 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
223 * If a peer NIC is attached, do not cleanup anything.
224 * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
225 * when the guest is shutting down.
227 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
230 munmap(s
->cvq_cmd_out_buffer
, vhost_vdpa_net_cvq_cmd_page_len());
231 munmap(s
->status
, vhost_vdpa_net_cvq_cmd_page_len());
233 vhost_net_cleanup(s
->vhost_net
);
234 g_free(s
->vhost_net
);
237 if (s
->vhost_vdpa
.device_fd
>= 0) {
238 qemu_close(s
->vhost_vdpa
.device_fd
);
239 s
->vhost_vdpa
.device_fd
= -1;
243 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
245 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
250 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
252 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
253 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
254 uint64_t features
= 0;
255 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
256 features
= vhost_net_get_features(s
->vhost_net
, features
);
257 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
261 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
264 const char *driver
= object_class_get_name(oc
);
266 if (!g_str_has_prefix(driver
, "virtio-net-")) {
267 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
274 /** Dummy receive in case qemu falls back to userland tap networking */
275 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
281 /** From any vdpa net client, get the netclient of the first queue pair */
282 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
284 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
285 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
287 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
290 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
292 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
295 int data_queue_pairs
, cvq
, r
;
297 /* We are only called on the first data vqs and only if x-svq is not set */
298 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
303 n
= VIRTIO_NET(vdev
);
304 if (!n
->vhost_started
) {
308 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
309 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
310 n
->max_ncs
- n
->max_queue_pairs
: 0;
312 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
313 * in the future and resume the device if read-only operations between
314 * suspend and reset goes wrong.
316 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
318 /* Start will check migration setup_or_active to configure or not SVQ */
319 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
320 if (unlikely(r
< 0)) {
321 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
325 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
327 MigrationState
*migration
= data
;
328 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
331 if (migration_in_setup(migration
)) {
332 vhost_vdpa_net_log_global_enable(s
, true);
333 } else if (migration_has_failed(migration
)) {
334 vhost_vdpa_net_log_global_enable(s
, false);
338 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
340 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
342 migration_add_notifier(&s
->migration_state
,
343 vdpa_net_migration_state_notifier
);
344 if (v
->shadow_vqs_enabled
) {
345 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
350 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
352 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
353 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
355 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
358 migration_is_setup_or_active(migrate_get_current()->state
)) {
359 v
->shadow_vqs_enabled
= true;
360 v
->shadow_data
= true;
362 v
->shadow_vqs_enabled
= false;
363 v
->shadow_data
= false;
367 vhost_vdpa_net_data_start_first(s
);
371 if (v
->shadow_vqs_enabled
) {
372 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
373 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
379 static int vhost_vdpa_net_data_load(NetClientState
*nc
)
381 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
382 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
383 bool has_cvq
= v
->dev
->vq_index_end
% 2;
389 for (int i
= 0; i
< v
->dev
->nvqs
; ++i
) {
390 vhost_vdpa_set_vring_ready(v
, i
+ v
->dev
->vq_index
);
395 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
397 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
398 struct vhost_dev
*dev
;
400 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
402 if (s
->vhost_vdpa
.index
== 0) {
403 migration_remove_notifier(&s
->migration_state
);
406 dev
= s
->vhost_vdpa
.dev
;
407 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
408 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
410 s
->vhost_vdpa
.iova_tree
= NULL
;
414 static NetClientInfo net_vhost_vdpa_info
= {
415 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
416 .size
= sizeof(VhostVDPAState
),
417 .receive
= vhost_vdpa_receive
,
418 .start
= vhost_vdpa_net_data_start
,
419 .load
= vhost_vdpa_net_data_load
,
420 .stop
= vhost_vdpa_net_client_stop
,
421 .cleanup
= vhost_vdpa_cleanup
,
422 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
423 .has_ufo
= vhost_vdpa_has_ufo
,
424 .check_peer_type
= vhost_vdpa_check_peer_type
,
427 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
430 struct vhost_vring_state state
= {
433 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
435 if (unlikely(r
< 0)) {
437 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
444 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
448 struct vhost_vring_state asid
= {
454 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
455 if (unlikely(r
< 0)) {
456 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
457 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
462 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
464 VhostIOVATree
*tree
= v
->iova_tree
;
467 * No need to specify size or to look for more translations since
468 * this contiguous chunk was allocated by us.
470 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
472 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
475 if (unlikely(!map
)) {
476 error_report("Cannot locate expected map");
480 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
481 if (unlikely(r
!= 0)) {
482 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
485 vhost_iova_tree_remove(tree
, *map
);
488 /** Map CVQ buffer. */
489 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
495 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
497 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
498 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
499 if (unlikely(r
!= IOVA_OK
)) {
500 error_report("Cannot map injected element");
504 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
505 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
506 if (unlikely(r
< 0)) {
513 vhost_iova_tree_remove(v
->iova_tree
, map
);
517 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
519 VhostVDPAState
*s
, *s0
;
520 struct vhost_vdpa
*v
;
525 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
527 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
530 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
531 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
532 v
->shadow_vqs_enabled
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
533 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
535 if (s
->vhost_vdpa
.shadow_data
) {
536 /* SVQ is already configured for all virtqueues */
541 * If we early return in these cases SVQ will not be enabled. The migration
542 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
544 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
548 if (!s
->cvq_isolated
) {
552 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
,
553 v
->dev
->vq_index_end
- 1,
555 if (unlikely(cvq_group
< 0)) {
556 error_report_err(err
);
560 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
561 if (unlikely(r
< 0)) {
565 v
->shadow_vqs_enabled
= true;
566 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
569 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
573 if (s0
->vhost_vdpa
.iova_tree
) {
575 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
576 * simplicity, whether CVQ shares ASID with guest or not, because:
577 * - Memory listener need access to guest's memory addresses allocated
579 * - There should be plenty of IOVA address space for both ASID not to
580 * worry about collisions between them. Guest's translations are
581 * still validated with virtio virtqueue_pop so there is no risk for
582 * the guest to access memory that it shouldn't.
584 * To allocate a iova tree per ASID is doable but it complicates the
585 * code and it is not worth it for the moment.
587 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
589 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
593 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
594 vhost_vdpa_net_cvq_cmd_page_len(), false);
595 if (unlikely(r
< 0)) {
599 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
600 vhost_vdpa_net_cvq_cmd_page_len(), true);
601 if (unlikely(r
< 0)) {
602 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
608 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
610 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
612 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
614 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
615 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
616 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
619 vhost_vdpa_net_client_stop(nc
);
622 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
,
623 const struct iovec
*out_sg
, size_t out_num
,
624 const struct iovec
*in_sg
, size_t in_num
)
626 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
629 r
= vhost_svq_add(svq
, out_sg
, out_num
, in_sg
, in_num
, NULL
);
630 if (unlikely(r
!= 0)) {
631 if (unlikely(r
== -ENOSPC
)) {
632 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
641 * Convenience wrapper to poll SVQ for multiple control commands.
643 * Caller should hold the BQL when invoking this function, and should take
644 * the answer before SVQ pulls by itself when BQL is released.
646 static ssize_t
vhost_vdpa_net_svq_poll(VhostVDPAState
*s
, size_t cmds_in_flight
)
648 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
649 return vhost_svq_poll(svq
, cmds_in_flight
);
652 static void vhost_vdpa_net_load_cursor_reset(VhostVDPAState
*s
,
653 struct iovec
*out_cursor
,
654 struct iovec
*in_cursor
)
656 /* reset the cursor of the output buffer for the device */
657 out_cursor
->iov_base
= s
->cvq_cmd_out_buffer
;
658 out_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
660 /* reset the cursor of the in buffer for the device */
661 in_cursor
->iov_base
= s
->status
;
662 in_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
666 * Poll SVQ for multiple pending control commands and check the device's ack.
668 * Caller should hold the BQL when invoking this function.
670 * @s: The VhostVDPAState
671 * @len: The length of the pending status shadow buffer
673 static ssize_t
vhost_vdpa_net_svq_flush(VhostVDPAState
*s
, size_t len
)
675 /* device uses a one-byte length ack for each control command */
676 ssize_t dev_written
= vhost_vdpa_net_svq_poll(s
, len
);
677 if (unlikely(dev_written
!= len
)) {
681 /* check the device's ack */
682 for (int i
= 0; i
< len
; ++i
) {
683 if (s
->status
[i
] != VIRTIO_NET_OK
) {
690 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
,
691 struct iovec
*out_cursor
,
692 struct iovec
*in_cursor
, uint8_t class,
693 uint8_t cmd
, const struct iovec
*data_sg
,
696 const struct virtio_net_ctrl_hdr ctrl
= {
700 size_t data_size
= iov_size(data_sg
, data_num
), cmd_size
;
701 struct iovec out
, in
;
703 unsigned dummy_cursor_iov_cnt
;
704 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
706 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
707 cmd_size
= sizeof(ctrl
) + data_size
;
708 if (vhost_svq_available_slots(svq
) < 2 ||
709 iov_size(out_cursor
, 1) < cmd_size
) {
711 * It is time to flush all pending control commands if SVQ is full
712 * or control commands shadow buffers are full.
714 * We can poll here since we've had BQL from the time
715 * we sent the descriptor.
717 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
->iov_base
-
719 if (unlikely(r
< 0)) {
723 vhost_vdpa_net_load_cursor_reset(s
, out_cursor
, in_cursor
);
726 /* pack the CVQ command header */
727 iov_from_buf(out_cursor
, 1, 0, &ctrl
, sizeof(ctrl
));
728 /* pack the CVQ command command-specific-data */
729 iov_to_buf(data_sg
, data_num
, 0,
730 out_cursor
->iov_base
+ sizeof(ctrl
), data_size
);
732 /* extract the required buffer from the cursor for output */
733 iov_copy(&out
, 1, out_cursor
, 1, 0, cmd_size
);
734 /* extract the required buffer from the cursor for input */
735 iov_copy(&in
, 1, in_cursor
, 1, 0, sizeof(*s
->status
));
737 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &in
, 1);
738 if (unlikely(r
< 0)) {
742 /* iterate the cursors */
743 dummy_cursor_iov_cnt
= 1;
744 iov_discard_front(&out_cursor
, &dummy_cursor_iov_cnt
, cmd_size
);
745 dummy_cursor_iov_cnt
= 1;
746 iov_discard_front(&in_cursor
, &dummy_cursor_iov_cnt
, sizeof(*s
->status
));
751 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
,
752 struct iovec
*out_cursor
,
753 struct iovec
*in_cursor
)
755 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
756 const struct iovec data
= {
757 .iov_base
= (void *)n
->mac
,
758 .iov_len
= sizeof(n
->mac
),
760 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
762 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
764 if (unlikely(r
< 0)) {
770 * According to VirtIO standard, "The device MUST have an
771 * empty MAC filtering table on reset.".
773 * Therefore, there is no need to send this CVQ command if the
774 * driver also sets an empty MAC filter table, which aligns with
775 * the device's defaults.
777 * Note that the device's defaults can mismatch the driver's
778 * configuration only at live migration.
780 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
) ||
781 n
->mac_table
.in_use
== 0) {
785 uint32_t uni_entries
= n
->mac_table
.first_multi
,
786 uni_macs_size
= uni_entries
* ETH_ALEN
,
787 mul_entries
= n
->mac_table
.in_use
- uni_entries
,
788 mul_macs_size
= mul_entries
* ETH_ALEN
;
789 struct virtio_net_ctrl_mac uni
= {
790 .entries
= cpu_to_le32(uni_entries
),
792 struct virtio_net_ctrl_mac mul
= {
793 .entries
= cpu_to_le32(mul_entries
),
795 const struct iovec data
[] = {
798 .iov_len
= sizeof(uni
),
800 .iov_base
= n
->mac_table
.macs
,
801 .iov_len
= uni_macs_size
,
804 .iov_len
= sizeof(mul
),
806 .iov_base
= &n
->mac_table
.macs
[uni_macs_size
],
807 .iov_len
= mul_macs_size
,
810 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
812 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
813 data
, ARRAY_SIZE(data
));
814 if (unlikely(r
< 0)) {
821 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
823 struct iovec
*out_cursor
,
824 struct iovec
*in_cursor
)
826 struct virtio_net_ctrl_mq mq
;
829 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
833 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
834 const struct iovec data
= {
836 .iov_len
= sizeof(mq
),
838 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
840 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
,
842 if (unlikely(r
< 0)) {
849 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
851 struct iovec
*out_cursor
,
852 struct iovec
*in_cursor
)
857 if (!virtio_vdev_has_feature(&n
->parent_obj
,
858 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
862 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
864 * According to VirtIO standard, "Upon feature negotiation
865 * corresponding offload gets enabled to preserve
866 * backward compatibility.".
868 * Therefore, there is no need to send this CVQ command if the
869 * driver also enables all supported offloads, which aligns with
870 * the device's defaults.
872 * Note that the device's defaults can mismatch the driver's
873 * configuration only at live migration.
878 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
879 const struct iovec data
= {
880 .iov_base
= &offloads
,
881 .iov_len
= sizeof(offloads
),
883 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
884 VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
885 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
887 if (unlikely(r
< 0)) {
894 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState
*s
,
895 struct iovec
*out_cursor
,
896 struct iovec
*in_cursor
,
900 const struct iovec data
= {
902 .iov_len
= sizeof(on
),
906 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
907 VIRTIO_NET_CTRL_RX
, cmd
, &data
, 1);
908 if (unlikely(r
< 0)) {
915 static int vhost_vdpa_net_load_rx(VhostVDPAState
*s
,
917 struct iovec
*out_cursor
,
918 struct iovec
*in_cursor
)
922 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
)) {
927 * According to virtio_net_reset(), device turns promiscuous mode
930 * Additionally, according to VirtIO standard, "Since there are
931 * no guarantees, it can use a hash filter or silently switch to
932 * allmulti or promiscuous mode if it is given too many addresses.".
933 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
934 * non-multicast MAC addresses, indicating that promiscuous mode
937 * Therefore, QEMU should only send this CVQ command if the
938 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
939 * which sets promiscuous mode on, different from the device's defaults.
941 * Note that the device's defaults can mismatch the driver's
942 * configuration only at live migration.
944 if (!n
->mac_table
.uni_overflow
&& !n
->promisc
) {
945 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
946 VIRTIO_NET_CTRL_RX_PROMISC
, 0);
947 if (unlikely(r
< 0)) {
953 * According to virtio_net_reset(), device turns all-multicast mode
956 * According to VirtIO standard, "Since there are no guarantees,
957 * it can use a hash filter or silently switch to allmulti or
958 * promiscuous mode if it is given too many addresses.". QEMU marks
959 * `n->mac_table.multi_overflow` if guest sets too many
960 * non-multicast MAC addresses.
962 * Therefore, QEMU should only send this CVQ command if the
963 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
964 * which sets all-multicast mode on, different from the device's defaults.
966 * Note that the device's defaults can mismatch the driver's
967 * configuration only at live migration.
969 if (n
->mac_table
.multi_overflow
|| n
->allmulti
) {
970 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
971 VIRTIO_NET_CTRL_RX_ALLMULTI
, 1);
972 if (unlikely(r
< 0)) {
977 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX_EXTRA
)) {
982 * According to virtio_net_reset(), device turns all-unicast mode
985 * Therefore, QEMU should only send this CVQ command if the driver
986 * sets all-unicast mode on, different from the device's defaults.
988 * Note that the device's defaults can mismatch the driver's
989 * configuration only at live migration.
992 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
993 VIRTIO_NET_CTRL_RX_ALLUNI
, 1);
1000 * According to virtio_net_reset(), device turns non-multicast mode
1003 * Therefore, QEMU should only send this CVQ command if the driver
1004 * sets non-multicast mode on, different from the device's defaults.
1006 * Note that the device's defaults can mismatch the driver's
1007 * configuration only at live migration.
1010 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1011 VIRTIO_NET_CTRL_RX_NOMULTI
, 1);
1018 * According to virtio_net_reset(), device turns non-unicast mode
1021 * Therefore, QEMU should only send this CVQ command if the driver
1022 * sets non-unicast mode on, different from the device's defaults.
1024 * Note that the device's defaults can mismatch the driver's
1025 * configuration only at live migration.
1028 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1029 VIRTIO_NET_CTRL_RX_NOUNI
, 1);
1036 * According to virtio_net_reset(), device turns non-broadcast mode
1039 * Therefore, QEMU should only send this CVQ command if the driver
1040 * sets non-broadcast mode on, different from the device's defaults.
1042 * Note that the device's defaults can mismatch the driver's
1043 * configuration only at live migration.
1046 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1047 VIRTIO_NET_CTRL_RX_NOBCAST
, 1);
1056 static int vhost_vdpa_net_load_single_vlan(VhostVDPAState
*s
,
1058 struct iovec
*out_cursor
,
1059 struct iovec
*in_cursor
,
1062 const struct iovec data
= {
1064 .iov_len
= sizeof(vid
),
1066 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1067 VIRTIO_NET_CTRL_VLAN
,
1068 VIRTIO_NET_CTRL_VLAN_ADD
,
1070 if (unlikely(r
< 0)) {
1077 static int vhost_vdpa_net_load_vlan(VhostVDPAState
*s
,
1079 struct iovec
*out_cursor
,
1080 struct iovec
*in_cursor
)
1084 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_VLAN
)) {
1088 for (int i
= 0; i
< MAX_VLAN
>> 5; i
++) {
1089 for (int j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
1090 if (n
->vlans
[i
] & (1U << j
)) {
1091 r
= vhost_vdpa_net_load_single_vlan(s
, n
, out_cursor
,
1092 in_cursor
, (i
<< 5) + j
);
1093 if (unlikely(r
!= 0)) {
1103 static int vhost_vdpa_net_cvq_load(NetClientState
*nc
)
1105 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1106 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
1109 struct iovec out_cursor
, in_cursor
;
1111 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1113 vhost_vdpa_set_vring_ready(v
, v
->dev
->vq_index
);
1115 if (v
->shadow_vqs_enabled
) {
1116 n
= VIRTIO_NET(v
->dev
->vdev
);
1117 vhost_vdpa_net_load_cursor_reset(s
, &out_cursor
, &in_cursor
);
1118 r
= vhost_vdpa_net_load_mac(s
, n
, &out_cursor
, &in_cursor
);
1119 if (unlikely(r
< 0)) {
1122 r
= vhost_vdpa_net_load_mq(s
, n
, &out_cursor
, &in_cursor
);
1126 r
= vhost_vdpa_net_load_offloads(s
, n
, &out_cursor
, &in_cursor
);
1130 r
= vhost_vdpa_net_load_rx(s
, n
, &out_cursor
, &in_cursor
);
1134 r
= vhost_vdpa_net_load_vlan(s
, n
, &out_cursor
, &in_cursor
);
1140 * We need to poll and check all pending device's used buffers.
1142 * We can poll here since we've had BQL from the time
1143 * we sent the descriptor.
1145 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
.iov_base
- (void *)s
->status
);
1151 for (int i
= 0; i
< v
->dev
->vq_index
; ++i
) {
1152 vhost_vdpa_set_vring_ready(v
, i
);
1158 static NetClientInfo net_vhost_vdpa_cvq_info
= {
1159 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
1160 .size
= sizeof(VhostVDPAState
),
1161 .receive
= vhost_vdpa_receive
,
1162 .start
= vhost_vdpa_net_cvq_start
,
1163 .load
= vhost_vdpa_net_cvq_load
,
1164 .stop
= vhost_vdpa_net_cvq_stop
,
1165 .cleanup
= vhost_vdpa_cleanup
,
1166 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
1167 .has_ufo
= vhost_vdpa_has_ufo
,
1168 .check_peer_type
= vhost_vdpa_check_peer_type
,
1172 * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1175 * Considering that QEMU cannot send the entire filter table to the
1176 * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1177 * command to enable promiscuous mode to receive all packets,
1178 * according to VirtIO standard, "Since there are no guarantees,
1179 * it can use a hash filter or silently switch to allmulti or
1180 * promiscuous mode if it is given too many addresses.".
1182 * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1183 * marks `n->mac_table.x_overflow` accordingly, it should have
1184 * the same effect on the device model to receive
1185 * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1186 * The same applies to multicast MAC addresses.
1188 * Therefore, QEMU can provide the device model with a fake
1189 * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1190 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1191 * MAC addresses. This ensures that the device model marks
1192 * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1193 * allowing all packets to be received, which aligns with the
1194 * state of the vdpa device.
1196 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState
*s
,
1197 VirtQueueElement
*elem
,
1199 const struct iovec
*in
)
1201 struct virtio_net_ctrl_mac mac_data
, *mac_ptr
;
1202 struct virtio_net_ctrl_hdr
*hdr_ptr
;
1207 /* parse the non-multicast MAC address entries from CVQ command */
1208 cursor
= sizeof(*hdr_ptr
);
1209 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1210 &mac_data
, sizeof(mac_data
));
1211 if (unlikely(r
!= sizeof(mac_data
))) {
1213 * If the CVQ command is invalid, we should simulate the vdpa device
1214 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1216 *s
->status
= VIRTIO_NET_ERR
;
1217 return sizeof(*s
->status
);
1219 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1221 /* parse the multicast MAC address entries from CVQ command */
1222 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1223 &mac_data
, sizeof(mac_data
));
1224 if (r
!= sizeof(mac_data
)) {
1226 * If the CVQ command is invalid, we should simulate the vdpa device
1227 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1229 *s
->status
= VIRTIO_NET_ERR
;
1230 return sizeof(*s
->status
);
1232 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1234 /* validate the CVQ command */
1235 if (iov_size(elem
->out_sg
, elem
->out_num
) != cursor
) {
1237 * If the CVQ command is invalid, we should simulate the vdpa device
1238 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1240 *s
->status
= VIRTIO_NET_ERR
;
1241 return sizeof(*s
->status
);
1245 * According to VirtIO standard, "Since there are no guarantees,
1246 * it can use a hash filter or silently switch to allmulti or
1247 * promiscuous mode if it is given too many addresses.".
1249 * Therefore, considering that QEMU is unable to send the entire
1250 * filter table to the vdpa device, it should send the
1251 * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1253 hdr_ptr
= out
->iov_base
;
1254 out
->iov_len
= sizeof(*hdr_ptr
) + sizeof(on
);
1256 hdr_ptr
->class = VIRTIO_NET_CTRL_RX
;
1257 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_RX_PROMISC
;
1258 iov_from_buf(out
, 1, sizeof(*hdr_ptr
), &on
, sizeof(on
));
1259 r
= vhost_vdpa_net_cvq_add(s
, out
, 1, in
, 1);
1260 if (unlikely(r
< 0)) {
1265 * We can poll here since we've had BQL from the time
1266 * we sent the descriptor.
1268 r
= vhost_vdpa_net_svq_poll(s
, 1);
1269 if (unlikely(r
< sizeof(*s
->status
))) {
1272 if (*s
->status
!= VIRTIO_NET_OK
) {
1273 return sizeof(*s
->status
);
1277 * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1278 * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1279 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1280 * multicast MAC addresses.
1282 * By doing so, the device model can mark `n->mac_table.uni_overflow`
1283 * and `n->mac_table.multi_overflow`, enabling all packets to be
1284 * received, which aligns with the state of the vdpa device.
1287 uint32_t fake_uni_entries
= MAC_TABLE_ENTRIES
+ 1,
1288 fake_mul_entries
= MAC_TABLE_ENTRIES
+ 1,
1289 fake_cvq_size
= sizeof(struct virtio_net_ctrl_hdr
) +
1290 sizeof(mac_data
) + fake_uni_entries
* ETH_ALEN
+
1291 sizeof(mac_data
) + fake_mul_entries
* ETH_ALEN
;
1293 assert(fake_cvq_size
< vhost_vdpa_net_cvq_cmd_page_len());
1294 out
->iov_len
= fake_cvq_size
;
1296 /* pack the header for fake CVQ command */
1297 hdr_ptr
= out
->iov_base
+ cursor
;
1298 hdr_ptr
->class = VIRTIO_NET_CTRL_MAC
;
1299 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_MAC_TABLE_SET
;
1300 cursor
+= sizeof(*hdr_ptr
);
1303 * Pack the non-multicast MAC addresses part for fake CVQ command.
1305 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1306 * addresses provided in CVQ command. Therefore, only the entries
1307 * field need to be prepared in the CVQ command.
1309 mac_ptr
= out
->iov_base
+ cursor
;
1310 mac_ptr
->entries
= cpu_to_le32(fake_uni_entries
);
1311 cursor
+= sizeof(*mac_ptr
) + fake_uni_entries
* ETH_ALEN
;
1314 * Pack the multicast MAC addresses part for fake CVQ command.
1316 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1317 * addresses provided in CVQ command. Therefore, only the entries
1318 * field need to be prepared in the CVQ command.
1320 mac_ptr
= out
->iov_base
+ cursor
;
1321 mac_ptr
->entries
= cpu_to_le32(fake_mul_entries
);
1324 * Simulating QEMU poll a vdpa device used buffer
1325 * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1327 return sizeof(*s
->status
);
1331 * Validate and copy control virtqueue commands.
1333 * Following QEMU guidelines, we offer a copy of the buffers to the device to
1334 * prevent TOCTOU bugs.
1336 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
1337 VirtQueueElement
*elem
,
1340 VhostVDPAState
*s
= opaque
;
1342 const struct virtio_net_ctrl_hdr
*ctrl
;
1343 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1344 /* Out buffer sent to both the vdpa device and the device model */
1345 struct iovec out
= {
1346 .iov_base
= s
->cvq_cmd_out_buffer
,
1348 /* in buffer used for device model */
1349 const struct iovec model_in
= {
1350 .iov_base
= &status
,
1351 .iov_len
= sizeof(status
),
1353 /* in buffer used for vdpa device */
1354 const struct iovec vdpa_in
= {
1355 .iov_base
= s
->status
,
1356 .iov_len
= sizeof(*s
->status
),
1358 ssize_t dev_written
= -EINVAL
;
1360 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1361 s
->cvq_cmd_out_buffer
,
1362 vhost_vdpa_net_cvq_cmd_page_len());
1364 ctrl
= s
->cvq_cmd_out_buffer
;
1365 if (ctrl
->class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1367 * Guest announce capability is emulated by qemu, so don't forward to
1370 dev_written
= sizeof(status
);
1371 *s
->status
= VIRTIO_NET_OK
;
1372 } else if (unlikely(ctrl
->class == VIRTIO_NET_CTRL_MAC
&&
1373 ctrl
->cmd
== VIRTIO_NET_CTRL_MAC_TABLE_SET
&&
1374 iov_size(elem
->out_sg
, elem
->out_num
) > out
.iov_len
)) {
1376 * Due to the size limitation of the out buffer sent to the vdpa device,
1377 * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1378 * MAC addresses set by the driver for the filter table can cause
1379 * truncation of the CVQ command in QEMU. As a result, the vdpa device
1380 * rejects the flawed CVQ command.
1382 * Therefore, QEMU must handle this situation instead of sending
1383 * the CVQ command directly.
1385 dev_written
= vhost_vdpa_net_excessive_mac_filter_cvq_add(s
, elem
,
1387 if (unlikely(dev_written
< 0)) {
1392 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &vdpa_in
, 1);
1393 if (unlikely(r
< 0)) {
1399 * We can poll here since we've had BQL from the time
1400 * we sent the descriptor.
1402 dev_written
= vhost_vdpa_net_svq_poll(s
, 1);
1405 if (unlikely(dev_written
< sizeof(status
))) {
1406 error_report("Insufficient written data (%zu)", dev_written
);
1410 if (*s
->status
!= VIRTIO_NET_OK
) {
1414 status
= VIRTIO_NET_ERR
;
1415 virtio_net_handle_ctrl_iov(svq
->vdev
, &model_in
, 1, &out
, 1);
1416 if (status
!= VIRTIO_NET_OK
) {
1417 error_report("Bad CVQ processing in model");
1421 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
1423 if (unlikely(in_len
< sizeof(status
))) {
1424 error_report("Bad device CVQ written length");
1426 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
1428 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1429 * the function successfully forwards the CVQ command, indicated
1430 * by a non-negative value of `dev_written`. Otherwise, it still
1432 * This function should only free the `elem` when it owns.
1434 if (dev_written
>= 0) {
1437 return dev_written
< 0 ? dev_written
: 0;
1440 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
1441 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
1445 * Probe if CVQ is isolated
1447 * @device_fd The vdpa device fd
1448 * @features Features offered by the device.
1449 * @cvq_index The control vq pair index
1451 * Returns <0 in case of failure, 0 if false and 1 if true.
1453 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
1454 int cvq_index
, Error
**errp
)
1456 uint64_t backend_features
;
1458 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1459 VIRTIO_CONFIG_S_DRIVER
;
1464 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
1465 if (unlikely(r
< 0)) {
1466 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
1470 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
1474 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1476 error_setg_errno(errp
, -r
, "Cannot set device status");
1480 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
1482 error_setg_errno(errp
, -r
, "Cannot set features");
1486 status
|= VIRTIO_CONFIG_S_FEATURES_OK
;
1487 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1489 error_setg_errno(errp
, -r
, "Cannot set device status");
1493 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
1494 if (unlikely(cvq_group
< 0)) {
1495 if (cvq_group
!= -ENOTSUP
) {
1501 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1502 * support ASID even if the parent driver does not. The CVQ cannot be
1503 * isolated in this case.
1511 for (int i
= 0; i
< cvq_index
; ++i
) {
1512 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
1513 if (unlikely(group
< 0)) {
1518 if (group
== (int64_t)cvq_group
) {
1528 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1532 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
1536 int queue_pair_index
,
1540 struct vhost_vdpa_iova_range iova_range
,
1544 NetClientState
*nc
= NULL
;
1548 int cvq_isolated
= 0;
1551 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
1554 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
1555 queue_pair_index
* 2,
1557 if (unlikely(cvq_isolated
< 0)) {
1561 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
1564 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
1565 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1567 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
1568 s
->vhost_vdpa
.index
= queue_pair_index
;
1569 s
->always_svq
= svq
;
1570 s
->migration_state
.notify
= NULL
;
1571 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
1572 s
->vhost_vdpa
.iova_range
= iova_range
;
1573 s
->vhost_vdpa
.shadow_data
= svq
;
1574 if (queue_pair_index
== 0) {
1575 vhost_vdpa_net_valid_svq_features(features
,
1576 &s
->vhost_vdpa
.migration_blocker
);
1577 } else if (!is_datapath
) {
1578 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1579 PROT_READ
| PROT_WRITE
,
1580 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1581 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1582 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
1585 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
1586 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
1587 s
->cvq_isolated
= cvq_isolated
;
1589 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
1591 qemu_del_net_client(nc
);
1597 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
1599 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
1600 if (unlikely(ret
< 0)) {
1601 error_setg_errno(errp
, errno
,
1602 "Fail to query features from vhost-vDPA device");
1607 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
1608 int *has_cvq
, Error
**errp
)
1610 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
1611 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
1612 __virtio16
*max_queue_pairs
;
1615 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1621 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1622 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1623 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1624 config
->len
= sizeof(*max_queue_pairs
);
1626 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1628 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1632 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1634 return lduw_le_p(max_queue_pairs
);
1640 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1641 NetClientState
*peer
, Error
**errp
)
1643 const NetdevVhostVDPAOptions
*opts
;
1646 g_autofree NetClientState
**ncs
= NULL
;
1647 struct vhost_vdpa_iova_range iova_range
;
1649 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1651 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1652 opts
= &netdev
->u
.vhost_vdpa
;
1653 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1655 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1659 if (opts
->vhostdev
&& opts
->vhostfd
) {
1661 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1665 if (opts
->vhostdev
) {
1666 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1667 if (vdpa_device_fd
== -1) {
1672 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1673 if (vdpa_device_fd
== -1) {
1674 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1679 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1680 if (unlikely(r
< 0)) {
1684 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1686 if (queue_pairs
< 0) {
1687 qemu_close(vdpa_device_fd
);
1691 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1692 if (unlikely(r
< 0)) {
1693 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1698 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1702 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1704 for (i
= 0; i
< queue_pairs
; i
++) {
1705 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1706 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1707 iova_range
, features
, errp
);
1713 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1714 vdpa_device_fd
, i
, 1, false,
1715 opts
->x_svq
, iova_range
, features
, errp
);
1724 for (i
--; i
>= 0; i
--) {
1725 qemu_del_net_client(ncs
[i
]);
1729 qemu_close(vdpa_device_fd
);