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