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 */
49 const int vdpa_feature_bits
[] = {
50 VIRTIO_F_NOTIFY_ON_EMPTY
,
51 VIRTIO_RING_F_INDIRECT_DESC
,
52 VIRTIO_RING_F_EVENT_IDX
,
56 VIRTIO_NET_F_GUEST_CSUM
,
58 VIRTIO_NET_F_GUEST_TSO4
,
59 VIRTIO_NET_F_GUEST_TSO6
,
60 VIRTIO_NET_F_GUEST_ECN
,
61 VIRTIO_NET_F_GUEST_UFO
,
62 VIRTIO_NET_F_HOST_TSO4
,
63 VIRTIO_NET_F_HOST_TSO6
,
64 VIRTIO_NET_F_HOST_ECN
,
65 VIRTIO_NET_F_HOST_UFO
,
66 VIRTIO_NET_F_MRG_RXBUF
,
69 VIRTIO_NET_F_CTRL_RX_EXTRA
,
70 VIRTIO_NET_F_CTRL_VLAN
,
71 VIRTIO_NET_F_CTRL_MAC_ADDR
,
75 VIRTIO_F_IOMMU_PLATFORM
,
79 VIRTIO_NET_F_HASH_REPORT
,
81 VHOST_INVALID_FEATURE_BIT
84 /** Supported device specific feature bits with SVQ */
85 static const uint64_t vdpa_svq_device_features
=
86 BIT_ULL(VIRTIO_NET_F_CSUM
) |
87 BIT_ULL(VIRTIO_NET_F_GUEST_CSUM
) |
88 BIT_ULL(VIRTIO_NET_F_MTU
) |
89 BIT_ULL(VIRTIO_NET_F_MAC
) |
90 BIT_ULL(VIRTIO_NET_F_GUEST_TSO4
) |
91 BIT_ULL(VIRTIO_NET_F_GUEST_TSO6
) |
92 BIT_ULL(VIRTIO_NET_F_GUEST_ECN
) |
93 BIT_ULL(VIRTIO_NET_F_GUEST_UFO
) |
94 BIT_ULL(VIRTIO_NET_F_HOST_TSO4
) |
95 BIT_ULL(VIRTIO_NET_F_HOST_TSO6
) |
96 BIT_ULL(VIRTIO_NET_F_HOST_ECN
) |
97 BIT_ULL(VIRTIO_NET_F_HOST_UFO
) |
98 BIT_ULL(VIRTIO_NET_F_MRG_RXBUF
) |
99 BIT_ULL(VIRTIO_NET_F_STATUS
) |
100 BIT_ULL(VIRTIO_NET_F_CTRL_VQ
) |
101 BIT_ULL(VIRTIO_NET_F_MQ
) |
102 BIT_ULL(VIRTIO_F_ANY_LAYOUT
) |
103 BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
) |
104 /* VHOST_F_LOG_ALL is exposed by SVQ */
105 BIT_ULL(VHOST_F_LOG_ALL
) |
106 BIT_ULL(VIRTIO_NET_F_RSC_EXT
) |
107 BIT_ULL(VIRTIO_NET_F_STANDBY
) |
108 BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX
);
110 #define VHOST_VDPA_NET_CVQ_ASID 1
112 VHostNetState
*vhost_vdpa_get_vhost_net(NetClientState
*nc
)
114 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
115 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
119 static bool vhost_vdpa_net_valid_svq_features(uint64_t features
, Error
**errp
)
121 uint64_t invalid_dev_features
=
122 features
& ~vdpa_svq_device_features
&
123 /* Transport are all accepted at this point */
124 ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START
,
125 VIRTIO_TRANSPORT_F_END
- VIRTIO_TRANSPORT_F_START
);
127 if (invalid_dev_features
) {
128 error_setg(errp
, "vdpa svq does not work with features 0x%" PRIx64
,
129 invalid_dev_features
);
133 return vhost_svq_valid_features(features
, errp
);
136 static int vhost_vdpa_net_check_device_id(struct vhost_net
*net
)
140 struct vhost_dev
*hdev
;
142 hdev
= (struct vhost_dev
*)&net
->dev
;
143 ret
= hdev
->vhost_ops
->vhost_get_device_id(hdev
, &device_id
);
144 if (device_id
!= VIRTIO_ID_NET
) {
150 static int vhost_vdpa_add(NetClientState
*ncs
, void *be
,
151 int queue_pair_index
, int nvqs
)
153 VhostNetOptions options
;
154 struct vhost_net
*net
= NULL
;
158 options
.backend_type
= VHOST_BACKEND_TYPE_VDPA
;
159 assert(ncs
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
160 s
= DO_UPCAST(VhostVDPAState
, nc
, ncs
);
161 options
.net_backend
= ncs
;
163 options
.busyloop_timeout
= 0;
166 net
= vhost_net_init(&options
);
168 error_report("failed to init vhost_net for queue");
172 ret
= vhost_vdpa_net_check_device_id(net
);
178 vhost_net_cleanup(net
);
184 static void vhost_vdpa_cleanup(NetClientState
*nc
)
186 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
188 qemu_vfree(s
->cvq_cmd_out_buffer
);
189 qemu_vfree(s
->status
);
191 vhost_net_cleanup(s
->vhost_net
);
192 g_free(s
->vhost_net
);
195 if (s
->vhost_vdpa
.device_fd
>= 0) {
196 qemu_close(s
->vhost_vdpa
.device_fd
);
197 s
->vhost_vdpa
.device_fd
= -1;
201 static bool vhost_vdpa_has_vnet_hdr(NetClientState
*nc
)
203 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
208 static bool vhost_vdpa_has_ufo(NetClientState
*nc
)
210 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
211 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
212 uint64_t features
= 0;
213 features
|= (1ULL << VIRTIO_NET_F_HOST_UFO
);
214 features
= vhost_net_get_features(s
->vhost_net
, features
);
215 return !!(features
& (1ULL << VIRTIO_NET_F_HOST_UFO
));
219 static bool vhost_vdpa_check_peer_type(NetClientState
*nc
, ObjectClass
*oc
,
222 const char *driver
= object_class_get_name(oc
);
224 if (!g_str_has_prefix(driver
, "virtio-net-")) {
225 error_setg(errp
, "vhost-vdpa requires frontend driver virtio-net-*");
232 /** Dummy receive in case qemu falls back to userland tap networking */
233 static ssize_t
vhost_vdpa_receive(NetClientState
*nc
, const uint8_t *buf
,
239 /** From any vdpa net client, get the netclient of the first queue pair */
240 static VhostVDPAState
*vhost_vdpa_net_first_nc_vdpa(VhostVDPAState
*s
)
242 NICState
*nic
= qemu_get_nic(s
->nc
.peer
);
243 NetClientState
*nc0
= qemu_get_peer(nic
->ncs
, 0);
245 return DO_UPCAST(VhostVDPAState
, nc
, nc0
);
248 static void vhost_vdpa_net_log_global_enable(VhostVDPAState
*s
, bool enable
)
250 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
253 int data_queue_pairs
, cvq
, r
;
255 /* We are only called on the first data vqs and only if x-svq is not set */
256 if (s
->vhost_vdpa
.shadow_vqs_enabled
== enable
) {
261 n
= VIRTIO_NET(vdev
);
262 if (!n
->vhost_started
) {
266 data_queue_pairs
= n
->multiqueue
? n
->max_queue_pairs
: 1;
267 cvq
= virtio_vdev_has_feature(vdev
, VIRTIO_NET_F_CTRL_VQ
) ?
268 n
->max_ncs
- n
->max_queue_pairs
: 0;
270 * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
271 * in the future and resume the device if read-only operations between
272 * suspend and reset goes wrong.
274 vhost_net_stop(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
276 /* Start will check migration setup_or_active to configure or not SVQ */
277 r
= vhost_net_start(vdev
, n
->nic
->ncs
, data_queue_pairs
, cvq
);
278 if (unlikely(r
< 0)) {
279 error_report("unable to start vhost net: %s(%d)", g_strerror(-r
), -r
);
283 static void vdpa_net_migration_state_notifier(Notifier
*notifier
, void *data
)
285 MigrationState
*migration
= data
;
286 VhostVDPAState
*s
= container_of(notifier
, VhostVDPAState
,
289 if (migration_in_setup(migration
)) {
290 vhost_vdpa_net_log_global_enable(s
, true);
291 } else if (migration_has_failed(migration
)) {
292 vhost_vdpa_net_log_global_enable(s
, false);
296 static void vhost_vdpa_net_data_start_first(VhostVDPAState
*s
)
298 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
300 add_migration_state_change_notifier(&s
->migration_state
);
301 if (v
->shadow_vqs_enabled
) {
302 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
307 static int vhost_vdpa_net_data_start(NetClientState
*nc
)
309 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
310 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
312 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
315 migration_is_setup_or_active(migrate_get_current()->state
)) {
316 v
->shadow_vqs_enabled
= true;
317 v
->shadow_data
= true;
319 v
->shadow_vqs_enabled
= false;
320 v
->shadow_data
= false;
324 vhost_vdpa_net_data_start_first(s
);
328 if (v
->shadow_vqs_enabled
) {
329 VhostVDPAState
*s0
= vhost_vdpa_net_first_nc_vdpa(s
);
330 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
336 static void vhost_vdpa_net_client_stop(NetClientState
*nc
)
338 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
339 struct vhost_dev
*dev
;
341 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
343 if (s
->vhost_vdpa
.index
== 0) {
344 remove_migration_state_change_notifier(&s
->migration_state
);
347 dev
= s
->vhost_vdpa
.dev
;
348 if (dev
->vq_index
+ dev
->nvqs
== dev
->vq_index_end
) {
349 g_clear_pointer(&s
->vhost_vdpa
.iova_tree
, vhost_iova_tree_delete
);
353 static NetClientInfo net_vhost_vdpa_info
= {
354 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
355 .size
= sizeof(VhostVDPAState
),
356 .receive
= vhost_vdpa_receive
,
357 .start
= vhost_vdpa_net_data_start
,
358 .stop
= vhost_vdpa_net_client_stop
,
359 .cleanup
= vhost_vdpa_cleanup
,
360 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
361 .has_ufo
= vhost_vdpa_has_ufo
,
362 .check_peer_type
= vhost_vdpa_check_peer_type
,
365 static int64_t vhost_vdpa_get_vring_group(int device_fd
, unsigned vq_index
)
367 struct vhost_vring_state state
= {
370 int r
= ioctl(device_fd
, VHOST_VDPA_GET_VRING_GROUP
, &state
);
372 if (unlikely(r
< 0)) {
373 error_report("Cannot get VQ %u group: %s", vq_index
,
381 static int vhost_vdpa_set_address_space_id(struct vhost_vdpa
*v
,
385 struct vhost_vring_state asid
= {
391 r
= ioctl(v
->device_fd
, VHOST_VDPA_SET_GROUP_ASID
, &asid
);
392 if (unlikely(r
< 0)) {
393 error_report("Can't set vq group %u asid %u, errno=%d (%s)",
394 asid
.index
, asid
.num
, errno
, g_strerror(errno
));
399 static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa
*v
, void *addr
)
401 VhostIOVATree
*tree
= v
->iova_tree
;
404 * No need to specify size or to look for more translations since
405 * this contiguous chunk was allocated by us.
407 .translated_addr
= (hwaddr
)(uintptr_t)addr
,
409 const DMAMap
*map
= vhost_iova_tree_find_iova(tree
, &needle
);
412 if (unlikely(!map
)) {
413 error_report("Cannot locate expected map");
417 r
= vhost_vdpa_dma_unmap(v
, v
->address_space_id
, map
->iova
, map
->size
+ 1);
418 if (unlikely(r
!= 0)) {
419 error_report("Device cannot unmap: %s(%d)", g_strerror(r
), r
);
422 vhost_iova_tree_remove(tree
, *map
);
425 static size_t vhost_vdpa_net_cvq_cmd_len(void)
428 * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
429 * In buffer is always 1 byte, so it should fit here
431 return sizeof(struct virtio_net_ctrl_hdr
) +
432 2 * sizeof(struct virtio_net_ctrl_mac
) +
433 MAC_TABLE_ENTRIES
* ETH_ALEN
;
436 static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
438 return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
441 /** Map CVQ buffer. */
442 static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa
*v
, void *buf
, size_t size
,
448 map
.translated_addr
= (hwaddr
)(uintptr_t)buf
;
450 map
.perm
= write
? IOMMU_RW
: IOMMU_RO
,
451 r
= vhost_iova_tree_map_alloc(v
->iova_tree
, &map
);
452 if (unlikely(r
!= IOVA_OK
)) {
453 error_report("Cannot map injected element");
457 r
= vhost_vdpa_dma_map(v
, v
->address_space_id
, map
.iova
,
458 vhost_vdpa_net_cvq_cmd_page_len(), buf
, !write
);
459 if (unlikely(r
< 0)) {
466 vhost_iova_tree_remove(v
->iova_tree
, map
);
470 static int vhost_vdpa_net_cvq_start(NetClientState
*nc
)
472 VhostVDPAState
*s
, *s0
;
473 struct vhost_vdpa
*v
;
474 uint64_t backend_features
;
478 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
480 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
483 s0
= vhost_vdpa_net_first_nc_vdpa(s
);
484 v
->shadow_data
= s0
->vhost_vdpa
.shadow_vqs_enabled
;
485 v
->shadow_vqs_enabled
= s
->always_svq
;
486 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_GUEST_PA_ASID
;
488 if (s
->vhost_vdpa
.shadow_data
) {
489 /* SVQ is already configured for all virtqueues */
494 * If we early return in these cases SVQ will not be enabled. The migration
495 * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
497 * Calling VHOST_GET_BACKEND_FEATURES as they are not available in v->dev
500 r
= ioctl(v
->device_fd
, VHOST_GET_BACKEND_FEATURES
, &backend_features
);
501 if (unlikely(r
< 0)) {
502 error_report("Cannot get vdpa backend_features: %s(%d)",
503 g_strerror(errno
), errno
);
506 if (!(backend_features
& BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID
)) ||
507 !vhost_vdpa_net_valid_svq_features(v
->dev
->features
, NULL
)) {
512 * Check if all the virtqueues of the virtio device are in a different vq
513 * than the last vq. VQ group of last group passed in cvq_group.
515 cvq_index
= v
->dev
->vq_index_end
- 1;
516 cvq_group
= vhost_vdpa_get_vring_group(v
->device_fd
, cvq_index
);
517 if (unlikely(cvq_group
< 0)) {
520 for (int i
= 0; i
< cvq_index
; ++i
) {
521 int64_t group
= vhost_vdpa_get_vring_group(v
->device_fd
, i
);
523 if (unlikely(group
< 0)) {
527 if (group
== cvq_group
) {
532 r
= vhost_vdpa_set_address_space_id(v
, cvq_group
, VHOST_VDPA_NET_CVQ_ASID
);
533 if (unlikely(r
< 0)) {
537 v
->shadow_vqs_enabled
= true;
538 s
->vhost_vdpa
.address_space_id
= VHOST_VDPA_NET_CVQ_ASID
;
541 if (!s
->vhost_vdpa
.shadow_vqs_enabled
) {
545 if (s0
->vhost_vdpa
.iova_tree
) {
547 * SVQ is already configured for all virtqueues. Reuse IOVA tree for
548 * simplicity, whether CVQ shares ASID with guest or not, because:
549 * - Memory listener need access to guest's memory addresses allocated
551 * - There should be plenty of IOVA address space for both ASID not to
552 * worry about collisions between them. Guest's translations are
553 * still validated with virtio virtqueue_pop so there is no risk for
554 * the guest to access memory that it shouldn't.
556 * To allocate a iova tree per ASID is doable but it complicates the
557 * code and it is not worth it for the moment.
559 v
->iova_tree
= s0
->vhost_vdpa
.iova_tree
;
561 v
->iova_tree
= vhost_iova_tree_new(v
->iova_range
.first
,
565 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
,
566 vhost_vdpa_net_cvq_cmd_page_len(), false);
567 if (unlikely(r
< 0)) {
571 r
= vhost_vdpa_cvq_map_buf(&s
->vhost_vdpa
, s
->status
,
572 vhost_vdpa_net_cvq_cmd_page_len(), true);
573 if (unlikely(r
< 0)) {
574 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
580 static void vhost_vdpa_net_cvq_stop(NetClientState
*nc
)
582 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
584 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
586 if (s
->vhost_vdpa
.shadow_vqs_enabled
) {
587 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->cvq_cmd_out_buffer
);
588 vhost_vdpa_cvq_unmap_buf(&s
->vhost_vdpa
, s
->status
);
591 vhost_vdpa_net_client_stop(nc
);
594 static ssize_t
vhost_vdpa_net_cvq_add(VhostVDPAState
*s
, size_t out_len
,
597 /* Buffers for the device */
598 const struct iovec out
= {
599 .iov_base
= s
->cvq_cmd_out_buffer
,
602 const struct iovec in
= {
603 .iov_base
= s
->status
,
604 .iov_len
= sizeof(virtio_net_ctrl_ack
),
606 VhostShadowVirtqueue
*svq
= g_ptr_array_index(s
->vhost_vdpa
.shadow_vqs
, 0);
609 r
= vhost_svq_add(svq
, &out
, 1, &in
, 1, NULL
);
610 if (unlikely(r
!= 0)) {
611 if (unlikely(r
== -ENOSPC
)) {
612 qemu_log_mask(LOG_GUEST_ERROR
, "%s: No space on device queue\n",
619 * We can poll here since we've had BQL from the time we sent the
620 * descriptor. Also, we need to take the answer before SVQ pulls by itself,
621 * when BQL is released
623 return vhost_svq_poll(svq
);
626 static ssize_t
vhost_vdpa_net_load_cmd(VhostVDPAState
*s
, uint8_t class,
627 uint8_t cmd
, const void *data
,
630 const struct virtio_net_ctrl_hdr ctrl
= {
635 assert(data_size
< vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl
));
637 memcpy(s
->cvq_cmd_out_buffer
, &ctrl
, sizeof(ctrl
));
638 memcpy(s
->cvq_cmd_out_buffer
+ sizeof(ctrl
), data
, data_size
);
640 return vhost_vdpa_net_cvq_add(s
, sizeof(ctrl
) + data_size
,
641 sizeof(virtio_net_ctrl_ack
));
644 static int vhost_vdpa_net_load_mac(VhostVDPAState
*s
, const VirtIONet
*n
)
646 uint64_t features
= n
->parent_obj
.guest_features
;
647 if (features
& BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR
)) {
648 ssize_t dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MAC
,
649 VIRTIO_NET_CTRL_MAC_ADDR_SET
,
650 n
->mac
, sizeof(n
->mac
));
651 if (unlikely(dev_written
< 0)) {
655 return *s
->status
!= VIRTIO_NET_OK
;
661 static int vhost_vdpa_net_load_mq(VhostVDPAState
*s
,
664 struct virtio_net_ctrl_mq mq
;
665 uint64_t features
= n
->parent_obj
.guest_features
;
668 if (!(features
& BIT_ULL(VIRTIO_NET_F_MQ
))) {
672 mq
.virtqueue_pairs
= cpu_to_le16(n
->curr_queue_pairs
);
673 dev_written
= vhost_vdpa_net_load_cmd(s
, VIRTIO_NET_CTRL_MQ
,
674 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET
, &mq
,
676 if (unlikely(dev_written
< 0)) {
680 return *s
->status
!= VIRTIO_NET_OK
;
683 static int vhost_vdpa_net_load(NetClientState
*nc
)
685 VhostVDPAState
*s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
686 struct vhost_vdpa
*v
= &s
->vhost_vdpa
;
690 assert(nc
->info
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
692 if (!v
->shadow_vqs_enabled
) {
696 n
= VIRTIO_NET(v
->dev
->vdev
);
697 r
= vhost_vdpa_net_load_mac(s
, n
);
698 if (unlikely(r
< 0)) {
701 r
= vhost_vdpa_net_load_mq(s
, n
);
709 static NetClientInfo net_vhost_vdpa_cvq_info
= {
710 .type
= NET_CLIENT_DRIVER_VHOST_VDPA
,
711 .size
= sizeof(VhostVDPAState
),
712 .receive
= vhost_vdpa_receive
,
713 .start
= vhost_vdpa_net_cvq_start
,
714 .load
= vhost_vdpa_net_load
,
715 .stop
= vhost_vdpa_net_cvq_stop
,
716 .cleanup
= vhost_vdpa_cleanup
,
717 .has_vnet_hdr
= vhost_vdpa_has_vnet_hdr
,
718 .has_ufo
= vhost_vdpa_has_ufo
,
719 .check_peer_type
= vhost_vdpa_check_peer_type
,
723 * Validate and copy control virtqueue commands.
725 * Following QEMU guidelines, we offer a copy of the buffers to the device to
726 * prevent TOCTOU bugs.
728 static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue
*svq
,
729 VirtQueueElement
*elem
,
732 VhostVDPAState
*s
= opaque
;
734 virtio_net_ctrl_ack status
= VIRTIO_NET_ERR
;
735 /* Out buffer sent to both the vdpa device and the device model */
737 .iov_base
= s
->cvq_cmd_out_buffer
,
739 /* in buffer used for device model */
740 const struct iovec in
= {
742 .iov_len
= sizeof(status
),
744 ssize_t dev_written
= -EINVAL
;
746 out
.iov_len
= iov_to_buf(elem
->out_sg
, elem
->out_num
, 0,
747 s
->cvq_cmd_out_buffer
,
748 vhost_vdpa_net_cvq_cmd_len());
749 if (*(uint8_t *)s
->cvq_cmd_out_buffer
== VIRTIO_NET_CTRL_ANNOUNCE
) {
751 * Guest announce capability is emulated by qemu, so don't forward to
754 dev_written
= sizeof(status
);
755 *s
->status
= VIRTIO_NET_OK
;
757 dev_written
= vhost_vdpa_net_cvq_add(s
, out
.iov_len
, sizeof(status
));
758 if (unlikely(dev_written
< 0)) {
763 if (unlikely(dev_written
< sizeof(status
))) {
764 error_report("Insufficient written data (%zu)", dev_written
);
768 if (*s
->status
!= VIRTIO_NET_OK
) {
769 return VIRTIO_NET_ERR
;
772 status
= VIRTIO_NET_ERR
;
773 virtio_net_handle_ctrl_iov(svq
->vdev
, &in
, 1, &out
, 1);
774 if (status
!= VIRTIO_NET_OK
) {
775 error_report("Bad CVQ processing in model");
779 in_len
= iov_from_buf(elem
->in_sg
, elem
->in_num
, 0, &status
,
781 if (unlikely(in_len
< sizeof(status
))) {
782 error_report("Bad device CVQ written length");
784 vhost_svq_push_elem(svq
, elem
, MIN(in_len
, sizeof(status
)));
786 return dev_written
< 0 ? dev_written
: 0;
789 static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops
= {
790 .avail_handler
= vhost_vdpa_net_handle_ctrl_avail
,
793 static NetClientState
*net_vhost_vdpa_init(NetClientState
*peer
,
797 int queue_pair_index
,
801 struct vhost_vdpa_iova_range iova_range
,
804 NetClientState
*nc
= NULL
;
809 nc
= qemu_new_net_client(&net_vhost_vdpa_info
, peer
, device
,
812 nc
= qemu_new_net_control_client(&net_vhost_vdpa_cvq_info
, peer
,
815 qemu_set_info_str(nc
, TYPE_VHOST_VDPA
);
816 s
= DO_UPCAST(VhostVDPAState
, nc
, nc
);
818 s
->vhost_vdpa
.device_fd
= vdpa_device_fd
;
819 s
->vhost_vdpa
.index
= queue_pair_index
;
821 s
->migration_state
.notify
= vdpa_net_migration_state_notifier
;
822 s
->vhost_vdpa
.shadow_vqs_enabled
= svq
;
823 s
->vhost_vdpa
.iova_range
= iova_range
;
824 s
->vhost_vdpa
.shadow_data
= svq
;
825 if (queue_pair_index
== 0) {
826 vhost_vdpa_net_valid_svq_features(features
,
827 &s
->vhost_vdpa
.migration_blocker
);
828 } else if (!is_datapath
) {
829 s
->cvq_cmd_out_buffer
= qemu_memalign(qemu_real_host_page_size(),
830 vhost_vdpa_net_cvq_cmd_page_len());
831 memset(s
->cvq_cmd_out_buffer
, 0, vhost_vdpa_net_cvq_cmd_page_len());
832 s
->status
= qemu_memalign(qemu_real_host_page_size(),
833 vhost_vdpa_net_cvq_cmd_page_len());
834 memset(s
->status
, 0, vhost_vdpa_net_cvq_cmd_page_len());
836 s
->vhost_vdpa
.shadow_vq_ops
= &vhost_vdpa_net_svq_ops
;
837 s
->vhost_vdpa
.shadow_vq_ops_opaque
= s
;
840 * TODO: We cannot migrate devices with CVQ as there is no way to set
841 * the device state (MAC, MQ, etc) before starting the datapath.
843 * Migration blocker ownership now belongs to s->vhost_vdpa.
845 error_setg(&s
->vhost_vdpa
.migration_blocker
,
846 "net vdpa cannot migrate with CVQ feature");
848 ret
= vhost_vdpa_add(nc
, (void *)&s
->vhost_vdpa
, queue_pair_index
, nvqs
);
850 qemu_del_net_client(nc
);
856 static int vhost_vdpa_get_features(int fd
, uint64_t *features
, Error
**errp
)
858 int ret
= ioctl(fd
, VHOST_GET_FEATURES
, features
);
859 if (unlikely(ret
< 0)) {
860 error_setg_errno(errp
, errno
,
861 "Fail to query features from vhost-vDPA device");
866 static int vhost_vdpa_get_max_queue_pairs(int fd
, uint64_t features
,
867 int *has_cvq
, Error
**errp
)
869 unsigned long config_size
= offsetof(struct vhost_vdpa_config
, buf
);
870 g_autofree
struct vhost_vdpa_config
*config
= NULL
;
871 __virtio16
*max_queue_pairs
;
874 if (features
& (1 << VIRTIO_NET_F_CTRL_VQ
)) {
880 if (features
& (1 << VIRTIO_NET_F_MQ
)) {
881 config
= g_malloc0(config_size
+ sizeof(*max_queue_pairs
));
882 config
->off
= offsetof(struct virtio_net_config
, max_virtqueue_pairs
);
883 config
->len
= sizeof(*max_queue_pairs
);
885 ret
= ioctl(fd
, VHOST_VDPA_GET_CONFIG
, config
);
887 error_setg(errp
, "Fail to get config from vhost-vDPA device");
891 max_queue_pairs
= (__virtio16
*)&config
->buf
;
893 return lduw_le_p(max_queue_pairs
);
899 int net_init_vhost_vdpa(const Netdev
*netdev
, const char *name
,
900 NetClientState
*peer
, Error
**errp
)
902 const NetdevVhostVDPAOptions
*opts
;
905 g_autofree NetClientState
**ncs
= NULL
;
906 struct vhost_vdpa_iova_range iova_range
;
908 int queue_pairs
, r
, i
= 0, has_cvq
= 0;
910 assert(netdev
->type
== NET_CLIENT_DRIVER_VHOST_VDPA
);
911 opts
= &netdev
->u
.vhost_vdpa
;
912 if (!opts
->vhostdev
&& !opts
->vhostfd
) {
914 "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
918 if (opts
->vhostdev
&& opts
->vhostfd
) {
920 "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
924 if (opts
->vhostdev
) {
925 vdpa_device_fd
= qemu_open(opts
->vhostdev
, O_RDWR
, errp
);
926 if (vdpa_device_fd
== -1) {
931 vdpa_device_fd
= monitor_fd_param(monitor_cur(), opts
->vhostfd
, errp
);
932 if (vdpa_device_fd
== -1) {
933 error_prepend(errp
, "vhost-vdpa: unable to parse vhostfd: ");
938 r
= vhost_vdpa_get_features(vdpa_device_fd
, &features
, errp
);
939 if (unlikely(r
< 0)) {
943 queue_pairs
= vhost_vdpa_get_max_queue_pairs(vdpa_device_fd
, features
,
945 if (queue_pairs
< 0) {
946 qemu_close(vdpa_device_fd
);
950 r
= vhost_vdpa_get_iova_range(vdpa_device_fd
, &iova_range
);
951 if (unlikely(r
< 0)) {
952 error_setg(errp
, "vhost-vdpa: get iova range failed: %s",
957 if (opts
->x_svq
&& !vhost_vdpa_net_valid_svq_features(features
, errp
)) {
961 ncs
= g_malloc0(sizeof(*ncs
) * queue_pairs
);
963 for (i
= 0; i
< queue_pairs
; i
++) {
964 ncs
[i
] = net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
965 vdpa_device_fd
, i
, 2, true, opts
->x_svq
,
966 iova_range
, features
);
972 nc
= net_vhost_vdpa_init(peer
, TYPE_VHOST_VDPA
, name
,
973 vdpa_device_fd
, i
, 1, false,
974 opts
->x_svq
, iova_range
, features
);
983 for (i
--; i
>= 0; i
--) {
984 qemu_del_net_client(ncs
[i
]);
988 qemu_close(vdpa_device_fd
);