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