xref: /openbmc/qemu/net/vhost-vdpa.c (revision 9c363cf6)
11e0a84eaSCindy Lu /*
21e0a84eaSCindy Lu  * vhost-vdpa.c
31e0a84eaSCindy Lu  *
41e0a84eaSCindy Lu  * Copyright(c) 2017-2018 Intel Corporation.
51e0a84eaSCindy Lu  * Copyright(c) 2020 Red Hat, Inc.
61e0a84eaSCindy Lu  *
71e0a84eaSCindy Lu  * This work is licensed under the terms of the GNU GPL, version 2 or later.
81e0a84eaSCindy Lu  * See the COPYING file in the top-level directory.
91e0a84eaSCindy Lu  *
101e0a84eaSCindy Lu  */
111e0a84eaSCindy Lu 
121e0a84eaSCindy Lu #include "qemu/osdep.h"
131e0a84eaSCindy Lu #include "clients.h"
14bd907ae4SEugenio Pérez #include "hw/virtio/virtio-net.h"
151e0a84eaSCindy Lu #include "net/vhost_net.h"
161e0a84eaSCindy Lu #include "net/vhost-vdpa.h"
171e0a84eaSCindy Lu #include "hw/virtio/vhost-vdpa.h"
181e0a84eaSCindy Lu #include "qemu/config-file.h"
191e0a84eaSCindy Lu #include "qemu/error-report.h"
20bd907ae4SEugenio Pérez #include "qemu/log.h"
21bd907ae4SEugenio Pérez #include "qemu/memalign.h"
221e0a84eaSCindy Lu #include "qemu/option.h"
231e0a84eaSCindy Lu #include "qapi/error.h"
2440237840SJason Wang #include <linux/vhost.h>
251e0a84eaSCindy Lu #include <sys/ioctl.h>
261e0a84eaSCindy Lu #include <err.h>
271e0a84eaSCindy Lu #include "standard-headers/linux/virtio_net.h"
281e0a84eaSCindy Lu #include "monitor/monitor.h"
2969498430SEugenio Pérez #include "migration/migration.h"
3069498430SEugenio Pérez #include "migration/misc.h"
311e0a84eaSCindy Lu #include "hw/virtio/vhost.h"
321e0a84eaSCindy Lu 
331e0a84eaSCindy Lu /* Todo:need to add the multiqueue support here */
341e0a84eaSCindy Lu typedef struct VhostVDPAState {
351e0a84eaSCindy Lu     NetClientState nc;
361e0a84eaSCindy Lu     struct vhost_vdpa vhost_vdpa;
3769498430SEugenio Pérez     Notifier migration_state;
381e0a84eaSCindy Lu     VHostNetState *vhost_net;
392df4dd31SEugenio Pérez 
402df4dd31SEugenio Pérez     /* Control commands shadow buffers */
4117fb889fSEugenio Pérez     void *cvq_cmd_out_buffer;
4217fb889fSEugenio Pérez     virtio_net_ctrl_ack *status;
4317fb889fSEugenio Pérez 
447f211a28SEugenio Pérez     /* The device always have SVQ enabled */
457f211a28SEugenio Pérez     bool always_svq;
461e0a84eaSCindy Lu     bool started;
471e0a84eaSCindy Lu } VhostVDPAState;
481e0a84eaSCindy Lu 
491e0a84eaSCindy Lu const int vdpa_feature_bits[] = {
501e0a84eaSCindy Lu     VIRTIO_F_NOTIFY_ON_EMPTY,
511e0a84eaSCindy Lu     VIRTIO_RING_F_INDIRECT_DESC,
521e0a84eaSCindy Lu     VIRTIO_RING_F_EVENT_IDX,
531e0a84eaSCindy Lu     VIRTIO_F_ANY_LAYOUT,
541e0a84eaSCindy Lu     VIRTIO_F_VERSION_1,
551e0a84eaSCindy Lu     VIRTIO_NET_F_CSUM,
561e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_CSUM,
571e0a84eaSCindy Lu     VIRTIO_NET_F_GSO,
581e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO4,
591e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO6,
601e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_ECN,
611e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_UFO,
621e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO4,
631e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO6,
641e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_ECN,
651e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_UFO,
661e0a84eaSCindy Lu     VIRTIO_NET_F_MRG_RXBUF,
671e0a84eaSCindy Lu     VIRTIO_NET_F_MTU,
6840237840SJason Wang     VIRTIO_NET_F_CTRL_RX,
6940237840SJason Wang     VIRTIO_NET_F_CTRL_RX_EXTRA,
7040237840SJason Wang     VIRTIO_NET_F_CTRL_VLAN,
7140237840SJason Wang     VIRTIO_NET_F_CTRL_MAC_ADDR,
7240237840SJason Wang     VIRTIO_NET_F_RSS,
7340237840SJason Wang     VIRTIO_NET_F_MQ,
7440237840SJason Wang     VIRTIO_NET_F_CTRL_VQ,
751e0a84eaSCindy Lu     VIRTIO_F_IOMMU_PLATFORM,
761e0a84eaSCindy Lu     VIRTIO_F_RING_PACKED,
77562a7d23SStefano Garzarella     VIRTIO_F_RING_RESET,
780145c393SAndrew Melnychenko     VIRTIO_NET_F_RSS,
790145c393SAndrew Melnychenko     VIRTIO_NET_F_HASH_REPORT,
809aa47eddSSi-Wei Liu     VIRTIO_NET_F_STATUS,
811e0a84eaSCindy Lu     VHOST_INVALID_FEATURE_BIT
821e0a84eaSCindy Lu };
831e0a84eaSCindy Lu 
841576dbb5SEugenio Pérez /** Supported device specific feature bits with SVQ */
851576dbb5SEugenio Pérez static const uint64_t vdpa_svq_device_features =
861576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CSUM) |
871576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
881576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MTU) |
891576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MAC) |
901576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
911576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
921576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
931576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
941576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
951576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
961576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
971576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
981576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
991576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STATUS) |
1001576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
10172b99a87SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MQ) |
1021576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
1031576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
1041576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
1051576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STANDBY);
1061576dbb5SEugenio Pérez 
107c1a10086SEugenio Pérez #define VHOST_VDPA_NET_CVQ_ASID 1
108c1a10086SEugenio Pérez 
1091e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
1101e0a84eaSCindy Lu {
1111e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1121e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1131e0a84eaSCindy Lu     return s->vhost_net;
1141e0a84eaSCindy Lu }
1151e0a84eaSCindy Lu 
11636e46472SEugenio Pérez static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
11736e46472SEugenio Pérez {
11836e46472SEugenio Pérez     uint64_t invalid_dev_features =
11936e46472SEugenio Pérez         features & ~vdpa_svq_device_features &
12036e46472SEugenio Pérez         /* Transport are all accepted at this point */
12136e46472SEugenio Pérez         ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
12236e46472SEugenio Pérez                          VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
12336e46472SEugenio Pérez 
12436e46472SEugenio Pérez     if (invalid_dev_features) {
12536e46472SEugenio Pérez         error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
12636e46472SEugenio Pérez                    invalid_dev_features);
127258a0394SEugenio Pérez         return false;
12836e46472SEugenio Pérez     }
12936e46472SEugenio Pérez 
130258a0394SEugenio Pérez     return vhost_svq_valid_features(features, errp);
13136e46472SEugenio Pérez }
13236e46472SEugenio Pérez 
1331e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
1341e0a84eaSCindy Lu {
1351e0a84eaSCindy Lu     uint32_t device_id;
1361e0a84eaSCindy Lu     int ret;
1371e0a84eaSCindy Lu     struct vhost_dev *hdev;
1381e0a84eaSCindy Lu 
1391e0a84eaSCindy Lu     hdev = (struct vhost_dev *)&net->dev;
1401e0a84eaSCindy Lu     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
1411e0a84eaSCindy Lu     if (device_id != VIRTIO_ID_NET) {
1421e0a84eaSCindy Lu         return -ENOTSUP;
1431e0a84eaSCindy Lu     }
1441e0a84eaSCindy Lu     return ret;
1451e0a84eaSCindy Lu }
1461e0a84eaSCindy Lu 
14740237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be,
14840237840SJason Wang                           int queue_pair_index, int nvqs)
1491e0a84eaSCindy Lu {
1501e0a84eaSCindy Lu     VhostNetOptions options;
1511e0a84eaSCindy Lu     struct vhost_net *net = NULL;
1521e0a84eaSCindy Lu     VhostVDPAState *s;
1531e0a84eaSCindy Lu     int ret;
1541e0a84eaSCindy Lu 
1551e0a84eaSCindy Lu     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
1561e0a84eaSCindy Lu     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1571e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, ncs);
1581e0a84eaSCindy Lu     options.net_backend = ncs;
1591e0a84eaSCindy Lu     options.opaque      = be;
1601e0a84eaSCindy Lu     options.busyloop_timeout = 0;
16140237840SJason Wang     options.nvqs = nvqs;
1621e0a84eaSCindy Lu 
1631e0a84eaSCindy Lu     net = vhost_net_init(&options);
1641e0a84eaSCindy Lu     if (!net) {
1651e0a84eaSCindy Lu         error_report("failed to init vhost_net for queue");
166a97ef87aSJason Wang         goto err_init;
1671e0a84eaSCindy Lu     }
1681e0a84eaSCindy Lu     s->vhost_net = net;
1691e0a84eaSCindy Lu     ret = vhost_vdpa_net_check_device_id(net);
1701e0a84eaSCindy Lu     if (ret) {
171a97ef87aSJason Wang         goto err_check;
1721e0a84eaSCindy Lu     }
1731e0a84eaSCindy Lu     return 0;
174a97ef87aSJason Wang err_check:
1751e0a84eaSCindy Lu     vhost_net_cleanup(net);
176ab36edcfSJason Wang     g_free(net);
177a97ef87aSJason Wang err_init:
1781e0a84eaSCindy Lu     return -1;
1791e0a84eaSCindy Lu }
1801e0a84eaSCindy Lu 
1811e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc)
1821e0a84eaSCindy Lu {
1831e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1841e0a84eaSCindy Lu 
1852df4dd31SEugenio Pérez     qemu_vfree(s->cvq_cmd_out_buffer);
18617fb889fSEugenio Pérez     qemu_vfree(s->status);
1871e0a84eaSCindy Lu     if (s->vhost_net) {
1881e0a84eaSCindy Lu         vhost_net_cleanup(s->vhost_net);
1891e0a84eaSCindy Lu         g_free(s->vhost_net);
1901e0a84eaSCindy Lu         s->vhost_net = NULL;
1911e0a84eaSCindy Lu     }
19257b3a7d8SCindy Lu      if (s->vhost_vdpa.device_fd >= 0) {
19357b3a7d8SCindy Lu         qemu_close(s->vhost_vdpa.device_fd);
19457b3a7d8SCindy Lu         s->vhost_vdpa.device_fd = -1;
19557b3a7d8SCindy Lu     }
1961e0a84eaSCindy Lu }
1971e0a84eaSCindy Lu 
1981e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
1991e0a84eaSCindy Lu {
2001e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2011e0a84eaSCindy Lu 
2021e0a84eaSCindy Lu     return true;
2031e0a84eaSCindy Lu }
2041e0a84eaSCindy Lu 
2051e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc)
2061e0a84eaSCindy Lu {
2071e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2081e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
2091e0a84eaSCindy Lu     uint64_t features = 0;
2101e0a84eaSCindy Lu     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
2111e0a84eaSCindy Lu     features = vhost_net_get_features(s->vhost_net, features);
2121e0a84eaSCindy Lu     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
2131e0a84eaSCindy Lu 
2141e0a84eaSCindy Lu }
2151e0a84eaSCindy Lu 
216ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
217ee8a1c63SKevin Wolf                                        Error **errp)
218ee8a1c63SKevin Wolf {
219ee8a1c63SKevin Wolf     const char *driver = object_class_get_name(oc);
220ee8a1c63SKevin Wolf 
221ee8a1c63SKevin Wolf     if (!g_str_has_prefix(driver, "virtio-net-")) {
222ee8a1c63SKevin Wolf         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
223ee8a1c63SKevin Wolf         return false;
224ee8a1c63SKevin Wolf     }
225ee8a1c63SKevin Wolf 
226ee8a1c63SKevin Wolf     return true;
227ee8a1c63SKevin Wolf }
228ee8a1c63SKevin Wolf 
229846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */
230846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
231846a1e85SEugenio Pérez                                   size_t size)
232846a1e85SEugenio Pérez {
233bc5add1dSSi-Wei Liu     return size;
234846a1e85SEugenio Pérez }
235846a1e85SEugenio Pérez 
23600ef422eSEugenio Pérez /** From any vdpa net client, get the netclient of the first queue pair */
23700ef422eSEugenio Pérez static VhostVDPAState *vhost_vdpa_net_first_nc_vdpa(VhostVDPAState *s)
23800ef422eSEugenio Pérez {
23900ef422eSEugenio Pérez     NICState *nic = qemu_get_nic(s->nc.peer);
24000ef422eSEugenio Pérez     NetClientState *nc0 = qemu_get_peer(nic->ncs, 0);
24100ef422eSEugenio Pérez 
24200ef422eSEugenio Pérez     return DO_UPCAST(VhostVDPAState, nc, nc0);
24300ef422eSEugenio Pérez }
24400ef422eSEugenio Pérez 
24569498430SEugenio Pérez static void vhost_vdpa_net_log_global_enable(VhostVDPAState *s, bool enable)
24669498430SEugenio Pérez {
24769498430SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
24869498430SEugenio Pérez     VirtIONet *n;
24969498430SEugenio Pérez     VirtIODevice *vdev;
25069498430SEugenio Pérez     int data_queue_pairs, cvq, r;
25169498430SEugenio Pérez 
25269498430SEugenio Pérez     /* We are only called on the first data vqs and only if x-svq is not set */
25369498430SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled == enable) {
25469498430SEugenio Pérez         return;
25569498430SEugenio Pérez     }
25669498430SEugenio Pérez 
25769498430SEugenio Pérez     vdev = v->dev->vdev;
25869498430SEugenio Pérez     n = VIRTIO_NET(vdev);
25969498430SEugenio Pérez     if (!n->vhost_started) {
26069498430SEugenio Pérez         return;
26169498430SEugenio Pérez     }
26269498430SEugenio Pérez 
26369498430SEugenio Pérez     data_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
26469498430SEugenio Pérez     cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
26569498430SEugenio Pérez                                   n->max_ncs - n->max_queue_pairs : 0;
26669498430SEugenio Pérez     /*
26769498430SEugenio Pérez      * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
26869498430SEugenio Pérez      * in the future and resume the device if read-only operations between
26969498430SEugenio Pérez      * suspend and reset goes wrong.
27069498430SEugenio Pérez      */
27169498430SEugenio Pérez     vhost_net_stop(vdev, n->nic->ncs, data_queue_pairs, cvq);
27269498430SEugenio Pérez 
27369498430SEugenio Pérez     /* Start will check migration setup_or_active to configure or not SVQ */
27469498430SEugenio Pérez     r = vhost_net_start(vdev, n->nic->ncs, data_queue_pairs, cvq);
27569498430SEugenio Pérez     if (unlikely(r < 0)) {
27669498430SEugenio Pérez         error_report("unable to start vhost net: %s(%d)", g_strerror(-r), -r);
27769498430SEugenio Pérez     }
27869498430SEugenio Pérez }
27969498430SEugenio Pérez 
28069498430SEugenio Pérez static void vdpa_net_migration_state_notifier(Notifier *notifier, void *data)
28169498430SEugenio Pérez {
28269498430SEugenio Pérez     MigrationState *migration = data;
28369498430SEugenio Pérez     VhostVDPAState *s = container_of(notifier, VhostVDPAState,
28469498430SEugenio Pérez                                      migration_state);
28569498430SEugenio Pérez 
28669498430SEugenio Pérez     if (migration_in_setup(migration)) {
28769498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, true);
28869498430SEugenio Pérez     } else if (migration_has_failed(migration)) {
28969498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, false);
29069498430SEugenio Pérez     }
29169498430SEugenio Pérez }
29269498430SEugenio Pérez 
29300ef422eSEugenio Pérez static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
29400ef422eSEugenio Pérez {
29500ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
29600ef422eSEugenio Pérez 
29769498430SEugenio Pérez     add_migration_state_change_notifier(&s->migration_state);
29800ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
29900ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
30000ef422eSEugenio Pérez                                            v->iova_range.last);
30100ef422eSEugenio Pérez     }
30200ef422eSEugenio Pérez }
30300ef422eSEugenio Pérez 
30400ef422eSEugenio Pérez static int vhost_vdpa_net_data_start(NetClientState *nc)
30500ef422eSEugenio Pérez {
30600ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
30700ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
30800ef422eSEugenio Pérez 
30900ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
31000ef422eSEugenio Pérez 
31169498430SEugenio Pérez     if (s->always_svq ||
31269498430SEugenio Pérez         migration_is_setup_or_active(migrate_get_current()->state)) {
31369498430SEugenio Pérez         v->shadow_vqs_enabled = true;
31469498430SEugenio Pérez         v->shadow_data = true;
31569498430SEugenio Pérez     } else {
31669498430SEugenio Pérez         v->shadow_vqs_enabled = false;
31769498430SEugenio Pérez         v->shadow_data = false;
31869498430SEugenio Pérez     }
31969498430SEugenio Pérez 
32000ef422eSEugenio Pérez     if (v->index == 0) {
32100ef422eSEugenio Pérez         vhost_vdpa_net_data_start_first(s);
32200ef422eSEugenio Pérez         return 0;
32300ef422eSEugenio Pérez     }
32400ef422eSEugenio Pérez 
32500ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
32600ef422eSEugenio Pérez         VhostVDPAState *s0 = vhost_vdpa_net_first_nc_vdpa(s);
32700ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
32800ef422eSEugenio Pérez     }
32900ef422eSEugenio Pérez 
33000ef422eSEugenio Pérez     return 0;
33100ef422eSEugenio Pérez }
33200ef422eSEugenio Pérez 
33300ef422eSEugenio Pérez static void vhost_vdpa_net_client_stop(NetClientState *nc)
33400ef422eSEugenio Pérez {
33500ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
33600ef422eSEugenio Pérez     struct vhost_dev *dev;
33700ef422eSEugenio Pérez 
33800ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
33900ef422eSEugenio Pérez 
34069498430SEugenio Pérez     if (s->vhost_vdpa.index == 0) {
34169498430SEugenio Pérez         remove_migration_state_change_notifier(&s->migration_state);
34269498430SEugenio Pérez     }
34369498430SEugenio Pérez 
34400ef422eSEugenio Pérez     dev = s->vhost_vdpa.dev;
34500ef422eSEugenio Pérez     if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
34600ef422eSEugenio Pérez         g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
34700ef422eSEugenio Pérez     }
34800ef422eSEugenio Pérez }
34900ef422eSEugenio Pérez 
3501e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = {
3511e0a84eaSCindy Lu         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
3521e0a84eaSCindy Lu         .size = sizeof(VhostVDPAState),
353846a1e85SEugenio Pérez         .receive = vhost_vdpa_receive,
35400ef422eSEugenio Pérez         .start = vhost_vdpa_net_data_start,
35500ef422eSEugenio Pérez         .stop = vhost_vdpa_net_client_stop,
3561e0a84eaSCindy Lu         .cleanup = vhost_vdpa_cleanup,
3571e0a84eaSCindy Lu         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
3581e0a84eaSCindy Lu         .has_ufo = vhost_vdpa_has_ufo,
359ee8a1c63SKevin Wolf         .check_peer_type = vhost_vdpa_check_peer_type,
3601e0a84eaSCindy Lu };
3611e0a84eaSCindy Lu 
362c1a10086SEugenio Pérez static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index)
363c1a10086SEugenio Pérez {
364c1a10086SEugenio Pérez     struct vhost_vring_state state = {
365c1a10086SEugenio Pérez         .index = vq_index,
366c1a10086SEugenio Pérez     };
367c1a10086SEugenio Pérez     int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
368c1a10086SEugenio Pérez 
369c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
370c1a10086SEugenio Pérez         error_report("Cannot get VQ %u group: %s", vq_index,
371c1a10086SEugenio Pérez                      g_strerror(errno));
372c1a10086SEugenio Pérez         return r;
373c1a10086SEugenio Pérez     }
374c1a10086SEugenio Pérez 
375c1a10086SEugenio Pérez     return state.num;
376c1a10086SEugenio Pérez }
377c1a10086SEugenio Pérez 
378c1a10086SEugenio Pérez static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
379c1a10086SEugenio Pérez                                            unsigned vq_group,
380c1a10086SEugenio Pérez                                            unsigned asid_num)
381c1a10086SEugenio Pérez {
382c1a10086SEugenio Pérez     struct vhost_vring_state asid = {
383c1a10086SEugenio Pérez         .index = vq_group,
384c1a10086SEugenio Pérez         .num = asid_num,
385c1a10086SEugenio Pérez     };
386c1a10086SEugenio Pérez     int r;
387c1a10086SEugenio Pérez 
388c1a10086SEugenio Pérez     r = ioctl(v->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
389c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
390c1a10086SEugenio Pérez         error_report("Can't set vq group %u asid %u, errno=%d (%s)",
391c1a10086SEugenio Pérez                      asid.index, asid.num, errno, g_strerror(errno));
392c1a10086SEugenio Pérez     }
393c1a10086SEugenio Pérez     return r;
394c1a10086SEugenio Pérez }
395c1a10086SEugenio Pérez 
3962df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
3972df4dd31SEugenio Pérez {
3982df4dd31SEugenio Pérez     VhostIOVATree *tree = v->iova_tree;
3992df4dd31SEugenio Pérez     DMAMap needle = {
4002df4dd31SEugenio Pérez         /*
4012df4dd31SEugenio Pérez          * No need to specify size or to look for more translations since
4022df4dd31SEugenio Pérez          * this contiguous chunk was allocated by us.
4032df4dd31SEugenio Pérez          */
4042df4dd31SEugenio Pérez         .translated_addr = (hwaddr)(uintptr_t)addr,
4052df4dd31SEugenio Pérez     };
4062df4dd31SEugenio Pérez     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
4072df4dd31SEugenio Pérez     int r;
4082df4dd31SEugenio Pérez 
4092df4dd31SEugenio Pérez     if (unlikely(!map)) {
4102df4dd31SEugenio Pérez         error_report("Cannot locate expected map");
4112df4dd31SEugenio Pérez         return;
4122df4dd31SEugenio Pérez     }
4132df4dd31SEugenio Pérez 
414cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_unmap(v, v->address_space_id, map->iova, map->size + 1);
4152df4dd31SEugenio Pérez     if (unlikely(r != 0)) {
4162df4dd31SEugenio Pérez         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
4172df4dd31SEugenio Pérez     }
4182df4dd31SEugenio Pérez 
41969292a8eSEugenio Pérez     vhost_iova_tree_remove(tree, *map);
4202df4dd31SEugenio Pérez }
4212df4dd31SEugenio Pérez 
4222df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void)
4232df4dd31SEugenio Pérez {
4242df4dd31SEugenio Pérez     /*
4252df4dd31SEugenio Pérez      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
4262df4dd31SEugenio Pérez      * In buffer is always 1 byte, so it should fit here
4272df4dd31SEugenio Pérez      */
4282df4dd31SEugenio Pérez     return sizeof(struct virtio_net_ctrl_hdr) +
4292df4dd31SEugenio Pérez            2 * sizeof(struct virtio_net_ctrl_mac) +
4302df4dd31SEugenio Pérez            MAC_TABLE_ENTRIES * ETH_ALEN;
4312df4dd31SEugenio Pérez }
4322df4dd31SEugenio Pérez 
4332df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
4342df4dd31SEugenio Pérez {
4352df4dd31SEugenio Pérez     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
4362df4dd31SEugenio Pérez }
4372df4dd31SEugenio Pérez 
4387a7f87e9SEugenio Pérez /** Map CVQ buffer. */
4397a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
4407a7f87e9SEugenio Pérez                                   bool write)
4412df4dd31SEugenio Pérez {
4422df4dd31SEugenio Pérez     DMAMap map = {};
4432df4dd31SEugenio Pérez     int r;
4442df4dd31SEugenio Pérez 
4452df4dd31SEugenio Pérez     map.translated_addr = (hwaddr)(uintptr_t)buf;
4467a7f87e9SEugenio Pérez     map.size = size - 1;
4472df4dd31SEugenio Pérez     map.perm = write ? IOMMU_RW : IOMMU_RO,
4482df4dd31SEugenio Pérez     r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
4492df4dd31SEugenio Pérez     if (unlikely(r != IOVA_OK)) {
4502df4dd31SEugenio Pérez         error_report("Cannot map injected element");
4517a7f87e9SEugenio Pérez         return r;
4522df4dd31SEugenio Pérez     }
4532df4dd31SEugenio Pérez 
454cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_map(v, v->address_space_id, map.iova,
455cd831ed5SEugenio Pérez                            vhost_vdpa_net_cvq_cmd_page_len(), buf, !write);
4562df4dd31SEugenio Pérez     if (unlikely(r < 0)) {
4572df4dd31SEugenio Pérez         goto dma_map_err;
4582df4dd31SEugenio Pérez     }
4592df4dd31SEugenio Pérez 
4607a7f87e9SEugenio Pérez     return 0;
4612df4dd31SEugenio Pérez 
4622df4dd31SEugenio Pérez dma_map_err:
46369292a8eSEugenio Pérez     vhost_iova_tree_remove(v->iova_tree, map);
4647a7f87e9SEugenio Pérez     return r;
4652df4dd31SEugenio Pérez }
4662df4dd31SEugenio Pérez 
4677a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc)
4682df4dd31SEugenio Pérez {
46900ef422eSEugenio Pérez     VhostVDPAState *s, *s0;
470c1a10086SEugenio Pérez     struct vhost_vdpa *v;
471c1a10086SEugenio Pérez     uint64_t backend_features;
472c1a10086SEugenio Pérez     int64_t cvq_group;
473c1a10086SEugenio Pérez     int cvq_index, r;
4742df4dd31SEugenio Pérez 
4757a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
4767a7f87e9SEugenio Pérez 
4777a7f87e9SEugenio Pérez     s = DO_UPCAST(VhostVDPAState, nc, nc);
478c1a10086SEugenio Pérez     v = &s->vhost_vdpa;
479c1a10086SEugenio Pérez 
48069498430SEugenio Pérez     s0 = vhost_vdpa_net_first_nc_vdpa(s);
48169498430SEugenio Pérez     v->shadow_data = s0->vhost_vdpa.shadow_vqs_enabled;
482c1a10086SEugenio Pérez     v->shadow_vqs_enabled = s->always_svq;
483c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
484c1a10086SEugenio Pérez 
48569498430SEugenio Pérez     if (s->vhost_vdpa.shadow_data) {
486c1a10086SEugenio Pérez         /* SVQ is already configured for all virtqueues */
487c1a10086SEugenio Pérez         goto out;
488c1a10086SEugenio Pérez     }
489c1a10086SEugenio Pérez 
490c1a10086SEugenio Pérez     /*
491c1a10086SEugenio Pérez      * If we early return in these cases SVQ will not be enabled. The migration
492c1a10086SEugenio Pérez      * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
493c1a10086SEugenio Pérez      *
494c1a10086SEugenio Pérez      * Calling VHOST_GET_BACKEND_FEATURES as they are not available in v->dev
495c1a10086SEugenio Pérez      * yet.
496c1a10086SEugenio Pérez      */
497c1a10086SEugenio Pérez     r = ioctl(v->device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
498c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
499c1a10086SEugenio Pérez         error_report("Cannot get vdpa backend_features: %s(%d)",
500c1a10086SEugenio Pérez             g_strerror(errno), errno);
501c1a10086SEugenio Pérez         return -1;
502c1a10086SEugenio Pérez     }
503525ae115SEugenio Pérez     if (!(backend_features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID)) ||
504c1a10086SEugenio Pérez         !vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
505c1a10086SEugenio Pérez         return 0;
506c1a10086SEugenio Pérez     }
507c1a10086SEugenio Pérez 
508c1a10086SEugenio Pérez     /*
509c1a10086SEugenio Pérez      * Check if all the virtqueues of the virtio device are in a different vq
510c1a10086SEugenio Pérez      * than the last vq. VQ group of last group passed in cvq_group.
511c1a10086SEugenio Pérez      */
512c1a10086SEugenio Pérez     cvq_index = v->dev->vq_index_end - 1;
513c1a10086SEugenio Pérez     cvq_group = vhost_vdpa_get_vring_group(v->device_fd, cvq_index);
514c1a10086SEugenio Pérez     if (unlikely(cvq_group < 0)) {
515c1a10086SEugenio Pérez         return cvq_group;
516c1a10086SEugenio Pérez     }
517c1a10086SEugenio Pérez     for (int i = 0; i < cvq_index; ++i) {
518c1a10086SEugenio Pérez         int64_t group = vhost_vdpa_get_vring_group(v->device_fd, i);
519c1a10086SEugenio Pérez 
520c1a10086SEugenio Pérez         if (unlikely(group < 0)) {
521c1a10086SEugenio Pérez             return group;
522c1a10086SEugenio Pérez         }
523c1a10086SEugenio Pérez 
524c1a10086SEugenio Pérez         if (group == cvq_group) {
525c1a10086SEugenio Pérez             return 0;
526c1a10086SEugenio Pérez         }
527c1a10086SEugenio Pérez     }
528c1a10086SEugenio Pérez 
529c1a10086SEugenio Pérez     r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
530c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
531c1a10086SEugenio Pérez         return r;
532c1a10086SEugenio Pérez     }
533c1a10086SEugenio Pérez 
534c1a10086SEugenio Pérez     v->shadow_vqs_enabled = true;
535c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
536c1a10086SEugenio Pérez 
537c1a10086SEugenio Pérez out:
5387a7f87e9SEugenio Pérez     if (!s->vhost_vdpa.shadow_vqs_enabled) {
5397a7f87e9SEugenio Pérez         return 0;
5402df4dd31SEugenio Pérez     }
5412df4dd31SEugenio Pérez 
54200ef422eSEugenio Pérez     if (s0->vhost_vdpa.iova_tree) {
54300ef422eSEugenio Pérez         /*
54400ef422eSEugenio Pérez          * SVQ is already configured for all virtqueues.  Reuse IOVA tree for
54500ef422eSEugenio Pérez          * simplicity, whether CVQ shares ASID with guest or not, because:
54600ef422eSEugenio Pérez          * - Memory listener need access to guest's memory addresses allocated
54700ef422eSEugenio Pérez          *   in the IOVA tree.
54800ef422eSEugenio Pérez          * - There should be plenty of IOVA address space for both ASID not to
54900ef422eSEugenio Pérez          *   worry about collisions between them.  Guest's translations are
55000ef422eSEugenio Pérez          *   still validated with virtio virtqueue_pop so there is no risk for
55100ef422eSEugenio Pérez          *   the guest to access memory that it shouldn't.
55200ef422eSEugenio Pérez          *
55300ef422eSEugenio Pérez          * To allocate a iova tree per ASID is doable but it complicates the
55400ef422eSEugenio Pérez          * code and it is not worth it for the moment.
55500ef422eSEugenio Pérez          */
55600ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
55700ef422eSEugenio Pérez     } else {
55800ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
55900ef422eSEugenio Pérez                                            v->iova_range.last);
56000ef422eSEugenio Pérez     }
56100ef422eSEugenio Pérez 
5627a7f87e9SEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
5637a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), false);
5647a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5657a7f87e9SEugenio Pérez         return r;
5667a7f87e9SEugenio Pérez     }
5677a7f87e9SEugenio Pérez 
56817fb889fSEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
5697a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), true);
5707a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5712df4dd31SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
5722df4dd31SEugenio Pérez     }
5732df4dd31SEugenio Pérez 
5747a7f87e9SEugenio Pérez     return r;
5757a7f87e9SEugenio Pérez }
5767a7f87e9SEugenio Pérez 
5777a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
5787a7f87e9SEugenio Pérez {
5797a7f87e9SEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
5807a7f87e9SEugenio Pérez 
5817a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
5827a7f87e9SEugenio Pérez 
5837a7f87e9SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled) {
5847a7f87e9SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
58517fb889fSEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
586c1a10086SEugenio Pérez     }
58700ef422eSEugenio Pérez 
58800ef422eSEugenio Pérez     vhost_vdpa_net_client_stop(nc);
5892df4dd31SEugenio Pérez }
5902df4dd31SEugenio Pérez 
591be4278b6SEugenio Pérez static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len,
592be4278b6SEugenio Pérez                                       size_t in_len)
593be4278b6SEugenio Pérez {
594be4278b6SEugenio Pérez     /* Buffers for the device */
595be4278b6SEugenio Pérez     const struct iovec out = {
596be4278b6SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
597be4278b6SEugenio Pérez         .iov_len = out_len,
598be4278b6SEugenio Pérez     };
599be4278b6SEugenio Pérez     const struct iovec in = {
60017fb889fSEugenio Pérez         .iov_base = s->status,
601be4278b6SEugenio Pérez         .iov_len = sizeof(virtio_net_ctrl_ack),
602be4278b6SEugenio Pérez     };
603be4278b6SEugenio Pérez     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
604be4278b6SEugenio Pérez     int r;
605be4278b6SEugenio Pérez 
606be4278b6SEugenio Pérez     r = vhost_svq_add(svq, &out, 1, &in, 1, NULL);
607be4278b6SEugenio Pérez     if (unlikely(r != 0)) {
608be4278b6SEugenio Pérez         if (unlikely(r == -ENOSPC)) {
609be4278b6SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
610be4278b6SEugenio Pérez                           __func__);
611be4278b6SEugenio Pérez         }
612be4278b6SEugenio Pérez         return r;
613be4278b6SEugenio Pérez     }
614be4278b6SEugenio Pérez 
615be4278b6SEugenio Pérez     /*
616be4278b6SEugenio Pérez      * We can poll here since we've had BQL from the time we sent the
617be4278b6SEugenio Pérez      * descriptor. Also, we need to take the answer before SVQ pulls by itself,
618be4278b6SEugenio Pérez      * when BQL is released
619be4278b6SEugenio Pérez      */
620be4278b6SEugenio Pérez     return vhost_svq_poll(svq);
621be4278b6SEugenio Pérez }
622be4278b6SEugenio Pérez 
623f73c0c43SEugenio Pérez static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class,
624f73c0c43SEugenio Pérez                                        uint8_t cmd, const void *data,
625f73c0c43SEugenio Pérez                                        size_t data_size)
626f73c0c43SEugenio Pérez {
627f73c0c43SEugenio Pérez     const struct virtio_net_ctrl_hdr ctrl = {
628f73c0c43SEugenio Pérez         .class = class,
629f73c0c43SEugenio Pérez         .cmd = cmd,
630f73c0c43SEugenio Pérez     };
631f73c0c43SEugenio Pérez 
632f73c0c43SEugenio Pérez     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
633f73c0c43SEugenio Pérez 
634f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl));
635f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size);
636f73c0c43SEugenio Pérez 
637f73c0c43SEugenio Pérez     return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size,
638f73c0c43SEugenio Pérez                                   sizeof(virtio_net_ctrl_ack));
639f73c0c43SEugenio Pérez }
640f73c0c43SEugenio Pérez 
641f73c0c43SEugenio Pérez static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n)
642f73c0c43SEugenio Pérez {
643f73c0c43SEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
644f73c0c43SEugenio Pérez     if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) {
645f73c0c43SEugenio Pérez         ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC,
646f73c0c43SEugenio Pérez                                                   VIRTIO_NET_CTRL_MAC_ADDR_SET,
647f73c0c43SEugenio Pérez                                                   n->mac, sizeof(n->mac));
648f73c0c43SEugenio Pérez         if (unlikely(dev_written < 0)) {
649f73c0c43SEugenio Pérez             return dev_written;
650f73c0c43SEugenio Pérez         }
651f73c0c43SEugenio Pérez 
652f73c0c43SEugenio Pérez         return *s->status != VIRTIO_NET_OK;
653f73c0c43SEugenio Pérez     }
654f73c0c43SEugenio Pérez 
655f73c0c43SEugenio Pérez     return 0;
656f73c0c43SEugenio Pérez }
657f73c0c43SEugenio Pérez 
658f64c7cdaSEugenio Pérez static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
659f64c7cdaSEugenio Pérez                                   const VirtIONet *n)
660f64c7cdaSEugenio Pérez {
661f64c7cdaSEugenio Pérez     struct virtio_net_ctrl_mq mq;
662f64c7cdaSEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
663f64c7cdaSEugenio Pérez     ssize_t dev_written;
664f64c7cdaSEugenio Pérez 
665f64c7cdaSEugenio Pérez     if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) {
666f64c7cdaSEugenio Pérez         return 0;
667f64c7cdaSEugenio Pérez     }
668f64c7cdaSEugenio Pérez 
669f64c7cdaSEugenio Pérez     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
670f64c7cdaSEugenio Pérez     dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ,
671f64c7cdaSEugenio Pérez                                           VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq,
672f64c7cdaSEugenio Pérez                                           sizeof(mq));
673f64c7cdaSEugenio Pérez     if (unlikely(dev_written < 0)) {
674f64c7cdaSEugenio Pérez         return dev_written;
675f64c7cdaSEugenio Pérez     }
676f64c7cdaSEugenio Pérez 
677f64c7cdaSEugenio Pérez     return *s->status != VIRTIO_NET_OK;
678f64c7cdaSEugenio Pérez }
679f64c7cdaSEugenio Pérez 
680dd036d8dSEugenio Pérez static int vhost_vdpa_net_load(NetClientState *nc)
681dd036d8dSEugenio Pérez {
682dd036d8dSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
683f73c0c43SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
684dd036d8dSEugenio Pérez     const VirtIONet *n;
685f73c0c43SEugenio Pérez     int r;
686dd036d8dSEugenio Pérez 
687dd036d8dSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
688dd036d8dSEugenio Pérez 
689dd036d8dSEugenio Pérez     if (!v->shadow_vqs_enabled) {
690dd036d8dSEugenio Pérez         return 0;
691dd036d8dSEugenio Pérez     }
692dd036d8dSEugenio Pérez 
693dd036d8dSEugenio Pérez     n = VIRTIO_NET(v->dev->vdev);
694f73c0c43SEugenio Pérez     r = vhost_vdpa_net_load_mac(s, n);
695f73c0c43SEugenio Pérez     if (unlikely(r < 0)) {
696f73c0c43SEugenio Pérez         return r;
697dd036d8dSEugenio Pérez     }
698f64c7cdaSEugenio Pérez     r = vhost_vdpa_net_load_mq(s, n);
699f64c7cdaSEugenio Pérez     if (unlikely(r)) {
700f64c7cdaSEugenio Pérez         return r;
701f64c7cdaSEugenio Pérez     }
702dd036d8dSEugenio Pérez 
703dd036d8dSEugenio Pérez     return 0;
704dd036d8dSEugenio Pérez }
705dd036d8dSEugenio Pérez 
706f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = {
707f8972b56SEugenio Pérez     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
708f8972b56SEugenio Pérez     .size = sizeof(VhostVDPAState),
709f8972b56SEugenio Pérez     .receive = vhost_vdpa_receive,
7107a7f87e9SEugenio Pérez     .start = vhost_vdpa_net_cvq_start,
711dd036d8dSEugenio Pérez     .load = vhost_vdpa_net_load,
7127a7f87e9SEugenio Pérez     .stop = vhost_vdpa_net_cvq_stop,
713f8972b56SEugenio Pérez     .cleanup = vhost_vdpa_cleanup,
714f8972b56SEugenio Pérez     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
715f8972b56SEugenio Pérez     .has_ufo = vhost_vdpa_has_ufo,
716f8972b56SEugenio Pérez     .check_peer_type = vhost_vdpa_check_peer_type,
717f8972b56SEugenio Pérez };
718f8972b56SEugenio Pérez 
7192df4dd31SEugenio Pérez /**
7202df4dd31SEugenio Pérez  * Validate and copy control virtqueue commands.
7212df4dd31SEugenio Pérez  *
7222df4dd31SEugenio Pérez  * Following QEMU guidelines, we offer a copy of the buffers to the device to
7232df4dd31SEugenio Pérez  * prevent TOCTOU bugs.
724bd907ae4SEugenio Pérez  */
725bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
726bd907ae4SEugenio Pérez                                             VirtQueueElement *elem,
727bd907ae4SEugenio Pérez                                             void *opaque)
728bd907ae4SEugenio Pérez {
7292df4dd31SEugenio Pérez     VhostVDPAState *s = opaque;
730be4278b6SEugenio Pérez     size_t in_len;
731bd907ae4SEugenio Pérez     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
7327a7f87e9SEugenio Pérez     /* Out buffer sent to both the vdpa device and the device model */
7337a7f87e9SEugenio Pérez     struct iovec out = {
7347a7f87e9SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
7357a7f87e9SEugenio Pérez     };
7362df4dd31SEugenio Pérez     /* in buffer used for device model */
7372df4dd31SEugenio Pérez     const struct iovec in = {
7382df4dd31SEugenio Pérez         .iov_base = &status,
7392df4dd31SEugenio Pérez         .iov_len = sizeof(status),
7402df4dd31SEugenio Pérez     };
741be4278b6SEugenio Pérez     ssize_t dev_written = -EINVAL;
742bd907ae4SEugenio Pérez 
7437a7f87e9SEugenio Pérez     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
7447a7f87e9SEugenio Pérez                              s->cvq_cmd_out_buffer,
7457a7f87e9SEugenio Pérez                              vhost_vdpa_net_cvq_cmd_len());
7463f9a3eebSEugenio Pérez     if (*(uint8_t *)s->cvq_cmd_out_buffer == VIRTIO_NET_CTRL_ANNOUNCE) {
7473f9a3eebSEugenio Pérez         /*
7483f9a3eebSEugenio Pérez          * Guest announce capability is emulated by qemu, so don't forward to
7493f9a3eebSEugenio Pérez          * the device.
7503f9a3eebSEugenio Pérez          */
7513f9a3eebSEugenio Pérez         dev_written = sizeof(status);
7523f9a3eebSEugenio Pérez         *s->status = VIRTIO_NET_OK;
7533f9a3eebSEugenio Pérez     } else {
754be4278b6SEugenio Pérez         dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
755be4278b6SEugenio Pérez         if (unlikely(dev_written < 0)) {
756bd907ae4SEugenio Pérez             goto out;
757bd907ae4SEugenio Pérez         }
7583f9a3eebSEugenio Pérez     }
759bd907ae4SEugenio Pérez 
760bd907ae4SEugenio Pérez     if (unlikely(dev_written < sizeof(status))) {
761bd907ae4SEugenio Pérez         error_report("Insufficient written data (%zu)", dev_written);
7622df4dd31SEugenio Pérez         goto out;
7632df4dd31SEugenio Pérez     }
7642df4dd31SEugenio Pérez 
76517fb889fSEugenio Pérez     if (*s->status != VIRTIO_NET_OK) {
766be4278b6SEugenio Pérez         return VIRTIO_NET_ERR;
7672df4dd31SEugenio Pérez     }
7682df4dd31SEugenio Pérez 
7692df4dd31SEugenio Pérez     status = VIRTIO_NET_ERR;
7707a7f87e9SEugenio Pérez     virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
7712df4dd31SEugenio Pérez     if (status != VIRTIO_NET_OK) {
7722df4dd31SEugenio Pérez         error_report("Bad CVQ processing in model");
773bd907ae4SEugenio Pérez     }
774bd907ae4SEugenio Pérez 
775bd907ae4SEugenio Pérez out:
776bd907ae4SEugenio Pérez     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
777bd907ae4SEugenio Pérez                           sizeof(status));
778bd907ae4SEugenio Pérez     if (unlikely(in_len < sizeof(status))) {
779bd907ae4SEugenio Pérez         error_report("Bad device CVQ written length");
780bd907ae4SEugenio Pérez     }
781bd907ae4SEugenio Pérez     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
782bd907ae4SEugenio Pérez     g_free(elem);
783be4278b6SEugenio Pérez     return dev_written < 0 ? dev_written : 0;
784bd907ae4SEugenio Pérez }
785bd907ae4SEugenio Pérez 
786bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
787bd907ae4SEugenio Pérez     .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
788bd907ae4SEugenio Pérez };
789bd907ae4SEugenio Pérez 
790654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
791654790b6SJason Wang                                        const char *device,
792654790b6SJason Wang                                        const char *name,
79340237840SJason Wang                                        int vdpa_device_fd,
79440237840SJason Wang                                        int queue_pair_index,
79540237840SJason Wang                                        int nvqs,
7961576dbb5SEugenio Pérez                                        bool is_datapath,
7971576dbb5SEugenio Pérez                                        bool svq,
79800ef422eSEugenio Pérez                                        struct vhost_vdpa_iova_range iova_range)
7991e0a84eaSCindy Lu {
8001e0a84eaSCindy Lu     NetClientState *nc = NULL;
8011e0a84eaSCindy Lu     VhostVDPAState *s;
8021e0a84eaSCindy Lu     int ret = 0;
8031e0a84eaSCindy Lu     assert(name);
80440237840SJason Wang     if (is_datapath) {
80540237840SJason Wang         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
80640237840SJason Wang                                  name);
80740237840SJason Wang     } else {
808f8972b56SEugenio Pérez         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
80940237840SJason Wang                                          device, name);
81040237840SJason Wang     }
81153b85d95SLaurent Vivier     qemu_set_info_str(nc, TYPE_VHOST_VDPA);
8121e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, nc);
8137327813dSJason Wang 
8141e0a84eaSCindy Lu     s->vhost_vdpa.device_fd = vdpa_device_fd;
81540237840SJason Wang     s->vhost_vdpa.index = queue_pair_index;
8167f211a28SEugenio Pérez     s->always_svq = svq;
81769498430SEugenio Pérez     s->migration_state.notify = vdpa_net_migration_state_notifier;
8181576dbb5SEugenio Pérez     s->vhost_vdpa.shadow_vqs_enabled = svq;
819a585fad2SEugenio Pérez     s->vhost_vdpa.iova_range = iova_range;
8206188d78aSEugenio Pérez     s->vhost_vdpa.shadow_data = svq;
821bd907ae4SEugenio Pérez     if (!is_datapath) {
8222df4dd31SEugenio Pérez         s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
8232df4dd31SEugenio Pérez                                             vhost_vdpa_net_cvq_cmd_page_len());
8242df4dd31SEugenio Pérez         memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len());
82517fb889fSEugenio Pérez         s->status = qemu_memalign(qemu_real_host_page_size(),
8262df4dd31SEugenio Pérez                                   vhost_vdpa_net_cvq_cmd_page_len());
82717fb889fSEugenio Pérez         memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len());
8282df4dd31SEugenio Pérez 
829bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
830bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops_opaque = s;
831*9c363cf6SEugenio Pérez 
832*9c363cf6SEugenio Pérez         /*
833*9c363cf6SEugenio Pérez          * TODO: We cannot migrate devices with CVQ as there is no way to set
834*9c363cf6SEugenio Pérez          * the device state (MAC, MQ, etc) before starting the datapath.
835*9c363cf6SEugenio Pérez          *
836*9c363cf6SEugenio Pérez          * Migration blocker ownership now belongs to s->vhost_vdpa.
837*9c363cf6SEugenio Pérez          */
838*9c363cf6SEugenio Pérez         error_setg(&s->vhost_vdpa.migration_blocker,
839*9c363cf6SEugenio Pérez                    "net vdpa cannot migrate with CVQ feature");
840bd907ae4SEugenio Pérez     }
84140237840SJason Wang     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
84274af5eecSJason Wang     if (ret) {
84374af5eecSJason Wang         qemu_del_net_client(nc);
844654790b6SJason Wang         return NULL;
84574af5eecSJason Wang     }
846654790b6SJason Wang     return nc;
8471e0a84eaSCindy Lu }
8481e0a84eaSCindy Lu 
8498170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
8508170ab3fSEugenio Pérez {
8518170ab3fSEugenio Pérez     int ret = ioctl(fd, VHOST_GET_FEATURES, features);
8528170ab3fSEugenio Pérez     if (unlikely(ret < 0)) {
8538170ab3fSEugenio Pérez         error_setg_errno(errp, errno,
8548170ab3fSEugenio Pérez                          "Fail to query features from vhost-vDPA device");
8558170ab3fSEugenio Pérez     }
8568170ab3fSEugenio Pérez     return ret;
8578170ab3fSEugenio Pérez }
8588170ab3fSEugenio Pérez 
8598170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
8608170ab3fSEugenio Pérez                                           int *has_cvq, Error **errp)
86140237840SJason Wang {
86240237840SJason Wang     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
863cd523a41SStefano Garzarella     g_autofree struct vhost_vdpa_config *config = NULL;
86440237840SJason Wang     __virtio16 *max_queue_pairs;
86540237840SJason Wang     int ret;
86640237840SJason Wang 
86740237840SJason Wang     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
86840237840SJason Wang         *has_cvq = 1;
86940237840SJason Wang     } else {
87040237840SJason Wang         *has_cvq = 0;
87140237840SJason Wang     }
87240237840SJason Wang 
87340237840SJason Wang     if (features & (1 << VIRTIO_NET_F_MQ)) {
87440237840SJason Wang         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
87540237840SJason Wang         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
87640237840SJason Wang         config->len = sizeof(*max_queue_pairs);
87740237840SJason Wang 
87840237840SJason Wang         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
87940237840SJason Wang         if (ret) {
88040237840SJason Wang             error_setg(errp, "Fail to get config from vhost-vDPA device");
88140237840SJason Wang             return -ret;
88240237840SJason Wang         }
88340237840SJason Wang 
88440237840SJason Wang         max_queue_pairs = (__virtio16 *)&config->buf;
88540237840SJason Wang 
88640237840SJason Wang         return lduw_le_p(max_queue_pairs);
88740237840SJason Wang     }
88840237840SJason Wang 
88940237840SJason Wang     return 1;
89040237840SJason Wang }
89140237840SJason Wang 
8921e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
8931e0a84eaSCindy Lu                         NetClientState *peer, Error **errp)
8941e0a84eaSCindy Lu {
8951e0a84eaSCindy Lu     const NetdevVhostVDPAOptions *opts;
8968170ab3fSEugenio Pérez     uint64_t features;
897654790b6SJason Wang     int vdpa_device_fd;
898eb3cb751SEugenio Pérez     g_autofree NetClientState **ncs = NULL;
899a585fad2SEugenio Pérez     struct vhost_vdpa_iova_range iova_range;
900eb3cb751SEugenio Pérez     NetClientState *nc;
901aed5da45SEugenio Pérez     int queue_pairs, r, i = 0, has_cvq = 0;
9021e0a84eaSCindy Lu 
9031e0a84eaSCindy Lu     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
9041e0a84eaSCindy Lu     opts = &netdev->u.vhost_vdpa;
9057480874aSMarkus Armbruster     if (!opts->vhostdev && !opts->vhostfd) {
9068801ccd0SSi-Wei Liu         error_setg(errp,
9078801ccd0SSi-Wei Liu                    "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
908c8295404SEugenio Pérez         return -1;
909c8295404SEugenio Pérez     }
9107327813dSJason Wang 
9117480874aSMarkus Armbruster     if (opts->vhostdev && opts->vhostfd) {
9128801ccd0SSi-Wei Liu         error_setg(errp,
9138801ccd0SSi-Wei Liu                    "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
9148801ccd0SSi-Wei Liu         return -1;
9158801ccd0SSi-Wei Liu     }
9168801ccd0SSi-Wei Liu 
9177480874aSMarkus Armbruster     if (opts->vhostdev) {
9180351152bSEugenio Pérez         vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
9197327813dSJason Wang         if (vdpa_device_fd == -1) {
9207327813dSJason Wang             return -errno;
9217327813dSJason Wang         }
9225107fd3eSPeter Maydell     } else {
9235107fd3eSPeter Maydell         /* has_vhostfd */
9248801ccd0SSi-Wei Liu         vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
9258801ccd0SSi-Wei Liu         if (vdpa_device_fd == -1) {
9268801ccd0SSi-Wei Liu             error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
9278801ccd0SSi-Wei Liu             return -1;
9288801ccd0SSi-Wei Liu         }
9298801ccd0SSi-Wei Liu     }
9307327813dSJason Wang 
9318170ab3fSEugenio Pérez     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
9328170ab3fSEugenio Pérez     if (unlikely(r < 0)) {
933aed5da45SEugenio Pérez         goto err;
9348170ab3fSEugenio Pérez     }
9358170ab3fSEugenio Pérez 
9368170ab3fSEugenio Pérez     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
93740237840SJason Wang                                                  &has_cvq, errp);
93840237840SJason Wang     if (queue_pairs < 0) {
9397327813dSJason Wang         qemu_close(vdpa_device_fd);
94040237840SJason Wang         return queue_pairs;
9417327813dSJason Wang     }
9427327813dSJason Wang 
943bf7a2ad8SLongpeng     r = vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
944bf7a2ad8SLongpeng     if (unlikely(r < 0)) {
945bf7a2ad8SLongpeng         error_setg(errp, "vhost-vdpa: get iova range failed: %s",
946bf7a2ad8SLongpeng                    strerror(-r));
947bf7a2ad8SLongpeng         goto err;
948bf7a2ad8SLongpeng     }
949bf7a2ad8SLongpeng 
95000ef422eSEugenio Pérez     if (opts->x_svq && !vhost_vdpa_net_valid_svq_features(features, errp)) {
95100ef422eSEugenio Pérez         goto err;
9521576dbb5SEugenio Pérez     }
9531576dbb5SEugenio Pérez 
95440237840SJason Wang     ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
95540237840SJason Wang 
95640237840SJason Wang     for (i = 0; i < queue_pairs; i++) {
95740237840SJason Wang         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
9581576dbb5SEugenio Pérez                                      vdpa_device_fd, i, 2, true, opts->x_svq,
95900ef422eSEugenio Pérez                                      iova_range);
96040237840SJason Wang         if (!ncs[i])
96140237840SJason Wang             goto err;
96240237840SJason Wang     }
96340237840SJason Wang 
96440237840SJason Wang     if (has_cvq) {
96540237840SJason Wang         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
9661576dbb5SEugenio Pérez                                  vdpa_device_fd, i, 1, false,
96700ef422eSEugenio Pérez                                  opts->x_svq, iova_range);
96840237840SJason Wang         if (!nc)
96940237840SJason Wang             goto err;
97040237840SJason Wang     }
97140237840SJason Wang 
972654790b6SJason Wang     return 0;
97340237840SJason Wang 
97440237840SJason Wang err:
97540237840SJason Wang     if (i) {
9769bd05507SSi-Wei Liu         for (i--; i >= 0; i--) {
9779bd05507SSi-Wei Liu             qemu_del_net_client(ncs[i]);
9789bd05507SSi-Wei Liu         }
97940237840SJason Wang     }
9801576dbb5SEugenio Pérez 
98140237840SJason Wang     qemu_close(vdpa_device_fd);
98240237840SJason Wang 
98340237840SJason Wang     return -1;
9841e0a84eaSCindy Lu }
985