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 static int vhost_net_get_fd(NetClientState *backend) 126 { 127 switch (backend->info->type) { 128 case NET_CLIENT_OPTIONS_KIND_TAP: 129 return tap_get_fd(backend); 130 default: 131 fprintf(stderr, "vhost-net requires tap backend\n"); 132 return -EBADFD; 133 } 134 } 135 136 struct vhost_net *vhost_net_init(VhostNetOptions *options) 137 { 138 int r; 139 bool backend_kernel = options->backend_type == VHOST_BACKEND_TYPE_KERNEL; 140 struct vhost_net *net = g_malloc(sizeof *net); 141 142 if (!options->net_backend) { 143 fprintf(stderr, "vhost-net requires net backend to be setup\n"); 144 goto fail; 145 } 146 147 if (backend_kernel) { 148 r = vhost_net_get_fd(options->net_backend); 149 if (r < 0) { 150 goto fail; 151 } 152 net->dev.backend_features = qemu_has_vnet_hdr(options->net_backend) 153 ? 0 : (1ULL << VHOST_NET_F_VIRTIO_NET_HDR); 154 net->backend = r; 155 net->dev.protocol_features = 0; 156 } else { 157 net->dev.backend_features = 0; 158 net->dev.protocol_features = 0; 159 net->backend = -1; 160 } 161 net->nc = options->net_backend; 162 163 net->dev.nvqs = 2; 164 net->dev.vqs = net->vqs; 165 166 r = vhost_dev_init(&net->dev, options->opaque, 167 options->backend_type); 168 if (r < 0) { 169 goto fail; 170 } 171 if (backend_kernel) { 172 if (!qemu_has_vnet_hdr_len(options->net_backend, 173 sizeof(struct virtio_net_hdr_mrg_rxbuf))) { 174 net->dev.features &= ~(1ULL << VIRTIO_NET_F_MRG_RXBUF); 175 } 176 if (~net->dev.features & net->dev.backend_features) { 177 fprintf(stderr, "vhost lacks feature mask %" PRIu64 178 " for backend\n", 179 (uint64_t)(~net->dev.features & net->dev.backend_features)); 180 vhost_dev_cleanup(&net->dev); 181 goto fail; 182 } 183 } 184 /* Set sane init value. Override when guest acks. */ 185 vhost_net_ack_features(net, 0); 186 return net; 187 fail: 188 g_free(net); 189 return NULL; 190 } 191 192 static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index) 193 { 194 net->dev.vq_index = vq_index; 195 } 196 197 static int vhost_net_set_vnet_endian(VirtIODevice *dev, NetClientState *peer, 198 bool set) 199 { 200 int r = 0; 201 202 if (virtio_vdev_has_feature(dev, VIRTIO_F_VERSION_1) || 203 (virtio_legacy_is_cross_endian(dev) && !virtio_is_big_endian(dev))) { 204 r = qemu_set_vnet_le(peer, set); 205 if (r) { 206 error_report("backend does not support LE vnet headers"); 207 } 208 } else if (virtio_legacy_is_cross_endian(dev)) { 209 r = qemu_set_vnet_be(peer, set); 210 if (r) { 211 error_report("backend does not support BE vnet headers"); 212 } 213 } 214 215 return r; 216 } 217 218 static int vhost_net_start_one(struct vhost_net *net, 219 VirtIODevice *dev) 220 { 221 struct vhost_vring_file file = { }; 222 int r; 223 224 net->dev.nvqs = 2; 225 net->dev.vqs = net->vqs; 226 227 r = vhost_dev_enable_notifiers(&net->dev, dev); 228 if (r < 0) { 229 goto fail_notifiers; 230 } 231 232 r = vhost_dev_start(&net->dev, dev); 233 if (r < 0) { 234 goto fail_start; 235 } 236 237 if (net->nc->info->poll) { 238 net->nc->info->poll(net->nc, false); 239 } 240 241 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) { 242 qemu_set_fd_handler(net->backend, NULL, NULL, NULL); 243 file.fd = net->backend; 244 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 245 const VhostOps *vhost_ops = net->dev.vhost_ops; 246 r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 247 &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_OPTIONS_KIND_TAP) { 258 while (file.index-- > 0) { 259 const VhostOps *vhost_ops = net->dev.vhost_ops; 260 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 261 &file); 262 assert(r >= 0); 263 } 264 } 265 if (net->nc->info->poll) { 266 net->nc->info->poll(net->nc, true); 267 } 268 vhost_dev_stop(&net->dev, dev); 269 fail_start: 270 vhost_dev_disable_notifiers(&net->dev, dev); 271 fail_notifiers: 272 return r; 273 } 274 275 static void vhost_net_stop_one(struct vhost_net *net, 276 VirtIODevice *dev) 277 { 278 struct vhost_vring_file file = { .fd = -1 }; 279 280 if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_TAP) { 281 for (file.index = 0; file.index < net->dev.nvqs; ++file.index) { 282 const VhostOps *vhost_ops = net->dev.vhost_ops; 283 int r = vhost_ops->vhost_call(&net->dev, VHOST_NET_SET_BACKEND, 284 &file); 285 assert(r >= 0); 286 } 287 } else if (net->nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER) { 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_call(&net->dev, VHOST_RESET_OWNER, 291 NULL); 292 assert(r >= 0); 293 } 294 } 295 if (net->nc->info->poll) { 296 net->nc->info->poll(net->nc, true); 297 } 298 vhost_dev_stop(&net->dev, dev); 299 vhost_dev_disable_notifiers(&net->dev, dev); 300 } 301 302 int vhost_net_start(VirtIODevice *dev, NetClientState *ncs, 303 int total_queues) 304 { 305 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 306 VirtioBusState *vbus = VIRTIO_BUS(qbus); 307 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 308 int r, e, i; 309 310 if (!k->set_guest_notifiers) { 311 error_report("binding does not support guest notifiers"); 312 r = -ENOSYS; 313 goto err; 314 } 315 316 r = vhost_net_set_vnet_endian(dev, ncs[0].peer, true); 317 if (r < 0) { 318 goto err; 319 } 320 321 for (i = 0; i < total_queues; i++) { 322 vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2); 323 } 324 325 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); 326 if (r < 0) { 327 error_report("Error binding guest notifier: %d", -r); 328 goto err_endian; 329 } 330 331 for (i = 0; i < total_queues; i++) { 332 r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev); 333 334 if (r < 0) { 335 goto err_start; 336 } 337 } 338 339 return 0; 340 341 err_start: 342 while (--i >= 0) { 343 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 344 } 345 e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 346 if (e < 0) { 347 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e); 348 fflush(stderr); 349 } 350 err_endian: 351 vhost_net_set_vnet_endian(dev, ncs[0].peer, false); 352 err: 353 return r; 354 } 355 356 void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs, 357 int total_queues) 358 { 359 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); 360 VirtioBusState *vbus = VIRTIO_BUS(qbus); 361 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); 362 int i, r; 363 364 for (i = 0; i < total_queues; i++) { 365 vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); 366 } 367 368 r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); 369 if (r < 0) { 370 fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); 371 fflush(stderr); 372 } 373 assert(r >= 0); 374 375 assert(vhost_net_set_vnet_endian(dev, ncs[0].peer, false) >= 0); 376 } 377 378 void vhost_net_cleanup(struct vhost_net *net) 379 { 380 vhost_dev_cleanup(&net->dev); 381 g_free(net); 382 } 383 384 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 385 { 386 return vhost_virtqueue_pending(&net->dev, idx); 387 } 388 389 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 390 int idx, bool mask) 391 { 392 vhost_virtqueue_mask(&net->dev, dev, idx, mask); 393 } 394 395 VHostNetState *get_vhost_net(NetClientState *nc) 396 { 397 VHostNetState *vhost_net = 0; 398 399 if (!nc) { 400 return 0; 401 } 402 403 switch (nc->info->type) { 404 case NET_CLIENT_OPTIONS_KIND_TAP: 405 vhost_net = tap_get_vhost_net(nc); 406 break; 407 case NET_CLIENT_OPTIONS_KIND_VHOST_USER: 408 vhost_net = vhost_user_get_vhost_net(nc); 409 break; 410 default: 411 break; 412 } 413 414 return vhost_net; 415 } 416 #else 417 struct vhost_net *vhost_net_init(VhostNetOptions *options) 418 { 419 error_report("vhost-net support is not compiled in"); 420 return NULL; 421 } 422 423 int vhost_net_start(VirtIODevice *dev, 424 NetClientState *ncs, 425 int total_queues) 426 { 427 return -ENOSYS; 428 } 429 void vhost_net_stop(VirtIODevice *dev, 430 NetClientState *ncs, 431 int total_queues) 432 { 433 } 434 435 void vhost_net_cleanup(struct vhost_net *net) 436 { 437 } 438 439 uint64_t vhost_net_get_features(struct vhost_net *net, uint64_t features) 440 { 441 return features; 442 } 443 void vhost_net_ack_features(struct vhost_net *net, uint64_t features) 444 { 445 } 446 447 bool vhost_net_virtqueue_pending(VHostNetState *net, int idx) 448 { 449 return false; 450 } 451 452 void vhost_net_virtqueue_mask(VHostNetState *net, VirtIODevice *dev, 453 int idx, bool mask) 454 { 455 } 456 457 VHostNetState *get_vhost_net(NetClientState *nc) 458 { 459 return 0; 460 } 461 #endif 462