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"
24 #define virtio_user_get_dev(hw) \
25 ((struct virtio_user_dev *)(hw)->virtio_user_dev)
28 virtio_user_server_reconnect(struct virtio_user_dev *dev)
32 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
34 connectfd = accept(dev->listenfd, NULL, NULL);
38 dev->vhostfd = connectfd;
39 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
40 &dev->device_features) < 0) {
41 PMD_INIT_LOG(ERR, "get_features failed: %s",
46 dev->device_features |= dev->frontend_features;
48 /* umask vhost-user unsupported features */
49 dev->device_features &= ~(dev->unsupported_features);
51 dev->features &= dev->device_features;
53 ret = virtio_user_start_device(dev);
57 if (dev->queue_pairs > 1) {
58 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
60 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
64 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
65 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
66 PMD_DRV_LOG(ERR, "interrupt disable failed");
69 rte_intr_callback_unregister(eth_dev->intr_handle,
70 virtio_interrupt_handler,
72 eth_dev->intr_handle->fd = connectfd;
73 rte_intr_callback_register(eth_dev->intr_handle,
74 virtio_interrupt_handler, eth_dev);
76 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
77 PMD_DRV_LOG(ERR, "interrupt enable failed");
81 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
86 virtio_user_delayed_handler(void *param)
88 struct virtio_hw *hw = (struct virtio_hw *)param;
89 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
90 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
92 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
93 PMD_DRV_LOG(ERR, "interrupt disable failed");
96 rte_intr_callback_unregister(eth_dev->intr_handle,
97 virtio_interrupt_handler, eth_dev);
99 if (dev->vhostfd >= 0) {
103 eth_dev->intr_handle->fd = dev->listenfd;
104 rte_intr_callback_register(eth_dev->intr_handle,
105 virtio_interrupt_handler, eth_dev);
106 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
107 PMD_DRV_LOG(ERR, "interrupt enable failed");
114 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
115 void *dst, int length)
118 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
120 if (offset == offsetof(struct virtio_net_config, mac) &&
121 length == ETHER_ADDR_LEN) {
122 for (i = 0; i < ETHER_ADDR_LEN; ++i)
123 ((uint8_t *)dst)[i] = dev->mac_addr[i];
127 if (offset == offsetof(struct virtio_net_config, status)) {
130 if (dev->vhostfd >= 0) {
134 flags = fcntl(dev->vhostfd, F_GETFL);
135 if (fcntl(dev->vhostfd, F_SETFL,
136 flags | O_NONBLOCK) == -1) {
137 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
140 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
141 if (r == 0 || (r < 0 && errno != EAGAIN)) {
142 dev->status &= (~VIRTIO_NET_S_LINK_UP);
143 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
146 /* This function could be called in the process
147 * of interrupt handling, callback cannot be
148 * unregistered here, set an alarm to do it.
151 virtio_user_delayed_handler,
154 dev->status |= VIRTIO_NET_S_LINK_UP;
156 if (fcntl(dev->vhostfd, F_SETFL,
157 flags & ~O_NONBLOCK) == -1) {
158 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
161 } else if (dev->is_server) {
162 dev->status &= (~VIRTIO_NET_S_LINK_UP);
163 if (virtio_user_server_reconnect(dev) >= 0)
164 dev->status |= VIRTIO_NET_S_LINK_UP;
167 *(uint16_t *)dst = dev->status;
170 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
171 *(uint16_t *)dst = dev->max_queue_pairs;
175 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
176 const void *src, int length)
179 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
181 if ((offset == offsetof(struct virtio_net_config, mac)) &&
182 (length == ETHER_ADDR_LEN))
183 for (i = 0; i < ETHER_ADDR_LEN; ++i)
184 dev->mac_addr[i] = ((const uint8_t *)src)[i];
186 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
191 virtio_user_reset(struct virtio_hw *hw)
193 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
195 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
196 virtio_user_stop_device(dev);
200 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
202 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
204 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
205 virtio_user_start_device(dev);
206 else if (status == VIRTIO_CONFIG_STATUS_RESET)
207 virtio_user_reset(hw);
208 dev->status = status;
212 virtio_user_get_status(struct virtio_hw *hw)
214 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
220 virtio_user_get_features(struct virtio_hw *hw)
222 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
224 /* unmask feature bits defined in vhost user protocol */
225 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
229 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
231 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
233 dev->features = features & dev->device_features;
237 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
239 /* rxq interrupts and config interrupt are separated in virtio-user,
240 * here we only report config change.
242 return VIRTIO_PCI_ISR_CONFIG;
246 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
247 uint16_t vec __rte_unused)
253 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
254 struct virtqueue *vq __rte_unused,
257 /* pretend we have done that */
261 /* This function is to get the queue size, aka, number of descs, of a specified
262 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
263 * max supported queues.
266 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
268 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
270 /* Currently, each queue has same queue size */
271 return dev->queue_size;
275 virtio_user_setup_queue_packed(struct virtqueue *vq,
276 struct virtio_user_dev *dev)
278 uint16_t queue_idx = vq->vq_queue_index;
279 struct vring_packed *vring;
285 vring = &dev->packed_vrings[queue_idx];
286 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
287 avail_addr = desc_addr + vq->vq_nentries *
288 sizeof(struct vring_packed_desc);
289 used_addr = RTE_ALIGN_CEIL(avail_addr +
290 sizeof(struct vring_packed_desc_event),
291 VIRTIO_PCI_VRING_ALIGN);
292 vring->num = vq->vq_nentries;
294 (void *)(uintptr_t)desc_addr;
295 vring->driver_event =
296 (void *)(uintptr_t)avail_addr;
297 vring->device_event =
298 (void *)(uintptr_t)used_addr;
299 dev->packed_queues[queue_idx].avail_wrap_counter = true;
300 dev->packed_queues[queue_idx].used_wrap_counter = true;
302 for (i = 0; i < vring->num; i++)
303 vring->desc_packed[i].flags = 0;
307 virtio_user_setup_queue_split(struct virtqueue *vq, struct virtio_user_dev *dev)
309 uint16_t queue_idx = vq->vq_queue_index;
310 uint64_t desc_addr, avail_addr, used_addr;
312 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
313 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
314 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
315 ring[vq->vq_nentries]),
316 VIRTIO_PCI_VRING_ALIGN);
318 dev->vrings[queue_idx].num = vq->vq_nentries;
319 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
320 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
321 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
325 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
327 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
329 if (vtpci_packed_queue(hw))
330 virtio_user_setup_queue_packed(vq, dev);
332 virtio_user_setup_queue_split(vq, dev);
338 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
340 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
341 * correspondingly stops the ioeventfds, and reset the status of
343 * For modern devices, set queue desc, avail, used in PCI bar to 0,
344 * not see any more behavior in QEMU.
346 * Here we just care about what information to deliver to vhost-user
347 * or vhost-kernel. So we just close ioeventfd for now.
349 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
351 close(dev->callfds[vq->vq_queue_index]);
352 close(dev->kickfds[vq->vq_queue_index]);
356 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
359 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
361 if (hw->cvq && (hw->cvq->vq == vq)) {
362 if (vtpci_packed_queue(vq->hw))
363 virtio_user_handle_cq_packed(dev, vq->vq_queue_index);
365 virtio_user_handle_cq(dev, vq->vq_queue_index);
369 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
370 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
374 const struct virtio_pci_ops virtio_user_ops = {
375 .read_dev_cfg = virtio_user_read_dev_config,
376 .write_dev_cfg = virtio_user_write_dev_config,
377 .get_status = virtio_user_get_status,
378 .set_status = virtio_user_set_status,
379 .get_features = virtio_user_get_features,
380 .set_features = virtio_user_set_features,
381 .get_isr = virtio_user_get_isr,
382 .set_config_irq = virtio_user_set_config_irq,
383 .set_queue_irq = virtio_user_set_queue_irq,
384 .get_queue_num = virtio_user_get_queue_num,
385 .setup_queue = virtio_user_setup_queue,
386 .del_queue = virtio_user_del_queue,
387 .notify_queue = virtio_user_notify_queue,
390 static const char *valid_args[] = {
391 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
392 VIRTIO_USER_ARG_QUEUES_NUM,
393 #define VIRTIO_USER_ARG_CQ_NUM "cq"
394 VIRTIO_USER_ARG_CQ_NUM,
395 #define VIRTIO_USER_ARG_MAC "mac"
397 #define VIRTIO_USER_ARG_PATH "path"
398 VIRTIO_USER_ARG_PATH,
399 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
400 VIRTIO_USER_ARG_QUEUE_SIZE,
401 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
402 VIRTIO_USER_ARG_INTERFACE_NAME,
403 #define VIRTIO_USER_ARG_SERVER_MODE "server"
404 VIRTIO_USER_ARG_SERVER_MODE,
405 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
406 VIRTIO_USER_ARG_MRG_RXBUF,
407 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
408 VIRTIO_USER_ARG_IN_ORDER,
409 #define VIRTIO_USER_ARG_PACKED_VQ "packed_vq"
410 VIRTIO_USER_ARG_PACKED_VQ,
414 #define VIRTIO_USER_DEF_CQ_EN 0
415 #define VIRTIO_USER_DEF_Q_NUM 1
416 #define VIRTIO_USER_DEF_Q_SZ 256
417 #define VIRTIO_USER_DEF_SERVER_MODE 0
420 get_string_arg(const char *key __rte_unused,
421 const char *value, void *extra_args)
423 if (!value || !extra_args)
426 *(char **)extra_args = strdup(value);
428 if (!*(char **)extra_args)
435 get_integer_arg(const char *key __rte_unused,
436 const char *value, void *extra_args)
438 if (!value || !extra_args)
441 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
446 static struct rte_vdev_driver virtio_user_driver;
448 static struct rte_eth_dev *
449 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
451 struct rte_eth_dev *eth_dev;
452 struct rte_eth_dev_data *data;
453 struct virtio_hw *hw;
454 struct virtio_user_dev *dev;
456 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
458 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
462 data = eth_dev->data;
463 hw = eth_dev->data->dev_private;
465 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
467 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
468 rte_eth_dev_release_port(eth_dev);
472 hw->port_id = data->port_id;
473 dev->port_id = data->port_id;
474 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
476 * MSIX is required to enable LSC (see virtio_init_device).
477 * Here just pretend that we support msix.
481 hw->use_simple_rx = 0;
482 hw->use_inorder_rx = 0;
483 hw->use_inorder_tx = 0;
484 hw->virtio_user_dev = dev;
489 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
491 struct rte_eth_dev_data *data = eth_dev->data;
492 struct virtio_hw *hw = data->dev_private;
494 rte_free(hw->virtio_user_dev);
495 rte_eth_dev_release_port(eth_dev);
498 /* Dev initialization routine. Invoked once for each virtio vdev at
499 * EAL init time, see rte_bus_probe().
500 * Returns 0 on success.
503 virtio_user_pmd_probe(struct rte_vdev_device *dev)
505 struct rte_kvargs *kvlist = NULL;
506 struct rte_eth_dev *eth_dev;
507 struct virtio_hw *hw;
508 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
509 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
510 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
511 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
512 uint64_t mrg_rxbuf = 1;
513 uint64_t in_order = 1;
514 uint64_t packed_vq = 0;
517 char *mac_addr = NULL;
520 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
522 PMD_INIT_LOG(ERR, "error when parsing param");
526 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
527 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
528 &get_string_arg, &path) < 0) {
529 PMD_INIT_LOG(ERR, "error to parse %s",
530 VIRTIO_USER_ARG_PATH);
534 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
535 VIRTIO_USER_ARG_PATH);
539 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
540 if (is_vhost_user_by_type(path)) {
542 "arg %s applies only to vhost-kernel backend",
543 VIRTIO_USER_ARG_INTERFACE_NAME);
547 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
548 &get_string_arg, &ifname) < 0) {
549 PMD_INIT_LOG(ERR, "error to parse %s",
550 VIRTIO_USER_ARG_INTERFACE_NAME);
555 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
556 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
557 &get_string_arg, &mac_addr) < 0) {
558 PMD_INIT_LOG(ERR, "error to parse %s",
559 VIRTIO_USER_ARG_MAC);
564 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
565 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
566 &get_integer_arg, &queue_size) < 0) {
567 PMD_INIT_LOG(ERR, "error to parse %s",
568 VIRTIO_USER_ARG_QUEUE_SIZE);
573 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
574 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
575 &get_integer_arg, &queues) < 0) {
576 PMD_INIT_LOG(ERR, "error to parse %s",
577 VIRTIO_USER_ARG_QUEUES_NUM);
582 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
583 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
584 &get_integer_arg, &server_mode) < 0) {
585 PMD_INIT_LOG(ERR, "error to parse %s",
586 VIRTIO_USER_ARG_SERVER_MODE);
591 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
592 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
593 &get_integer_arg, &cq) < 0) {
594 PMD_INIT_LOG(ERR, "error to parse %s",
595 VIRTIO_USER_ARG_CQ_NUM);
598 } else if (queues > 1) {
602 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PACKED_VQ) == 1) {
603 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PACKED_VQ,
604 &get_integer_arg, &packed_vq) < 0) {
605 PMD_INIT_LOG(ERR, "error to parse %s",
606 VIRTIO_USER_ARG_PACKED_VQ);
611 if (queues > 1 && cq == 0) {
612 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
616 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
617 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
618 VIRTIO_USER_ARG_QUEUES_NUM, queues,
619 VIRTIO_MAX_VIRTQUEUE_PAIRS);
623 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
624 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
625 &get_integer_arg, &mrg_rxbuf) < 0) {
626 PMD_INIT_LOG(ERR, "error to parse %s",
627 VIRTIO_USER_ARG_MRG_RXBUF);
632 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
633 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
634 &get_integer_arg, &in_order) < 0) {
635 PMD_INIT_LOG(ERR, "error to parse %s",
636 VIRTIO_USER_ARG_IN_ORDER);
641 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
642 struct virtio_user_dev *vu_dev;
644 eth_dev = virtio_user_eth_dev_alloc(dev);
646 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
650 hw = eth_dev->data->dev_private;
651 vu_dev = virtio_user_get_dev(hw);
652 if (server_mode == 1)
653 vu_dev->is_server = true;
655 vu_dev->is_server = false;
656 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
657 queue_size, mac_addr, &ifname, mrg_rxbuf,
658 in_order, packed_vq) < 0) {
659 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
660 virtio_user_eth_dev_free(eth_dev);
665 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
670 /* previously called by rte_pci_probe() for physical dev */
671 if (eth_virtio_dev_init(eth_dev) < 0) {
672 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
673 virtio_user_eth_dev_free(eth_dev);
677 rte_eth_dev_probing_finish(eth_dev);
682 rte_kvargs_free(kvlist);
693 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
696 struct rte_eth_dev *eth_dev;
697 struct virtio_hw *hw;
698 struct virtio_user_dev *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);
709 /* make sure the device is stopped, queues freed */
710 rte_eth_dev_close(eth_dev->data->port_id);
712 hw = eth_dev->data->dev_private;
713 dev = hw->virtio_user_dev;
714 virtio_user_dev_uninit(dev);
716 rte_eth_dev_release_port(eth_dev);
721 static struct rte_vdev_driver virtio_user_driver = {
722 .probe = virtio_user_pmd_probe,
723 .remove = virtio_user_pmd_remove,
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,