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 "qemu/osdep.h" 17 #include "net/net.h" 18 #include "net/tap.h" 19 #include "net/vhost-user.h" 20 21 #include "hw/virtio/virtio-net.h" 22 #include "net/vhost_net.h" 23 #include "qemu/error-report.h" 24 25 26 #ifdef CONFIG_VHOST_NET 27 #include <linux/vhost.h> 28 #include <sys/socket.h> 29 #include <linux/kvm.h> 30 #include <netpacket/packet.h> 31 #include <net/ethernet.h> 32 #include <net/if.h> 33 #include <netinet/in.h> 34 35 36 #include "standard-headers/linux/virtio_ring.h" 37 #include "hw/virtio/vhost.h" 38 #include "hw/virtio/virtio-bus.h" 39 40 struct vhost_net { 41 struct vhost_dev dev; 42 struct vhost_virtqueue vqs[2]; 43 int backend; 44 NetClientState *nc; 45 }; 46 47 /* Features supported by host kernel. */ 48 static const int kernel_feature_bits[] = { 49 VIRTIO_F_NOTIFY_ON_EMPTY, 50 VIRTIO_RING_F_INDIRECT_DESC, 51 VIRTIO_RING_F_EVENT_IDX, 52 VIRTIO_NET_F_MRG_RXBUF, 53 VIRTIO_F_VERSION_1, 54 VIRTIO_NET_F_MTU, 55 VHOST_INVALID_FEATURE_BIT 56 }; 57 58 /* Features supported by others. */ 59 static const int user_feature_bits[] = { 60 VIRTIO_F_NOTIFY_ON_EMPTY, 61 VIRTIO_RING_F_INDIRECT_DESC, 62 VIRTIO_RING_F_EVENT_IDX, 63 64 VIRTIO_F_ANY_LAYOUT, 65 VIRTIO_F_VERSION_1, 66 VIRTIO_NET_F_CSUM, 67 VIRTIO_NET_F_GUEST_CSUM, 68 VIRTIO_NET_F_GSO, 69 VIRTIO_NET_F_GUEST_TSO4, 70 VIRTIO_NET_F_GUEST_TSO6, 71 VIRTIO_NET_F_GUEST_ECN, 72 VIRTIO_NET_F_GUEST_UFO, 73 VIRTIO_NET_F_HOST_TSO4, 74 VIRTIO_NET_F_HOST_TSO6, 75 VIRTIO_NET_F_HOST_ECN, 76 VIRTIO_NET_F_HOST_UFO, 77 VIRTIO_NET_F_MRG_RXBUF, 78 VIRTIO_NET_F_MTU, 79 80 /* This bit implies RARP isn't sent by QEMU out of band */ 81 VIRTIO_NET_F_GUEST_ANNOUNCE, 82 83 VIRTIO_NET_F_MQ, 84 85 VHOST_INVALID_FEATURE_BIT 86 }; 87 88 static const int *vhost_net_get_feature_bits(struct vhost_net *net) 89 { 90 const int *feature_bits = 0; 91 92 switch (net->nc->info->type) { 93 case NET_CLIENT_DRIVER_TAP: 94 feature_bits = kernel_feature_bits; 95 break; 96 case NET_CLIENT_DRIVER_VHOST_USER: 97 feature_bits = user_feature_bits; 98 break; 99 default: 100 error_report("Feature bits not defined for this type: %d", 101 net->nc->info->type); 102 break; 103 } 104 105 return feature_bits; 106 } 107 108 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) 109 { 110 return vhost_get_features(&net->dev, vhost_net_get_feature_bits(net), 111 features); 112 } 113 114 void vhost_net_ack_features(struct vhost_net *net, uint64_t features) 115 { 116 net->dev.acked_features = net->dev.backend_features; 117 vhost_ack_features(&net->dev, vhost_net_get_feature_bits(net), features); 118 } 119 120 uint64_t vhost_net_get_max_queues(VHostNetState *net) 121 { 122 return net->dev.max_queues; 123 } 124 125 uint64_t vhost_net_get_acked_features(VHostNetState *net) 126 { 127 return net->dev.acked_features; 128 } 129 130 static int vhost_net_get_fd(NetClientState *backend) 131 { 132 switch (backend->info->type) { 133 case NET_CLIENT_DRIVER_TAP: 134 return tap_get_fd(backend); 135 default: 136 fprintf(stderr, "vhost-net requires tap backend\n"); 137 return -EBADFD; 138 } 139 } 140 141 struct vhost_net *vhost_net_init(VhostNetOptions *options) 142 { 143 int r; 144 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL; 145 struct vhost_net *net = g_new0(struct vhost_net, 1); 146 uint64_t features = 0; 147 148 if (!options->net_backend) { 149 fprintf(stderr, "vhost-net requires net backend to be setup\n"); 150 goto fail; 151 } 152 net->nc = options->net_backend; 153 154 net->dev.max_queues = 1; 155 net->dev.nvqs = 2; 156 net->dev.vqs = net->vqs; 157 158 if (backend_kernel) { 159 r = vhost_net_get_fd(options->net_backend); 160 if (r < 0) { 161 goto fail; 162 } 163 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) 164 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR); 165 net->backend = r; 166 net->dev.protocol_features = 0; 167 } else { 168 net->dev.backend_features = 0; 169 net->dev.protocol_features = 0; 170 net->backend = -1; 171 172 /* vhost-user needs vq_index to initiate a specific queue pair */ 173 net->dev.vq_index = net->nc->queue_index * net->dev.nvqs; 174 } 175 176 r = vhost_dev_init(&net->dev, options->opaque, 177 options->backend_type, options->busyloop_timeout); 178 if (r < 0) { 179 goto fail; 180 } 181 if (backend_kernel) { 182 if (!qemu_has_vnet_hdr_len(options->net_backend, 183 sizeof(struct virtio_net_hdr_mrg_rxbuf))) { 184 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF); 185 } 186 if (~net->dev.features & net->dev.backend_features) { 187 fprintf(stderr, "vhost lacks feature mask %" PRIu64 188 " for backend\n", 189 (uint64_t)(~net->dev.features & net->dev.backend_features)); 190 goto fail; 191 } 192 } 193 194 /* Set sane init value. Override when guest acks. */ 195 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 196 features = vhost_user_get_acked_features(net->nc); 197 if (~net->dev.features & features) { 198 fprintf(stderr, "vhost lacks feature mask %" PRIu64 199 " for backend\n", 200 (uint64_t)(~net->dev.features & features)); 201 goto fail; 202 } 203 } 204 205 vhost_net_ack_features(net, features); 206 207 return net; 208 209 fail: 210 vhost_dev_cleanup(&net->dev); 211 g_free(net); 212 return NULL; 213 } 214 215 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index) 216 { 217 net->dev.vq_index = vq_index; 218 } 219 220 static int vhost_net_start_one(struct vhost_net *net, 221 VirtIODevice *dev) 222 { 223 struct vhost_vring_file file = { }; 224 int r; 225 226 net->dev.nvqs = 2; 227 net->dev.vqs = net->vqs; 228 229 r = vhost_dev_enable_notifiers(&net->dev, dev); 230 if (r < 0) { 231 goto fail_notifiers; 232 } 233 234 r = vhost_dev_start(&net->dev, dev); 235 if (r < 0) { 236 goto fail_start; 237 } 238 239 if (net->nc->info->poll) { 240 net->nc->info->poll(net->nc, false); 241 } 242 243 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { 244 qemu_set_fd_handler(net->backend, NULL, NULL, NULL); 245 file.fd = net->backend; 246 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 247 r = vhost_net_set_backend(&net->dev, &file); 248 if (r < 0) { 249 r = -errno; 250 goto fail; 251 } 252 } 253 } 254 return 0; 255 fail: 256 file.fd = -1; 257 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { 258 while (file.index-- > 0) { 259 int r = vhost_net_set_backend(&net->dev, &file); 260 assert(r >= 0); 261 } 262 } 263 if (net->nc->info->poll) { 264 net->nc->info->poll(net->nc, true); 265 } 266 vhost_dev_stop(&net->dev, dev); 267 fail_start: 268 vhost_dev_disable_notifiers(&net->dev, dev); 269 fail_notifiers: 270 return r; 271 } 272 273 static void vhost_net_stop_one(struct vhost_net *net, 274 VirtIODevice *dev) 275 { 276 struct vhost_vring_file file = { .fd = -1 }; 277 278 if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) { 279 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 280 int r = vhost_net_set_backend(&net->dev, &file); 281 assert(r >= 0); 282 } 283 } 284 if (net->nc->info->poll) { 285 net->nc->info->poll(net->nc, true); 286 } 287 vhost_dev_stop(&net->dev, dev); 288 vhost_dev_disable_notifiers(&net->dev, dev); 289 } 290 291 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, 292 int total_queues) 293 { 294 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 295 VirtioBusState *vbus = VIRTIO_BUS(qbus); 296 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 297 int r, e, i; 298 299 if (!k->set_guest_notifiers) { 300 error_report("binding does not support guest notifiers"); 301 return -ENOSYS; 302 } 303 304 for (i = 0; i < total_queues; i++) { 305 struct vhost_net *net; 306 307 net = get_vhost_net(ncs[i].peer); 308 vhost_net_set_vq_index(net, i * 2); 309 310 /* Suppress the masking guest notifiers on vhost user 311 * because vhost user doesn't interrupt masking/unmasking 312 * properly. 313 */ 314 if (net->nc->info->type == NET_CLIENT_DRIVER_VHOST_USER) { 315 dev->use_guest_notifier_mask = false; 316 } 317 } 318 319 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); 320 if (r < 0) { 321 error_report("Error binding guest notifier: %d", -r); 322 goto err; 323 } 324 325 for (i = 0; i < total_queues; i++) { 326 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev); 327 328 if (r < 0) { 329 goto err_start; 330 } 331 332 if (ncs[i].peer->vring_enable) { 333 /* restore vring enable state */ 334 r = vhost_set_vring_enable(ncs[i].peer, ncs[i].peer->vring_enable); 335 336 if (r < 0) { 337 goto err_start; 338 } 339 } 340 } 341 342 return 0; 343 344 err_start: 345 while (--i >= 0) { 346 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 347 } 348 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 349 if (e < 0) { 350 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); 351 fflush(stderr); 352 } 353 err: 354 return r; 355 } 356 357 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, 358 int total_queues) 359 { 360 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 361 VirtioBusState *vbus = VIRTIO_BUS(qbus); 362 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 363 int i, r; 364 365 for (i = 0; i < total_queues; i++) { 366 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 367 } 368 369 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 370 if (r < 0) { 371 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); 372 fflush(stderr); 373 } 374 assert(r >= 0); 375 } 376 377 void vhost_net_cleanup(struct vhost_net *net) 378 { 379 vhost_dev_cleanup(&net->dev); 380 } 381 382 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr) 383 { 384 const VhostOps *vhost_ops = net->dev.vhost_ops; 385 386 assert(vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER); 387 assert(vhost_ops->vhost_migration_done); 388 389 return vhost_ops->vhost_migration_done(&net->dev, mac_addr); 390 } 391 392 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 393 { 394 return vhost_virtqueue_pending(&net->dev, idx); 395 } 396 397 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 398 int idx, bool mask) 399 { 400 vhost_virtqueue_mask(&net->dev, dev, idx, mask); 401 } 402 403 VHostNetState *get_vhost_net(NetClientState *nc) 404 { 405 VHostNetState *vhost_net = 0; 406 407 if (!nc) { 408 return 0; 409 } 410 411 switch (nc->info->type) { 412 case NET_CLIENT_DRIVER_TAP: 413 vhost_net = tap_get_vhost_net(nc); 414 break; 415 case NET_CLIENT_DRIVER_VHOST_USER: 416 vhost_net = vhost_user_get_vhost_net(nc); 417 assert(vhost_net); 418 break; 419 default: 420 break; 421 } 422 423 return vhost_net; 424 } 425 426 int vhost_set_vring_enable(NetClientState *nc, int enable) 427 { 428 VHostNetState *net = get_vhost_net(nc); 429 const VhostOps *vhost_ops = net->dev.vhost_ops; 430 431 nc->vring_enable = enable; 432 433 if (vhost_ops && vhost_ops->vhost_set_vring_enable) { 434 return vhost_ops->vhost_set_vring_enable(&net->dev, enable); 435 } 436 437 return 0; 438 } 439 440 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu) 441 { 442 const VhostOps *vhost_ops = net->dev.vhost_ops; 443 444 if (!vhost_ops->vhost_net_set_mtu) { 445 return 0; 446 } 447 448 return vhost_ops->vhost_net_set_mtu(&net->dev, mtu); 449 } 450 451 #else 452 uint64_t vhost_net_get_max_queues(VHostNetState *net) 453 { 454 return 1; 455 } 456 457 struct vhost_net *vhost_net_init(VhostNetOptions *options) 458 { 459 error_report("vhost-net support is not compiled in"); 460 return NULL; 461 } 462 463 int vhost_net_start(VirtIODevice *dev, 464 NetClientState *ncs, 465 int total_queues) 466 { 467 return -ENOSYS; 468 } 469 void vhost_net_stop(VirtIODevice *dev, 470 NetClientState *ncs, 471 int total_queues) 472 { 473 } 474 475 void vhost_net_cleanup(struct vhost_net *net) 476 { 477 } 478 479 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) 480 { 481 return features; 482 } 483 484 void vhost_net_ack_features(struct vhost_net *net, uint64_t features) 485 { 486 } 487 488 uint64_t vhost_net_get_acked_features(VHostNetState *net) 489 { 490 return 0; 491 } 492 493 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 494 { 495 return false; 496 } 497 498 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 499 int idx, bool mask) 500 { 501 } 502 503 int vhost_net_notify_migration_done(struct vhost_net *net, char* mac_addr) 504 { 505 return -1; 506 } 507 508 VHostNetState *get_vhost_net(NetClientState *nc) 509 { 510 return 0; 511 } 512 513 int vhost_set_vring_enable(NetClientState *nc, int enable) 514 { 515 return 0; 516 } 517 518 int vhost_net_set_mtu(struct vhost_net *net, uint16_t mtu) 519 { 520 return 0; 521 } 522 #endif 523