1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <stdio.h> 7 #include <fcntl.h> 8 #include <string.h> 9 #include <errno.h> 10 #include <sys/mman.h> 11 #include <unistd.h> 12 #include <sys/eventfd.h> 13 #include <sys/types.h> 14 #include <sys/stat.h> 15 16 #include <rte_eal_memconfig.h> 17 18 #include "vhost.h" 19 #include "virtio_user_dev.h" 20 #include "../virtio_ethdev.h" 21 22 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb" 23 24 static int 25 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 26 { 27 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come 28 * firstly because vhost depends on this msg to allocate virtqueue 29 * pair. 30 */ 31 struct vhost_vring_file file; 32 33 file.index = queue_sel; 34 file.fd = dev->callfds[queue_sel]; 35 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file); 36 37 return 0; 38 } 39 40 static int 41 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel) 42 { 43 struct vhost_vring_file file; 44 struct vhost_vring_state state; 45 struct vring *vring = &dev->vrings[queue_sel]; 46 struct vhost_vring_addr addr = { 47 .index = queue_sel, 48 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc, 49 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail, 50 .used_user_addr = (uint64_t)(uintptr_t)vring->used, 51 .log_guest_addr = 0, 52 .flags = 0, /* disable log */ 53 }; 54 55 state.index = queue_sel; 56 state.num = vring->num; 57 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state); 58 59 state.index = queue_sel; 60 state.num = 0; /* no reservation */ 61 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) 62 state.num |= (1 << 15); 63 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state); 64 65 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr); 66 67 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes 68 * lastly because vhost depends on this msg to judge if 69 * virtio is ready. 70 */ 71 file.index = queue_sel; 72 file.fd = dev->kickfds[queue_sel]; 73 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file); 74 75 return 0; 76 } 77 78 static int 79 virtio_user_queue_setup(struct virtio_user_dev *dev, 80 int (*fn)(struct virtio_user_dev *, uint32_t)) 81 { 82 uint32_t i, queue_sel; 83 84 for (i = 0; i < dev->max_queue_pairs; ++i) { 85 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX; 86 if (fn(dev, queue_sel) < 0) { 87 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i); 88 return -1; 89 } 90 } 91 for (i = 0; i < dev->max_queue_pairs; ++i) { 92 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX; 93 if (fn(dev, queue_sel) < 0) { 94 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i); 95 return -1; 96 } 97 } 98 99 return 0; 100 } 101 102 int 103 is_vhost_user_by_type(const char *path) 104 { 105 struct stat sb; 106 107 if (stat(path, &sb) == -1) 108 return 0; 109 110 return S_ISSOCK(sb.st_mode); 111 } 112 113 int 114 virtio_user_start_device(struct virtio_user_dev *dev) 115 { 116 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config; 117 uint64_t features; 118 int ret; 119 120 /* 121 * XXX workaround! 122 * 123 * We need to make sure that the locks will be 124 * taken in the correct order to avoid deadlocks. 125 * 126 * Before releasing this lock, this thread should 127 * not trigger any memory hotplug events. 128 * 129 * This is a temporary workaround, and should be 130 * replaced when we get proper supports from the 131 * memory subsystem in the future. 132 */ 133 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock); 134 pthread_mutex_lock(&dev->mutex); 135 136 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0) 137 goto error; 138 139 /* Step 0: tell vhost to create queues */ 140 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0) 141 goto error; 142 143 /* Step 1: set features */ 144 features = dev->features; 145 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */ 146 features &= ~(1ull << VIRTIO_NET_F_MAC); 147 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */ 148 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ); 149 features &= ~(1ull << VIRTIO_NET_F_STATUS); 150 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features); 151 if (ret < 0) 152 goto error; 153 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features); 154 155 /* Step 2: share memory regions */ 156 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 157 if (ret < 0) 158 goto error; 159 160 /* Step 3: kick queues */ 161 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0) 162 goto error; 163 164 /* Step 4: enable queues 165 * we enable the 1st queue pair by default. 166 */ 167 dev->ops->enable_qp(dev, 0, 1); 168 169 dev->started = true; 170 pthread_mutex_unlock(&dev->mutex); 171 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 172 173 return 0; 174 error: 175 pthread_mutex_unlock(&dev->mutex); 176 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock); 177 /* TODO: free resource here or caller to check */ 178 return -1; 179 } 180 181 int virtio_user_stop_device(struct virtio_user_dev *dev) 182 { 183 struct vhost_vring_state state; 184 uint32_t i; 185 int error = 0; 186 187 pthread_mutex_lock(&dev->mutex); 188 if (!dev->started) 189 goto out; 190 191 for (i = 0; i < dev->max_queue_pairs; ++i) 192 dev->ops->enable_qp(dev, i, 0); 193 194 /* Stop the backend. */ 195 for (i = 0; i < dev->max_queue_pairs * 2; ++i) { 196 state.index = i; 197 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE, 198 &state) < 0) { 199 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n", 200 i); 201 error = -1; 202 goto out; 203 } 204 } 205 206 dev->started = false; 207 out: 208 pthread_mutex_unlock(&dev->mutex); 209 210 return error; 211 } 212 213 static inline void 214 parse_mac(struct virtio_user_dev *dev, const char *mac) 215 { 216 int i, r; 217 uint32_t tmp[ETHER_ADDR_LEN]; 218 219 if (!mac) 220 return; 221 222 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0], 223 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]); 224 if (r == ETHER_ADDR_LEN) { 225 for (i = 0; i < ETHER_ADDR_LEN; ++i) 226 dev->mac_addr[i] = (uint8_t)tmp[i]; 227 dev->mac_specified = 1; 228 } else { 229 /* ignore the wrong mac, use random mac */ 230 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac); 231 } 232 } 233 234 static int 235 virtio_user_dev_init_notify(struct virtio_user_dev *dev) 236 { 237 uint32_t i, j; 238 int callfd; 239 int kickfd; 240 241 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) { 242 if (i >= dev->max_queue_pairs * 2) { 243 dev->kickfds[i] = -1; 244 dev->callfds[i] = -1; 245 continue; 246 } 247 248 /* May use invalid flag, but some backend uses kickfd and 249 * callfd as criteria to judge if dev is alive. so finally we 250 * use real event_fd. 251 */ 252 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 253 if (callfd < 0) { 254 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno)); 255 break; 256 } 257 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); 258 if (kickfd < 0) { 259 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno)); 260 break; 261 } 262 dev->callfds[i] = callfd; 263 dev->kickfds[i] = kickfd; 264 } 265 266 if (i < VIRTIO_MAX_VIRTQUEUES) { 267 for (j = 0; j <= i; ++j) { 268 close(dev->callfds[j]); 269 close(dev->kickfds[j]); 270 } 271 272 return -1; 273 } 274 275 return 0; 276 } 277 278 static int 279 virtio_user_fill_intr_handle(struct virtio_user_dev *dev) 280 { 281 uint32_t i; 282 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id]; 283 284 if (!eth_dev->intr_handle) { 285 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle)); 286 if (!eth_dev->intr_handle) { 287 PMD_DRV_LOG(ERR, "fail to allocate intr_handle"); 288 return -1; 289 } 290 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle)); 291 } 292 293 for (i = 0; i < dev->max_queue_pairs; ++i) 294 eth_dev->intr_handle->efds[i] = dev->callfds[i]; 295 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs; 296 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1; 297 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV; 298 /* For virtio vdev, no need to read counter for clean */ 299 eth_dev->intr_handle->efd_counter_size = 0; 300 eth_dev->intr_handle->fd = -1; 301 if (dev->vhostfd >= 0) 302 eth_dev->intr_handle->fd = dev->vhostfd; 303 else if (dev->is_server) 304 eth_dev->intr_handle->fd = dev->listenfd; 305 306 return 0; 307 } 308 309 static void 310 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused, 311 const void *addr __rte_unused, 312 size_t len __rte_unused, 313 void *arg) 314 { 315 struct virtio_user_dev *dev = arg; 316 struct rte_memseg_list *msl; 317 uint16_t i; 318 319 /* ignore externally allocated memory */ 320 msl = rte_mem_virt2memseg_list(addr); 321 if (msl->external) 322 return; 323 324 pthread_mutex_lock(&dev->mutex); 325 326 if (dev->started == false) 327 goto exit; 328 329 /* Step 1: pause the active queues */ 330 for (i = 0; i < dev->queue_pairs; i++) 331 dev->ops->enable_qp(dev, i, 0); 332 333 /* Step 2: update memory regions */ 334 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL); 335 336 /* Step 3: resume the active queues */ 337 for (i = 0; i < dev->queue_pairs; i++) 338 dev->ops->enable_qp(dev, i, 1); 339 340 exit: 341 pthread_mutex_unlock(&dev->mutex); 342 } 343 344 static int 345 virtio_user_dev_setup(struct virtio_user_dev *dev) 346 { 347 uint32_t q; 348 349 dev->vhostfd = -1; 350 dev->vhostfds = NULL; 351 dev->tapfds = NULL; 352 353 if (dev->is_server) { 354 if (access(dev->path, F_OK) == 0 && 355 !is_vhost_user_by_type(dev->path)) { 356 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!"); 357 return -1; 358 } 359 dev->ops = &virtio_ops_user; 360 } else { 361 if (is_vhost_user_by_type(dev->path)) { 362 dev->ops = &virtio_ops_user; 363 } else { 364 dev->ops = &virtio_ops_kernel; 365 366 dev->vhostfds = malloc(dev->max_queue_pairs * 367 sizeof(int)); 368 dev->tapfds = malloc(dev->max_queue_pairs * 369 sizeof(int)); 370 if (!dev->vhostfds || !dev->tapfds) { 371 PMD_INIT_LOG(ERR, "Failed to malloc"); 372 return -1; 373 } 374 375 for (q = 0; q < dev->max_queue_pairs; ++q) { 376 dev->vhostfds[q] = -1; 377 dev->tapfds[q] = -1; 378 } 379 } 380 } 381 382 if (dev->ops->setup(dev) < 0) 383 return -1; 384 385 if (virtio_user_dev_init_notify(dev) < 0) 386 return -1; 387 388 if (virtio_user_fill_intr_handle(dev) < 0) 389 return -1; 390 391 return 0; 392 } 393 394 /* Use below macro to filter features from vhost backend */ 395 #define VIRTIO_USER_SUPPORTED_FEATURES \ 396 (1ULL << VIRTIO_NET_F_MAC | \ 397 1ULL << VIRTIO_NET_F_STATUS | \ 398 1ULL << VIRTIO_NET_F_MQ | \ 399 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \ 400 1ULL << VIRTIO_NET_F_CTRL_VQ | \ 401 1ULL << VIRTIO_NET_F_CTRL_RX | \ 402 1ULL << VIRTIO_NET_F_CTRL_VLAN | \ 403 1ULL << VIRTIO_NET_F_CSUM | \ 404 1ULL << VIRTIO_NET_F_HOST_TSO4 | \ 405 1ULL << VIRTIO_NET_F_HOST_TSO6 | \ 406 1ULL << VIRTIO_NET_F_MRG_RXBUF | \ 407 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \ 408 1ULL << VIRTIO_NET_F_GUEST_CSUM | \ 409 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \ 410 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \ 411 1ULL << VIRTIO_F_IN_ORDER | \ 412 1ULL << VIRTIO_F_VERSION_1 | \ 413 1ULL << VIRTIO_F_RING_PACKED) 414 415 int 416 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues, 417 int cq, int queue_size, const char *mac, char **ifname, 418 int mrg_rxbuf, int in_order, int packed_vq) 419 { 420 pthread_mutex_init(&dev->mutex, NULL); 421 snprintf(dev->path, PATH_MAX, "%s", path); 422 dev->started = 0; 423 dev->max_queue_pairs = queues; 424 dev->queue_pairs = 1; /* mq disabled by default */ 425 dev->queue_size = queue_size; 426 dev->mac_specified = 0; 427 dev->frontend_features = 0; 428 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES; 429 parse_mac(dev, mac); 430 431 if (*ifname) { 432 dev->ifname = *ifname; 433 *ifname = NULL; 434 } 435 436 if (virtio_user_dev_setup(dev) < 0) { 437 PMD_INIT_LOG(ERR, "backend set up fails"); 438 return -1; 439 } 440 441 if (!dev->is_server) { 442 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, 443 NULL) < 0) { 444 PMD_INIT_LOG(ERR, "set_owner fails: %s", 445 strerror(errno)); 446 return -1; 447 } 448 449 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES, 450 &dev->device_features) < 0) { 451 PMD_INIT_LOG(ERR, "get_features failed: %s", 452 strerror(errno)); 453 return -1; 454 } 455 } else { 456 /* We just pretend vhost-user can support all these features. 457 * Note that this could be problematic that if some feature is 458 * negotiated but not supported by the vhost-user which comes 459 * later. 460 */ 461 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES; 462 } 463 464 if (!mrg_rxbuf) 465 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF); 466 467 if (!in_order) 468 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER); 469 470 if (packed_vq) { 471 if (cq) { 472 PMD_INIT_LOG(ERR, "control vq not supported yet with " 473 "packed virtqueues\n"); 474 return -1; 475 } 476 } else { 477 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED); 478 } 479 480 if (dev->mac_specified) 481 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC); 482 else 483 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC); 484 485 if (cq) { 486 /* device does not really need to know anything about CQ, 487 * so if necessary, we just claim to support CQ 488 */ 489 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ); 490 } else { 491 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ); 492 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */ 493 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX); 494 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN); 495 dev->unsupported_features |= 496 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE); 497 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ); 498 dev->unsupported_features |= 499 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR); 500 } 501 502 /* The backend will not report this feature, we add it explicitly */ 503 if (is_vhost_user_by_type(dev->path)) 504 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS); 505 506 /* 507 * Device features = 508 * (frontend_features | backend_features) & ~unsupported_features; 509 */ 510 dev->device_features |= dev->frontend_features; 511 dev->device_features &= ~dev->unsupported_features; 512 513 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME, 514 virtio_user_mem_event_cb, dev)) { 515 if (rte_errno != ENOTSUP) { 516 PMD_INIT_LOG(ERR, "Failed to register mem event" 517 " callback\n"); 518 return -1; 519 } 520 } 521 522 return 0; 523 } 524 525 void 526 virtio_user_dev_uninit(struct virtio_user_dev *dev) 527 { 528 uint32_t i; 529 530 virtio_user_stop_device(dev); 531 532 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev); 533 534 for (i = 0; i < dev->max_queue_pairs * 2; ++i) { 535 close(dev->callfds[i]); 536 close(dev->kickfds[i]); 537 } 538 539 close(dev->vhostfd); 540 541 if (dev->is_server && dev->listenfd >= 0) { 542 close(dev->listenfd); 543 dev->listenfd = -1; 544 } 545 546 if (dev->vhostfds) { 547 for (i = 0; i < dev->max_queue_pairs; ++i) 548 close(dev->vhostfds[i]); 549 free(dev->vhostfds); 550 free(dev->tapfds); 551 } 552 553 free(dev->ifname); 554 555 if (dev->is_server) 556 unlink(dev->path); 557 } 558 559 uint8_t 560 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs) 561 { 562 uint16_t i; 563 uint8_t ret = 0; 564 565 if (q_pairs > dev->max_queue_pairs) { 566 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported", 567 q_pairs, dev->max_queue_pairs); 568 return -1; 569 } 570 571 /* Server mode can't enable queue pairs if vhostfd is invalid, 572 * always return 0 in this case. 573 */ 574 if (!dev->is_server || dev->vhostfd >= 0) { 575 for (i = 0; i < q_pairs; ++i) 576 ret |= dev->ops->enable_qp(dev, i, 1); 577 for (i = q_pairs; i < dev->max_queue_pairs; ++i) 578 ret |= dev->ops->enable_qp(dev, i, 0); 579 } 580 dev->queue_pairs = q_pairs; 581 582 return ret; 583 } 584 585 static uint32_t 586 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring, 587 uint16_t idx_hdr) 588 { 589 struct virtio_net_ctrl_hdr *hdr; 590 virtio_net_ctrl_ack status = ~0; 591 uint16_t i, idx_data, idx_status; 592 uint32_t n_descs = 0; 593 594 /* locate desc for header, data, and status */ 595 idx_data = vring->desc[idx_hdr].next; 596 n_descs++; 597 598 i = idx_data; 599 while (vring->desc[i].flags == VRING_DESC_F_NEXT) { 600 i = vring->desc[i].next; 601 n_descs++; 602 } 603 604 /* locate desc for status */ 605 idx_status = i; 606 n_descs++; 607 608 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr; 609 if (hdr->class == VIRTIO_NET_CTRL_MQ && 610 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { 611 uint16_t queues; 612 613 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr; 614 status = virtio_user_handle_mq(dev, queues); 615 } 616 617 /* Update status */ 618 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status; 619 620 return n_descs; 621 } 622 623 void 624 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx) 625 { 626 uint16_t avail_idx, desc_idx; 627 struct vring_used_elem *uep; 628 uint32_t n_descs; 629 struct vring *vring = &dev->vrings[queue_idx]; 630 631 /* Consume avail ring, using used ring idx as first one */ 632 while (vring->used->idx != vring->avail->idx) { 633 avail_idx = (vring->used->idx) & (vring->num - 1); 634 desc_idx = vring->avail->ring[avail_idx]; 635 636 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx); 637 638 /* Update used ring */ 639 uep = &vring->used->ring[avail_idx]; 640 uep->id = avail_idx; 641 uep->len = n_descs; 642 643 vring->used->idx++; 644 } 645 } 646