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_HASH_REPORT
,
79 VIRTIO_NET_F_HOST_ECN
,
80 VIRTIO_NET_F_HOST_TSO4
,
81 VIRTIO_NET_F_HOST_TSO6
,
82 VIRTIO_NET_F_HOST_UFO
,
84 VIRTIO_NET_F_MRG_RXBUF
,
88 VIRTIO_RING_F_EVENT_IDX
,
89 VIRTIO_RING_F_INDIRECT_DESC
,
91 /* VHOST_INVALID_FEATURE_BIT should always be the last entry */
92 VHOST_INVALID_FEATURE_BIT
95 /** Supported device specific feature bits with SVQ */
96 static const uint64_t vdpa_svq_device_features
=
97 BIT_ULL(VIRTIO_NET_F_CSUM
) |
98 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM
) |
99 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
) |
100 BIT_ULL(VIRTIO_NET_F_MTU
) |
101 BIT_ULL(VIRTIO_NET_F_MAC
) |
102 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4
) |
103 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6
) |
104 BIT_ULL(VIRTIO_NET_F_GUEST_ECN
) |
105 BIT_ULL(VIRTIO_NET_F_GUEST_UFO
) |
106 BIT_ULL(VIRTIO_NET_F_HOST_TSO4
) |
107 BIT_ULL(VIRTIO_NET_F_HOST_TSO6
) |
108 BIT_ULL(VIRTIO_NET_F_HOST_ECN
) |
109 BIT_ULL(VIRTIO_NET_F_HOST_UFO
) |
110 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF
) |
111 BIT_ULL(VIRTIO_NET_F_STATUS
) |
112 BIT_ULL(VIRTIO_NET_F_CTRL_VQ
) |
113 BIT_ULL(VIRTIO_NET_F_CTRL_RX
) |
114 BIT_ULL(VIRTIO_NET_F_CTRL_RX_EXTRA
) |
115 BIT_ULL(VIRTIO_NET_F_MQ
) |
116 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
117 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
118 /* VHOST_F_LOG_ALL is exposed by SVQ */
119 BIT_ULL(VHOST_F_LOG_ALL
) |
120 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
121 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
122 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
124 #define VHOST_VDPA_NET_CVQ_ASID 1
126 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
128 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
129 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
133 static size_t vhost_vdpa_net_cvq_cmd_len(void)
136 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
137 * In buffer is always 1 byte, so it should fit here
139 return sizeof(struct virtio_net_ctrl_hdr
) +
140 2 * sizeof(struct virtio_net_ctrl_mac
) +
141 MAC_TABLE_ENTRIES
* ETH_ALEN
;
144 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
146 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
149 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
151 uint64_t invalid_dev_features
=
152 features
& ~vdpa_svq_device_features
&
153 /* Transport are all accepted at this point */
154 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
155 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
157 if (invalid_dev_features
) {
158 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
159 invalid_dev_features
);
163 return vhost_svq_valid_features(features
, errp
);
166 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
170 struct vhost_dev
*hdev
;
172 hdev
= (struct vhost_dev
*)&net
->dev
;
173 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
174 if (device_id
!= VIRTIO_ID_NET
) {
180 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
181 int queue_pair_index
, int nvqs
)
183 VhostNetOptions options
;
184 struct vhost_net
*net
= NULL
;
188 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
189 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
190 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
191 options
.net_backend
= ncs
;
193 options
.busyloop_timeout
= 0;
196 net
= vhost_net_init(&options
);
198 error_report("failed to init vhost_net for queue");
202 ret
= vhost_vdpa_net_check_device_id(net
);
208 vhost_net_cleanup(net
);
214 static void vhost_vdpa_cleanup(NetClientState
*nc
)
216 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
219 * If a peer NIC is attached, do not cleanup anything.
220 * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
221 * when the guest is shutting down.
223 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
226 munmap(s
->cvq_cmd_out_buffer
, vhost_vdpa_net_cvq_cmd_page_len());
227 munmap(s
->status
, vhost_vdpa_net_cvq_cmd_page_len());
229 vhost_net_cleanup(s
->vhost_net
);
230 g_free(s
->vhost_net
);
233 if (s
->vhost_vdpa
.device_fd
>= 0) {
234 qemu_close(s
->vhost_vdpa
.device_fd
);
235 s
->vhost_vdpa
.device_fd
= -1;
239 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
241 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
246 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
248 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
249 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
250 uint64_t features
= 0;
251 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
252 features
= vhost_net_get_features(s
->vhost_net
, features
);
253 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
257 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
260 const char *driver
= object_class_get_name(oc
);
262 if (!g_str_has_prefix(driver
, "virtio-net-")) {
263 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
270 /** Dummy receive in case qemu falls back to userland tap networking */
271 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
277 /** From any vdpa net client, get the netclient of the first queue pair */
278 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
280 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
281 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
283 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
286 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
288 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
291 int data_queue_pairs
, cvq
, r
;
293 /* We are only called on the first data vqs and only if x-svq is not set */
294 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
299 n
= VIRTIO_NET(vdev
);
300 if (!n
->vhost_started
) {
304 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
305 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
306 n
->max_ncs
- n
->max_queue_pairs
: 0;
308 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
309 * in the future and resume the device if read-only operations between
310 * suspend and reset goes wrong.
312 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
314 /* Start will check migration setup_or_active to configure or not SVQ */
315 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
316 if (unlikely(r
< 0)) {
317 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
321 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
323 MigrationState
*migration
= data
;
324 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
327 if (migration_in_setup(migration
)) {
328 vhost_vdpa_net_log_global_enable(s
, true);
329 } else if (migration_has_failed(migration
)) {
330 vhost_vdpa_net_log_global_enable(s
, false);
334 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
336 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
338 add_migration_state_change_notifier(&s
->migration_state
);
339 if (v
->shadow_vqs_enabled
) {
340 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
345 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
347 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
348 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
350 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
353 migration_is_setup_or_active(migrate_get_current()->state
)) {
354 v
->shadow_vqs_enabled
= true;
355 v
->shadow_data
= true;
357 v
->shadow_vqs_enabled
= false;
358 v
->shadow_data
= false;
362 vhost_vdpa_net_data_start_first(s
);
366 if (v
->shadow_vqs_enabled
) {
367 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
368 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
374 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
376 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
377 struct vhost_dev
*dev
;
379 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
381 if (s
->vhost_vdpa
.index
== 0) {
382 remove_migration_state_change_notifier(&s
->migration_state
);
385 dev
= s
->vhost_vdpa
.dev
;
386 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
387 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
391 static NetClientInfo net_vhost_vdpa_info
= {
392 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
393 .size
= sizeof(VhostVDPAState
),
394 .receive
= vhost_vdpa_receive
,
395 .start
= vhost_vdpa_net_data_start
,
396 .stop
= vhost_vdpa_net_client_stop
,
397 .cleanup
= vhost_vdpa_cleanup
,
398 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
399 .has_ufo
= vhost_vdpa_has_ufo
,
400 .check_peer_type
= vhost_vdpa_check_peer_type
,
403 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
406 struct vhost_vring_state state
= {
409 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
411 if (unlikely(r
< 0)) {
413 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
420 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
424 struct vhost_vring_state asid
= {
430 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
431 if (unlikely(r
< 0)) {
432 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
433 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
438 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
440 VhostIOVATree
*tree
= v
->iova_tree
;
443 * No need to specify size or to look for more translations since
444 * this contiguous chunk was allocated by us.
446 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
448 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
451 if (unlikely(!map
)) {
452 error_report("Cannot locate expected map");
456 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
457 if (unlikely(r
!= 0)) {
458 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
461 vhost_iova_tree_remove(tree
, *map
);
464 /** Map CVQ buffer. */
465 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
471 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
473 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
474 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
475 if (unlikely(r
!= IOVA_OK
)) {
476 error_report("Cannot map injected element");
480 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
481 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
482 if (unlikely(r
< 0)) {
489 vhost_iova_tree_remove(v
->iova_tree
, map
);
493 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
495 VhostVDPAState
*s
, *s0
;
496 struct vhost_vdpa
*v
;
501 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
503 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
506 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
507 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
508 v
->shadow_vqs_enabled
= s
->always_svq
;
509 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
511 if (s
->vhost_vdpa
.shadow_data
) {
512 /* SVQ is already configured for all virtqueues */
517 * If we early return in these cases SVQ will not be enabled. The migration
518 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
520 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
524 if (!s
->cvq_isolated
) {
528 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
,
529 v
->dev
->vq_index_end
- 1,
531 if (unlikely(cvq_group
< 0)) {
532 error_report_err(err
);
536 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
537 if (unlikely(r
< 0)) {
541 v
->shadow_vqs_enabled
= true;
542 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
545 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
549 if (s0
->vhost_vdpa
.iova_tree
) {
551 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
552 * simplicity, whether CVQ shares ASID with guest or not, because:
553 * - Memory listener need access to guest's memory addresses allocated
555 * - There should be plenty of IOVA address space for both ASID not to
556 * worry about collisions between them. Guest's translations are
557 * still validated with virtio virtqueue_pop so there is no risk for
558 * the guest to access memory that it shouldn't.
560 * To allocate a iova tree per ASID is doable but it complicates the
561 * code and it is not worth it for the moment.
563 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
565 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
569 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
570 vhost_vdpa_net_cvq_cmd_page_len(), false);
571 if (unlikely(r
< 0)) {
575 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
576 vhost_vdpa_net_cvq_cmd_page_len(), true);
577 if (unlikely(r
< 0)) {
578 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
584 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
586 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
588 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
590 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
591 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
592 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
595 vhost_vdpa_net_client_stop(nc
);
598 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
, size_t out_len
,
601 /* Buffers for the device */
602 const struct iovec out
= {
603 .iov_base
= s
->cvq_cmd_out_buffer
,
606 const struct iovec in
= {
607 .iov_base
= s
->status
,
608 .iov_len
= sizeof(virtio_net_ctrl_ack
),
610 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
613 r
= vhost_svq_add(svq
, &out
, 1, &in
, 1, NULL
);
614 if (unlikely(r
!= 0)) {
615 if (unlikely(r
== -ENOSPC
)) {
616 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
623 * We can poll here since we've had BQL from the time we sent the
624 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
625 * when BQL is released
627 return vhost_svq_poll(svq
);
630 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
, uint8_t class,
631 uint8_t cmd
, const struct iovec
*data_sg
,
634 const struct virtio_net_ctrl_hdr ctrl
= {
638 size_t data_size
= iov_size(data_sg
, data_num
);
640 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
642 /* pack the CVQ command header */
643 memcpy(s
->cvq_cmd_out_buffer
, &ctrl
, sizeof(ctrl
));
645 /* pack the CVQ command command-specific-data */
646 iov_to_buf(data_sg
, data_num
, 0,
647 s
->cvq_cmd_out_buffer
+ sizeof(ctrl
), data_size
);
649 return vhost_vdpa_net_cvq_add(s
, data_size
+ sizeof(ctrl
),
650 sizeof(virtio_net_ctrl_ack
));
653 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
)
655 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
656 const struct iovec data
= {
657 .iov_base
= (void *)n
->mac
,
658 .iov_len
= sizeof(n
->mac
),
660 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MAC
,
661 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
663 if (unlikely(dev_written
< 0)) {
666 if (*s
->status
!= VIRTIO_NET_OK
) {
672 * According to VirtIO standard, "The device MUST have an
673 * empty MAC filtering table on reset.".
675 * Therefore, there is no need to send this CVQ command if the
676 * driver also sets an empty MAC filter table, which aligns with
677 * the device's defaults.
679 * Note that the device's defaults can mismatch the driver's
680 * configuration only at live migration.
682 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
) ||
683 n
->mac_table
.in_use
== 0) {
687 uint32_t uni_entries
= n
->mac_table
.first_multi
,
688 uni_macs_size
= uni_entries
* ETH_ALEN
,
689 mul_entries
= n
->mac_table
.in_use
- uni_entries
,
690 mul_macs_size
= mul_entries
* ETH_ALEN
;
691 struct virtio_net_ctrl_mac uni
= {
692 .entries
= cpu_to_le32(uni_entries
),
694 struct virtio_net_ctrl_mac mul
= {
695 .entries
= cpu_to_le32(mul_entries
),
697 const struct iovec data
[] = {
700 .iov_len
= sizeof(uni
),
702 .iov_base
= n
->mac_table
.macs
,
703 .iov_len
= uni_macs_size
,
706 .iov_len
= sizeof(mul
),
708 .iov_base
= &n
->mac_table
.macs
[uni_macs_size
],
709 .iov_len
= mul_macs_size
,
712 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
,
714 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
715 data
, ARRAY_SIZE(data
));
716 if (unlikely(dev_written
< 0)) {
719 if (*s
->status
!= VIRTIO_NET_OK
) {
726 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
729 struct virtio_net_ctrl_mq mq
;
732 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
736 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
737 const struct iovec data
= {
739 .iov_len
= sizeof(mq
),
741 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MQ
,
742 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
,
744 if (unlikely(dev_written
< 0)) {
747 if (*s
->status
!= VIRTIO_NET_OK
) {
754 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
760 if (!virtio_vdev_has_feature(&n
->parent_obj
,
761 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
765 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
767 * According to VirtIO standard, "Upon feature negotiation
768 * corresponding offload gets enabled to preserve
769 * backward compatibility.".
771 * Therefore, there is no need to send this CVQ command if the
772 * driver also enables all supported offloads, which aligns with
773 * the device's defaults.
775 * Note that the device's defaults can mismatch the driver's
776 * configuration only at live migration.
781 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
782 const struct iovec data
= {
783 .iov_base
= &offloads
,
784 .iov_len
= sizeof(offloads
),
786 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
787 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
789 if (unlikely(dev_written
< 0)) {
792 if (*s
->status
!= VIRTIO_NET_OK
) {
799 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState
*s
,
803 const struct iovec data
= {
805 .iov_len
= sizeof(on
),
807 return vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_RX
,
811 static int vhost_vdpa_net_load_rx(VhostVDPAState
*s
,
816 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
)) {
821 * According to virtio_net_reset(), device turns promiscuous mode
824 * Addtionally, according to VirtIO standard, "Since there are
825 * no guarantees, it can use a hash filter or silently switch to
826 * allmulti or promiscuous mode if it is given too many addresses.".
827 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
828 * non-multicast MAC addresses, indicating that promiscuous mode
831 * Therefore, QEMU should only send this CVQ command if the
832 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
833 * which sets promiscuous mode on, different from the device's defaults.
835 * Note that the device's defaults can mismatch the driver's
836 * configuration only at live migration.
838 if (!n
->mac_table
.uni_overflow
&& !n
->promisc
) {
839 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
840 VIRTIO_NET_CTRL_RX_PROMISC
, 0);
841 if (unlikely(dev_written
< 0)) {
844 if (*s
->status
!= VIRTIO_NET_OK
) {
850 * According to virtio_net_reset(), device turns all-multicast mode
853 * According to VirtIO standard, "Since there are no guarantees,
854 * it can use a hash filter or silently switch to allmulti or
855 * promiscuous mode if it is given too many addresses.". QEMU marks
856 * `n->mac_table.multi_overflow` if guest sets too many
857 * non-multicast MAC addresses.
859 * Therefore, QEMU should only send this CVQ command if the
860 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
861 * which sets all-multicast mode on, different from the device's defaults.
863 * Note that the device's defaults can mismatch the driver's
864 * configuration only at live migration.
866 if (n
->mac_table
.multi_overflow
|| n
->allmulti
) {
867 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
868 VIRTIO_NET_CTRL_RX_ALLMULTI
, 1);
869 if (unlikely(dev_written
< 0)) {
872 if (*s
->status
!= VIRTIO_NET_OK
) {
877 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX_EXTRA
)) {
882 * According to virtio_net_reset(), device turns all-unicast mode
885 * Therefore, QEMU should only send this CVQ command if the driver
886 * sets all-unicast mode on, different from the device's defaults.
888 * Note that the device's defaults can mismatch the driver's
889 * configuration only at live migration.
892 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
893 VIRTIO_NET_CTRL_RX_ALLUNI
, 1);
894 if (dev_written
< 0) {
897 if (*s
->status
!= VIRTIO_NET_OK
) {
903 * According to virtio_net_reset(), device turns non-multicast mode
906 * Therefore, QEMU should only send this CVQ command if the driver
907 * sets non-multicast mode on, different from the device's defaults.
909 * Note that the device's defaults can mismatch the driver's
910 * configuration only at live migration.
913 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
914 VIRTIO_NET_CTRL_RX_NOMULTI
, 1);
915 if (dev_written
< 0) {
918 if (*s
->status
!= VIRTIO_NET_OK
) {
924 * According to virtio_net_reset(), device turns non-unicast mode
927 * Therefore, QEMU should only send this CVQ command if the driver
928 * sets non-unicast mode on, different from the device's defaults.
930 * Note that the device's defaults can mismatch the driver's
931 * configuration only at live migration.
934 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
935 VIRTIO_NET_CTRL_RX_NOUNI
, 1);
936 if (dev_written
< 0) {
939 if (*s
->status
!= VIRTIO_NET_OK
) {
945 * According to virtio_net_reset(), device turns non-broadcast mode
948 * Therefore, QEMU should only send this CVQ command if the driver
949 * sets non-broadcast mode on, different from the device's defaults.
951 * Note that the device's defaults can mismatch the driver's
952 * configuration only at live migration.
955 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
956 VIRTIO_NET_CTRL_RX_NOBCAST
, 1);
957 if (dev_written
< 0) {
960 if (*s
->status
!= VIRTIO_NET_OK
) {
968 static int vhost_vdpa_net_load(NetClientState
*nc
)
970 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
971 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
975 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
977 if (!v
->shadow_vqs_enabled
) {
981 n
= VIRTIO_NET(v
->dev
->vdev
);
982 r
= vhost_vdpa_net_load_mac(s
, n
);
983 if (unlikely(r
< 0)) {
986 r
= vhost_vdpa_net_load_mq(s
, n
);
990 r
= vhost_vdpa_net_load_offloads(s
, n
);
994 r
= vhost_vdpa_net_load_rx(s
, n
);
1002 static NetClientInfo net_vhost_vdpa_cvq_info
= {
1003 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
1004 .size
= sizeof(VhostVDPAState
),
1005 .receive
= vhost_vdpa_receive
,
1006 .start
= vhost_vdpa_net_cvq_start
,
1007 .load
= vhost_vdpa_net_load
,
1008 .stop
= vhost_vdpa_net_cvq_stop
,
1009 .cleanup
= vhost_vdpa_cleanup
,
1010 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
1011 .has_ufo
= vhost_vdpa_has_ufo
,
1012 .check_peer_type
= vhost_vdpa_check_peer_type
,
1016 * Forward the excessive VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command to
1019 * Considering that QEMU cannot send the entire filter table to the
1020 * vdpa device, it should send the VIRTIO_NET_CTRL_RX_PROMISC CVQ
1021 * command to enable promiscuous mode to receive all packets,
1022 * according to VirtIO standard, "Since there are no guarantees,
1023 * it can use a hash filter or silently switch to allmulti or
1024 * promiscuous mode if it is given too many addresses.".
1026 * Since QEMU ignores MAC addresses beyond `MAC_TABLE_ENTRIES` and
1027 * marks `n->mac_table.x_overflow` accordingly, it should have
1028 * the same effect on the device model to receive
1029 * (`MAC_TABLE_ENTRIES` + 1) or more non-multicast MAC addresses.
1030 * The same applies to multicast MAC addresses.
1032 * Therefore, QEMU can provide the device model with a fake
1033 * VIRTIO_NET_CTRL_MAC_TABLE_SET command with (`MAC_TABLE_ENTRIES` + 1)
1034 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1) multicast
1035 * MAC addresses. This ensures that the device model marks
1036 * `n->mac_table.uni_overflow` and `n->mac_table.multi_overflow`,
1037 * allowing all packets to be received, which aligns with the
1038 * state of the vdpa device.
1040 static int vhost_vdpa_net_excessive_mac_filter_cvq_add(VhostVDPAState
*s
,
1041 VirtQueueElement
*elem
,
1044 struct virtio_net_ctrl_mac mac_data
, *mac_ptr
;
1045 struct virtio_net_ctrl_hdr
*hdr_ptr
;
1049 /* parse the non-multicast MAC address entries from CVQ command */
1050 cursor
= sizeof(*hdr_ptr
);
1051 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1052 &mac_data
, sizeof(mac_data
));
1053 if (unlikely(r
!= sizeof(mac_data
))) {
1055 * If the CVQ command is invalid, we should simulate the vdpa device
1056 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1058 *s
->status
= VIRTIO_NET_ERR
;
1059 return sizeof(*s
->status
);
1061 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1063 /* parse the multicast MAC address entries from CVQ command */
1064 r
= iov_to_buf(elem
->out_sg
, elem
->out_num
, cursor
,
1065 &mac_data
, sizeof(mac_data
));
1066 if (r
!= sizeof(mac_data
)) {
1068 * If the CVQ command is invalid, we should simulate the vdpa device
1069 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1071 *s
->status
= VIRTIO_NET_ERR
;
1072 return sizeof(*s
->status
);
1074 cursor
+= sizeof(mac_data
) + le32_to_cpu(mac_data
.entries
) * ETH_ALEN
;
1076 /* validate the CVQ command */
1077 if (iov_size(elem
->out_sg
, elem
->out_num
) != cursor
) {
1079 * If the CVQ command is invalid, we should simulate the vdpa device
1080 * to reject the VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1082 *s
->status
= VIRTIO_NET_ERR
;
1083 return sizeof(*s
->status
);
1087 * According to VirtIO standard, "Since there are no guarantees,
1088 * it can use a hash filter or silently switch to allmulti or
1089 * promiscuous mode if it is given too many addresses.".
1091 * Therefore, considering that QEMU is unable to send the entire
1092 * filter table to the vdpa device, it should send the
1093 * VIRTIO_NET_CTRL_RX_PROMISC CVQ command to enable promiscuous mode
1095 r
= vhost_vdpa_net_load_rx_mode(s
, VIRTIO_NET_CTRL_RX_PROMISC
, 1);
1096 if (unlikely(r
< 0)) {
1099 if (*s
->status
!= VIRTIO_NET_OK
) {
1100 return sizeof(*s
->status
);
1104 * QEMU should also send a fake VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ
1105 * command to the device model, including (`MAC_TABLE_ENTRIES` + 1)
1106 * non-multicast MAC addresses and (`MAC_TABLE_ENTRIES` + 1)
1107 * multicast MAC addresses.
1109 * By doing so, the device model can mark `n->mac_table.uni_overflow`
1110 * and `n->mac_table.multi_overflow`, enabling all packets to be
1111 * received, which aligns with the state of the vdpa device.
1114 uint32_t fake_uni_entries
= MAC_TABLE_ENTRIES
+ 1,
1115 fake_mul_entries
= MAC_TABLE_ENTRIES
+ 1,
1116 fake_cvq_size
= sizeof(struct virtio_net_ctrl_hdr
) +
1117 sizeof(mac_data
) + fake_uni_entries
* ETH_ALEN
+
1118 sizeof(mac_data
) + fake_mul_entries
* ETH_ALEN
;
1120 assert(fake_cvq_size
< vhost_vdpa_net_cvq_cmd_page_len());
1121 out
->iov_len
= fake_cvq_size
;
1123 /* pack the header for fake CVQ command */
1124 hdr_ptr
= out
->iov_base
+ cursor
;
1125 hdr_ptr
->class = VIRTIO_NET_CTRL_MAC
;
1126 hdr_ptr
->cmd
= VIRTIO_NET_CTRL_MAC_TABLE_SET
;
1127 cursor
+= sizeof(*hdr_ptr
);
1130 * Pack the non-multicast MAC addresses part for fake CVQ command.
1132 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1133 * addresses provieded in CVQ command. Therefore, only the entries
1134 * field need to be prepared in the CVQ command.
1136 mac_ptr
= out
->iov_base
+ cursor
;
1137 mac_ptr
->entries
= cpu_to_le32(fake_uni_entries
);
1138 cursor
+= sizeof(*mac_ptr
) + fake_uni_entries
* ETH_ALEN
;
1141 * Pack the multicast MAC addresses part for fake CVQ command.
1143 * According to virtio_net_handle_mac(), QEMU doesn't verify the MAC
1144 * addresses provieded in CVQ command. Therefore, only the entries
1145 * field need to be prepared in the CVQ command.
1147 mac_ptr
= out
->iov_base
+ cursor
;
1148 mac_ptr
->entries
= cpu_to_le32(fake_mul_entries
);
1151 * Simulating QEMU poll a vdpa device used buffer
1152 * for VIRTIO_NET_CTRL_MAC_TABLE_SET CVQ command
1154 return sizeof(*s
->status
);
1158 * Validate and copy control virtqueue commands.
1160 * Following QEMU guidelines, we offer a copy of the buffers to the device to
1161 * prevent TOCTOU bugs.
1163 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
1164 VirtQueueElement
*elem
,
1167 VhostVDPAState
*s
= opaque
;
1169 const struct virtio_net_ctrl_hdr
*ctrl
;
1170 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
1171 /* Out buffer sent to both the vdpa device and the device model */
1172 struct iovec out
= {
1173 .iov_base
= s
->cvq_cmd_out_buffer
,
1175 /* in buffer used for device model */
1176 const struct iovec in
= {
1177 .iov_base
= &status
,
1178 .iov_len
= sizeof(status
),
1180 ssize_t dev_written
= -EINVAL
;
1182 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
1183 s
->cvq_cmd_out_buffer
,
1184 vhost_vdpa_net_cvq_cmd_page_len());
1186 ctrl
= s
->cvq_cmd_out_buffer
;
1187 if (ctrl
->class == VIRTIO_NET_CTRL_ANNOUNCE
) {
1189 * Guest announce capability is emulated by qemu, so don't forward to
1192 dev_written
= sizeof(status
);
1193 *s
->status
= VIRTIO_NET_OK
;
1194 } else if (unlikely(ctrl
->class == VIRTIO_NET_CTRL_MAC
&&
1195 ctrl
->cmd
== VIRTIO_NET_CTRL_MAC_TABLE_SET
&&
1196 iov_size(elem
->out_sg
, elem
->out_num
) > out
.iov_len
)) {
1198 * Due to the size limitation of the out buffer sent to the vdpa device,
1199 * which is determined by vhost_vdpa_net_cvq_cmd_page_len(), excessive
1200 * MAC addresses set by the driver for the filter table can cause
1201 * truncation of the CVQ command in QEMU. As a result, the vdpa device
1202 * rejects the flawed CVQ command.
1204 * Therefore, QEMU must handle this situation instead of sending
1205 * the CVQ command direclty.
1207 dev_written
= vhost_vdpa_net_excessive_mac_filter_cvq_add(s
, elem
,
1209 if (unlikely(dev_written
< 0)) {
1213 dev_written
= vhost_vdpa_net_cvq_add(s
, out
.iov_len
, sizeof(status
));
1214 if (unlikely(dev_written
< 0)) {
1219 if (unlikely(dev_written
< sizeof(status
))) {
1220 error_report("Insufficient written data (%zu)", dev_written
);
1224 if (*s
->status
!= VIRTIO_NET_OK
) {
1228 status
= VIRTIO_NET_ERR
;
1229 virtio_net_handle_ctrl_iov(svq
->vdev
, &in
, 1, &out
, 1);
1230 if (status
!= VIRTIO_NET_OK
) {
1231 error_report("Bad CVQ processing in model");
1235 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
1237 if (unlikely(in_len
< sizeof(status
))) {
1238 error_report("Bad device CVQ written length");
1240 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
1242 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
1243 * the function successfully forwards the CVQ command, indicated
1244 * by a non-negative value of `dev_written`. Otherwise, it still
1246 * This function should only free the `elem` when it owns.
1248 if (dev_written
>= 0) {
1251 return dev_written
< 0 ? dev_written
: 0;
1254 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
1255 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
1259 * Probe if CVQ is isolated
1261 * @device_fd The vdpa device fd
1262 * @features Features offered by the device.
1263 * @cvq_index The control vq pair index
1265 * Returns <0 in case of failure, 0 if false and 1 if true.
1267 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
1268 int cvq_index
, Error
**errp
)
1270 uint64_t backend_features
;
1272 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1273 VIRTIO_CONFIG_S_DRIVER
|
1274 VIRTIO_CONFIG_S_FEATURES_OK
;
1279 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
1280 if (unlikely(r
< 0)) {
1281 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
1285 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
1289 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
1291 error_setg_errno(errp
, errno
, "Cannot set features");
1294 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1296 error_setg_errno(errp
, -r
, "Cannot set device features");
1300 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
1301 if (unlikely(cvq_group
< 0)) {
1302 if (cvq_group
!= -ENOTSUP
) {
1308 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1309 * support ASID even if the parent driver does not. The CVQ cannot be
1310 * isolated in this case.
1318 for (int i
= 0; i
< cvq_index
; ++i
) {
1319 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
1320 if (unlikely(group
< 0)) {
1325 if (group
== (int64_t)cvq_group
) {
1335 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1339 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
1343 int queue_pair_index
,
1347 struct vhost_vdpa_iova_range iova_range
,
1351 NetClientState
*nc
= NULL
;
1358 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
1361 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
1362 queue_pair_index
* 2,
1364 if (unlikely(cvq_isolated
< 0)) {
1368 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
1371 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
1372 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1374 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
1375 s
->vhost_vdpa
.index
= queue_pair_index
;
1376 s
->always_svq
= svq
;
1377 s
->migration_state
.notify
= vdpa_net_migration_state_notifier
;
1378 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
1379 s
->vhost_vdpa
.iova_range
= iova_range
;
1380 s
->vhost_vdpa
.shadow_data
= svq
;
1381 if (queue_pair_index
== 0) {
1382 vhost_vdpa_net_valid_svq_features(features
,
1383 &s
->vhost_vdpa
.migration_blocker
);
1384 } else if (!is_datapath
) {
1385 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1386 PROT_READ
| PROT_WRITE
,
1387 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1388 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1389 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
1392 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
1393 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
1394 s
->cvq_isolated
= cvq_isolated
;
1397 * TODO: We cannot migrate devices with CVQ and no x-svq enabled as
1398 * there is no way to set the device state (MAC, MQ, etc) before
1399 * starting the datapath.
1401 * Migration blocker ownership now belongs to s->vhost_vdpa.
1404 error_setg(&s
->vhost_vdpa
.migration_blocker
,
1405 "net vdpa cannot migrate with CVQ feature");
1408 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
1410 qemu_del_net_client(nc
);
1416 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
1418 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
1419 if (unlikely(ret
< 0)) {
1420 error_setg_errno(errp
, errno
,
1421 "Fail to query features from vhost-vDPA device");
1426 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
1427 int *has_cvq
, Error
**errp
)
1429 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
1430 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
1431 __virtio16
*max_queue_pairs
;
1434 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1440 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1441 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1442 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1443 config
->len
= sizeof(*max_queue_pairs
);
1445 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1447 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1451 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1453 return lduw_le_p(max_queue_pairs
);
1459 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1460 NetClientState
*peer
, Error
**errp
)
1462 const NetdevVhostVDPAOptions
*opts
;
1465 g_autofree NetClientState
**ncs
= NULL
;
1466 struct vhost_vdpa_iova_range iova_range
;
1468 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1470 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1471 opts
= &netdev
->u
.vhost_vdpa
;
1472 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1474 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1478 if (opts
->vhostdev
&& opts
->vhostfd
) {
1480 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1484 if (opts
->vhostdev
) {
1485 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1486 if (vdpa_device_fd
== -1) {
1491 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1492 if (vdpa_device_fd
== -1) {
1493 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1498 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1499 if (unlikely(r
< 0)) {
1503 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1505 if (queue_pairs
< 0) {
1506 qemu_close(vdpa_device_fd
);
1510 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1511 if (unlikely(r
< 0)) {
1512 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1517 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1521 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1523 for (i
= 0; i
< queue_pairs
; i
++) {
1524 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1525 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1526 iova_range
, features
, errp
);
1532 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1533 vdpa_device_fd
, i
, 1, false,
1534 opts
->x_svq
, iova_range
, features
, errp
);
1543 for (i
--; i
>= 0; i
--) {
1544 qemu_del_net_client(ncs
[i
]);
1548 qemu_close(vdpa_device_fd
);