| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * vhost-vdpa.c | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright(c) 2017-2018 Intel Corporation. | 
					
						
							|  |  |  |  * Copyright(c) 2020 Red Hat, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							|  |  |  | #include "clients.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  | #include "hw/virtio/virtio-net.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | #include "net/vhost_net.h"
 | 
					
						
							|  |  |  | #include "net/vhost-vdpa.h"
 | 
					
						
							|  |  |  | #include "hw/virtio/vhost-vdpa.h"
 | 
					
						
							|  |  |  | #include "qemu/config-file.h"
 | 
					
						
							|  |  |  | #include "qemu/error-report.h"
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  | #include "qemu/log.h"
 | 
					
						
							|  |  |  | #include "qemu/memalign.h"
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | #include "qemu/option.h"
 | 
					
						
							|  |  |  | #include "qapi/error.h"
 | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  | #include <linux/vhost.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | #include <sys/ioctl.h>
 | 
					
						
							|  |  |  | #include <err.h>
 | 
					
						
							|  |  |  | #include "standard-headers/linux/virtio_net.h"
 | 
					
						
							|  |  |  | #include "monitor/monitor.h"
 | 
					
						
							|  |  |  | #include "hw/virtio/vhost.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Todo:need to add the multiqueue support here */ | 
					
						
							|  |  |  | typedef struct VhostVDPAState { | 
					
						
							|  |  |  |     NetClientState nc; | 
					
						
							|  |  |  |     struct vhost_vdpa vhost_vdpa; | 
					
						
							|  |  |  |     VHostNetState *vhost_net; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Control commands shadow buffers */ | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |     void *cvq_cmd_out_buffer; | 
					
						
							|  |  |  |     virtio_net_ctrl_ack *status; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     bool started; | 
					
						
							|  |  |  | } VhostVDPAState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const int vdpa_feature_bits[] = { | 
					
						
							|  |  |  |     VIRTIO_F_NOTIFY_ON_EMPTY, | 
					
						
							|  |  |  |     VIRTIO_RING_F_INDIRECT_DESC, | 
					
						
							|  |  |  |     VIRTIO_RING_F_EVENT_IDX, | 
					
						
							|  |  |  |     VIRTIO_F_ANY_LAYOUT, | 
					
						
							|  |  |  |     VIRTIO_F_VERSION_1, | 
					
						
							|  |  |  |     VIRTIO_NET_F_CSUM, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_CSUM, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GSO, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_TSO4, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_TSO6, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_ECN, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_UFO, | 
					
						
							|  |  |  |     VIRTIO_NET_F_HOST_TSO4, | 
					
						
							|  |  |  |     VIRTIO_NET_F_HOST_TSO6, | 
					
						
							|  |  |  |     VIRTIO_NET_F_HOST_ECN, | 
					
						
							|  |  |  |     VIRTIO_NET_F_HOST_UFO, | 
					
						
							|  |  |  |     VIRTIO_NET_F_MRG_RXBUF, | 
					
						
							|  |  |  |     VIRTIO_NET_F_MTU, | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     VIRTIO_NET_F_CTRL_RX, | 
					
						
							|  |  |  |     VIRTIO_NET_F_CTRL_RX_EXTRA, | 
					
						
							|  |  |  |     VIRTIO_NET_F_CTRL_VLAN, | 
					
						
							|  |  |  |     VIRTIO_NET_F_GUEST_ANNOUNCE, | 
					
						
							|  |  |  |     VIRTIO_NET_F_CTRL_MAC_ADDR, | 
					
						
							|  |  |  |     VIRTIO_NET_F_RSS, | 
					
						
							|  |  |  |     VIRTIO_NET_F_MQ, | 
					
						
							|  |  |  |     VIRTIO_NET_F_CTRL_VQ, | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     VIRTIO_F_IOMMU_PLATFORM, | 
					
						
							|  |  |  |     VIRTIO_F_RING_PACKED, | 
					
						
							| 
									
										
										
										
											2021-05-14 14:48:33 +03:00
										 |  |  |     VIRTIO_NET_F_RSS, | 
					
						
							|  |  |  |     VIRTIO_NET_F_HASH_REPORT, | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     VIRTIO_NET_F_GUEST_ANNOUNCE, | 
					
						
							| 
									
										
										
										
											2020-10-01 16:09:45 -04:00
										 |  |  |     VIRTIO_NET_F_STATUS, | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     VHOST_INVALID_FEATURE_BIT | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  | /** Supported device specific feature bits with SVQ */ | 
					
						
							|  |  |  | static const uint64_t vdpa_svq_device_features = | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_CSUM) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_GUEST_CSUM) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_MTU) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_MAC) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_GUEST_TSO4) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_GUEST_TSO6) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_GUEST_ECN) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_GUEST_UFO) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_HOST_TSO4) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_HOST_TSO6) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_HOST_ECN) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_HOST_UFO) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_MRG_RXBUF) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_STATUS) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_CTRL_VQ) | | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:19 +02:00
										 |  |  |     BIT_ULL(VIRTIO_NET_F_MQ) | | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     BIT_ULL(VIRTIO_F_ANY_LAYOUT) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_RSC_EXT) | | 
					
						
							|  |  |  |     BIT_ULL(VIRTIO_NET_F_STANDBY); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							|  |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  |     return s->vhost_net; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int vhost_vdpa_net_check_device_id(struct vhost_net *net) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t device_id; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     struct vhost_dev *hdev; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     hdev = (struct vhost_dev *)&net->dev; | 
					
						
							|  |  |  |     ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id); | 
					
						
							|  |  |  |     if (device_id != VIRTIO_ID_NET) { | 
					
						
							|  |  |  |         return -ENOTSUP; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  | static int vhost_vdpa_add(NetClientState *ncs, void *be, | 
					
						
							|  |  |  |                           int queue_pair_index, int nvqs) | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     VhostNetOptions options; | 
					
						
							|  |  |  |     struct vhost_net *net = NULL; | 
					
						
							|  |  |  |     VhostVDPAState *s; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     options.backend_type = VHOST_BACKEND_TYPE_VDPA; | 
					
						
							|  |  |  |     assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  |     s = DO_UPCAST(VhostVDPAState, nc, ncs); | 
					
						
							|  |  |  |     options.net_backend = ncs; | 
					
						
							|  |  |  |     options.opaque      = be; | 
					
						
							|  |  |  |     options.busyloop_timeout = 0; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     options.nvqs = nvqs; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     net = vhost_net_init(&options); | 
					
						
							|  |  |  |     if (!net) { | 
					
						
							|  |  |  |         error_report("failed to init vhost_net for queue"); | 
					
						
							| 
									
										
										
										
											2021-09-03 17:10:19 +08:00
										 |  |  |         goto err_init; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     s->vhost_net = net; | 
					
						
							|  |  |  |     ret = vhost_vdpa_net_check_device_id(net); | 
					
						
							|  |  |  |     if (ret) { | 
					
						
							| 
									
										
										
										
											2021-09-03 17:10:19 +08:00
										 |  |  |         goto err_check; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2021-09-03 17:10:19 +08:00
										 |  |  | err_check: | 
					
						
							|  |  |  |     vhost_net_cleanup(net); | 
					
						
							|  |  |  |     g_free(net); | 
					
						
							|  |  |  | err_init: | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void vhost_vdpa_cleanup(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     struct vhost_dev *dev = &s->vhost_net->dev; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     qemu_vfree(s->cvq_cmd_out_buffer); | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |     qemu_vfree(s->status); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     if (dev->vq_index + dev->nvqs == dev->vq_index_end) { | 
					
						
							|  |  |  |         g_clear_pointer(&s->vhost_vdpa.iova_tree, vhost_iova_tree_delete); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     if (s->vhost_net) { | 
					
						
							|  |  |  |         vhost_net_cleanup(s->vhost_net); | 
					
						
							|  |  |  |         g_free(s->vhost_net); | 
					
						
							|  |  |  |         s->vhost_net = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-16 11:09:08 +08:00
										 |  |  |      if (s->vhost_vdpa.device_fd >= 0) { | 
					
						
							|  |  |  |         qemu_close(s->vhost_vdpa.device_fd); | 
					
						
							|  |  |  |         s->vhost_vdpa.device_fd = -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static bool vhost_vdpa_has_ufo(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  |     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							|  |  |  |     uint64_t features = 0; | 
					
						
							|  |  |  |     features |= (1ULL << VIRTIO_NET_F_HOST_UFO); | 
					
						
							|  |  |  |     features = vhost_net_get_features(s->vhost_net, features); | 
					
						
							|  |  |  |     return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-08 15:34:30 +02:00
										 |  |  | static bool vhost_vdpa_check_peer_type(NetClientState *nc, ObjectClass *oc, | 
					
						
							|  |  |  |                                        Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const char *driver = object_class_get_name(oc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!g_str_has_prefix(driver, "virtio-net-")) { | 
					
						
							|  |  |  |         error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*"); | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 11:16:13 +01:00
										 |  |  | /** Dummy receive in case qemu falls back to userland tap networking */ | 
					
						
							|  |  |  | static ssize_t vhost_vdpa_receive(NetClientState *nc, const uint8_t *buf, | 
					
						
							|  |  |  |                                   size_t size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | static NetClientInfo net_vhost_vdpa_info = { | 
					
						
							|  |  |  |         .type = NET_CLIENT_DRIVER_VHOST_VDPA, | 
					
						
							|  |  |  |         .size = sizeof(VhostVDPAState), | 
					
						
							| 
									
										
										
										
											2021-11-25 11:16:13 +01:00
										 |  |  |         .receive = vhost_vdpa_receive, | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |         .cleanup = vhost_vdpa_cleanup, | 
					
						
							|  |  |  |         .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, | 
					
						
							|  |  |  |         .has_ufo = vhost_vdpa_has_ufo, | 
					
						
							| 
									
										
										
										
											2021-10-08 15:34:30 +02:00
										 |  |  |         .check_peer_type = vhost_vdpa_check_peer_type, | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | static void vhost_vdpa_cvq_unmap_buf(struct vhost_vdpa *v, void *addr) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VhostIOVATree *tree = v->iova_tree; | 
					
						
							|  |  |  |     DMAMap needle = { | 
					
						
							|  |  |  |         /*
 | 
					
						
							|  |  |  |          * No need to specify size or to look for more translations since | 
					
						
							|  |  |  |          * this contiguous chunk was allocated by us. | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         .translated_addr = (hwaddr)(uintptr_t)addr, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const DMAMap *map = vhost_iova_tree_find_iova(tree, &needle); | 
					
						
							|  |  |  |     int r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (unlikely(!map)) { | 
					
						
							|  |  |  |         error_report("Cannot locate expected map"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     r = vhost_vdpa_dma_unmap(v, map->iova, map->size + 1); | 
					
						
							|  |  |  |     if (unlikely(r != 0)) { | 
					
						
							|  |  |  |         error_report("Device cannot unmap: %s(%d)", g_strerror(r), r); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:20:04 +02:00
										 |  |  |     vhost_iova_tree_remove(tree, *map); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static size_t vhost_vdpa_net_cvq_cmd_len(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * MAC_TABLE_SET is the ctrl command that produces the longer out buffer. | 
					
						
							|  |  |  |      * In buffer is always 1 byte, so it should fit here | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     return sizeof(struct virtio_net_ctrl_hdr) + | 
					
						
							|  |  |  |            2 * sizeof(struct virtio_net_ctrl_mac) + | 
					
						
							|  |  |  |            MAC_TABLE_ENTRIES * ETH_ALEN; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static size_t vhost_vdpa_net_cvq_cmd_page_len(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return ROUND_UP(vhost_vdpa_net_cvq_cmd_len(), qemu_real_host_page_size()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  | /** Map CVQ buffer. */ | 
					
						
							|  |  |  | static int vhost_vdpa_cvq_map_buf(struct vhost_vdpa *v, void *buf, size_t size, | 
					
						
							|  |  |  |                                   bool write) | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     DMAMap map = {}; | 
					
						
							|  |  |  |     int r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     map.translated_addr = (hwaddr)(uintptr_t)buf; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     map.size = size - 1; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     map.perm = write ? IOMMU_RW : IOMMU_RO, | 
					
						
							|  |  |  |     r = vhost_iova_tree_map_alloc(v->iova_tree, &map); | 
					
						
							|  |  |  |     if (unlikely(r != IOVA_OK)) { | 
					
						
							|  |  |  |         error_report("Cannot map injected element"); | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |         return r; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     r = vhost_vdpa_dma_map(v, map.iova, vhost_vdpa_net_cvq_cmd_page_len(), buf, | 
					
						
							|  |  |  |                            !write); | 
					
						
							|  |  |  |     if (unlikely(r < 0)) { | 
					
						
							|  |  |  |         goto dma_map_err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | dma_map_err: | 
					
						
							| 
									
										
										
										
											2022-08-23 20:20:04 +02:00
										 |  |  |     vhost_iova_tree_remove(v->iova_tree, map); | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     return r; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  | static int vhost_vdpa_net_cvq_start(NetClientState *nc) | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     VhostVDPAState *s; | 
					
						
							|  |  |  |     int r; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							|  |  |  |     if (!s->vhost_vdpa.shadow_vqs_enabled) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer, | 
					
						
							|  |  |  |                                vhost_vdpa_net_cvq_cmd_page_len(), false); | 
					
						
							|  |  |  |     if (unlikely(r < 0)) { | 
					
						
							|  |  |  |         return r; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |     r = vhost_vdpa_cvq_map_buf(&s->vhost_vdpa, s->status, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |                                vhost_vdpa_net_cvq_cmd_page_len(), true); | 
					
						
							|  |  |  |     if (unlikely(r < 0)) { | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     return r; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void vhost_vdpa_net_cvq_stop(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (s->vhost_vdpa.shadow_vqs_enabled) { | 
					
						
							|  |  |  |         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->cvq_cmd_out_buffer); | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |         vhost_vdpa_cvq_unmap_buf(&s->vhost_vdpa, s->status); | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  | static ssize_t vhost_vdpa_net_cvq_add(VhostVDPAState *s, size_t out_len, | 
					
						
							|  |  |  |                                       size_t in_len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Buffers for the device */ | 
					
						
							|  |  |  |     const struct iovec out = { | 
					
						
							|  |  |  |         .iov_base = s->cvq_cmd_out_buffer, | 
					
						
							|  |  |  |         .iov_len = out_len, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const struct iovec in = { | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |         .iov_base = s->status, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |         .iov_len = sizeof(virtio_net_ctrl_ack), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     VhostShadowVirtqueue *svq = g_ptr_array_index(s->vhost_vdpa.shadow_vqs, 0); | 
					
						
							|  |  |  |     int r; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     r = vhost_svq_add(svq, &out, 1, &in, 1, NULL); | 
					
						
							|  |  |  |     if (unlikely(r != 0)) { | 
					
						
							|  |  |  |         if (unlikely(r == -ENOSPC)) { | 
					
						
							|  |  |  |             qemu_log_mask(LOG_GUEST_ERROR, "%s: No space on device queue\n", | 
					
						
							|  |  |  |                           __func__); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return r; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /*
 | 
					
						
							|  |  |  |      * We can poll here since we've had BQL from the time we sent the | 
					
						
							|  |  |  |      * descriptor. Also, we need to take the answer before SVQ pulls by itself, | 
					
						
							|  |  |  |      * when BQL is released | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     return vhost_svq_poll(svq); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:15 +02:00
										 |  |  | static ssize_t vhost_vdpa_net_load_cmd(VhostVDPAState *s, uint8_t class, | 
					
						
							|  |  |  |                                        uint8_t cmd, const void *data, | 
					
						
							|  |  |  |                                        size_t data_size) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const struct virtio_net_ctrl_hdr ctrl = { | 
					
						
							|  |  |  |         .class = class, | 
					
						
							|  |  |  |         .cmd = cmd, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(data_size < vhost_vdpa_net_cvq_cmd_page_len() - sizeof(ctrl)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     memcpy(s->cvq_cmd_out_buffer, &ctrl, sizeof(ctrl)); | 
					
						
							|  |  |  |     memcpy(s->cvq_cmd_out_buffer + sizeof(ctrl), data, data_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return vhost_vdpa_net_cvq_add(s, sizeof(ctrl) + data_size, | 
					
						
							|  |  |  |                                   sizeof(virtio_net_ctrl_ack)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int vhost_vdpa_net_load_mac(VhostVDPAState *s, const VirtIONet *n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint64_t features = n->parent_obj.guest_features; | 
					
						
							|  |  |  |     if (features & BIT_ULL(VIRTIO_NET_F_CTRL_MAC_ADDR)) { | 
					
						
							|  |  |  |         ssize_t dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MAC, | 
					
						
							|  |  |  |                                                   VIRTIO_NET_CTRL_MAC_ADDR_SET, | 
					
						
							|  |  |  |                                                   n->mac, sizeof(n->mac)); | 
					
						
							|  |  |  |         if (unlikely(dev_written < 0)) { | 
					
						
							|  |  |  |             return dev_written; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return *s->status != VIRTIO_NET_OK; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:16 +02:00
										 |  |  | static int vhost_vdpa_net_load_mq(VhostVDPAState *s, | 
					
						
							|  |  |  |                                   const VirtIONet *n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     struct virtio_net_ctrl_mq mq; | 
					
						
							|  |  |  |     uint64_t features = n->parent_obj.guest_features; | 
					
						
							|  |  |  |     ssize_t dev_written; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!(features & BIT_ULL(VIRTIO_NET_F_MQ))) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     mq.virtqueue_pairs = cpu_to_le16(n->curr_queue_pairs); | 
					
						
							|  |  |  |     dev_written = vhost_vdpa_net_load_cmd(s, VIRTIO_NET_CTRL_MQ, | 
					
						
							|  |  |  |                                           VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &mq, | 
					
						
							|  |  |  |                                           sizeof(mq)); | 
					
						
							|  |  |  |     if (unlikely(dev_written < 0)) { | 
					
						
							|  |  |  |         return dev_written; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return *s->status != VIRTIO_NET_OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  | static int vhost_vdpa_net_load(NetClientState *nc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:15 +02:00
										 |  |  |     struct vhost_vdpa *v = &s->vhost_vdpa; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  |     const VirtIONet *n; | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:15 +02:00
										 |  |  |     int r; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!v->shadow_vqs_enabled) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     n = VIRTIO_NET(v->dev->vdev); | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:15 +02:00
										 |  |  |     r = vhost_vdpa_net_load_mac(s, n); | 
					
						
							|  |  |  |     if (unlikely(r < 0)) { | 
					
						
							|  |  |  |         return r; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:16 +02:00
										 |  |  |     r = vhost_vdpa_net_load_mq(s, n); | 
					
						
							|  |  |  |     if (unlikely(r)) { | 
					
						
							|  |  |  |         return r; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:32 +02:00
										 |  |  | static NetClientInfo net_vhost_vdpa_cvq_info = { | 
					
						
							|  |  |  |     .type = NET_CLIENT_DRIVER_VHOST_VDPA, | 
					
						
							|  |  |  |     .size = sizeof(VhostVDPAState), | 
					
						
							|  |  |  |     .receive = vhost_vdpa_receive, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     .start = vhost_vdpa_net_cvq_start, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:36 +02:00
										 |  |  |     .load = vhost_vdpa_net_load, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     .stop = vhost_vdpa_net_cvq_stop, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:32 +02:00
										 |  |  |     .cleanup = vhost_vdpa_cleanup, | 
					
						
							|  |  |  |     .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, | 
					
						
							|  |  |  |     .has_ufo = vhost_vdpa_has_ufo, | 
					
						
							|  |  |  |     .check_peer_type = vhost_vdpa_check_peer_type, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Do not forward commands not supported by SVQ. Otherwise, the device could | 
					
						
							|  |  |  |  * accept it and qemu would not know how to update the device model. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  | static bool vhost_vdpa_net_cvq_validate_cmd(const void *out_buf, size_t len) | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     struct virtio_net_ctrl_hdr ctrl; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     if (unlikely(len < sizeof(ctrl))) { | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |         qemu_log_mask(LOG_GUEST_ERROR, | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |                       "%s: invalid legnth of out buffer %zu\n", __func__, len); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     memcpy(&ctrl, out_buf, sizeof(ctrl)); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     switch (ctrl.class) { | 
					
						
							|  |  |  |     case VIRTIO_NET_CTRL_MAC: | 
					
						
							|  |  |  |         switch (ctrl.cmd) { | 
					
						
							|  |  |  |         case VIRTIO_NET_CTRL_MAC_ADDR_SET: | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mac cmd %u\n", | 
					
						
							|  |  |  |                           __func__, ctrl.cmd); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:17 +02:00
										 |  |  |     case VIRTIO_NET_CTRL_MQ: | 
					
						
							|  |  |  |         switch (ctrl.cmd) { | 
					
						
							|  |  |  |         case VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET: | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid mq cmd %u\n", | 
					
						
							|  |  |  |                           __func__, ctrl.cmd); | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     default: | 
					
						
							|  |  |  |         qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid control class %u\n", | 
					
						
							|  |  |  |                       __func__, ctrl.class); | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Validate and copy control virtqueue commands. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Following QEMU guidelines, we offer a copy of the buffers to the device to | 
					
						
							|  |  |  |  * prevent TOCTOU bugs. | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | static int vhost_vdpa_net_handle_ctrl_avail(VhostShadowVirtqueue *svq, | 
					
						
							|  |  |  |                                             VirtQueueElement *elem, | 
					
						
							|  |  |  |                                             void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     VhostVDPAState *s = opaque; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |     size_t in_len; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |     virtio_net_ctrl_ack status = VIRTIO_NET_ERR; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     /* Out buffer sent to both the vdpa device and the device model */ | 
					
						
							|  |  |  |     struct iovec out = { | 
					
						
							|  |  |  |         .iov_base = s->cvq_cmd_out_buffer, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     /* in buffer used for device model */ | 
					
						
							|  |  |  |     const struct iovec in = { | 
					
						
							|  |  |  |         .iov_base = &status, | 
					
						
							|  |  |  |         .iov_len = sizeof(status), | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |     ssize_t dev_written = -EINVAL; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     bool ok; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     out.iov_len = iov_to_buf(elem->out_sg, elem->out_num, 0, | 
					
						
							|  |  |  |                              s->cvq_cmd_out_buffer, | 
					
						
							|  |  |  |                              vhost_vdpa_net_cvq_cmd_len()); | 
					
						
							|  |  |  |     ok = vhost_vdpa_net_cvq_validate_cmd(s->cvq_cmd_out_buffer, out.iov_len); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     if (unlikely(!ok)) { | 
					
						
							|  |  |  |         goto out; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |     dev_written = vhost_vdpa_net_cvq_add(s, out.iov_len, sizeof(status)); | 
					
						
							|  |  |  |     if (unlikely(dev_written < 0)) { | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |         goto out; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (unlikely(dev_written < sizeof(status))) { | 
					
						
							|  |  |  |         error_report("Insufficient written data (%zu)", dev_written); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |         goto out; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |     if (*s->status != VIRTIO_NET_OK) { | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |         return VIRTIO_NET_ERR; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     status = VIRTIO_NET_ERR; | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:33 +02:00
										 |  |  |     virtio_net_handle_ctrl_iov(svq->vdev, &in, 1, &out, 1); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |     if (status != VIRTIO_NET_OK) { | 
					
						
							|  |  |  |         error_report("Bad CVQ processing in model"); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | out: | 
					
						
							|  |  |  |     in_len = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, | 
					
						
							|  |  |  |                           sizeof(status)); | 
					
						
							|  |  |  |     if (unlikely(in_len < sizeof(status))) { | 
					
						
							|  |  |  |         error_report("Bad device CVQ written length"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     vhost_svq_push_elem(svq, elem, MIN(in_len, sizeof(status))); | 
					
						
							|  |  |  |     g_free(elem); | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:34 +02:00
										 |  |  |     return dev_written < 0 ? dev_written : 0; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const VhostShadowVirtqueueOps vhost_vdpa_net_svq_ops = { | 
					
						
							|  |  |  |     .avail_handler = vhost_vdpa_net_handle_ctrl_avail, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:54 +08:00
										 |  |  | static NetClientState *net_vhost_vdpa_init(NetClientState *peer, | 
					
						
							|  |  |  |                                            const char *device, | 
					
						
							|  |  |  |                                            const char *name, | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |                                            int vdpa_device_fd, | 
					
						
							|  |  |  |                                            int queue_pair_index, | 
					
						
							|  |  |  |                                            int nvqs, | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |                                            bool is_datapath, | 
					
						
							|  |  |  |                                            bool svq, | 
					
						
							|  |  |  |                                            VhostIOVATree *iova_tree) | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     NetClientState *nc = NULL; | 
					
						
							|  |  |  |     VhostVDPAState *s; | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							|  |  |  |     assert(name); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     if (is_datapath) { | 
					
						
							|  |  |  |         nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device, | 
					
						
							|  |  |  |                                  name); | 
					
						
							|  |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2022-08-23 20:30:32 +02:00
										 |  |  |         nc = qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer, | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |                                          device, name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-02 11:03:33 +08:00
										 |  |  |     snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     s = DO_UPCAST(VhostVDPAState, nc, nc); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  |     s->vhost_vdpa.device_fd = vdpa_device_fd; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     s->vhost_vdpa.index = queue_pair_index; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     s->vhost_vdpa.shadow_vqs_enabled = svq; | 
					
						
							|  |  |  |     s->vhost_vdpa.iova_tree = iova_tree; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |     if (!is_datapath) { | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  |         s->cvq_cmd_out_buffer = qemu_memalign(qemu_real_host_page_size(), | 
					
						
							|  |  |  |                                             vhost_vdpa_net_cvq_cmd_page_len()); | 
					
						
							|  |  |  |         memset(s->cvq_cmd_out_buffer, 0, vhost_vdpa_net_cvq_cmd_page_len()); | 
					
						
							| 
									
										
										
										
											2022-09-06 17:07:14 +02:00
										 |  |  |         s->status = qemu_memalign(qemu_real_host_page_size(), | 
					
						
							|  |  |  |                                   vhost_vdpa_net_cvq_cmd_page_len()); | 
					
						
							|  |  |  |         memset(s->status, 0, vhost_vdpa_net_cvq_cmd_page_len()); | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:43 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:42 +02:00
										 |  |  |         s->vhost_vdpa.shadow_vq_ops = &vhost_vdpa_net_svq_ops; | 
					
						
							|  |  |  |         s->vhost_vdpa.shadow_vq_ops_opaque = s; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs); | 
					
						
							| 
									
										
										
										
											2021-09-03 17:10:20 +08:00
										 |  |  |     if (ret) { | 
					
						
							|  |  |  |         qemu_del_net_client(nc); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:54 +08:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2021-09-03 17:10:20 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:54 +08:00
										 |  |  |     return nc; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  | static int vhost_vdpa_get_iova_range(int fd, | 
					
						
							|  |  |  |                                      struct vhost_vdpa_iova_range *iova_range) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = ioctl(fd, VHOST_VDPA_GET_IOVA_RANGE, iova_range); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret < 0 ? -errno : 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:44 +02:00
										 |  |  | static int vhost_vdpa_get_features(int fd, uint64_t *features, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret = ioctl(fd, VHOST_GET_FEATURES, features); | 
					
						
							|  |  |  |     if (unlikely(ret < 0)) { | 
					
						
							|  |  |  |         error_setg_errno(errp, errno, | 
					
						
							|  |  |  |                          "Fail to query features from vhost-vDPA device"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int vhost_vdpa_get_max_queue_pairs(int fd, uint64_t features, | 
					
						
							|  |  |  |                                           int *has_cvq, Error **errp) | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned long config_size = offsetof(struct vhost_vdpa_config, buf); | 
					
						
							| 
									
										
										
										
											2021-11-02 16:51:57 +01:00
										 |  |  |     g_autofree struct vhost_vdpa_config *config = NULL; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     __virtio16 *max_queue_pairs; | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (features & (1 << VIRTIO_NET_F_CTRL_VQ)) { | 
					
						
							|  |  |  |         *has_cvq = 1; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         *has_cvq = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (features & (1 << VIRTIO_NET_F_MQ)) { | 
					
						
							|  |  |  |         config = g_malloc0(config_size + sizeof(*max_queue_pairs)); | 
					
						
							|  |  |  |         config->off = offsetof(struct virtio_net_config, max_virtqueue_pairs); | 
					
						
							|  |  |  |         config->len = sizeof(*max_queue_pairs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ret = ioctl(fd, VHOST_VDPA_GET_CONFIG, config); | 
					
						
							|  |  |  |         if (ret) { | 
					
						
							|  |  |  |             error_setg(errp, "Fail to get config from vhost-vDPA device"); | 
					
						
							|  |  |  |             return -ret; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         max_queue_pairs = (__virtio16 *)&config->buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return lduw_le_p(max_queue_pairs); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | int net_init_vhost_vdpa(const Netdev *netdev, const char *name, | 
					
						
							|  |  |  |                         NetClientState *peer, Error **errp) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const NetdevVhostVDPAOptions *opts; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:44 +02:00
										 |  |  |     uint64_t features; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:54 +08:00
										 |  |  |     int vdpa_device_fd; | 
					
						
							| 
									
										
										
										
											2022-02-14 20:34:15 +01:00
										 |  |  |     g_autofree NetClientState **ncs = NULL; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     g_autoptr(VhostIOVATree) iova_tree = NULL; | 
					
						
							| 
									
										
										
										
											2022-02-14 20:34:15 +01:00
										 |  |  |     NetClientState *nc; | 
					
						
							| 
									
										
										
										
											2022-08-02 13:24:46 +02:00
										 |  |  |     int queue_pairs, r, i = 0, has_cvq = 0; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA); | 
					
						
							|  |  |  |     opts = &netdev->u.vhost_vdpa; | 
					
						
							| 
									
										
										
										
											2021-11-12 20:34:31 +01:00
										 |  |  |     if (!opts->vhostdev) { | 
					
						
							|  |  |  |         error_setg(errp, "vdpa character device not specified with vhostdev"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-12 20:34:30 +01:00
										 |  |  |     vdpa_device_fd = qemu_open(opts->vhostdev, O_RDWR, errp); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:51 +08:00
										 |  |  |     if (vdpa_device_fd == -1) { | 
					
						
							|  |  |  |         return -errno; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:44 +02:00
										 |  |  |     r = vhost_vdpa_get_features(vdpa_device_fd, &features, errp); | 
					
						
							|  |  |  |     if (unlikely(r < 0)) { | 
					
						
							| 
									
										
										
										
											2022-08-02 13:24:46 +02:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     queue_pairs = vhost_vdpa_get_max_queue_pairs(vdpa_device_fd, features, | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |                                                  &has_cvq, errp); | 
					
						
							|  |  |  |     if (queue_pairs < 0) { | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:51 +08:00
										 |  |  |         qemu_close(vdpa_device_fd); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |         return queue_pairs; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     if (opts->x_svq) { | 
					
						
							|  |  |  |         struct vhost_vdpa_iova_range iova_range; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         uint64_t invalid_dev_features = | 
					
						
							|  |  |  |             features & ~vdpa_svq_device_features & | 
					
						
							|  |  |  |             /* Transport are all accepted at this point */ | 
					
						
							|  |  |  |             ~MAKE_64BIT_MASK(VIRTIO_TRANSPORT_F_START, | 
					
						
							|  |  |  |                              VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (invalid_dev_features) { | 
					
						
							|  |  |  |             error_setg(errp, "vdpa svq does not work with features 0x%" PRIx64, | 
					
						
							|  |  |  |                        invalid_dev_features); | 
					
						
							|  |  |  |             goto err_svq; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         vhost_vdpa_get_iova_range(vdpa_device_fd, &iova_range); | 
					
						
							|  |  |  |         iova_tree = vhost_iova_tree_new(iova_range.first, iova_range.last); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     ncs = g_malloc0(sizeof(*ncs) * queue_pairs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < queue_pairs; i++) { | 
					
						
							|  |  |  |         ncs[i] = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |                                      vdpa_device_fd, i, 2, true, opts->x_svq, | 
					
						
							|  |  |  |                                      iova_tree); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |         if (!ncs[i]) | 
					
						
							|  |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     if (has_cvq) { | 
					
						
							|  |  |  |         nc = net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |                                  vdpa_device_fd, i, 1, false, | 
					
						
							|  |  |  |                                  opts->x_svq, iova_tree); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |         if (!nc) | 
					
						
							|  |  |  |             goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  |     /* iova_tree ownership belongs to last NetClientState */ | 
					
						
							|  |  |  |     g_steal_pointer(&iova_tree); | 
					
						
							| 
									
										
										
										
											2021-10-20 12:55:54 +08:00
										 |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | err: | 
					
						
							|  |  |  |     if (i) { | 
					
						
							| 
									
										
										
										
											2022-05-06 19:28:14 -07:00
										 |  |  |         for (i--; i >= 0; i--) { | 
					
						
							|  |  |  |             qemu_del_net_client(ncs[i]); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-20 08:59:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | err_svq: | 
					
						
							| 
									
										
										
										
											2021-10-20 12:56:00 +08:00
										 |  |  |     qemu_close(vdpa_device_fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2020-07-01 22:55:38 +08:00
										 |  |  | } |