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