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