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 flag = fcntl(connectfd, F_GETFD);
41 fcntl(connectfd, F_SETFL, flag | O_NONBLOCK);
43 ret = virtio_user_start_device(dev);
47 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
48 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
49 PMD_DRV_LOG(ERR, "interrupt disable failed");
52 rte_intr_callback_unregister(eth_dev->intr_handle,
53 virtio_interrupt_handler,
55 eth_dev->intr_handle->fd = connectfd;
56 rte_intr_callback_register(eth_dev->intr_handle,
57 virtio_interrupt_handler, eth_dev);
59 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
60 PMD_DRV_LOG(ERR, "interrupt enable failed");
64 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");
69 virtio_user_delayed_handler(void *param)
71 struct virtio_hw *hw = (struct virtio_hw *)param;
72 struct rte_eth_dev *eth_dev = &rte_eth_devices[hw->port_id];
73 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
75 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
76 PMD_DRV_LOG(ERR, "interrupt disable failed");
79 rte_intr_callback_unregister(eth_dev->intr_handle,
80 virtio_interrupt_handler, eth_dev);
82 if (dev->vhostfd >= 0) {
86 eth_dev->intr_handle->fd = dev->listenfd;
87 rte_intr_callback_register(eth_dev->intr_handle,
88 virtio_interrupt_handler, eth_dev);
89 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
90 PMD_DRV_LOG(ERR, "interrupt enable failed");
97 virtio_user_read_dev_config(struct virtio_hw *hw, size_t offset,
98 void *dst, 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 ((uint8_t *)dst)[i] = dev->mac_addr[i];
110 if (offset == offsetof(struct virtio_net_config, status)) {
113 if (dev->vhostfd >= 0) {
117 flags = fcntl(dev->vhostfd, F_GETFL);
118 if (fcntl(dev->vhostfd, F_SETFL,
119 flags | O_NONBLOCK) == -1) {
120 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
123 r = recv(dev->vhostfd, buf, 128, MSG_PEEK);
124 if (r == 0 || (r < 0 && errno != EAGAIN)) {
125 dev->status &= (~VIRTIO_NET_S_LINK_UP);
126 PMD_DRV_LOG(ERR, "virtio-user port %u is down",
129 /* This function could be called in the process
130 * of interrupt handling, callback cannot be
131 * unregistered here, set an alarm to do it.
134 virtio_user_delayed_handler,
137 dev->status |= VIRTIO_NET_S_LINK_UP;
139 if (fcntl(dev->vhostfd, F_SETFL,
140 flags & ~O_NONBLOCK) == -1) {
141 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
144 } else if (dev->is_server) {
145 dev->status &= (~VIRTIO_NET_S_LINK_UP);
146 if (virtio_user_server_reconnect(dev) >= 0)
147 dev->status |= VIRTIO_NET_S_LINK_UP;
150 *(uint16_t *)dst = dev->status;
153 if (offset == offsetof(struct virtio_net_config, max_virtqueue_pairs))
154 *(uint16_t *)dst = dev->max_queue_pairs;
158 virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
159 const void *src, int length)
162 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
164 if ((offset == offsetof(struct virtio_net_config, mac)) &&
165 (length == ETHER_ADDR_LEN))
166 for (i = 0; i < ETHER_ADDR_LEN; ++i)
167 dev->mac_addr[i] = ((const uint8_t *)src)[i];
169 PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
174 virtio_user_reset(struct virtio_hw *hw)
176 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
178 if (dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
179 virtio_user_stop_device(dev);
183 virtio_user_set_status(struct virtio_hw *hw, uint8_t status)
185 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
187 if (status & VIRTIO_CONFIG_STATUS_DRIVER_OK)
188 virtio_user_start_device(dev);
189 else if (status == VIRTIO_CONFIG_STATUS_RESET)
190 virtio_user_reset(hw);
191 dev->status = status;
195 virtio_user_get_status(struct virtio_hw *hw)
197 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
203 virtio_user_get_features(struct virtio_hw *hw)
205 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
207 /* unmask feature bits defined in vhost user protocol */
208 return dev->device_features & VIRTIO_PMD_SUPPORTED_GUEST_FEATURES;
212 virtio_user_set_features(struct virtio_hw *hw, uint64_t features)
214 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
216 dev->features = features & dev->device_features;
220 virtio_user_get_isr(struct virtio_hw *hw __rte_unused)
222 /* rxq interrupts and config interrupt are separated in virtio-user,
223 * here we only report config change.
225 return VIRTIO_PCI_ISR_CONFIG;
229 virtio_user_set_config_irq(struct virtio_hw *hw __rte_unused,
230 uint16_t vec __rte_unused)
236 virtio_user_set_queue_irq(struct virtio_hw *hw __rte_unused,
237 struct virtqueue *vq __rte_unused,
240 /* pretend we have done that */
244 /* This function is to get the queue size, aka, number of descs, of a specified
245 * queue. Different with the VHOST_USER_GET_QUEUE_NUM, which is used to get the
246 * max supported queues.
249 virtio_user_get_queue_num(struct virtio_hw *hw, uint16_t queue_id __rte_unused)
251 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
253 /* Currently, each queue has same queue size */
254 return dev->queue_size;
258 virtio_user_setup_queue(struct virtio_hw *hw, struct virtqueue *vq)
260 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
261 uint16_t queue_idx = vq->vq_queue_index;
262 uint64_t desc_addr, avail_addr, used_addr;
264 desc_addr = (uintptr_t)vq->vq_ring_virt_mem;
265 avail_addr = desc_addr + vq->vq_nentries * sizeof(struct vring_desc);
266 used_addr = RTE_ALIGN_CEIL(avail_addr + offsetof(struct vring_avail,
267 ring[vq->vq_nentries]),
268 VIRTIO_PCI_VRING_ALIGN);
270 dev->vrings[queue_idx].num = vq->vq_nentries;
271 dev->vrings[queue_idx].desc = (void *)(uintptr_t)desc_addr;
272 dev->vrings[queue_idx].avail = (void *)(uintptr_t)avail_addr;
273 dev->vrings[queue_idx].used = (void *)(uintptr_t)used_addr;
279 virtio_user_del_queue(struct virtio_hw *hw, struct virtqueue *vq)
281 /* For legacy devices, write 0 to VIRTIO_PCI_QUEUE_PFN port, QEMU
282 * correspondingly stops the ioeventfds, and reset the status of
284 * For modern devices, set queue desc, avail, used in PCI bar to 0,
285 * not see any more behavior in QEMU.
287 * Here we just care about what information to deliver to vhost-user
288 * or vhost-kernel. So we just close ioeventfd for now.
290 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
292 close(dev->callfds[vq->vq_queue_index]);
293 close(dev->kickfds[vq->vq_queue_index]);
297 virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
300 struct virtio_user_dev *dev = virtio_user_get_dev(hw);
302 if (hw->cvq && (hw->cvq->vq == vq)) {
303 virtio_user_handle_cq(dev, vq->vq_queue_index);
307 if (write(dev->kickfds[vq->vq_queue_index], &buf, sizeof(buf)) < 0)
308 PMD_DRV_LOG(ERR, "failed to kick backend: %s",
312 const struct virtio_pci_ops virtio_user_ops = {
313 .read_dev_cfg = virtio_user_read_dev_config,
314 .write_dev_cfg = virtio_user_write_dev_config,
315 .reset = virtio_user_reset,
316 .get_status = virtio_user_get_status,
317 .set_status = virtio_user_set_status,
318 .get_features = virtio_user_get_features,
319 .set_features = virtio_user_set_features,
320 .get_isr = virtio_user_get_isr,
321 .set_config_irq = virtio_user_set_config_irq,
322 .set_queue_irq = virtio_user_set_queue_irq,
323 .get_queue_num = virtio_user_get_queue_num,
324 .setup_queue = virtio_user_setup_queue,
325 .del_queue = virtio_user_del_queue,
326 .notify_queue = virtio_user_notify_queue,
329 static const char *valid_args[] = {
330 #define VIRTIO_USER_ARG_QUEUES_NUM "queues"
331 VIRTIO_USER_ARG_QUEUES_NUM,
332 #define VIRTIO_USER_ARG_CQ_NUM "cq"
333 VIRTIO_USER_ARG_CQ_NUM,
334 #define VIRTIO_USER_ARG_MAC "mac"
336 #define VIRTIO_USER_ARG_PATH "path"
337 VIRTIO_USER_ARG_PATH,
338 #define VIRTIO_USER_ARG_QUEUE_SIZE "queue_size"
339 VIRTIO_USER_ARG_QUEUE_SIZE,
340 #define VIRTIO_USER_ARG_INTERFACE_NAME "iface"
341 VIRTIO_USER_ARG_INTERFACE_NAME,
342 #define VIRTIO_USER_ARG_SERVER_MODE "server"
343 VIRTIO_USER_ARG_SERVER_MODE,
347 #define VIRTIO_USER_DEF_CQ_EN 0
348 #define VIRTIO_USER_DEF_Q_NUM 1
349 #define VIRTIO_USER_DEF_Q_SZ 256
350 #define VIRTIO_USER_DEF_SERVER_MODE 0
353 get_string_arg(const char *key __rte_unused,
354 const char *value, void *extra_args)
356 if (!value || !extra_args)
359 *(char **)extra_args = strdup(value);
361 if (!*(char **)extra_args)
368 get_integer_arg(const char *key __rte_unused,
369 const char *value, void *extra_args)
371 if (!value || !extra_args)
374 *(uint64_t *)extra_args = strtoull(value, NULL, 0);
379 static struct rte_vdev_driver virtio_user_driver;
381 static struct rte_eth_dev *
382 virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev)
384 struct rte_eth_dev *eth_dev;
385 struct rte_eth_dev_data *data;
386 struct virtio_hw *hw;
387 struct virtio_user_dev *dev;
389 eth_dev = rte_eth_vdev_allocate(vdev, sizeof(*hw));
391 PMD_INIT_LOG(ERR, "cannot alloc rte_eth_dev");
395 data = eth_dev->data;
396 hw = eth_dev->data->dev_private;
398 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
400 PMD_INIT_LOG(ERR, "malloc virtio_user_dev failed");
401 rte_eth_dev_release_port(eth_dev);
406 hw->port_id = data->port_id;
407 dev->port_id = data->port_id;
408 virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops;
410 * MSIX is required to enable LSC (see virtio_init_device).
411 * Here just pretend that we support msix.
415 hw->use_simple_rx = 0;
416 hw->use_simple_tx = 0;
417 hw->virtio_user_dev = dev;
422 virtio_user_eth_dev_free(struct rte_eth_dev *eth_dev)
424 struct rte_eth_dev_data *data = eth_dev->data;
425 struct virtio_hw *hw = data->dev_private;
427 rte_free(hw->virtio_user_dev);
429 rte_eth_dev_release_port(eth_dev);
432 /* Dev initialization routine. Invoked once for each virtio vdev at
433 * EAL init time, see rte_bus_probe().
434 * Returns 0 on success.
437 virtio_user_pmd_probe(struct rte_vdev_device *dev)
439 struct rte_kvargs *kvlist = NULL;
440 struct rte_eth_dev *eth_dev;
441 struct virtio_hw *hw;
442 uint64_t queues = VIRTIO_USER_DEF_Q_NUM;
443 uint64_t cq = VIRTIO_USER_DEF_CQ_EN;
444 uint64_t queue_size = VIRTIO_USER_DEF_Q_SZ;
445 uint64_t server_mode = VIRTIO_USER_DEF_SERVER_MODE;
448 char *mac_addr = NULL;
451 kvlist = rte_kvargs_parse(rte_vdev_device_args(dev), valid_args);
453 PMD_INIT_LOG(ERR, "error when parsing param");
457 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_PATH) == 1) {
458 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_PATH,
459 &get_string_arg, &path) < 0) {
460 PMD_INIT_LOG(ERR, "error to parse %s",
461 VIRTIO_USER_ARG_PATH);
465 PMD_INIT_LOG(ERR, "arg %s is mandatory for virtio_user",
466 VIRTIO_USER_ARG_QUEUE_SIZE);
470 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME) == 1) {
471 if (is_vhost_user_by_type(path)) {
473 "arg %s applies only to vhost-kernel backend",
474 VIRTIO_USER_ARG_INTERFACE_NAME);
478 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_INTERFACE_NAME,
479 &get_string_arg, &ifname) < 0) {
480 PMD_INIT_LOG(ERR, "error to parse %s",
481 VIRTIO_USER_ARG_INTERFACE_NAME);
486 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_MAC) == 1) {
487 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_MAC,
488 &get_string_arg, &mac_addr) < 0) {
489 PMD_INIT_LOG(ERR, "error to parse %s",
490 VIRTIO_USER_ARG_MAC);
495 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE) == 1) {
496 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUE_SIZE,
497 &get_integer_arg, &queue_size) < 0) {
498 PMD_INIT_LOG(ERR, "error to parse %s",
499 VIRTIO_USER_ARG_QUEUE_SIZE);
504 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_QUEUES_NUM) == 1) {
505 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_QUEUES_NUM,
506 &get_integer_arg, &queues) < 0) {
507 PMD_INIT_LOG(ERR, "error to parse %s",
508 VIRTIO_USER_ARG_QUEUES_NUM);
513 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_SERVER_MODE) == 1) {
514 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_SERVER_MODE,
515 &get_integer_arg, &server_mode) < 0) {
516 PMD_INIT_LOG(ERR, "error to parse %s",
517 VIRTIO_USER_ARG_SERVER_MODE);
522 if (rte_kvargs_count(kvlist, VIRTIO_USER_ARG_CQ_NUM) == 1) {
523 if (rte_kvargs_process(kvlist, VIRTIO_USER_ARG_CQ_NUM,
524 &get_integer_arg, &cq) < 0) {
525 PMD_INIT_LOG(ERR, "error to parse %s",
526 VIRTIO_USER_ARG_CQ_NUM);
529 } else if (queues > 1) {
533 if (queues > 1 && cq == 0) {
534 PMD_INIT_LOG(ERR, "multi-q requires ctrl-q");
538 if (queues > VIRTIO_MAX_VIRTQUEUE_PAIRS) {
539 PMD_INIT_LOG(ERR, "arg %s %" PRIu64 " exceeds the limit %u",
540 VIRTIO_USER_ARG_QUEUES_NUM, queues,
541 VIRTIO_MAX_VIRTQUEUE_PAIRS);
545 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
546 struct virtio_user_dev *vu_dev;
548 eth_dev = virtio_user_eth_dev_alloc(dev);
550 PMD_INIT_LOG(ERR, "virtio_user fails to alloc device");
554 hw = eth_dev->data->dev_private;
555 vu_dev = virtio_user_get_dev(hw);
556 if (server_mode == 1)
557 vu_dev->is_server = true;
559 vu_dev->is_server = false;
560 if (virtio_user_dev_init(hw->virtio_user_dev, path, queues, cq,
561 queue_size, mac_addr, &ifname) < 0) {
562 PMD_INIT_LOG(ERR, "virtio_user_dev_init fails");
563 virtio_user_eth_dev_free(eth_dev);
567 eth_dev = rte_eth_dev_attach_secondary(rte_vdev_device_name(dev));
572 /* previously called by rte_pci_probe() for physical dev */
573 if (eth_virtio_dev_init(eth_dev) < 0) {
574 PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails");
575 virtio_user_eth_dev_free(eth_dev);
582 rte_kvargs_free(kvlist);
592 /** Called by rte_eth_dev_detach() */
594 virtio_user_pmd_remove(struct rte_vdev_device *vdev)
597 struct rte_eth_dev *eth_dev;
598 struct virtio_hw *hw;
599 struct virtio_user_dev *dev;
604 name = rte_vdev_device_name(vdev);
605 PMD_DRV_LOG(INFO, "Un-Initializing %s", name);
606 eth_dev = rte_eth_dev_allocated(name);
610 /* make sure the device is stopped, queues freed */
611 rte_eth_dev_close(eth_dev->data->port_id);
613 hw = eth_dev->data->dev_private;
614 dev = hw->virtio_user_dev;
615 virtio_user_dev_uninit(dev);
617 rte_free(eth_dev->data->dev_private);
618 rte_eth_dev_release_port(eth_dev);
623 static struct rte_vdev_driver virtio_user_driver = {
624 .probe = virtio_user_pmd_probe,
625 .remove = virtio_user_pmd_remove,
628 RTE_PMD_REGISTER_VDEV(net_virtio_user, virtio_user_driver);
629 RTE_PMD_REGISTER_ALIAS(net_virtio_user, virtio_user);
630 RTE_PMD_REGISTER_PARAM_STRING(net_virtio_user,