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
.device_fd
>= 0) {
240 qemu_close(s
->vhost_vdpa
.device_fd
);
241 s
->vhost_vdpa
.device_fd
= -1;
245 /** Dummy SetSteeringEBPF to support RSS for vhost-vdpa backend */
246 static bool vhost_vdpa_set_steering_ebpf(NetClientState
*nc
, int prog_fd
)
251 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
253 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
258 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
260 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
261 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
262 uint64_t features
= 0;
263 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
264 features
= vhost_net_get_features(s
->vhost_net
, features
);
265 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
269 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
272 const char *driver
= object_class_get_name(oc
);
274 if (!g_str_has_prefix(driver
, "virtio-net-")) {
275 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
282 /** Dummy receive in case qemu falls back to userland tap networking */
283 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
289 /** From any vdpa net client, get the netclient of the first queue pair */
290 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
292 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
293 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
295 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
298 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
300 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
303 int data_queue_pairs
, cvq
, r
;
305 /* We are only called on the first data vqs and only if x-svq is not set */
306 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
311 n
= VIRTIO_NET(vdev
);
312 if (!n
->vhost_started
) {
316 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
317 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
318 n
->max_ncs
- n
->max_queue_pairs
: 0;
320 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
321 * in the future and resume the device if read-only operations between
322 * suspend and reset goes wrong.
324 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
326 /* Start will check migration setup_or_active to configure or not SVQ */
327 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
328 if (unlikely(r
< 0)) {
329 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
333 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
335 MigrationState
*migration
= data
;
336 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
339 if (migration_in_setup(migration
)) {
340 vhost_vdpa_net_log_global_enable(s
, true);
341 } else if (migration_has_failed(migration
)) {
342 vhost_vdpa_net_log_global_enable(s
, false);
346 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
348 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
350 migration_add_notifier(&s
->migration_state
,
351 vdpa_net_migration_state_notifier
);
352 if (v
->shadow_vqs_enabled
) {
353 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
358 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
360 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
361 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
363 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
366 migration_is_setup_or_active(migrate_get_current()->state
)) {
367 v
->shadow_vqs_enabled
= true;
368 v
->shadow_data
= true;
370 v
->shadow_vqs_enabled
= false;
371 v
->shadow_data
= false;
375 vhost_vdpa_net_data_start_first(s
);
379 if (v
->shadow_vqs_enabled
) {
380 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
381 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
387 static int vhost_vdpa_net_data_load(NetClientState
*nc
)
389 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
390 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
391 bool has_cvq
= v
->dev
->vq_index_end
% 2;
397 for (int i
= 0; i
< v
->dev
->nvqs
; ++i
) {
398 vhost_vdpa_set_vring_ready(v
, i
+ v
->dev
->vq_index
);
403 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
405 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
406 struct vhost_dev
*dev
;
408 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
410 if (s
->vhost_vdpa
.index
== 0) {
411 migration_remove_notifier(&s
->migration_state
);
414 dev
= s
->vhost_vdpa
.dev
;
415 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
416 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
418 s
->vhost_vdpa
.iova_tree
= NULL
;
422 static NetClientInfo net_vhost_vdpa_info
= {
423 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
424 .size
= sizeof(VhostVDPAState
),
425 .receive
= vhost_vdpa_receive
,
426 .start
= vhost_vdpa_net_data_start
,
427 .load
= vhost_vdpa_net_data_load
,
428 .stop
= vhost_vdpa_net_client_stop
,
429 .cleanup
= vhost_vdpa_cleanup
,
430 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
431 .has_ufo
= vhost_vdpa_has_ufo
,
432 .check_peer_type
= vhost_vdpa_check_peer_type
,
433 .set_steering_ebpf
= vhost_vdpa_set_steering_ebpf
,
436 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
439 struct vhost_vring_state state
= {
442 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
444 if (unlikely(r
< 0)) {
446 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
453 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
457 struct vhost_vring_state asid
= {
463 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
464 if (unlikely(r
< 0)) {
465 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
466 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
471 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
473 VhostIOVATree
*tree
= v
->iova_tree
;
476 * No need to specify size or to look for more translations since
477 * this contiguous chunk was allocated by us.
479 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
481 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
484 if (unlikely(!map
)) {
485 error_report("Cannot locate expected map");
489 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
490 if (unlikely(r
!= 0)) {
491 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
494 vhost_iova_tree_remove(tree
, *map
);
497 /** Map CVQ buffer. */
498 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
504 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
506 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
507 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
508 if (unlikely(r
!= IOVA_OK
)) {
509 error_report("Cannot map injected element");
513 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
514 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
515 if (unlikely(r
< 0)) {
522 vhost_iova_tree_remove(v
->iova_tree
, map
);
526 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
528 VhostVDPAState
*s
, *s0
;
529 struct vhost_vdpa
*v
;
534 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
536 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
539 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
540 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
541 v
->shadow_vqs_enabled
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
542 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
544 if (s
->vhost_vdpa
.shadow_data
) {
545 /* SVQ is already configured for all virtqueues */
550 * If we early return in these cases SVQ will not be enabled. The migration
551 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
553 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
557 if (!s
->cvq_isolated
) {
561 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
,
562 v
->dev
->vq_index_end
- 1,
564 if (unlikely(cvq_group
< 0)) {
565 error_report_err(err
);
569 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
570 if (unlikely(r
< 0)) {
574 v
->shadow_vqs_enabled
= true;
575 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
578 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
582 if (s0
->vhost_vdpa
.iova_tree
) {
584 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
585 * simplicity, whether CVQ shares ASID with guest or not, because:
586 * - Memory listener need access to guest's memory addresses allocated
588 * - There should be plenty of IOVA address space for both ASID not to
589 * worry about collisions between them. Guest's translations are
590 * still validated with virtio virtqueue_pop so there is no risk for
591 * the guest to access memory that it shouldn't.
593 * To allocate a iova tree per ASID is doable but it complicates the
594 * code and it is not worth it for the moment.
596 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
598 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
602 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
603 vhost_vdpa_net_cvq_cmd_page_len(), false);
604 if (unlikely(r
< 0)) {
608 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
609 vhost_vdpa_net_cvq_cmd_page_len(), true);
610 if (unlikely(r
< 0)) {
611 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
617 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
619 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
621 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
623 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
624 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
625 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
628 vhost_vdpa_net_client_stop(nc
);
631 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
,
632 const struct iovec
*out_sg
, size_t out_num
,
633 const struct iovec
*in_sg
, size_t in_num
)
635 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
638 r
= vhost_svq_add(svq
, out_sg
, out_num
, in_sg
, in_num
, NULL
);
639 if (unlikely(r
!= 0)) {
640 if (unlikely(r
== -ENOSPC
)) {
641 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
650 * Convenience wrapper to poll SVQ for multiple control commands.
652 * Caller should hold the BQL when invoking this function, and should take
653 * the answer before SVQ pulls by itself when BQL is released.
655 static ssize_t
vhost_vdpa_net_svq_poll(VhostVDPAState
*s
, size_t cmds_in_flight
)
657 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
658 return vhost_svq_poll(svq
, cmds_in_flight
);
661 static void vhost_vdpa_net_load_cursor_reset(VhostVDPAState
*s
,
662 struct iovec
*out_cursor
,
663 struct iovec
*in_cursor
)
665 /* reset the cursor of the output buffer for the device */
666 out_cursor
->iov_base
= s
->cvq_cmd_out_buffer
;
667 out_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
669 /* reset the cursor of the in buffer for the device */
670 in_cursor
->iov_base
= s
->status
;
671 in_cursor
->iov_len
= vhost_vdpa_net_cvq_cmd_page_len();
675 * Poll SVQ for multiple pending control commands and check the device's ack.
677 * Caller should hold the BQL when invoking this function.
679 * @s: The VhostVDPAState
680 * @len: The length of the pending status shadow buffer
682 static ssize_t
vhost_vdpa_net_svq_flush(VhostVDPAState
*s
, size_t len
)
684 /* device uses a one-byte length ack for each control command */
685 ssize_t dev_written
= vhost_vdpa_net_svq_poll(s
, len
);
686 if (unlikely(dev_written
!= len
)) {
690 /* check the device's ack */
691 for (int i
= 0; i
< len
; ++i
) {
692 if (s
->status
[i
] != VIRTIO_NET_OK
) {
699 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
,
700 struct iovec
*out_cursor
,
701 struct iovec
*in_cursor
, uint8_t class,
702 uint8_t cmd
, const struct iovec
*data_sg
,
705 const struct virtio_net_ctrl_hdr ctrl
= {
709 size_t data_size
= iov_size(data_sg
, data_num
), cmd_size
;
710 struct iovec out
, in
;
712 unsigned dummy_cursor_iov_cnt
;
713 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
715 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
716 cmd_size
= sizeof(ctrl
) + data_size
;
717 if (vhost_svq_available_slots(svq
) < 2 ||
718 iov_size(out_cursor
, 1) < cmd_size
) {
720 * It is time to flush all pending control commands if SVQ is full
721 * or control commands shadow buffers are full.
723 * We can poll here since we've had BQL from the time
724 * we sent the descriptor.
726 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
->iov_base
-
728 if (unlikely(r
< 0)) {
732 vhost_vdpa_net_load_cursor_reset(s
, out_cursor
, in_cursor
);
735 /* pack the CVQ command header */
736 iov_from_buf(out_cursor
, 1, 0, &ctrl
, sizeof(ctrl
));
737 /* pack the CVQ command command-specific-data */
738 iov_to_buf(data_sg
, data_num
, 0,
739 out_cursor
->iov_base
+ sizeof(ctrl
), data_size
);
741 /* extract the required buffer from the cursor for output */
742 iov_copy(&out
, 1, out_cursor
, 1, 0, cmd_size
);
743 /* extract the required buffer from the cursor for input */
744 iov_copy(&in
, 1, in_cursor
, 1, 0, sizeof(*s
->status
));
746 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &in
, 1);
747 if (unlikely(r
< 0)) {
751 /* iterate the cursors */
752 dummy_cursor_iov_cnt
= 1;
753 iov_discard_front(&out_cursor
, &dummy_cursor_iov_cnt
, cmd_size
);
754 dummy_cursor_iov_cnt
= 1;
755 iov_discard_front(&in_cursor
, &dummy_cursor_iov_cnt
, sizeof(*s
->status
));
760 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
,
761 struct iovec
*out_cursor
,
762 struct iovec
*in_cursor
)
764 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
765 const struct iovec data
= {
766 .iov_base
= (void *)n
->mac
,
767 .iov_len
= sizeof(n
->mac
),
769 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
771 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
773 if (unlikely(r
< 0)) {
779 * According to VirtIO standard, "The device MUST have an
780 * empty MAC filtering table on reset.".
782 * Therefore, there is no need to send this CVQ command if the
783 * driver also sets an empty MAC filter table, which aligns with
784 * the device's defaults.
786 * Note that the device's defaults can mismatch the driver's
787 * configuration only at live migration.
789 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
) ||
790 n
->mac_table
.in_use
== 0) {
794 uint32_t uni_entries
= n
->mac_table
.first_multi
,
795 uni_macs_size
= uni_entries
* ETH_ALEN
,
796 mul_entries
= n
->mac_table
.in_use
- uni_entries
,
797 mul_macs_size
= mul_entries
* ETH_ALEN
;
798 struct virtio_net_ctrl_mac uni
= {
799 .entries
= cpu_to_le32(uni_entries
),
801 struct virtio_net_ctrl_mac mul
= {
802 .entries
= cpu_to_le32(mul_entries
),
804 const struct iovec data
[] = {
807 .iov_len
= sizeof(uni
),
809 .iov_base
= n
->mac_table
.macs
,
810 .iov_len
= uni_macs_size
,
813 .iov_len
= sizeof(mul
),
815 .iov_base
= &n
->mac_table
.macs
[uni_macs_size
],
816 .iov_len
= mul_macs_size
,
819 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
821 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
822 data
, ARRAY_SIZE(data
));
823 if (unlikely(r
< 0)) {
830 static int vhost_vdpa_net_load_rss(VhostVDPAState
*s
, const VirtIONet
*n
,
831 struct iovec
*out_cursor
,
832 struct iovec
*in_cursor
, bool do_rss
)
834 struct virtio_net_rss_config cfg
= {};
836 g_autofree
uint16_t *table
= NULL
;
839 * According to VirtIO standard, "Initially the device has all hash
840 * types disabled and reports only VIRTIO_NET_HASH_REPORT_NONE.".
842 * Therefore, there is no need to send this CVQ command if the
843 * driver disables the all hash types, which aligns with
844 * the device's defaults.
846 * Note that the device's defaults can mismatch the driver's
847 * configuration only at live migration.
849 if (!n
->rss_data
.enabled
||
850 n
->rss_data
.hash_types
== VIRTIO_NET_HASH_REPORT_NONE
) {
854 table
= g_malloc_n(n
->rss_data
.indirections_len
,
855 sizeof(n
->rss_data
.indirections_table
[0]));
856 cfg
.hash_types
= cpu_to_le32(n
->rss_data
.hash_types
);
860 * According to VirtIO standard, "Number of entries in indirection_table
861 * is (indirection_table_mask + 1)".
863 cfg
.indirection_table_mask
= cpu_to_le16(n
->rss_data
.indirections_len
-
865 cfg
.unclassified_queue
= cpu_to_le16(n
->rss_data
.default_queue
);
866 for (int i
= 0; i
< n
->rss_data
.indirections_len
; ++i
) {
867 table
[i
] = cpu_to_le16(n
->rss_data
.indirections_table
[i
]);
869 cfg
.max_tx_vq
= cpu_to_le16(n
->curr_queue_pairs
);
872 * According to VirtIO standard, "Field reserved MUST contain zeroes.
873 * It is defined to make the structure to match the layout of
874 * virtio_net_rss_config structure, defined in 5.1.6.5.7.".
876 * Therefore, we need to zero the fields in
877 * struct virtio_net_rss_config, which corresponds to the
878 * `reserved` field in struct virtio_net_hash_config.
880 * Note that all other fields are zeroed at their definitions,
881 * except for the `indirection_table` field, where the actual data
882 * is stored in the `table` variable to ensure compatibility
883 * with RSS case. Therefore, we need to zero the `table` variable here.
889 * Considering that virtio_net_handle_rss() currently does not restore
890 * the hash key length parsed from the CVQ command sent from the guest
891 * into n->rss_data and uses the maximum key length in other code, so
892 * we also employ the maximum key length here.
894 cfg
.hash_key_length
= sizeof(n
->rss_data
.key
);
896 const struct iovec data
[] = {
899 .iov_len
= offsetof(struct virtio_net_rss_config
,
903 .iov_len
= n
->rss_data
.indirections_len
*
904 sizeof(n
->rss_data
.indirections_table
[0]),
906 .iov_base
= &cfg
.max_tx_vq
,
907 .iov_len
= offsetof(struct virtio_net_rss_config
, hash_key_data
) -
908 offsetof(struct virtio_net_rss_config
, max_tx_vq
),
910 .iov_base
= (void *)n
->rss_data
.key
,
911 .iov_len
= sizeof(n
->rss_data
.key
),
915 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
917 do_rss
? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
:
918 VIRTIO_NET_CTRL_MQ_HASH_CONFIG
,
919 data
, ARRAY_SIZE(data
));
920 if (unlikely(r
< 0)) {
927 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
929 struct iovec
*out_cursor
,
930 struct iovec
*in_cursor
)
932 struct virtio_net_ctrl_mq mq
;
935 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
939 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
940 const struct iovec data
= {
942 .iov_len
= sizeof(mq
),
944 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
946 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
,
948 if (unlikely(r
< 0)) {
952 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_RSS
)) {
953 /* load the receive-side scaling state */
954 r
= vhost_vdpa_net_load_rss(s
, n
, out_cursor
, in_cursor
, true);
955 if (unlikely(r
< 0)) {
958 } else if (virtio_vdev_has_feature(&n
->parent_obj
,
959 VIRTIO_NET_F_HASH_REPORT
)) {
960 /* load the hash calculation state */
961 r
= vhost_vdpa_net_load_rss(s
, n
, out_cursor
, in_cursor
, false);
962 if (unlikely(r
< 0)) {
970 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
972 struct iovec
*out_cursor
,
973 struct iovec
*in_cursor
)
978 if (!virtio_vdev_has_feature(&n
->parent_obj
,
979 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
983 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
985 * According to VirtIO standard, "Upon feature negotiation
986 * corresponding offload gets enabled to preserve
987 * backward compatibility.".
989 * Therefore, there is no need to send this CVQ command if the
990 * driver also enables all supported offloads, which aligns with
991 * the device's defaults.
993 * Note that the device's defaults can mismatch the driver's
994 * configuration only at live migration.
999 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
1000 const struct iovec data
= {
1001 .iov_base
= &offloads
,
1002 .iov_len
= sizeof(offloads
),
1004 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1005 VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
1006 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
1008 if (unlikely(r
< 0)) {
1015 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState
*s
,
1016 struct iovec
*out_cursor
,
1017 struct iovec
*in_cursor
,
1021 const struct iovec data
= {
1023 .iov_len
= sizeof(on
),
1027 r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1028 VIRTIO_NET_CTRL_RX
, cmd
, &data
, 1);
1029 if (unlikely(r
< 0)) {
1036 static int vhost_vdpa_net_load_rx(VhostVDPAState
*s
,
1038 struct iovec
*out_cursor
,
1039 struct iovec
*in_cursor
)
1043 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
)) {
1048 * According to virtio_net_reset(), device turns promiscuous mode
1051 * Additionally, according to VirtIO standard, "Since there are
1052 * no guarantees, it can use a hash filter or silently switch to
1053 * allmulti or promiscuous mode if it is given too many addresses.".
1054 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
1055 * non-multicast MAC addresses, indicating that promiscuous mode
1056 * should be enabled.
1058 * Therefore, QEMU should only send this CVQ command if the
1059 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
1060 * which sets promiscuous mode on, different from the device's defaults.
1062 * Note that the device's defaults can mismatch the driver's
1063 * configuration only at live migration.
1065 if (!n
->mac_table
.uni_overflow
&& !n
->promisc
) {
1066 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1067 VIRTIO_NET_CTRL_RX_PROMISC
, 0);
1068 if (unlikely(r
< 0)) {
1074 * According to virtio_net_reset(), device turns all-multicast mode
1077 * According to VirtIO standard, "Since there are no guarantees,
1078 * it can use a hash filter or silently switch to allmulti or
1079 * promiscuous mode if it is given too many addresses.". QEMU marks
1080 * `n->mac_table.multi_overflow` if guest sets too many
1081 * non-multicast MAC addresses.
1083 * Therefore, QEMU should only send this CVQ command if the
1084 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
1085 * which sets all-multicast mode on, different from the device's defaults.
1087 * Note that the device's defaults can mismatch the driver's
1088 * configuration only at live migration.
1090 if (n
->mac_table
.multi_overflow
|| n
->allmulti
) {
1091 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1092 VIRTIO_NET_CTRL_RX_ALLMULTI
, 1);
1093 if (unlikely(r
< 0)) {
1098 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX_EXTRA
)) {
1103 * According to virtio_net_reset(), device turns all-unicast mode
1106 * Therefore, QEMU should only send this CVQ command if the driver
1107 * sets all-unicast 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_ALLUNI
, 1);
1121 * According to virtio_net_reset(), device turns non-multicast mode
1124 * Therefore, QEMU should only send this CVQ command if the driver
1125 * sets non-multicast 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_NOMULTI
, 1);
1139 * According to virtio_net_reset(), device turns non-unicast mode
1142 * Therefore, QEMU should only send this CVQ command if the driver
1143 * sets non-unicast 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_NOUNI
, 1);
1157 * According to virtio_net_reset(), device turns non-broadcast mode
1160 * Therefore, QEMU should only send this CVQ command if the driver
1161 * sets non-broadcast mode on, different from the device's defaults.
1163 * Note that the device's defaults can mismatch the driver's
1164 * configuration only at live migration.
1167 r
= vhost_vdpa_net_load_rx_mode(s
, out_cursor
, in_cursor
,
1168 VIRTIO_NET_CTRL_RX_NOBCAST
, 1);
1177 static int vhost_vdpa_net_load_single_vlan(VhostVDPAState
*s
,
1179 struct iovec
*out_cursor
,
1180 struct iovec
*in_cursor
,
1183 const struct iovec data
= {
1185 .iov_len
= sizeof(vid
),
1187 ssize_t r
= vhost_vdpa_net_load_cmd(s
, out_cursor
, in_cursor
,
1188 VIRTIO_NET_CTRL_VLAN
,
1189 VIRTIO_NET_CTRL_VLAN_ADD
,
1191 if (unlikely(r
< 0)) {
1198 static int vhost_vdpa_net_load_vlan(VhostVDPAState
*s
,
1200 struct iovec
*out_cursor
,
1201 struct iovec
*in_cursor
)
1205 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_VLAN
)) {
1209 for (int i
= 0; i
< MAX_VLAN
>> 5; i
++) {
1210 for (int j
= 0; n
->vlans
[i
] && j
<= 0x1f; j
++) {
1211 if (n
->vlans
[i
] & (1U << j
)) {
1212 r
= vhost_vdpa_net_load_single_vlan(s
, n
, out_cursor
,
1213 in_cursor
, (i
<< 5) + j
);
1214 if (unlikely(r
!= 0)) {
1224 static int vhost_vdpa_net_cvq_load(NetClientState
*nc
)
1226 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1227 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
1230 struct iovec out_cursor
, in_cursor
;
1232 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1234 vhost_vdpa_set_vring_ready(v
, v
->dev
->vq_index
);
1236 if (v
->shadow_vqs_enabled
) {
1237 n
= VIRTIO_NET(v
->dev
->vdev
);
1238 vhost_vdpa_net_load_cursor_reset(s
, &out_cursor
, &in_cursor
);
1239 r
= vhost_vdpa_net_load_mac(s
, n
, &out_cursor
, &in_cursor
);
1240 if (unlikely(r
< 0)) {
1243 r
= vhost_vdpa_net_load_mq(s
, n
, &out_cursor
, &in_cursor
);
1247 r
= vhost_vdpa_net_load_offloads(s
, n
, &out_cursor
, &in_cursor
);
1251 r
= vhost_vdpa_net_load_rx(s
, n
, &out_cursor
, &in_cursor
);
1255 r
= vhost_vdpa_net_load_vlan(s
, n
, &out_cursor
, &in_cursor
);
1261 * We need to poll and check all pending device's used buffers.
1263 * We can poll here since we've had BQL from the time
1264 * we sent the descriptor.
1266 r
= vhost_vdpa_net_svq_flush(s
, in_cursor
.iov_base
- (void *)s
->status
);
1272 for (int i
= 0; i
< v
->dev
->vq_index
; ++i
) {
1273 vhost_vdpa_set_vring_ready(v
, i
);
1279 static NetClientInfo net_vhost_vdpa_cvq_info
= {
1280 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
1281 .size
= sizeof(VhostVDPAState
),
1282 .receive
= vhost_vdpa_receive
,
1283 .start
= vhost_vdpa_net_cvq_start
,
1284 .load
= vhost_vdpa_net_cvq_load
,
1285 .stop
= vhost_vdpa_net_cvq_stop
,
1286 .cleanup
= vhost_vdpa_cleanup
,
1287 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
1288 .has_ufo
= vhost_vdpa_has_ufo
,
1289 .check_peer_type
= vhost_vdpa_check_peer_type
,
1290 .set_steering_ebpf
= vhost_vdpa_set_steering_ebpf
,
1294 * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1297 * Considering that QEMU cannot send the entire filter table to the
1298 * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1299 * command to enable promiscuous mode to receive all packets,
1300 * according to VirtIO standard, "Since there are no guarantees,
1301 * it can use a hash filter or silently switch to allmulti or
1302 * promiscuous mode if it is given too many addresses.".
1304 * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1305 * marks `n->mac_table.x_overflow` accordingly, it should have
1306 * the same effect on the device model to receive
1307 * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1308 * The same applies to multicast MAC addresses.
1310 * Therefore, QEMU can provide the device model with a fake
1311 * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1312 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1313 * MAC addresses. This ensures that the device model marks
1314 * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1315 * allowing all packets to be received, which aligns with the
1316 * state of the vdpa device.
1318 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState
*s
,
1319 VirtQueueElement
*elem
,
1321 const struct iovec
*in
)
1323 struct virtio_net_ctrl_mac mac_data
, *mac_ptr
;
1324 struct virtio_net_ctrl_hdr
*hdr_ptr
;
1329 /* parse the non-multicast MAC address entries from CVQ command */
1330 cursor
= sizeof(*hdr_ptr
);
1331 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1332 &mac_data
, sizeof(mac_data
));
1333 if (unlikely(r
!= sizeof(mac_data
))) {
1335 * If the CVQ command is invalid, we should simulate the vdpa device
1336 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1338 *s
->status
= VIRTIO_NET_ERR
;
1339 return sizeof(*s
->status
);
1341 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1343 /* parse the multicast MAC address entries from CVQ command */
1344 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1345 &mac_data
, sizeof(mac_data
));
1346 if (r
!= sizeof(mac_data
)) {
1348 * If the CVQ command is invalid, we should simulate the vdpa device
1349 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1351 *s
->status
= VIRTIO_NET_ERR
;
1352 return sizeof(*s
->status
);
1354 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1356 /* validate the CVQ command */
1357 if (iov_size(elem
->out_sg
, elem
->out_num
) != cursor
) {
1359 * If the CVQ command is invalid, we should simulate the vdpa device
1360 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1362 *s
->status
= VIRTIO_NET_ERR
;
1363 return sizeof(*s
->status
);
1367 * According to VirtIO standard, "Since there are no guarantees,
1368 * it can use a hash filter or silently switch to allmulti or
1369 * promiscuous mode if it is given too many addresses.".
1371 * Therefore, considering that QEMU is unable to send the entire
1372 * filter table to the vdpa device, it should send the
1373 * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1375 hdr_ptr
= out
->iov_base
;
1376 out
->iov_len
= sizeof(*hdr_ptr
) + sizeof(on
);
1378 hdr_ptr
->class = VIRTIO_NET_CTRL_RX
;
1379 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_RX_PROMISC
;
1380 iov_from_buf(out
, 1, sizeof(*hdr_ptr
), &on
, sizeof(on
));
1381 r
= vhost_vdpa_net_cvq_add(s
, out
, 1, in
, 1);
1382 if (unlikely(r
< 0)) {
1387 * We can poll here since we've had BQL from the time
1388 * we sent the descriptor.
1390 r
= vhost_vdpa_net_svq_poll(s
, 1);
1391 if (unlikely(r
< sizeof(*s
->status
))) {
1394 if (*s
->status
!= VIRTIO_NET_OK
) {
1395 return sizeof(*s
->status
);
1399 * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1400 * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1401 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1402 * multicast MAC addresses.
1404 * By doing so, the device model can mark `n->mac_table.uni_overflow`
1405 * and `n->mac_table.multi_overflow`, enabling all packets to be
1406 * received, which aligns with the state of the vdpa device.
1409 uint32_t fake_uni_entries
= MAC_TABLE_ENTRIES
+ 1,
1410 fake_mul_entries
= MAC_TABLE_ENTRIES
+ 1,
1411 fake_cvq_size
= sizeof(struct virtio_net_ctrl_hdr
) +
1412 sizeof(mac_data
) + fake_uni_entries
* ETH_ALEN
+
1413 sizeof(mac_data
) + fake_mul_entries
* ETH_ALEN
;
1415 assert(fake_cvq_size
< vhost_vdpa_net_cvq_cmd_page_len());
1416 out
->iov_len
= fake_cvq_size
;
1418 /* pack the header for fake CVQ command */
1419 hdr_ptr
= out
->iov_base
+ cursor
;
1420 hdr_ptr
->class = VIRTIO_NET_CTRL_MAC
;
1421 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_MAC_TABLE_SET
;
1422 cursor
+= sizeof(*hdr_ptr
);
1425 * Pack the non-multicast MAC addresses part for fake CVQ command.
1427 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1428 * addresses provided in CVQ command. Therefore, only the entries
1429 * field need to be prepared in the CVQ command.
1431 mac_ptr
= out
->iov_base
+ cursor
;
1432 mac_ptr
->entries
= cpu_to_le32(fake_uni_entries
);
1433 cursor
+= sizeof(*mac_ptr
) + fake_uni_entries
* ETH_ALEN
;
1436 * Pack the multicast MAC addresses part for fake CVQ command.
1438 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1439 * addresses provided in CVQ command. Therefore, only the entries
1440 * field need to be prepared in the CVQ command.
1442 mac_ptr
= out
->iov_base
+ cursor
;
1443 mac_ptr
->entries
= cpu_to_le32(fake_mul_entries
);
1446 * Simulating QEMU poll a vdpa device used buffer
1447 * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1449 return sizeof(*s
->status
);
1453 * Validate and copy control virtqueue commands.
1455 * Following QEMU guidelines, we offer a copy of the buffers to the device to
1456 * prevent TOCTOU bugs.
1458 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
1459 VirtQueueElement
*elem
,
1462 VhostVDPAState
*s
= opaque
;
1464 const struct virtio_net_ctrl_hdr
*ctrl
;
1465 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1466 /* Out buffer sent to both the vdpa device and the device model */
1467 struct iovec out
= {
1468 .iov_base
= s
->cvq_cmd_out_buffer
,
1470 /* in buffer used for device model */
1471 const struct iovec model_in
= {
1472 .iov_base
= &status
,
1473 .iov_len
= sizeof(status
),
1475 /* in buffer used for vdpa device */
1476 const struct iovec vdpa_in
= {
1477 .iov_base
= s
->status
,
1478 .iov_len
= sizeof(*s
->status
),
1480 ssize_t dev_written
= -EINVAL
;
1482 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1483 s
->cvq_cmd_out_buffer
,
1484 vhost_vdpa_net_cvq_cmd_page_len());
1486 ctrl
= s
->cvq_cmd_out_buffer
;
1487 if (ctrl
->class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1489 * Guest announce capability is emulated by qemu, so don't forward to
1492 dev_written
= sizeof(status
);
1493 *s
->status
= VIRTIO_NET_OK
;
1494 } else if (unlikely(ctrl
->class == VIRTIO_NET_CTRL_MAC
&&
1495 ctrl
->cmd
== VIRTIO_NET_CTRL_MAC_TABLE_SET
&&
1496 iov_size(elem
->out_sg
, elem
->out_num
) > out
.iov_len
)) {
1498 * Due to the size limitation of the out buffer sent to the vdpa device,
1499 * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1500 * MAC addresses set by the driver for the filter table can cause
1501 * truncation of the CVQ command in QEMU. As a result, the vdpa device
1502 * rejects the flawed CVQ command.
1504 * Therefore, QEMU must handle this situation instead of sending
1505 * the CVQ command directly.
1507 dev_written
= vhost_vdpa_net_excessive_mac_filter_cvq_add(s
, elem
,
1509 if (unlikely(dev_written
< 0)) {
1514 r
= vhost_vdpa_net_cvq_add(s
, &out
, 1, &vdpa_in
, 1);
1515 if (unlikely(r
< 0)) {
1521 * We can poll here since we've had BQL from the time
1522 * we sent the descriptor.
1524 dev_written
= vhost_vdpa_net_svq_poll(s
, 1);
1527 if (unlikely(dev_written
< sizeof(status
))) {
1528 error_report("Insufficient written data (%zu)", dev_written
);
1532 if (*s
->status
!= VIRTIO_NET_OK
) {
1536 status
= VIRTIO_NET_ERR
;
1537 virtio_net_handle_ctrl_iov(svq
->vdev
, &model_in
, 1, &out
, 1);
1538 if (status
!= VIRTIO_NET_OK
) {
1539 error_report("Bad CVQ processing in model");
1543 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
1545 if (unlikely(in_len
< sizeof(status
))) {
1546 error_report("Bad device CVQ written length");
1548 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
1550 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1551 * the function successfully forwards the CVQ command, indicated
1552 * by a non-negative value of `dev_written`. Otherwise, it still
1554 * This function should only free the `elem` when it owns.
1556 if (dev_written
>= 0) {
1559 return dev_written
< 0 ? dev_written
: 0;
1562 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
1563 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
1567 * Probe if CVQ is isolated
1569 * @device_fd The vdpa device fd
1570 * @features Features offered by the device.
1571 * @cvq_index The control vq pair index
1573 * Returns <0 in case of failure, 0 if false and 1 if true.
1575 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
1576 int cvq_index
, Error
**errp
)
1578 uint64_t backend_features
;
1580 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1581 VIRTIO_CONFIG_S_DRIVER
;
1586 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
1587 if (unlikely(r
< 0)) {
1588 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
1592 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
1596 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1598 error_setg_errno(errp
, -r
, "Cannot set device status");
1602 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
1604 error_setg_errno(errp
, -r
, "Cannot set features");
1608 status
|= VIRTIO_CONFIG_S_FEATURES_OK
;
1609 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1611 error_setg_errno(errp
, -r
, "Cannot set device status");
1615 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
1616 if (unlikely(cvq_group
< 0)) {
1617 if (cvq_group
!= -ENOTSUP
) {
1623 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1624 * support ASID even if the parent driver does not. The CVQ cannot be
1625 * isolated in this case.
1633 for (int i
= 0; i
< cvq_index
; ++i
) {
1634 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
1635 if (unlikely(group
< 0)) {
1640 if (group
== (int64_t)cvq_group
) {
1650 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1654 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
1658 int queue_pair_index
,
1662 struct vhost_vdpa_iova_range iova_range
,
1666 NetClientState
*nc
= NULL
;
1670 int cvq_isolated
= 0;
1673 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
1676 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
1677 queue_pair_index
* 2,
1679 if (unlikely(cvq_isolated
< 0)) {
1683 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
1686 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
1687 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1689 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
1690 s
->vhost_vdpa
.index
= queue_pair_index
;
1691 s
->always_svq
= svq
;
1692 s
->migration_state
.notify
= NULL
;
1693 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
1694 s
->vhost_vdpa
.iova_range
= iova_range
;
1695 s
->vhost_vdpa
.shadow_data
= svq
;
1696 if (queue_pair_index
== 0) {
1697 vhost_vdpa_net_valid_svq_features(features
,
1698 &s
->vhost_vdpa
.migration_blocker
);
1699 } else if (!is_datapath
) {
1700 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1701 PROT_READ
| PROT_WRITE
,
1702 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1703 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1704 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
1707 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
1708 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
1709 s
->cvq_isolated
= cvq_isolated
;
1711 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
1713 qemu_del_net_client(nc
);
1719 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
1721 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
1722 if (unlikely(ret
< 0)) {
1723 error_setg_errno(errp
, errno
,
1724 "Fail to query features from vhost-vDPA device");
1729 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
1730 int *has_cvq
, Error
**errp
)
1732 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
1733 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
1734 __virtio16
*max_queue_pairs
;
1737 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1743 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1744 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1745 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1746 config
->len
= sizeof(*max_queue_pairs
);
1748 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1750 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1754 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1756 return lduw_le_p(max_queue_pairs
);
1762 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1763 NetClientState
*peer
, Error
**errp
)
1765 const NetdevVhostVDPAOptions
*opts
;
1768 g_autofree NetClientState
**ncs
= NULL
;
1769 struct vhost_vdpa_iova_range iova_range
;
1771 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1773 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1774 opts
= &netdev
->u
.vhost_vdpa
;
1775 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1777 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1781 if (opts
->vhostdev
&& opts
->vhostfd
) {
1783 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1787 if (opts
->vhostdev
) {
1788 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1789 if (vdpa_device_fd
== -1) {
1794 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1795 if (vdpa_device_fd
== -1) {
1796 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1801 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1802 if (unlikely(r
< 0)) {
1806 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1808 if (queue_pairs
< 0) {
1809 qemu_close(vdpa_device_fd
);
1813 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1814 if (unlikely(r
< 0)) {
1815 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1820 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1824 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1826 for (i
= 0; i
< queue_pairs
; i
++) {
1827 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1828 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1829 iova_range
, features
, errp
);
1835 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1836 vdpa_device_fd
, i
, 1, false,
1837 opts
->x_svq
, iova_range
, features
, errp
);
1846 for (i
--; i
>= 0; i
--) {
1847 qemu_del_net_client(ncs
[i
]);
1851 qemu_close(vdpa_device_fd
);