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 fcntl(dev->vhostfd, F_SETFL, flags & (~O_NONBLOCK));
85 *(uint16_t *)dst = dev->status;
88 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
89 *(uint16_t *)dst = dev->max_queue_pairs;
93 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
94 const void *src, int length)
97 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
99 if ((offset == offsetof(struct virtio_net_config, mac)) &&
100 (length == ETHER_ADDR_LEN))
101 for (i = 0; i < ETHER_ADDR_LEN; ++i)
102 dev->mac_addr[i] = ((const uint8_t *)src)[i];
104 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
109 virtio_user_reset(struct virtio_hw *hw)
111 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
113 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
114 virtio_user_stop_device(dev);
118 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
120 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
122 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
123 virtio_user_start_device(dev);
124 else if (status == VIRTIO_CONFIG_STATUS_RESET)
125 virtio_user_reset(hw);
126 dev->status = status;
130 virtio_user_get_status(struct virtio_hw *hw)
132 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
138 virtio_user_get_features(struct virtio_hw *hw)
140 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
142 /* unmask feature bits defined in vhost user protocol */
143 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
147 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
149 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
151 dev->features = features & dev->device_features;
155 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
157 /* rxq interrupts and config interrupt are separated in virtio-user,
158 * here we only report config change.
160 return VIRTIO_PCI_ISR_CONFIG;
164 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
165 uint16_t vec __rte_unused)
171 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
172 struct virtqueue *vq __rte_unused,
175 /* pretend we have done that */
179 /* This function is to get the queue size, aka, number of descs, of a specified
180 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
181 * max supported queues.
184 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
186 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
188 /* Currently, each queue has same queue size */
189 return dev->queue_size;
193 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
195 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
196 uint16_t queue_idx = vq->vq_queue_index;
197 uint64_t desc_addr, avail_addr, used_addr;
199 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
200 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
201 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
202 ring[vq->vq_nentries]),
203 VIRTIO_PCI_VRING_ALIGN);
205 dev->vrings[queue_idx].num = vq->vq_nentries;
206 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
207 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
208 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
214 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
216 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
217 * correspondingly stops the ioeventfds, and reset the status of
219 * For modern devices, set queue desc, avail, used in PCI bar to 0,
220 * not see any more behavior in QEMU.
222 * Here we just care about what information to deliver to vhost-user
223 * or vhost-kernel. So we just close ioeventfd for now.
225 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
227 close(dev->callfds[vq->vq_queue_index]);
228 close(dev->kickfds[vq->vq_queue_index]);
232 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
235 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
237 if (hw->cvq && (hw->cvq->vq == vq)) {
238 virtio_user_handle_cq(dev, vq->vq_queue_index);
242 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
243 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
247 const struct virtio_pci_ops virtio_user_ops = {
248 .read_dev_cfg = virtio_user_read_dev_config,
249 .write_dev_cfg = virtio_user_write_dev_config,
250 .reset = virtio_user_reset,
251 .get_status = virtio_user_get_status,
252 .set_status = virtio_user_set_status,
253 .get_features = virtio_user_get_features,
254 .set_features = virtio_user_set_features,
255 .get_isr = virtio_user_get_isr,
256 .set_config_irq = virtio_user_set_config_irq,
257 .set_queue_irq = virtio_user_set_queue_irq,
258 .get_queue_num = virtio_user_get_queue_num,
259 .setup_queue = virtio_user_setup_queue,
260 .del_queue = virtio_user_del_queue,
261 .notify_queue = virtio_user_notify_queue,
264 static const char *valid_args[] = {
265 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
266 VIRTIO_USER_ARG_QUEUES_NUM,
267 #define VIRTIO_USER_ARG_CQ_NUM "cq"
268 VIRTIO_USER_ARG_CQ_NUM,
269 #define VIRTIO_USER_ARG_MAC "mac"
271 #define VIRTIO_USER_ARG_PATH "path"
272 VIRTIO_USER_ARG_PATH,
273 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
274 VIRTIO_USER_ARG_QUEUE_SIZE,
275 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
276 VIRTIO_USER_ARG_INTERFACE_NAME,
280 #define VIRTIO_USER_DEF_CQ_EN 0
281 #define VIRTIO_USER_DEF_Q_NUM 1
282 #define VIRTIO_USER_DEF_Q_SZ 256
285 get_string_arg(const char *key __rte_unused,
286 const char *value, void *extra_args)
288 if (!value || !extra_args)
291 *(char **)extra_args = strdup(value);
293 if (!*(char **)extra_args)
300 get_integer_arg(const char *key __rte_unused,
301 const char *value, void *extra_args)
303 if (!value || !extra_args)
306 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
311 static struct rte_vdev_driver virtio_user_driver;
313 static struct rte_eth_dev *
314 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
316 struct rte_eth_dev *eth_dev;
317 struct rte_eth_dev_data *data;
318 struct virtio_hw *hw;
319 struct virtio_user_dev *dev;
321 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
323 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
327 data = eth_dev->data;
328 hw = eth_dev->data->dev_private;
330 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
332 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
333 rte_eth_dev_release_port(eth_dev);
338 hw->port_id = data->port_id;
339 dev->port_id = data->port_id;
340 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
342 * MSIX is required to enable LSC (see virtio_init_device).
343 * Here just pretend that we support msix.
347 hw->use_simple_rx = 0;
348 hw->use_simple_tx = 0;
349 hw->virtio_user_dev = dev;
354 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
356 struct rte_eth_dev_data *data = eth_dev->data;
357 struct virtio_hw *hw = data->dev_private;
359 rte_free(hw->virtio_user_dev);
361 rte_eth_dev_release_port(eth_dev);
364 /* Dev initialization routine. Invoked once for each virtio vdev at
365 * EAL init time, see rte_bus_probe().
366 * Returns 0 on success.
369 virtio_user_pmd_probe(struct rte_vdev_device *dev)
371 struct rte_kvargs *kvlist = NULL;
372 struct rte_eth_dev *eth_dev;
373 struct virtio_hw *hw;
374 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
375 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
376 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
379 char *mac_addr = NULL;
382 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
384 PMD_INIT_LOG(ERR, "error when parsing param");
388 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
389 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
390 &get_string_arg, &path) < 0) {
391 PMD_INIT_LOG(ERR, "error to parse %s",
392 VIRTIO_USER_ARG_PATH);
396 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
397 VIRTIO_USER_ARG_QUEUE_SIZE);
401 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
402 if (is_vhost_user_by_type(path)) {
404 "arg %s applies only to vhost-kernel backend",
405 VIRTIO_USER_ARG_INTERFACE_NAME);
409 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
410 &get_string_arg, &ifname) < 0) {
411 PMD_INIT_LOG(ERR, "error to parse %s",
412 VIRTIO_USER_ARG_INTERFACE_NAME);
417 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
418 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
419 &get_string_arg, &mac_addr) < 0) {
420 PMD_INIT_LOG(ERR, "error to parse %s",
421 VIRTIO_USER_ARG_MAC);
426 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
427 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
428 &get_integer_arg, &queue_size) < 0) {
429 PMD_INIT_LOG(ERR, "error to parse %s",
430 VIRTIO_USER_ARG_QUEUE_SIZE);
435 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
436 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
437 &get_integer_arg, &queues) < 0) {
438 PMD_INIT_LOG(ERR, "error to parse %s",
439 VIRTIO_USER_ARG_QUEUES_NUM);
444 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
445 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
446 &get_integer_arg, &cq) < 0) {
447 PMD_INIT_LOG(ERR, "error to parse %s",
448 VIRTIO_USER_ARG_CQ_NUM);
451 } else if (queues > 1) {
455 if (queues > 1 && cq == 0) {
456 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
460 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
461 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
462 VIRTIO_USER_ARG_QUEUES_NUM, queues,
463 VIRTIO_MAX_VIRTQUEUE_PAIRS);
467 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
468 eth_dev = virtio_user_eth_dev_alloc(dev);
470 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
474 hw = eth_dev->data->dev_private;
475 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
476 queue_size, mac_addr, &ifname) < 0) {
477 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
478 virtio_user_eth_dev_free(eth_dev);
482 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
487 /* previously called by rte_pci_probe() for physical dev */
488 if (eth_virtio_dev_init(eth_dev) < 0) {
489 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
490 virtio_user_eth_dev_free(eth_dev);
497 rte_kvargs_free(kvlist);
507 /** Called by rte_eth_dev_detach() */
509 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
512 struct rte_eth_dev *eth_dev;
513 struct virtio_hw *hw;
514 struct virtio_user_dev *dev;
519 name = rte_vdev_device_name(vdev);
520 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
521 eth_dev = rte_eth_dev_allocated(name);
525 /* make sure the device is stopped, queues freed */
526 rte_eth_dev_close(eth_dev->data->port_id);
528 hw = eth_dev->data->dev_private;
529 dev = hw->virtio_user_dev;
530 virtio_user_dev_uninit(dev);
532 rte_free(eth_dev->data->dev_private);
533 rte_eth_dev_release_port(eth_dev);
538 static struct rte_vdev_driver virtio_user_driver = {
539 .probe = virtio_user_pmd_probe,
540 .remove = virtio_user_pmd_remove,
543 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
544 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
545 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,