xref: /openbmc/qemu/net/vhost-vdpa.c (revision 609ab4c3)
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) |
104*609ab4c3SEugenio Pérez     /* VHOST_F_LOG_ALL is exposed by SVQ */
105*609ab4c3SEugenio Pérez     BIT_ULL(VHOST_F_LOG_ALL) |
1061576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
1071576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STANDBY);
1081576dbb5SEugenio Pérez 
109c1a10086SEugenio Pérez #define VHOST_VDPA_NET_CVQ_ASID 1
110c1a10086SEugenio Pérez 
1111e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
1121e0a84eaSCindy Lu {
1131e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1141e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1151e0a84eaSCindy Lu     return s->vhost_net;
1161e0a84eaSCindy Lu }
1171e0a84eaSCindy Lu 
11836e46472SEugenio Pérez static bool vhost_vdpa_net_valid_svq_features(uint64_t features, Error **errp)
11936e46472SEugenio Pérez {
12036e46472SEugenio Pérez     uint64_t invalid_dev_features =
12136e46472SEugenio Pérez         features & ~vdpa_svq_device_features &
12236e46472SEugenio Pérez         /* Transport are all accepted at this point */
12336e46472SEugenio Pérez         ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
12436e46472SEugenio Pérez                          VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
12536e46472SEugenio Pérez 
12636e46472SEugenio Pérez     if (invalid_dev_features) {
12736e46472SEugenio Pérez         error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
12836e46472SEugenio Pérez                    invalid_dev_features);
129258a0394SEugenio Pérez         return false;
13036e46472SEugenio Pérez     }
13136e46472SEugenio Pérez 
132258a0394SEugenio Pérez     return vhost_svq_valid_features(features, errp);
13336e46472SEugenio Pérez }
13436e46472SEugenio Pérez 
1351e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
1361e0a84eaSCindy Lu {
1371e0a84eaSCindy Lu     uint32_t device_id;
1381e0a84eaSCindy Lu     int ret;
1391e0a84eaSCindy Lu     struct vhost_dev *hdev;
1401e0a84eaSCindy Lu 
1411e0a84eaSCindy Lu     hdev = (struct vhost_dev *)&net->dev;
1421e0a84eaSCindy Lu     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
1431e0a84eaSCindy Lu     if (device_id != VIRTIO_ID_NET) {
1441e0a84eaSCindy Lu         return -ENOTSUP;
1451e0a84eaSCindy Lu     }
1461e0a84eaSCindy Lu     return ret;
1471e0a84eaSCindy Lu }
1481e0a84eaSCindy Lu 
14940237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be,
15040237840SJason Wang                           int queue_pair_index, int nvqs)
1511e0a84eaSCindy Lu {
1521e0a84eaSCindy Lu     VhostNetOptions options;
1531e0a84eaSCindy Lu     struct vhost_net *net = NULL;
1541e0a84eaSCindy Lu     VhostVDPAState *s;
1551e0a84eaSCindy Lu     int ret;
1561e0a84eaSCindy Lu 
1571e0a84eaSCindy Lu     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
1581e0a84eaSCindy Lu     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1591e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, ncs);
1601e0a84eaSCindy Lu     options.net_backend = ncs;
1611e0a84eaSCindy Lu     options.opaque      = be;
1621e0a84eaSCindy Lu     options.busyloop_timeout = 0;
16340237840SJason Wang     options.nvqs = nvqs;
1641e0a84eaSCindy Lu 
1651e0a84eaSCindy Lu     net = vhost_net_init(&options);
1661e0a84eaSCindy Lu     if (!net) {
1671e0a84eaSCindy Lu         error_report("failed to init vhost_net for queue");
168a97ef87aSJason Wang         goto err_init;
1691e0a84eaSCindy Lu     }
1701e0a84eaSCindy Lu     s->vhost_net = net;
1711e0a84eaSCindy Lu     ret = vhost_vdpa_net_check_device_id(net);
1721e0a84eaSCindy Lu     if (ret) {
173a97ef87aSJason Wang         goto err_check;
1741e0a84eaSCindy Lu     }
1751e0a84eaSCindy Lu     return 0;
176a97ef87aSJason Wang err_check:
1771e0a84eaSCindy Lu     vhost_net_cleanup(net);
178ab36edcfSJason Wang     g_free(net);
179a97ef87aSJason Wang err_init:
1801e0a84eaSCindy Lu     return -1;
1811e0a84eaSCindy Lu }
1821e0a84eaSCindy Lu 
1831e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc)
1841e0a84eaSCindy Lu {
1851e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1861e0a84eaSCindy Lu 
1872df4dd31SEugenio Pérez     qemu_vfree(s->cvq_cmd_out_buffer);
18817fb889fSEugenio Pérez     qemu_vfree(s->status);
1891e0a84eaSCindy Lu     if (s->vhost_net) {
1901e0a84eaSCindy Lu         vhost_net_cleanup(s->vhost_net);
1911e0a84eaSCindy Lu         g_free(s->vhost_net);
1921e0a84eaSCindy Lu         s->vhost_net = NULL;
1931e0a84eaSCindy Lu     }
19457b3a7d8SCindy Lu      if (s->vhost_vdpa.device_fd >= 0) {
19557b3a7d8SCindy Lu         qemu_close(s->vhost_vdpa.device_fd);
19657b3a7d8SCindy Lu         s->vhost_vdpa.device_fd = -1;
19757b3a7d8SCindy Lu     }
1981e0a84eaSCindy Lu }
1991e0a84eaSCindy Lu 
2001e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
2011e0a84eaSCindy Lu {
2021e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2031e0a84eaSCindy Lu 
2041e0a84eaSCindy Lu     return true;
2051e0a84eaSCindy Lu }
2061e0a84eaSCindy Lu 
2071e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc)
2081e0a84eaSCindy Lu {
2091e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
2101e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
2111e0a84eaSCindy Lu     uint64_t features = 0;
2121e0a84eaSCindy Lu     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
2131e0a84eaSCindy Lu     features = vhost_net_get_features(s->vhost_net, features);
2141e0a84eaSCindy Lu     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
2151e0a84eaSCindy Lu 
2161e0a84eaSCindy Lu }
2171e0a84eaSCindy Lu 
218ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
219ee8a1c63SKevin Wolf                                        Error **errp)
220ee8a1c63SKevin Wolf {
221ee8a1c63SKevin Wolf     const char *driver = object_class_get_name(oc);
222ee8a1c63SKevin Wolf 
223ee8a1c63SKevin Wolf     if (!g_str_has_prefix(driver, "virtio-net-")) {
224ee8a1c63SKevin Wolf         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
225ee8a1c63SKevin Wolf         return false;
226ee8a1c63SKevin Wolf     }
227ee8a1c63SKevin Wolf 
228ee8a1c63SKevin Wolf     return true;
229ee8a1c63SKevin Wolf }
230ee8a1c63SKevin Wolf 
231846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */
232846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
233846a1e85SEugenio Pérez                                   size_t size)
234846a1e85SEugenio Pérez {
235bc5add1dSSi-Wei Liu     return size;
236846a1e85SEugenio Pérez }
237846a1e85SEugenio Pérez 
23800ef422eSEugenio Pérez /** From any vdpa net client, get the netclient of the first queue pair */
23900ef422eSEugenio Pérez static VhostVDPAState *vhost_vdpa_net_first_nc_vdpa(VhostVDPAState *s)
24000ef422eSEugenio Pérez {
24100ef422eSEugenio Pérez     NICState *nic = qemu_get_nic(s->nc.peer);
24200ef422eSEugenio Pérez     NetClientState *nc0 = qemu_get_peer(nic->ncs, 0);
24300ef422eSEugenio Pérez 
24400ef422eSEugenio Pérez     return DO_UPCAST(VhostVDPAState, nc, nc0);
24500ef422eSEugenio Pérez }
24600ef422eSEugenio Pérez 
24769498430SEugenio Pérez static void vhost_vdpa_net_log_global_enable(VhostVDPAState *s, bool enable)
24869498430SEugenio Pérez {
24969498430SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
25069498430SEugenio Pérez     VirtIONet *n;
25169498430SEugenio Pérez     VirtIODevice *vdev;
25269498430SEugenio Pérez     int data_queue_pairs, cvq, r;
25369498430SEugenio Pérez 
25469498430SEugenio Pérez     /* We are only called on the first data vqs and only if x-svq is not set */
25569498430SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled == enable) {
25669498430SEugenio Pérez         return;
25769498430SEugenio Pérez     }
25869498430SEugenio Pérez 
25969498430SEugenio Pérez     vdev = v->dev->vdev;
26069498430SEugenio Pérez     n = VIRTIO_NET(vdev);
26169498430SEugenio Pérez     if (!n->vhost_started) {
26269498430SEugenio Pérez         return;
26369498430SEugenio Pérez     }
26469498430SEugenio Pérez 
26569498430SEugenio Pérez     data_queue_pairs = n->multiqueue ? n->max_queue_pairs : 1;
26669498430SEugenio Pérez     cvq = virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) ?
26769498430SEugenio Pérez                                   n->max_ncs - n->max_queue_pairs : 0;
26869498430SEugenio Pérez     /*
26969498430SEugenio Pérez      * TODO: vhost_net_stop does suspend, get_base and reset. We can be smarter
27069498430SEugenio Pérez      * in the future and resume the device if read-only operations between
27169498430SEugenio Pérez      * suspend and reset goes wrong.
27269498430SEugenio Pérez      */
27369498430SEugenio Pérez     vhost_net_stop(vdev, n->nic->ncs, data_queue_pairs, cvq);
27469498430SEugenio Pérez 
27569498430SEugenio Pérez     /* Start will check migration setup_or_active to configure or not SVQ */
27669498430SEugenio Pérez     r = vhost_net_start(vdev, n->nic->ncs, data_queue_pairs, cvq);
27769498430SEugenio Pérez     if (unlikely(r < 0)) {
27869498430SEugenio Pérez         error_report("unable to start vhost net: %s(%d)", g_strerror(-r), -r);
27969498430SEugenio Pérez     }
28069498430SEugenio Pérez }
28169498430SEugenio Pérez 
28269498430SEugenio Pérez static void vdpa_net_migration_state_notifier(Notifier *notifier, void *data)
28369498430SEugenio Pérez {
28469498430SEugenio Pérez     MigrationState *migration = data;
28569498430SEugenio Pérez     VhostVDPAState *s = container_of(notifier, VhostVDPAState,
28669498430SEugenio Pérez                                      migration_state);
28769498430SEugenio Pérez 
28869498430SEugenio Pérez     if (migration_in_setup(migration)) {
28969498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, true);
29069498430SEugenio Pérez     } else if (migration_has_failed(migration)) {
29169498430SEugenio Pérez         vhost_vdpa_net_log_global_enable(s, false);
29269498430SEugenio Pérez     }
29369498430SEugenio Pérez }
29469498430SEugenio Pérez 
29500ef422eSEugenio Pérez static void vhost_vdpa_net_data_start_first(VhostVDPAState *s)
29600ef422eSEugenio Pérez {
29700ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
29800ef422eSEugenio Pérez 
29969498430SEugenio Pérez     add_migration_state_change_notifier(&s->migration_state);
30000ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
30100ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
30200ef422eSEugenio Pérez                                            v->iova_range.last);
30300ef422eSEugenio Pérez     }
30400ef422eSEugenio Pérez }
30500ef422eSEugenio Pérez 
30600ef422eSEugenio Pérez static int vhost_vdpa_net_data_start(NetClientState *nc)
30700ef422eSEugenio Pérez {
30800ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
30900ef422eSEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
31000ef422eSEugenio Pérez 
31100ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
31200ef422eSEugenio Pérez 
31369498430SEugenio Pérez     if (s->always_svq ||
31469498430SEugenio Pérez         migration_is_setup_or_active(migrate_get_current()->state)) {
31569498430SEugenio Pérez         v->shadow_vqs_enabled = true;
31669498430SEugenio Pérez         v->shadow_data = true;
31769498430SEugenio Pérez     } else {
31869498430SEugenio Pérez         v->shadow_vqs_enabled = false;
31969498430SEugenio Pérez         v->shadow_data = false;
32069498430SEugenio Pérez     }
32169498430SEugenio Pérez 
32200ef422eSEugenio Pérez     if (v->index == 0) {
32300ef422eSEugenio Pérez         vhost_vdpa_net_data_start_first(s);
32400ef422eSEugenio Pérez         return 0;
32500ef422eSEugenio Pérez     }
32600ef422eSEugenio Pérez 
32700ef422eSEugenio Pérez     if (v->shadow_vqs_enabled) {
32800ef422eSEugenio Pérez         VhostVDPAState *s0 = vhost_vdpa_net_first_nc_vdpa(s);
32900ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
33000ef422eSEugenio Pérez     }
33100ef422eSEugenio Pérez 
33200ef422eSEugenio Pérez     return 0;
33300ef422eSEugenio Pérez }
33400ef422eSEugenio Pérez 
33500ef422eSEugenio Pérez static void vhost_vdpa_net_client_stop(NetClientState *nc)
33600ef422eSEugenio Pérez {
33700ef422eSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
33800ef422eSEugenio Pérez     struct vhost_dev *dev;
33900ef422eSEugenio Pérez 
34000ef422eSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
34100ef422eSEugenio Pérez 
34269498430SEugenio Pérez     if (s->vhost_vdpa.index == 0) {
34369498430SEugenio Pérez         remove_migration_state_change_notifier(&s->migration_state);
34469498430SEugenio Pérez     }
34569498430SEugenio Pérez 
34600ef422eSEugenio Pérez     dev = s->vhost_vdpa.dev;
34700ef422eSEugenio Pérez     if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
34800ef422eSEugenio Pérez         g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
34900ef422eSEugenio Pérez     }
35000ef422eSEugenio Pérez }
35100ef422eSEugenio Pérez 
3521e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = {
3531e0a84eaSCindy Lu         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
3541e0a84eaSCindy Lu         .size = sizeof(VhostVDPAState),
355846a1e85SEugenio Pérez         .receive = vhost_vdpa_receive,
35600ef422eSEugenio Pérez         .start = vhost_vdpa_net_data_start,
35700ef422eSEugenio Pérez         .stop = vhost_vdpa_net_client_stop,
3581e0a84eaSCindy Lu         .cleanup = vhost_vdpa_cleanup,
3591e0a84eaSCindy Lu         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
3601e0a84eaSCindy Lu         .has_ufo = vhost_vdpa_has_ufo,
361ee8a1c63SKevin Wolf         .check_peer_type = vhost_vdpa_check_peer_type,
3621e0a84eaSCindy Lu };
3631e0a84eaSCindy Lu 
364c1a10086SEugenio Pérez static int64_t vhost_vdpa_get_vring_group(int device_fd, unsigned vq_index)
365c1a10086SEugenio Pérez {
366c1a10086SEugenio Pérez     struct vhost_vring_state state = {
367c1a10086SEugenio Pérez         .index = vq_index,
368c1a10086SEugenio Pérez     };
369c1a10086SEugenio Pérez     int r = ioctl(device_fd, VHOST_VDPA_GET_VRING_GROUP, &state);
370c1a10086SEugenio Pérez 
371c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
372c1a10086SEugenio Pérez         error_report("Cannot get VQ %u group: %s", vq_index,
373c1a10086SEugenio Pérez                      g_strerror(errno));
374c1a10086SEugenio Pérez         return r;
375c1a10086SEugenio Pérez     }
376c1a10086SEugenio Pérez 
377c1a10086SEugenio Pérez     return state.num;
378c1a10086SEugenio Pérez }
379c1a10086SEugenio Pérez 
380c1a10086SEugenio Pérez static int vhost_vdpa_set_address_space_id(struct vhost_vdpa *v,
381c1a10086SEugenio Pérez                                            unsigned vq_group,
382c1a10086SEugenio Pérez                                            unsigned asid_num)
383c1a10086SEugenio Pérez {
384c1a10086SEugenio Pérez     struct vhost_vring_state asid = {
385c1a10086SEugenio Pérez         .index = vq_group,
386c1a10086SEugenio Pérez         .num = asid_num,
387c1a10086SEugenio Pérez     };
388c1a10086SEugenio Pérez     int r;
389c1a10086SEugenio Pérez 
390c1a10086SEugenio Pérez     r = ioctl(v->device_fd, VHOST_VDPA_SET_GROUP_ASID, &asid);
391c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
392c1a10086SEugenio Pérez         error_report("Can't set vq group %u asid %u, errno=%d (%s)",
393c1a10086SEugenio Pérez                      asid.index, asid.num, errno, g_strerror(errno));
394c1a10086SEugenio Pérez     }
395c1a10086SEugenio Pérez     return r;
396c1a10086SEugenio Pérez }
397c1a10086SEugenio Pérez 
3982df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
3992df4dd31SEugenio Pérez {
4002df4dd31SEugenio Pérez     VhostIOVATree *tree = v->iova_tree;
4012df4dd31SEugenio Pérez     DMAMap needle = {
4022df4dd31SEugenio Pérez         /*
4032df4dd31SEugenio Pérez          * No need to specify size or to look for more translations since
4042df4dd31SEugenio Pérez          * this contiguous chunk was allocated by us.
4052df4dd31SEugenio Pérez          */
4062df4dd31SEugenio Pérez         .translated_addr = (hwaddr)(uintptr_t)addr,
4072df4dd31SEugenio Pérez     };
4082df4dd31SEugenio Pérez     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
4092df4dd31SEugenio Pérez     int r;
4102df4dd31SEugenio Pérez 
4112df4dd31SEugenio Pérez     if (unlikely(!map)) {
4122df4dd31SEugenio Pérez         error_report("Cannot locate expected map");
4132df4dd31SEugenio Pérez         return;
4142df4dd31SEugenio Pérez     }
4152df4dd31SEugenio Pérez 
416cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_unmap(v, v->address_space_id, map->iova, map->size + 1);
4172df4dd31SEugenio Pérez     if (unlikely(r != 0)) {
4182df4dd31SEugenio Pérez         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
4192df4dd31SEugenio Pérez     }
4202df4dd31SEugenio Pérez 
42169292a8eSEugenio Pérez     vhost_iova_tree_remove(tree, *map);
4222df4dd31SEugenio Pérez }
4232df4dd31SEugenio Pérez 
4242df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void)
4252df4dd31SEugenio Pérez {
4262df4dd31SEugenio Pérez     /*
4272df4dd31SEugenio Pérez      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
4282df4dd31SEugenio Pérez      * In buffer is always 1 byte, so it should fit here
4292df4dd31SEugenio Pérez      */
4302df4dd31SEugenio Pérez     return sizeof(struct virtio_net_ctrl_hdr) +
4312df4dd31SEugenio Pérez            2 * sizeof(struct virtio_net_ctrl_mac) +
4322df4dd31SEugenio Pérez            MAC_TABLE_ENTRIES * ETH_ALEN;
4332df4dd31SEugenio Pérez }
4342df4dd31SEugenio Pérez 
4352df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
4362df4dd31SEugenio Pérez {
4372df4dd31SEugenio Pérez     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
4382df4dd31SEugenio Pérez }
4392df4dd31SEugenio Pérez 
4407a7f87e9SEugenio Pérez /** Map CVQ buffer. */
4417a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
4427a7f87e9SEugenio Pérez                                   bool write)
4432df4dd31SEugenio Pérez {
4442df4dd31SEugenio Pérez     DMAMap map = {};
4452df4dd31SEugenio Pérez     int r;
4462df4dd31SEugenio Pérez 
4472df4dd31SEugenio Pérez     map.translated_addr = (hwaddr)(uintptr_t)buf;
4487a7f87e9SEugenio Pérez     map.size = size - 1;
4492df4dd31SEugenio Pérez     map.perm = write ? IOMMU_RW : IOMMU_RO,
4502df4dd31SEugenio Pérez     r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
4512df4dd31SEugenio Pérez     if (unlikely(r != IOVA_OK)) {
4522df4dd31SEugenio Pérez         error_report("Cannot map injected element");
4537a7f87e9SEugenio Pérez         return r;
4542df4dd31SEugenio Pérez     }
4552df4dd31SEugenio Pérez 
456cd831ed5SEugenio Pérez     r = vhost_vdpa_dma_map(v, v->address_space_id, map.iova,
457cd831ed5SEugenio Pérez                            vhost_vdpa_net_cvq_cmd_page_len(), buf, !write);
4582df4dd31SEugenio Pérez     if (unlikely(r < 0)) {
4592df4dd31SEugenio Pérez         goto dma_map_err;
4602df4dd31SEugenio Pérez     }
4612df4dd31SEugenio Pérez 
4627a7f87e9SEugenio Pérez     return 0;
4632df4dd31SEugenio Pérez 
4642df4dd31SEugenio Pérez dma_map_err:
46569292a8eSEugenio Pérez     vhost_iova_tree_remove(v->iova_tree, map);
4667a7f87e9SEugenio Pérez     return r;
4672df4dd31SEugenio Pérez }
4682df4dd31SEugenio Pérez 
4697a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc)
4702df4dd31SEugenio Pérez {
47100ef422eSEugenio Pérez     VhostVDPAState *s, *s0;
472c1a10086SEugenio Pérez     struct vhost_vdpa *v;
473c1a10086SEugenio Pérez     uint64_t backend_features;
474c1a10086SEugenio Pérez     int64_t cvq_group;
475c1a10086SEugenio Pérez     int cvq_index, r;
4762df4dd31SEugenio Pérez 
4777a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
4787a7f87e9SEugenio Pérez 
4797a7f87e9SEugenio Pérez     s = DO_UPCAST(VhostVDPAState, nc, nc);
480c1a10086SEugenio Pérez     v = &s->vhost_vdpa;
481c1a10086SEugenio Pérez 
48269498430SEugenio Pérez     s0 = vhost_vdpa_net_first_nc_vdpa(s);
48369498430SEugenio Pérez     v->shadow_data = s0->vhost_vdpa.shadow_vqs_enabled;
484c1a10086SEugenio Pérez     v->shadow_vqs_enabled = s->always_svq;
485c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_GUEST_PA_ASID;
486c1a10086SEugenio Pérez 
48769498430SEugenio Pérez     if (s->vhost_vdpa.shadow_data) {
488c1a10086SEugenio Pérez         /* SVQ is already configured for all virtqueues */
489c1a10086SEugenio Pérez         goto out;
490c1a10086SEugenio Pérez     }
491c1a10086SEugenio Pérez 
492c1a10086SEugenio Pérez     /*
493c1a10086SEugenio Pérez      * If we early return in these cases SVQ will not be enabled. The migration
494c1a10086SEugenio Pérez      * will be blocked as long as vhost-vdpa backends will not offer _F_LOG.
495c1a10086SEugenio Pérez      *
496c1a10086SEugenio Pérez      * Calling VHOST_GET_BACKEND_FEATURES as they are not available in v->dev
497c1a10086SEugenio Pérez      * yet.
498c1a10086SEugenio Pérez      */
499c1a10086SEugenio Pérez     r = ioctl(v->device_fd, VHOST_GET_BACKEND_FEATURES, &backend_features);
500c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
501c1a10086SEugenio Pérez         error_report("Cannot get vdpa backend_features: %s(%d)",
502c1a10086SEugenio Pérez             g_strerror(errno), errno);
503c1a10086SEugenio Pérez         return -1;
504c1a10086SEugenio Pérez     }
505525ae115SEugenio Pérez     if (!(backend_features & BIT_ULL(VHOST_BACKEND_F_IOTLB_ASID)) ||
506c1a10086SEugenio Pérez         !vhost_vdpa_net_valid_svq_features(v->dev->features, NULL)) {
507c1a10086SEugenio Pérez         return 0;
508c1a10086SEugenio Pérez     }
509c1a10086SEugenio Pérez 
510c1a10086SEugenio Pérez     /*
511c1a10086SEugenio Pérez      * Check if all the virtqueues of the virtio device are in a different vq
512c1a10086SEugenio Pérez      * than the last vq. VQ group of last group passed in cvq_group.
513c1a10086SEugenio Pérez      */
514c1a10086SEugenio Pérez     cvq_index = v->dev->vq_index_end - 1;
515c1a10086SEugenio Pérez     cvq_group = vhost_vdpa_get_vring_group(v->device_fd, cvq_index);
516c1a10086SEugenio Pérez     if (unlikely(cvq_group < 0)) {
517c1a10086SEugenio Pérez         return cvq_group;
518c1a10086SEugenio Pérez     }
519c1a10086SEugenio Pérez     for (int i = 0; i < cvq_index; ++i) {
520c1a10086SEugenio Pérez         int64_t group = vhost_vdpa_get_vring_group(v->device_fd, i);
521c1a10086SEugenio Pérez 
522c1a10086SEugenio Pérez         if (unlikely(group < 0)) {
523c1a10086SEugenio Pérez             return group;
524c1a10086SEugenio Pérez         }
525c1a10086SEugenio Pérez 
526c1a10086SEugenio Pérez         if (group == cvq_group) {
527c1a10086SEugenio Pérez             return 0;
528c1a10086SEugenio Pérez         }
529c1a10086SEugenio Pérez     }
530c1a10086SEugenio Pérez 
531c1a10086SEugenio Pérez     r = vhost_vdpa_set_address_space_id(v, cvq_group, VHOST_VDPA_NET_CVQ_ASID);
532c1a10086SEugenio Pérez     if (unlikely(r < 0)) {
533c1a10086SEugenio Pérez         return r;
534c1a10086SEugenio Pérez     }
535c1a10086SEugenio Pérez 
536c1a10086SEugenio Pérez     v->shadow_vqs_enabled = true;
537c1a10086SEugenio Pérez     s->vhost_vdpa.address_space_id = VHOST_VDPA_NET_CVQ_ASID;
538c1a10086SEugenio Pérez 
539c1a10086SEugenio Pérez out:
5407a7f87e9SEugenio Pérez     if (!s->vhost_vdpa.shadow_vqs_enabled) {
5417a7f87e9SEugenio Pérez         return 0;
5422df4dd31SEugenio Pérez     }
5432df4dd31SEugenio Pérez 
54400ef422eSEugenio Pérez     if (s0->vhost_vdpa.iova_tree) {
54500ef422eSEugenio Pérez         /*
54600ef422eSEugenio Pérez          * SVQ is already configured for all virtqueues.  Reuse IOVA tree for
54700ef422eSEugenio Pérez          * simplicity, whether CVQ shares ASID with guest or not, because:
54800ef422eSEugenio Pérez          * - Memory listener need access to guest's memory addresses allocated
54900ef422eSEugenio Pérez          *   in the IOVA tree.
55000ef422eSEugenio Pérez          * - There should be plenty of IOVA address space for both ASID not to
55100ef422eSEugenio Pérez          *   worry about collisions between them.  Guest's translations are
55200ef422eSEugenio Pérez          *   still validated with virtio virtqueue_pop so there is no risk for
55300ef422eSEugenio Pérez          *   the guest to access memory that it shouldn't.
55400ef422eSEugenio Pérez          *
55500ef422eSEugenio Pérez          * To allocate a iova tree per ASID is doable but it complicates the
55600ef422eSEugenio Pérez          * code and it is not worth it for the moment.
55700ef422eSEugenio Pérez          */
55800ef422eSEugenio Pérez         v->iova_tree = s0->vhost_vdpa.iova_tree;
55900ef422eSEugenio Pérez     } else {
56000ef422eSEugenio Pérez         v->iova_tree = vhost_iova_tree_new(v->iova_range.first,
56100ef422eSEugenio Pérez                                            v->iova_range.last);
56200ef422eSEugenio Pérez     }
56300ef422eSEugenio Pérez 
5647a7f87e9SEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
5657a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), false);
5667a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5677a7f87e9SEugenio Pérez         return r;
5687a7f87e9SEugenio Pérez     }
5697a7f87e9SEugenio Pérez 
57017fb889fSEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
5717a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), true);
5727a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
5732df4dd31SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
5742df4dd31SEugenio Pérez     }
5752df4dd31SEugenio Pérez 
5767a7f87e9SEugenio Pérez     return r;
5777a7f87e9SEugenio Pérez }
5787a7f87e9SEugenio Pérez 
5797a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
5807a7f87e9SEugenio Pérez {
5817a7f87e9SEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
5827a7f87e9SEugenio Pérez 
5837a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
5847a7f87e9SEugenio Pérez 
5857a7f87e9SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled) {
5867a7f87e9SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
58717fb889fSEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
588c1a10086SEugenio Pérez     }
58900ef422eSEugenio Pérez 
59000ef422eSEugenio Pérez     vhost_vdpa_net_client_stop(nc);
5912df4dd31SEugenio Pérez }
5922df4dd31SEugenio Pérez 
593be4278b6SEugenio Pérez static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len,
594be4278b6SEugenio Pérez                                       size_t in_len)
595be4278b6SEugenio Pérez {
596be4278b6SEugenio Pérez     /* Buffers for the device */
597be4278b6SEugenio Pérez     const struct iovec out = {
598be4278b6SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
599be4278b6SEugenio Pérez         .iov_len = out_len,
600be4278b6SEugenio Pérez     };
601be4278b6SEugenio Pérez     const struct iovec in = {
60217fb889fSEugenio Pérez         .iov_base = s->status,
603be4278b6SEugenio Pérez         .iov_len = sizeof(virtio_net_ctrl_ack),
604be4278b6SEugenio Pérez     };
605be4278b6SEugenio Pérez     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
606be4278b6SEugenio Pérez     int r;
607be4278b6SEugenio Pérez 
608be4278b6SEugenio Pérez     r = vhost_svq_add(svq, &out, 1, &in, 1, NULL);
609be4278b6SEugenio Pérez     if (unlikely(r != 0)) {
610be4278b6SEugenio Pérez         if (unlikely(r == -ENOSPC)) {
611be4278b6SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
612be4278b6SEugenio Pérez                           __func__);
613be4278b6SEugenio Pérez         }
614be4278b6SEugenio Pérez         return r;
615be4278b6SEugenio Pérez     }
616be4278b6SEugenio Pérez 
617be4278b6SEugenio Pérez     /*
618be4278b6SEugenio Pérez      * We can poll here since we've had BQL from the time we sent the
619be4278b6SEugenio Pérez      * descriptor. Also, we need to take the answer before SVQ pulls by itself,
620be4278b6SEugenio Pérez      * when BQL is released
621be4278b6SEugenio Pérez      */
622be4278b6SEugenio Pérez     return vhost_svq_poll(svq);
623be4278b6SEugenio Pérez }
624be4278b6SEugenio Pérez 
625f73c0c43SEugenio Pérez static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class,
626f73c0c43SEugenio Pérez                                        uint8_t cmd, const void *data,
627f73c0c43SEugenio Pérez                                        size_t data_size)
628f73c0c43SEugenio Pérez {
629f73c0c43SEugenio Pérez     const struct virtio_net_ctrl_hdr ctrl = {
630f73c0c43SEugenio Pérez         .class = class,
631f73c0c43SEugenio Pérez         .cmd = cmd,
632f73c0c43SEugenio Pérez     };
633f73c0c43SEugenio Pérez 
634f73c0c43SEugenio Pérez     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
635f73c0c43SEugenio Pérez 
636f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl));
637f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size);
638f73c0c43SEugenio Pérez 
639f73c0c43SEugenio Pérez     return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size,
640f73c0c43SEugenio Pérez                                   sizeof(virtio_net_ctrl_ack));
641f73c0c43SEugenio Pérez }
642f73c0c43SEugenio Pérez 
643f73c0c43SEugenio Pérez static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n)
644f73c0c43SEugenio Pérez {
645f73c0c43SEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
646f73c0c43SEugenio Pérez     if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) {
647f73c0c43SEugenio Pérez         ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC,
648f73c0c43SEugenio Pérez                                                   VIRTIO_NET_CTRL_MAC_ADDR_SET,
649f73c0c43SEugenio Pérez                                                   n->mac, sizeof(n->mac));
650f73c0c43SEugenio Pérez         if (unlikely(dev_written < 0)) {
651f73c0c43SEugenio Pérez             return dev_written;
652f73c0c43SEugenio Pérez         }
653f73c0c43SEugenio Pérez 
654f73c0c43SEugenio Pérez         return *s->status != VIRTIO_NET_OK;
655f73c0c43SEugenio Pérez     }
656f73c0c43SEugenio Pérez 
657f73c0c43SEugenio Pérez     return 0;
658f73c0c43SEugenio Pérez }
659f73c0c43SEugenio Pérez 
660f64c7cdaSEugenio Pérez static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
661f64c7cdaSEugenio Pérez                                   const VirtIONet *n)
662f64c7cdaSEugenio Pérez {
663f64c7cdaSEugenio Pérez     struct virtio_net_ctrl_mq mq;
664f64c7cdaSEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
665f64c7cdaSEugenio Pérez     ssize_t dev_written;
666f64c7cdaSEugenio Pérez 
667f64c7cdaSEugenio Pérez     if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) {
668f64c7cdaSEugenio Pérez         return 0;
669f64c7cdaSEugenio Pérez     }
670f64c7cdaSEugenio Pérez 
671f64c7cdaSEugenio Pérez     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
672f64c7cdaSEugenio Pérez     dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ,
673f64c7cdaSEugenio Pérez                                           VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq,
674f64c7cdaSEugenio Pérez                                           sizeof(mq));
675f64c7cdaSEugenio Pérez     if (unlikely(dev_written < 0)) {
676f64c7cdaSEugenio Pérez         return dev_written;
677f64c7cdaSEugenio Pérez     }
678f64c7cdaSEugenio Pérez 
679f64c7cdaSEugenio Pérez     return *s->status != VIRTIO_NET_OK;
680f64c7cdaSEugenio Pérez }
681f64c7cdaSEugenio Pérez 
682dd036d8dSEugenio Pérez static int vhost_vdpa_net_load(NetClientState *nc)
683dd036d8dSEugenio Pérez {
684dd036d8dSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
685f73c0c43SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
686dd036d8dSEugenio Pérez     const VirtIONet *n;
687f73c0c43SEugenio Pérez     int r;
688dd036d8dSEugenio Pérez 
689dd036d8dSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
690dd036d8dSEugenio Pérez 
691dd036d8dSEugenio Pérez     if (!v->shadow_vqs_enabled) {
692dd036d8dSEugenio Pérez         return 0;
693dd036d8dSEugenio Pérez     }
694dd036d8dSEugenio Pérez 
695dd036d8dSEugenio Pérez     n = VIRTIO_NET(v->dev->vdev);
696f73c0c43SEugenio Pérez     r = vhost_vdpa_net_load_mac(s, n);
697f73c0c43SEugenio Pérez     if (unlikely(r < 0)) {
698f73c0c43SEugenio Pérez         return r;
699dd036d8dSEugenio Pérez     }
700f64c7cdaSEugenio Pérez     r = vhost_vdpa_net_load_mq(s, n);
701f64c7cdaSEugenio Pérez     if (unlikely(r)) {
702f64c7cdaSEugenio Pérez         return r;
703f64c7cdaSEugenio Pérez     }
704dd036d8dSEugenio Pérez 
705dd036d8dSEugenio Pérez     return 0;
706dd036d8dSEugenio Pérez }
707dd036d8dSEugenio Pérez 
708f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = {
709f8972b56SEugenio Pérez     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
710f8972b56SEugenio Pérez     .size = sizeof(VhostVDPAState),
711f8972b56SEugenio Pérez     .receive = vhost_vdpa_receive,
7127a7f87e9SEugenio Pérez     .start = vhost_vdpa_net_cvq_start,
713dd036d8dSEugenio Pérez     .load = vhost_vdpa_net_load,
7147a7f87e9SEugenio Pérez     .stop = vhost_vdpa_net_cvq_stop,
715f8972b56SEugenio Pérez     .cleanup = vhost_vdpa_cleanup,
716f8972b56SEugenio Pérez     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
717f8972b56SEugenio Pérez     .has_ufo = vhost_vdpa_has_ufo,
718f8972b56SEugenio Pérez     .check_peer_type = vhost_vdpa_check_peer_type,
719f8972b56SEugenio Pérez };
720f8972b56SEugenio Pérez 
7212df4dd31SEugenio Pérez /**
7222df4dd31SEugenio Pérez  * Validate and copy control virtqueue commands.
7232df4dd31SEugenio Pérez  *
7242df4dd31SEugenio Pérez  * Following QEMU guidelines, we offer a copy of the buffers to the device to
7252df4dd31SEugenio Pérez  * prevent TOCTOU bugs.
726bd907ae4SEugenio Pérez  */
727bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
728bd907ae4SEugenio Pérez                                             VirtQueueElement *elem,
729bd907ae4SEugenio Pérez                                             void *opaque)
730bd907ae4SEugenio Pérez {
7312df4dd31SEugenio Pérez     VhostVDPAState *s = opaque;
732be4278b6SEugenio Pérez     size_t in_len;
733bd907ae4SEugenio Pérez     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
7347a7f87e9SEugenio Pérez     /* Out buffer sent to both the vdpa device and the device model */
7357a7f87e9SEugenio Pérez     struct iovec out = {
7367a7f87e9SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
7377a7f87e9SEugenio Pérez     };
7382df4dd31SEugenio Pérez     /* in buffer used for device model */
7392df4dd31SEugenio Pérez     const struct iovec in = {
7402df4dd31SEugenio Pérez         .iov_base = &status,
7412df4dd31SEugenio Pérez         .iov_len = sizeof(status),
7422df4dd31SEugenio Pérez     };
743be4278b6SEugenio Pérez     ssize_t dev_written = -EINVAL;
744bd907ae4SEugenio Pérez 
7457a7f87e9SEugenio Pérez     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
7467a7f87e9SEugenio Pérez                              s->cvq_cmd_out_buffer,
7477a7f87e9SEugenio Pérez                              vhost_vdpa_net_cvq_cmd_len());
7483f9a3eebSEugenio Pérez     if (*(uint8_t *)s->cvq_cmd_out_buffer == VIRTIO_NET_CTRL_ANNOUNCE) {
7493f9a3eebSEugenio Pérez         /*
7503f9a3eebSEugenio Pérez          * Guest announce capability is emulated by qemu, so don't forward to
7513f9a3eebSEugenio Pérez          * the device.
7523f9a3eebSEugenio Pérez          */
7533f9a3eebSEugenio Pérez         dev_written = sizeof(status);
7543f9a3eebSEugenio Pérez         *s->status = VIRTIO_NET_OK;
7553f9a3eebSEugenio Pérez     } else {
756be4278b6SEugenio Pérez         dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
757be4278b6SEugenio Pérez         if (unlikely(dev_written < 0)) {
758bd907ae4SEugenio Pérez             goto out;
759bd907ae4SEugenio Pérez         }
7603f9a3eebSEugenio Pérez     }
761bd907ae4SEugenio Pérez 
762bd907ae4SEugenio Pérez     if (unlikely(dev_written < sizeof(status))) {
763bd907ae4SEugenio Pérez         error_report("Insufficient written data (%zu)", dev_written);
7642df4dd31SEugenio Pérez         goto out;
7652df4dd31SEugenio Pérez     }
7662df4dd31SEugenio Pérez 
76717fb889fSEugenio Pérez     if (*s->status != VIRTIO_NET_OK) {
768be4278b6SEugenio Pérez         return VIRTIO_NET_ERR;
7692df4dd31SEugenio Pérez     }
7702df4dd31SEugenio Pérez 
7712df4dd31SEugenio Pérez     status = VIRTIO_NET_ERR;
7727a7f87e9SEugenio Pérez     virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
7732df4dd31SEugenio Pérez     if (status != VIRTIO_NET_OK) {
7742df4dd31SEugenio Pérez         error_report("Bad CVQ processing in model");
775bd907ae4SEugenio Pérez     }
776bd907ae4SEugenio Pérez 
777bd907ae4SEugenio Pérez out:
778bd907ae4SEugenio Pérez     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
779bd907ae4SEugenio Pérez                           sizeof(status));
780bd907ae4SEugenio Pérez     if (unlikely(in_len < sizeof(status))) {
781bd907ae4SEugenio Pérez         error_report("Bad device CVQ written length");
782bd907ae4SEugenio Pérez     }
783bd907ae4SEugenio Pérez     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
784bd907ae4SEugenio Pérez     g_free(elem);
785be4278b6SEugenio Pérez     return dev_written < 0 ? dev_written : 0;
786bd907ae4SEugenio Pérez }
787bd907ae4SEugenio Pérez 
788bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
789bd907ae4SEugenio Pérez     .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
790bd907ae4SEugenio Pérez };
791bd907ae4SEugenio Pérez 
792654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
793654790b6SJason Wang                                        const char *device,
794654790b6SJason Wang                                        const char *name,
79540237840SJason Wang                                        int vdpa_device_fd,
79640237840SJason Wang                                        int queue_pair_index,
79740237840SJason Wang                                        int nvqs,
7981576dbb5SEugenio Pérez                                        bool is_datapath,
7991576dbb5SEugenio Pérez                                        bool svq,
8005c1ebd4cSEugenio Pérez                                        struct vhost_vdpa_iova_range iova_range,
8015c1ebd4cSEugenio Pérez                                        uint64_t features)
8021e0a84eaSCindy Lu {
8031e0a84eaSCindy Lu     NetClientState *nc = NULL;
8041e0a84eaSCindy Lu     VhostVDPAState *s;
8051e0a84eaSCindy Lu     int ret = 0;
8061e0a84eaSCindy Lu     assert(name);
80740237840SJason Wang     if (is_datapath) {
80840237840SJason Wang         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
80940237840SJason Wang                                  name);
81040237840SJason Wang     } else {
811f8972b56SEugenio Pérez         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
81240237840SJason Wang                                          device, name);
81340237840SJason Wang     }
81453b85d95SLaurent Vivier     qemu_set_info_str(nc, TYPE_VHOST_VDPA);
8151e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, nc);
8167327813dSJason Wang 
8171e0a84eaSCindy Lu     s->vhost_vdpa.device_fd = vdpa_device_fd;
81840237840SJason Wang     s->vhost_vdpa.index = queue_pair_index;
8197f211a28SEugenio Pérez     s->always_svq = svq;
82069498430SEugenio Pérez     s->migration_state.notify = vdpa_net_migration_state_notifier;
8211576dbb5SEugenio Pérez     s->vhost_vdpa.shadow_vqs_enabled = svq;
822a585fad2SEugenio Pérez     s->vhost_vdpa.iova_range = iova_range;
8236188d78aSEugenio Pérez     s->vhost_vdpa.shadow_data = svq;
8245c1ebd4cSEugenio Pérez     if (queue_pair_index == 0) {
8255c1ebd4cSEugenio Pérez         vhost_vdpa_net_valid_svq_features(features,
8265c1ebd4cSEugenio Pérez                                           &s->vhost_vdpa.migration_blocker);
8275c1ebd4cSEugenio Pérez     } else if (!is_datapath) {
8282df4dd31SEugenio Pérez         s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
8292df4dd31SEugenio Pérez                                             vhost_vdpa_net_cvq_cmd_page_len());
8302df4dd31SEugenio Pérez         memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len());
83117fb889fSEugenio Pérez         s->status = qemu_memalign(qemu_real_host_page_size(),
8322df4dd31SEugenio Pérez                                   vhost_vdpa_net_cvq_cmd_page_len());
83317fb889fSEugenio Pérez         memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len());
8342df4dd31SEugenio Pérez 
835bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
836bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops_opaque = s;
8379c363cf6SEugenio Pérez 
8389c363cf6SEugenio Pérez         /*
8399c363cf6SEugenio Pérez          * TODO: We cannot migrate devices with CVQ as there is no way to set
8409c363cf6SEugenio Pérez          * the device state (MAC, MQ, etc) before starting the datapath.
8419c363cf6SEugenio Pérez          *
8429c363cf6SEugenio Pérez          * Migration blocker ownership now belongs to s->vhost_vdpa.
8439c363cf6SEugenio Pérez          */
8449c363cf6SEugenio Pérez         error_setg(&s->vhost_vdpa.migration_blocker,
8459c363cf6SEugenio Pérez                    "net vdpa cannot migrate with CVQ feature");
846bd907ae4SEugenio Pérez     }
84740237840SJason Wang     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
84874af5eecSJason Wang     if (ret) {
84974af5eecSJason Wang         qemu_del_net_client(nc);
850654790b6SJason Wang         return NULL;
85174af5eecSJason Wang     }
852654790b6SJason Wang     return nc;
8531e0a84eaSCindy Lu }
8541e0a84eaSCindy Lu 
8558170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
8568170ab3fSEugenio Pérez {
8578170ab3fSEugenio Pérez     int ret = ioctl(fd, VHOST_GET_FEATURES, features);
8588170ab3fSEugenio Pérez     if (unlikely(ret < 0)) {
8598170ab3fSEugenio Pérez         error_setg_errno(errp, errno,
8608170ab3fSEugenio Pérez                          "Fail to query features from vhost-vDPA device");
8618170ab3fSEugenio Pérez     }
8628170ab3fSEugenio Pérez     return ret;
8638170ab3fSEugenio Pérez }
8648170ab3fSEugenio Pérez 
8658170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
8668170ab3fSEugenio Pérez                                           int *has_cvq, Error **errp)
86740237840SJason Wang {
86840237840SJason Wang     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
869cd523a41SStefano Garzarella     g_autofree struct vhost_vdpa_config *config = NULL;
87040237840SJason Wang     __virtio16 *max_queue_pairs;
87140237840SJason Wang     int ret;
87240237840SJason Wang 
87340237840SJason Wang     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
87440237840SJason Wang         *has_cvq = 1;
87540237840SJason Wang     } else {
87640237840SJason Wang         *has_cvq = 0;
87740237840SJason Wang     }
87840237840SJason Wang 
87940237840SJason Wang     if (features & (1 << VIRTIO_NET_F_MQ)) {
88040237840SJason Wang         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
88140237840SJason Wang         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
88240237840SJason Wang         config->len = sizeof(*max_queue_pairs);
88340237840SJason Wang 
88440237840SJason Wang         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
88540237840SJason Wang         if (ret) {
88640237840SJason Wang             error_setg(errp, "Fail to get config from vhost-vDPA device");
88740237840SJason Wang             return -ret;
88840237840SJason Wang         }
88940237840SJason Wang 
89040237840SJason Wang         max_queue_pairs = (__virtio16 *)&config->buf;
89140237840SJason Wang 
89240237840SJason Wang         return lduw_le_p(max_queue_pairs);
89340237840SJason Wang     }
89440237840SJason Wang 
89540237840SJason Wang     return 1;
89640237840SJason Wang }
89740237840SJason Wang 
8981e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
8991e0a84eaSCindy Lu                         NetClientState *peer, Error **errp)
9001e0a84eaSCindy Lu {
9011e0a84eaSCindy Lu     const NetdevVhostVDPAOptions *opts;
9028170ab3fSEugenio Pérez     uint64_t features;
903654790b6SJason Wang     int vdpa_device_fd;
904eb3cb751SEugenio Pérez     g_autofree NetClientState **ncs = NULL;
905a585fad2SEugenio Pérez     struct vhost_vdpa_iova_range iova_range;
906eb3cb751SEugenio Pérez     NetClientState *nc;
907aed5da45SEugenio Pérez     int queue_pairs, r, i = 0, has_cvq = 0;
9081e0a84eaSCindy Lu 
9091e0a84eaSCindy Lu     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
9101e0a84eaSCindy Lu     opts = &netdev->u.vhost_vdpa;
9117480874aSMarkus Armbruster     if (!opts->vhostdev && !opts->vhostfd) {
9128801ccd0SSi-Wei Liu         error_setg(errp,
9138801ccd0SSi-Wei Liu                    "vhost-vdpa: neither vhostdev= nor vhostfd= was specified");
914c8295404SEugenio Pérez         return -1;
915c8295404SEugenio Pérez     }
9167327813dSJason Wang 
9177480874aSMarkus Armbruster     if (opts->vhostdev && opts->vhostfd) {
9188801ccd0SSi-Wei Liu         error_setg(errp,
9198801ccd0SSi-Wei Liu                    "vhost-vdpa: vhostdev= and vhostfd= are mutually exclusive");
9208801ccd0SSi-Wei Liu         return -1;
9218801ccd0SSi-Wei Liu     }
9228801ccd0SSi-Wei Liu 
9237480874aSMarkus Armbruster     if (opts->vhostdev) {
9240351152bSEugenio Pérez         vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
9257327813dSJason Wang         if (vdpa_device_fd == -1) {
9267327813dSJason Wang             return -errno;
9277327813dSJason Wang         }
9285107fd3eSPeter Maydell     } else {
9295107fd3eSPeter Maydell         /* has_vhostfd */
9308801ccd0SSi-Wei Liu         vdpa_device_fd = monitor_fd_param(monitor_cur(), opts->vhostfd, errp);
9318801ccd0SSi-Wei Liu         if (vdpa_device_fd == -1) {
9328801ccd0SSi-Wei Liu             error_prepend(errp, "vhost-vdpa: unable to parse vhostfd: ");
9338801ccd0SSi-Wei Liu             return -1;
9348801ccd0SSi-Wei Liu         }
9358801ccd0SSi-Wei Liu     }
9367327813dSJason Wang 
9378170ab3fSEugenio Pérez     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
9388170ab3fSEugenio Pérez     if (unlikely(r < 0)) {
939aed5da45SEugenio Pérez         goto err;
9408170ab3fSEugenio Pérez     }
9418170ab3fSEugenio Pérez 
9428170ab3fSEugenio Pérez     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
94340237840SJason Wang                                                  &has_cvq, errp);
94440237840SJason Wang     if (queue_pairs < 0) {
9457327813dSJason Wang         qemu_close(vdpa_device_fd);
94640237840SJason Wang         return queue_pairs;
9477327813dSJason Wang     }
9487327813dSJason Wang 
949bf7a2ad8SLongpeng     r = vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
950bf7a2ad8SLongpeng     if (unlikely(r < 0)) {
951bf7a2ad8SLongpeng         error_setg(errp, "vhost-vdpa: get iova range failed: %s",
952bf7a2ad8SLongpeng                    strerror(-r));
953bf7a2ad8SLongpeng         goto err;
954bf7a2ad8SLongpeng     }
955bf7a2ad8SLongpeng 
95600ef422eSEugenio Pérez     if (opts->x_svq && !vhost_vdpa_net_valid_svq_features(features, errp)) {
95700ef422eSEugenio Pérez         goto err;
9581576dbb5SEugenio Pérez     }
9591576dbb5SEugenio Pérez 
96040237840SJason Wang     ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
96140237840SJason Wang 
96240237840SJason Wang     for (i = 0; i < queue_pairs; i++) {
96340237840SJason Wang         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
9641576dbb5SEugenio Pérez                                      vdpa_device_fd, i, 2, true, opts->x_svq,
9655c1ebd4cSEugenio Pérez                                      iova_range, features);
96640237840SJason Wang         if (!ncs[i])
96740237840SJason Wang             goto err;
96840237840SJason Wang     }
96940237840SJason Wang 
97040237840SJason Wang     if (has_cvq) {
97140237840SJason Wang         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
9721576dbb5SEugenio Pérez                                  vdpa_device_fd, i, 1, false,
9735c1ebd4cSEugenio Pérez                                  opts->x_svq, iova_range, features);
97440237840SJason Wang         if (!nc)
97540237840SJason Wang             goto err;
97640237840SJason Wang     }
97740237840SJason Wang 
978654790b6SJason Wang     return 0;
97940237840SJason Wang 
98040237840SJason Wang err:
98140237840SJason Wang     if (i) {
9829bd05507SSi-Wei Liu         for (i--; i >= 0; i--) {
9839bd05507SSi-Wei Liu             qemu_del_net_client(ncs[i]);
9849bd05507SSi-Wei Liu         }
98540237840SJason Wang     }
9861576dbb5SEugenio Pérez 
98740237840SJason Wang     qemu_close(vdpa_device_fd);
98840237840SJason Wang 
98940237840SJason Wang     return -1;
9901e0a84eaSCindy Lu }
991