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 */
53 const int vdpa_feature_bits
[] = {
54 VIRTIO_F_NOTIFY_ON_EMPTY
,
55 VIRTIO_RING_F_INDIRECT_DESC
,
56 VIRTIO_RING_F_EVENT_IDX
,
60 VIRTIO_NET_F_GUEST_CSUM
,
61 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
,
63 VIRTIO_NET_F_GUEST_TSO4
,
64 VIRTIO_NET_F_GUEST_TSO6
,
65 VIRTIO_NET_F_GUEST_ECN
,
66 VIRTIO_NET_F_GUEST_UFO
,
67 VIRTIO_NET_F_HOST_TSO4
,
68 VIRTIO_NET_F_HOST_TSO6
,
69 VIRTIO_NET_F_HOST_ECN
,
70 VIRTIO_NET_F_HOST_UFO
,
71 VIRTIO_NET_F_MRG_RXBUF
,
74 VIRTIO_NET_F_CTRL_RX_EXTRA
,
75 VIRTIO_NET_F_CTRL_VLAN
,
76 VIRTIO_NET_F_CTRL_MAC_ADDR
,
80 VIRTIO_F_IOMMU_PLATFORM
,
84 VIRTIO_NET_F_HASH_REPORT
,
86 VHOST_INVALID_FEATURE_BIT
89 /** Supported device specific feature bits with SVQ */
90 static const uint64_t vdpa_svq_device_features
=
91 BIT_ULL(VIRTIO_NET_F_CSUM
) |
92 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM
) |
93 BIT_ULL(VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
) |
94 BIT_ULL(VIRTIO_NET_F_MTU
) |
95 BIT_ULL(VIRTIO_NET_F_MAC
) |
96 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4
) |
97 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6
) |
98 BIT_ULL(VIRTIO_NET_F_GUEST_ECN
) |
99 BIT_ULL(VIRTIO_NET_F_GUEST_UFO
) |
100 BIT_ULL(VIRTIO_NET_F_HOST_TSO4
) |
101 BIT_ULL(VIRTIO_NET_F_HOST_TSO6
) |
102 BIT_ULL(VIRTIO_NET_F_HOST_ECN
) |
103 BIT_ULL(VIRTIO_NET_F_HOST_UFO
) |
104 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF
) |
105 BIT_ULL(VIRTIO_NET_F_STATUS
) |
106 BIT_ULL(VIRTIO_NET_F_CTRL_VQ
) |
107 BIT_ULL(VIRTIO_NET_F_MQ
) |
108 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
109 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
110 /* VHOST_F_LOG_ALL is exposed by SVQ */
111 BIT_ULL(VHOST_F_LOG_ALL
) |
112 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
113 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
114 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
116 #define VHOST_VDPA_NET_CVQ_ASID 1
118 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
120 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
121 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
125 static size_t vhost_vdpa_net_cvq_cmd_len(void)
128 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
129 * In buffer is always 1 byte, so it should fit here
131 return sizeof(struct virtio_net_ctrl_hdr
) +
132 2 * sizeof(struct virtio_net_ctrl_mac
) +
133 MAC_TABLE_ENTRIES
* ETH_ALEN
;
136 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
138 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
141 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
143 uint64_t invalid_dev_features
=
144 features
& ~vdpa_svq_device_features
&
145 /* Transport are all accepted at this point */
146 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
147 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
149 if (invalid_dev_features
) {
150 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
151 invalid_dev_features
);
155 return vhost_svq_valid_features(features
, errp
);
158 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
162 struct vhost_dev
*hdev
;
164 hdev
= (struct vhost_dev
*)&net
->dev
;
165 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
166 if (device_id
!= VIRTIO_ID_NET
) {
172 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
173 int queue_pair_index
, int nvqs
)
175 VhostNetOptions options
;
176 struct vhost_net
*net
= NULL
;
180 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
181 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
182 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
183 options
.net_backend
= ncs
;
185 options
.busyloop_timeout
= 0;
188 net
= vhost_net_init(&options
);
190 error_report("failed to init vhost_net for queue");
194 ret
= vhost_vdpa_net_check_device_id(net
);
200 vhost_net_cleanup(net
);
206 static void vhost_vdpa_cleanup(NetClientState
*nc
)
208 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
210 munmap(s
->cvq_cmd_out_buffer
, vhost_vdpa_net_cvq_cmd_page_len());
211 munmap(s
->status
, vhost_vdpa_net_cvq_cmd_page_len());
213 vhost_net_cleanup(s
->vhost_net
);
214 g_free(s
->vhost_net
);
217 if (s
->vhost_vdpa
.device_fd
>= 0) {
218 qemu_close(s
->vhost_vdpa
.device_fd
);
219 s
->vhost_vdpa
.device_fd
= -1;
223 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
225 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
230 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
232 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
233 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
234 uint64_t features
= 0;
235 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
236 features
= vhost_net_get_features(s
->vhost_net
, features
);
237 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
241 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
244 const char *driver
= object_class_get_name(oc
);
246 if (!g_str_has_prefix(driver
, "virtio-net-")) {
247 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
254 /** Dummy receive in case qemu falls back to userland tap networking */
255 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
261 /** From any vdpa net client, get the netclient of the first queue pair */
262 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
264 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
265 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
267 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
270 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
272 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
275 int data_queue_pairs
, cvq
, r
;
277 /* We are only called on the first data vqs and only if x-svq is not set */
278 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
283 n
= VIRTIO_NET(vdev
);
284 if (!n
->vhost_started
) {
288 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
289 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
290 n
->max_ncs
- n
->max_queue_pairs
: 0;
292 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
293 * in the future and resume the device if read-only operations between
294 * suspend and reset goes wrong.
296 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
298 /* Start will check migration setup_or_active to configure or not SVQ */
299 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
300 if (unlikely(r
< 0)) {
301 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
305 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
307 MigrationState
*migration
= data
;
308 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
311 if (migration_in_setup(migration
)) {
312 vhost_vdpa_net_log_global_enable(s
, true);
313 } else if (migration_has_failed(migration
)) {
314 vhost_vdpa_net_log_global_enable(s
, false);
318 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
320 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
322 add_migration_state_change_notifier(&s
->migration_state
);
323 if (v
->shadow_vqs_enabled
) {
324 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
329 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
331 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
332 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
334 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
337 migration_is_setup_or_active(migrate_get_current()->state
)) {
338 v
->shadow_vqs_enabled
= true;
339 v
->shadow_data
= true;
341 v
->shadow_vqs_enabled
= false;
342 v
->shadow_data
= false;
346 vhost_vdpa_net_data_start_first(s
);
350 if (v
->shadow_vqs_enabled
) {
351 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
352 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
358 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
360 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
361 struct vhost_dev
*dev
;
363 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
365 if (s
->vhost_vdpa
.index
== 0) {
366 remove_migration_state_change_notifier(&s
->migration_state
);
369 dev
= s
->vhost_vdpa
.dev
;
370 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
371 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
375 static NetClientInfo net_vhost_vdpa_info
= {
376 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
377 .size
= sizeof(VhostVDPAState
),
378 .receive
= vhost_vdpa_receive
,
379 .start
= vhost_vdpa_net_data_start
,
380 .stop
= vhost_vdpa_net_client_stop
,
381 .cleanup
= vhost_vdpa_cleanup
,
382 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
383 .has_ufo
= vhost_vdpa_has_ufo
,
384 .check_peer_type
= vhost_vdpa_check_peer_type
,
387 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
,
390 struct vhost_vring_state state
= {
393 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
395 if (unlikely(r
< 0)) {
397 error_setg_errno(errp
, errno
, "Cannot get VQ %u group", vq_index
);
404 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
408 struct vhost_vring_state asid
= {
414 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
415 if (unlikely(r
< 0)) {
416 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
417 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
422 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
424 VhostIOVATree
*tree
= v
->iova_tree
;
427 * No need to specify size or to look for more translations since
428 * this contiguous chunk was allocated by us.
430 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
432 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
435 if (unlikely(!map
)) {
436 error_report("Cannot locate expected map");
440 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
441 if (unlikely(r
!= 0)) {
442 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
445 vhost_iova_tree_remove(tree
, *map
);
448 /** Map CVQ buffer. */
449 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
455 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
457 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
458 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
459 if (unlikely(r
!= IOVA_OK
)) {
460 error_report("Cannot map injected element");
464 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
465 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
466 if (unlikely(r
< 0)) {
473 vhost_iova_tree_remove(v
->iova_tree
, map
);
477 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
479 VhostVDPAState
*s
, *s0
;
480 struct vhost_vdpa
*v
;
485 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
487 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
490 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
491 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
492 v
->shadow_vqs_enabled
= s
->always_svq
;
493 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
495 if (s
->vhost_vdpa
.shadow_data
) {
496 /* SVQ is already configured for all virtqueues */
501 * If we early return in these cases SVQ will not be enabled. The migration
502 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
504 if (!vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
508 if (!s
->cvq_isolated
) {
512 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
,
513 v
->dev
->vq_index_end
- 1,
515 if (unlikely(cvq_group
< 0)) {
516 error_report_err(err
);
520 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
521 if (unlikely(r
< 0)) {
525 v
->shadow_vqs_enabled
= true;
526 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
529 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
533 if (s0
->vhost_vdpa
.iova_tree
) {
535 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
536 * simplicity, whether CVQ shares ASID with guest or not, because:
537 * - Memory listener need access to guest's memory addresses allocated
539 * - There should be plenty of IOVA address space for both ASID not to
540 * worry about collisions between them. Guest's translations are
541 * still validated with virtio virtqueue_pop so there is no risk for
542 * the guest to access memory that it shouldn't.
544 * To allocate a iova tree per ASID is doable but it complicates the
545 * code and it is not worth it for the moment.
547 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
549 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
553 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
554 vhost_vdpa_net_cvq_cmd_page_len(), false);
555 if (unlikely(r
< 0)) {
559 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
560 vhost_vdpa_net_cvq_cmd_page_len(), true);
561 if (unlikely(r
< 0)) {
562 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
568 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
570 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
572 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
574 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
575 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
576 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
579 vhost_vdpa_net_client_stop(nc
);
582 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
, size_t out_len
,
585 /* Buffers for the device */
586 const struct iovec out
= {
587 .iov_base
= s
->cvq_cmd_out_buffer
,
590 const struct iovec in
= {
591 .iov_base
= s
->status
,
592 .iov_len
= sizeof(virtio_net_ctrl_ack
),
594 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
597 r
= vhost_svq_add(svq
, &out
, 1, &in
, 1, NULL
);
598 if (unlikely(r
!= 0)) {
599 if (unlikely(r
== -ENOSPC
)) {
600 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
607 * We can poll here since we've had BQL from the time we sent the
608 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
609 * when BQL is released
611 return vhost_svq_poll(svq
);
614 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
, uint8_t class,
615 uint8_t cmd
, const void *data
,
618 const struct virtio_net_ctrl_hdr ctrl
= {
623 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
625 memcpy(s
->cvq_cmd_out_buffer
, &ctrl
, sizeof(ctrl
));
626 memcpy(s
->cvq_cmd_out_buffer
+ sizeof(ctrl
), data
, data_size
);
628 return vhost_vdpa_net_cvq_add(s
, sizeof(ctrl
) + data_size
,
629 sizeof(virtio_net_ctrl_ack
));
632 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
)
634 if (virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
635 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MAC
,
636 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
637 n
->mac
, sizeof(n
->mac
));
638 if (unlikely(dev_written
< 0)) {
642 return *s
->status
!= VIRTIO_NET_OK
;
648 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
651 struct virtio_net_ctrl_mq mq
;
654 if (!virtio_vdev_has_feature(&n
->parent_obj
, VIRTIO_NET_F_MQ
)) {
658 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
659 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MQ
,
660 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
, &mq
,
662 if (unlikely(dev_written
< 0)) {
666 return *s
->status
!= VIRTIO_NET_OK
;
669 static int vhost_vdpa_net_load_offloads(VhostVDPAState
*s
,
675 if (!virtio_vdev_has_feature(&n
->parent_obj
,
676 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS
)) {
680 if (n
->curr_guest_offloads
== virtio_net_supported_guest_offloads(n
)) {
682 * According to VirtIO standard, "Upon feature negotiation
683 * corresponding offload gets enabled to preserve
684 * backward compatibility.".
686 * Therefore, there is no need to send this CVQ command if the
687 * driver also enables all supported offloads, which aligns with
688 * the device's defaults.
690 * Note that the device's defaults can mismatch the driver's
691 * configuration only at live migration.
696 offloads
= cpu_to_le64(n
->curr_guest_offloads
);
697 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_GUEST_OFFLOADS
,
698 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET
,
699 &offloads
, sizeof(offloads
));
700 if (unlikely(dev_written
< 0)) {
704 return *s
->status
!= VIRTIO_NET_OK
;
707 static int vhost_vdpa_net_load(NetClientState
*nc
)
709 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
710 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
714 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
716 if (!v
->shadow_vqs_enabled
) {
720 n
= VIRTIO_NET(v
->dev
->vdev
);
721 r
= vhost_vdpa_net_load_mac(s
, n
);
722 if (unlikely(r
< 0)) {
725 r
= vhost_vdpa_net_load_mq(s
, n
);
729 r
= vhost_vdpa_net_load_offloads(s
, n
);
737 static NetClientInfo net_vhost_vdpa_cvq_info
= {
738 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
739 .size
= sizeof(VhostVDPAState
),
740 .receive
= vhost_vdpa_receive
,
741 .start
= vhost_vdpa_net_cvq_start
,
742 .load
= vhost_vdpa_net_load
,
743 .stop
= vhost_vdpa_net_cvq_stop
,
744 .cleanup
= vhost_vdpa_cleanup
,
745 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
746 .has_ufo
= vhost_vdpa_has_ufo
,
747 .check_peer_type
= vhost_vdpa_check_peer_type
,
751 * Validate and copy control virtqueue commands.
753 * Following QEMU guidelines, we offer a copy of the buffers to the device to
754 * prevent TOCTOU bugs.
756 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
757 VirtQueueElement
*elem
,
760 VhostVDPAState
*s
= opaque
;
762 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
763 /* Out buffer sent to both the vdpa device and the device model */
765 .iov_base
= s
->cvq_cmd_out_buffer
,
767 /* in buffer used for device model */
768 const struct iovec in
= {
770 .iov_len
= sizeof(status
),
772 ssize_t dev_written
= -EINVAL
;
774 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
775 s
->cvq_cmd_out_buffer
,
776 vhost_vdpa_net_cvq_cmd_len());
777 if (*(uint8_t *)s
->cvq_cmd_out_buffer
== VIRTIO_NET_CTRL_ANNOUNCE
) {
779 * Guest announce capability is emulated by qemu, so don't forward to
782 dev_written
= sizeof(status
);
783 *s
->status
= VIRTIO_NET_OK
;
785 dev_written
= vhost_vdpa_net_cvq_add(s
, out
.iov_len
, sizeof(status
));
786 if (unlikely(dev_written
< 0)) {
791 if (unlikely(dev_written
< sizeof(status
))) {
792 error_report("Insufficient written data (%zu)", dev_written
);
796 if (*s
->status
!= VIRTIO_NET_OK
) {
800 status
= VIRTIO_NET_ERR
;
801 virtio_net_handle_ctrl_iov(svq
->vdev
, &in
, 1, &out
, 1);
802 if (status
!= VIRTIO_NET_OK
) {
803 error_report("Bad CVQ processing in model");
807 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
809 if (unlikely(in_len
< sizeof(status
))) {
810 error_report("Bad device CVQ written length");
812 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
814 return dev_written
< 0 ? dev_written
: 0;
817 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
818 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
822 * Probe if CVQ is isolated
824 * @device_fd The vdpa device fd
825 * @features Features offered by the device.
826 * @cvq_index The control vq pair index
828 * Returns <0 in case of failure, 0 if false and 1 if true.
830 static int vhost_vdpa_probe_cvq_isolation(int device_fd
, uint64_t features
,
831 int cvq_index
, Error
**errp
)
833 uint64_t backend_features
;
835 uint8_t status
= VIRTIO_CONFIG_S_ACKNOWLEDGE
|
836 VIRTIO_CONFIG_S_DRIVER
|
837 VIRTIO_CONFIG_S_FEATURES_OK
;
842 r
= ioctl(device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
843 if (unlikely(r
< 0)) {
844 error_setg_errno(errp
, errno
, "Cannot get vdpa backend_features");
848 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
))) {
852 r
= ioctl(device_fd
, VHOST_SET_FEATURES
, &features
);
854 error_setg_errno(errp
, errno
, "Cannot set features");
857 r
= ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
859 error_setg_errno(errp
, -r
, "Cannot set device features");
863 cvq_group
= vhost_vdpa_get_vring_group(device_fd
, cvq_index
, errp
);
864 if (unlikely(cvq_group
< 0)) {
865 if (cvq_group
!= -ENOTSUP
) {
871 * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
872 * support ASID even if the parent driver does not. The CVQ cannot be
873 * isolated in this case.
881 for (int i
= 0; i
< cvq_index
; ++i
) {
882 int64_t group
= vhost_vdpa_get_vring_group(device_fd
, i
, errp
);
883 if (unlikely(group
< 0)) {
888 if (group
== (int64_t)cvq_group
) {
898 ioctl(device_fd
, VHOST_VDPA_SET_STATUS
, &status
);
902 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
906 int queue_pair_index
,
910 struct vhost_vdpa_iova_range iova_range
,
914 NetClientState
*nc
= NULL
;
921 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
924 cvq_isolated
= vhost_vdpa_probe_cvq_isolation(vdpa_device_fd
, features
,
925 queue_pair_index
* 2,
927 if (unlikely(cvq_isolated
< 0)) {
931 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
934 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
935 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
937 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
938 s
->vhost_vdpa
.index
= queue_pair_index
;
940 s
->migration_state
.notify
= vdpa_net_migration_state_notifier
;
941 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
942 s
->vhost_vdpa
.iova_range
= iova_range
;
943 s
->vhost_vdpa
.shadow_data
= svq
;
944 if (queue_pair_index
== 0) {
945 vhost_vdpa_net_valid_svq_features(features
,
946 &s
->vhost_vdpa
.migration_blocker
);
947 } else if (!is_datapath
) {
948 s
->cvq_cmd_out_buffer
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
949 PROT_READ
| PROT_WRITE
,
950 MAP_SHARED
| MAP_ANONYMOUS
, -1, 0);
951 s
->status
= mmap(NULL
, vhost_vdpa_net_cvq_cmd_page_len(),
952 PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
,
955 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
956 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
957 s
->cvq_isolated
= cvq_isolated
;
960 * TODO: We cannot migrate devices with CVQ and no x-svq enabled as
961 * there is no way to set the device state (MAC, MQ, etc) before
962 * starting the datapath.
964 * Migration blocker ownership now belongs to s->vhost_vdpa.
967 error_setg(&s
->vhost_vdpa
.migration_blocker
,
968 "net vdpa cannot migrate with CVQ feature");
971 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
973 qemu_del_net_client(nc
);
979 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
981 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
982 if (unlikely(ret
< 0)) {
983 error_setg_errno(errp
, errno
,
984 "Fail to query features from vhost-vDPA device");
989 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
990 int *has_cvq
, Error
**errp
)
992 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
993 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
994 __virtio16
*max_queue_pairs
;
997 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
1003 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
1004 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
1005 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
1006 config
->len
= sizeof(*max_queue_pairs
);
1008 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
1010 error_setg(errp
, "Fail to get config from vhost-vDPA device");
1014 max_queue_pairs
= (__virtio16
*)&config
->buf
;
1016 return lduw_le_p(max_queue_pairs
);
1022 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
1023 NetClientState
*peer
, Error
**errp
)
1025 const NetdevVhostVDPAOptions
*opts
;
1028 g_autofree NetClientState
**ncs
= NULL
;
1029 struct vhost_vdpa_iova_range iova_range
;
1031 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
1033 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
1034 opts
= &netdev
->u
.vhost_vdpa
;
1035 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
1037 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
1041 if (opts
->vhostdev
&& opts
->vhostfd
) {
1043 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
1047 if (opts
->vhostdev
) {
1048 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
1049 if (vdpa_device_fd
== -1) {
1054 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
1055 if (vdpa_device_fd
== -1) {
1056 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
1061 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
1062 if (unlikely(r
< 0)) {
1066 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
1068 if (queue_pairs
< 0) {
1069 qemu_close(vdpa_device_fd
);
1073 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
1074 if (unlikely(r
< 0)) {
1075 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
1080 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
1084 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
1086 for (i
= 0; i
< queue_pairs
; i
++) {
1087 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1088 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
1089 iova_range
, features
, errp
);
1095 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
1096 vdpa_device_fd
, i
, 1, false,
1097 opts
->x_svq
, iova_range
, features
, errp
);
1106 for (i
--; i
>= 0; i
--) {
1107 qemu_del_net_client(ncs
[i
]);
1111 qemu_close(vdpa_device_fd
);