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 */ 382df4dd31SEugenio Pérez void *cvq_cmd_out_buffer, *cvq_cmd_in_buffer; 391e0a84eaSCindy Lu bool started; 401e0a84eaSCindy Lu } VhostVDPAState; 411e0a84eaSCindy Lu 421e0a84eaSCindy Lu const int vdpa_feature_bits[] = { 431e0a84eaSCindy Lu VIRTIO_F_NOTIFY_ON_EMPTY, 441e0a84eaSCindy Lu VIRTIO_RING_F_INDIRECT_DESC, 451e0a84eaSCindy Lu VIRTIO_RING_F_EVENT_IDX, 461e0a84eaSCindy Lu VIRTIO_F_ANY_LAYOUT, 471e0a84eaSCindy Lu VIRTIO_F_VERSION_1, 481e0a84eaSCindy Lu VIRTIO_NET_F_CSUM, 491e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_CSUM, 501e0a84eaSCindy Lu VIRTIO_NET_F_GSO, 511e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_TSO4, 521e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_TSO6, 531e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_ECN, 541e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_UFO, 551e0a84eaSCindy Lu VIRTIO_NET_F_HOST_TSO4, 561e0a84eaSCindy Lu VIRTIO_NET_F_HOST_TSO6, 571e0a84eaSCindy Lu VIRTIO_NET_F_HOST_ECN, 581e0a84eaSCindy Lu VIRTIO_NET_F_HOST_UFO, 591e0a84eaSCindy Lu VIRTIO_NET_F_MRG_RXBUF, 601e0a84eaSCindy Lu VIRTIO_NET_F_MTU, 6140237840SJason Wang VIRTIO_NET_F_CTRL_RX, 6240237840SJason Wang VIRTIO_NET_F_CTRL_RX_EXTRA, 6340237840SJason Wang VIRTIO_NET_F_CTRL_VLAN, 6440237840SJason Wang VIRTIO_NET_F_GUEST_ANNOUNCE, 6540237840SJason Wang VIRTIO_NET_F_CTRL_MAC_ADDR, 6640237840SJason Wang VIRTIO_NET_F_RSS, 6740237840SJason Wang VIRTIO_NET_F_MQ, 6840237840SJason Wang VIRTIO_NET_F_CTRL_VQ, 691e0a84eaSCindy Lu VIRTIO_F_IOMMU_PLATFORM, 701e0a84eaSCindy Lu VIRTIO_F_RING_PACKED, 710145c393SAndrew Melnychenko VIRTIO_NET_F_RSS, 720145c393SAndrew Melnychenko VIRTIO_NET_F_HASH_REPORT, 731e0a84eaSCindy Lu VIRTIO_NET_F_GUEST_ANNOUNCE, 749aa47eddSSi-Wei Liu VIRTIO_NET_F_STATUS, 751e0a84eaSCindy Lu VHOST_INVALID_FEATURE_BIT 761e0a84eaSCindy Lu }; 771e0a84eaSCindy Lu 781576dbb5SEugenio Pérez /** Supported device specific feature bits with SVQ */ 791576dbb5SEugenio Pérez static const uint64_t vdpa_svq_device_features = 801576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_CSUM) | 811576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) | 821576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_MTU) | 831576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_MAC) | 841576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) | 851576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) | 861576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_GUEST_ECN) | 871576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_GUEST_UFO) | 881576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_HOST_TSO4) | 891576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_HOST_TSO6) | 901576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_HOST_ECN) | 911576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_HOST_UFO) | 921576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) | 931576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_STATUS) | 941576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_CTRL_VQ) | 951576dbb5SEugenio Pérez BIT_ULL(VIRTIO_F_ANY_LAYOUT) | 961576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | 971576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_RSC_EXT) | 981576dbb5SEugenio Pérez BIT_ULL(VIRTIO_NET_F_STANDBY); 991576dbb5SEugenio Pérez 1001e0a84eaSCindy Lu VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc) 1011e0a84eaSCindy Lu { 1021e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1031e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1041e0a84eaSCindy Lu return s->vhost_net; 1051e0a84eaSCindy Lu } 1061e0a84eaSCindy Lu 1071e0a84eaSCindy Lu static int vhost_vdpa_net_check_device_id(struct vhost_net *net) 1081e0a84eaSCindy Lu { 1091e0a84eaSCindy Lu uint32_t device_id; 1101e0a84eaSCindy Lu int ret; 1111e0a84eaSCindy Lu struct vhost_dev *hdev; 1121e0a84eaSCindy Lu 1131e0a84eaSCindy Lu hdev = (struct vhost_dev *)&net->dev; 1141e0a84eaSCindy Lu ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id); 1151e0a84eaSCindy Lu if (device_id != VIRTIO_ID_NET) { 1161e0a84eaSCindy Lu return -ENOTSUP; 1171e0a84eaSCindy Lu } 1181e0a84eaSCindy Lu return ret; 1191e0a84eaSCindy Lu } 1201e0a84eaSCindy Lu 12140237840SJason Wang static int vhost_vdpa_add(NetClientState *ncs, void *be, 12240237840SJason Wang int queue_pair_index, int nvqs) 1231e0a84eaSCindy Lu { 1241e0a84eaSCindy Lu VhostNetOptions options; 1251e0a84eaSCindy Lu struct vhost_net *net = NULL; 1261e0a84eaSCindy Lu VhostVDPAState *s; 1271e0a84eaSCindy Lu int ret; 1281e0a84eaSCindy Lu 1291e0a84eaSCindy Lu options.backend_type = VHOST_BACKEND_TYPE_VDPA; 1301e0a84eaSCindy Lu assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1311e0a84eaSCindy Lu s = DO_UPCAST(VhostVDPAState, nc, ncs); 1321e0a84eaSCindy Lu options.net_backend = ncs; 1331e0a84eaSCindy Lu options.opaque = be; 1341e0a84eaSCindy Lu options.busyloop_timeout = 0; 13540237840SJason Wang options.nvqs = nvqs; 1361e0a84eaSCindy Lu 1371e0a84eaSCindy Lu net = vhost_net_init(&options); 1381e0a84eaSCindy Lu if (!net) { 1391e0a84eaSCindy Lu error_report("failed to init vhost_net for queue"); 140a97ef87aSJason Wang goto err_init; 1411e0a84eaSCindy Lu } 1421e0a84eaSCindy Lu s->vhost_net = net; 1431e0a84eaSCindy Lu ret = vhost_vdpa_net_check_device_id(net); 1441e0a84eaSCindy Lu if (ret) { 145a97ef87aSJason Wang goto err_check; 1461e0a84eaSCindy Lu } 1471e0a84eaSCindy Lu return 0; 148a97ef87aSJason Wang err_check: 1491e0a84eaSCindy Lu vhost_net_cleanup(net); 150ab36edcfSJason Wang g_free(net); 151a97ef87aSJason Wang err_init: 1521e0a84eaSCindy Lu return -1; 1531e0a84eaSCindy Lu } 1541e0a84eaSCindy Lu 1551e0a84eaSCindy Lu static void vhost_vdpa_cleanup(NetClientState *nc) 1561e0a84eaSCindy Lu { 1571e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1581576dbb5SEugenio Pérez struct vhost_dev *dev = &s->vhost_net->dev; 1591e0a84eaSCindy Lu 1602df4dd31SEugenio Pérez qemu_vfree(s->cvq_cmd_out_buffer); 1612df4dd31SEugenio Pérez qemu_vfree(s->cvq_cmd_in_buffer); 1621576dbb5SEugenio Pérez if (dev->vq_index + dev->nvqs == dev->vq_index_end) { 1631576dbb5SEugenio Pérez g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete); 1641576dbb5SEugenio Pérez } 1651e0a84eaSCindy Lu if (s->vhost_net) { 1661e0a84eaSCindy Lu vhost_net_cleanup(s->vhost_net); 1671e0a84eaSCindy Lu g_free(s->vhost_net); 1681e0a84eaSCindy Lu s->vhost_net = NULL; 1691e0a84eaSCindy Lu } 17057b3a7d8SCindy Lu if (s->vhost_vdpa.device_fd >= 0) { 17157b3a7d8SCindy Lu qemu_close(s->vhost_vdpa.device_fd); 17257b3a7d8SCindy Lu s->vhost_vdpa.device_fd = -1; 17357b3a7d8SCindy Lu } 1741e0a84eaSCindy Lu } 1751e0a84eaSCindy Lu 1761e0a84eaSCindy Lu static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc) 1771e0a84eaSCindy Lu { 1781e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1791e0a84eaSCindy Lu 1801e0a84eaSCindy Lu return true; 1811e0a84eaSCindy Lu } 1821e0a84eaSCindy Lu 1831e0a84eaSCindy Lu static bool vhost_vdpa_has_ufo(NetClientState *nc) 1841e0a84eaSCindy Lu { 1851e0a84eaSCindy Lu assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 1861e0a84eaSCindy Lu VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 1871e0a84eaSCindy Lu uint64_t features = 0; 1881e0a84eaSCindy Lu features |= (1ULL << VIRTIO_NET_F_HOST_UFO); 1891e0a84eaSCindy Lu features = vhost_net_get_features(s->vhost_net, features); 1901e0a84eaSCindy Lu return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO)); 1911e0a84eaSCindy Lu 1921e0a84eaSCindy Lu } 1931e0a84eaSCindy Lu 194ee8a1c63SKevin Wolf static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc, 195ee8a1c63SKevin Wolf Error **errp) 196ee8a1c63SKevin Wolf { 197ee8a1c63SKevin Wolf const char *driver = object_class_get_name(oc); 198ee8a1c63SKevin Wolf 199ee8a1c63SKevin Wolf if (!g_str_has_prefix(driver, "virtio-net-")) { 200ee8a1c63SKevin Wolf error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*"); 201ee8a1c63SKevin Wolf return false; 202ee8a1c63SKevin Wolf } 203ee8a1c63SKevin Wolf 204ee8a1c63SKevin Wolf return true; 205ee8a1c63SKevin Wolf } 206ee8a1c63SKevin Wolf 207846a1e85SEugenio Pérez /** Dummy receive in case qemu falls back to userland tap networking */ 208846a1e85SEugenio Pérez static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf, 209846a1e85SEugenio Pérez size_t size) 210846a1e85SEugenio Pérez { 211846a1e85SEugenio Pérez return 0; 212846a1e85SEugenio Pérez } 213846a1e85SEugenio Pérez 2141e0a84eaSCindy Lu static NetClientInfo net_vhost_vdpa_info = { 2151e0a84eaSCindy Lu .type = NET_CLIENT_DRIVER_VHOST_VDPA, 2161e0a84eaSCindy Lu .size = sizeof(VhostVDPAState), 217846a1e85SEugenio Pérez .receive = vhost_vdpa_receive, 2181e0a84eaSCindy Lu .cleanup = vhost_vdpa_cleanup, 2191e0a84eaSCindy Lu .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, 2201e0a84eaSCindy Lu .has_ufo = vhost_vdpa_has_ufo, 221ee8a1c63SKevin Wolf .check_peer_type = vhost_vdpa_check_peer_type, 2221e0a84eaSCindy Lu }; 2231e0a84eaSCindy Lu 2242df4dd31SEugenio Pérez static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr) 2252df4dd31SEugenio Pérez { 2262df4dd31SEugenio Pérez VhostIOVATree *tree = v->iova_tree; 2272df4dd31SEugenio Pérez DMAMap needle = { 2282df4dd31SEugenio Pérez /* 2292df4dd31SEugenio Pérez * No need to specify size or to look for more translations since 2302df4dd31SEugenio Pérez * this contiguous chunk was allocated by us. 2312df4dd31SEugenio Pérez */ 2322df4dd31SEugenio Pérez .translated_addr = (hwaddr)(uintptr_t)addr, 2332df4dd31SEugenio Pérez }; 2342df4dd31SEugenio Pérez const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle); 2352df4dd31SEugenio Pérez int r; 2362df4dd31SEugenio Pérez 2372df4dd31SEugenio Pérez if (unlikely(!map)) { 2382df4dd31SEugenio Pérez error_report("Cannot locate expected map"); 2392df4dd31SEugenio Pérez return; 2402df4dd31SEugenio Pérez } 2412df4dd31SEugenio Pérez 2422df4dd31SEugenio Pérez r = vhost_vdpa_dma_unmap(v, map->iova, map->size + 1); 2432df4dd31SEugenio Pérez if (unlikely(r != 0)) { 2442df4dd31SEugenio Pérez error_report("Device cannot unmap: %s(%d)", g_strerror(r), r); 2452df4dd31SEugenio Pérez } 2462df4dd31SEugenio Pérez 24769292a8eSEugenio Pérez vhost_iova_tree_remove(tree, *map); 2482df4dd31SEugenio Pérez } 2492df4dd31SEugenio Pérez 2502df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_len(void) 2512df4dd31SEugenio Pérez { 2522df4dd31SEugenio Pérez /* 2532df4dd31SEugenio Pérez * MAC_TABLE_SET is the ctrl command that produces the longer out buffer. 2542df4dd31SEugenio Pérez * In buffer is always 1 byte, so it should fit here 2552df4dd31SEugenio Pérez */ 2562df4dd31SEugenio Pérez return sizeof(struct virtio_net_ctrl_hdr) + 2572df4dd31SEugenio Pérez 2 * sizeof(struct virtio_net_ctrl_mac) + 2582df4dd31SEugenio Pérez MAC_TABLE_ENTRIES * ETH_ALEN; 2592df4dd31SEugenio Pérez } 2602df4dd31SEugenio Pérez 2612df4dd31SEugenio Pérez static size_t vhost_vdpa_net_cvq_cmd_page_len(void) 2622df4dd31SEugenio Pérez { 2632df4dd31SEugenio Pérez return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size()); 2642df4dd31SEugenio Pérez } 2652df4dd31SEugenio Pérez 266*7a7f87e9SEugenio Pérez /** Map CVQ buffer. */ 267*7a7f87e9SEugenio Pérez static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size, 268*7a7f87e9SEugenio Pérez bool write) 2692df4dd31SEugenio Pérez { 2702df4dd31SEugenio Pérez DMAMap map = {}; 2712df4dd31SEugenio Pérez int r; 2722df4dd31SEugenio Pérez 2732df4dd31SEugenio Pérez map.translated_addr = (hwaddr)(uintptr_t)buf; 274*7a7f87e9SEugenio Pérez map.size = size - 1; 2752df4dd31SEugenio Pérez map.perm = write ? IOMMU_RW : IOMMU_RO, 2762df4dd31SEugenio Pérez r = vhost_iova_tree_map_alloc(v->iova_tree, &map); 2772df4dd31SEugenio Pérez if (unlikely(r != IOVA_OK)) { 2782df4dd31SEugenio Pérez error_report("Cannot map injected element"); 279*7a7f87e9SEugenio Pérez return r; 2802df4dd31SEugenio Pérez } 2812df4dd31SEugenio Pérez 2822df4dd31SEugenio Pérez r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf, 2832df4dd31SEugenio Pérez !write); 2842df4dd31SEugenio Pérez if (unlikely(r < 0)) { 2852df4dd31SEugenio Pérez goto dma_map_err; 2862df4dd31SEugenio Pérez } 2872df4dd31SEugenio Pérez 288*7a7f87e9SEugenio Pérez return 0; 2892df4dd31SEugenio Pérez 2902df4dd31SEugenio Pérez dma_map_err: 29169292a8eSEugenio Pérez vhost_iova_tree_remove(v->iova_tree, map); 292*7a7f87e9SEugenio Pérez return r; 2932df4dd31SEugenio Pérez } 2942df4dd31SEugenio Pérez 295*7a7f87e9SEugenio Pérez static int vhost_vdpa_net_cvq_start(NetClientState *nc) 2962df4dd31SEugenio Pérez { 297*7a7f87e9SEugenio Pérez VhostVDPAState *s; 298*7a7f87e9SEugenio Pérez int r; 2992df4dd31SEugenio Pérez 300*7a7f87e9SEugenio Pérez assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 301*7a7f87e9SEugenio Pérez 302*7a7f87e9SEugenio Pérez s = DO_UPCAST(VhostVDPAState, nc, nc); 303*7a7f87e9SEugenio Pérez if (!s->vhost_vdpa.shadow_vqs_enabled) { 304*7a7f87e9SEugenio Pérez return 0; 3052df4dd31SEugenio Pérez } 3062df4dd31SEugenio Pérez 307*7a7f87e9SEugenio Pérez r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer, 308*7a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len(), false); 309*7a7f87e9SEugenio Pérez if (unlikely(r < 0)) { 310*7a7f87e9SEugenio Pérez return r; 311*7a7f87e9SEugenio Pérez } 312*7a7f87e9SEugenio Pérez 313*7a7f87e9SEugenio Pérez r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_in_buffer, 314*7a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len(), true); 315*7a7f87e9SEugenio Pérez if (unlikely(r < 0)) { 3162df4dd31SEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); 3172df4dd31SEugenio Pérez } 3182df4dd31SEugenio Pérez 319*7a7f87e9SEugenio Pérez return r; 320*7a7f87e9SEugenio Pérez } 321*7a7f87e9SEugenio Pérez 322*7a7f87e9SEugenio Pérez static void vhost_vdpa_net_cvq_stop(NetClientState *nc) 323*7a7f87e9SEugenio Pérez { 324*7a7f87e9SEugenio Pérez VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 325*7a7f87e9SEugenio Pérez 326*7a7f87e9SEugenio Pérez assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 327*7a7f87e9SEugenio Pérez 328*7a7f87e9SEugenio Pérez if (s->vhost_vdpa.shadow_vqs_enabled) { 329*7a7f87e9SEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); 330*7a7f87e9SEugenio Pérez vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_in_buffer); 331*7a7f87e9SEugenio Pérez } 3322df4dd31SEugenio Pérez } 3332df4dd31SEugenio Pérez 334f8972b56SEugenio Pérez static NetClientInfo net_vhost_vdpa_cvq_info = { 335f8972b56SEugenio Pérez .type = NET_CLIENT_DRIVER_VHOST_VDPA, 336f8972b56SEugenio Pérez .size = sizeof(VhostVDPAState), 337f8972b56SEugenio Pérez .receive = vhost_vdpa_receive, 338*7a7f87e9SEugenio Pérez .start = vhost_vdpa_net_cvq_start, 339*7a7f87e9SEugenio Pérez .stop = vhost_vdpa_net_cvq_stop, 340f8972b56SEugenio Pérez .cleanup = vhost_vdpa_cleanup, 341f8972b56SEugenio Pérez .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, 342f8972b56SEugenio Pérez .has_ufo = vhost_vdpa_has_ufo, 343f8972b56SEugenio Pérez .check_peer_type = vhost_vdpa_check_peer_type, 344f8972b56SEugenio Pérez }; 345f8972b56SEugenio Pérez 3462df4dd31SEugenio Pérez /** 3472df4dd31SEugenio Pérez * Do not forward commands not supported by SVQ. Otherwise, the device could 3482df4dd31SEugenio Pérez * accept it and qemu would not know how to update the device model. 3492df4dd31SEugenio Pérez */ 350*7a7f87e9SEugenio Pérez static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len) 3512df4dd31SEugenio Pérez { 3522df4dd31SEugenio Pérez struct virtio_net_ctrl_hdr ctrl; 3532df4dd31SEugenio Pérez 354*7a7f87e9SEugenio Pérez if (unlikely(len < sizeof(ctrl))) { 3552df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, 356*7a7f87e9SEugenio Pérez "%s: invalid legnth of out buffer %zu\n", __func__, len); 3572df4dd31SEugenio Pérez return false; 3582df4dd31SEugenio Pérez } 3592df4dd31SEugenio Pérez 360*7a7f87e9SEugenio Pérez memcpy(&ctrl, out_buf, sizeof(ctrl)); 3612df4dd31SEugenio Pérez switch (ctrl.class) { 3622df4dd31SEugenio Pérez case VIRTIO_NET_CTRL_MAC: 3632df4dd31SEugenio Pérez switch (ctrl.cmd) { 3642df4dd31SEugenio Pérez case VIRTIO_NET_CTRL_MAC_ADDR_SET: 3652df4dd31SEugenio Pérez return true; 3662df4dd31SEugenio Pérez default: 3672df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mac cmd %u\n", 3682df4dd31SEugenio Pérez __func__, ctrl.cmd); 3692df4dd31SEugenio Pérez }; 3702df4dd31SEugenio Pérez break; 3712df4dd31SEugenio Pérez default: 3722df4dd31SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid control class %u\n", 3732df4dd31SEugenio Pérez __func__, ctrl.class); 3742df4dd31SEugenio Pérez }; 3752df4dd31SEugenio Pérez 3762df4dd31SEugenio Pérez return false; 3772df4dd31SEugenio Pérez } 3782df4dd31SEugenio Pérez 3792df4dd31SEugenio Pérez /** 3802df4dd31SEugenio Pérez * Validate and copy control virtqueue commands. 3812df4dd31SEugenio Pérez * 3822df4dd31SEugenio Pérez * Following QEMU guidelines, we offer a copy of the buffers to the device to 3832df4dd31SEugenio Pérez * prevent TOCTOU bugs. 384bd907ae4SEugenio Pérez */ 385bd907ae4SEugenio Pérez static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq, 386bd907ae4SEugenio Pérez VirtQueueElement *elem, 387bd907ae4SEugenio Pérez void *opaque) 388bd907ae4SEugenio Pérez { 3892df4dd31SEugenio Pérez VhostVDPAState *s = opaque; 390bd907ae4SEugenio Pérez size_t in_len, dev_written; 391bd907ae4SEugenio Pérez virtio_net_ctrl_ack status = VIRTIO_NET_ERR; 392*7a7f87e9SEugenio Pérez /* Out buffer sent to both the vdpa device and the device model */ 393*7a7f87e9SEugenio Pérez struct iovec out = { 394*7a7f87e9SEugenio Pérez .iov_base = s->cvq_cmd_out_buffer, 395*7a7f87e9SEugenio Pérez }; 396*7a7f87e9SEugenio Pérez /* In buffer sent to the device */ 397*7a7f87e9SEugenio Pérez const struct iovec dev_in = { 398*7a7f87e9SEugenio Pérez .iov_base = s->cvq_cmd_in_buffer, 399*7a7f87e9SEugenio Pérez .iov_len = sizeof(virtio_net_ctrl_ack), 4002df4dd31SEugenio Pérez }; 4012df4dd31SEugenio Pérez /* in buffer used for device model */ 4022df4dd31SEugenio Pérez const struct iovec in = { 4032df4dd31SEugenio Pérez .iov_base = &status, 4042df4dd31SEugenio Pérez .iov_len = sizeof(status), 4052df4dd31SEugenio Pérez }; 4062df4dd31SEugenio Pérez int r = -EINVAL; 4072df4dd31SEugenio Pérez bool ok; 408bd907ae4SEugenio Pérez 409*7a7f87e9SEugenio Pérez out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0, 410*7a7f87e9SEugenio Pérez s->cvq_cmd_out_buffer, 411*7a7f87e9SEugenio Pérez vhost_vdpa_net_cvq_cmd_len()); 412*7a7f87e9SEugenio Pérez ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len); 4132df4dd31SEugenio Pérez if (unlikely(!ok)) { 4142df4dd31SEugenio Pérez goto out; 4152df4dd31SEugenio Pérez } 416bd907ae4SEugenio Pérez 417*7a7f87e9SEugenio Pérez r = vhost_svq_add(svq, &out, 1, &dev_in, 1, elem); 418bd907ae4SEugenio Pérez if (unlikely(r != 0)) { 419bd907ae4SEugenio Pérez if (unlikely(r == -ENOSPC)) { 420bd907ae4SEugenio Pérez qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n", 421bd907ae4SEugenio Pérez __func__); 422bd907ae4SEugenio Pérez } 423bd907ae4SEugenio Pérez goto out; 424bd907ae4SEugenio Pérez } 425bd907ae4SEugenio Pérez 426bd907ae4SEugenio Pérez /* 427bd907ae4SEugenio Pérez * We can poll here since we've had BQL from the time we sent the 428bd907ae4SEugenio Pérez * descriptor. Also, we need to take the answer before SVQ pulls by itself, 429bd907ae4SEugenio Pérez * when BQL is released 430bd907ae4SEugenio Pérez */ 431bd907ae4SEugenio Pérez dev_written = vhost_svq_poll(svq); 432bd907ae4SEugenio Pérez if (unlikely(dev_written < sizeof(status))) { 433bd907ae4SEugenio Pérez error_report("Insufficient written data (%zu)", dev_written); 4342df4dd31SEugenio Pérez goto out; 4352df4dd31SEugenio Pérez } 4362df4dd31SEugenio Pérez 437*7a7f87e9SEugenio Pérez memcpy(&status, s->cvq_cmd_in_buffer, sizeof(status)); 4382df4dd31SEugenio Pérez if (status != VIRTIO_NET_OK) { 4392df4dd31SEugenio Pérez goto out; 4402df4dd31SEugenio Pérez } 4412df4dd31SEugenio Pérez 4422df4dd31SEugenio Pérez status = VIRTIO_NET_ERR; 443*7a7f87e9SEugenio Pérez virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1); 4442df4dd31SEugenio Pérez if (status != VIRTIO_NET_OK) { 4452df4dd31SEugenio Pérez error_report("Bad CVQ processing in model"); 446bd907ae4SEugenio Pérez } 447bd907ae4SEugenio Pérez 448bd907ae4SEugenio Pérez out: 449bd907ae4SEugenio Pérez in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, 450bd907ae4SEugenio Pérez sizeof(status)); 451bd907ae4SEugenio Pérez if (unlikely(in_len < sizeof(status))) { 452bd907ae4SEugenio Pérez error_report("Bad device CVQ written length"); 453bd907ae4SEugenio Pérez } 454bd907ae4SEugenio Pérez vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status))); 455bd907ae4SEugenio Pérez g_free(elem); 456bd907ae4SEugenio Pérez return r; 457bd907ae4SEugenio Pérez } 458bd907ae4SEugenio Pérez 459bd907ae4SEugenio Pérez static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = { 460bd907ae4SEugenio Pérez .avail_handler = vhost_vdpa_net_handle_ctrl_avail, 461bd907ae4SEugenio Pérez }; 462bd907ae4SEugenio Pérez 463654790b6SJason Wang static NetClientState *net_vhost_vdpa_init(NetClientState *peer, 464654790b6SJason Wang const char *device, 465654790b6SJason Wang const char *name, 46640237840SJason Wang int vdpa_device_fd, 46740237840SJason Wang int queue_pair_index, 46840237840SJason Wang int nvqs, 4691576dbb5SEugenio Pérez bool is_datapath, 4701576dbb5SEugenio Pérez bool svq, 4711576dbb5SEugenio Pérez VhostIOVATree *iova_tree) 4721e0a84eaSCindy Lu { 4731e0a84eaSCindy Lu NetClientState *nc = NULL; 4741e0a84eaSCindy Lu VhostVDPAState *s; 4751e0a84eaSCindy Lu int ret = 0; 4761e0a84eaSCindy Lu assert(name); 47740237840SJason Wang if (is_datapath) { 47840237840SJason Wang nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device, 47940237840SJason Wang name); 48040237840SJason Wang } else { 481f8972b56SEugenio Pérez nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer, 48240237840SJason Wang device, name); 48340237840SJason Wang } 48456e6f594SJason Wang snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); 4851e0a84eaSCindy Lu s = DO_UPCAST(VhostVDPAState, nc, nc); 4867327813dSJason Wang 4871e0a84eaSCindy Lu s->vhost_vdpa.device_fd = vdpa_device_fd; 48840237840SJason Wang s->vhost_vdpa.index = queue_pair_index; 4891576dbb5SEugenio Pérez s->vhost_vdpa.shadow_vqs_enabled = svq; 4901576dbb5SEugenio Pérez s->vhost_vdpa.iova_tree = iova_tree; 491bd907ae4SEugenio Pérez if (!is_datapath) { 4922df4dd31SEugenio Pérez s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(), 4932df4dd31SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len()); 4942df4dd31SEugenio Pérez memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len()); 4952df4dd31SEugenio Pérez s->cvq_cmd_in_buffer = qemu_memalign(qemu_real_host_page_size(), 4962df4dd31SEugenio Pérez vhost_vdpa_net_cvq_cmd_page_len()); 4972df4dd31SEugenio Pérez memset(s->cvq_cmd_in_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len()); 4982df4dd31SEugenio Pérez 499bd907ae4SEugenio Pérez s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops; 500bd907ae4SEugenio Pérez s->vhost_vdpa.shadow_vq_ops_opaque = s; 5011576dbb5SEugenio Pérez error_setg(&s->vhost_vdpa.migration_blocker, 5021576dbb5SEugenio Pérez "Migration disabled: vhost-vdpa uses CVQ."); 503bd907ae4SEugenio Pérez } 50440237840SJason Wang ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs); 50574af5eecSJason Wang if (ret) { 50674af5eecSJason Wang qemu_del_net_client(nc); 507654790b6SJason Wang return NULL; 50874af5eecSJason Wang } 509654790b6SJason Wang return nc; 5101e0a84eaSCindy Lu } 5111e0a84eaSCindy Lu 5121576dbb5SEugenio Pérez static int vhost_vdpa_get_iova_range(int fd, 5131576dbb5SEugenio Pérez struct vhost_vdpa_iova_range *iova_range) 5141576dbb5SEugenio Pérez { 5151576dbb5SEugenio Pérez int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); 5161576dbb5SEugenio Pérez 5171576dbb5SEugenio Pérez return ret < 0 ? -errno : 0; 5181576dbb5SEugenio Pérez } 5191576dbb5SEugenio Pérez 5208170ab3fSEugenio Pérez static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp) 5218170ab3fSEugenio Pérez { 5228170ab3fSEugenio Pérez int ret = ioctl(fd, VHOST_GET_FEATURES, features); 5238170ab3fSEugenio Pérez if (unlikely(ret < 0)) { 5248170ab3fSEugenio Pérez error_setg_errno(errp, errno, 5258170ab3fSEugenio Pérez "Fail to query features from vhost-vDPA device"); 5268170ab3fSEugenio Pérez } 5278170ab3fSEugenio Pérez return ret; 5288170ab3fSEugenio Pérez } 5298170ab3fSEugenio Pérez 5308170ab3fSEugenio Pérez static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features, 5318170ab3fSEugenio Pérez int *has_cvq, Error **errp) 53240237840SJason Wang { 53340237840SJason Wang unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); 534cd523a41SStefano Garzarella g_autofree struct vhost_vdpa_config *config = NULL; 53540237840SJason Wang __virtio16 *max_queue_pairs; 53640237840SJason Wang int ret; 53740237840SJason Wang 53840237840SJason Wang if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) { 53940237840SJason Wang *has_cvq = 1; 54040237840SJason Wang } else { 54140237840SJason Wang *has_cvq = 0; 54240237840SJason Wang } 54340237840SJason Wang 54440237840SJason Wang if (features & (1 << VIRTIO_NET_F_MQ)) { 54540237840SJason Wang config = g_malloc0(config_size + sizeof(*max_queue_pairs)); 54640237840SJason Wang config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs); 54740237840SJason Wang config->len = sizeof(*max_queue_pairs); 54840237840SJason Wang 54940237840SJason Wang ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config); 55040237840SJason Wang if (ret) { 55140237840SJason Wang error_setg(errp, "Fail to get config from vhost-vDPA device"); 55240237840SJason Wang return -ret; 55340237840SJason Wang } 55440237840SJason Wang 55540237840SJason Wang max_queue_pairs = (__virtio16 *)&config->buf; 55640237840SJason Wang 55740237840SJason Wang return lduw_le_p(max_queue_pairs); 55840237840SJason Wang } 55940237840SJason Wang 56040237840SJason Wang return 1; 56140237840SJason Wang } 56240237840SJason Wang 5631e0a84eaSCindy Lu int net_init_vhost_vdpa(const Netdev *netdev, const char *name, 5641e0a84eaSCindy Lu NetClientState *peer, Error **errp) 5651e0a84eaSCindy Lu { 5661e0a84eaSCindy Lu const NetdevVhostVDPAOptions *opts; 5678170ab3fSEugenio Pérez uint64_t features; 568654790b6SJason Wang int vdpa_device_fd; 569eb3cb751SEugenio Pérez g_autofree NetClientState **ncs = NULL; 5701576dbb5SEugenio Pérez g_autoptr(VhostIOVATree) iova_tree = NULL; 571eb3cb751SEugenio Pérez NetClientState *nc; 572aed5da45SEugenio Pérez int queue_pairs, r, i = 0, has_cvq = 0; 5731e0a84eaSCindy Lu 5741e0a84eaSCindy Lu assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA); 5751e0a84eaSCindy Lu opts = &netdev->u.vhost_vdpa; 576c8295404SEugenio Pérez if (!opts->vhostdev) { 577c8295404SEugenio Pérez error_setg(errp, "vdpa character device not specified with vhostdev"); 578c8295404SEugenio Pérez return -1; 579c8295404SEugenio Pérez } 5807327813dSJason Wang 5810351152bSEugenio Pérez vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp); 5827327813dSJason Wang if (vdpa_device_fd == -1) { 5837327813dSJason Wang return -errno; 5847327813dSJason Wang } 5857327813dSJason Wang 5868170ab3fSEugenio Pérez r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp); 5878170ab3fSEugenio Pérez if (unlikely(r < 0)) { 588aed5da45SEugenio Pérez goto err; 5898170ab3fSEugenio Pérez } 5908170ab3fSEugenio Pérez 5918170ab3fSEugenio Pérez queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features, 59240237840SJason Wang &has_cvq, errp); 59340237840SJason Wang if (queue_pairs < 0) { 5947327813dSJason Wang qemu_close(vdpa_device_fd); 59540237840SJason Wang return queue_pairs; 5967327813dSJason Wang } 5977327813dSJason Wang 5981576dbb5SEugenio Pérez if (opts->x_svq) { 5991576dbb5SEugenio Pérez struct vhost_vdpa_iova_range iova_range; 6001576dbb5SEugenio Pérez 6011576dbb5SEugenio Pérez uint64_t invalid_dev_features = 6021576dbb5SEugenio Pérez features & ~vdpa_svq_device_features & 6031576dbb5SEugenio Pérez /* Transport are all accepted at this point */ 6041576dbb5SEugenio Pérez ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START, 6051576dbb5SEugenio Pérez VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START); 6061576dbb5SEugenio Pérez 6071576dbb5SEugenio Pérez if (invalid_dev_features) { 6081576dbb5SEugenio Pérez error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64, 6091576dbb5SEugenio Pérez invalid_dev_features); 6101576dbb5SEugenio Pérez goto err_svq; 6111576dbb5SEugenio Pérez } 6121576dbb5SEugenio Pérez 6131576dbb5SEugenio Pérez vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range); 6141576dbb5SEugenio Pérez iova_tree = vhost_iova_tree_new(iova_range.first, iova_range.last); 6151576dbb5SEugenio Pérez } 6161576dbb5SEugenio Pérez 61740237840SJason Wang ncs = g_malloc0(sizeof(*ncs) * queue_pairs); 61840237840SJason Wang 61940237840SJason Wang for (i = 0; i < queue_pairs; i++) { 62040237840SJason Wang ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, 6211576dbb5SEugenio Pérez vdpa_device_fd, i, 2, true, opts->x_svq, 6221576dbb5SEugenio Pérez iova_tree); 62340237840SJason Wang if (!ncs[i]) 62440237840SJason Wang goto err; 62540237840SJason Wang } 62640237840SJason Wang 62740237840SJason Wang if (has_cvq) { 62840237840SJason Wang nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, 6291576dbb5SEugenio Pérez vdpa_device_fd, i, 1, false, 6301576dbb5SEugenio Pérez opts->x_svq, iova_tree); 63140237840SJason Wang if (!nc) 63240237840SJason Wang goto err; 63340237840SJason Wang } 63440237840SJason Wang 6351576dbb5SEugenio Pérez /* iova_tree ownership belongs to last NetClientState */ 6361576dbb5SEugenio Pérez g_steal_pointer(&iova_tree); 637654790b6SJason Wang return 0; 63840237840SJason Wang 63940237840SJason Wang err: 64040237840SJason Wang if (i) { 6419bd05507SSi-Wei Liu for (i--; i >= 0; i--) { 6429bd05507SSi-Wei Liu qemu_del_net_client(ncs[i]); 6439bd05507SSi-Wei Liu } 64440237840SJason Wang } 6451576dbb5SEugenio Pérez 6461576dbb5SEugenio Pérez err_svq: 64740237840SJason Wang qemu_close(vdpa_device_fd); 64840237840SJason Wang 64940237840SJason Wang return -1; 6501e0a84eaSCindy Lu } 651