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 /* umask vhost-user unsupported features */
47 dev->device_features &= ~(dev->unsupported_features);
49 dev->features &= dev->device_features;
51 ret = virtio_user_start_device(dev);
55 if (dev->queue_pairs > 1) {
56 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
58 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
62 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
63 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
64 PMD_DRV_LOG(ERR, "interrupt disable failed");
67 rte_intr_callback_unregister(eth_dev->intr_handle,
68 virtio_interrupt_handler,
70 eth_dev->intr_handle->fd = connectfd;
71 rte_intr_callback_register(eth_dev->intr_handle,
72 virtio_interrupt_handler, eth_dev);
74 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
75 PMD_DRV_LOG(ERR, "interrupt enable failed");
79 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
84 virtio_user_delayed_handler(void *param)
86 struct virtio_hw *hw = (struct virtio_hw *)param;
87 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
88 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
90 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
91 PMD_DRV_LOG(ERR, "interrupt disable failed");
94 rte_intr_callback_unregister(eth_dev->intr_handle,
95 virtio_interrupt_handler, eth_dev);
97 if (dev->vhostfd >= 0) {
101 eth_dev->intr_handle->fd = dev->listenfd;
102 rte_intr_callback_register(eth_dev->intr_handle,
103 virtio_interrupt_handler, eth_dev);
104 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
105 PMD_DRV_LOG(ERR, "interrupt enable failed");
112 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
113 void *dst, int length)
116 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
118 if (offset == offsetof(struct virtio_net_config, mac) &&
119 length == ETHER_ADDR_LEN) {
120 for (i = 0; i < ETHER_ADDR_LEN; ++i)
121 ((uint8_t *)dst)[i] = dev->mac_addr[i];
125 if (offset == offsetof(struct virtio_net_config, status)) {
128 if (dev->vhostfd >= 0) {
132 flags = fcntl(dev->vhostfd, F_GETFL);
133 if (fcntl(dev->vhostfd, F_SETFL,
134 flags | O_NONBLOCK) == -1) {
135 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
138 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
139 if (r == 0 || (r < 0 && errno != EAGAIN)) {
140 dev->status &= (~VIRTIO_NET_S_LINK_UP);
141 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
144 /* This function could be called in the process
145 * of interrupt handling, callback cannot be
146 * unregistered here, set an alarm to do it.
149 virtio_user_delayed_handler,
152 dev->status |= VIRTIO_NET_S_LINK_UP;
154 if (fcntl(dev->vhostfd, F_SETFL,
155 flags & ~O_NONBLOCK) == -1) {
156 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
159 } else if (dev->is_server) {
160 dev->status &= (~VIRTIO_NET_S_LINK_UP);
161 if (virtio_user_server_reconnect(dev) >= 0)
162 dev->status |= VIRTIO_NET_S_LINK_UP;
165 *(uint16_t *)dst = dev->status;
168 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
169 *(uint16_t *)dst = dev->max_queue_pairs;
173 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
174 const void *src, int length)
177 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
179 if ((offset == offsetof(struct virtio_net_config, mac)) &&
180 (length == ETHER_ADDR_LEN))
181 for (i = 0; i < ETHER_ADDR_LEN; ++i)
182 dev->mac_addr[i] = ((const uint8_t *)src)[i];
184 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
189 virtio_user_reset(struct virtio_hw *hw)
191 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
193 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
194 virtio_user_stop_device(dev);
198 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
200 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
202 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
203 virtio_user_start_device(dev);
204 else if (status == VIRTIO_CONFIG_STATUS_RESET)
205 virtio_user_reset(hw);
206 dev->status = status;
210 virtio_user_get_status(struct virtio_hw *hw)
212 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
218 virtio_user_get_features(struct virtio_hw *hw)
220 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
222 /* unmask feature bits defined in vhost user protocol */
223 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
227 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
229 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
231 dev->features = features & dev->device_features;
235 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
237 /* rxq interrupts and config interrupt are separated in virtio-user,
238 * here we only report config change.
240 return VIRTIO_PCI_ISR_CONFIG;
244 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
245 uint16_t vec __rte_unused)
251 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
252 struct virtqueue *vq __rte_unused,
255 /* pretend we have done that */
259 /* This function is to get the queue size, aka, number of descs, of a specified
260 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
261 * max supported queues.
264 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
266 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
268 /* Currently, each queue has same queue size */
269 return dev->queue_size;
273 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
275 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
276 uint16_t queue_idx = vq->vq_queue_index;
277 uint64_t desc_addr, avail_addr, used_addr;
279 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
280 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
281 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
282 ring[vq->vq_nentries]),
283 VIRTIO_PCI_VRING_ALIGN);
285 dev->vrings[queue_idx].num = vq->vq_nentries;
286 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
287 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
288 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
294 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
296 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
297 * correspondingly stops the ioeventfds, and reset the status of
299 * For modern devices, set queue desc, avail, used in PCI bar to 0,
300 * not see any more behavior in QEMU.
302 * Here we just care about what information to deliver to vhost-user
303 * or vhost-kernel. So we just close ioeventfd for now.
305 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
307 close(dev->callfds[vq->vq_queue_index]);
308 close(dev->kickfds[vq->vq_queue_index]);
312 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
315 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
317 if (hw->cvq && (hw->cvq->vq == vq)) {
318 virtio_user_handle_cq(dev, vq->vq_queue_index);
322 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
323 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
327 const struct virtio_pci_ops virtio_user_ops = {
328 .read_dev_cfg = virtio_user_read_dev_config,
329 .write_dev_cfg = virtio_user_write_dev_config,
330 .get_status = virtio_user_get_status,
331 .set_status = virtio_user_set_status,
332 .get_features = virtio_user_get_features,
333 .set_features = virtio_user_set_features,
334 .get_isr = virtio_user_get_isr,
335 .set_config_irq = virtio_user_set_config_irq,
336 .set_queue_irq = virtio_user_set_queue_irq,
337 .get_queue_num = virtio_user_get_queue_num,
338 .setup_queue = virtio_user_setup_queue,
339 .del_queue = virtio_user_del_queue,
340 .notify_queue = virtio_user_notify_queue,
343 static const char *valid_args[] = {
344 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
345 VIRTIO_USER_ARG_QUEUES_NUM,
346 #define VIRTIO_USER_ARG_CQ_NUM "cq"
347 VIRTIO_USER_ARG_CQ_NUM,
348 #define VIRTIO_USER_ARG_MAC "mac"
350 #define VIRTIO_USER_ARG_PATH "path"
351 VIRTIO_USER_ARG_PATH,
352 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
353 VIRTIO_USER_ARG_QUEUE_SIZE,
354 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
355 VIRTIO_USER_ARG_INTERFACE_NAME,
356 #define VIRTIO_USER_ARG_SERVER_MODE "server"
357 VIRTIO_USER_ARG_SERVER_MODE,
358 #define VIRTIO_USER_ARG_MRG_RXBUF "mrg_rxbuf"
359 VIRTIO_USER_ARG_MRG_RXBUF,
360 #define VIRTIO_USER_ARG_IN_ORDER "in_order"
361 VIRTIO_USER_ARG_IN_ORDER,
365 #define VIRTIO_USER_DEF_CQ_EN 0
366 #define VIRTIO_USER_DEF_Q_NUM 1
367 #define VIRTIO_USER_DEF_Q_SZ 256
368 #define VIRTIO_USER_DEF_SERVER_MODE 0
371 get_string_arg(const char *key __rte_unused,
372 const char *value, void *extra_args)
374 if (!value || !extra_args)
377 *(char **)extra_args = strdup(value);
379 if (!*(char **)extra_args)
386 get_integer_arg(const char *key __rte_unused,
387 const char *value, void *extra_args)
389 if (!value || !extra_args)
392 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
397 static struct rte_vdev_driver virtio_user_driver;
399 static struct rte_eth_dev *
400 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
402 struct rte_eth_dev *eth_dev;
403 struct rte_eth_dev_data *data;
404 struct virtio_hw *hw;
405 struct virtio_user_dev *dev;
407 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
409 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
413 data = eth_dev->data;
414 hw = eth_dev->data->dev_private;
416 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
418 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
419 rte_eth_dev_release_port(eth_dev);
423 hw->port_id = data->port_id;
424 dev->port_id = data->port_id;
425 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
427 * MSIX is required to enable LSC (see virtio_init_device).
428 * Here just pretend that we support msix.
432 hw->use_simple_rx = 0;
433 hw->use_inorder_rx = 0;
434 hw->use_inorder_tx = 0;
435 hw->virtio_user_dev = dev;
440 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
442 struct rte_eth_dev_data *data = eth_dev->data;
443 struct virtio_hw *hw = data->dev_private;
445 rte_free(hw->virtio_user_dev);
446 rte_eth_dev_release_port(eth_dev);
449 /* Dev initialization routine. Invoked once for each virtio vdev at
450 * EAL init time, see rte_bus_probe().
451 * Returns 0 on success.
454 virtio_user_pmd_probe(struct rte_vdev_device *dev)
456 struct rte_kvargs *kvlist = NULL;
457 struct rte_eth_dev *eth_dev;
458 struct virtio_hw *hw;
459 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
460 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
461 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
462 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
463 uint64_t mrg_rxbuf = 1;
464 uint64_t in_order = 1;
467 char *mac_addr = NULL;
470 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
472 PMD_INIT_LOG(ERR, "error when parsing param");
476 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
477 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
478 &get_string_arg, &path) < 0) {
479 PMD_INIT_LOG(ERR, "error to parse %s",
480 VIRTIO_USER_ARG_PATH);
484 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
485 VIRTIO_USER_ARG_QUEUE_SIZE);
489 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
490 if (is_vhost_user_by_type(path)) {
492 "arg %s applies only to vhost-kernel backend",
493 VIRTIO_USER_ARG_INTERFACE_NAME);
497 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
498 &get_string_arg, &ifname) < 0) {
499 PMD_INIT_LOG(ERR, "error to parse %s",
500 VIRTIO_USER_ARG_INTERFACE_NAME);
505 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
506 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
507 &get_string_arg, &mac_addr) < 0) {
508 PMD_INIT_LOG(ERR, "error to parse %s",
509 VIRTIO_USER_ARG_MAC);
514 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
515 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
516 &get_integer_arg, &queue_size) < 0) {
517 PMD_INIT_LOG(ERR, "error to parse %s",
518 VIRTIO_USER_ARG_QUEUE_SIZE);
523 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
524 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
525 &get_integer_arg, &queues) < 0) {
526 PMD_INIT_LOG(ERR, "error to parse %s",
527 VIRTIO_USER_ARG_QUEUES_NUM);
532 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
533 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
534 &get_integer_arg, &server_mode) < 0) {
535 PMD_INIT_LOG(ERR, "error to parse %s",
536 VIRTIO_USER_ARG_SERVER_MODE);
541 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
542 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
543 &get_integer_arg, &cq) < 0) {
544 PMD_INIT_LOG(ERR, "error to parse %s",
545 VIRTIO_USER_ARG_CQ_NUM);
548 } else if (queues > 1) {
552 if (queues > 1 && cq == 0) {
553 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
557 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
558 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
559 VIRTIO_USER_ARG_QUEUES_NUM, queues,
560 VIRTIO_MAX_VIRTQUEUE_PAIRS);
564 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MRG_RXBUF) == 1) {
565 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MRG_RXBUF,
566 &get_integer_arg, &mrg_rxbuf) < 0) {
567 PMD_INIT_LOG(ERR, "error to parse %s",
568 VIRTIO_USER_ARG_MRG_RXBUF);
573 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_IN_ORDER) == 1) {
574 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_IN_ORDER,
575 &get_integer_arg, &in_order) < 0) {
576 PMD_INIT_LOG(ERR, "error to parse %s",
577 VIRTIO_USER_ARG_IN_ORDER);
582 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
583 struct virtio_user_dev *vu_dev;
585 eth_dev = virtio_user_eth_dev_alloc(dev);
587 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
591 hw = eth_dev->data->dev_private;
592 vu_dev = virtio_user_get_dev(hw);
593 if (server_mode == 1)
594 vu_dev->is_server = true;
596 vu_dev->is_server = false;
597 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
598 queue_size, mac_addr, &ifname, mrg_rxbuf,
600 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
601 virtio_user_eth_dev_free(eth_dev);
606 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
611 /* previously called by rte_pci_probe() for physical dev */
612 if (eth_virtio_dev_init(eth_dev) < 0) {
613 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
614 virtio_user_eth_dev_free(eth_dev);
618 rte_eth_dev_probing_finish(eth_dev);
623 rte_kvargs_free(kvlist);
634 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
637 struct rte_eth_dev *eth_dev;
638 struct virtio_hw *hw;
639 struct virtio_user_dev *dev;
644 name = rte_vdev_device_name(vdev);
645 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
646 eth_dev = rte_eth_dev_allocated(name);
650 /* make sure the device is stopped, queues freed */
651 rte_eth_dev_close(eth_dev->data->port_id);
653 hw = eth_dev->data->dev_private;
654 dev = hw->virtio_user_dev;
655 virtio_user_dev_uninit(dev);
657 rte_eth_dev_release_port(eth_dev);
662 static struct rte_vdev_driver virtio_user_driver = {
663 .probe = virtio_user_pmd_probe,
664 .remove = virtio_user_pmd_remove,
667 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
668 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
669 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,