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