xref: /openbmc/qemu/net/vhost-vdpa.c (revision 72b99a87)
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"
291e0a84eaSCindy Lu #include "hw/virtio/vhost.h"
301e0a84eaSCindy Lu 
311e0a84eaSCindy Lu /* Todo:need to add the multiqueue support here */
321e0a84eaSCindy Lu typedef struct VhostVDPAState {
331e0a84eaSCindy Lu     NetClientState nc;
341e0a84eaSCindy Lu     struct vhost_vdpa vhost_vdpa;
351e0a84eaSCindy Lu     VHostNetState *vhost_net;
362df4dd31SEugenio Pérez 
372df4dd31SEugenio Pérez     /* Control commands shadow buffers */
3817fb889fSEugenio Pérez     void *cvq_cmd_out_buffer;
3917fb889fSEugenio Pérez     virtio_net_ctrl_ack *status;
4017fb889fSEugenio Pérez 
411e0a84eaSCindy Lu     bool started;
421e0a84eaSCindy Lu } VhostVDPAState;
431e0a84eaSCindy Lu 
441e0a84eaSCindy Lu const int vdpa_feature_bits[] = {
451e0a84eaSCindy Lu     VIRTIO_F_NOTIFY_ON_EMPTY,
461e0a84eaSCindy Lu     VIRTIO_RING_F_INDIRECT_DESC,
471e0a84eaSCindy Lu     VIRTIO_RING_F_EVENT_IDX,
481e0a84eaSCindy Lu     VIRTIO_F_ANY_LAYOUT,
491e0a84eaSCindy Lu     VIRTIO_F_VERSION_1,
501e0a84eaSCindy Lu     VIRTIO_NET_F_CSUM,
511e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_CSUM,
521e0a84eaSCindy Lu     VIRTIO_NET_F_GSO,
531e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO4,
541e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_TSO6,
551e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_ECN,
561e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_UFO,
571e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO4,
581e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_TSO6,
591e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_ECN,
601e0a84eaSCindy Lu     VIRTIO_NET_F_HOST_UFO,
611e0a84eaSCindy Lu     VIRTIO_NET_F_MRG_RXBUF,
621e0a84eaSCindy Lu     VIRTIO_NET_F_MTU,
6340237840SJason Wang     VIRTIO_NET_F_CTRL_RX,
6440237840SJason Wang     VIRTIO_NET_F_CTRL_RX_EXTRA,
6540237840SJason Wang     VIRTIO_NET_F_CTRL_VLAN,
6640237840SJason Wang     VIRTIO_NET_F_GUEST_ANNOUNCE,
6740237840SJason Wang     VIRTIO_NET_F_CTRL_MAC_ADDR,
6840237840SJason Wang     VIRTIO_NET_F_RSS,
6940237840SJason Wang     VIRTIO_NET_F_MQ,
7040237840SJason Wang     VIRTIO_NET_F_CTRL_VQ,
711e0a84eaSCindy Lu     VIRTIO_F_IOMMU_PLATFORM,
721e0a84eaSCindy Lu     VIRTIO_F_RING_PACKED,
730145c393SAndrew Melnychenko     VIRTIO_NET_F_RSS,
740145c393SAndrew Melnychenko     VIRTIO_NET_F_HASH_REPORT,
751e0a84eaSCindy Lu     VIRTIO_NET_F_GUEST_ANNOUNCE,
769aa47eddSSi-Wei Liu     VIRTIO_NET_F_STATUS,
771e0a84eaSCindy Lu     VHOST_INVALID_FEATURE_BIT
781e0a84eaSCindy Lu };
791e0a84eaSCindy Lu 
801576dbb5SEugenio Pérez /** Supported device specific feature bits with SVQ */
811576dbb5SEugenio Pérez static const uint64_t vdpa_svq_device_features =
821576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CSUM) |
831576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) |
841576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MTU) |
851576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MAC) |
861576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) |
871576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) |
881576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_ECN) |
891576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_GUEST_UFO) |
901576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO4) |
911576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_TSO6) |
921576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_ECN) |
931576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_HOST_UFO) |
941576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) |
951576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STATUS) |
961576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) |
97*72b99a87SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_MQ) |
981576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_F_ANY_LAYOUT) |
991576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) |
1001576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_RSC_EXT) |
1011576dbb5SEugenio Pérez     BIT_ULL(VIRTIO_NET_F_STANDBY);
1021576dbb5SEugenio Pérez 
1031e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc)
1041e0a84eaSCindy Lu {
1051e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1061e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1071e0a84eaSCindy Lu     return s->vhost_net;
1081e0a84eaSCindy Lu }
1091e0a84eaSCindy Lu 
1101e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net)
1111e0a84eaSCindy Lu {
1121e0a84eaSCindy Lu     uint32_t device_id;
1131e0a84eaSCindy Lu     int ret;
1141e0a84eaSCindy Lu     struct vhost_dev *hdev;
1151e0a84eaSCindy Lu 
1161e0a84eaSCindy Lu     hdev = (struct vhost_dev *)&net->dev;
1171e0a84eaSCindy Lu     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id);
1181e0a84eaSCindy Lu     if (device_id != VIRTIO_ID_NET) {
1191e0a84eaSCindy Lu         return -ENOTSUP;
1201e0a84eaSCindy Lu     }
1211e0a84eaSCindy Lu     return ret;
1221e0a84eaSCindy Lu }
1231e0a84eaSCindy Lu 
12440237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be,
12540237840SJason Wang                           int queue_pair_index, int nvqs)
1261e0a84eaSCindy Lu {
1271e0a84eaSCindy Lu     VhostNetOptions options;
1281e0a84eaSCindy Lu     struct vhost_net *net = NULL;
1291e0a84eaSCindy Lu     VhostVDPAState *s;
1301e0a84eaSCindy Lu     int ret;
1311e0a84eaSCindy Lu 
1321e0a84eaSCindy Lu     options.backend_type = VHOST_BACKEND_TYPE_VDPA;
1331e0a84eaSCindy Lu     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1341e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, ncs);
1351e0a84eaSCindy Lu     options.net_backend = ncs;
1361e0a84eaSCindy Lu     options.opaque      = be;
1371e0a84eaSCindy Lu     options.busyloop_timeout = 0;
13840237840SJason Wang     options.nvqs = nvqs;
1391e0a84eaSCindy Lu 
1401e0a84eaSCindy Lu     net = vhost_net_init(&options);
1411e0a84eaSCindy Lu     if (!net) {
1421e0a84eaSCindy Lu         error_report("failed to init vhost_net for queue");
143a97ef87aSJason Wang         goto err_init;
1441e0a84eaSCindy Lu     }
1451e0a84eaSCindy Lu     s->vhost_net = net;
1461e0a84eaSCindy Lu     ret = vhost_vdpa_net_check_device_id(net);
1471e0a84eaSCindy Lu     if (ret) {
148a97ef87aSJason Wang         goto err_check;
1491e0a84eaSCindy Lu     }
1501e0a84eaSCindy Lu     return 0;
151a97ef87aSJason Wang err_check:
1521e0a84eaSCindy Lu     vhost_net_cleanup(net);
153ab36edcfSJason Wang     g_free(net);
154a97ef87aSJason Wang err_init:
1551e0a84eaSCindy Lu     return -1;
1561e0a84eaSCindy Lu }
1571e0a84eaSCindy Lu 
1581e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc)
1591e0a84eaSCindy Lu {
1601e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1611576dbb5SEugenio Pérez     struct vhost_dev *dev = &s->vhost_net->dev;
1621e0a84eaSCindy Lu 
1632df4dd31SEugenio Pérez     qemu_vfree(s->cvq_cmd_out_buffer);
16417fb889fSEugenio Pérez     qemu_vfree(s->status);
1651576dbb5SEugenio Pérez     if (dev->vq_index + dev->nvqs == dev->vq_index_end) {
1661576dbb5SEugenio Pérez         g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete);
1671576dbb5SEugenio Pérez     }
1681e0a84eaSCindy Lu     if (s->vhost_net) {
1691e0a84eaSCindy Lu         vhost_net_cleanup(s->vhost_net);
1701e0a84eaSCindy Lu         g_free(s->vhost_net);
1711e0a84eaSCindy Lu         s->vhost_net = NULL;
1721e0a84eaSCindy Lu     }
17357b3a7d8SCindy Lu      if (s->vhost_vdpa.device_fd >= 0) {
17457b3a7d8SCindy Lu         qemu_close(s->vhost_vdpa.device_fd);
17557b3a7d8SCindy Lu         s->vhost_vdpa.device_fd = -1;
17657b3a7d8SCindy Lu     }
1771e0a84eaSCindy Lu }
1781e0a84eaSCindy Lu 
1791e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc)
1801e0a84eaSCindy Lu {
1811e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1821e0a84eaSCindy Lu 
1831e0a84eaSCindy Lu     return true;
1841e0a84eaSCindy Lu }
1851e0a84eaSCindy Lu 
1861e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc)
1871e0a84eaSCindy Lu {
1881e0a84eaSCindy Lu     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
1891e0a84eaSCindy Lu     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
1901e0a84eaSCindy Lu     uint64_t features = 0;
1911e0a84eaSCindy Lu     features |= (1ULL << VIRTIO_NET_F_HOST_UFO);
1921e0a84eaSCindy Lu     features = vhost_net_get_features(s->vhost_net, features);
1931e0a84eaSCindy Lu     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO));
1941e0a84eaSCindy Lu 
1951e0a84eaSCindy Lu }
1961e0a84eaSCindy Lu 
197ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc,
198ee8a1c63SKevin Wolf                                        Error **errp)
199ee8a1c63SKevin Wolf {
200ee8a1c63SKevin Wolf     const char *driver = object_class_get_name(oc);
201ee8a1c63SKevin Wolf 
202ee8a1c63SKevin Wolf     if (!g_str_has_prefix(driver, "virtio-net-")) {
203ee8a1c63SKevin Wolf         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*");
204ee8a1c63SKevin Wolf         return false;
205ee8a1c63SKevin Wolf     }
206ee8a1c63SKevin Wolf 
207ee8a1c63SKevin Wolf     return true;
208ee8a1c63SKevin Wolf }
209ee8a1c63SKevin Wolf 
210846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */
211846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf,
212846a1e85SEugenio Pérez                                   size_t size)
213846a1e85SEugenio Pérez {
214846a1e85SEugenio Pérez     return 0;
215846a1e85SEugenio Pérez }
216846a1e85SEugenio Pérez 
2171e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = {
2181e0a84eaSCindy Lu         .type = NET_CLIENT_DRIVER_VHOST_VDPA,
2191e0a84eaSCindy Lu         .size = sizeof(VhostVDPAState),
220846a1e85SEugenio Pérez         .receive = vhost_vdpa_receive,
2211e0a84eaSCindy Lu         .cleanup = vhost_vdpa_cleanup,
2221e0a84eaSCindy Lu         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
2231e0a84eaSCindy Lu         .has_ufo = vhost_vdpa_has_ufo,
224ee8a1c63SKevin Wolf         .check_peer_type = vhost_vdpa_check_peer_type,
2251e0a84eaSCindy Lu };
2261e0a84eaSCindy Lu 
2272df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr)
2282df4dd31SEugenio Pérez {
2292df4dd31SEugenio Pérez     VhostIOVATree *tree = v->iova_tree;
2302df4dd31SEugenio Pérez     DMAMap needle = {
2312df4dd31SEugenio Pérez         /*
2322df4dd31SEugenio Pérez          * No need to specify size or to look for more translations since
2332df4dd31SEugenio Pérez          * this contiguous chunk was allocated by us.
2342df4dd31SEugenio Pérez          */
2352df4dd31SEugenio Pérez         .translated_addr = (hwaddr)(uintptr_t)addr,
2362df4dd31SEugenio Pérez     };
2372df4dd31SEugenio Pérez     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle);
2382df4dd31SEugenio Pérez     int r;
2392df4dd31SEugenio Pérez 
2402df4dd31SEugenio Pérez     if (unlikely(!map)) {
2412df4dd31SEugenio Pérez         error_report("Cannot locate expected map");
2422df4dd31SEugenio Pérez         return;
2432df4dd31SEugenio Pérez     }
2442df4dd31SEugenio Pérez 
2452df4dd31SEugenio Pérez     r = vhost_vdpa_dma_unmap(v, map->iova, map->size + 1);
2462df4dd31SEugenio Pérez     if (unlikely(r != 0)) {
2472df4dd31SEugenio Pérez         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r);
2482df4dd31SEugenio Pérez     }
2492df4dd31SEugenio Pérez 
25069292a8eSEugenio Pérez     vhost_iova_tree_remove(tree, *map);
2512df4dd31SEugenio Pérez }
2522df4dd31SEugenio Pérez 
2532df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void)
2542df4dd31SEugenio Pérez {
2552df4dd31SEugenio Pérez     /*
2562df4dd31SEugenio Pérez      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer.
2572df4dd31SEugenio Pérez      * In buffer is always 1 byte, so it should fit here
2582df4dd31SEugenio Pérez      */
2592df4dd31SEugenio Pérez     return sizeof(struct virtio_net_ctrl_hdr) +
2602df4dd31SEugenio Pérez            2 * sizeof(struct virtio_net_ctrl_mac) +
2612df4dd31SEugenio Pérez            MAC_TABLE_ENTRIES * ETH_ALEN;
2622df4dd31SEugenio Pérez }
2632df4dd31SEugenio Pérez 
2642df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void)
2652df4dd31SEugenio Pérez {
2662df4dd31SEugenio Pérez     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size());
2672df4dd31SEugenio Pérez }
2682df4dd31SEugenio Pérez 
2697a7f87e9SEugenio Pérez /** Map CVQ buffer. */
2707a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size,
2717a7f87e9SEugenio Pérez                                   bool write)
2722df4dd31SEugenio Pérez {
2732df4dd31SEugenio Pérez     DMAMap map = {};
2742df4dd31SEugenio Pérez     int r;
2752df4dd31SEugenio Pérez 
2762df4dd31SEugenio Pérez     map.translated_addr = (hwaddr)(uintptr_t)buf;
2777a7f87e9SEugenio Pérez     map.size = size - 1;
2782df4dd31SEugenio Pérez     map.perm = write ? IOMMU_RW : IOMMU_RO,
2792df4dd31SEugenio Pérez     r = vhost_iova_tree_map_alloc(v->iova_tree, &map);
2802df4dd31SEugenio Pérez     if (unlikely(r != IOVA_OK)) {
2812df4dd31SEugenio Pérez         error_report("Cannot map injected element");
2827a7f87e9SEugenio Pérez         return r;
2832df4dd31SEugenio Pérez     }
2842df4dd31SEugenio Pérez 
2852df4dd31SEugenio Pérez     r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf,
2862df4dd31SEugenio Pérez                            !write);
2872df4dd31SEugenio Pérez     if (unlikely(r < 0)) {
2882df4dd31SEugenio Pérez         goto dma_map_err;
2892df4dd31SEugenio Pérez     }
2902df4dd31SEugenio Pérez 
2917a7f87e9SEugenio Pérez     return 0;
2922df4dd31SEugenio Pérez 
2932df4dd31SEugenio Pérez dma_map_err:
29469292a8eSEugenio Pérez     vhost_iova_tree_remove(v->iova_tree, map);
2957a7f87e9SEugenio Pérez     return r;
2962df4dd31SEugenio Pérez }
2972df4dd31SEugenio Pérez 
2987a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc)
2992df4dd31SEugenio Pérez {
3007a7f87e9SEugenio Pérez     VhostVDPAState *s;
3017a7f87e9SEugenio Pérez     int r;
3022df4dd31SEugenio Pérez 
3037a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
3047a7f87e9SEugenio Pérez 
3057a7f87e9SEugenio Pérez     s = DO_UPCAST(VhostVDPAState, nc, nc);
3067a7f87e9SEugenio Pérez     if (!s->vhost_vdpa.shadow_vqs_enabled) {
3077a7f87e9SEugenio Pérez         return 0;
3082df4dd31SEugenio Pérez     }
3092df4dd31SEugenio Pérez 
3107a7f87e9SEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer,
3117a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), false);
3127a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
3137a7f87e9SEugenio Pérez         return r;
3147a7f87e9SEugenio Pérez     }
3157a7f87e9SEugenio Pérez 
31617fb889fSEugenio Pérez     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status,
3177a7f87e9SEugenio Pérez                                vhost_vdpa_net_cvq_cmd_page_len(), true);
3187a7f87e9SEugenio Pérez     if (unlikely(r < 0)) {
3192df4dd31SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
3202df4dd31SEugenio Pérez     }
3212df4dd31SEugenio Pérez 
3227a7f87e9SEugenio Pérez     return r;
3237a7f87e9SEugenio Pérez }
3247a7f87e9SEugenio Pérez 
3257a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc)
3267a7f87e9SEugenio Pérez {
3277a7f87e9SEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
3287a7f87e9SEugenio Pérez 
3297a7f87e9SEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
3307a7f87e9SEugenio Pérez 
3317a7f87e9SEugenio Pérez     if (s->vhost_vdpa.shadow_vqs_enabled) {
3327a7f87e9SEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer);
33317fb889fSEugenio Pérez         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status);
3347a7f87e9SEugenio Pérez     }
3352df4dd31SEugenio Pérez }
3362df4dd31SEugenio Pérez 
337be4278b6SEugenio Pérez static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len,
338be4278b6SEugenio Pérez                                       size_t in_len)
339be4278b6SEugenio Pérez {
340be4278b6SEugenio Pérez     /* Buffers for the device */
341be4278b6SEugenio Pérez     const struct iovec out = {
342be4278b6SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
343be4278b6SEugenio Pérez         .iov_len = out_len,
344be4278b6SEugenio Pérez     };
345be4278b6SEugenio Pérez     const struct iovec in = {
34617fb889fSEugenio Pérez         .iov_base = s->status,
347be4278b6SEugenio Pérez         .iov_len = sizeof(virtio_net_ctrl_ack),
348be4278b6SEugenio Pérez     };
349be4278b6SEugenio Pérez     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0);
350be4278b6SEugenio Pérez     int r;
351be4278b6SEugenio Pérez 
352be4278b6SEugenio Pérez     r = vhost_svq_add(svq, &out, 1, &in, 1, NULL);
353be4278b6SEugenio Pérez     if (unlikely(r != 0)) {
354be4278b6SEugenio Pérez         if (unlikely(r == -ENOSPC)) {
355be4278b6SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n",
356be4278b6SEugenio Pérez                           __func__);
357be4278b6SEugenio Pérez         }
358be4278b6SEugenio Pérez         return r;
359be4278b6SEugenio Pérez     }
360be4278b6SEugenio Pérez 
361be4278b6SEugenio Pérez     /*
362be4278b6SEugenio Pérez      * We can poll here since we've had BQL from the time we sent the
363be4278b6SEugenio Pérez      * descriptor. Also, we need to take the answer before SVQ pulls by itself,
364be4278b6SEugenio Pérez      * when BQL is released
365be4278b6SEugenio Pérez      */
366be4278b6SEugenio Pérez     return vhost_svq_poll(svq);
367be4278b6SEugenio Pérez }
368be4278b6SEugenio Pérez 
369f73c0c43SEugenio Pérez static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class,
370f73c0c43SEugenio Pérez                                        uint8_t cmd, const void *data,
371f73c0c43SEugenio Pérez                                        size_t data_size)
372f73c0c43SEugenio Pérez {
373f73c0c43SEugenio Pérez     const struct virtio_net_ctrl_hdr ctrl = {
374f73c0c43SEugenio Pérez         .class = class,
375f73c0c43SEugenio Pérez         .cmd = cmd,
376f73c0c43SEugenio Pérez     };
377f73c0c43SEugenio Pérez 
378f73c0c43SEugenio Pérez     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl));
379f73c0c43SEugenio Pérez 
380f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl));
381f73c0c43SEugenio Pérez     memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size);
382f73c0c43SEugenio Pérez 
383f73c0c43SEugenio Pérez     return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size,
384f73c0c43SEugenio Pérez                                   sizeof(virtio_net_ctrl_ack));
385f73c0c43SEugenio Pérez }
386f73c0c43SEugenio Pérez 
387f73c0c43SEugenio Pérez static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n)
388f73c0c43SEugenio Pérez {
389f73c0c43SEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
390f73c0c43SEugenio Pérez     if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) {
391f73c0c43SEugenio Pérez         ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC,
392f73c0c43SEugenio Pérez                                                   VIRTIO_NET_CTRL_MAC_ADDR_SET,
393f73c0c43SEugenio Pérez                                                   n->mac, sizeof(n->mac));
394f73c0c43SEugenio Pérez         if (unlikely(dev_written < 0)) {
395f73c0c43SEugenio Pérez             return dev_written;
396f73c0c43SEugenio Pérez         }
397f73c0c43SEugenio Pérez 
398f73c0c43SEugenio Pérez         return *s->status != VIRTIO_NET_OK;
399f73c0c43SEugenio Pérez     }
400f73c0c43SEugenio Pérez 
401f73c0c43SEugenio Pérez     return 0;
402f73c0c43SEugenio Pérez }
403f73c0c43SEugenio Pérez 
404f64c7cdaSEugenio Pérez static int vhost_vdpa_net_load_mq(VhostVDPAState *s,
405f64c7cdaSEugenio Pérez                                   const VirtIONet *n)
406f64c7cdaSEugenio Pérez {
407f64c7cdaSEugenio Pérez     struct virtio_net_ctrl_mq mq;
408f64c7cdaSEugenio Pérez     uint64_t features = n->parent_obj.guest_features;
409f64c7cdaSEugenio Pérez     ssize_t dev_written;
410f64c7cdaSEugenio Pérez 
411f64c7cdaSEugenio Pérez     if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) {
412f64c7cdaSEugenio Pérez         return 0;
413f64c7cdaSEugenio Pérez     }
414f64c7cdaSEugenio Pérez 
415f64c7cdaSEugenio Pérez     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs);
416f64c7cdaSEugenio Pérez     dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ,
417f64c7cdaSEugenio Pérez                                           VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq,
418f64c7cdaSEugenio Pérez                                           sizeof(mq));
419f64c7cdaSEugenio Pérez     if (unlikely(dev_written < 0)) {
420f64c7cdaSEugenio Pérez         return dev_written;
421f64c7cdaSEugenio Pérez     }
422f64c7cdaSEugenio Pérez 
423f64c7cdaSEugenio Pérez     return *s->status != VIRTIO_NET_OK;
424f64c7cdaSEugenio Pérez }
425f64c7cdaSEugenio Pérez 
426dd036d8dSEugenio Pérez static int vhost_vdpa_net_load(NetClientState *nc)
427dd036d8dSEugenio Pérez {
428dd036d8dSEugenio Pérez     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc);
429f73c0c43SEugenio Pérez     struct vhost_vdpa *v = &s->vhost_vdpa;
430dd036d8dSEugenio Pérez     const VirtIONet *n;
431f73c0c43SEugenio Pérez     int r;
432dd036d8dSEugenio Pérez 
433dd036d8dSEugenio Pérez     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA);
434dd036d8dSEugenio Pérez 
435dd036d8dSEugenio Pérez     if (!v->shadow_vqs_enabled) {
436dd036d8dSEugenio Pérez         return 0;
437dd036d8dSEugenio Pérez     }
438dd036d8dSEugenio Pérez 
439dd036d8dSEugenio Pérez     n = VIRTIO_NET(v->dev->vdev);
440f73c0c43SEugenio Pérez     r = vhost_vdpa_net_load_mac(s, n);
441f73c0c43SEugenio Pérez     if (unlikely(r < 0)) {
442f73c0c43SEugenio Pérez         return r;
443dd036d8dSEugenio Pérez     }
444f64c7cdaSEugenio Pérez     r = vhost_vdpa_net_load_mq(s, n);
445f64c7cdaSEugenio Pérez     if (unlikely(r)) {
446f64c7cdaSEugenio Pérez         return r;
447f64c7cdaSEugenio Pérez     }
448dd036d8dSEugenio Pérez 
449dd036d8dSEugenio Pérez     return 0;
450dd036d8dSEugenio Pérez }
451dd036d8dSEugenio Pérez 
452f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = {
453f8972b56SEugenio Pérez     .type = NET_CLIENT_DRIVER_VHOST_VDPA,
454f8972b56SEugenio Pérez     .size = sizeof(VhostVDPAState),
455f8972b56SEugenio Pérez     .receive = vhost_vdpa_receive,
4567a7f87e9SEugenio Pérez     .start = vhost_vdpa_net_cvq_start,
457dd036d8dSEugenio Pérez     .load = vhost_vdpa_net_load,
4587a7f87e9SEugenio Pérez     .stop = vhost_vdpa_net_cvq_stop,
459f8972b56SEugenio Pérez     .cleanup = vhost_vdpa_cleanup,
460f8972b56SEugenio Pérez     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr,
461f8972b56SEugenio Pérez     .has_ufo = vhost_vdpa_has_ufo,
462f8972b56SEugenio Pérez     .check_peer_type = vhost_vdpa_check_peer_type,
463f8972b56SEugenio Pérez };
464f8972b56SEugenio Pérez 
4652df4dd31SEugenio Pérez /**
4662df4dd31SEugenio Pérez  * Do not forward commands not supported by SVQ. Otherwise, the device could
4672df4dd31SEugenio Pérez  * accept it and qemu would not know how to update the device model.
4682df4dd31SEugenio Pérez  */
4697a7f87e9SEugenio Pérez static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len)
4702df4dd31SEugenio Pérez {
4712df4dd31SEugenio Pérez     struct virtio_net_ctrl_hdr ctrl;
4722df4dd31SEugenio Pérez 
4737a7f87e9SEugenio Pérez     if (unlikely(len < sizeof(ctrl))) {
4742df4dd31SEugenio Pérez         qemu_log_mask(LOG_GUEST_ERROR,
4757a7f87e9SEugenio Pérez                       "%s: invalid legnth of out buffer %zu\n", __func__, len);
4762df4dd31SEugenio Pérez         return false;
4772df4dd31SEugenio Pérez     }
4782df4dd31SEugenio Pérez 
4797a7f87e9SEugenio Pérez     memcpy(&ctrl, out_buf, sizeof(ctrl));
4802df4dd31SEugenio Pérez     switch (ctrl.class) {
4812df4dd31SEugenio Pérez     case VIRTIO_NET_CTRL_MAC:
4822df4dd31SEugenio Pérez         switch (ctrl.cmd) {
4832df4dd31SEugenio Pérez         case VIRTIO_NET_CTRL_MAC_ADDR_SET:
4842df4dd31SEugenio Pérez             return true;
4852df4dd31SEugenio Pérez         default:
4862df4dd31SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mac cmd %u\n",
4872df4dd31SEugenio Pérez                           __func__, ctrl.cmd);
4882df4dd31SEugenio Pérez         };
4892df4dd31SEugenio Pérez         break;
490275ba561SEugenio Pérez     case VIRTIO_NET_CTRL_MQ:
491275ba561SEugenio Pérez         switch (ctrl.cmd) {
492275ba561SEugenio Pérez         case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET:
493275ba561SEugenio Pérez             return true;
494275ba561SEugenio Pérez         default:
495275ba561SEugenio Pérez             qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mq cmd %u\n",
496275ba561SEugenio Pérez                           __func__, ctrl.cmd);
497275ba561SEugenio Pérez         };
498275ba561SEugenio Pérez         break;
4992df4dd31SEugenio Pérez     default:
5002df4dd31SEugenio Pérez         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid control class %u\n",
5012df4dd31SEugenio Pérez                       __func__, ctrl.class);
5022df4dd31SEugenio Pérez     };
5032df4dd31SEugenio Pérez 
5042df4dd31SEugenio Pérez     return false;
5052df4dd31SEugenio Pérez }
5062df4dd31SEugenio Pérez 
5072df4dd31SEugenio Pérez /**
5082df4dd31SEugenio Pérez  * Validate and copy control virtqueue commands.
5092df4dd31SEugenio Pérez  *
5102df4dd31SEugenio Pérez  * Following QEMU guidelines, we offer a copy of the buffers to the device to
5112df4dd31SEugenio Pérez  * prevent TOCTOU bugs.
512bd907ae4SEugenio Pérez  */
513bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq,
514bd907ae4SEugenio Pérez                                             VirtQueueElement *elem,
515bd907ae4SEugenio Pérez                                             void *opaque)
516bd907ae4SEugenio Pérez {
5172df4dd31SEugenio Pérez     VhostVDPAState *s = opaque;
518be4278b6SEugenio Pérez     size_t in_len;
519bd907ae4SEugenio Pérez     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
5207a7f87e9SEugenio Pérez     /* Out buffer sent to both the vdpa device and the device model */
5217a7f87e9SEugenio Pérez     struct iovec out = {
5227a7f87e9SEugenio Pérez         .iov_base = s->cvq_cmd_out_buffer,
5237a7f87e9SEugenio Pérez     };
5242df4dd31SEugenio Pérez     /* in buffer used for device model */
5252df4dd31SEugenio Pérez     const struct iovec in = {
5262df4dd31SEugenio Pérez         .iov_base = &status,
5272df4dd31SEugenio Pérez         .iov_len = sizeof(status),
5282df4dd31SEugenio Pérez     };
529be4278b6SEugenio Pérez     ssize_t dev_written = -EINVAL;
5302df4dd31SEugenio Pérez     bool ok;
531bd907ae4SEugenio Pérez 
5327a7f87e9SEugenio Pérez     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0,
5337a7f87e9SEugenio Pérez                              s->cvq_cmd_out_buffer,
5347a7f87e9SEugenio Pérez                              vhost_vdpa_net_cvq_cmd_len());
5357a7f87e9SEugenio Pérez     ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len);
5362df4dd31SEugenio Pérez     if (unlikely(!ok)) {
5372df4dd31SEugenio Pérez         goto out;
5382df4dd31SEugenio Pérez     }
539bd907ae4SEugenio Pérez 
540be4278b6SEugenio Pérez     dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status));
541be4278b6SEugenio Pérez     if (unlikely(dev_written < 0)) {
542bd907ae4SEugenio Pérez         goto out;
543bd907ae4SEugenio Pérez     }
544bd907ae4SEugenio Pérez 
545bd907ae4SEugenio Pérez     if (unlikely(dev_written < sizeof(status))) {
546bd907ae4SEugenio Pérez         error_report("Insufficient written data (%zu)", dev_written);
5472df4dd31SEugenio Pérez         goto out;
5482df4dd31SEugenio Pérez     }
5492df4dd31SEugenio Pérez 
55017fb889fSEugenio Pérez     if (*s->status != VIRTIO_NET_OK) {
551be4278b6SEugenio Pérez         return VIRTIO_NET_ERR;
5522df4dd31SEugenio Pérez     }
5532df4dd31SEugenio Pérez 
5542df4dd31SEugenio Pérez     status = VIRTIO_NET_ERR;
5557a7f87e9SEugenio Pérez     virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1);
5562df4dd31SEugenio Pérez     if (status != VIRTIO_NET_OK) {
5572df4dd31SEugenio Pérez         error_report("Bad CVQ processing in model");
558bd907ae4SEugenio Pérez     }
559bd907ae4SEugenio Pérez 
560bd907ae4SEugenio Pérez out:
561bd907ae4SEugenio Pérez     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status,
562bd907ae4SEugenio Pérez                           sizeof(status));
563bd907ae4SEugenio Pérez     if (unlikely(in_len < sizeof(status))) {
564bd907ae4SEugenio Pérez         error_report("Bad device CVQ written length");
565bd907ae4SEugenio Pérez     }
566bd907ae4SEugenio Pérez     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status)));
567bd907ae4SEugenio Pérez     g_free(elem);
568be4278b6SEugenio Pérez     return dev_written < 0 ? dev_written : 0;
569bd907ae4SEugenio Pérez }
570bd907ae4SEugenio Pérez 
571bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = {
572bd907ae4SEugenio Pérez     .avail_handler = vhost_vdpa_net_handle_ctrl_avail,
573bd907ae4SEugenio Pérez };
574bd907ae4SEugenio Pérez 
575654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer,
576654790b6SJason Wang                                            const char *device,
577654790b6SJason Wang                                            const char *name,
57840237840SJason Wang                                            int vdpa_device_fd,
57940237840SJason Wang                                            int queue_pair_index,
58040237840SJason Wang                                            int nvqs,
5811576dbb5SEugenio Pérez                                            bool is_datapath,
5821576dbb5SEugenio Pérez                                            bool svq,
5831576dbb5SEugenio Pérez                                            VhostIOVATree *iova_tree)
5841e0a84eaSCindy Lu {
5851e0a84eaSCindy Lu     NetClientState *nc = NULL;
5861e0a84eaSCindy Lu     VhostVDPAState *s;
5871e0a84eaSCindy Lu     int ret = 0;
5881e0a84eaSCindy Lu     assert(name);
58940237840SJason Wang     if (is_datapath) {
59040237840SJason Wang         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device,
59140237840SJason Wang                                  name);
59240237840SJason Wang     } else {
593f8972b56SEugenio Pérez         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer,
59440237840SJason Wang                                          device, name);
59540237840SJason Wang     }
59656e6f594SJason Wang     snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA);
5971e0a84eaSCindy Lu     s = DO_UPCAST(VhostVDPAState, nc, nc);
5987327813dSJason Wang 
5991e0a84eaSCindy Lu     s->vhost_vdpa.device_fd = vdpa_device_fd;
60040237840SJason Wang     s->vhost_vdpa.index = queue_pair_index;
6011576dbb5SEugenio Pérez     s->vhost_vdpa.shadow_vqs_enabled = svq;
6021576dbb5SEugenio Pérez     s->vhost_vdpa.iova_tree = iova_tree;
603bd907ae4SEugenio Pérez     if (!is_datapath) {
6042df4dd31SEugenio Pérez         s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(),
6052df4dd31SEugenio Pérez                                             vhost_vdpa_net_cvq_cmd_page_len());
6062df4dd31SEugenio Pérez         memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len());
60717fb889fSEugenio Pérez         s->status = qemu_memalign(qemu_real_host_page_size(),
6082df4dd31SEugenio Pérez                                   vhost_vdpa_net_cvq_cmd_page_len());
60917fb889fSEugenio Pérez         memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len());
6102df4dd31SEugenio Pérez 
611bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops;
612bd907ae4SEugenio Pérez         s->vhost_vdpa.shadow_vq_ops_opaque = s;
613bd907ae4SEugenio Pérez     }
61440237840SJason Wang     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
61574af5eecSJason Wang     if (ret) {
61674af5eecSJason Wang         qemu_del_net_client(nc);
617654790b6SJason Wang         return NULL;
61874af5eecSJason Wang     }
619654790b6SJason Wang     return nc;
6201e0a84eaSCindy Lu }
6211e0a84eaSCindy Lu 
6221576dbb5SEugenio Pérez static int vhost_vdpa_get_iova_range(int fd,
6231576dbb5SEugenio Pérez                                      struct vhost_vdpa_iova_range *iova_range)
6241576dbb5SEugenio Pérez {
6251576dbb5SEugenio Pérez     int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range);
6261576dbb5SEugenio Pérez 
6271576dbb5SEugenio Pérez     return ret < 0 ? -errno : 0;
6281576dbb5SEugenio Pérez }
6291576dbb5SEugenio Pérez 
6308170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp)
6318170ab3fSEugenio Pérez {
6328170ab3fSEugenio Pérez     int ret = ioctl(fd, VHOST_GET_FEATURES, features);
6338170ab3fSEugenio Pérez     if (unlikely(ret < 0)) {
6348170ab3fSEugenio Pérez         error_setg_errno(errp, errno,
6358170ab3fSEugenio Pérez                          "Fail to query features from vhost-vDPA device");
6368170ab3fSEugenio Pérez     }
6378170ab3fSEugenio Pérez     return ret;
6388170ab3fSEugenio Pérez }
6398170ab3fSEugenio Pérez 
6408170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features,
6418170ab3fSEugenio Pérez                                           int *has_cvq, Error **errp)
64240237840SJason Wang {
64340237840SJason Wang     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf);
644cd523a41SStefano Garzarella     g_autofree struct vhost_vdpa_config *config = NULL;
64540237840SJason Wang     __virtio16 *max_queue_pairs;
64640237840SJason Wang     int ret;
64740237840SJason Wang 
64840237840SJason Wang     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) {
64940237840SJason Wang         *has_cvq = 1;
65040237840SJason Wang     } else {
65140237840SJason Wang         *has_cvq = 0;
65240237840SJason Wang     }
65340237840SJason Wang 
65440237840SJason Wang     if (features & (1 << VIRTIO_NET_F_MQ)) {
65540237840SJason Wang         config = g_malloc0(config_size + sizeof(*max_queue_pairs));
65640237840SJason Wang         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs);
65740237840SJason Wang         config->len = sizeof(*max_queue_pairs);
65840237840SJason Wang 
65940237840SJason Wang         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config);
66040237840SJason Wang         if (ret) {
66140237840SJason Wang             error_setg(errp, "Fail to get config from vhost-vDPA device");
66240237840SJason Wang             return -ret;
66340237840SJason Wang         }
66440237840SJason Wang 
66540237840SJason Wang         max_queue_pairs = (__virtio16 *)&config->buf;
66640237840SJason Wang 
66740237840SJason Wang         return lduw_le_p(max_queue_pairs);
66840237840SJason Wang     }
66940237840SJason Wang 
67040237840SJason Wang     return 1;
67140237840SJason Wang }
67240237840SJason Wang 
6731e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name,
6741e0a84eaSCindy Lu                         NetClientState *peer, Error **errp)
6751e0a84eaSCindy Lu {
6761e0a84eaSCindy Lu     const NetdevVhostVDPAOptions *opts;
6778170ab3fSEugenio Pérez     uint64_t features;
678654790b6SJason Wang     int vdpa_device_fd;
679eb3cb751SEugenio Pérez     g_autofree NetClientState **ncs = NULL;
6801576dbb5SEugenio Pérez     g_autoptr(VhostIOVATree) iova_tree = NULL;
681eb3cb751SEugenio Pérez     NetClientState *nc;
682aed5da45SEugenio Pérez     int queue_pairs, r, i = 0, has_cvq = 0;
6831e0a84eaSCindy Lu 
6841e0a84eaSCindy Lu     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA);
6851e0a84eaSCindy Lu     opts = &netdev->u.vhost_vdpa;
686c8295404SEugenio Pérez     if (!opts->vhostdev) {
687c8295404SEugenio Pérez         error_setg(errp, "vdpa character device not specified with vhostdev");
688c8295404SEugenio Pérez         return -1;
689c8295404SEugenio Pérez     }
6907327813dSJason Wang 
6910351152bSEugenio Pérez     vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp);
6927327813dSJason Wang     if (vdpa_device_fd == -1) {
6937327813dSJason Wang         return -errno;
6947327813dSJason Wang     }
6957327813dSJason Wang 
6968170ab3fSEugenio Pérez     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp);
6978170ab3fSEugenio Pérez     if (unlikely(r < 0)) {
698aed5da45SEugenio Pérez         goto err;
6998170ab3fSEugenio Pérez     }
7008170ab3fSEugenio Pérez 
7018170ab3fSEugenio Pérez     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features,
70240237840SJason Wang                                                  &has_cvq, errp);
70340237840SJason Wang     if (queue_pairs < 0) {
7047327813dSJason Wang         qemu_close(vdpa_device_fd);
70540237840SJason Wang         return queue_pairs;
7067327813dSJason Wang     }
7077327813dSJason Wang 
7081576dbb5SEugenio Pérez     if (opts->x_svq) {
7091576dbb5SEugenio Pérez         struct vhost_vdpa_iova_range iova_range;
7101576dbb5SEugenio Pérez 
7111576dbb5SEugenio Pérez         uint64_t invalid_dev_features =
7121576dbb5SEugenio Pérez             features & ~vdpa_svq_device_features &
7131576dbb5SEugenio Pérez             /* Transport are all accepted at this point */
7141576dbb5SEugenio Pérez             ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START,
7151576dbb5SEugenio Pérez                              VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START);
7161576dbb5SEugenio Pérez 
7171576dbb5SEugenio Pérez         if (invalid_dev_features) {
7181576dbb5SEugenio Pérez             error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64,
7191576dbb5SEugenio Pérez                        invalid_dev_features);
7201576dbb5SEugenio Pérez             goto err_svq;
7211576dbb5SEugenio Pérez         }
7221576dbb5SEugenio Pérez 
7231576dbb5SEugenio Pérez         vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range);
7241576dbb5SEugenio Pérez         iova_tree = vhost_iova_tree_new(iova_range.first, iova_range.last);
7251576dbb5SEugenio Pérez     }
7261576dbb5SEugenio Pérez 
72740237840SJason Wang     ncs = g_malloc0(sizeof(*ncs) * queue_pairs);
72840237840SJason Wang 
72940237840SJason Wang     for (i = 0; i < queue_pairs; i++) {
73040237840SJason Wang         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
7311576dbb5SEugenio Pérez                                      vdpa_device_fd, i, 2, true, opts->x_svq,
7321576dbb5SEugenio Pérez                                      iova_tree);
73340237840SJason Wang         if (!ncs[i])
73440237840SJason Wang             goto err;
73540237840SJason Wang     }
73640237840SJason Wang 
73740237840SJason Wang     if (has_cvq) {
73840237840SJason Wang         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name,
7391576dbb5SEugenio Pérez                                  vdpa_device_fd, i, 1, false,
7401576dbb5SEugenio Pérez                                  opts->x_svq, iova_tree);
74140237840SJason Wang         if (!nc)
74240237840SJason Wang             goto err;
74340237840SJason Wang     }
74440237840SJason Wang 
7451576dbb5SEugenio Pérez     /* iova_tree ownership belongs to last NetClientState */
7461576dbb5SEugenio Pérez     g_steal_pointer(&iova_tree);
747654790b6SJason Wang     return 0;
74840237840SJason Wang 
74940237840SJason Wang err:
75040237840SJason Wang     if (i) {
7519bd05507SSi-Wei Liu         for (i--; i >= 0; i--) {
7529bd05507SSi-Wei Liu             qemu_del_net_client(ncs[i]);
7539bd05507SSi-Wei Liu         }
75440237840SJason Wang     }
7551576dbb5SEugenio Pérez 
7561576dbb5SEugenio Pérez err_svq:
75740237840SJason Wang     qemu_close(vdpa_device_fd);
75840237840SJason Wang 
75940237840SJason Wang     return -1;
7601e0a84eaSCindy Lu }
761