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