1 /* 2 * vhost-vdpa.c 3 * 4 * Copyright(c) 2017-2018 Intel Corporation. 5 * Copyright(c) 2020 Red Hat, Inc. 6 * 7 * This work is licensed under the terms of the GNU GPL, version 2 or later. 8 * See the COPYING file in the top-level directory. 9 * 10 */ 11 12 #include "qemu/osdep.h" 13 #include "clients.h" 14 #include "net/vhost_net.h" 15 #include "net/vhost-vdpa.h" 16 #include "hw/virtio/vhost-vdpa.h" 17 #include "qemu/config-file.h" 18 #include "qemu/error-report.h" 19 #include "qemu/option.h" 20 #include "qapi/error.h" 21 #include <sys/ioctl.h> 22 #include <err.h> 23 #include "standard-headers/linux/virtio_net.h" 24 #include "monitor/monitor.h" 25 #include "hw/virtio/vhost.h" 26 27 /* Todo:need to add the multiqueue support here */ 28 typedef struct VhostVDPAState { 29 NetClientState nc; 30 struct vhost_vdpa vhost_vdpa; 31 VHostNetState *vhost_net; 32 uint64_t acked_features; 33 bool started; 34 } VhostVDPAState; 35 36 const int vdpa_feature_bits[] = { 37 VIRTIO_F_NOTIFY_ON_EMPTY, 38 VIRTIO_RING_F_INDIRECT_DESC, 39 VIRTIO_RING_F_EVENT_IDX, 40 VIRTIO_F_ANY_LAYOUT, 41 VIRTIO_F_VERSION_1, 42 VIRTIO_NET_F_CSUM, 43 VIRTIO_NET_F_GUEST_CSUM, 44 VIRTIO_NET_F_GSO, 45 VIRTIO_NET_F_GUEST_TSO4, 46 VIRTIO_NET_F_GUEST_TSO6, 47 VIRTIO_NET_F_GUEST_ECN, 48 VIRTIO_NET_F_GUEST_UFO, 49 VIRTIO_NET_F_HOST_TSO4, 50 VIRTIO_NET_F_HOST_TSO6, 51 VIRTIO_NET_F_HOST_ECN, 52 VIRTIO_NET_F_HOST_UFO, 53 VIRTIO_NET_F_MRG_RXBUF, 54 VIRTIO_NET_F_MTU, 55 VIRTIO_F_IOMMU_PLATFORM, 56 VIRTIO_F_RING_PACKED, 57 VIRTIO_NET_F_RSS, 58 VIRTIO_NET_F_HASH_REPORT, 59 VIRTIO_NET_F_GUEST_ANNOUNCE, 60 VIRTIO_NET_F_STATUS, 61 VHOST_INVALID_FEATURE_BIT 62 }; 63 64 VHostNetState *vhost_vdpa_get_vhost_net(NetClientState *nc) 65 { 66 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 67 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 68 return s->vhost_net; 69 } 70 71 uint64_t vhost_vdpa_get_acked_features(NetClientState *nc) 72 { 73 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 74 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 75 s->acked_features = vhost_net_get_acked_features(s->vhost_net); 76 77 return s->acked_features; 78 } 79 80 static int vhost_vdpa_net_check_device_id(struct vhost_net *net) 81 { 82 uint32_t device_id; 83 int ret; 84 struct vhost_dev *hdev; 85 86 hdev = (struct vhost_dev *)&net->dev; 87 ret = hdev->vhost_ops->vhost_get_device_id(hdev, &device_id); 88 if (device_id != VIRTIO_ID_NET) { 89 return -ENOTSUP; 90 } 91 return ret; 92 } 93 94 static void vhost_vdpa_del(NetClientState *ncs) 95 { 96 VhostVDPAState *s; 97 assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 98 s = DO_UPCAST(VhostVDPAState, nc, ncs); 99 if (s->vhost_net) { 100 vhost_net_cleanup(s->vhost_net); 101 } 102 } 103 104 static int vhost_vdpa_add(NetClientState *ncs, void *be) 105 { 106 VhostNetOptions options; 107 struct vhost_net *net = NULL; 108 VhostVDPAState *s; 109 int ret; 110 111 options.backend_type = VHOST_BACKEND_TYPE_VDPA; 112 assert(ncs->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 113 s = DO_UPCAST(VhostVDPAState, nc, ncs); 114 options.net_backend = ncs; 115 options.opaque = be; 116 options.busyloop_timeout = 0; 117 118 net = vhost_net_init(&options); 119 if (!net) { 120 error_report("failed to init vhost_net for queue"); 121 goto err; 122 } 123 if (s->vhost_net) { 124 vhost_net_cleanup(s->vhost_net); 125 g_free(s->vhost_net); 126 } 127 s->vhost_net = net; 128 ret = vhost_vdpa_net_check_device_id(net); 129 if (ret) { 130 goto err; 131 } 132 return 0; 133 err: 134 if (net) { 135 vhost_net_cleanup(net); 136 } 137 vhost_vdpa_del(ncs); 138 return -1; 139 } 140 141 static void vhost_vdpa_cleanup(NetClientState *nc) 142 { 143 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 144 145 if (s->vhost_net) { 146 vhost_net_cleanup(s->vhost_net); 147 g_free(s->vhost_net); 148 s->vhost_net = NULL; 149 } 150 if (s->vhost_vdpa.device_fd >= 0) { 151 qemu_close(s->vhost_vdpa.device_fd); 152 s->vhost_vdpa.device_fd = -1; 153 } 154 } 155 156 static bool vhost_vdpa_has_vnet_hdr(NetClientState *nc) 157 { 158 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 159 160 return true; 161 } 162 163 static bool vhost_vdpa_has_ufo(NetClientState *nc) 164 { 165 assert(nc->info->type == NET_CLIENT_DRIVER_VHOST_VDPA); 166 VhostVDPAState *s = DO_UPCAST(VhostVDPAState, nc, nc); 167 uint64_t features = 0; 168 features |= (1ULL << VIRTIO_NET_F_HOST_UFO); 169 features = vhost_net_get_features(s->vhost_net, features); 170 return !!(features & (1ULL << VIRTIO_NET_F_HOST_UFO)); 171 172 } 173 174 static NetClientInfo net_vhost_vdpa_info = { 175 .type = NET_CLIENT_DRIVER_VHOST_VDPA, 176 .size = sizeof(VhostVDPAState), 177 .cleanup = vhost_vdpa_cleanup, 178 .has_vnet_hdr = vhost_vdpa_has_vnet_hdr, 179 .has_ufo = vhost_vdpa_has_ufo, 180 }; 181 182 static int net_vhost_vdpa_init(NetClientState *peer, const char *device, 183 const char *name, const char *vhostdev) 184 { 185 NetClientState *nc = NULL; 186 VhostVDPAState *s; 187 int vdpa_device_fd = -1; 188 int ret = 0; 189 assert(name); 190 nc = qemu_new_net_client(&net_vhost_vdpa_info, peer, device, name); 191 snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); 192 nc->queue_index = 0; 193 s = DO_UPCAST(VhostVDPAState, nc, nc); 194 vdpa_device_fd = qemu_open_old(vhostdev, O_RDWR); 195 if (vdpa_device_fd == -1) { 196 return -errno; 197 } 198 s->vhost_vdpa.device_fd = vdpa_device_fd; 199 ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa); 200 assert(s->vhost_net); 201 return ret; 202 } 203 204 static int net_vhost_check_net(void *opaque, QemuOpts *opts, Error **errp) 205 { 206 const char *name = opaque; 207 const char *driver, *netdev; 208 209 driver = qemu_opt_get(opts, "driver"); 210 netdev = qemu_opt_get(opts, "netdev"); 211 if (!driver || !netdev) { 212 return 0; 213 } 214 if (strcmp(netdev, name) == 0 && 215 !g_str_has_prefix(driver, "virtio-net-")) { 216 error_setg(errp, "vhost-vdpa requires frontend driver virtio-net-*"); 217 return -1; 218 } 219 return 0; 220 } 221 222 int net_init_vhost_vdpa(const Netdev *netdev, const char *name, 223 NetClientState *peer, Error **errp) 224 { 225 const NetdevVhostVDPAOptions *opts; 226 227 assert(netdev->type == NET_CLIENT_DRIVER_VHOST_VDPA); 228 opts = &netdev->u.vhost_vdpa; 229 /* verify net frontend */ 230 if (qemu_opts_foreach(qemu_find_opts("device"), net_vhost_check_net, 231 (char *)name, errp)) { 232 return -1; 233 } 234 return net_vhost_vdpa_init(peer, TYPE_VHOST_VDPA, name, opts->vhostdev); 235 } 236