xref: /openbmc/qemu/net/vhost-vdpa.c (revision 8bc0049e)
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;
46152128d6SEugenio Pérez 
47152128d6SEugenio Pérez     /* The device can isolate CVQ in its own ASID */
48152128d6SEugenio Pérez     bool cvq_isolated;
49152128d6SEugenio Pérez 
501e0a84eaSCindy Lu     bool started;
511e0a84eaSCindy Lu } VhostVDPAState;
521e0a84eaSCindy Lu 
531e0a84eaSCindy Lu const int vdpa_feature_bits[] = {
541e0a84eaSCindy Lu     VIRTIO_F_NOTIFY_ON_EMPTY,
551e0a84eaSCindy Lu     VIRTIO_RING_F_INDIRECT_DESC,
561e0a84eaSCindy Lu     VIRTIO_RING_F_EVENT_IDX,
571e0a84eaSCindy Lu     VIRTIO_F_ANY_LAYOUT,
581e0a84eaSCindy Lu     VIRTIO_F_VERSION_1,
591e0a84eaSCindy Lu     VIRTIO_NET_F_CSUM,
601e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_CSUM,
611e0a84eaSCindy Lu     VIRTIO_NET_F_GSO,
621e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO4,
631e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO6,
641e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_ECN,
651e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_UFO,
661e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO4,
671e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO6,
681e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_ECN,
691e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_UFO,
701e0a84eaSCindy Lu     VIRTIO_NET_F_MRG_RXBUF,
711e0a84eaSCindy Lu     VIRTIO_NET_F_MTU,
7240237840SJason Wang     VIRTIO_NET_F_CTRL_RX,
7340237840SJason Wang     VIRTIO_NET_F_CTRL_RX_EXTRA,
7440237840SJason Wang     VIRTIO_NET_F_CTRL_VLAN,
7540237840SJason Wang     VIRTIO_NET_F_CTRL_MAC_ADDR,
7640237840SJason Wang     VIRTIO_NET_F_RSS,
7740237840SJason Wang     VIRTIO_NET_F_MQ,
7840237840SJason Wang     VIRTIO_NET_F_CTRL_VQ,
791e0a84eaSCindy Lu     VIRTIO_F_IOMMU_PLATFORM,
801e0a84eaSCindy Lu     VIRTIO_F_RING_PACKED,
81562a7d23SStefano Garzarella     VIRTIO_F_RING_RESET,
820145c393SAndrew Melnychenko     VIRTIO_NET_F_RSS,
830145c393SAndrew Melnychenko     VIRTIO_NET_F_HASH_REPORT,
849aa47eddSSi-Wei Liu     VIRTIO_NET_F_STATUS,
851e0a84eaSCindy Lu     VHOST_INVALID_FEATURE_BIT
861e0a84eaSCindy Lu };
871e0a84eaSCindy Lu 
881576dbb5SEugenio Pérez /** Supported device specific feature bits with SVQ */
891576dbb5SEugenio Pérez static const uint64_t vdpa_svq_device_features =
901576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CSUM) |
911576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
921576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MTU) |
931576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MAC) |
941576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
951576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
961576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
971576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
981576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
991576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
1001576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
1011576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
1021576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
1031576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STATUS) |
1041576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
10572b99a87SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MQ) |
1061576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
1071576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
108609ab4c3SEugenio Pérez     /* VHOST_F_LOG_ALL is exposed by SVQ */
109609ab4c3SEugenio Pérez     BIT_ULL(VHOST_F_LOG_ALL) |
1101576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
1110d74e2b7SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STANDBY) |
1120d74e2b7SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_SPEED_DUPLEX);
1131576dbb5SEugenio Pérez 
114c1a10086SEugenio Pérez #define VHOST_VDPA_NET_CVQ_ASID 1
115c1a10086SEugenio Pérez 
1161e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
1171e0a84eaSCindy Lu {
1181e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1191e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1201e0a84eaSCindy Lu     return s->vhost_net;
1211e0a84eaSCindy Lu }
1221e0a84eaSCindy Lu 
12336e46472SEugenio Pérez static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
12436e46472SEugenio Pérez {
12536e46472SEugenio Pérez     uint64_t invalid_dev_features =
12636e46472SEugenio Pérez         features & ~vdpa_svq_device_features &
12736e46472SEugenio Pérez         /* Transport are all accepted at this point */
12836e46472SEugenio Pérez         ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
12936e46472SEugenio Pérez                          VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
13036e46472SEugenio Pérez 
13136e46472SEugenio Pérez     if (invalid_dev_features) {
13236e46472SEugenio Pérez         error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
13336e46472SEugenio Pérez                    invalid_dev_features);
134258a0394SEugenio Pérez         return false;
13536e46472SEugenio Pérez     }
13636e46472SEugenio Pérez 
137258a0394SEugenio Pérez     return vhost_svq_valid_features(features, errp);
13836e46472SEugenio Pérez }
13936e46472SEugenio Pérez 
1401e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
1411e0a84eaSCindy Lu {
1421e0a84eaSCindy Lu     uint32_t device_id;
1431e0a84eaSCindy Lu     int ret;
1441e0a84eaSCindy Lu     struct vhost_dev *hdev;
1451e0a84eaSCindy Lu 
1461e0a84eaSCindy Lu     hdev = (struct vhost_dev *)&net->dev;
1471e0a84eaSCindy Lu     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
1481e0a84eaSCindy Lu     if (device_id != VIRTIO_ID_NET) {
1491e0a84eaSCindy Lu         return -ENOTSUP;
1501e0a84eaSCindy Lu     }
1511e0a84eaSCindy Lu     return ret;
1521e0a84eaSCindy Lu }
1531e0a84eaSCindy Lu 
15440237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be,
15540237840SJason Wang                           int queue_pair_index, int nvqs)
1561e0a84eaSCindy Lu {
1571e0a84eaSCindy Lu     VhostNetOptions options;
1581e0a84eaSCindy Lu     struct vhost_net *net = NULL;
1591e0a84eaSCindy Lu     VhostVDPAState *s;
1601e0a84eaSCindy Lu     int ret;
1611e0a84eaSCindy Lu 
1621e0a84eaSCindy Lu     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
1631e0a84eaSCindy Lu     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1641e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, ncs);
1651e0a84eaSCindy Lu     options.net_backend = ncs;
1661e0a84eaSCindy Lu     options.opaque      = be;
1671e0a84eaSCindy Lu     options.busyloop_timeout = 0;
16840237840SJason Wang     options.nvqs = nvqs;
1691e0a84eaSCindy Lu 
1701e0a84eaSCindy Lu     net = vhost_net_init(&options);
1711e0a84eaSCindy Lu     if (!net) {
1721e0a84eaSCindy Lu         error_report("failed to init vhost_net for queue");
173a97ef87aSJason Wang         goto err_init;
1741e0a84eaSCindy Lu     }
1751e0a84eaSCindy Lu     s->vhost_net = net;
1761e0a84eaSCindy Lu     ret = vhost_vdpa_net_check_device_id(net);
1771e0a84eaSCindy Lu     if (ret) {
178a97ef87aSJason Wang         goto err_check;
1791e0a84eaSCindy Lu     }
1801e0a84eaSCindy Lu     return 0;
181a97ef87aSJason Wang err_check:
1821e0a84eaSCindy Lu     vhost_net_cleanup(net);
183ab36edcfSJason Wang     g_free(net);
184a97ef87aSJason Wang err_init:
1851e0a84eaSCindy Lu     return -1;
1861e0a84eaSCindy Lu }
1871e0a84eaSCindy Lu 
1881e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc)
1891e0a84eaSCindy Lu {
1901e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1911e0a84eaSCindy Lu 
1922df4dd31SEugenio Pérez     qemu_vfree(s->cvq_cmd_out_buffer);
19317fb889fSEugenio Pérez     qemu_vfree(s->status);
1941e0a84eaSCindy Lu     if (s->vhost_net) {
1951e0a84eaSCindy Lu         vhost_net_cleanup(s->vhost_net);
1961e0a84eaSCindy Lu         g_free(s->vhost_net);
1971e0a84eaSCindy Lu         s->vhost_net = NULL;
1981e0a84eaSCindy Lu     }
19957b3a7d8SCindy Lu      if (s->vhost_vdpa.device_fd >= 0) {
20057b3a7d8SCindy Lu         qemu_close(s->vhost_vdpa.device_fd);
20157b3a7d8SCindy Lu         s->vhost_vdpa.device_fd = -1;
20257b3a7d8SCindy Lu     }
2031e0a84eaSCindy Lu }
2041e0a84eaSCindy Lu 
2051e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
2061e0a84eaSCindy Lu {
2071e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2081e0a84eaSCindy Lu 
2091e0a84eaSCindy Lu     return true;
2101e0a84eaSCindy Lu }
2111e0a84eaSCindy Lu 
2121e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc)
2131e0a84eaSCindy Lu {
2141e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2151e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
2161e0a84eaSCindy Lu     uint64_t features = 0;
2171e0a84eaSCindy Lu     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
2181e0a84eaSCindy Lu     features = vhost_net_get_features(s->vhost_net, features);
2191e0a84eaSCindy Lu     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
2201e0a84eaSCindy Lu 
2211e0a84eaSCindy Lu }
2221e0a84eaSCindy Lu 
223ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
224ee8a1c63SKevin Wolf                                        Error **errp)
225ee8a1c63SKevin Wolf {
226ee8a1c63SKevin Wolf     const char *driver = object_class_get_name(oc);
227ee8a1c63SKevin Wolf 
228ee8a1c63SKevin Wolf     if (!g_str_has_prefix(driver, "virtio-net-")) {
229ee8a1c63SKevin Wolf         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
230ee8a1c63SKevin Wolf         return false;
231ee8a1c63SKevin Wolf     }
232ee8a1c63SKevin Wolf 
233ee8a1c63SKevin Wolf     return true;
234ee8a1c63SKevin Wolf }
235ee8a1c63SKevin Wolf 
236846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */
237846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
238846a1e85SEugenio Pérez                                   size_t size)
239846a1e85SEugenio Pérez {
240bc5add1dSSi-Wei Liu     return size;
241846a1e85SEugenio Pérez }
242846a1e85SEugenio Pérez 
24300ef422eSEugenio Pérez /** From any vdpa net client, get the netclient of the first queue pair */
24400ef422eSEugenio Pérez static VhostVDPAState *vhost_vdpa_net_first_nc_vdpa(VhostVDPAState *s)
24500ef422eSEugenio Pérez {
24600ef422eSEugenio Pérez     NICState *nic = qemu_get_nic(s->nc.peer);
24700ef422eSEugenio Pérez     NetClientState *nc0 = qemu_get_peer(nic->ncs, 0);
24800ef422eSEugenio Pérez 
24900ef422eSEugenio Pérez     return DO_UPCAST(VhostVDPAState, nc, nc0);
25000ef422eSEugenio Pérez }
25100ef422eSEugenio Pérez 
25269498430SEugenio Pérez static void vhost_vdpa_net_log_global_enable(VhostVDPAState *s, bool enable)
25369498430SEugenio Pérez {
25469498430SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
25569498430SEugenio Pérez     VirtIONet *n;
25669498430SEugenio Pérez     VirtIODevice *vdev;
25769498430SEugenio Pérez     int data_queue_pairs, cvq, r;
25869498430SEugenio Pérez 
25969498430SEugenio Pérez     /* We are only called on the first data vqs and only if x-svq is not set */
26069498430SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled == enable) {
26169498430SEugenio Pérez         return;
26269498430SEugenio Pérez     }
26369498430SEugenio Pérez 
26469498430SEugenio Pérez     vdev = v->dev->vdev;
26569498430SEugenio Pérez     n = VIRTIO_NET(vdev);
26669498430SEugenio Pérez     if (!n->vhost_started) {
26769498430SEugenio Pérez         return;
26869498430SEugenio Pérez     }
26969498430SEugenio Pérez 
27069498430SEugenio Pérez     data_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
27169498430SEugenio Pérez     cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
27269498430SEugenio Pérez                                   n->max_ncs - n->max_queue_pairs : 0;
27369498430SEugenio Pérez     /*
27469498430SEugenio Pérez      * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
27569498430SEugenio Pérez      * in the future and resume the device if read-only operations between
27669498430SEugenio Pérez      * suspend and reset goes wrong.
27769498430SEugenio Pérez      */
27869498430SEugenio Pérez     vhost_net_stop(vdev, n->nic->ncs, data_queue_pairs, cvq);
27969498430SEugenio Pérez 
28069498430SEugenio Pérez     /* Start will check migration setup_or_active to configure or not SVQ */
28169498430SEugenio Pérez     r = vhost_net_start(vdev, n->nic->ncs, data_queue_pairs, cvq);
28269498430SEugenio Pérez     if (unlikely(r < 0)) {
28369498430SEugenio Pérez         error_report("unable to start vhost net: %s(%d)", g_strerror(-r), -r);
28469498430SEugenio Pérez     }
28569498430SEugenio Pérez }
28669498430SEugenio Pérez 
28769498430SEugenio Pérez static void vdpa_net_migration_state_notifier(Notifier *notifier, void *data)
28869498430SEugenio Pérez {
28969498430SEugenio Pérez     MigrationState *migration = data;
29069498430SEugenio Pérez     VhostVDPAState *s = container_of(notifier, VhostVDPAState,
29169498430SEugenio Pérez                                      migration_state);
29269498430SEugenio Pérez 
29369498430SEugenio Pérez     if (migration_in_setup(migration)) {
29469498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, true);
29569498430SEugenio Pérez     } else if (migration_has_failed(migration)) {
29669498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, false);
29769498430SEugenio Pérez     }
29869498430SEugenio Pérez }
29969498430SEugenio Pérez 
30000ef422eSEugenio Pérez static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
30100ef422eSEugenio Pérez {
30200ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
30300ef422eSEugenio Pérez 
30469498430SEugenio Pérez     add_migration_state_change_notifier(&s->migration_state);
30500ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
30600ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
30700ef422eSEugenio Pérez                                            v->iova_range.last);
30800ef422eSEugenio Pérez     }
30900ef422eSEugenio Pérez }
31000ef422eSEugenio Pérez 
31100ef422eSEugenio Pérez static int vhost_vdpa_net_data_start(NetClientState *nc)
31200ef422eSEugenio Pérez {
31300ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
31400ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
31500ef422eSEugenio Pérez 
31600ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
31700ef422eSEugenio Pérez 
31869498430SEugenio Pérez     if (s->always_svq ||
31969498430SEugenio Pérez         migration_is_setup_or_active(migrate_get_current()->state)) {
32069498430SEugenio Pérez         v->shadow_vqs_enabled = true;
32169498430SEugenio Pérez         v->shadow_data = true;
32269498430SEugenio Pérez     } else {
32369498430SEugenio Pérez         v->shadow_vqs_enabled = false;
32469498430SEugenio Pérez         v->shadow_data = false;
32569498430SEugenio Pérez     }
32669498430SEugenio Pérez 
32700ef422eSEugenio Pérez     if (v->index == 0) {
32800ef422eSEugenio Pérez         vhost_vdpa_net_data_start_first(s);
32900ef422eSEugenio Pérez         return 0;
33000ef422eSEugenio Pérez     }
33100ef422eSEugenio Pérez 
33200ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
33300ef422eSEugenio Pérez         VhostVDPAState *s0 = vhost_vdpa_net_first_nc_vdpa(s);
33400ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
33500ef422eSEugenio Pérez     }
33600ef422eSEugenio Pérez 
33700ef422eSEugenio Pérez     return 0;
33800ef422eSEugenio Pérez }
33900ef422eSEugenio Pérez 
34000ef422eSEugenio Pérez static void vhost_vdpa_net_client_stop(NetClientState *nc)
34100ef422eSEugenio Pérez {
34200ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
34300ef422eSEugenio Pérez     struct vhost_dev *dev;
34400ef422eSEugenio Pérez 
34500ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
34600ef422eSEugenio Pérez 
34769498430SEugenio Pérez     if (s->vhost_vdpa.index == 0) {
34869498430SEugenio Pérez         remove_migration_state_change_notifier(&s->migration_state);
34969498430SEugenio Pérez     }
35069498430SEugenio Pérez 
35100ef422eSEugenio Pérez     dev = s->vhost_vdpa.dev;
35200ef422eSEugenio Pérez     if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
35300ef422eSEugenio Pérez         g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
35400ef422eSEugenio Pérez     }
35500ef422eSEugenio Pérez }
35600ef422eSEugenio Pérez 
3571e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = {
3581e0a84eaSCindy Lu         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
3591e0a84eaSCindy Lu         .size = sizeof(VhostVDPAState),
360846a1e85SEugenio Pérez         .receive = vhost_vdpa_receive,
36100ef422eSEugenio Pérez         .start = vhost_vdpa_net_data_start,
36200ef422eSEugenio Pérez         .stop = vhost_vdpa_net_client_stop,
3631e0a84eaSCindy Lu         .cleanup = vhost_vdpa_cleanup,
3641e0a84eaSCindy Lu         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
3651e0a84eaSCindy Lu         .has_ufo = vhost_vdpa_has_ufo,
366ee8a1c63SKevin Wolf         .check_peer_type = vhost_vdpa_check_peer_type,
3671e0a84eaSCindy Lu };
3681e0a84eaSCindy Lu 
369152128d6SEugenio Pérez static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index,
370152128d6SEugenio Pérez                                           Error **errp)
371c1a10086SEugenio Pérez {
372c1a10086SEugenio Pérez     struct vhost_vring_state state = {
373c1a10086SEugenio Pérez         .index = vq_index,
374c1a10086SEugenio Pérez     };
375c1a10086SEugenio Pérez     int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
376c1a10086SEugenio Pérez 
377c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
3780f2bb0bfSEugenio Pérez         r = -errno;
379152128d6SEugenio Pérez         error_setg_errno(errp, errno, "Cannot get VQ %u group", vq_index);
380c1a10086SEugenio Pérez         return r;
381c1a10086SEugenio Pérez     }
382c1a10086SEugenio Pérez 
383c1a10086SEugenio Pérez     return state.num;
384c1a10086SEugenio Pérez }
385c1a10086SEugenio Pérez 
386c1a10086SEugenio Pérez static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
387c1a10086SEugenio Pérez                                            unsigned vq_group,
388c1a10086SEugenio Pérez                                            unsigned asid_num)
389c1a10086SEugenio Pérez {
390c1a10086SEugenio Pérez     struct vhost_vring_state asid = {
391c1a10086SEugenio Pérez         .index = vq_group,
392c1a10086SEugenio Pérez         .num = asid_num,
393c1a10086SEugenio Pérez     };
394c1a10086SEugenio Pérez     int r;
395c1a10086SEugenio Pérez 
396c1a10086SEugenio Pérez     r = ioctl(v->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
397c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
398c1a10086SEugenio Pérez         error_report("Can't set vq group %u asid %u, errno=%d (%s)",
399c1a10086SEugenio Pérez                      asid.index, asid.num, errno, g_strerror(errno));
400c1a10086SEugenio Pérez     }
401c1a10086SEugenio Pérez     return r;
402c1a10086SEugenio Pérez }
403c1a10086SEugenio Pérez 
4042df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
4052df4dd31SEugenio Pérez {
4062df4dd31SEugenio Pérez     VhostIOVATree *tree = v->iova_tree;
4072df4dd31SEugenio Pérez     DMAMap needle = {
4082df4dd31SEugenio Pérez         /*
4092df4dd31SEugenio Pérez          * No need to specify size or to look for more translations since
4102df4dd31SEugenio Pérez          * this contiguous chunk was allocated by us.
4112df4dd31SEugenio Pérez          */
4122df4dd31SEugenio Pérez         .translated_addr = (hwaddr)(uintptr_t)addr,
4132df4dd31SEugenio Pérez     };
4142df4dd31SEugenio Pérez     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
4152df4dd31SEugenio Pérez     int r;
4162df4dd31SEugenio Pérez 
4172df4dd31SEugenio Pérez     if (unlikely(!map)) {
4182df4dd31SEugenio Pérez         error_report("Cannot locate expected map");
4192df4dd31SEugenio Pérez         return;
4202df4dd31SEugenio Pérez     }
4212df4dd31SEugenio Pérez 
422cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_unmap(v, v->address_space_id, map->iova, map->size + 1);
4232df4dd31SEugenio Pérez     if (unlikely(r != 0)) {
4242df4dd31SEugenio Pérez         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
4252df4dd31SEugenio Pérez     }
4262df4dd31SEugenio Pérez 
42769292a8eSEugenio Pérez     vhost_iova_tree_remove(tree, *map);
4282df4dd31SEugenio Pérez }
4292df4dd31SEugenio Pérez 
4302df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void)
4312df4dd31SEugenio Pérez {
4322df4dd31SEugenio Pérez     /*
4332df4dd31SEugenio Pérez      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
4342df4dd31SEugenio Pérez      * In buffer is always 1 byte, so it should fit here
4352df4dd31SEugenio Pérez      */
4362df4dd31SEugenio Pérez     return sizeof(struct virtio_net_ctrl_hdr) +
4372df4dd31SEugenio Pérez            2 * sizeof(struct virtio_net_ctrl_mac) +
4382df4dd31SEugenio Pérez            MAC_TABLE_ENTRIES * ETH_ALEN;
4392df4dd31SEugenio Pérez }
4402df4dd31SEugenio Pérez 
4412df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
4422df4dd31SEugenio Pérez {
4432df4dd31SEugenio Pérez     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
4442df4dd31SEugenio Pérez }
4452df4dd31SEugenio Pérez 
4467a7f87e9SEugenio Pérez /** Map CVQ buffer. */
4477a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
4487a7f87e9SEugenio Pérez                                   bool write)
4492df4dd31SEugenio Pérez {
4502df4dd31SEugenio Pérez     DMAMap map = {};
4512df4dd31SEugenio Pérez     int r;
4522df4dd31SEugenio Pérez 
4532df4dd31SEugenio Pérez     map.translated_addr = (hwaddr)(uintptr_t)buf;
4547a7f87e9SEugenio Pérez     map.size = size - 1;
4552df4dd31SEugenio Pérez     map.perm = write ? IOMMU_RW : IOMMU_RO,
4562df4dd31SEugenio Pérez     r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
4572df4dd31SEugenio Pérez     if (unlikely(r != IOVA_OK)) {
4582df4dd31SEugenio Pérez         error_report("Cannot map injected element");
4597a7f87e9SEugenio Pérez         return r;
4602df4dd31SEugenio Pérez     }
4612df4dd31SEugenio Pérez 
462cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_map(v, v->address_space_id, map.iova,
463cd831ed5SEugenio Pérez                            vhost_vdpa_net_cvq_cmd_page_len(), buf, !write);
4642df4dd31SEugenio Pérez     if (unlikely(r < 0)) {
4652df4dd31SEugenio Pérez         goto dma_map_err;
4662df4dd31SEugenio Pérez     }
4672df4dd31SEugenio Pérez 
4687a7f87e9SEugenio Pérez     return 0;
4692df4dd31SEugenio Pérez 
4702df4dd31SEugenio Pérez dma_map_err:
47169292a8eSEugenio Pérez     vhost_iova_tree_remove(v->iova_tree, map);
4727a7f87e9SEugenio Pérez     return r;
4732df4dd31SEugenio Pérez }
4742df4dd31SEugenio Pérez 
4757a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc)
4762df4dd31SEugenio Pérez {
47700ef422eSEugenio Pérez     VhostVDPAState *s, *s0;
478c1a10086SEugenio Pérez     struct vhost_vdpa *v;
479c1a10086SEugenio Pérez     int64_t cvq_group;
480152128d6SEugenio Pérez     int r;
481152128d6SEugenio Pérez     Error *err = NULL;
4822df4dd31SEugenio Pérez 
4837a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
4847a7f87e9SEugenio Pérez 
4857a7f87e9SEugenio Pérez     s = DO_UPCAST(VhostVDPAState, nc, nc);
486c1a10086SEugenio Pérez     v = &s->vhost_vdpa;
487c1a10086SEugenio Pérez 
48869498430SEugenio Pérez     s0 = vhost_vdpa_net_first_nc_vdpa(s);
48969498430SEugenio Pérez     v->shadow_data = s0->vhost_vdpa.shadow_vqs_enabled;
490c1a10086SEugenio Pérez     v->shadow_vqs_enabled = s->always_svq;
491c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
492c1a10086SEugenio Pérez 
49369498430SEugenio Pérez     if (s->vhost_vdpa.shadow_data) {
494c1a10086SEugenio Pérez         /* SVQ is already configured for all virtqueues */
495c1a10086SEugenio Pérez         goto out;
496c1a10086SEugenio Pérez     }
497c1a10086SEugenio Pérez 
498c1a10086SEugenio Pérez     /*
499c1a10086SEugenio Pérez      * If we early return in these cases SVQ will not be enabled. The migration
500c1a10086SEugenio Pérez      * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
501c1a10086SEugenio Pérez      */
502152128d6SEugenio Pérez     if (!vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
503c1a10086SEugenio Pérez         return 0;
504c1a10086SEugenio Pérez     }
505c1a10086SEugenio Pérez 
506152128d6SEugenio Pérez     if (!s->cvq_isolated) {
507152128d6SEugenio Pérez         return 0;
508152128d6SEugenio Pérez     }
509152128d6SEugenio Pérez 
510152128d6SEugenio Pérez     cvq_group = vhost_vdpa_get_vring_group(v->device_fd,
511152128d6SEugenio Pérez                                            v->dev->vq_index_end - 1,
512152128d6SEugenio Pérez                                            &err);
513c1a10086SEugenio Pérez     if (unlikely(cvq_group < 0)) {
514152128d6SEugenio Pérez         error_report_err(err);
515c1a10086SEugenio Pérez         return cvq_group;
516c1a10086SEugenio Pérez     }
517c1a10086SEugenio Pérez 
518c1a10086SEugenio Pérez     r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
519c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
520c1a10086SEugenio Pérez         return r;
521c1a10086SEugenio Pérez     }
522c1a10086SEugenio Pérez 
523c1a10086SEugenio Pérez     v->shadow_vqs_enabled = true;
524c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
525c1a10086SEugenio Pérez 
526c1a10086SEugenio Pérez out:
5277a7f87e9SEugenio Pérez     if (!s->vhost_vdpa.shadow_vqs_enabled) {
5287a7f87e9SEugenio Pérez         return 0;
5292df4dd31SEugenio Pérez     }
5302df4dd31SEugenio Pérez 
53100ef422eSEugenio Pérez     if (s0->vhost_vdpa.iova_tree) {
53200ef422eSEugenio Pérez         /*
53300ef422eSEugenio Pérez          * SVQ is already configured for all virtqueues.  Reuse IOVA tree for
53400ef422eSEugenio Pérez          * simplicity, whether CVQ shares ASID with guest or not, because:
53500ef422eSEugenio Pérez          * - Memory listener need access to guest's memory addresses allocated
53600ef422eSEugenio Pérez          *   in the IOVA tree.
53700ef422eSEugenio Pérez          * - There should be plenty of IOVA address space for both ASID not to
53800ef422eSEugenio Pérez          *   worry about collisions between them.  Guest's translations are
53900ef422eSEugenio Pérez          *   still validated with virtio virtqueue_pop so there is no risk for
54000ef422eSEugenio Pérez          *   the guest to access memory that it shouldn't.
54100ef422eSEugenio Pérez          *
54200ef422eSEugenio Pérez          * To allocate a iova tree per ASID is doable but it complicates the
54300ef422eSEugenio Pérez          * code and it is not worth it for the moment.
54400ef422eSEugenio Pérez          */
54500ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
54600ef422eSEugenio Pérez     } else {
54700ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
54800ef422eSEugenio Pérez                                            v->iova_range.last);
54900ef422eSEugenio Pérez     }
55000ef422eSEugenio Pérez 
5517a7f87e9SEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
5527a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), false);
5537a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5547a7f87e9SEugenio Pérez         return r;
5557a7f87e9SEugenio Pérez     }
5567a7f87e9SEugenio Pérez 
55717fb889fSEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
5587a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), true);
5597a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5602df4dd31SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
5612df4dd31SEugenio Pérez     }
5622df4dd31SEugenio Pérez 
5637a7f87e9SEugenio Pérez     return r;
5647a7f87e9SEugenio Pérez }
5657a7f87e9SEugenio Pérez 
5667a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
5677a7f87e9SEugenio Pérez {
5687a7f87e9SEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
5697a7f87e9SEugenio Pérez 
5707a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
5717a7f87e9SEugenio Pérez 
5727a7f87e9SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled) {
5737a7f87e9SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
57417fb889fSEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
575c1a10086SEugenio Pérez     }
57600ef422eSEugenio Pérez 
57700ef422eSEugenio Pérez     vhost_vdpa_net_client_stop(nc);
5782df4dd31SEugenio Pérez }
5792df4dd31SEugenio Pérez 
580be4278b6SEugenio Pérez static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len,
581be4278b6SEugenio Pérez                                       size_t in_len)
582be4278b6SEugenio Pérez {
583be4278b6SEugenio Pérez     /* Buffers for the device */
584be4278b6SEugenio Pérez     const struct iovec out = {
585be4278b6SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
586be4278b6SEugenio Pérez         .iov_len = out_len,
587be4278b6SEugenio Pérez     };
588be4278b6SEugenio Pérez     const struct iovec in = {
58917fb889fSEugenio Pérez         .iov_base = s->status,
590be4278b6SEugenio Pérez         .iov_len = sizeof(virtio_net_ctrl_ack),
591be4278b6SEugenio Pérez     };
592be4278b6SEugenio Pérez     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
593be4278b6SEugenio Pérez     int r;
594be4278b6SEugenio Pérez 
595be4278b6SEugenio Pérez     r = vhost_svq_add(svq, &out, 1, &in, 1, NULL);
596be4278b6SEugenio Pérez     if (unlikely(r != 0)) {
597be4278b6SEugenio Pérez         if (unlikely(r == -ENOSPC)) {
598be4278b6SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
599be4278b6SEugenio Pérez                           __func__);
600be4278b6SEugenio Pérez         }
601be4278b6SEugenio Pérez         return r;
602be4278b6SEugenio Pérez     }
603be4278b6SEugenio Pérez 
604be4278b6SEugenio Pérez     /*
605be4278b6SEugenio Pérez      * We can poll here since we've had BQL from the time we sent the
606be4278b6SEugenio Pérez      * descriptor. Also, we need to take the answer before SVQ pulls by itself,
607be4278b6SEugenio Pérez      * when BQL is released
608be4278b6SEugenio Pérez      */
609be4278b6SEugenio Pérez     return vhost_svq_poll(svq);
610be4278b6SEugenio Pérez }
611be4278b6SEugenio Pérez 
612f73c0c43SEugenio Pérez static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class,
613f73c0c43SEugenio Pérez                                        uint8_t cmd, const void *data,
614f73c0c43SEugenio Pérez                                        size_t data_size)
615f73c0c43SEugenio Pérez {
616f73c0c43SEugenio Pérez     const struct virtio_net_ctrl_hdr ctrl = {
617f73c0c43SEugenio Pérez         .class = class,
618f73c0c43SEugenio Pérez         .cmd = cmd,
619f73c0c43SEugenio Pérez     };
620f73c0c43SEugenio Pérez 
621f73c0c43SEugenio Pérez     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
622f73c0c43SEugenio Pérez 
623f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl));
624f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size);
625f73c0c43SEugenio Pérez 
626f73c0c43SEugenio Pérez     return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size,
627f73c0c43SEugenio Pérez                                   sizeof(virtio_net_ctrl_ack));
628f73c0c43SEugenio Pérez }
629f73c0c43SEugenio Pérez 
630f73c0c43SEugenio Pérez static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n)
631f73c0c43SEugenio Pérez {
632f73c0c43SEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
633f73c0c43SEugenio Pérez     if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) {
634f73c0c43SEugenio Pérez         ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC,
635f73c0c43SEugenio Pérez                                                   VIRTIO_NET_CTRL_MAC_ADDR_SET,
636f73c0c43SEugenio Pérez                                                   n->mac, sizeof(n->mac));
637f73c0c43SEugenio Pérez         if (unlikely(dev_written < 0)) {
638f73c0c43SEugenio Pérez             return dev_written;
639f73c0c43SEugenio Pérez         }
640f73c0c43SEugenio Pérez 
641f73c0c43SEugenio Pérez         return *s->status != VIRTIO_NET_OK;
642f73c0c43SEugenio Pérez     }
643f73c0c43SEugenio Pérez 
644f73c0c43SEugenio Pérez     return 0;
645f73c0c43SEugenio Pérez }
646f73c0c43SEugenio Pérez 
647f64c7cdaSEugenio Pérez static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
648f64c7cdaSEugenio Pérez                                   const VirtIONet *n)
649f64c7cdaSEugenio Pérez {
650f64c7cdaSEugenio Pérez     struct virtio_net_ctrl_mq mq;
651f64c7cdaSEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
652f64c7cdaSEugenio Pérez     ssize_t dev_written;
653f64c7cdaSEugenio Pérez 
654f64c7cdaSEugenio Pérez     if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) {
655f64c7cdaSEugenio Pérez         return 0;
656f64c7cdaSEugenio Pérez     }
657f64c7cdaSEugenio Pérez 
658f64c7cdaSEugenio Pérez     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
659f64c7cdaSEugenio Pérez     dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ,
660f64c7cdaSEugenio Pérez                                           VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq,
661f64c7cdaSEugenio Pérez                                           sizeof(mq));
662f64c7cdaSEugenio Pérez     if (unlikely(dev_written < 0)) {
663f64c7cdaSEugenio Pérez         return dev_written;
664f64c7cdaSEugenio Pérez     }
665f64c7cdaSEugenio Pérez 
666f64c7cdaSEugenio Pérez     return *s->status != VIRTIO_NET_OK;
667f64c7cdaSEugenio Pérez }
668f64c7cdaSEugenio Pérez 
669dd036d8dSEugenio Pérez static int vhost_vdpa_net_load(NetClientState *nc)
670dd036d8dSEugenio Pérez {
671dd036d8dSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
672f73c0c43SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
673dd036d8dSEugenio Pérez     const VirtIONet *n;
674f73c0c43SEugenio Pérez     int r;
675dd036d8dSEugenio Pérez 
676dd036d8dSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
677dd036d8dSEugenio Pérez 
678dd036d8dSEugenio Pérez     if (!v->shadow_vqs_enabled) {
679dd036d8dSEugenio Pérez         return 0;
680dd036d8dSEugenio Pérez     }
681dd036d8dSEugenio Pérez 
682dd036d8dSEugenio Pérez     n = VIRTIO_NET(v->dev->vdev);
683f73c0c43SEugenio Pérez     r = vhost_vdpa_net_load_mac(s, n);
684f73c0c43SEugenio Pérez     if (unlikely(r < 0)) {
685f73c0c43SEugenio Pérez         return r;
686dd036d8dSEugenio Pérez     }
687f64c7cdaSEugenio Pérez     r = vhost_vdpa_net_load_mq(s, n);
688f64c7cdaSEugenio Pérez     if (unlikely(r)) {
689f64c7cdaSEugenio Pérez         return r;
690f64c7cdaSEugenio Pérez     }
691dd036d8dSEugenio Pérez 
692dd036d8dSEugenio Pérez     return 0;
693dd036d8dSEugenio Pérez }
694dd036d8dSEugenio Pérez 
695f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = {
696f8972b56SEugenio Pérez     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
697f8972b56SEugenio Pérez     .size = sizeof(VhostVDPAState),
698f8972b56SEugenio Pérez     .receive = vhost_vdpa_receive,
6997a7f87e9SEugenio Pérez     .start = vhost_vdpa_net_cvq_start,
700dd036d8dSEugenio Pérez     .load = vhost_vdpa_net_load,
7017a7f87e9SEugenio Pérez     .stop = vhost_vdpa_net_cvq_stop,
702f8972b56SEugenio Pérez     .cleanup = vhost_vdpa_cleanup,
703f8972b56SEugenio Pérez     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
704f8972b56SEugenio Pérez     .has_ufo = vhost_vdpa_has_ufo,
705f8972b56SEugenio Pérez     .check_peer_type = vhost_vdpa_check_peer_type,
706f8972b56SEugenio Pérez };
707f8972b56SEugenio Pérez 
7082df4dd31SEugenio Pérez /**
7092df4dd31SEugenio Pérez  * Validate and copy control virtqueue commands.
7102df4dd31SEugenio Pérez  *
7112df4dd31SEugenio Pérez  * Following QEMU guidelines, we offer a copy of the buffers to the device to
7122df4dd31SEugenio Pérez  * prevent TOCTOU bugs.
713bd907ae4SEugenio Pérez  */
714bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
715bd907ae4SEugenio Pérez                                             VirtQueueElement *elem,
716bd907ae4SEugenio Pérez                                             void *opaque)
717bd907ae4SEugenio Pérez {
7182df4dd31SEugenio Pérez     VhostVDPAState *s = opaque;
719be4278b6SEugenio Pérez     size_t in_len;
720bd907ae4SEugenio Pérez     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
7217a7f87e9SEugenio Pérez     /* Out buffer sent to both the vdpa device and the device model */
7227a7f87e9SEugenio Pérez     struct iovec out = {
7237a7f87e9SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
7247a7f87e9SEugenio Pérez     };
7252df4dd31SEugenio Pérez     /* in buffer used for device model */
7262df4dd31SEugenio Pérez     const struct iovec in = {
7272df4dd31SEugenio Pérez         .iov_base = &status,
7282df4dd31SEugenio Pérez         .iov_len = sizeof(status),
7292df4dd31SEugenio Pérez     };
730be4278b6SEugenio Pérez     ssize_t dev_written = -EINVAL;
731bd907ae4SEugenio Pérez 
7327a7f87e9SEugenio Pérez     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
7337a7f87e9SEugenio Pérez                              s->cvq_cmd_out_buffer,
7347a7f87e9SEugenio Pérez                              vhost_vdpa_net_cvq_cmd_len());
7353f9a3eebSEugenio Pérez     if (*(uint8_t *)s->cvq_cmd_out_buffer == VIRTIO_NET_CTRL_ANNOUNCE) {
7363f9a3eebSEugenio Pérez         /*
7373f9a3eebSEugenio Pérez          * Guest announce capability is emulated by qemu, so don't forward to
7383f9a3eebSEugenio Pérez          * the device.
7393f9a3eebSEugenio Pérez          */
7403f9a3eebSEugenio Pérez         dev_written = sizeof(status);
7413f9a3eebSEugenio Pérez         *s->status = VIRTIO_NET_OK;
7423f9a3eebSEugenio Pérez     } else {
743be4278b6SEugenio Pérez         dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
744be4278b6SEugenio Pérez         if (unlikely(dev_written < 0)) {
745bd907ae4SEugenio Pérez             goto out;
746bd907ae4SEugenio Pérez         }
7473f9a3eebSEugenio Pérez     }
748bd907ae4SEugenio Pérez 
749bd907ae4SEugenio Pérez     if (unlikely(dev_written < sizeof(status))) {
750bd907ae4SEugenio Pérez         error_report("Insufficient written data (%zu)", dev_written);
7512df4dd31SEugenio Pérez         goto out;
7522df4dd31SEugenio Pérez     }
7532df4dd31SEugenio Pérez 
75417fb889fSEugenio Pérez     if (*s->status != VIRTIO_NET_OK) {
755be4278b6SEugenio Pérez         return VIRTIO_NET_ERR;
7562df4dd31SEugenio Pérez     }
7572df4dd31SEugenio Pérez 
7582df4dd31SEugenio Pérez     status = VIRTIO_NET_ERR;
7597a7f87e9SEugenio Pérez     virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
7602df4dd31SEugenio Pérez     if (status != VIRTIO_NET_OK) {
7612df4dd31SEugenio Pérez         error_report("Bad CVQ processing in model");
762bd907ae4SEugenio Pérez     }
763bd907ae4SEugenio Pérez 
764bd907ae4SEugenio Pérez out:
765bd907ae4SEugenio Pérez     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
766bd907ae4SEugenio Pérez                           sizeof(status));
767bd907ae4SEugenio Pérez     if (unlikely(in_len < sizeof(status))) {
768bd907ae4SEugenio Pérez         error_report("Bad device CVQ written length");
769bd907ae4SEugenio Pérez     }
770bd907ae4SEugenio Pérez     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
771bd907ae4SEugenio Pérez     g_free(elem);
772be4278b6SEugenio Pérez     return dev_written < 0 ? dev_written : 0;
773bd907ae4SEugenio Pérez }
774bd907ae4SEugenio Pérez 
775bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
776bd907ae4SEugenio Pérez     .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
777bd907ae4SEugenio Pérez };
778bd907ae4SEugenio Pérez 
779152128d6SEugenio Pérez /**
780152128d6SEugenio Pérez  * Probe if CVQ is isolated
781152128d6SEugenio Pérez  *
782152128d6SEugenio Pérez  * @device_fd         The vdpa device fd
783152128d6SEugenio Pérez  * @features          Features offered by the device.
784152128d6SEugenio Pérez  * @cvq_index         The control vq pair index
785152128d6SEugenio Pérez  *
786152128d6SEugenio Pérez  * Returns <0 in case of failure, 0 if false and 1 if true.
787152128d6SEugenio Pérez  */
788152128d6SEugenio Pérez static int vhost_vdpa_probe_cvq_isolation(int device_fd, uint64_t features,
789152128d6SEugenio Pérez                                           int cvq_index, Error **errp)
790152128d6SEugenio Pérez {
791152128d6SEugenio Pérez     uint64_t backend_features;
792152128d6SEugenio Pérez     int64_t cvq_group;
793152128d6SEugenio Pérez     uint8_t status = VIRTIO_CONFIG_S_ACKNOWLEDGE |
794152128d6SEugenio Pérez                      VIRTIO_CONFIG_S_DRIVER |
795152128d6SEugenio Pérez                      VIRTIO_CONFIG_S_FEATURES_OK;
796152128d6SEugenio Pérez     int r;
797152128d6SEugenio Pérez 
798152128d6SEugenio Pérez     ERRP_GUARD();
799152128d6SEugenio Pérez 
800152128d6SEugenio Pérez     r = ioctl(device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
801152128d6SEugenio Pérez     if (unlikely(r < 0)) {
802152128d6SEugenio Pérez         error_setg_errno(errp, errno, "Cannot get vdpa backend_features");
803152128d6SEugenio Pérez         return r;
804152128d6SEugenio Pérez     }
805152128d6SEugenio Pérez 
806152128d6SEugenio Pérez     if (!(backend_features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID))) {
807152128d6SEugenio Pérez         return 0;
808152128d6SEugenio Pérez     }
809152128d6SEugenio Pérez 
810152128d6SEugenio Pérez     r = ioctl(device_fd, VHOST_SET_FEATURES, &features);
811152128d6SEugenio Pérez     if (unlikely(r)) {
812152128d6SEugenio Pérez         error_setg_errno(errp, errno, "Cannot set features");
813152128d6SEugenio Pérez     }
814152128d6SEugenio Pérez 
815152128d6SEugenio Pérez     r = ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
816152128d6SEugenio Pérez     if (unlikely(r)) {
817152128d6SEugenio Pérez         error_setg_errno(errp, -r, "Cannot set device features");
818152128d6SEugenio Pérez         goto out;
819152128d6SEugenio Pérez     }
820152128d6SEugenio Pérez 
821152128d6SEugenio Pérez     cvq_group = vhost_vdpa_get_vring_group(device_fd, cvq_index, errp);
822152128d6SEugenio Pérez     if (unlikely(cvq_group < 0)) {
823152128d6SEugenio Pérez         if (cvq_group != -ENOTSUP) {
824152128d6SEugenio Pérez             r = cvq_group;
825152128d6SEugenio Pérez             goto out;
826152128d6SEugenio Pérez         }
827152128d6SEugenio Pérez 
828152128d6SEugenio Pérez         /*
829152128d6SEugenio Pérez          * The kernel report VHOST_BACKEND_F_IOTLB_ASID if the vdpa frontend
830152128d6SEugenio Pérez          * support ASID even if the parent driver does not.  The CVQ cannot be
831152128d6SEugenio Pérez          * isolated in this case.
832152128d6SEugenio Pérez          */
833152128d6SEugenio Pérez         error_free(*errp);
834152128d6SEugenio Pérez         *errp = NULL;
835152128d6SEugenio Pérez         r = 0;
836152128d6SEugenio Pérez         goto out;
837152128d6SEugenio Pérez     }
838152128d6SEugenio Pérez 
839152128d6SEugenio Pérez     for (int i = 0; i < cvq_index; ++i) {
840152128d6SEugenio Pérez         int64_t group = vhost_vdpa_get_vring_group(device_fd, i, errp);
841152128d6SEugenio Pérez         if (unlikely(group < 0)) {
842152128d6SEugenio Pérez             r = group;
843152128d6SEugenio Pérez             goto out;
844152128d6SEugenio Pérez         }
845152128d6SEugenio Pérez 
846152128d6SEugenio Pérez         if (group == (int64_t)cvq_group) {
847152128d6SEugenio Pérez             r = 0;
848152128d6SEugenio Pérez             goto out;
849152128d6SEugenio Pérez         }
850152128d6SEugenio Pérez     }
851152128d6SEugenio Pérez 
852152128d6SEugenio Pérez     r = 1;
853152128d6SEugenio Pérez 
854152128d6SEugenio Pérez out:
855152128d6SEugenio Pérez     status = 0;
856152128d6SEugenio Pérez     ioctl(device_fd, VHOST_VDPA_SET_STATUS, &status);
857152128d6SEugenio Pérez     return r;
858152128d6SEugenio Pérez }
859152128d6SEugenio Pérez 
860654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
861654790b6SJason Wang                                        const char *device,
862654790b6SJason Wang                                        const char *name,
86340237840SJason Wang                                        int vdpa_device_fd,
86440237840SJason Wang                                        int queue_pair_index,
86540237840SJason Wang                                        int nvqs,
8661576dbb5SEugenio Pérez                                        bool is_datapath,
8671576dbb5SEugenio Pérez                                        bool svq,
8685c1ebd4cSEugenio Pérez                                        struct vhost_vdpa_iova_range iova_range,
869152128d6SEugenio Pérez                                        uint64_t features,
870152128d6SEugenio Pérez                                        Error **errp)
8711e0a84eaSCindy Lu {
8721e0a84eaSCindy Lu     NetClientState *nc = NULL;
8731e0a84eaSCindy Lu     VhostVDPAState *s;
8741e0a84eaSCindy Lu     int ret = 0;
8751e0a84eaSCindy Lu     assert(name);
876152128d6SEugenio Pérez     int cvq_isolated;
877152128d6SEugenio Pérez 
87840237840SJason Wang     if (is_datapath) {
87940237840SJason Wang         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
88040237840SJason Wang                                  name);
88140237840SJason Wang     } else {
882152128d6SEugenio Pérez         cvq_isolated = vhost_vdpa_probe_cvq_isolation(vdpa_device_fd, features,
883152128d6SEugenio Pérez                                                       queue_pair_index * 2,
884152128d6SEugenio Pérez                                                       errp);
885152128d6SEugenio Pérez         if (unlikely(cvq_isolated < 0)) {
886152128d6SEugenio Pérez             return NULL;
887152128d6SEugenio Pérez         }
888152128d6SEugenio Pérez 
889f8972b56SEugenio Pérez         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
89040237840SJason Wang                                          device, name);
89140237840SJason Wang     }
89253b85d95SLaurent Vivier     qemu_set_info_str(nc, TYPE_VHOST_VDPA);
8931e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, nc);
8947327813dSJason Wang 
8951e0a84eaSCindy Lu     s->vhost_vdpa.device_fd = vdpa_device_fd;
89640237840SJason Wang     s->vhost_vdpa.index = queue_pair_index;
8977f211a28SEugenio Pérez     s->always_svq = svq;
89869498430SEugenio Pérez     s->migration_state.notify = vdpa_net_migration_state_notifier;
8991576dbb5SEugenio Pérez     s->vhost_vdpa.shadow_vqs_enabled = svq;
900a585fad2SEugenio Pérez     s->vhost_vdpa.iova_range = iova_range;
9016188d78aSEugenio Pérez     s->vhost_vdpa.shadow_data = svq;
9025c1ebd4cSEugenio Pérez     if (queue_pair_index == 0) {
9035c1ebd4cSEugenio Pérez         vhost_vdpa_net_valid_svq_features(features,
9045c1ebd4cSEugenio Pérez                                           &s->vhost_vdpa.migration_blocker);
9055c1ebd4cSEugenio Pérez     } else if (!is_datapath) {
9062df4dd31SEugenio Pérez         s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
9072df4dd31SEugenio Pérez                                             vhost_vdpa_net_cvq_cmd_page_len());
9082df4dd31SEugenio Pérez         memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len());
90917fb889fSEugenio Pérez         s->status = qemu_memalign(qemu_real_host_page_size(),
9102df4dd31SEugenio Pérez                                   vhost_vdpa_net_cvq_cmd_page_len());
91117fb889fSEugenio Pérez         memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len());
9122df4dd31SEugenio Pérez 
913bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
914bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops_opaque = s;
915152128d6SEugenio Pérez         s->cvq_isolated = cvq_isolated;
9169c363cf6SEugenio Pérez 
9179c363cf6SEugenio Pérez         /*
918*8bc0049eSEugenio Pérez          * TODO: We cannot migrate devices with CVQ and no x-svq enabled as
919*8bc0049eSEugenio Pérez          * there is no way to set the device state (MAC, MQ, etc) before
920*8bc0049eSEugenio Pérez          * starting the datapath.
9219c363cf6SEugenio Pérez          *
9229c363cf6SEugenio Pérez          * Migration blocker ownership now belongs to s->vhost_vdpa.
9239c363cf6SEugenio Pérez          */
924*8bc0049eSEugenio Pérez         if (!svq) {
9259c363cf6SEugenio Pérez             error_setg(&s->vhost_vdpa.migration_blocker,
9269c363cf6SEugenio Pérez                        "net vdpa cannot migrate with CVQ feature");
927bd907ae4SEugenio Pérez         }
928*8bc0049eSEugenio Pérez     }
92940237840SJason Wang     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
93074af5eecSJason Wang     if (ret) {
93174af5eecSJason Wang         qemu_del_net_client(nc);
932654790b6SJason Wang         return NULL;
93374af5eecSJason Wang     }
934654790b6SJason Wang     return nc;
9351e0a84eaSCindy Lu }
9361e0a84eaSCindy Lu 
9378170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
9388170ab3fSEugenio Pérez {
9398170ab3fSEugenio Pérez     int ret = ioctl(fd, VHOST_GET_FEATURES, features);
9408170ab3fSEugenio Pérez     if (unlikely(ret < 0)) {
9418170ab3fSEugenio Pérez         error_setg_errno(errp, errno,
9428170ab3fSEugenio Pérez                          "Fail to query features from vhost-vDPA device");
9438170ab3fSEugenio Pérez     }
9448170ab3fSEugenio Pérez     return ret;
9458170ab3fSEugenio Pérez }
9468170ab3fSEugenio Pérez 
9478170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
9488170ab3fSEugenio Pérez                                           int *has_cvq, Error **errp)
94940237840SJason Wang {
95040237840SJason Wang     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
951cd523a41SStefano Garzarella     g_autofree struct vhost_vdpa_config *config = NULL;
95240237840SJason Wang     __virtio16 *max_queue_pairs;
95340237840SJason Wang     int ret;
95440237840SJason Wang 
95540237840SJason Wang     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
95640237840SJason Wang         *has_cvq = 1;
95740237840SJason Wang     } else {
95840237840SJason Wang         *has_cvq = 0;
95940237840SJason Wang     }
96040237840SJason Wang 
96140237840SJason Wang     if (features & (1 << VIRTIO_NET_F_MQ)) {
96240237840SJason Wang         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
96340237840SJason Wang         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
96440237840SJason Wang         config->len = sizeof(*max_queue_pairs);
96540237840SJason Wang 
96640237840SJason Wang         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
96740237840SJason Wang         if (ret) {
96840237840SJason Wang             error_setg(errp, "Fail to get config from vhost-vDPA device");
96940237840SJason Wang             return -ret;
97040237840SJason Wang         }
97140237840SJason Wang 
97240237840SJason Wang         max_queue_pairs = (__virtio16 *)&config->buf;
97340237840SJason Wang 
97440237840SJason Wang         return lduw_le_p(max_queue_pairs);
97540237840SJason Wang     }
97640237840SJason Wang 
97740237840SJason Wang     return 1;
97840237840SJason Wang }
97940237840SJason Wang 
9801e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
9811e0a84eaSCindy Lu                         NetClientState *peer, Error **errp)
9821e0a84eaSCindy Lu {
9831e0a84eaSCindy Lu     const NetdevVhostVDPAOptions *opts;
9848170ab3fSEugenio Pérez     uint64_t features;
985654790b6SJason Wang     int vdpa_device_fd;
986eb3cb751SEugenio Pérez     g_autofree NetClientState **ncs = NULL;
987a585fad2SEugenio Pérez     struct vhost_vdpa_iova_range iova_range;
988eb3cb751SEugenio Pérez     NetClientState *nc;
989aed5da45SEugenio Pérez     int queue_pairs, r, i = 0, has_cvq = 0;
9901e0a84eaSCindy Lu 
9911e0a84eaSCindy Lu     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
9921e0a84eaSCindy Lu     opts = &netdev->u.vhost_vdpa;
9937480874aSMarkus Armbruster     if (!opts->vhostdev && !opts->vhostfd) {
9948801ccd0SSi-Wei Liu         error_setg(errp,
9958801ccd0SSi-Wei Liu                    "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
996c8295404SEugenio Pérez         return -1;
997c8295404SEugenio Pérez     }
9987327813dSJason Wang 
9997480874aSMarkus Armbruster     if (opts->vhostdev && opts->vhostfd) {
10008801ccd0SSi-Wei Liu         error_setg(errp,
10018801ccd0SSi-Wei Liu                    "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
10028801ccd0SSi-Wei Liu         return -1;
10038801ccd0SSi-Wei Liu     }
10048801ccd0SSi-Wei Liu 
10057480874aSMarkus Armbruster     if (opts->vhostdev) {
10060351152bSEugenio Pérez         vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
10077327813dSJason Wang         if (vdpa_device_fd == -1) {
10087327813dSJason Wang             return -errno;
10097327813dSJason Wang         }
10105107fd3eSPeter Maydell     } else {
10115107fd3eSPeter Maydell         /* has_vhostfd */
10128801ccd0SSi-Wei Liu         vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
10138801ccd0SSi-Wei Liu         if (vdpa_device_fd == -1) {
10148801ccd0SSi-Wei Liu             error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
10158801ccd0SSi-Wei Liu             return -1;
10168801ccd0SSi-Wei Liu         }
10178801ccd0SSi-Wei Liu     }
10187327813dSJason Wang 
10198170ab3fSEugenio Pérez     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
10208170ab3fSEugenio Pérez     if (unlikely(r < 0)) {
1021aed5da45SEugenio Pérez         goto err;
10228170ab3fSEugenio Pérez     }
10238170ab3fSEugenio Pérez 
10248170ab3fSEugenio Pérez     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
102540237840SJason Wang                                                  &has_cvq, errp);
102640237840SJason Wang     if (queue_pairs < 0) {
10277327813dSJason Wang         qemu_close(vdpa_device_fd);
102840237840SJason Wang         return queue_pairs;
10297327813dSJason Wang     }
10307327813dSJason Wang 
1031bf7a2ad8SLongpeng     r = vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
1032bf7a2ad8SLongpeng     if (unlikely(r < 0)) {
1033bf7a2ad8SLongpeng         error_setg(errp, "vhost-vdpa: get iova range failed: %s",
1034bf7a2ad8SLongpeng                    strerror(-r));
1035bf7a2ad8SLongpeng         goto err;
1036bf7a2ad8SLongpeng     }
1037bf7a2ad8SLongpeng 
103800ef422eSEugenio Pérez     if (opts->x_svq && !vhost_vdpa_net_valid_svq_features(features, errp)) {
103900ef422eSEugenio Pérez         goto err;
10401576dbb5SEugenio Pérez     }
10411576dbb5SEugenio Pérez 
104240237840SJason Wang     ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
104340237840SJason Wang 
104440237840SJason Wang     for (i = 0; i < queue_pairs; i++) {
104540237840SJason Wang         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
10461576dbb5SEugenio Pérez                                      vdpa_device_fd, i, 2, true, opts->x_svq,
1047152128d6SEugenio Pérez                                      iova_range, features, errp);
104840237840SJason Wang         if (!ncs[i])
104940237840SJason Wang             goto err;
105040237840SJason Wang     }
105140237840SJason Wang 
105240237840SJason Wang     if (has_cvq) {
105340237840SJason Wang         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
10541576dbb5SEugenio Pérez                                  vdpa_device_fd, i, 1, false,
1055152128d6SEugenio Pérez                                  opts->x_svq, iova_range, features, errp);
105640237840SJason Wang         if (!nc)
105740237840SJason Wang             goto err;
105840237840SJason Wang     }
105940237840SJason Wang 
1060654790b6SJason Wang     return 0;
106140237840SJason Wang 
106240237840SJason Wang err:
106340237840SJason Wang     if (i) {
10649bd05507SSi-Wei Liu         for (i--; i >= 0; i--) {
10659bd05507SSi-Wei Liu             qemu_del_net_client(ncs[i]);
10669bd05507SSi-Wei Liu         }
106740237840SJason Wang     }
10681576dbb5SEugenio Pérez 
106940237840SJason Wang     qemu_close(vdpa_device_fd);
107040237840SJason Wang 
107140237840SJason Wang     return -1;
10721e0a84eaSCindy Lu }
1073