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_MQ
) |
114 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
115 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
116 /* VHOST_F_LOG_ALL is exposed by SVQ */
117 BIT_ULL(VHOST_F_LOG_ALL
) |
118 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
119 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
120 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
122 #define VHOST_VDPA_NET_CVQ_ASID 1
124 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
126 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
127 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
131 static size_t vhost_vdpa_net_cvq_cmd_len(void)
134 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
135 * In buffer is always 1 byte, so it should fit here
137 return sizeof(struct virtio_net_ctrl_hdr
) +
138 2 * sizeof(struct virtio_net_ctrl_mac
) +
139 MAC_TABLE_ENTRIES
* ETH_ALEN
;
142 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
144 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
147 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
149 uint64_t invalid_dev_features
=
150 features
& ~vdpa_svq_device_features
&
151 /* Transport are all accepted at this point */
152 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
153 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
155 if (invalid_dev_features
) {
156 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
157 invalid_dev_features
);
161 return vhost_svq_valid_features(features
, errp
);
164 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
168 struct vhost_dev
*hdev
;
170 hdev
= (struct vhost_dev
*)&net
->dev
;
171 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
172 if (device_id
!= VIRTIO_ID_NET
) {
178 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
179 int queue_pair_index
, int nvqs
)
181 VhostNetOptions options
;
182 struct vhost_net
*net
= NULL
;
186 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
187 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
188 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
189 options
.net_backend
= ncs
;
191 options
.busyloop_timeout
= 0;
194 net
= vhost_net_init(&options
);
196 error_report("failed to init vhost_net for queue");
200 ret
= vhost_vdpa_net_check_device_id(net
);
206 vhost_net_cleanup(net
);
212 static void vhost_vdpa_cleanup(NetClientState
*nc
)
214 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
217 * If a peer NIC is attached, do not cleanup anything.
218 * Cleanup will happen as a part of qemu_cleanup() -> net_cleanup()
219 * when the guest is shutting down.
221 if (nc
->peer
&& nc
->peer
->info
->type
== NET_CLIENT_DRIVER_NIC
) {
224 munmap(s
->cvq_cmd_out_buffer
, vhost_vdpa_net_cvq_cmd_page_len());
225 munmap(s
->status
, vhost_vdpa_net_cvq_cmd_page_len());
227 vhost_net_cleanup(s
->vhost_net
);
228 g_free(s
->vhost_net
);
231 if (s
->vhost_vdpa
.device_fd
>= 0) {
232 qemu_close(s
->vhost_vdpa
.device_fd
);
233 s
->vhost_vdpa
.device_fd
= -1;
237 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
239 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
244 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
246 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
247 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
248 uint64_t features
= 0;
249 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
250 features
= vhost_net_get_features(s
->vhost_net
, features
);
251 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
255 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
258 const char *driver
= object_class_get_name(oc
);
260 if (!g_str_has_prefix(driver
, "virtio-net-")) {
261 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
268 /** Dummy receive in case qemu falls back to userland tap networking */
269 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
275 /** From any vdpa net client, get the netclient of the first queue pair */
276 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
278 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
279 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
281 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
284 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
286 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
289 int data_queue_pairs
, cvq
, r
;
291 /* We are only called on the first data vqs and only if x-svq is not set */
292 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
297 n
= VIRTIO_NET(vdev
);
298 if (!n
->vhost_started
) {
302 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
303 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
304 n
->max_ncs
- n
->max_queue_pairs
: 0;
306 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
307 * in the future and resume the device if read-only operations between
308 * suspend and reset goes wrong.
310 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
312 /* Start will check migration setup_or_active to configure or not SVQ */
313 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
314 if (unlikely(r
< 0)) {
315 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
319 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
321 MigrationState
*migration
= data
;
322 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
325 if (migration_in_setup(migration
)) {
326 vhost_vdpa_net_log_global_enable(s
, true);
327 } else if (migration_has_failed(migration
)) {
328 vhost_vdpa_net_log_global_enable(s
, false);
332 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
334 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
336 add_migration_state_change_notifier(&s
->migration_state
);
337 if (v
->shadow_vqs_enabled
) {
338 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
343 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
345 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
346 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
348 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
351 migration_is_setup_or_active(migrate_get_current()->state
)) {
352 v
->shadow_vqs_enabled
= true;
353 v
->shadow_data
= true;
355 v
->shadow_vqs_enabled
= false;
356 v
->shadow_data
= false;
360 vhost_vdpa_net_data_start_first(s
);
364 if (v
->shadow_vqs_enabled
) {
365 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
366 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
372 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
374 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
375 struct vhost_dev
*dev
;
377 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
379 if (s
->vhost_vdpa
.index
== 0) {
380 remove_migration_state_change_notifier(&s
->migration_state
);
383 dev
= s
->vhost_vdpa
.dev
;
384 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
385 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
389 static NetClientInfo net_vhost_vdpa_info
= {
390 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
391 .size
= sizeof(VhostVDPAState
),
392 .receive
= vhost_vdpa_receive
,
393 .start
= vhost_vdpa_net_data_start
,
394 .stop
= vhost_vdpa_net_client_stop
,
395 .cleanup
= vhost_vdpa_cleanup
,
396 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
397 .has_ufo
= vhost_vdpa_has_ufo
,
398 .check_peer_type
= vhost_vdpa_check_peer_type
,
401 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
404 struct vhost_vring_state state
= {
407 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
409 if (unlikely(r
< 0)) {
411 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
418 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
422 struct vhost_vring_state asid
= {
428 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
429 if (unlikely(r
< 0)) {
430 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
431 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
436 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
438 VhostIOVATree
*tree
= v
->iova_tree
;
441 * No need to specify size or to look for more translations since
442 * this contiguous chunk was allocated by us.
444 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
446 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
449 if (unlikely(!map
)) {
450 error_report("Cannot locate expected map");
454 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
455 if (unlikely(r
!= 0)) {
456 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
459 vhost_iova_tree_remove(tree
, *map
);
462 /** Map CVQ buffer. */
463 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
469 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
471 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
472 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
473 if (unlikely(r
!= IOVA_OK
)) {
474 error_report("Cannot map injected element");
478 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
479 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
480 if (unlikely(r
< 0)) {
487 vhost_iova_tree_remove(v
->iova_tree
, map
);
491 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
493 VhostVDPAState
*s
, *s0
;
494 struct vhost_vdpa
*v
;
499 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
501 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
504 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
505 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
506 v
->shadow_vqs_enabled
= s
->always_svq
;
507 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
509 if (s
->vhost_vdpa
.shadow_data
) {
510 /* SVQ is already configured for all virtqueues */
515 * If we early return in these cases SVQ will not be enabled. The migration
516 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
518 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
522 if (!s
->cvq_isolated
) {
526 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
,
527 v
->dev
->vq_index_end
- 1,
529 if (unlikely(cvq_group
< 0)) {
530 error_report_err(err
);
534 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
535 if (unlikely(r
< 0)) {
539 v
->shadow_vqs_enabled
= true;
540 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
543 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
547 if (s0
->vhost_vdpa
.iova_tree
) {
549 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
550 * simplicity, whether CVQ shares ASID with guest or not, because:
551 * - Memory listener need access to guest's memory addresses allocated
553 * - There should be plenty of IOVA address space for both ASID not to
554 * worry about collisions between them. Guest's translations are
555 * still validated with virtio virtqueue_pop so there is no risk for
556 * the guest to access memory that it shouldn't.
558 * To allocate a iova tree per ASID is doable but it complicates the
559 * code and it is not worth it for the moment.
561 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
563 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
567 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
568 vhost_vdpa_net_cvq_cmd_page_len(), false);
569 if (unlikely(r
< 0)) {
573 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
574 vhost_vdpa_net_cvq_cmd_page_len(), true);
575 if (unlikely(r
< 0)) {
576 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
582 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
584 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
586 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
588 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
589 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
590 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
593 vhost_vdpa_net_client_stop(nc
);
596 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
, size_t out_len
,
599 /* Buffers for the device */
600 const struct iovec out
= {
601 .iov_base
= s
->cvq_cmd_out_buffer
,
604 const struct iovec in
= {
605 .iov_base
= s
->status
,
606 .iov_len
= sizeof(virtio_net_ctrl_ack
),
608 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
611 r
= vhost_svq_add(svq
, &out
, 1, &in
, 1, NULL
);
612 if (unlikely(r
!= 0)) {
613 if (unlikely(r
== -ENOSPC
)) {
614 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
621 * We can poll here since we've had BQL from the time we sent the
622 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
623 * when BQL is released
625 return vhost_svq_poll(svq
);
628 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
, uint8_t class,
629 uint8_t cmd
, const struct iovec
*data_sg
,
632 const struct virtio_net_ctrl_hdr ctrl
= {
636 size_t data_size
= iov_size(data_sg
, data_num
);
638 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
640 /* pack the CVQ command header */
641 memcpy(s
->cvq_cmd_out_buffer
, &ctrl
, sizeof(ctrl
));
643 /* pack the CVQ command command-specific-data */
644 iov_to_buf(data_sg
, data_num
, 0,
645 s
->cvq_cmd_out_buffer
+ sizeof(ctrl
), data_size
);
647 return vhost_vdpa_net_cvq_add(s
, data_size
+ sizeof(ctrl
),
648 sizeof(virtio_net_ctrl_ack
));
651 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
)
653 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
654 const struct iovec data
= {
655 .iov_base
= (void *)n
->mac
,
656 .iov_len
= sizeof(n
->mac
),
658 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MAC
,
659 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
661 if (unlikely(dev_written
< 0)) {
664 if (*s
->status
!= VIRTIO_NET_OK
) {
670 * According to VirtIO standard, "The device MUST have an
671 * empty MAC filtering table on reset.".
673 * Therefore, there is no need to send this CVQ command if the
674 * driver also sets an empty MAC filter table, which aligns with
675 * the device's defaults.
677 * Note that the device's defaults can mismatch the driver's
678 * configuration only at live migration.
680 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
) ||
681 n
->mac_table
.in_use
== 0) {
685 uint32_t uni_entries
= n
->mac_table
.first_multi
,
686 uni_macs_size
= uni_entries
* ETH_ALEN
,
687 mul_entries
= n
->mac_table
.in_use
- uni_entries
,
688 mul_macs_size
= mul_entries
* ETH_ALEN
;
689 struct virtio_net_ctrl_mac uni
= {
690 .entries
= cpu_to_le32(uni_entries
),
692 struct virtio_net_ctrl_mac mul
= {
693 .entries
= cpu_to_le32(mul_entries
),
695 const struct iovec data
[] = {
698 .iov_len
= sizeof(uni
),
700 .iov_base
= n
->mac_table
.macs
,
701 .iov_len
= uni_macs_size
,
704 .iov_len
= sizeof(mul
),
706 .iov_base
= &n
->mac_table
.macs
[uni_macs_size
],
707 .iov_len
= mul_macs_size
,
710 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
,
712 VIRTIO_NET_CTRL_MAC_TABLE_SET
,
713 data
, ARRAY_SIZE(data
));
714 if (unlikely(dev_written
< 0)) {
717 if (*s
->status
!= VIRTIO_NET_OK
) {
724 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
727 struct virtio_net_ctrl_mq mq
;
730 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
734 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
735 const struct iovec data
= {
737 .iov_len
= sizeof(mq
),
739 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MQ
,
740 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
,
742 if (unlikely(dev_written
< 0)) {
745 if (*s
->status
!= VIRTIO_NET_OK
) {
752 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
758 if (!virtio_vdev_has_feature(&n
->parent_obj
,
759 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
763 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
765 * According to VirtIO standard, "Upon feature negotiation
766 * corresponding offload gets enabled to preserve
767 * backward compatibility.".
769 * Therefore, there is no need to send this CVQ command if the
770 * driver also enables all supported offloads, which aligns with
771 * the device's defaults.
773 * Note that the device's defaults can mismatch the driver's
774 * configuration only at live migration.
779 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
780 const struct iovec data
= {
781 .iov_base
= &offloads
,
782 .iov_len
= sizeof(offloads
),
784 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
785 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
787 if (unlikely(dev_written
< 0)) {
790 if (*s
->status
!= VIRTIO_NET_OK
) {
797 static int vhost_vdpa_net_load_rx_mode(VhostVDPAState
*s
,
801 const struct iovec data
= {
803 .iov_len
= sizeof(on
),
805 return vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_RX
,
809 static int vhost_vdpa_net_load_rx(VhostVDPAState
*s
,
814 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_RX
)) {
819 * According to virtio_net_reset(), device turns promiscuous mode
822 * Addtionally, according to VirtIO standard, "Since there are
823 * no guarantees, it can use a hash filter or silently switch to
824 * allmulti or promiscuous mode if it is given too many addresses.".
825 * QEMU marks `n->mac_table.uni_overflow` if guest sets too many
826 * non-multicast MAC addresses, indicating that promiscuous mode
829 * Therefore, QEMU should only send this CVQ command if the
830 * `n->mac_table.uni_overflow` is not marked and `n->promisc` is off,
831 * which sets promiscuous mode on, different from the device's defaults.
833 * Note that the device's defaults can mismatch the driver's
834 * configuration only at live migration.
836 if (!n
->mac_table
.uni_overflow
&& !n
->promisc
) {
837 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
838 VIRTIO_NET_CTRL_RX_PROMISC
, 0);
839 if (unlikely(dev_written
< 0)) {
842 if (*s
->status
!= VIRTIO_NET_OK
) {
848 * According to virtio_net_reset(), device turns all-multicast mode
851 * According to VirtIO standard, "Since there are no guarantees,
852 * it can use a hash filter or silently switch to allmulti or
853 * promiscuous mode if it is given too many addresses.". QEMU marks
854 * `n->mac_table.multi_overflow` if guest sets too many
855 * non-multicast MAC addresses.
857 * Therefore, QEMU should only send this CVQ command if the
858 * `n->mac_table.multi_overflow` is marked or `n->allmulti` is on,
859 * which sets all-multicast mode on, different from the device's defaults.
861 * Note that the device's defaults can mismatch the driver's
862 * configuration only at live migration.
864 if (n
->mac_table
.multi_overflow
|| n
->allmulti
) {
865 dev_written
= vhost_vdpa_net_load_rx_mode(s
,
866 VIRTIO_NET_CTRL_RX_ALLMULTI
, 1);
867 if (unlikely(dev_written
< 0)) {
870 if (*s
->status
!= VIRTIO_NET_OK
) {
878 static int vhost_vdpa_net_load(NetClientState
*nc
)
880 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
881 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
885 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
887 if (!v
->shadow_vqs_enabled
) {
891 n
= VIRTIO_NET(v
->dev
->vdev
);
892 r
= vhost_vdpa_net_load_mac(s
, n
);
893 if (unlikely(r
< 0)) {
896 r
= vhost_vdpa_net_load_mq(s
, n
);
900 r
= vhost_vdpa_net_load_offloads(s
, n
);
904 r
= vhost_vdpa_net_load_rx(s
, n
);
912 static NetClientInfo net_vhost_vdpa_cvq_info
= {
913 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
914 .size
= sizeof(VhostVDPAState
),
915 .receive
= vhost_vdpa_receive
,
916 .start
= vhost_vdpa_net_cvq_start
,
917 .load
= vhost_vdpa_net_load
,
918 .stop
= vhost_vdpa_net_cvq_stop
,
919 .cleanup
= vhost_vdpa_cleanup
,
920 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
921 .has_ufo
= vhost_vdpa_has_ufo
,
922 .check_peer_type
= vhost_vdpa_check_peer_type
,
926 * Validate and copy control virtqueue commands.
928 * Following QEMU guidelines, we offer a copy of the buffers to the device to
929 * prevent TOCTOU bugs.
931 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
932 VirtQueueElement
*elem
,
935 VhostVDPAState
*s
= opaque
;
937 const struct virtio_net_ctrl_hdr
*ctrl
;
938 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
939 /* Out buffer sent to both the vdpa device and the device model */
941 .iov_base
= s
->cvq_cmd_out_buffer
,
943 /* in buffer used for device model */
944 const struct iovec in
= {
946 .iov_len
= sizeof(status
),
948 ssize_t dev_written
= -EINVAL
;
950 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
951 s
->cvq_cmd_out_buffer
,
952 vhost_vdpa_net_cvq_cmd_len());
954 ctrl
= s
->cvq_cmd_out_buffer
;
955 if (ctrl
->class == VIRTIO_NET_CTRL_ANNOUNCE
) {
957 * Guest announce capability is emulated by qemu, so don't forward to
960 dev_written
= sizeof(status
);
961 *s
->status
= VIRTIO_NET_OK
;
963 dev_written
= vhost_vdpa_net_cvq_add(s
, out
.iov_len
, sizeof(status
));
964 if (unlikely(dev_written
< 0)) {
969 if (unlikely(dev_written
< sizeof(status
))) {
970 error_report("Insufficient written data (%zu)", dev_written
);
974 if (*s
->status
!= VIRTIO_NET_OK
) {
978 status
= VIRTIO_NET_ERR
;
979 virtio_net_handle_ctrl_iov(svq
->vdev
, &in
, 1, &out
, 1);
980 if (status
!= VIRTIO_NET_OK
) {
981 error_report("Bad CVQ processing in model");
985 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
987 if (unlikely(in_len
< sizeof(status
))) {
988 error_report("Bad device CVQ written length");
990 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
992 * `elem` belongs to vhost_vdpa_net_handle_ctrl_avail() only when
993 * the function successfully forwards the CVQ command, indicated
994 * by a non-negative value of `dev_written`. Otherwise, it still
996 * This function should only free the `elem` when it owns.
998 if (dev_written
>= 0) {
1001 return dev_written
< 0 ? dev_written
: 0;
1004 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
1005 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
1009 * Probe if CVQ is isolated
1011 * @device_fd The vdpa device fd
1012 * @features Features offered by the device.
1013 * @cvq_index The control vq pair index
1015 * Returns <0 in case of failure, 0 if false and 1 if true.
1017 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
1018 int cvq_index
, Error
**errp
)
1020 uint64_t backend_features
;
1022 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
1023 VIRTIO_CONFIG_S_DRIVER
|
1024 VIRTIO_CONFIG_S_FEATURES_OK
;
1029 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
1030 if (unlikely(r
< 0)) {
1031 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
1035 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
1039 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
1041 error_setg_errno(errp
, errno
, "Cannot set features");
1044 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1046 error_setg_errno(errp
, -r
, "Cannot set device features");
1050 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
1051 if (unlikely(cvq_group
< 0)) {
1052 if (cvq_group
!= -ENOTSUP
) {
1058 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
1059 * support ASID even if the parent driver does not. The CVQ cannot be
1060 * isolated in this case.
1068 for (int i
= 0; i
< cvq_index
; ++i
) {
1069 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
1070 if (unlikely(group
< 0)) {
1075 if (group
== (int64_t)cvq_group
) {
1085 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
1089 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
1093 int queue_pair_index
,
1097 struct vhost_vdpa_iova_range iova_range
,
1101 NetClientState
*nc
= NULL
;
1108 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
1111 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
1112 queue_pair_index
* 2,
1114 if (unlikely(cvq_isolated
< 0)) {
1118 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
1121 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
1122 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
1124 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
1125 s
->vhost_vdpa
.index
= queue_pair_index
;
1126 s
->always_svq
= svq
;
1127 s
->migration_state
.notify
= vdpa_net_migration_state_notifier
;
1128 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
1129 s
->vhost_vdpa
.iova_range
= iova_range
;
1130 s
->vhost_vdpa
.shadow_data
= svq
;
1131 if (queue_pair_index
== 0) {
1132 vhost_vdpa_net_valid_svq_features(features
,
1133 &s
->vhost_vdpa
.migration_blocker
);
1134 } else if (!is_datapath
) {
1135 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1136 PROT_READ
| PROT_WRITE
,
1137 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
1138 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
1139 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
1142 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
1143 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
1144 s
->cvq_isolated
= cvq_isolated
;
1147 * TODO: We cannot migrate devices with CVQ and no x-svq enabled as
1148 * there is no way to set the device state (MAC, MQ, etc) before
1149 * starting the datapath.
1151 * Migration blocker ownership now belongs to s->vhost_vdpa.
1154 error_setg(&s
->vhost_vdpa
.migration_blocker
,
1155 "net vdpa cannot migrate with CVQ feature");
1158 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
1160 qemu_del_net_client(nc
);
1166 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
1168 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
1169 if (unlikely(ret
< 0)) {
1170 error_setg_errno(errp
, errno
,
1171 "Fail to query features from vhost-vDPA device");
1176 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
1177 int *has_cvq
, Error
**errp
)
1179 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
1180 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
1181 __virtio16
*max_queue_pairs
;
1184 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1190 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1191 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1192 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1193 config
->len
= sizeof(*max_queue_pairs
);
1195 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1197 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1201 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1203 return lduw_le_p(max_queue_pairs
);
1209 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1210 NetClientState
*peer
, Error
**errp
)
1212 const NetdevVhostVDPAOptions
*opts
;
1215 g_autofree NetClientState
**ncs
= NULL
;
1216 struct vhost_vdpa_iova_range iova_range
;
1218 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1220 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1221 opts
= &netdev
->u
.vhost_vdpa
;
1222 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1224 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1228 if (opts
->vhostdev
&& opts
->vhostfd
) {
1230 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1234 if (opts
->vhostdev
) {
1235 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1236 if (vdpa_device_fd
== -1) {
1241 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1242 if (vdpa_device_fd
== -1) {
1243 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1248 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1249 if (unlikely(r
< 0)) {
1253 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1255 if (queue_pairs
< 0) {
1256 qemu_close(vdpa_device_fd
);
1260 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1261 if (unlikely(r
< 0)) {
1262 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1267 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1271 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1273 for (i
= 0; i
< queue_pairs
; i
++) {
1274 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1275 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1276 iova_range
, features
, errp
);
1282 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1283 vdpa_device_fd
, i
, 1, false,
1284 opts
->x_svq
, iova_range
, features
, errp
);
1293 for (i
--; i
>= 0; i
--) {
1294 qemu_del_net_client(ncs
[i
]);
1298 qemu_close(vdpa_device_fd
);