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