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) | 971576dbb5SEugenio Pérez BIT_ULL(VIRTIO_F_ANY_LAYOUT) | 981576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | 991576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_RSC_EXT) | 1001576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_STANDBY); 1011576dbb5SEugenio Pérez 1021e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc) 1031e0a84eaSCindy Lu { 1041e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1051e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1061e0a84eaSCindy Lu return s->vhost_net; 1071e0a84eaSCindy Lu } 1081e0a84eaSCindy Lu 1091e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net) 1101e0a84eaSCindy Lu { 1111e0a84eaSCindy Lu uint32_t device_id; 1121e0a84eaSCindy Lu int ret; 1131e0a84eaSCindy Lu struct vhost_dev *hdev; 1141e0a84eaSCindy Lu 1151e0a84eaSCindy Lu hdev = (struct vhost_dev *)&net->dev; 1161e0a84eaSCindy Lu ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id); 1171e0a84eaSCindy Lu if (device_id != VIRTIO_ID_NET) { 1181e0a84eaSCindy Lu return -ENOTSUP; 1191e0a84eaSCindy Lu } 1201e0a84eaSCindy Lu return ret; 1211e0a84eaSCindy Lu } 1221e0a84eaSCindy Lu 12340237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be, 12440237840SJason Wang int queue_pair_index, int nvqs) 1251e0a84eaSCindy Lu { 1261e0a84eaSCindy Lu VhostNetOptions options; 1271e0a84eaSCindy Lu struct vhost_net *net = NULL; 1281e0a84eaSCindy Lu VhostVDPAState *s; 1291e0a84eaSCindy Lu int ret; 1301e0a84eaSCindy Lu 1311e0a84eaSCindy Lu options.backend_type = VHOST_BACKEND_TYPE_VDPA; 1321e0a84eaSCindy Lu assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1331e0a84eaSCindy Lu s = DO_UPCAST(VhostVDPAState, nc, ncs); 1341e0a84eaSCindy Lu options.net_backend = ncs; 1351e0a84eaSCindy Lu options.opaque = be; 1361e0a84eaSCindy Lu options.busyloop_timeout = 0; 13740237840SJason Wang options.nvqs = nvqs; 1381e0a84eaSCindy Lu 1391e0a84eaSCindy Lu net = vhost_net_init(&options); 1401e0a84eaSCindy Lu if (!net) { 1411e0a84eaSCindy Lu error_report("failed to init vhost_net for queue"); 142a97ef87aSJason Wang goto err_init; 1431e0a84eaSCindy Lu } 1441e0a84eaSCindy Lu s->vhost_net = net; 1451e0a84eaSCindy Lu ret = vhost_vdpa_net_check_device_id(net); 1461e0a84eaSCindy Lu if (ret) { 147a97ef87aSJason Wang goto err_check; 1481e0a84eaSCindy Lu } 1491e0a84eaSCindy Lu return 0; 150a97ef87aSJason Wang err_check: 1511e0a84eaSCindy Lu vhost_net_cleanup(net); 152ab36edcfSJason Wang g_free(net); 153a97ef87aSJason Wang err_init: 1541e0a84eaSCindy Lu return -1; 1551e0a84eaSCindy Lu } 1561e0a84eaSCindy Lu 1571e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc) 1581e0a84eaSCindy Lu { 1591e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1601576dbb5SEugenio Pérez struct vhost_dev *dev = &s->vhost_net->dev; 1611e0a84eaSCindy Lu 1622df4dd31SEugenio Pérez qemu_vfree(s->cvq_cmd_out_buffer); 16317fb889fSEugenio Pérez qemu_vfree(s->status); 1641576dbb5SEugenio Pérez if (dev->vq_index + dev->nvqs == dev->vq_index_end) { 1651576dbb5SEugenio Pérez g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete); 1661576dbb5SEugenio Pérez } 1671e0a84eaSCindy Lu if (s->vhost_net) { 1681e0a84eaSCindy Lu vhost_net_cleanup(s->vhost_net); 1691e0a84eaSCindy Lu g_free(s->vhost_net); 1701e0a84eaSCindy Lu s->vhost_net = NULL; 1711e0a84eaSCindy Lu } 17257b3a7d8SCindy Lu if (s->vhost_vdpa.device_fd >= 0) { 17357b3a7d8SCindy Lu qemu_close(s->vhost_vdpa.device_fd); 17457b3a7d8SCindy Lu s->vhost_vdpa.device_fd = -1; 17557b3a7d8SCindy Lu } 1761e0a84eaSCindy Lu } 1771e0a84eaSCindy Lu 1781e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc) 1791e0a84eaSCindy Lu { 1801e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1811e0a84eaSCindy Lu 1821e0a84eaSCindy Lu return true; 1831e0a84eaSCindy Lu } 1841e0a84eaSCindy Lu 1851e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc) 1861e0a84eaSCindy Lu { 1871e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1881e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1891e0a84eaSCindy Lu uint64_t features = 0; 1901e0a84eaSCindy Lu features |= (1ULL << VIRTIO_NET_F_HOST_UFO); 1911e0a84eaSCindy Lu features = vhost_net_get_features(s->vhost_net, features); 1921e0a84eaSCindy Lu return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO)); 1931e0a84eaSCindy Lu 1941e0a84eaSCindy Lu } 1951e0a84eaSCindy Lu 196ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc, 197ee8a1c63SKevin Wolf Error **errp) 198ee8a1c63SKevin Wolf { 199ee8a1c63SKevin Wolf const char *driver = object_class_get_name(oc); 200ee8a1c63SKevin Wolf 201ee8a1c63SKevin Wolf if (!g_str_has_prefix(driver, "virtio-net-")) { 202ee8a1c63SKevin Wolf error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*"); 203ee8a1c63SKevin Wolf return false; 204ee8a1c63SKevin Wolf } 205ee8a1c63SKevin Wolf 206ee8a1c63SKevin Wolf return true; 207ee8a1c63SKevin Wolf } 208ee8a1c63SKevin Wolf 209846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */ 210846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf, 211846a1e85SEugenio Pérez size_t size) 212846a1e85SEugenio Pérez { 213846a1e85SEugenio Pérez return 0; 214846a1e85SEugenio Pérez } 215846a1e85SEugenio Pérez 2161e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = { 2171e0a84eaSCindy Lu .type = NET_CLIENT_DRIVER_VHOST_VDPA, 2181e0a84eaSCindy Lu .size = sizeof(VhostVDPAState), 219846a1e85SEugenio Pérez .receive = vhost_vdpa_receive, 2201e0a84eaSCindy Lu .cleanup = vhost_vdpa_cleanup, 2211e0a84eaSCindy Lu .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, 2221e0a84eaSCindy Lu .has_ufo = vhost_vdpa_has_ufo, 223ee8a1c63SKevin Wolf .check_peer_type = vhost_vdpa_check_peer_type, 2241e0a84eaSCindy Lu }; 2251e0a84eaSCindy Lu 2262df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr) 2272df4dd31SEugenio Pérez { 2282df4dd31SEugenio Pérez VhostIOVATree *tree = v->iova_tree; 2292df4dd31SEugenio Pérez DMAMap needle = { 2302df4dd31SEugenio Pérez /* 2312df4dd31SEugenio Pérez * No need to specify size or to look for more translations since 2322df4dd31SEugenio Pérez * this contiguous chunk was allocated by us. 2332df4dd31SEugenio Pérez */ 2342df4dd31SEugenio Pérez .translated_addr = (hwaddr)(uintptr_t)addr, 2352df4dd31SEugenio Pérez }; 2362df4dd31SEugenio Pérez const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle); 2372df4dd31SEugenio Pérez int r; 2382df4dd31SEugenio Pérez 2392df4dd31SEugenio Pérez if (unlikely(!map)) { 2402df4dd31SEugenio Pérez error_report("Cannot locate expected map"); 2412df4dd31SEugenio Pérez return; 2422df4dd31SEugenio Pérez } 2432df4dd31SEugenio Pérez 2442df4dd31SEugenio Pérez r = vhost_vdpa_dma_unmap(v, map->iova, map->size + 1); 2452df4dd31SEugenio Pérez if (unlikely(r != 0)) { 2462df4dd31SEugenio Pérez error_report("Device cannot unmap: %s(%d)", g_strerror(r), r); 2472df4dd31SEugenio Pérez } 2482df4dd31SEugenio Pérez 24969292a8eSEugenio Pérez vhost_iova_tree_remove(tree, *map); 2502df4dd31SEugenio Pérez } 2512df4dd31SEugenio Pérez 2522df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void) 2532df4dd31SEugenio Pérez { 2542df4dd31SEugenio Pérez /* 2552df4dd31SEugenio Pérez * MAC_TABLE_SET is the ctrl command that produces the longer out buffer. 2562df4dd31SEugenio Pérez * In buffer is always 1 byte, so it should fit here 2572df4dd31SEugenio Pérez */ 2582df4dd31SEugenio Pérez return sizeof(struct virtio_net_ctrl_hdr) + 2592df4dd31SEugenio Pérez 2 * sizeof(struct virtio_net_ctrl_mac) + 2602df4dd31SEugenio Pérez MAC_TABLE_ENTRIES * ETH_ALEN; 2612df4dd31SEugenio Pérez } 2622df4dd31SEugenio Pérez 2632df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void) 2642df4dd31SEugenio Pérez { 2652df4dd31SEugenio Pérez return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size()); 2662df4dd31SEugenio Pérez } 2672df4dd31SEugenio Pérez 2687a7f87e9SEugenio Pérez /** Map CVQ buffer. */ 2697a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size, 2707a7f87e9SEugenio Pérez bool write) 2712df4dd31SEugenio Pérez { 2722df4dd31SEugenio Pérez DMAMap map = {}; 2732df4dd31SEugenio Pérez int r; 2742df4dd31SEugenio Pérez 2752df4dd31SEugenio Pérez map.translated_addr = (hwaddr)(uintptr_t)buf; 2767a7f87e9SEugenio Pérez map.size = size - 1; 2772df4dd31SEugenio Pérez map.perm = write ? IOMMU_RW : IOMMU_RO, 2782df4dd31SEugenio Pérez r = vhost_iova_tree_map_alloc(v->iova_tree, &map); 2792df4dd31SEugenio Pérez if (unlikely(r != IOVA_OK)) { 2802df4dd31SEugenio Pérez error_report("Cannot map injected element"); 2817a7f87e9SEugenio Pérez return r; 2822df4dd31SEugenio Pérez } 2832df4dd31SEugenio Pérez 2842df4dd31SEugenio Pérez r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf, 2852df4dd31SEugenio Pérez !write); 2862df4dd31SEugenio Pérez if (unlikely(r < 0)) { 2872df4dd31SEugenio Pérez goto dma_map_err; 2882df4dd31SEugenio Pérez } 2892df4dd31SEugenio Pérez 2907a7f87e9SEugenio Pérez return 0; 2912df4dd31SEugenio Pérez 2922df4dd31SEugenio Pérez dma_map_err: 29369292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, map); 2947a7f87e9SEugenio Pérez return r; 2952df4dd31SEugenio Pérez } 2962df4dd31SEugenio Pérez 2977a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc) 2982df4dd31SEugenio Pérez { 2997a7f87e9SEugenio Pérez VhostVDPAState *s; 3007a7f87e9SEugenio Pérez int r; 3012df4dd31SEugenio Pérez 3027a7f87e9SEugenio Pérez assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 3037a7f87e9SEugenio Pérez 3047a7f87e9SEugenio Pérez s = DO_UPCAST(VhostVDPAState, nc, nc); 3057a7f87e9SEugenio Pérez if (!s->vhost_vdpa.shadow_vqs_enabled) { 3067a7f87e9SEugenio Pérez return 0; 3072df4dd31SEugenio Pérez } 3082df4dd31SEugenio Pérez 3097a7f87e9SEugenio Pérez r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer, 3107a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len(), false); 3117a7f87e9SEugenio Pérez if (unlikely(r < 0)) { 3127a7f87e9SEugenio Pérez return r; 3137a7f87e9SEugenio Pérez } 3147a7f87e9SEugenio Pérez 31517fb889fSEugenio Pérez r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status, 3167a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len(), true); 3177a7f87e9SEugenio Pérez if (unlikely(r < 0)) { 3182df4dd31SEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); 3192df4dd31SEugenio Pérez } 3202df4dd31SEugenio Pérez 3217a7f87e9SEugenio Pérez return r; 3227a7f87e9SEugenio Pérez } 3237a7f87e9SEugenio Pérez 3247a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc) 3257a7f87e9SEugenio Pérez { 3267a7f87e9SEugenio Pérez VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 3277a7f87e9SEugenio Pérez 3287a7f87e9SEugenio Pérez assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 3297a7f87e9SEugenio Pérez 3307a7f87e9SEugenio Pérez if (s->vhost_vdpa.shadow_vqs_enabled) { 3317a7f87e9SEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); 33217fb889fSEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status); 3337a7f87e9SEugenio Pérez } 3342df4dd31SEugenio Pérez } 3352df4dd31SEugenio Pérez 336be4278b6SEugenio Pérez static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len, 337be4278b6SEugenio Pérez size_t in_len) 338be4278b6SEugenio Pérez { 339be4278b6SEugenio Pérez /* Buffers for the device */ 340be4278b6SEugenio Pérez const struct iovec out = { 341be4278b6SEugenio Pérez .iov_base = s->cvq_cmd_out_buffer, 342be4278b6SEugenio Pérez .iov_len = out_len, 343be4278b6SEugenio Pérez }; 344be4278b6SEugenio Pérez const struct iovec in = { 34517fb889fSEugenio Pérez .iov_base = s->status, 346be4278b6SEugenio Pérez .iov_len = sizeof(virtio_net_ctrl_ack), 347be4278b6SEugenio Pérez }; 348be4278b6SEugenio Pérez VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0); 349be4278b6SEugenio Pérez int r; 350be4278b6SEugenio Pérez 351be4278b6SEugenio Pérez r = vhost_svq_add(svq, &out, 1, &in, 1, NULL); 352be4278b6SEugenio Pérez if (unlikely(r != 0)) { 353be4278b6SEugenio Pérez if (unlikely(r == -ENOSPC)) { 354be4278b6SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n", 355be4278b6SEugenio Pérez __func__); 356be4278b6SEugenio Pérez } 357be4278b6SEugenio Pérez return r; 358be4278b6SEugenio Pérez } 359be4278b6SEugenio Pérez 360be4278b6SEugenio Pérez /* 361be4278b6SEugenio Pérez * We can poll here since we've had BQL from the time we sent the 362be4278b6SEugenio Pérez * descriptor. Also, we need to take the answer before SVQ pulls by itself, 363be4278b6SEugenio Pérez * when BQL is released 364be4278b6SEugenio Pérez */ 365be4278b6SEugenio Pérez return vhost_svq_poll(svq); 366be4278b6SEugenio Pérez } 367be4278b6SEugenio Pérez 368f73c0c43SEugenio Pérez static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class, 369f73c0c43SEugenio Pérez uint8_t cmd, const void *data, 370f73c0c43SEugenio Pérez size_t data_size) 371f73c0c43SEugenio Pérez { 372f73c0c43SEugenio Pérez const struct virtio_net_ctrl_hdr ctrl = { 373f73c0c43SEugenio Pérez .class = class, 374f73c0c43SEugenio Pérez .cmd = cmd, 375f73c0c43SEugenio Pérez }; 376f73c0c43SEugenio Pérez 377f73c0c43SEugenio Pérez assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl)); 378f73c0c43SEugenio Pérez 379f73c0c43SEugenio Pérez memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl)); 380f73c0c43SEugenio Pérez memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size); 381f73c0c43SEugenio Pérez 382f73c0c43SEugenio Pérez return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size, 383f73c0c43SEugenio Pérez sizeof(virtio_net_ctrl_ack)); 384f73c0c43SEugenio Pérez } 385f73c0c43SEugenio Pérez 386f73c0c43SEugenio Pérez static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n) 387f73c0c43SEugenio Pérez { 388f73c0c43SEugenio Pérez uint64_t features = n->parent_obj.guest_features; 389f73c0c43SEugenio Pérez if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) { 390f73c0c43SEugenio Pérez ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC, 391f73c0c43SEugenio Pérez VIRTIO_NET_CTRL_MAC_ADDR_SET, 392f73c0c43SEugenio Pérez n->mac, sizeof(n->mac)); 393f73c0c43SEugenio Pérez if (unlikely(dev_written < 0)) { 394f73c0c43SEugenio Pérez return dev_written; 395f73c0c43SEugenio Pérez } 396f73c0c43SEugenio Pérez 397f73c0c43SEugenio Pérez return *s->status != VIRTIO_NET_OK; 398f73c0c43SEugenio Pérez } 399f73c0c43SEugenio Pérez 400f73c0c43SEugenio Pérez return 0; 401f73c0c43SEugenio Pérez } 402f73c0c43SEugenio Pérez 403*f64c7cdaSEugenio Pérez static int vhost_vdpa_net_load_mq(VhostVDPAState *s, 404*f64c7cdaSEugenio Pérez const VirtIONet *n) 405*f64c7cdaSEugenio Pérez { 406*f64c7cdaSEugenio Pérez struct virtio_net_ctrl_mq mq; 407*f64c7cdaSEugenio Pérez uint64_t features = n->parent_obj.guest_features; 408*f64c7cdaSEugenio Pérez ssize_t dev_written; 409*f64c7cdaSEugenio Pérez 410*f64c7cdaSEugenio Pérez if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) { 411*f64c7cdaSEugenio Pérez return 0; 412*f64c7cdaSEugenio Pérez } 413*f64c7cdaSEugenio Pérez 414*f64c7cdaSEugenio Pérez mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs); 415*f64c7cdaSEugenio Pérez dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ, 416*f64c7cdaSEugenio Pérez VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq, 417*f64c7cdaSEugenio Pérez sizeof(mq)); 418*f64c7cdaSEugenio Pérez if (unlikely(dev_written < 0)) { 419*f64c7cdaSEugenio Pérez return dev_written; 420*f64c7cdaSEugenio Pérez } 421*f64c7cdaSEugenio Pérez 422*f64c7cdaSEugenio Pérez return *s->status != VIRTIO_NET_OK; 423*f64c7cdaSEugenio Pérez } 424*f64c7cdaSEugenio Pérez 425dd036d8dSEugenio Pérez static int vhost_vdpa_net_load(NetClientState *nc) 426dd036d8dSEugenio Pérez { 427dd036d8dSEugenio Pérez VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 428f73c0c43SEugenio Pérez struct vhost_vdpa *v = &s->vhost_vdpa; 429dd036d8dSEugenio Pérez const VirtIONet *n; 430f73c0c43SEugenio Pérez int r; 431dd036d8dSEugenio Pérez 432dd036d8dSEugenio Pérez assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 433dd036d8dSEugenio Pérez 434dd036d8dSEugenio Pérez if (!v->shadow_vqs_enabled) { 435dd036d8dSEugenio Pérez return 0; 436dd036d8dSEugenio Pérez } 437dd036d8dSEugenio Pérez 438dd036d8dSEugenio Pérez n = VIRTIO_NET(v->dev->vdev); 439f73c0c43SEugenio Pérez r = vhost_vdpa_net_load_mac(s, n); 440f73c0c43SEugenio Pérez if (unlikely(r < 0)) { 441f73c0c43SEugenio Pérez return r; 442dd036d8dSEugenio Pérez } 443*f64c7cdaSEugenio Pérez r = vhost_vdpa_net_load_mq(s, n); 444*f64c7cdaSEugenio Pérez if (unlikely(r)) { 445*f64c7cdaSEugenio Pérez return r; 446*f64c7cdaSEugenio Pérez } 447dd036d8dSEugenio Pérez 448dd036d8dSEugenio Pérez return 0; 449dd036d8dSEugenio Pérez } 450dd036d8dSEugenio Pérez 451f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = { 452f8972b56SEugenio Pérez .type = NET_CLIENT_DRIVER_VHOST_VDPA, 453f8972b56SEugenio Pérez .size = sizeof(VhostVDPAState), 454f8972b56SEugenio Pérez .receive = vhost_vdpa_receive, 4557a7f87e9SEugenio Pérez .start = vhost_vdpa_net_cvq_start, 456dd036d8dSEugenio Pérez .load = vhost_vdpa_net_load, 4577a7f87e9SEugenio Pérez .stop = vhost_vdpa_net_cvq_stop, 458f8972b56SEugenio Pérez .cleanup = vhost_vdpa_cleanup, 459f8972b56SEugenio Pérez .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, 460f8972b56SEugenio Pérez .has_ufo = vhost_vdpa_has_ufo, 461f8972b56SEugenio Pérez .check_peer_type = vhost_vdpa_check_peer_type, 462f8972b56SEugenio Pérez }; 463f8972b56SEugenio Pérez 4642df4dd31SEugenio Pérez /** 4652df4dd31SEugenio Pérez * Do not forward commands not supported by SVQ. Otherwise, the device could 4662df4dd31SEugenio Pérez * accept it and qemu would not know how to update the device model. 4672df4dd31SEugenio Pérez */ 4687a7f87e9SEugenio Pérez static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len) 4692df4dd31SEugenio Pérez { 4702df4dd31SEugenio Pérez struct virtio_net_ctrl_hdr ctrl; 4712df4dd31SEugenio Pérez 4727a7f87e9SEugenio Pérez if (unlikely(len < sizeof(ctrl))) { 4732df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, 4747a7f87e9SEugenio Pérez "%s: invalid legnth of out buffer %zu\n", __func__, len); 4752df4dd31SEugenio Pérez return false; 4762df4dd31SEugenio Pérez } 4772df4dd31SEugenio Pérez 4787a7f87e9SEugenio Pérez memcpy(&ctrl, out_buf, sizeof(ctrl)); 4792df4dd31SEugenio Pérez switch (ctrl.class) { 4802df4dd31SEugenio Pérez case VIRTIO_NET_CTRL_MAC: 4812df4dd31SEugenio Pérez switch (ctrl.cmd) { 4822df4dd31SEugenio Pérez case VIRTIO_NET_CTRL_MAC_ADDR_SET: 4832df4dd31SEugenio Pérez return true; 4842df4dd31SEugenio Pérez default: 4852df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mac cmd %u\n", 4862df4dd31SEugenio Pérez __func__, ctrl.cmd); 4872df4dd31SEugenio Pérez }; 4882df4dd31SEugenio Pérez break; 4892df4dd31SEugenio Pérez default: 4902df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid control class %u\n", 4912df4dd31SEugenio Pérez __func__, ctrl.class); 4922df4dd31SEugenio Pérez }; 4932df4dd31SEugenio Pérez 4942df4dd31SEugenio Pérez return false; 4952df4dd31SEugenio Pérez } 4962df4dd31SEugenio Pérez 4972df4dd31SEugenio Pérez /** 4982df4dd31SEugenio Pérez * Validate and copy control virtqueue commands. 4992df4dd31SEugenio Pérez * 5002df4dd31SEugenio Pérez * Following QEMU guidelines, we offer a copy of the buffers to the device to 5012df4dd31SEugenio Pérez * prevent TOCTOU bugs. 502bd907ae4SEugenio Pérez */ 503bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq, 504bd907ae4SEugenio Pérez VirtQueueElement *elem, 505bd907ae4SEugenio Pérez void *opaque) 506bd907ae4SEugenio Pérez { 5072df4dd31SEugenio Pérez VhostVDPAState *s = opaque; 508be4278b6SEugenio Pérez size_t in_len; 509bd907ae4SEugenio Pérez virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 5107a7f87e9SEugenio Pérez /* Out buffer sent to both the vdpa device and the device model */ 5117a7f87e9SEugenio Pérez struct iovec out = { 5127a7f87e9SEugenio Pérez .iov_base = s->cvq_cmd_out_buffer, 5137a7f87e9SEugenio Pérez }; 5142df4dd31SEugenio Pérez /* in buffer used for device model */ 5152df4dd31SEugenio Pérez const struct iovec in = { 5162df4dd31SEugenio Pérez .iov_base = &status, 5172df4dd31SEugenio Pérez .iov_len = sizeof(status), 5182df4dd31SEugenio Pérez }; 519be4278b6SEugenio Pérez ssize_t dev_written = -EINVAL; 5202df4dd31SEugenio Pérez bool ok; 521bd907ae4SEugenio Pérez 5227a7f87e9SEugenio Pérez out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0, 5237a7f87e9SEugenio Pérez s->cvq_cmd_out_buffer, 5247a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_len()); 5257a7f87e9SEugenio Pérez ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len); 5262df4dd31SEugenio Pérez if (unlikely(!ok)) { 5272df4dd31SEugenio Pérez goto out; 5282df4dd31SEugenio Pérez } 529bd907ae4SEugenio Pérez 530be4278b6SEugenio Pérez dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status)); 531be4278b6SEugenio Pérez if (unlikely(dev_written < 0)) { 532bd907ae4SEugenio Pérez goto out; 533bd907ae4SEugenio Pérez } 534bd907ae4SEugenio Pérez 535bd907ae4SEugenio Pérez if (unlikely(dev_written < sizeof(status))) { 536bd907ae4SEugenio Pérez error_report("Insufficient written data (%zu)", dev_written); 5372df4dd31SEugenio Pérez goto out; 5382df4dd31SEugenio Pérez } 5392df4dd31SEugenio Pérez 54017fb889fSEugenio Pérez if (*s->status != VIRTIO_NET_OK) { 541be4278b6SEugenio Pérez return VIRTIO_NET_ERR; 5422df4dd31SEugenio Pérez } 5432df4dd31SEugenio Pérez 5442df4dd31SEugenio Pérez status = VIRTIO_NET_ERR; 5457a7f87e9SEugenio Pérez virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1); 5462df4dd31SEugenio Pérez if (status != VIRTIO_NET_OK) { 5472df4dd31SEugenio Pérez error_report("Bad CVQ processing in model"); 548bd907ae4SEugenio Pérez } 549bd907ae4SEugenio Pérez 550bd907ae4SEugenio Pérez out: 551bd907ae4SEugenio Pérez in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, 552bd907ae4SEugenio Pérez sizeof(status)); 553bd907ae4SEugenio Pérez if (unlikely(in_len < sizeof(status))) { 554bd907ae4SEugenio Pérez error_report("Bad device CVQ written length"); 555bd907ae4SEugenio Pérez } 556bd907ae4SEugenio Pérez vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status))); 557bd907ae4SEugenio Pérez g_free(elem); 558be4278b6SEugenio Pérez return dev_written < 0 ? dev_written : 0; 559bd907ae4SEugenio Pérez } 560bd907ae4SEugenio Pérez 561bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = { 562bd907ae4SEugenio Pérez .avail_handler = vhost_vdpa_net_handle_ctrl_avail, 563bd907ae4SEugenio Pérez }; 564bd907ae4SEugenio Pérez 565654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer, 566654790b6SJason Wang const char *device, 567654790b6SJason Wang const char *name, 56840237840SJason Wang int vdpa_device_fd, 56940237840SJason Wang int queue_pair_index, 57040237840SJason Wang int nvqs, 5711576dbb5SEugenio Pérez bool is_datapath, 5721576dbb5SEugenio Pérez bool svq, 5731576dbb5SEugenio Pérez VhostIOVATree *iova_tree) 5741e0a84eaSCindy Lu { 5751e0a84eaSCindy Lu NetClientState *nc = NULL; 5761e0a84eaSCindy Lu VhostVDPAState *s; 5771e0a84eaSCindy Lu int ret = 0; 5781e0a84eaSCindy Lu assert(name); 57940237840SJason Wang if (is_datapath) { 58040237840SJason Wang nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device, 58140237840SJason Wang name); 58240237840SJason Wang } else { 583f8972b56SEugenio Pérez nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer, 58440237840SJason Wang device, name); 58540237840SJason Wang } 58656e6f594SJason Wang snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); 5871e0a84eaSCindy Lu s = DO_UPCAST(VhostVDPAState, nc, nc); 5887327813dSJason Wang 5891e0a84eaSCindy Lu s->vhost_vdpa.device_fd = vdpa_device_fd; 59040237840SJason Wang s->vhost_vdpa.index = queue_pair_index; 5911576dbb5SEugenio Pérez s->vhost_vdpa.shadow_vqs_enabled = svq; 5921576dbb5SEugenio Pérez s->vhost_vdpa.iova_tree = iova_tree; 593bd907ae4SEugenio Pérez if (!is_datapath) { 5942df4dd31SEugenio Pérez s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(), 5952df4dd31SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len()); 5962df4dd31SEugenio Pérez memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len()); 59717fb889fSEugenio Pérez s->status = qemu_memalign(qemu_real_host_page_size(), 5982df4dd31SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len()); 59917fb889fSEugenio Pérez memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len()); 6002df4dd31SEugenio Pérez 601bd907ae4SEugenio Pérez s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops; 602bd907ae4SEugenio Pérez s->vhost_vdpa.shadow_vq_ops_opaque = s; 603bd907ae4SEugenio Pérez } 60440237840SJason Wang ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs); 60574af5eecSJason Wang if (ret) { 60674af5eecSJason Wang qemu_del_net_client(nc); 607654790b6SJason Wang return NULL; 60874af5eecSJason Wang } 609654790b6SJason Wang return nc; 6101e0a84eaSCindy Lu } 6111e0a84eaSCindy Lu 6121576dbb5SEugenio Pérez static int vhost_vdpa_get_iova_range(int fd, 6131576dbb5SEugenio Pérez struct vhost_vdpa_iova_range *iova_range) 6141576dbb5SEugenio Pérez { 6151576dbb5SEugenio Pérez int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); 6161576dbb5SEugenio Pérez 6171576dbb5SEugenio Pérez return ret < 0 ? -errno : 0; 6181576dbb5SEugenio Pérez } 6191576dbb5SEugenio Pérez 6208170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp) 6218170ab3fSEugenio Pérez { 6228170ab3fSEugenio Pérez int ret = ioctl(fd, VHOST_GET_FEATURES, features); 6238170ab3fSEugenio Pérez if (unlikely(ret < 0)) { 6248170ab3fSEugenio Pérez error_setg_errno(errp, errno, 6258170ab3fSEugenio Pérez "Fail to query features from vhost-vDPA device"); 6268170ab3fSEugenio Pérez } 6278170ab3fSEugenio Pérez return ret; 6288170ab3fSEugenio Pérez } 6298170ab3fSEugenio Pérez 6308170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features, 6318170ab3fSEugenio Pérez int *has_cvq, Error **errp) 63240237840SJason Wang { 63340237840SJason Wang unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 634cd523a41SStefano Garzarella g_autofree struct vhost_vdpa_config *config = NULL; 63540237840SJason Wang __virtio16 *max_queue_pairs; 63640237840SJason Wang int ret; 63740237840SJason Wang 63840237840SJason Wang if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) { 63940237840SJason Wang *has_cvq = 1; 64040237840SJason Wang } else { 64140237840SJason Wang *has_cvq = 0; 64240237840SJason Wang } 64340237840SJason Wang 64440237840SJason Wang if (features & (1 << VIRTIO_NET_F_MQ)) { 64540237840SJason Wang config = g_malloc0(config_size + sizeof(*max_queue_pairs)); 64640237840SJason Wang config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs); 64740237840SJason Wang config->len = sizeof(*max_queue_pairs); 64840237840SJason Wang 64940237840SJason Wang ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config); 65040237840SJason Wang if (ret) { 65140237840SJason Wang error_setg(errp, "Fail to get config from vhost-vDPA device"); 65240237840SJason Wang return -ret; 65340237840SJason Wang } 65440237840SJason Wang 65540237840SJason Wang max_queue_pairs = (__virtio16 *)&config->buf; 65640237840SJason Wang 65740237840SJason Wang return lduw_le_p(max_queue_pairs); 65840237840SJason Wang } 65940237840SJason Wang 66040237840SJason Wang return 1; 66140237840SJason Wang } 66240237840SJason Wang 6631e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name, 6641e0a84eaSCindy Lu NetClientState *peer, Error **errp) 6651e0a84eaSCindy Lu { 6661e0a84eaSCindy Lu const NetdevVhostVDPAOptions *opts; 6678170ab3fSEugenio Pérez uint64_t features; 668654790b6SJason Wang int vdpa_device_fd; 669eb3cb751SEugenio Pérez g_autofree NetClientState **ncs = NULL; 6701576dbb5SEugenio Pérez g_autoptr(VhostIOVATree) iova_tree = NULL; 671eb3cb751SEugenio Pérez NetClientState *nc; 672aed5da45SEugenio Pérez int queue_pairs, r, i = 0, has_cvq = 0; 6731e0a84eaSCindy Lu 6741e0a84eaSCindy Lu assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA); 6751e0a84eaSCindy Lu opts = &netdev->u.vhost_vdpa; 676c8295404SEugenio Pérez if (!opts->vhostdev) { 677c8295404SEugenio Pérez error_setg(errp, "vdpa character device not specified with vhostdev"); 678c8295404SEugenio Pérez return -1; 679c8295404SEugenio Pérez } 6807327813dSJason Wang 6810351152bSEugenio Pérez vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp); 6827327813dSJason Wang if (vdpa_device_fd == -1) { 6837327813dSJason Wang return -errno; 6847327813dSJason Wang } 6857327813dSJason Wang 6868170ab3fSEugenio Pérez r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp); 6878170ab3fSEugenio Pérez if (unlikely(r < 0)) { 688aed5da45SEugenio Pérez goto err; 6898170ab3fSEugenio Pérez } 6908170ab3fSEugenio Pérez 6918170ab3fSEugenio Pérez queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features, 69240237840SJason Wang &has_cvq, errp); 69340237840SJason Wang if (queue_pairs < 0) { 6947327813dSJason Wang qemu_close(vdpa_device_fd); 69540237840SJason Wang return queue_pairs; 6967327813dSJason Wang } 6977327813dSJason Wang 6981576dbb5SEugenio Pérez if (opts->x_svq) { 6991576dbb5SEugenio Pérez struct vhost_vdpa_iova_range iova_range; 7001576dbb5SEugenio Pérez 7011576dbb5SEugenio Pérez uint64_t invalid_dev_features = 7021576dbb5SEugenio Pérez features & ~vdpa_svq_device_features & 7031576dbb5SEugenio Pérez /* Transport are all accepted at this point */ 7041576dbb5SEugenio Pérez ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START, 7051576dbb5SEugenio Pérez VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START); 7061576dbb5SEugenio Pérez 7071576dbb5SEugenio Pérez if (invalid_dev_features) { 7081576dbb5SEugenio Pérez error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64, 7091576dbb5SEugenio Pérez invalid_dev_features); 7101576dbb5SEugenio Pérez goto err_svq; 7111576dbb5SEugenio Pérez } 7121576dbb5SEugenio Pérez 7131576dbb5SEugenio Pérez vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range); 7141576dbb5SEugenio Pérez iova_tree = vhost_iova_tree_new(iova_range.first, iova_range.last); 7151576dbb5SEugenio Pérez } 7161576dbb5SEugenio Pérez 71740237840SJason Wang ncs = g_malloc0(sizeof(*ncs) * queue_pairs); 71840237840SJason Wang 71940237840SJason Wang for (i = 0; i < queue_pairs; i++) { 72040237840SJason Wang ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, 7211576dbb5SEugenio Pérez vdpa_device_fd, i, 2, true, opts->x_svq, 7221576dbb5SEugenio Pérez iova_tree); 72340237840SJason Wang if (!ncs[i]) 72440237840SJason Wang goto err; 72540237840SJason Wang } 72640237840SJason Wang 72740237840SJason Wang if (has_cvq) { 72840237840SJason Wang nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, 7291576dbb5SEugenio Pérez vdpa_device_fd, i, 1, false, 7301576dbb5SEugenio Pérez opts->x_svq, iova_tree); 73140237840SJason Wang if (!nc) 73240237840SJason Wang goto err; 73340237840SJason Wang } 73440237840SJason Wang 7351576dbb5SEugenio Pérez /* iova_tree ownership belongs to last NetClientState */ 7361576dbb5SEugenio Pérez g_steal_pointer(&iova_tree); 737654790b6SJason Wang return 0; 73840237840SJason Wang 73940237840SJason Wang err: 74040237840SJason Wang if (i) { 7419bd05507SSi-Wei Liu for (i--; i >= 0; i--) { 7429bd05507SSi-Wei Liu qemu_del_net_client(ncs[i]); 7439bd05507SSi-Wei Liu } 74440237840SJason Wang } 7451576dbb5SEugenio Pérez 7461576dbb5SEugenio Pérez err_svq: 74740237840SJason Wang qemu_close(vdpa_device_fd); 74840237840SJason Wang 74940237840SJason Wang return -1; 7501e0a84eaSCindy Lu } 751