1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
9 #include <sys/socket.h>
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>
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"
25 #define virtio_user_get_dev(hw) \
26 ((struct virtio_user_dev *)(hw)->virtio_user_dev)
29 virtio_user_server_reconnect(struct virtio_user_dev *dev)
33 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
35 connectfd = accept(dev->listenfd, NULL, NULL);
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",
47 dev->device_features |= dev->frontend_features;
49 /* umask vhost-user unsupported features */
50 dev->device_features &= ~(dev->unsupported_features);
52 dev->features &= dev->device_features;
54 ret = virtio_user_start_device(dev);
58 if (dev->queue_pairs > 1) {
59 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
61 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
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");
70 rte_intr_callback_unregister(eth_dev->intr_handle,
71 virtio_interrupt_handler,
73 eth_dev->intr_handle->fd = connectfd;
74 rte_intr_callback_register(eth_dev->intr_handle,
75 virtio_interrupt_handler, eth_dev);
77 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
78 PMD_DRV_LOG(ERR, "interrupt enable failed");
82 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
87 virtio_user_delayed_handler(void *param)
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);
93 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
94 PMD_DRV_LOG(ERR, "interrupt disable failed");
97 rte_intr_callback_unregister(eth_dev->intr_handle,
98 virtio_interrupt_handler, eth_dev);
100 if (dev->vhostfd >= 0) {
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");
115 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
116 void *dst, int length)
119 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
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];
128 if (offset == offsetof(struct virtio_net_config, status)) {
131 if (dev->vhostfd >= 0) {
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");
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",
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.
152 virtio_user_delayed_handler,
155 dev->status |= VIRTIO_NET_S_LINK_UP;
157 if (fcntl(dev->vhostfd, F_SETFL,
158 flags & ~O_NONBLOCK) == -1) {
159 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
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;
168 *(uint16_t *)dst = dev->status;
171 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
172 *(uint16_t *)dst = dev->max_queue_pairs;
176 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
177 const void *src, int length)
180 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
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];
187 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
192 virtio_user_reset(struct virtio_hw *hw)
194 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
196 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
197 virtio_user_stop_device(dev);
201 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
203 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
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;
213 virtio_user_get_status(struct virtio_hw *hw)
215 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
221 virtio_user_get_features(struct virtio_hw *hw)
223 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
225 /* unmask feature bits defined in vhost user protocol */
226 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
230 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
232 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
234 dev->features = features & dev->device_features;
238 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
240 /* rxq interrupts and config interrupt are separated in virtio-user,
241 * here we only report config change.
243 return VIRTIO_PCI_ISR_CONFIG;
247 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
248 uint16_t vec __rte_unused)
254 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
255 struct virtqueue *vq __rte_unused,
258 /* pretend we have done that */
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.
267 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
269 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
271 /* Currently, each queue has same queue size */
272 return dev->queue_size;
276 virtio_user_setup_queue_packed(struct virtqueue *vq,
277 struct virtio_user_dev *dev)
279 uint16_t queue_idx = vq->vq_queue_index;
280 struct vring_packed *vring;
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;
300 for (i = 0; i < vring->num; i++)
301 vring->desc[i].flags = 0;
305 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
307 uint16_t queue_idx = vq->vq_queue_index;
308 uint64_t desc_addr, avail_addr, used_addr;
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);
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;
323 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
325 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
327 if (vtpci_packed_queue(hw))
328 virtio_user_setup_queue_packed(vq, dev);
330 virtio_user_setup_queue_split(vq, dev);
336 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
338 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
339 * correspondingly stops the ioeventfds, and reset the status of
341 * For modern devices, set queue desc, avail, used in PCI bar to 0,
342 * not see any more behavior in QEMU.
344 * Here we just care about what information to deliver to vhost-user
345 * or vhost-kernel. So we just close ioeventfd for now.
347 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
349 close(dev->callfds[vq->vq_queue_index]);
350 close(dev->kickfds[vq->vq_queue_index]);
354 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
357 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
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);
363 virtio_user_handle_cq(dev, vq->vq_queue_index);
367 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
368 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
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,
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"
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,
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
418 get_string_arg(const char *key __rte_unused,
419 const char *value, void *extra_args)
421 if (!value || !extra_args)
424 *(char **)extra_args = strdup(value);
426 if (!*(char **)extra_args)
433 get_integer_arg(const char *key __rte_unused,
434 const char *value, void *extra_args)
436 if (!value || !extra_args)
439 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
444 static struct rte_eth_dev *
445 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
447 struct rte_eth_dev *eth_dev;
448 struct rte_eth_dev_data *data;
449 struct virtio_hw *hw;
450 struct virtio_user_dev *dev;
452 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
454 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
458 data = eth_dev->data;
459 hw = eth_dev->data->dev_private;
461 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
463 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
464 rte_eth_dev_release_port(eth_dev);
468 hw->port_id = data->port_id;
469 dev->port_id = data->port_id;
470 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
472 * MSIX is required to enable LSC (see virtio_init_device).
473 * Here just pretend that we support msix.
477 hw->use_simple_rx = 0;
478 hw->use_inorder_rx = 0;
479 hw->use_inorder_tx = 0;
480 hw->virtio_user_dev = dev;
485 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
487 struct rte_eth_dev_data *data = eth_dev->data;
488 struct virtio_hw *hw = data->dev_private;
490 rte_free(hw->virtio_user_dev);
491 rte_eth_dev_release_port(eth_dev);
494 /* Dev initialization routine. Invoked once for each virtio vdev at
495 * EAL init time, see rte_bus_probe().
496 * Returns 0 on success.
499 virtio_user_pmd_probe(struct rte_vdev_device *dev)
501 struct rte_kvargs *kvlist = NULL;
502 struct rte_eth_dev *eth_dev;
503 struct virtio_hw *hw;
504 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
505 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
506 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
507 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
508 uint64_t mrg_rxbuf = 1;
509 uint64_t in_order = 1;
510 uint64_t packed_vq = 0;
513 char *mac_addr = NULL;
516 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
517 const char *name = rte_vdev_device_name(dev);
518 eth_dev = rte_eth_dev_attach_secondary(name);
520 RTE_LOG(ERR, PMD, "Failed to probe %s\n", name);
524 if (eth_virtio_dev_init(eth_dev) < 0) {
525 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
526 rte_eth_dev_release_port(eth_dev);
530 eth_dev->dev_ops = &virtio_user_secondary_eth_dev_ops;
531 eth_dev->device = &dev->device;
532 rte_eth_dev_probing_finish(eth_dev);
536 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
538 PMD_INIT_LOG(ERR, "error when parsing param");
542 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
543 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
544 &get_string_arg, &path) < 0) {
545 PMD_INIT_LOG(ERR, "error to parse %s",
546 VIRTIO_USER_ARG_PATH);
550 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
551 VIRTIO_USER_ARG_PATH);
555 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
556 if (is_vhost_user_by_type(path)) {
558 "arg %s applies only to vhost-kernel backend",
559 VIRTIO_USER_ARG_INTERFACE_NAME);
563 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
564 &get_string_arg, &ifname) < 0) {
565 PMD_INIT_LOG(ERR, "error to parse %s",
566 VIRTIO_USER_ARG_INTERFACE_NAME);
571 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
572 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
573 &get_string_arg, &mac_addr) < 0) {
574 PMD_INIT_LOG(ERR, "error to parse %s",
575 VIRTIO_USER_ARG_MAC);
580 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
581 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
582 &get_integer_arg, &queue_size) < 0) {
583 PMD_INIT_LOG(ERR, "error to parse %s",
584 VIRTIO_USER_ARG_QUEUE_SIZE);
589 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
590 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
591 &get_integer_arg, &queues) < 0) {
592 PMD_INIT_LOG(ERR, "error to parse %s",
593 VIRTIO_USER_ARG_QUEUES_NUM);
598 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
599 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
600 &get_integer_arg, &server_mode) < 0) {
601 PMD_INIT_LOG(ERR, "error to parse %s",
602 VIRTIO_USER_ARG_SERVER_MODE);
607 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
608 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
609 &get_integer_arg, &cq) < 0) {
610 PMD_INIT_LOG(ERR, "error to parse %s",
611 VIRTIO_USER_ARG_CQ_NUM);
614 } else if (queues > 1) {
618 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
619 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
620 &get_integer_arg, &packed_vq) < 0) {
621 PMD_INIT_LOG(ERR, "error to parse %s",
622 VIRTIO_USER_ARG_PACKED_VQ);
627 if (queues > 1 && cq == 0) {
628 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
632 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
633 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
634 VIRTIO_USER_ARG_QUEUES_NUM, queues,
635 VIRTIO_MAX_VIRTQUEUE_PAIRS);
639 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
640 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
641 &get_integer_arg, &mrg_rxbuf) < 0) {
642 PMD_INIT_LOG(ERR, "error to parse %s",
643 VIRTIO_USER_ARG_MRG_RXBUF);
648 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
649 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
650 &get_integer_arg, &in_order) < 0) {
651 PMD_INIT_LOG(ERR, "error to parse %s",
652 VIRTIO_USER_ARG_IN_ORDER);
657 eth_dev = virtio_user_eth_dev_alloc(dev);
659 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
663 hw = eth_dev->data->dev_private;
664 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
665 queue_size, mac_addr, &ifname, server_mode,
666 mrg_rxbuf, in_order, packed_vq) < 0) {
667 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
668 virtio_user_eth_dev_free(eth_dev);
672 /* previously called by rte_pci_probe() for physical dev */
673 if (eth_virtio_dev_init(eth_dev) < 0) {
674 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
675 virtio_user_eth_dev_free(eth_dev);
679 rte_eth_dev_probing_finish(eth_dev);
684 rte_kvargs_free(kvlist);
695 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
698 struct rte_eth_dev *eth_dev;
703 name = rte_vdev_device_name(vdev);
704 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
705 eth_dev = rte_eth_dev_allocated(name);
706 /* Port has already been released by close. */
710 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
711 return rte_eth_dev_release_port(eth_dev);
713 /* make sure the device is stopped, queues freed */
714 rte_eth_dev_close(eth_dev->data->port_id);
719 static struct rte_vdev_driver virtio_user_driver = {
720 .probe = virtio_user_pmd_probe,
721 .remove = virtio_user_pmd_remove,
724 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
725 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
726 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,