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_delayed_handler(void *param)
30 struct virtio_hw *hw = (struct virtio_hw *)param;
31 struct rte_eth_dev *dev = &rte_eth_devices[hw->port_id];
33 rte_intr_callback_unregister(dev->intr_handle,
34 virtio_interrupt_handler,
39 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
40 void *dst, int length)
43 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
45 if (offset == offsetof(struct virtio_net_config, mac) &&
46 length == ETHER_ADDR_LEN) {
47 for (i = 0; i < ETHER_ADDR_LEN; ++i)
48 ((uint8_t *)dst)[i] = dev->mac_addr[i];
52 if (offset == offsetof(struct virtio_net_config, status)) {
55 if (dev->vhostfd >= 0) {
59 flags = fcntl(dev->vhostfd, F_GETFL);
60 if (fcntl(dev->vhostfd, F_SETFL,
61 flags | O_NONBLOCK) == -1) {
62 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
65 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
66 if (r == 0 || (r < 0 && errno != EAGAIN)) {
67 dev->status &= (~VIRTIO_NET_S_LINK_UP);
68 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
70 /* Only client mode is available now. Once the
71 * connection is broken, it can never be up
72 * again. Besides, this function could be called
73 * in the process of interrupt handling,
74 * callback cannot be unregistered here, set an
78 virtio_user_delayed_handler,
81 dev->status |= VIRTIO_NET_S_LINK_UP;
83 if (fcntl(dev->vhostfd, F_SETFL,
84 flags & ~O_NONBLOCK) == -1) {
85 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
89 *(uint16_t *)dst = dev->status;
92 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
93 *(uint16_t *)dst = dev->max_queue_pairs;
97 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
98 const void *src, int length)
101 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
103 if ((offset == offsetof(struct virtio_net_config, mac)) &&
104 (length == ETHER_ADDR_LEN))
105 for (i = 0; i < ETHER_ADDR_LEN; ++i)
106 dev->mac_addr[i] = ((const uint8_t *)src)[i];
108 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
113 virtio_user_reset(struct virtio_hw *hw)
115 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
117 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
118 virtio_user_stop_device(dev);
122 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
124 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
126 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
127 virtio_user_start_device(dev);
128 else if (status == VIRTIO_CONFIG_STATUS_RESET)
129 virtio_user_reset(hw);
130 dev->status = status;
134 virtio_user_get_status(struct virtio_hw *hw)
136 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
142 virtio_user_get_features(struct virtio_hw *hw)
144 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
146 /* unmask feature bits defined in vhost user protocol */
147 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
151 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
153 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
155 dev->features = features & dev->device_features;
159 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
161 /* rxq interrupts and config interrupt are separated in virtio-user,
162 * here we only report config change.
164 return VIRTIO_PCI_ISR_CONFIG;
168 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
169 uint16_t vec __rte_unused)
175 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
176 struct virtqueue *vq __rte_unused,
179 /* pretend we have done that */
183 /* This function is to get the queue size, aka, number of descs, of a specified
184 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
185 * max supported queues.
188 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
190 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
192 /* Currently, each queue has same queue size */
193 return dev->queue_size;
197 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
199 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
200 uint16_t queue_idx = vq->vq_queue_index;
201 uint64_t desc_addr, avail_addr, used_addr;
203 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
204 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
205 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
206 ring[vq->vq_nentries]),
207 VIRTIO_PCI_VRING_ALIGN);
209 dev->vrings[queue_idx].num = vq->vq_nentries;
210 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
211 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
212 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
218 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
220 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
221 * correspondingly stops the ioeventfds, and reset the status of
223 * For modern devices, set queue desc, avail, used in PCI bar to 0,
224 * not see any more behavior in QEMU.
226 * Here we just care about what information to deliver to vhost-user
227 * or vhost-kernel. So we just close ioeventfd for now.
229 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
231 close(dev->callfds[vq->vq_queue_index]);
232 close(dev->kickfds[vq->vq_queue_index]);
236 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
239 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
241 if (hw->cvq && (hw->cvq->vq == vq)) {
242 virtio_user_handle_cq(dev, vq->vq_queue_index);
246 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
247 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
251 const struct virtio_pci_ops virtio_user_ops = {
252 .read_dev_cfg = virtio_user_read_dev_config,
253 .write_dev_cfg = virtio_user_write_dev_config,
254 .reset = virtio_user_reset,
255 .get_status = virtio_user_get_status,
256 .set_status = virtio_user_set_status,
257 .get_features = virtio_user_get_features,
258 .set_features = virtio_user_set_features,
259 .get_isr = virtio_user_get_isr,
260 .set_config_irq = virtio_user_set_config_irq,
261 .set_queue_irq = virtio_user_set_queue_irq,
262 .get_queue_num = virtio_user_get_queue_num,
263 .setup_queue = virtio_user_setup_queue,
264 .del_queue = virtio_user_del_queue,
265 .notify_queue = virtio_user_notify_queue,
268 static const char *valid_args[] = {
269 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
270 VIRTIO_USER_ARG_QUEUES_NUM,
271 #define VIRTIO_USER_ARG_CQ_NUM "cq"
272 VIRTIO_USER_ARG_CQ_NUM,
273 #define VIRTIO_USER_ARG_MAC "mac"
275 #define VIRTIO_USER_ARG_PATH "path"
276 VIRTIO_USER_ARG_PATH,
277 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
278 VIRTIO_USER_ARG_QUEUE_SIZE,
279 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
280 VIRTIO_USER_ARG_INTERFACE_NAME,
284 #define VIRTIO_USER_DEF_CQ_EN 0
285 #define VIRTIO_USER_DEF_Q_NUM 1
286 #define VIRTIO_USER_DEF_Q_SZ 256
289 get_string_arg(const char *key __rte_unused,
290 const char *value, void *extra_args)
292 if (!value || !extra_args)
295 *(char **)extra_args = strdup(value);
297 if (!*(char **)extra_args)
304 get_integer_arg(const char *key __rte_unused,
305 const char *value, void *extra_args)
307 if (!value || !extra_args)
310 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
315 static struct rte_vdev_driver virtio_user_driver;
317 static struct rte_eth_dev *
318 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
320 struct rte_eth_dev *eth_dev;
321 struct rte_eth_dev_data *data;
322 struct virtio_hw *hw;
323 struct virtio_user_dev *dev;
325 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
327 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
331 data = eth_dev->data;
332 hw = eth_dev->data->dev_private;
334 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
336 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
337 rte_eth_dev_release_port(eth_dev);
342 hw->port_id = data->port_id;
343 dev->port_id = data->port_id;
344 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
346 * MSIX is required to enable LSC (see virtio_init_device).
347 * Here just pretend that we support msix.
351 hw->use_simple_rx = 0;
352 hw->use_simple_tx = 0;
353 hw->virtio_user_dev = dev;
358 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
360 struct rte_eth_dev_data *data = eth_dev->data;
361 struct virtio_hw *hw = data->dev_private;
363 rte_free(hw->virtio_user_dev);
365 rte_eth_dev_release_port(eth_dev);
368 /* Dev initialization routine. Invoked once for each virtio vdev at
369 * EAL init time, see rte_bus_probe().
370 * Returns 0 on success.
373 virtio_user_pmd_probe(struct rte_vdev_device *dev)
375 struct rte_kvargs *kvlist = NULL;
376 struct rte_eth_dev *eth_dev;
377 struct virtio_hw *hw;
378 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
379 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
380 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
383 char *mac_addr = NULL;
386 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
388 PMD_INIT_LOG(ERR, "error when parsing param");
392 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
393 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
394 &get_string_arg, &path) < 0) {
395 PMD_INIT_LOG(ERR, "error to parse %s",
396 VIRTIO_USER_ARG_PATH);
400 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
401 VIRTIO_USER_ARG_QUEUE_SIZE);
405 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
406 if (is_vhost_user_by_type(path)) {
408 "arg %s applies only to vhost-kernel backend",
409 VIRTIO_USER_ARG_INTERFACE_NAME);
413 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
414 &get_string_arg, &ifname) < 0) {
415 PMD_INIT_LOG(ERR, "error to parse %s",
416 VIRTIO_USER_ARG_INTERFACE_NAME);
421 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
422 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
423 &get_string_arg, &mac_addr) < 0) {
424 PMD_INIT_LOG(ERR, "error to parse %s",
425 VIRTIO_USER_ARG_MAC);
430 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
431 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
432 &get_integer_arg, &queue_size) < 0) {
433 PMD_INIT_LOG(ERR, "error to parse %s",
434 VIRTIO_USER_ARG_QUEUE_SIZE);
439 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
440 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
441 &get_integer_arg, &queues) < 0) {
442 PMD_INIT_LOG(ERR, "error to parse %s",
443 VIRTIO_USER_ARG_QUEUES_NUM);
448 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
449 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
450 &get_integer_arg, &cq) < 0) {
451 PMD_INIT_LOG(ERR, "error to parse %s",
452 VIRTIO_USER_ARG_CQ_NUM);
455 } else if (queues > 1) {
459 if (queues > 1 && cq == 0) {
460 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
464 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
465 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
466 VIRTIO_USER_ARG_QUEUES_NUM, queues,
467 VIRTIO_MAX_VIRTQUEUE_PAIRS);
471 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
472 eth_dev = virtio_user_eth_dev_alloc(dev);
474 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
478 hw = eth_dev->data->dev_private;
479 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
480 queue_size, mac_addr, &ifname) < 0) {
481 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
482 virtio_user_eth_dev_free(eth_dev);
486 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
491 /* previously called by rte_pci_probe() for physical dev */
492 if (eth_virtio_dev_init(eth_dev) < 0) {
493 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
494 virtio_user_eth_dev_free(eth_dev);
501 rte_kvargs_free(kvlist);
511 /** Called by rte_eth_dev_detach() */
513 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
516 struct rte_eth_dev *eth_dev;
517 struct virtio_hw *hw;
518 struct virtio_user_dev *dev;
523 name = rte_vdev_device_name(vdev);
524 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
525 eth_dev = rte_eth_dev_allocated(name);
529 /* make sure the device is stopped, queues freed */
530 rte_eth_dev_close(eth_dev->data->port_id);
532 hw = eth_dev->data->dev_private;
533 dev = hw->virtio_user_dev;
534 virtio_user_dev_uninit(dev);
536 rte_free(eth_dev->data->dev_private);
537 rte_eth_dev_release_port(eth_dev);
542 static struct rte_vdev_driver virtio_user_driver = {
543 .probe = virtio_user_pmd_probe,
544 .remove = virtio_user_pmd_remove,
547 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
548 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
549 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,