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_HASH_REPORT
) |
125 BIT_ULL(VIRTIO_NET_F_RSS
) |
126 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
127 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
128 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
130 #define VHOST_VDPA_NET_CVQ_ASID 1
132 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
134 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
135 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
139 static size_t vhost_vdpa_net_cvq_cmd_len(void)
142 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
143 * In buffer is always 1 byte, so it should fit here
145 return sizeof(struct virtio_net_ctrl_hdr
) +
146 2 * sizeof(struct virtio_net_ctrl_mac
) +
147 MAC_TABLE_ENTRIES
* ETH_ALEN
;
150 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
152 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
155 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
157 uint64_t invalid_dev_features
=
158 features
& ~vdpa_svq_device_features
&
159 /* Transport are all accepted at this point */
160 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
161 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
163 if (invalid_dev_features
) {
164 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
165 invalid_dev_features
);
169 return vhost_svq_valid_features(features
, errp
);
172 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
176 struct vhost_dev
*hdev
;
178 hdev
= (struct vhost_dev
*)&net
->dev
;
179 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
180 if (device_id
!= VIRTIO_ID_NET
) {
186 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
187 int queue_pair_index
, int nvqs
)
189 VhostNetOptions options
;
190 struct vhost_net
*net
= NULL
;
194 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
195 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
196 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
197 options
.net_backend
= ncs
;
199 options
.busyloop_timeout
= 0;
202 net
= vhost_net_init(&options
);
204 error_report("failed to init vhost_net for queue");
208 ret
= vhost_vdpa_net_check_device_id(net
);
214 vhost_net_cleanup(net
);
220 static void vhost_vdpa_cleanup(NetClientState
*nc
)
222 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
225 * If a peer NIC is attached, do not cleanup anything.
226 * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
227 * when the guest is shutting down.
229 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
232 munmap(s
->cvq_cmd_out_buffer
, vhost_vdpa_net_cvq_cmd_page_len());
233 munmap(s
->status
, vhost_vdpa_net_cvq_cmd_page_len());
235 vhost_net_cleanup(s
->vhost_net
);
236 g_free(s
->vhost_net
);
239 if (s
->vhost_vdpa
.index
!= 0) {
242 qemu_close(s
->vhost_vdpa
.shared
->device_fd
);
243 g_free(s
->vhost_vdpa
.shared
);
246 /** Dummy SetSteeringEBPF to support RSS for vhost-vdpa backend */
247 static bool vhost_vdpa_set_steering_ebpf(NetClientState
*nc
, int prog_fd
)
252 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
254 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
259 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
261 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
262 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
263 uint64_t features
= 0;
264 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
265 features
= vhost_net_get_features(s
->vhost_net
, features
);
266 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
270 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
273 const char *driver
= object_class_get_name(oc
);
275 if (!g_str_has_prefix(driver
, "virtio-net-")) {
276 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
283 /** Dummy receive in case qemu falls back to userland tap networking */
284 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
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
->shared
->iova_tree
= vhost_iova_tree_new(v
->shared
->iova_range
.first
,
346 v
->shared
->iova_range
.last
);
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;
361 v
->shadow_vqs_enabled
= false;
365 v
->shared
->shadow_data
= v
->shadow_vqs_enabled
;
366 vhost_vdpa_net_data_start_first(s
);
373 static int vhost_vdpa_net_data_load(NetClientState
*nc
)
375 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
376 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
377 bool has_cvq
= v
->dev
->vq_index_end
% 2;
383 for (int i
= 0; i
< v
->dev
->nvqs
; ++i
) {
384 vhost_vdpa_set_vring_ready(v
, i
+ v
->dev
->vq_index
);
389 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
391 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
392 struct vhost_dev
*dev
;
394 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
396 if (s
->vhost_vdpa
.index
== 0) {
397 migration_remove_notifier(&s
->migration_state
);
400 dev
= s
->vhost_vdpa
.dev
;
401 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
402 g_clear_pointer(&s
->vhost_vdpa
.shared
->iova_tree
,
403 vhost_iova_tree_delete
);
407 static NetClientInfo net_vhost_vdpa_info
= {
408 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
409 .size
= sizeof(VhostVDPAState
),
410 .receive
= vhost_vdpa_receive
,
411 .start
= vhost_vdpa_net_data_start
,
412 .load
= vhost_vdpa_net_data_load
,
413 .stop
= vhost_vdpa_net_client_stop
,
414 .cleanup
= vhost_vdpa_cleanup
,
415 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
416 .has_ufo
= vhost_vdpa_has_ufo
,
417 .check_peer_type
= vhost_vdpa_check_peer_type
,
418 .set_steering_ebpf
= vhost_vdpa_set_steering_ebpf
,
421 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
424 struct vhost_vring_state state
= {
427 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
429 if (unlikely(r
< 0)) {
431 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
438 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
442 struct vhost_vring_state asid
= {
448 r
= ioctl(v
->shared
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
449 if (unlikely(r
< 0)) {
450 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
451 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
456 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
458 VhostIOVATree
*tree
= v
->shared
->iova_tree
;
461 * No need to specify size or to look for more translations since
462 * this contiguous chunk was allocated by us.
464 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
466 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
469 if (unlikely(!map
)) {
470 error_report("Cannot locate expected map");
474 r
= vhost_vdpa_dma_unmap(v
->shared
, v
->address_space_id
, map
->iova
,
476 if (unlikely(r
!= 0)) {
477 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
480 vhost_iova_tree_remove(tree
, *map
);
483 /** Map CVQ buffer. */
484 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
490 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
492 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
493 r
= vhost_iova_tree_map_alloc(v
->shared
->iova_tree
, &map
);
494 if (unlikely(r
!= IOVA_OK
)) {
495 error_report("Cannot map injected element");
499 r
= vhost_vdpa_dma_map(v
->shared
, v
->address_space_id
, map
.iova
,
500 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
501 if (unlikely(r
< 0)) {
508 vhost_iova_tree_remove(v
->shared
->iova_tree
, map
);
512 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
515 struct vhost_vdpa
*v
;
520 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
522 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
525 v
->shadow_vqs_enabled
= v
->shared
->shadow_data
;
526 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
528 if (v
->shared
->shadow_data
) {
529 /* SVQ is already configured for all virtqueues */
534 * If we early return in these cases SVQ will not be enabled. The migration
535 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
537 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
541 if (!s
->cvq_isolated
) {
545 cvq_group
= vhost_vdpa_get_vring_group(v
->shared
->device_fd
,
546 v
->dev
->vq_index_end
- 1,
548 if (unlikely(cvq_group
< 0)) {
549 error_report_err(err
);
553 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
554 if (unlikely(r
< 0)) {
558 v
->shadow_vqs_enabled
= true;
559 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
562 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
567 * If other vhost_vdpa already have an iova_tree, reuse it for simplicity,
568 * whether CVQ shares ASID with guest or not, because:
569 * - Memory listener need access to guest's memory addresses allocated in
571 * - There should be plenty of IOVA address space for both ASID not to
572 * worry about collisions between them. Guest's translations are still
573 * validated with virtio virtqueue_pop so there is no risk for the guest
574 * to access memory that it shouldn't.
576 * To allocate a iova tree per ASID is doable but it complicates the code
577 * and it is not worth it for the moment.
579 if (!v
->shared
->iova_tree
) {
580 v
->shared
->iova_tree
= vhost_iova_tree_new(v
->shared
->iova_range
.first
,
581 v
->shared
->iova_range
.last
);
584 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
585 vhost_vdpa_net_cvq_cmd_page_len(), false);
586 if (unlikely(r
< 0)) {
590 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
591 vhost_vdpa_net_cvq_cmd_page_len(), true);
592 if (unlikely(r
< 0)) {
593 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
599 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
601 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
603 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
605 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
606 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
607 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
610 vhost_vdpa_net_client_stop(nc
);
613 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
,
614 const struct iovec
*out_sg
, size_t out_num
,
615 const struct iovec
*in_sg
, size_t in_num
)
617 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
620 r
= vhost_svq_add(svq
, out_sg
, out_num
, in_sg
, in_num
, NULL
);
621 if (unlikely(r
!= 0)) {
622 if (unlikely(r
== -ENOSPC
)) {
623 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
632 * Convenience wrapper to poll SVQ for multiple control commands.
634 * Caller should hold the BQL when invoking this function, and should take
635 * the answer before SVQ pulls by itself when BQL is released.
637 static ssize_t
vhost_vdpa_net_svq_poll(VhostVDPAState
*s
, size_t cmds_in_flight
)
639 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
640 return vhost_svq_poll(svq
, cmds_in_flight
);
643 static void vhost_vdpa_net_load_cursor_reset(VhostVDPAState
*s
,
644 struct iovec
*out_cursor
,
645 struct iovec
*in_cursor
)
647 /* reset the cursor of the output buffer for the device */
648 out_cursor
->iov_base
= s
->cvq_cmd_out_buffer
;
649 out_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
651 /* reset the cursor of the in buffer for the device */
652 in_cursor
->iov_base
= s
->status
;
653 in_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
657 * Poll SVQ for multiple pending control commands and check the device's ack.
659 * Caller should hold the BQL when invoking this function.
661 * @s: The VhostVDPAState
662 * @len: The length of the pending status shadow buffer
664 static ssize_t
vhost_vdpa_net_svq_flush(VhostVDPAState
*s
, size_t len
)
666 /* device uses a one-byte length ack for each control command */
667 ssize_t dev_written
= vhost_vdpa_net_svq_poll(s
, len
);
668 if (unlikely(dev_written
!= len
)) {
672 /* check the device's ack */
673 for (int i
= 0; i
< len
; ++i
) {
674 if (s
->status
[i
] != VIRTIO_NET_OK
) {
681 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
,
682 struct iovec
*out_cursor
,
683 struct iovec
*in_cursor
, uint8_t class,
684 uint8_t cmd
, const struct iovec
*data_sg
,
687 const struct virtio_net_ctrl_hdr ctrl
= {
691 size_t data_size
= iov_size(data_sg
, data_num
), cmd_size
;
692 struct iovec out
, in
;
694 unsigned dummy_cursor_iov_cnt
;
695 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
697 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
698 cmd_size
= sizeof(ctrl
) + data_size
;
699 if (vhost_svq_available_slots(svq
) < 2 ||
700 iov_size(out_cursor
, 1) < cmd_size
) {
702 * It is time to flush all pending control commands if SVQ is full
703 * or control commands shadow buffers are full.
705 * We can poll here since we've had BQL from the time
706 * we sent the descriptor.
708 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
->iov_base
-
710 if (unlikely(r
< 0)) {
714 vhost_vdpa_net_load_cursor_reset(s
, out_cursor
, in_cursor
);
717 /* pack the CVQ command header */
718 iov_from_buf(out_cursor
, 1, 0, &ctrl
, sizeof(ctrl
));
719 /* pack the CVQ command command-specific-data */
720 iov_to_buf(data_sg
, data_num
, 0,
721 out_cursor
->iov_base
+ sizeof(ctrl
), data_size
);
723 /* extract the required buffer from the cursor for output */
724 iov_copy(&out
, 1, out_cursor
, 1, 0, cmd_size
);
725 /* extract the required buffer from the cursor for input */
726 iov_copy(&in
, 1, in_cursor
, 1, 0, sizeof(*s
->status
));
728 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &in
, 1);
729 if (unlikely(r
< 0)) {
733 /* iterate the cursors */
734 dummy_cursor_iov_cnt
= 1;
735 iov_discard_front(&out_cursor
, &dummy_cursor_iov_cnt
, cmd_size
);
736 dummy_cursor_iov_cnt
= 1;
737 iov_discard_front(&in_cursor
, &dummy_cursor_iov_cnt
, sizeof(*s
->status
));
742 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
,
743 struct iovec
*out_cursor
,
744 struct iovec
*in_cursor
)
746 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
747 const struct iovec data
= {
748 .iov_base
= (void *)n
->mac
,
749 .iov_len
= sizeof(n
->mac
),
751 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
753 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
755 if (unlikely(r
< 0)) {
761 * According to VirtIO standard, "The device MUST have an
762 * empty MAC filtering table on reset.".
764 * Therefore, there is no need to send this CVQ command if the
765 * driver also sets an empty MAC filter table, which aligns with
766 * the device's defaults.
768 * Note that the device's defaults can mismatch the driver's
769 * configuration only at live migration.
771 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
) ||
772 n
->mac_table
.in_use
== 0) {
776 uint32_t uni_entries
= n
->mac_table
.first_multi
,
777 uni_macs_size
= uni_entries
* ETH_ALEN
,
778 mul_entries
= n
->mac_table
.in_use
- uni_entries
,
779 mul_macs_size
= mul_entries
* ETH_ALEN
;
780 struct virtio_net_ctrl_mac uni
= {
781 .entries
= cpu_to_le32(uni_entries
),
783 struct virtio_net_ctrl_mac mul
= {
784 .entries
= cpu_to_le32(mul_entries
),
786 const struct iovec data
[] = {
789 .iov_len
= sizeof(uni
),
791 .iov_base
= n
->mac_table
.macs
,
792 .iov_len
= uni_macs_size
,
795 .iov_len
= sizeof(mul
),
797 .iov_base
= &n
->mac_table
.macs
[uni_macs_size
],
798 .iov_len
= mul_macs_size
,
801 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
803 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
804 data
, ARRAY_SIZE(data
));
805 if (unlikely(r
< 0)) {
812 static int vhost_vdpa_net_load_rss(VhostVDPAState
*s
, const VirtIONet
*n
,
813 struct iovec
*out_cursor
,
814 struct iovec
*in_cursor
, bool do_rss
)
816 struct virtio_net_rss_config cfg
= {};
818 g_autofree
uint16_t *table
= NULL
;
821 * According to VirtIO standard, "Initially the device has all hash
822 * types disabled and reports only VIRTIO_NET_HASH_REPORT_NONE.".
824 * Therefore, there is no need to send this CVQ command if the
825 * driver disables the all hash types, which aligns with
826 * the device's defaults.
828 * Note that the device's defaults can mismatch the driver's
829 * configuration only at live migration.
831 if (!n
->rss_data
.enabled
||
832 n
->rss_data
.hash_types
== VIRTIO_NET_HASH_REPORT_NONE
) {
836 table
= g_malloc_n(n
->rss_data
.indirections_len
,
837 sizeof(n
->rss_data
.indirections_table
[0]));
838 cfg
.hash_types
= cpu_to_le32(n
->rss_data
.hash_types
);
842 * According to VirtIO standard, "Number of entries in indirection_table
843 * is (indirection_table_mask + 1)".
845 cfg
.indirection_table_mask
= cpu_to_le16(n
->rss_data
.indirections_len
-
847 cfg
.unclassified_queue
= cpu_to_le16(n
->rss_data
.default_queue
);
848 for (int i
= 0; i
< n
->rss_data
.indirections_len
; ++i
) {
849 table
[i
] = cpu_to_le16(n
->rss_data
.indirections_table
[i
]);
851 cfg
.max_tx_vq
= cpu_to_le16(n
->curr_queue_pairs
);
854 * According to VirtIO standard, "Field reserved MUST contain zeroes.
855 * It is defined to make the structure to match the layout of
856 * virtio_net_rss_config structure, defined in 5.1.6.5.7.".
858 * Therefore, we need to zero the fields in
859 * struct virtio_net_rss_config, which corresponds to the
860 * `reserved` field in struct virtio_net_hash_config.
862 * Note that all other fields are zeroed at their definitions,
863 * except for the `indirection_table` field, where the actual data
864 * is stored in the `table` variable to ensure compatibility
865 * with RSS case. Therefore, we need to zero the `table` variable here.
871 * Considering that virtio_net_handle_rss() currently does not restore
872 * the hash key length parsed from the CVQ command sent from the guest
873 * into n->rss_data and uses the maximum key length in other code, so
874 * we also employ the maximum key length here.
876 cfg
.hash_key_length
= sizeof(n
->rss_data
.key
);
878 const struct iovec data
[] = {
881 .iov_len
= offsetof(struct virtio_net_rss_config
,
885 .iov_len
= n
->rss_data
.indirections_len
*
886 sizeof(n
->rss_data
.indirections_table
[0]),
888 .iov_base
= &cfg
.max_tx_vq
,
889 .iov_len
= offsetof(struct virtio_net_rss_config
, hash_key_data
) -
890 offsetof(struct virtio_net_rss_config
, max_tx_vq
),
892 .iov_base
= (void *)n
->rss_data
.key
,
893 .iov_len
= sizeof(n
->rss_data
.key
),
897 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
899 do_rss
? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
:
900 VIRTIO_NET_CTRL_MQ_HASH_CONFIG
,
901 data
, ARRAY_SIZE(data
));
902 if (unlikely(r
< 0)) {
909 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
911 struct iovec
*out_cursor
,
912 struct iovec
*in_cursor
)
914 struct virtio_net_ctrl_mq mq
;
917 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
921 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
922 const struct iovec data
= {
924 .iov_len
= sizeof(mq
),
926 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
928 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
,
930 if (unlikely(r
< 0)) {
934 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_RSS
)) {
935 /* load the receive-side scaling state */
936 r
= vhost_vdpa_net_load_rss(s
, n
, out_cursor
, in_cursor
, true);
937 if (unlikely(r
< 0)) {
940 } else if (virtio_vdev_has_feature(&n
->parent_obj
,
941 VIRTIO_NET_F_HASH_REPORT
)) {
942 /* load the hash calculation state */
943 r
= vhost_vdpa_net_load_rss(s
, n
, out_cursor
, in_cursor
, false);
944 if (unlikely(r
< 0)) {
952 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
954 struct iovec
*out_cursor
,
955 struct iovec
*in_cursor
)
960 if (!virtio_vdev_has_feature(&n
->parent_obj
,
961 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
965 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
967 * According to VirtIO standard, "Upon feature negotiation
968 * corresponding offload gets enabled to preserve
969 * backward compatibility.".
971 * Therefore, there is no need to send this CVQ command if the
972 * driver also enables all supported offloads, which aligns with
973 * the device's defaults.
975 * Note that the device's defaults can mismatch the driver's
976 * configuration only at live migration.
981 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
982 const struct iovec data
= {
983 .iov_base
= &offloads
,
984 .iov_len
= sizeof(offloads
),
986 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
987 VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
988 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
990 if (unlikely(r
< 0)) {
997 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState
*s
,
998 struct iovec
*out_cursor
,
999 struct iovec
*in_cursor
,
1003 const struct iovec data
= {
1005 .iov_len
= sizeof(on
),
1009 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1010 VIRTIO_NET_CTRL_RX
, cmd
, &data
, 1);
1011 if (unlikely(r
< 0)) {
1018 static int vhost_vdpa_net_load_rx(VhostVDPAState
*s
,
1020 struct iovec
*out_cursor
,
1021 struct iovec
*in_cursor
)
1025 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
)) {
1030 * According to virtio_net_reset(), device turns promiscuous mode
1033 * Additionally, according to VirtIO standard, "Since there are
1034 * no guarantees, it can use a hash filter or silently switch to
1035 * allmulti or promiscuous mode if it is given too many addresses.".
1036 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
1037 * non-multicast MAC addresses, indicating that promiscuous mode
1038 * should be enabled.
1040 * Therefore, QEMU should only send this CVQ command if the
1041 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
1042 * which sets promiscuous mode on, different from the device's defaults.
1044 * Note that the device's defaults can mismatch the driver's
1045 * configuration only at live migration.
1047 if (!n
->mac_table
.uni_overflow
&& !n
->promisc
) {
1048 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1049 VIRTIO_NET_CTRL_RX_PROMISC
, 0);
1050 if (unlikely(r
< 0)) {
1056 * According to virtio_net_reset(), device turns all-multicast mode
1059 * According to VirtIO standard, "Since there are no guarantees,
1060 * it can use a hash filter or silently switch to allmulti or
1061 * promiscuous mode if it is given too many addresses.". QEMU marks
1062 * `n->mac_table.multi_overflow` if guest sets too many
1063 * non-multicast MAC addresses.
1065 * Therefore, QEMU should only send this CVQ command if the
1066 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
1067 * which sets all-multicast mode on, different from the device's defaults.
1069 * Note that the device's defaults can mismatch the driver's
1070 * configuration only at live migration.
1072 if (n
->mac_table
.multi_overflow
|| n
->allmulti
) {
1073 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1074 VIRTIO_NET_CTRL_RX_ALLMULTI
, 1);
1075 if (unlikely(r
< 0)) {
1080 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX_EXTRA
)) {
1085 * According to virtio_net_reset(), device turns all-unicast mode
1088 * Therefore, QEMU should only send this CVQ command if the driver
1089 * sets all-unicast mode on, different from the device's defaults.
1091 * Note that the device's defaults can mismatch the driver's
1092 * configuration only at live migration.
1095 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1096 VIRTIO_NET_CTRL_RX_ALLUNI
, 1);
1103 * According to virtio_net_reset(), device turns non-multicast mode
1106 * Therefore, QEMU should only send this CVQ command if the driver
1107 * sets non-multicast mode on, different from the device's defaults.
1109 * Note that the device's defaults can mismatch the driver's
1110 * configuration only at live migration.
1113 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1114 VIRTIO_NET_CTRL_RX_NOMULTI
, 1);
1121 * According to virtio_net_reset(), device turns non-unicast mode
1124 * Therefore, QEMU should only send this CVQ command if the driver
1125 * sets non-unicast mode on, different from the device's defaults.
1127 * Note that the device's defaults can mismatch the driver's
1128 * configuration only at live migration.
1131 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1132 VIRTIO_NET_CTRL_RX_NOUNI
, 1);
1139 * According to virtio_net_reset(), device turns non-broadcast mode
1142 * Therefore, QEMU should only send this CVQ command if the driver
1143 * sets non-broadcast mode on, different from the device's defaults.
1145 * Note that the device's defaults can mismatch the driver's
1146 * configuration only at live migration.
1149 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1150 VIRTIO_NET_CTRL_RX_NOBCAST
, 1);
1159 static int vhost_vdpa_net_load_single_vlan(VhostVDPAState
*s
,
1161 struct iovec
*out_cursor
,
1162 struct iovec
*in_cursor
,
1165 const struct iovec data
= {
1167 .iov_len
= sizeof(vid
),
1169 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1170 VIRTIO_NET_CTRL_VLAN
,
1171 VIRTIO_NET_CTRL_VLAN_ADD
,
1173 if (unlikely(r
< 0)) {
1180 static int vhost_vdpa_net_load_vlan(VhostVDPAState
*s
,
1182 struct iovec
*out_cursor
,
1183 struct iovec
*in_cursor
)
1187 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_VLAN
)) {
1191 for (int i
= 0; i
< MAX_VLAN
>> 5; i
++) {
1192 for (int j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
1193 if (n
->vlans
[i
] & (1U << j
)) {
1194 r
= vhost_vdpa_net_load_single_vlan(s
, n
, out_cursor
,
1195 in_cursor
, (i
<< 5) + j
);
1196 if (unlikely(r
!= 0)) {
1206 static int vhost_vdpa_net_cvq_load(NetClientState
*nc
)
1208 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1209 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
1212 struct iovec out_cursor
, in_cursor
;
1214 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1216 vhost_vdpa_set_vring_ready(v
, v
->dev
->vq_index
);
1218 if (v
->shadow_vqs_enabled
) {
1219 n
= VIRTIO_NET(v
->dev
->vdev
);
1220 vhost_vdpa_net_load_cursor_reset(s
, &out_cursor
, &in_cursor
);
1221 r
= vhost_vdpa_net_load_mac(s
, n
, &out_cursor
, &in_cursor
);
1222 if (unlikely(r
< 0)) {
1225 r
= vhost_vdpa_net_load_mq(s
, n
, &out_cursor
, &in_cursor
);
1229 r
= vhost_vdpa_net_load_offloads(s
, n
, &out_cursor
, &in_cursor
);
1233 r
= vhost_vdpa_net_load_rx(s
, n
, &out_cursor
, &in_cursor
);
1237 r
= vhost_vdpa_net_load_vlan(s
, n
, &out_cursor
, &in_cursor
);
1243 * We need to poll and check all pending device's used buffers.
1245 * We can poll here since we've had BQL from the time
1246 * we sent the descriptor.
1248 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
.iov_base
- (void *)s
->status
);
1254 for (int i
= 0; i
< v
->dev
->vq_index
; ++i
) {
1255 vhost_vdpa_set_vring_ready(v
, i
);
1261 static NetClientInfo net_vhost_vdpa_cvq_info
= {
1262 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
1263 .size
= sizeof(VhostVDPAState
),
1264 .receive
= vhost_vdpa_receive
,
1265 .start
= vhost_vdpa_net_cvq_start
,
1266 .load
= vhost_vdpa_net_cvq_load
,
1267 .stop
= vhost_vdpa_net_cvq_stop
,
1268 .cleanup
= vhost_vdpa_cleanup
,
1269 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
1270 .has_ufo
= vhost_vdpa_has_ufo
,
1271 .check_peer_type
= vhost_vdpa_check_peer_type
,
1272 .set_steering_ebpf
= vhost_vdpa_set_steering_ebpf
,
1276 * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1279 * Considering that QEMU cannot send the entire filter table to the
1280 * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1281 * command to enable promiscuous mode to receive all packets,
1282 * according to VirtIO standard, "Since there are no guarantees,
1283 * it can use a hash filter or silently switch to allmulti or
1284 * promiscuous mode if it is given too many addresses.".
1286 * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1287 * marks `n->mac_table.x_overflow` accordingly, it should have
1288 * the same effect on the device model to receive
1289 * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1290 * The same applies to multicast MAC addresses.
1292 * Therefore, QEMU can provide the device model with a fake
1293 * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1294 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1295 * MAC addresses. This ensures that the device model marks
1296 * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1297 * allowing all packets to be received, which aligns with the
1298 * state of the vdpa device.
1300 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState
*s
,
1301 VirtQueueElement
*elem
,
1303 const struct iovec
*in
)
1305 struct virtio_net_ctrl_mac mac_data
, *mac_ptr
;
1306 struct virtio_net_ctrl_hdr
*hdr_ptr
;
1311 /* parse the non-multicast MAC address entries from CVQ command */
1312 cursor
= sizeof(*hdr_ptr
);
1313 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1314 &mac_data
, sizeof(mac_data
));
1315 if (unlikely(r
!= sizeof(mac_data
))) {
1317 * If the CVQ command is invalid, we should simulate the vdpa device
1318 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1320 *s
->status
= VIRTIO_NET_ERR
;
1321 return sizeof(*s
->status
);
1323 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1325 /* parse the multicast MAC address entries from CVQ command */
1326 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1327 &mac_data
, sizeof(mac_data
));
1328 if (r
!= sizeof(mac_data
)) {
1330 * If the CVQ command is invalid, we should simulate the vdpa device
1331 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1333 *s
->status
= VIRTIO_NET_ERR
;
1334 return sizeof(*s
->status
);
1336 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1338 /* validate the CVQ command */
1339 if (iov_size(elem
->out_sg
, elem
->out_num
) != cursor
) {
1341 * If the CVQ command is invalid, we should simulate the vdpa device
1342 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1344 *s
->status
= VIRTIO_NET_ERR
;
1345 return sizeof(*s
->status
);
1349 * According to VirtIO standard, "Since there are no guarantees,
1350 * it can use a hash filter or silently switch to allmulti or
1351 * promiscuous mode if it is given too many addresses.".
1353 * Therefore, considering that QEMU is unable to send the entire
1354 * filter table to the vdpa device, it should send the
1355 * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1357 hdr_ptr
= out
->iov_base
;
1358 out
->iov_len
= sizeof(*hdr_ptr
) + sizeof(on
);
1360 hdr_ptr
->class = VIRTIO_NET_CTRL_RX
;
1361 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_RX_PROMISC
;
1362 iov_from_buf(out
, 1, sizeof(*hdr_ptr
), &on
, sizeof(on
));
1363 r
= vhost_vdpa_net_cvq_add(s
, out
, 1, in
, 1);
1364 if (unlikely(r
< 0)) {
1369 * We can poll here since we've had BQL from the time
1370 * we sent the descriptor.
1372 r
= vhost_vdpa_net_svq_poll(s
, 1);
1373 if (unlikely(r
< sizeof(*s
->status
))) {
1376 if (*s
->status
!= VIRTIO_NET_OK
) {
1377 return sizeof(*s
->status
);
1381 * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1382 * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1383 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1384 * multicast MAC addresses.
1386 * By doing so, the device model can mark `n->mac_table.uni_overflow`
1387 * and `n->mac_table.multi_overflow`, enabling all packets to be
1388 * received, which aligns with the state of the vdpa device.
1391 uint32_t fake_uni_entries
= MAC_TABLE_ENTRIES
+ 1,
1392 fake_mul_entries
= MAC_TABLE_ENTRIES
+ 1,
1393 fake_cvq_size
= sizeof(struct virtio_net_ctrl_hdr
) +
1394 sizeof(mac_data
) + fake_uni_entries
* ETH_ALEN
+
1395 sizeof(mac_data
) + fake_mul_entries
* ETH_ALEN
;
1397 assert(fake_cvq_size
< vhost_vdpa_net_cvq_cmd_page_len());
1398 out
->iov_len
= fake_cvq_size
;
1400 /* pack the header for fake CVQ command */
1401 hdr_ptr
= out
->iov_base
+ cursor
;
1402 hdr_ptr
->class = VIRTIO_NET_CTRL_MAC
;
1403 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_MAC_TABLE_SET
;
1404 cursor
+= sizeof(*hdr_ptr
);
1407 * Pack the non-multicast MAC addresses part for fake CVQ command.
1409 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1410 * addresses provided in CVQ command. Therefore, only the entries
1411 * field need to be prepared in the CVQ command.
1413 mac_ptr
= out
->iov_base
+ cursor
;
1414 mac_ptr
->entries
= cpu_to_le32(fake_uni_entries
);
1415 cursor
+= sizeof(*mac_ptr
) + fake_uni_entries
* ETH_ALEN
;
1418 * Pack the multicast MAC addresses part for fake CVQ command.
1420 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1421 * addresses provided in CVQ command. Therefore, only the entries
1422 * field need to be prepared in the CVQ command.
1424 mac_ptr
= out
->iov_base
+ cursor
;
1425 mac_ptr
->entries
= cpu_to_le32(fake_mul_entries
);
1428 * Simulating QEMU poll a vdpa device used buffer
1429 * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1431 return sizeof(*s
->status
);
1435 * Validate and copy control virtqueue commands.
1437 * Following QEMU guidelines, we offer a copy of the buffers to the device to
1438 * prevent TOCTOU bugs.
1440 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
1441 VirtQueueElement
*elem
,
1444 VhostVDPAState
*s
= opaque
;
1446 const struct virtio_net_ctrl_hdr
*ctrl
;
1447 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1448 /* Out buffer sent to both the vdpa device and the device model */
1449 struct iovec out
= {
1450 .iov_base
= s
->cvq_cmd_out_buffer
,
1452 /* in buffer used for device model */
1453 const struct iovec model_in
= {
1454 .iov_base
= &status
,
1455 .iov_len
= sizeof(status
),
1457 /* in buffer used for vdpa device */
1458 const struct iovec vdpa_in
= {
1459 .iov_base
= s
->status
,
1460 .iov_len
= sizeof(*s
->status
),
1462 ssize_t dev_written
= -EINVAL
;
1464 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1465 s
->cvq_cmd_out_buffer
,
1466 vhost_vdpa_net_cvq_cmd_page_len());
1468 ctrl
= s
->cvq_cmd_out_buffer
;
1469 if (ctrl
->class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1471 * Guest announce capability is emulated by qemu, so don't forward to
1474 dev_written
= sizeof(status
);
1475 *s
->status
= VIRTIO_NET_OK
;
1476 } else if (unlikely(ctrl
->class == VIRTIO_NET_CTRL_MAC
&&
1477 ctrl
->cmd
== VIRTIO_NET_CTRL_MAC_TABLE_SET
&&
1478 iov_size(elem
->out_sg
, elem
->out_num
) > out
.iov_len
)) {
1480 * Due to the size limitation of the out buffer sent to the vdpa device,
1481 * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1482 * MAC addresses set by the driver for the filter table can cause
1483 * truncation of the CVQ command in QEMU. As a result, the vdpa device
1484 * rejects the flawed CVQ command.
1486 * Therefore, QEMU must handle this situation instead of sending
1487 * the CVQ command directly.
1489 dev_written
= vhost_vdpa_net_excessive_mac_filter_cvq_add(s
, elem
,
1491 if (unlikely(dev_written
< 0)) {
1496 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &vdpa_in
, 1);
1497 if (unlikely(r
< 0)) {
1503 * We can poll here since we've had BQL from the time
1504 * we sent the descriptor.
1506 dev_written
= vhost_vdpa_net_svq_poll(s
, 1);
1509 if (unlikely(dev_written
< sizeof(status
))) {
1510 error_report("Insufficient written data (%zu)", dev_written
);
1514 if (*s
->status
!= VIRTIO_NET_OK
) {
1518 status
= VIRTIO_NET_ERR
;
1519 virtio_net_handle_ctrl_iov(svq
->vdev
, &model_in
, 1, &out
, 1);
1520 if (status
!= VIRTIO_NET_OK
) {
1521 error_report("Bad CVQ processing in model");
1525 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
1527 if (unlikely(in_len
< sizeof(status
))) {
1528 error_report("Bad device CVQ written length");
1530 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
1532 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1533 * the function successfully forwards the CVQ command, indicated
1534 * by a non-negative value of `dev_written`. Otherwise, it still
1536 * This function should only free the `elem` when it owns.
1538 if (dev_written
>= 0) {
1541 return dev_written
< 0 ? dev_written
: 0;
1544 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
1545 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
1549 * Probe if CVQ is isolated
1551 * @device_fd The vdpa device fd
1552 * @features Features offered by the device.
1553 * @cvq_index The control vq pair index
1555 * Returns <0 in case of failure, 0 if false and 1 if true.
1557 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
1558 int cvq_index
, Error
**errp
)
1560 uint64_t backend_features
;
1562 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1563 VIRTIO_CONFIG_S_DRIVER
;
1568 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
1569 if (unlikely(r
< 0)) {
1570 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
1574 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
1578 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1580 error_setg_errno(errp
, -r
, "Cannot set device status");
1584 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
1586 error_setg_errno(errp
, -r
, "Cannot set features");
1590 status
|= VIRTIO_CONFIG_S_FEATURES_OK
;
1591 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1593 error_setg_errno(errp
, -r
, "Cannot set device status");
1597 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
1598 if (unlikely(cvq_group
< 0)) {
1599 if (cvq_group
!= -ENOTSUP
) {
1605 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1606 * support ASID even if the parent driver does not. The CVQ cannot be
1607 * isolated in this case.
1615 for (int i
= 0; i
< cvq_index
; ++i
) {
1616 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
1617 if (unlikely(group
< 0)) {
1622 if (group
== (int64_t)cvq_group
) {
1632 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1636 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
1640 int queue_pair_index
,
1644 struct vhost_vdpa_iova_range iova_range
,
1646 VhostVDPAShared
*shared
,
1649 NetClientState
*nc
= NULL
;
1653 int cvq_isolated
= 0;
1656 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
1659 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
1660 queue_pair_index
* 2,
1662 if (unlikely(cvq_isolated
< 0)) {
1666 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
1669 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
1670 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1672 s
->vhost_vdpa
.index
= queue_pair_index
;
1673 s
->always_svq
= svq
;
1674 s
->migration_state
.notify
= NULL
;
1675 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
1676 if (queue_pair_index
== 0) {
1677 vhost_vdpa_net_valid_svq_features(features
,
1678 &s
->vhost_vdpa
.migration_blocker
);
1679 s
->vhost_vdpa
.shared
= g_new0(VhostVDPAShared
, 1);
1680 s
->vhost_vdpa
.shared
->device_fd
= vdpa_device_fd
;
1681 s
->vhost_vdpa
.shared
->iova_range
= iova_range
;
1682 s
->vhost_vdpa
.shared
->shadow_data
= svq
;
1683 } else if (!is_datapath
) {
1684 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1685 PROT_READ
| PROT_WRITE
,
1686 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1687 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1688 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
1691 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
1692 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
1693 s
->cvq_isolated
= cvq_isolated
;
1695 if (queue_pair_index
!= 0) {
1696 s
->vhost_vdpa
.shared
= shared
;
1699 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
1701 qemu_del_net_client(nc
);
1708 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
1710 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
1711 if (unlikely(ret
< 0)) {
1712 error_setg_errno(errp
, errno
,
1713 "Fail to query features from vhost-vDPA device");
1718 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
1719 int *has_cvq
, Error
**errp
)
1721 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
1722 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
1723 __virtio16
*max_queue_pairs
;
1726 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1732 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1733 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1734 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1735 config
->len
= sizeof(*max_queue_pairs
);
1737 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1739 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1743 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1745 return lduw_le_p(max_queue_pairs
);
1751 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1752 NetClientState
*peer
, Error
**errp
)
1754 const NetdevVhostVDPAOptions
*opts
;
1757 g_autofree NetClientState
**ncs
= NULL
;
1758 struct vhost_vdpa_iova_range iova_range
;
1760 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1762 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1763 opts
= &netdev
->u
.vhost_vdpa
;
1764 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1766 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1770 if (opts
->vhostdev
&& opts
->vhostfd
) {
1772 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1776 if (opts
->vhostdev
) {
1777 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1778 if (vdpa_device_fd
== -1) {
1783 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1784 if (vdpa_device_fd
== -1) {
1785 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1790 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1791 if (unlikely(r
< 0)) {
1795 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1797 if (queue_pairs
< 0) {
1798 qemu_close(vdpa_device_fd
);
1802 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1803 if (unlikely(r
< 0)) {
1804 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1809 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1813 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1815 for (i
= 0; i
< queue_pairs
; i
++) {
1816 VhostVDPAShared
*shared
= NULL
;
1819 shared
= DO_UPCAST(VhostVDPAState
, nc
, ncs
[0])->vhost_vdpa
.shared
;
1821 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1822 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1823 iova_range
, features
, shared
, errp
);
1829 VhostVDPAState
*s0
= DO_UPCAST(VhostVDPAState
, nc
, ncs
[0]);
1830 VhostVDPAShared
*shared
= s0
->vhost_vdpa
.shared
;
1832 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1833 vdpa_device_fd
, i
, 1, false,
1834 opts
->x_svq
, iova_range
, features
, shared
,
1844 for (i
--; i
>= 0; i
--) {
1845 qemu_del_net_client(ncs
[i
]);
1849 qemu_close(vdpa_device_fd
);