1 /* 2 * vhost-net support 3 * 4 * Copyright Red Hat, Inc. 2010 5 * 6 * Authors: 7 * Michael S. Tsirkin <mst@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 * Contributions after 2012-01-13 are licensed under the terms of the 13 * GNU GPL, version 2 or (at your option) any later version. 14 */ 15 16 #include "net/net.h" 17 #include "net/tap.h" 18 #include "net/vhost-user.h" 19 20 #include "hw/virtio/virtio-net.h" 21 #include "net/vhost_net.h" 22 #include "qemu/error-report.h" 23 24 #include "config.h" 25 26 #ifdef CONFIG_VHOST_NET 27 #include <linux/vhost.h> 28 #include <sys/socket.h> 29 #include <linux/kvm.h> 30 #include <fcntl.h> 31 #include <netpacket/packet.h> 32 #include <net/ethernet.h> 33 #include <net/if.h> 34 #include <netinet/in.h> 35 36 #include <stdio.h> 37 38 #include "standard-headers/linux/virtio_ring.h" 39 #include "hw/virtio/vhost.h" 40 #include "hw/virtio/virtio-bus.h" 41 42 struct vhost_net { 43 struct vhost_dev dev; 44 struct vhost_virtqueue vqs[2]; 45 int backend; 46 NetClientState *nc; 47 }; 48 49 /* Features supported by host kernel. */ 50 static const int kernel_feature_bits[] = { 51 VIRTIO_F_NOTIFY_ON_EMPTY, 52 VIRTIO_RING_F_INDIRECT_DESC, 53 VIRTIO_RING_F_EVENT_IDX, 54 VIRTIO_NET_F_MRG_RXBUF, 55 VIRTIO_F_VERSION_1, 56 VHOST_INVALID_FEATURE_BIT 57 }; 58 59 /* Features supported by others. */ 60 static const int user_feature_bits[] = { 61 VIRTIO_F_NOTIFY_ON_EMPTY, 62 VIRTIO_RING_F_INDIRECT_DESC, 63 VIRTIO_RING_F_EVENT_IDX, 64 65 VIRTIO_F_ANY_LAYOUT, 66 VIRTIO_F_VERSION_1, 67 VIRTIO_NET_F_CSUM, 68 VIRTIO_NET_F_GUEST_CSUM, 69 VIRTIO_NET_F_GSO, 70 VIRTIO_NET_F_GUEST_TSO4, 71 VIRTIO_NET_F_GUEST_TSO6, 72 VIRTIO_NET_F_GUEST_ECN, 73 VIRTIO_NET_F_GUEST_UFO, 74 VIRTIO_NET_F_HOST_TSO4, 75 VIRTIO_NET_F_HOST_TSO6, 76 VIRTIO_NET_F_HOST_ECN, 77 VIRTIO_NET_F_HOST_UFO, 78 VIRTIO_NET_F_MRG_RXBUF, 79 VIRTIO_NET_F_STATUS, 80 VIRTIO_NET_F_CTRL_VQ, 81 VIRTIO_NET_F_CTRL_RX, 82 VIRTIO_NET_F_CTRL_VLAN, 83 VIRTIO_NET_F_CTRL_RX_EXTRA, 84 VIRTIO_NET_F_CTRL_MAC_ADDR, 85 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 86 87 VIRTIO_NET_F_MQ, 88 89 VHOST_INVALID_FEATURE_BIT 90 }; 91 92 static const int *vhost_net_get_feature_bits(struct vhost_net *net) 93 { 94 const int *feature_bits = 0; 95 96 switch (net->nc->info->type) { 97 case NET_CLIENT_OPTIONS_KIND_TAP: 98 feature_bits = kernel_feature_bits; 99 break; 100 case NET_CLIENT_OPTIONS_KIND_VHOST_USER: 101 feature_bits = user_feature_bits; 102 break; 103 default: 104 error_report("Feature bits not defined for this type: %d", 105 net->nc->info->type); 106 break; 107 } 108 109 return feature_bits; 110 } 111 112 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) 113 { 114 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net), 115 features); 116 } 117 118 void vhost_net_ack_features(struct vhost_net *net, uint64_t features) 119 { 120 net->dev.acked_features = net->dev.backend_features; 121 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features); 122 } 123 124 static int vhost_net_get_fd(NetClientState *backend) 125 { 126 switch (backend->info->type) { 127 case NET_CLIENT_OPTIONS_KIND_TAP: 128 return tap_get_fd(backend); 129 default: 130 fprintf(stderr, "vhost-net requires tap backend\n"); 131 return -EBADFD; 132 } 133 } 134 135 struct vhost_net *vhost_net_init(VhostNetOptions *options) 136 { 137 int r; 138 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL; 139 struct vhost_net *net = g_malloc(sizeof *net); 140 141 if (!options->net_backend) { 142 fprintf(stderr, "vhost-net requires net backend to be setup\n"); 143 goto fail; 144 } 145 146 if (backend_kernel) { 147 r = vhost_net_get_fd(options->net_backend); 148 if (r < 0) { 149 goto fail; 150 } 151 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) 152 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR); 153 net->backend = r; 154 } else { 155 net->dev.backend_features = 0; 156 net->backend = -1; 157 } 158 net->nc = options->net_backend; 159 160 net->dev.nvqs = 2; 161 net->dev.vqs = net->vqs; 162 net->dev.vq_index = net->nc->queue_index; 163 164 r = vhost_dev_init(&net->dev, options->opaque, 165 options->backend_type, options->force); 166 if (r < 0) { 167 goto fail; 168 } 169 if (backend_kernel) { 170 if (!qemu_has_vnet_hdr_len(options->net_backend, 171 sizeof(struct virtio_net_hdr_mrg_rxbuf))) { 172 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF); 173 } 174 if (~net->dev.features & net->dev.backend_features) { 175 fprintf(stderr, "vhost lacks feature mask %" PRIu64 176 " for backend\n", 177 (uint64_t)(~net->dev.features & net->dev.backend_features)); 178 vhost_dev_cleanup(&net->dev); 179 goto fail; 180 } 181 } 182 /* Set sane init value. Override when guest acks. */ 183 vhost_net_ack_features(net, 0); 184 return net; 185 fail: 186 g_free(net); 187 return NULL; 188 } 189 190 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev) 191 { 192 return vhost_dev_query(&net->dev, dev); 193 } 194 195 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index) 196 { 197 net->dev.vq_index = vq_index; 198 } 199 200 static int vhost_net_start_one(struct vhost_net *net, 201 VirtIODevice *dev) 202 { 203 struct vhost_vring_file file = { }; 204 int r; 205 206 net->dev.nvqs = 2; 207 net->dev.vqs = net->vqs; 208 209 r = vhost_dev_enable_notifiers(&net->dev, dev); 210 if (r < 0) { 211 goto fail_notifiers; 212 } 213 214 r = vhost_dev_start(&net->dev, dev); 215 if (r < 0) { 216 goto fail_start; 217 } 218 219 if (net->nc->info->poll) { 220 net->nc->info->poll(net->nc, false); 221 } 222 223 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) { 224 qemu_set_fd_handler(net->backend, NULL, NULL, NULL); 225 file.fd = net->backend; 226 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 227 const VhostOps *vhost_ops = net->dev.vhost_ops; 228 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 229 &file); 230 if (r < 0) { 231 r = -errno; 232 goto fail; 233 } 234 } 235 } 236 return 0; 237 fail: 238 file.fd = -1; 239 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) { 240 while (file.index-- > 0) { 241 const VhostOps *vhost_ops = net->dev.vhost_ops; 242 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 243 &file); 244 assert(r >= 0); 245 } 246 } 247 if (net->nc->info->poll) { 248 net->nc->info->poll(net->nc, true); 249 } 250 vhost_dev_stop(&net->dev, dev); 251 fail_start: 252 vhost_dev_disable_notifiers(&net->dev, dev); 253 fail_notifiers: 254 return r; 255 } 256 257 static void vhost_net_stop_one(struct vhost_net *net, 258 VirtIODevice *dev) 259 { 260 struct vhost_vring_file file = { .fd = -1 }; 261 262 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) { 263 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 264 const VhostOps *vhost_ops = net->dev.vhost_ops; 265 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 266 &file); 267 assert(r >= 0); 268 } 269 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) { 270 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 271 const VhostOps *vhost_ops = net->dev.vhost_ops; 272 int r = vhost_ops->vhost_call(&net->dev, VHOST_RESET_OWNER, 273 &file); 274 assert(r >= 0); 275 } 276 } 277 if (net->nc->info->poll) { 278 net->nc->info->poll(net->nc, true); 279 } 280 vhost_dev_stop(&net->dev, dev); 281 vhost_dev_disable_notifiers(&net->dev, dev); 282 } 283 284 static bool vhost_net_device_endian_ok(VirtIODevice *vdev) 285 { 286 #ifdef TARGET_IS_BIENDIAN 287 #ifdef HOST_WORDS_BIGENDIAN 288 return virtio_is_big_endian(vdev); 289 #else 290 return !virtio_is_big_endian(vdev); 291 #endif 292 #else 293 return true; 294 #endif 295 } 296 297 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, 298 int total_queues) 299 { 300 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 301 VirtioBusState *vbus = VIRTIO_BUS(qbus); 302 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 303 int r, e, i; 304 305 if (!vhost_net_device_endian_ok(dev)) { 306 error_report("vhost-net does not support cross-endian"); 307 r = -ENOSYS; 308 goto err; 309 } 310 311 if (!k->set_guest_notifiers) { 312 error_report("binding does not support guest notifiers"); 313 r = -ENOSYS; 314 goto err; 315 } 316 317 for (i = 0; i < total_queues; i++) { 318 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2); 319 } 320 321 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); 322 if (r < 0) { 323 error_report("Error binding guest notifier: %d", -r); 324 goto err; 325 } 326 327 for (i = 0; i < total_queues; i++) { 328 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev); 329 330 if (r < 0) { 331 goto err_start; 332 } 333 } 334 335 return 0; 336 337 err_start: 338 while (--i >= 0) { 339 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 340 } 341 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 342 if (e < 0) { 343 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); 344 fflush(stderr); 345 } 346 err: 347 return r; 348 } 349 350 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, 351 int total_queues) 352 { 353 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 354 VirtioBusState *vbus = VIRTIO_BUS(qbus); 355 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 356 int i, r; 357 358 for (i = 0; i < total_queues; i++) { 359 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 360 } 361 362 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 363 if (r < 0) { 364 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); 365 fflush(stderr); 366 } 367 assert(r >= 0); 368 } 369 370 void vhost_net_cleanup(struct vhost_net *net) 371 { 372 vhost_dev_cleanup(&net->dev); 373 g_free(net); 374 } 375 376 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 377 { 378 return vhost_virtqueue_pending(&net->dev, idx); 379 } 380 381 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 382 int idx, bool mask) 383 { 384 vhost_virtqueue_mask(&net->dev, dev, idx, mask); 385 } 386 387 VHostNetState *get_vhost_net(NetClientState *nc) 388 { 389 VHostNetState *vhost_net = 0; 390 391 if (!nc) { 392 return 0; 393 } 394 395 switch (nc->info->type) { 396 case NET_CLIENT_OPTIONS_KIND_TAP: 397 vhost_net = tap_get_vhost_net(nc); 398 break; 399 case NET_CLIENT_OPTIONS_KIND_VHOST_USER: 400 vhost_net = vhost_user_get_vhost_net(nc); 401 break; 402 default: 403 break; 404 } 405 406 return vhost_net; 407 } 408 #else 409 struct vhost_net *vhost_net_init(VhostNetOptions *options) 410 { 411 error_report("vhost-net support is not compiled in"); 412 return NULL; 413 } 414 415 bool vhost_net_query(VHostNetState *net, VirtIODevice *dev) 416 { 417 return false; 418 } 419 420 int vhost_net_start(VirtIODevice *dev, 421 NetClientState *ncs, 422 int total_queues) 423 { 424 return -ENOSYS; 425 } 426 void vhost_net_stop(VirtIODevice *dev, 427 NetClientState *ncs, 428 int total_queues) 429 { 430 } 431 432 void vhost_net_cleanup(struct vhost_net *net) 433 { 434 } 435 436 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) 437 { 438 return features; 439 } 440 void vhost_net_ack_features(struct vhost_net *net, uint64_t features) 441 { 442 } 443 444 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 445 { 446 return false; 447 } 448 449 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 450 int idx, bool mask) 451 { 452 } 453 454 VHostNetState *get_vhost_net(NetClientState *nc) 455 { 456 return 0; 457 } 458 #endif 459