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)
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 /* umask vhost-user unsupported features */
48 dev->device_features &= ~(dev->unsupported_features);
50 dev->features &= dev->device_features;
52 flag = fcntl(connectfd, F_GETFD);
53 fcntl(connectfd, F_SETFL, flag | O_NONBLOCK);
55 ret = virtio_user_start_device(dev);
59 if (dev->queue_pairs > 1) {
60 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
62 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
66 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
67 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
68 PMD_DRV_LOG(ERR, "interrupt disable failed");
71 rte_intr_callback_unregister(eth_dev->intr_handle,
72 virtio_interrupt_handler,
74 eth_dev->intr_handle->fd = connectfd;
75 rte_intr_callback_register(eth_dev->intr_handle,
76 virtio_interrupt_handler, eth_dev);
78 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
79 PMD_DRV_LOG(ERR, "interrupt enable failed");
83 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
88 virtio_user_delayed_handler(void *param)
90 struct virtio_hw *hw = (struct virtio_hw *)param;
91 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
92 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
94 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
95 PMD_DRV_LOG(ERR, "interrupt disable failed");
98 rte_intr_callback_unregister(eth_dev->intr_handle,
99 virtio_interrupt_handler, eth_dev);
100 if (dev->is_server) {
101 if (dev->vhostfd >= 0) {
105 eth_dev->intr_handle->fd = dev->listenfd;
106 rte_intr_callback_register(eth_dev->intr_handle,
107 virtio_interrupt_handler, eth_dev);
108 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
109 PMD_DRV_LOG(ERR, "interrupt enable failed");
116 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
117 void *dst, int length)
120 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
122 if (offset == offsetof(struct virtio_net_config, mac) &&
123 length == ETHER_ADDR_LEN) {
124 for (i = 0; i < ETHER_ADDR_LEN; ++i)
125 ((uint8_t *)dst)[i] = dev->mac_addr[i];
129 if (offset == offsetof(struct virtio_net_config, status)) {
132 if (dev->vhostfd >= 0) {
136 flags = fcntl(dev->vhostfd, F_GETFL);
137 if (fcntl(dev->vhostfd, F_SETFL,
138 flags | O_NONBLOCK) == -1) {
139 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
142 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
143 if (r == 0 || (r < 0 && errno != EAGAIN)) {
144 dev->status &= (~VIRTIO_NET_S_LINK_UP);
145 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
148 /* This function could be called in the process
149 * of interrupt handling, callback cannot be
150 * unregistered here, set an alarm to do it.
153 virtio_user_delayed_handler,
156 dev->status |= VIRTIO_NET_S_LINK_UP;
158 if (fcntl(dev->vhostfd, F_SETFL,
159 flags & ~O_NONBLOCK) == -1) {
160 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
163 } else if (dev->is_server) {
164 dev->status &= (~VIRTIO_NET_S_LINK_UP);
165 if (virtio_user_server_reconnect(dev) >= 0)
166 dev->status |= VIRTIO_NET_S_LINK_UP;
169 *(uint16_t *)dst = dev->status;
172 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
173 *(uint16_t *)dst = dev->max_queue_pairs;
177 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
178 const void *src, int length)
181 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
183 if ((offset == offsetof(struct virtio_net_config, mac)) &&
184 (length == ETHER_ADDR_LEN))
185 for (i = 0; i < ETHER_ADDR_LEN; ++i)
186 dev->mac_addr[i] = ((const uint8_t *)src)[i];
188 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
193 virtio_user_reset(struct virtio_hw *hw)
195 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
197 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
198 virtio_user_stop_device(dev);
202 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
204 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
206 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
207 virtio_user_start_device(dev);
208 else if (status == VIRTIO_CONFIG_STATUS_RESET)
209 virtio_user_reset(hw);
210 dev->status = status;
214 virtio_user_get_status(struct virtio_hw *hw)
216 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
222 virtio_user_get_features(struct virtio_hw *hw)
224 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
226 /* unmask feature bits defined in vhost user protocol */
227 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
231 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
233 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
235 dev->features = features & dev->device_features;
239 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
241 /* rxq interrupts and config interrupt are separated in virtio-user,
242 * here we only report config change.
244 return VIRTIO_PCI_ISR_CONFIG;
248 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
249 uint16_t vec __rte_unused)
255 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
256 struct virtqueue *vq __rte_unused,
259 /* pretend we have done that */
263 /* This function is to get the queue size, aka, number of descs, of a specified
264 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
265 * max supported queues.
268 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
270 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
272 /* Currently, each queue has same queue size */
273 return dev->queue_size;
277 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
279 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
280 uint16_t queue_idx = vq->vq_queue_index;
281 uint64_t desc_addr, avail_addr, used_addr;
283 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
284 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
285 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
286 ring[vq->vq_nentries]),
287 VIRTIO_PCI_VRING_ALIGN);
289 dev->vrings[queue_idx].num = vq->vq_nentries;
290 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
291 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
292 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
298 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
300 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
301 * correspondingly stops the ioeventfds, and reset the status of
303 * For modern devices, set queue desc, avail, used in PCI bar to 0,
304 * not see any more behavior in QEMU.
306 * Here we just care about what information to deliver to vhost-user
307 * or vhost-kernel. So we just close ioeventfd for now.
309 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
311 close(dev->callfds[vq->vq_queue_index]);
312 close(dev->kickfds[vq->vq_queue_index]);
316 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
319 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
321 if (hw->cvq && (hw->cvq->vq == vq)) {
322 virtio_user_handle_cq(dev, vq->vq_queue_index);
326 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
327 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
331 const struct virtio_pci_ops virtio_user_ops = {
332 .read_dev_cfg = virtio_user_read_dev_config,
333 .write_dev_cfg = virtio_user_write_dev_config,
334 .get_status = virtio_user_get_status,
335 .set_status = virtio_user_set_status,
336 .get_features = virtio_user_get_features,
337 .set_features = virtio_user_set_features,
338 .get_isr = virtio_user_get_isr,
339 .set_config_irq = virtio_user_set_config_irq,
340 .set_queue_irq = virtio_user_set_queue_irq,
341 .get_queue_num = virtio_user_get_queue_num,
342 .setup_queue = virtio_user_setup_queue,
343 .del_queue = virtio_user_del_queue,
344 .notify_queue = virtio_user_notify_queue,
347 static const char *valid_args[] = {
348 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
349 VIRTIO_USER_ARG_QUEUES_NUM,
350 #define VIRTIO_USER_ARG_CQ_NUM "cq"
351 VIRTIO_USER_ARG_CQ_NUM,
352 #define VIRTIO_USER_ARG_MAC "mac"
354 #define VIRTIO_USER_ARG_PATH "path"
355 VIRTIO_USER_ARG_PATH,
356 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
357 VIRTIO_USER_ARG_QUEUE_SIZE,
358 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
359 VIRTIO_USER_ARG_INTERFACE_NAME,
360 #define VIRTIO_USER_ARG_SERVER_MODE "server"
361 VIRTIO_USER_ARG_SERVER_MODE,
362 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
363 VIRTIO_USER_ARG_MRG_RXBUF,
364 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
365 VIRTIO_USER_ARG_IN_ORDER,
369 #define VIRTIO_USER_DEF_CQ_EN 0
370 #define VIRTIO_USER_DEF_Q_NUM 1
371 #define VIRTIO_USER_DEF_Q_SZ 256
372 #define VIRTIO_USER_DEF_SERVER_MODE 0
375 get_string_arg(const char *key __rte_unused,
376 const char *value, void *extra_args)
378 if (!value || !extra_args)
381 *(char **)extra_args = strdup(value);
383 if (!*(char **)extra_args)
390 get_integer_arg(const char *key __rte_unused,
391 const char *value, void *extra_args)
393 if (!value || !extra_args)
396 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
401 static struct rte_vdev_driver virtio_user_driver;
403 static struct rte_eth_dev *
404 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
406 struct rte_eth_dev *eth_dev;
407 struct rte_eth_dev_data *data;
408 struct virtio_hw *hw;
409 struct virtio_user_dev *dev;
411 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
413 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
417 data = eth_dev->data;
418 hw = eth_dev->data->dev_private;
420 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
422 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
423 rte_eth_dev_release_port(eth_dev);
427 hw->port_id = data->port_id;
428 dev->port_id = data->port_id;
429 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
431 * MSIX is required to enable LSC (see virtio_init_device).
432 * Here just pretend that we support msix.
436 hw->use_simple_rx = 0;
437 hw->use_inorder_rx = 0;
438 hw->use_inorder_tx = 0;
439 hw->virtio_user_dev = dev;
444 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
446 struct rte_eth_dev_data *data = eth_dev->data;
447 struct virtio_hw *hw = data->dev_private;
449 rte_free(hw->virtio_user_dev);
450 rte_eth_dev_release_port(eth_dev);
453 /* Dev initialization routine. Invoked once for each virtio vdev at
454 * EAL init time, see rte_bus_probe().
455 * Returns 0 on success.
458 virtio_user_pmd_probe(struct rte_vdev_device *dev)
460 struct rte_kvargs *kvlist = NULL;
461 struct rte_eth_dev *eth_dev;
462 struct virtio_hw *hw;
463 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
464 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
465 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
466 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
467 uint64_t mrg_rxbuf = 1;
468 uint64_t in_order = 1;
471 char *mac_addr = NULL;
474 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
476 PMD_INIT_LOG(ERR, "error when parsing param");
480 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
481 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
482 &get_string_arg, &path) < 0) {
483 PMD_INIT_LOG(ERR, "error to parse %s",
484 VIRTIO_USER_ARG_PATH);
488 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
489 VIRTIO_USER_ARG_QUEUE_SIZE);
493 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
494 if (is_vhost_user_by_type(path)) {
496 "arg %s applies only to vhost-kernel backend",
497 VIRTIO_USER_ARG_INTERFACE_NAME);
501 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
502 &get_string_arg, &ifname) < 0) {
503 PMD_INIT_LOG(ERR, "error to parse %s",
504 VIRTIO_USER_ARG_INTERFACE_NAME);
509 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
510 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
511 &get_string_arg, &mac_addr) < 0) {
512 PMD_INIT_LOG(ERR, "error to parse %s",
513 VIRTIO_USER_ARG_MAC);
518 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
519 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
520 &get_integer_arg, &queue_size) < 0) {
521 PMD_INIT_LOG(ERR, "error to parse %s",
522 VIRTIO_USER_ARG_QUEUE_SIZE);
527 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
528 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
529 &get_integer_arg, &queues) < 0) {
530 PMD_INIT_LOG(ERR, "error to parse %s",
531 VIRTIO_USER_ARG_QUEUES_NUM);
536 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
537 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
538 &get_integer_arg, &server_mode) < 0) {
539 PMD_INIT_LOG(ERR, "error to parse %s",
540 VIRTIO_USER_ARG_SERVER_MODE);
545 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
546 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
547 &get_integer_arg, &cq) < 0) {
548 PMD_INIT_LOG(ERR, "error to parse %s",
549 VIRTIO_USER_ARG_CQ_NUM);
552 } else if (queues > 1) {
556 if (queues > 1 && cq == 0) {
557 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
561 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
562 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
563 VIRTIO_USER_ARG_QUEUES_NUM, queues,
564 VIRTIO_MAX_VIRTQUEUE_PAIRS);
568 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
569 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
570 &get_integer_arg, &mrg_rxbuf) < 0) {
571 PMD_INIT_LOG(ERR, "error to parse %s",
572 VIRTIO_USER_ARG_MRG_RXBUF);
577 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
578 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
579 &get_integer_arg, &in_order) < 0) {
580 PMD_INIT_LOG(ERR, "error to parse %s",
581 VIRTIO_USER_ARG_IN_ORDER);
586 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
587 struct virtio_user_dev *vu_dev;
589 eth_dev = virtio_user_eth_dev_alloc(dev);
591 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
595 hw = eth_dev->data->dev_private;
596 vu_dev = virtio_user_get_dev(hw);
597 if (server_mode == 1)
598 vu_dev->is_server = true;
600 vu_dev->is_server = false;
601 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
602 queue_size, mac_addr, &ifname, mrg_rxbuf,
604 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
605 virtio_user_eth_dev_free(eth_dev);
610 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
615 /* previously called by rte_pci_probe() for physical dev */
616 if (eth_virtio_dev_init(eth_dev) < 0) {
617 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
618 virtio_user_eth_dev_free(eth_dev);
622 rte_eth_dev_probing_finish(eth_dev);
627 rte_kvargs_free(kvlist);
638 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
641 struct rte_eth_dev *eth_dev;
642 struct virtio_hw *hw;
643 struct virtio_user_dev *dev;
648 name = rte_vdev_device_name(vdev);
649 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
650 eth_dev = rte_eth_dev_allocated(name);
654 /* make sure the device is stopped, queues freed */
655 rte_eth_dev_close(eth_dev->data->port_id);
657 hw = eth_dev->data->dev_private;
658 dev = hw->virtio_user_dev;
659 virtio_user_dev_uninit(dev);
661 rte_eth_dev_release_port(eth_dev);
666 static struct rte_vdev_driver virtio_user_driver = {
667 .probe = virtio_user_pmd_probe,
668 .remove = virtio_user_pmd_remove,
671 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
672 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
673 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,