1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdint.h> 6 #include <sys/types.h> 7 #include <unistd.h> 8 #include <fcntl.h> 9 #include <linux/major.h> 10 #include <sys/stat.h> 11 #include <sys/sysmacros.h> 12 #include <sys/socket.h> 13 14 #include <rte_malloc.h> 15 #include <rte_kvargs.h> 16 #include <ethdev_vdev.h> 17 #include <rte_bus_vdev.h> 18 #include <rte_alarm.h> 19 #include <rte_cycles.h> 20 21 #include "virtio_ethdev.h" 22 #include "virtio_logs.h" 23 #include "virtio.h" 24 #include "virtqueue.h" 25 #include "virtio_rxtx.h" 26 #include "virtio_user/virtio_user_dev.h" 27 #include "virtio_user/vhost.h" 28 29 #define virtio_user_get_dev(hwp) container_of(hwp, struct virtio_user_dev, hw) 30 31 static void 32 virtio_user_reset_queues_packed(struct rte_eth_dev *eth_dev) 33 { 34 struct virtio_user_dev *dev = eth_dev->data->dev_private; 35 struct virtio_hw *hw = &dev->hw; 36 struct virtnet_rx *rxvq; 37 struct virtnet_tx *txvq; 38 uint16_t i; 39 40 /* Add lock to avoid queue contention. */ 41 rte_spinlock_lock(&hw->state_lock); 42 hw->started = 0; 43 44 /* 45 * Waitting for datapath to complete before resetting queues. 46 * 1 ms should be enough for the ongoing Tx/Rx function to finish. 47 */ 48 rte_delay_ms(1); 49 50 /* Vring reset for each Tx queue and Rx queue. */ 51 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) { 52 rxvq = eth_dev->data->rx_queues[i]; 53 virtqueue_rxvq_reset_packed(rxvq->vq); 54 virtio_dev_rx_queue_setup_finish(eth_dev, i); 55 } 56 57 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) { 58 txvq = eth_dev->data->tx_queues[i]; 59 virtqueue_txvq_reset_packed(txvq->vq); 60 } 61 62 hw->started = 1; 63 rte_spinlock_unlock(&hw->state_lock); 64 } 65 66 67 static int 68 virtio_user_server_reconnect(struct virtio_user_dev *dev) 69 { 70 int ret, connectfd, old_status; 71 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id]; 72 struct virtio_hw *hw = &dev->hw; 73 uint64_t protocol_features; 74 75 connectfd = accept(dev->listenfd, NULL, NULL); 76 if (connectfd < 0) 77 return -1; 78 79 dev->vhostfd = connectfd; 80 old_status = dev->status; 81 82 virtio_reset(hw); 83 84 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK); 85 86 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER); 87 88 if (dev->ops->get_features(dev, &dev->device_features) < 0) { 89 PMD_INIT_LOG(ERR, "get_features failed: %s", 90 strerror(errno)); 91 return -1; 92 } 93 94 if (dev->device_features & 95 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) { 96 if (dev->ops->get_protocol_features(dev, &protocol_features)) 97 return -1; 98 99 /* Offer VHOST_USER_PROTOCOL_F_STATUS */ 100 dev->protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_STATUS); 101 dev->protocol_features &= protocol_features; 102 103 if (dev->ops->set_protocol_features(dev, dev->protocol_features)) 104 return -1; 105 106 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ))) 107 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ); 108 } 109 110 dev->device_features |= dev->frontend_features; 111 112 /* umask vhost-user unsupported features */ 113 dev->device_features &= ~(dev->unsupported_features); 114 115 dev->features &= dev->device_features; 116 117 /* For packed ring, resetting queues is required in reconnection. */ 118 if (virtio_with_packed_queue(hw) && 119 (old_status & VIRTIO_CONFIG_STATUS_DRIVER_OK)) { 120 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped" 121 " when packed ring reconnecting."); 122 virtio_user_reset_queues_packed(eth_dev); 123 } 124 125 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK); 126 127 /* Start the device */ 128 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK); 129 if (!dev->started) 130 return -1; 131 132 if (dev->queue_pairs > 1) { 133 ret = virtio_user_handle_mq(dev, dev->queue_pairs); 134 if (ret != 0) { 135 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!"); 136 return -1; 137 } 138 } 139 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) { 140 if (rte_intr_disable(eth_dev->intr_handle) < 0) { 141 PMD_DRV_LOG(ERR, "interrupt disable failed"); 142 return -1; 143 } 144 rte_intr_callback_unregister(eth_dev->intr_handle, 145 virtio_interrupt_handler, 146 eth_dev); 147 eth_dev->intr_handle->fd = connectfd; 148 rte_intr_callback_register(eth_dev->intr_handle, 149 virtio_interrupt_handler, eth_dev); 150 151 if (rte_intr_enable(eth_dev->intr_handle) < 0) { 152 PMD_DRV_LOG(ERR, "interrupt enable failed"); 153 return -1; 154 } 155 } 156 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!"); 157 return 0; 158 } 159 160 static void 161 virtio_user_delayed_handler(void *param) 162 { 163 struct virtio_hw *hw = (struct virtio_hw *)param; 164 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id]; 165 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 166 167 if (rte_intr_disable(eth_dev->intr_handle) < 0) { 168 PMD_DRV_LOG(ERR, "interrupt disable failed"); 169 return; 170 } 171 rte_intr_callback_unregister(eth_dev->intr_handle, 172 virtio_interrupt_handler, eth_dev); 173 if (dev->is_server) { 174 if (dev->vhostfd >= 0) { 175 close(dev->vhostfd); 176 dev->vhostfd = -1; 177 } 178 eth_dev->intr_handle->fd = dev->listenfd; 179 rte_intr_callback_register(eth_dev->intr_handle, 180 virtio_interrupt_handler, eth_dev); 181 if (rte_intr_enable(eth_dev->intr_handle) < 0) { 182 PMD_DRV_LOG(ERR, "interrupt enable failed"); 183 return; 184 } 185 } 186 } 187 188 static void 189 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset, 190 void *dst, int length) 191 { 192 int i; 193 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 194 195 if (offset == offsetof(struct virtio_net_config, mac) && 196 length == RTE_ETHER_ADDR_LEN) { 197 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) 198 ((uint8_t *)dst)[i] = dev->mac_addr[i]; 199 return; 200 } 201 202 if (offset == offsetof(struct virtio_net_config, status)) { 203 char buf[128]; 204 205 if (dev->vhostfd >= 0) { 206 int r; 207 int flags; 208 209 flags = fcntl(dev->vhostfd, F_GETFL); 210 if (fcntl(dev->vhostfd, F_SETFL, 211 flags | O_NONBLOCK) == -1) { 212 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag"); 213 return; 214 } 215 r = recv(dev->vhostfd, buf, 128, MSG_PEEK); 216 if (r == 0 || (r < 0 && errno != EAGAIN)) { 217 dev->net_status &= (~VIRTIO_NET_S_LINK_UP); 218 PMD_DRV_LOG(ERR, "virtio-user port %u is down", 219 hw->port_id); 220 221 /* This function could be called in the process 222 * of interrupt handling, callback cannot be 223 * unregistered here, set an alarm to do it. 224 */ 225 rte_eal_alarm_set(1, 226 virtio_user_delayed_handler, 227 (void *)hw); 228 } else { 229 dev->net_status |= VIRTIO_NET_S_LINK_UP; 230 } 231 if (fcntl(dev->vhostfd, F_SETFL, 232 flags & ~O_NONBLOCK) == -1) { 233 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag"); 234 return; 235 } 236 } else if (dev->is_server) { 237 dev->net_status &= (~VIRTIO_NET_S_LINK_UP); 238 if (virtio_user_server_reconnect(dev) >= 0) 239 dev->net_status |= VIRTIO_NET_S_LINK_UP; 240 } 241 242 *(uint16_t *)dst = dev->net_status; 243 } 244 245 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs)) 246 *(uint16_t *)dst = dev->max_queue_pairs; 247 } 248 249 static void 250 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset, 251 const void *src, int length) 252 { 253 int i; 254 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 255 256 if ((offset == offsetof(struct virtio_net_config, mac)) && 257 (length == RTE_ETHER_ADDR_LEN)) 258 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i) 259 dev->mac_addr[i] = ((const uint8_t *)src)[i]; 260 else 261 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d", 262 offset, length); 263 } 264 265 static void 266 virtio_user_reset(struct virtio_hw *hw) 267 { 268 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 269 270 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK) 271 virtio_user_stop_device(dev); 272 } 273 274 static void 275 virtio_user_set_status(struct virtio_hw *hw, uint8_t status) 276 { 277 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 278 uint8_t old_status = dev->status; 279 280 if (status & VIRTIO_CONFIG_STATUS_FEATURES_OK && 281 ~old_status & VIRTIO_CONFIG_STATUS_FEATURES_OK) 282 virtio_user_dev_set_features(dev); 283 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK) 284 virtio_user_start_device(dev); 285 else if (status == VIRTIO_CONFIG_STATUS_RESET) 286 virtio_user_reset(hw); 287 288 virtio_user_dev_set_status(dev, status); 289 } 290 291 static uint8_t 292 virtio_user_get_status(struct virtio_hw *hw) 293 { 294 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 295 296 virtio_user_dev_update_status(dev); 297 298 return dev->status; 299 } 300 301 static uint64_t 302 virtio_user_get_features(struct virtio_hw *hw) 303 { 304 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 305 306 /* unmask feature bits defined in vhost user protocol */ 307 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES; 308 } 309 310 static void 311 virtio_user_set_features(struct virtio_hw *hw, uint64_t features) 312 { 313 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 314 315 dev->features = features & dev->device_features; 316 } 317 318 static int 319 virtio_user_features_ok(struct virtio_hw *hw __rte_unused) 320 { 321 return 0; 322 } 323 324 static uint8_t 325 virtio_user_get_isr(struct virtio_hw *hw __rte_unused) 326 { 327 /* rxq interrupts and config interrupt are separated in virtio-user, 328 * here we only report config change. 329 */ 330 return VIRTIO_ISR_CONFIG; 331 } 332 333 static uint16_t 334 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused, 335 uint16_t vec __rte_unused) 336 { 337 return 0; 338 } 339 340 static uint16_t 341 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused, 342 struct virtqueue *vq __rte_unused, 343 uint16_t vec) 344 { 345 /* pretend we have done that */ 346 return vec; 347 } 348 349 /* This function is to get the queue size, aka, number of descs, of a specified 350 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the 351 * max supported queues. 352 */ 353 static uint16_t 354 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused) 355 { 356 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 357 358 /* Currently, each queue has same queue size */ 359 return dev->queue_size; 360 } 361 362 static void 363 virtio_user_setup_queue_packed(struct virtqueue *vq, 364 struct virtio_user_dev *dev) 365 { 366 uint16_t queue_idx = vq->vq_queue_index; 367 struct vring_packed *vring; 368 uint64_t desc_addr; 369 uint64_t avail_addr; 370 uint64_t used_addr; 371 uint16_t i; 372 373 vring = &dev->packed_vrings[queue_idx]; 374 desc_addr = (uintptr_t)vq->vq_ring_virt_mem; 375 avail_addr = desc_addr + vq->vq_nentries * 376 sizeof(struct vring_packed_desc); 377 used_addr = RTE_ALIGN_CEIL(avail_addr + 378 sizeof(struct vring_packed_desc_event), 379 VIRTIO_VRING_ALIGN); 380 vring->num = vq->vq_nentries; 381 vring->desc = (void *)(uintptr_t)desc_addr; 382 vring->driver = (void *)(uintptr_t)avail_addr; 383 vring->device = (void *)(uintptr_t)used_addr; 384 dev->packed_queues[queue_idx].avail_wrap_counter = true; 385 dev->packed_queues[queue_idx].used_wrap_counter = true; 386 387 for (i = 0; i < vring->num; i++) 388 vring->desc[i].flags = 0; 389 } 390 391 static void 392 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev) 393 { 394 uint16_t queue_idx = vq->vq_queue_index; 395 uint64_t desc_addr, avail_addr, used_addr; 396 397 desc_addr = (uintptr_t)vq->vq_ring_virt_mem; 398 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc); 399 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail, 400 ring[vq->vq_nentries]), 401 VIRTIO_VRING_ALIGN); 402 403 dev->vrings[queue_idx].num = vq->vq_nentries; 404 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr; 405 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr; 406 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr; 407 } 408 409 static int 410 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq) 411 { 412 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 413 414 if (virtio_with_packed_queue(hw)) 415 virtio_user_setup_queue_packed(vq, dev); 416 else 417 virtio_user_setup_queue_split(vq, dev); 418 419 return 0; 420 } 421 422 static void 423 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq) 424 { 425 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU 426 * correspondingly stops the ioeventfds, and reset the status of 427 * the device. 428 * For modern devices, set queue desc, avail, used in PCI bar to 0, 429 * not see any more behavior in QEMU. 430 * 431 * Here we just care about what information to deliver to vhost-user 432 * or vhost-kernel. So we just close ioeventfd for now. 433 */ 434 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 435 436 close(dev->callfds[vq->vq_queue_index]); 437 close(dev->kickfds[vq->vq_queue_index]); 438 } 439 440 static void 441 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq) 442 { 443 uint64_t buf = 1; 444 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 445 446 if (hw->cvq && (hw->cvq->vq == vq)) { 447 if (virtio_with_packed_queue(vq->hw)) 448 virtio_user_handle_cq_packed(dev, vq->vq_queue_index); 449 else 450 virtio_user_handle_cq(dev, vq->vq_queue_index); 451 return; 452 } 453 454 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0) 455 PMD_DRV_LOG(ERR, "failed to kick backend: %s", 456 strerror(errno)); 457 } 458 459 static int 460 virtio_user_dev_close(struct virtio_hw *hw) 461 { 462 struct virtio_user_dev *dev = virtio_user_get_dev(hw); 463 464 virtio_user_dev_uninit(dev); 465 466 return 0; 467 } 468 469 const struct virtio_ops virtio_user_ops = { 470 .read_dev_cfg = virtio_user_read_dev_config, 471 .write_dev_cfg = virtio_user_write_dev_config, 472 .get_status = virtio_user_get_status, 473 .set_status = virtio_user_set_status, 474 .get_features = virtio_user_get_features, 475 .set_features = virtio_user_set_features, 476 .features_ok = virtio_user_features_ok, 477 .get_isr = virtio_user_get_isr, 478 .set_config_irq = virtio_user_set_config_irq, 479 .set_queue_irq = virtio_user_set_queue_irq, 480 .get_queue_num = virtio_user_get_queue_num, 481 .setup_queue = virtio_user_setup_queue, 482 .del_queue = virtio_user_del_queue, 483 .notify_queue = virtio_user_notify_queue, 484 .dev_close = virtio_user_dev_close, 485 }; 486 487 static const char *valid_args[] = { 488 #define VIRTIO_USER_ARG_QUEUES_NUM "queues" 489 VIRTIO_USER_ARG_QUEUES_NUM, 490 #define VIRTIO_USER_ARG_CQ_NUM "cq" 491 VIRTIO_USER_ARG_CQ_NUM, 492 #define VIRTIO_USER_ARG_MAC "mac" 493 VIRTIO_USER_ARG_MAC, 494 #define VIRTIO_USER_ARG_PATH "path" 495 VIRTIO_USER_ARG_PATH, 496 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size" 497 VIRTIO_USER_ARG_QUEUE_SIZE, 498 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface" 499 VIRTIO_USER_ARG_INTERFACE_NAME, 500 #define VIRTIO_USER_ARG_SERVER_MODE "server" 501 VIRTIO_USER_ARG_SERVER_MODE, 502 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf" 503 VIRTIO_USER_ARG_MRG_RXBUF, 504 #define VIRTIO_USER_ARG_IN_ORDER "in_order" 505 VIRTIO_USER_ARG_IN_ORDER, 506 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq" 507 VIRTIO_USER_ARG_PACKED_VQ, 508 #define VIRTIO_USER_ARG_SPEED "speed" 509 VIRTIO_USER_ARG_SPEED, 510 #define VIRTIO_USER_ARG_VECTORIZED "vectorized" 511 VIRTIO_USER_ARG_VECTORIZED, 512 NULL 513 }; 514 515 #define VIRTIO_USER_DEF_CQ_EN 0 516 #define VIRTIO_USER_DEF_Q_NUM 1 517 #define VIRTIO_USER_DEF_Q_SZ 256 518 #define VIRTIO_USER_DEF_SERVER_MODE 0 519 520 static int 521 get_string_arg(const char *key __rte_unused, 522 const char *value, void *extra_args) 523 { 524 if (!value || !extra_args) 525 return -EINVAL; 526 527 *(char **)extra_args = strdup(value); 528 529 if (!*(char **)extra_args) 530 return -ENOMEM; 531 532 return 0; 533 } 534 535 static int 536 get_integer_arg(const char *key __rte_unused, 537 const char *value, void *extra_args) 538 { 539 uint64_t integer = 0; 540 if (!value || !extra_args) 541 return -EINVAL; 542 errno = 0; 543 integer = strtoull(value, NULL, 0); 544 /* extra_args keeps default value, it should be replaced 545 * only in case of successful parsing of the 'value' arg 546 */ 547 if (errno == 0) 548 *(uint64_t *)extra_args = integer; 549 return -errno; 550 } 551 552 static uint32_t 553 vdpa_dynamic_major_num(void) 554 { 555 FILE *fp; 556 char *line = NULL; 557 size_t size; 558 char name[11]; 559 bool found = false; 560 uint32_t num; 561 562 fp = fopen("/proc/devices", "r"); 563 if (fp == NULL) { 564 PMD_INIT_LOG(ERR, "Cannot open /proc/devices: %s", 565 strerror(errno)); 566 return UNNAMED_MAJOR; 567 } 568 569 while (getline(&line, &size, fp) > 0) { 570 char *stripped = line + strspn(line, " "); 571 if ((sscanf(stripped, "%u %10s", &num, name) == 2) && 572 (strncmp(name, "vhost-vdpa", 10) == 0)) { 573 found = true; 574 break; 575 } 576 } 577 fclose(fp); 578 return found ? num : UNNAMED_MAJOR; 579 } 580 581 static enum virtio_user_backend_type 582 virtio_user_backend_type(const char *path) 583 { 584 struct stat sb; 585 586 if (stat(path, &sb) == -1) { 587 if (errno == ENOENT) 588 return VIRTIO_USER_BACKEND_VHOST_USER; 589 590 PMD_INIT_LOG(ERR, "Stat fails: %s (%s)\n", path, 591 strerror(errno)); 592 return VIRTIO_USER_BACKEND_UNKNOWN; 593 } 594 595 if (S_ISSOCK(sb.st_mode)) { 596 return VIRTIO_USER_BACKEND_VHOST_USER; 597 } else if (S_ISCHR(sb.st_mode)) { 598 if (major(sb.st_rdev) == MISC_MAJOR) 599 return VIRTIO_USER_BACKEND_VHOST_KERNEL; 600 if (major(sb.st_rdev) == vdpa_dynamic_major_num()) 601 return VIRTIO_USER_BACKEND_VHOST_VDPA; 602 } 603 return VIRTIO_USER_BACKEND_UNKNOWN; 604 } 605 606 static struct rte_eth_dev * 607 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev) 608 { 609 struct rte_eth_dev *eth_dev; 610 struct rte_eth_dev_data *data; 611 struct virtio_hw *hw; 612 struct virtio_user_dev *dev; 613 614 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*dev)); 615 if (!eth_dev) { 616 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev"); 617 return NULL; 618 } 619 620 data = eth_dev->data; 621 dev = eth_dev->data->dev_private; 622 hw = &dev->hw; 623 624 hw->port_id = data->port_id; 625 dev->port_id = data->port_id; 626 VIRTIO_OPS(hw) = &virtio_user_ops; 627 628 hw->intr_lsc = 1; 629 hw->use_vec_rx = 0; 630 hw->use_vec_tx = 0; 631 hw->use_inorder_rx = 0; 632 hw->use_inorder_tx = 0; 633 634 return eth_dev; 635 } 636 637 static void 638 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev) 639 { 640 rte_eth_dev_release_port(eth_dev); 641 } 642 643 /* Dev initialization routine. Invoked once for each virtio vdev at 644 * EAL init time, see rte_bus_probe(). 645 * Returns 0 on success. 646 */ 647 static int 648 virtio_user_pmd_probe(struct rte_vdev_device *vdev) 649 { 650 struct rte_kvargs *kvlist = NULL; 651 struct rte_eth_dev *eth_dev; 652 struct virtio_hw *hw; 653 struct virtio_user_dev *dev; 654 enum virtio_user_backend_type backend_type = VIRTIO_USER_BACKEND_UNKNOWN; 655 uint64_t queues = VIRTIO_USER_DEF_Q_NUM; 656 uint64_t cq = VIRTIO_USER_DEF_CQ_EN; 657 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ; 658 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE; 659 uint64_t mrg_rxbuf = 1; 660 uint64_t in_order = 1; 661 uint64_t packed_vq = 0; 662 uint64_t vectorized = 0; 663 char *path = NULL; 664 char *ifname = NULL; 665 char *mac_addr = NULL; 666 int ret = -1; 667 668 RTE_BUILD_BUG_ON(offsetof(struct virtio_user_dev, hw) != 0); 669 670 if (rte_eal_process_type() == RTE_PROC_SECONDARY) { 671 const char *name = rte_vdev_device_name(vdev); 672 eth_dev = rte_eth_dev_attach_secondary(name); 673 if (!eth_dev) { 674 PMD_INIT_LOG(ERR, "Failed to probe %s", name); 675 return -1; 676 } 677 678 dev = eth_dev->data->dev_private; 679 hw = &dev->hw; 680 VIRTIO_OPS(hw) = &virtio_user_ops; 681 682 if (eth_virtio_dev_init(eth_dev) < 0) { 683 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails"); 684 rte_eth_dev_release_port(eth_dev); 685 return -1; 686 } 687 688 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops; 689 eth_dev->device = &vdev->device; 690 rte_eth_dev_probing_finish(eth_dev); 691 return 0; 692 } 693 694 kvlist = rte_kvargs_parse(rte_vdev_device_args(vdev), valid_args); 695 if (!kvlist) { 696 PMD_INIT_LOG(ERR, "error when parsing param"); 697 goto end; 698 } 699 700 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) { 701 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH, 702 &get_string_arg, &path) < 0) { 703 PMD_INIT_LOG(ERR, "error to parse %s", 704 VIRTIO_USER_ARG_PATH); 705 goto end; 706 } 707 } else { 708 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user", 709 VIRTIO_USER_ARG_PATH); 710 goto end; 711 } 712 713 backend_type = virtio_user_backend_type(path); 714 if (backend_type == VIRTIO_USER_BACKEND_UNKNOWN) { 715 PMD_INIT_LOG(ERR, 716 "unable to determine backend type for path %s", 717 path); 718 goto end; 719 } 720 PMD_INIT_LOG(INFO, "Backend type detected: %s", 721 virtio_user_backend_strings[backend_type]); 722 723 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) { 724 if (backend_type != VIRTIO_USER_BACKEND_VHOST_KERNEL) { 725 PMD_INIT_LOG(ERR, 726 "arg %s applies only to vhost-kernel backend", 727 VIRTIO_USER_ARG_INTERFACE_NAME); 728 goto end; 729 } 730 731 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME, 732 &get_string_arg, &ifname) < 0) { 733 PMD_INIT_LOG(ERR, "error to parse %s", 734 VIRTIO_USER_ARG_INTERFACE_NAME); 735 goto end; 736 } 737 } 738 739 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) { 740 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC, 741 &get_string_arg, &mac_addr) < 0) { 742 PMD_INIT_LOG(ERR, "error to parse %s", 743 VIRTIO_USER_ARG_MAC); 744 goto end; 745 } 746 } 747 748 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) { 749 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE, 750 &get_integer_arg, &queue_size) < 0) { 751 PMD_INIT_LOG(ERR, "error to parse %s", 752 VIRTIO_USER_ARG_QUEUE_SIZE); 753 goto end; 754 } 755 } 756 757 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) { 758 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM, 759 &get_integer_arg, &queues) < 0) { 760 PMD_INIT_LOG(ERR, "error to parse %s", 761 VIRTIO_USER_ARG_QUEUES_NUM); 762 goto end; 763 } 764 } 765 766 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) { 767 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE, 768 &get_integer_arg, &server_mode) < 0) { 769 PMD_INIT_LOG(ERR, "error to parse %s", 770 VIRTIO_USER_ARG_SERVER_MODE); 771 goto end; 772 } 773 } 774 775 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) { 776 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM, 777 &get_integer_arg, &cq) < 0) { 778 PMD_INIT_LOG(ERR, "error to parse %s", 779 VIRTIO_USER_ARG_CQ_NUM); 780 goto end; 781 } 782 } else if (queues > 1) { 783 cq = 1; 784 } 785 786 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) { 787 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ, 788 &get_integer_arg, &packed_vq) < 0) { 789 PMD_INIT_LOG(ERR, "error to parse %s", 790 VIRTIO_USER_ARG_PACKED_VQ); 791 goto end; 792 } 793 } 794 795 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_VECTORIZED) == 1) { 796 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_VECTORIZED, 797 &get_integer_arg, &vectorized) < 0) { 798 PMD_INIT_LOG(ERR, "error to parse %s", 799 VIRTIO_USER_ARG_VECTORIZED); 800 goto end; 801 } 802 } 803 804 if (queues > 1 && cq == 0) { 805 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q"); 806 goto end; 807 } 808 809 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) { 810 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u", 811 VIRTIO_USER_ARG_QUEUES_NUM, queues, 812 VIRTIO_MAX_VIRTQUEUE_PAIRS); 813 goto end; 814 } 815 816 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) { 817 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF, 818 &get_integer_arg, &mrg_rxbuf) < 0) { 819 PMD_INIT_LOG(ERR, "error to parse %s", 820 VIRTIO_USER_ARG_MRG_RXBUF); 821 goto end; 822 } 823 } 824 825 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) { 826 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER, 827 &get_integer_arg, &in_order) < 0) { 828 PMD_INIT_LOG(ERR, "error to parse %s", 829 VIRTIO_USER_ARG_IN_ORDER); 830 goto end; 831 } 832 } 833 834 eth_dev = virtio_user_eth_dev_alloc(vdev); 835 if (!eth_dev) { 836 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device"); 837 goto end; 838 } 839 840 dev = eth_dev->data->dev_private; 841 hw = &dev->hw; 842 if (virtio_user_dev_init(dev, path, queues, cq, 843 queue_size, mac_addr, &ifname, server_mode, 844 mrg_rxbuf, in_order, packed_vq, backend_type) < 0) { 845 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails"); 846 virtio_user_eth_dev_free(eth_dev); 847 goto end; 848 } 849 850 /* previously called by pci probing for physical dev */ 851 if (eth_virtio_dev_init(eth_dev) < 0) { 852 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails"); 853 virtio_user_eth_dev_free(eth_dev); 854 goto end; 855 } 856 857 if (vectorized) { 858 if (packed_vq) { 859 #if defined(CC_AVX512_SUPPORT) || defined(RTE_ARCH_ARM) 860 hw->use_vec_rx = 1; 861 hw->use_vec_tx = 1; 862 #else 863 PMD_INIT_LOG(INFO, 864 "building environment do not support packed ring vectorized"); 865 #endif 866 } else { 867 hw->use_vec_rx = 1; 868 } 869 } 870 871 rte_eth_dev_probing_finish(eth_dev); 872 ret = 0; 873 874 end: 875 if (kvlist) 876 rte_kvargs_free(kvlist); 877 if (path) 878 free(path); 879 if (mac_addr) 880 free(mac_addr); 881 if (ifname) 882 free(ifname); 883 return ret; 884 } 885 886 static int 887 virtio_user_pmd_remove(struct rte_vdev_device *vdev) 888 { 889 const char *name; 890 struct rte_eth_dev *eth_dev; 891 892 if (!vdev) 893 return -EINVAL; 894 895 name = rte_vdev_device_name(vdev); 896 PMD_DRV_LOG(INFO, "Un-Initializing %s", name); 897 eth_dev = rte_eth_dev_allocated(name); 898 /* Port has already been released by close. */ 899 if (!eth_dev) 900 return 0; 901 902 if (rte_eal_process_type() != RTE_PROC_PRIMARY) 903 return rte_eth_dev_release_port(eth_dev); 904 905 /* make sure the device is stopped, queues freed */ 906 return rte_eth_dev_close(eth_dev->data->port_id); 907 } 908 909 static int virtio_user_pmd_dma_map(struct rte_vdev_device *vdev, void *addr, 910 uint64_t iova, size_t len) 911 { 912 const char *name; 913 struct rte_eth_dev *eth_dev; 914 struct virtio_user_dev *dev; 915 916 if (!vdev) 917 return -EINVAL; 918 919 name = rte_vdev_device_name(vdev); 920 eth_dev = rte_eth_dev_allocated(name); 921 /* Port has already been released by close. */ 922 if (!eth_dev) 923 return 0; 924 925 dev = eth_dev->data->dev_private; 926 927 if (dev->ops->dma_map) 928 return dev->ops->dma_map(dev, addr, iova, len); 929 930 return 0; 931 } 932 933 static int virtio_user_pmd_dma_unmap(struct rte_vdev_device *vdev, void *addr, 934 uint64_t iova, size_t len) 935 { 936 const char *name; 937 struct rte_eth_dev *eth_dev; 938 struct virtio_user_dev *dev; 939 940 if (!vdev) 941 return -EINVAL; 942 943 name = rte_vdev_device_name(vdev); 944 eth_dev = rte_eth_dev_allocated(name); 945 /* Port has already been released by close. */ 946 if (!eth_dev) 947 return 0; 948 949 dev = eth_dev->data->dev_private; 950 951 if (dev->ops->dma_unmap) 952 return dev->ops->dma_unmap(dev, addr, iova, len); 953 954 return 0; 955 } 956 957 static struct rte_vdev_driver virtio_user_driver = { 958 .probe = virtio_user_pmd_probe, 959 .remove = virtio_user_pmd_remove, 960 .dma_map = virtio_user_pmd_dma_map, 961 .dma_unmap = virtio_user_pmd_dma_unmap, 962 .drv_flags = RTE_VDEV_DRV_NEED_IOVA_AS_VA, 963 }; 964 965 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver); 966 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user); 967 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user, 968 "path=<path> " 969 "mac=<mac addr> " 970 "cq=<int> " 971 "queue_size=<int> " 972 "queues=<int> " 973 "iface=<string> " 974 "server=<0|1> " 975 "mrg_rxbuf=<0|1> " 976 "in_order=<0|1> " 977 "packed_vq=<0|1> " 978 "speed=<int> " 979 "vectorized=<0|1>"); 980