1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
16 #include <rte_string_fns.h>
17 #include <rte_eal_memconfig.h>
20 #include "virtio_user_dev.h"
21 #include "../virtio_ethdev.h"
23 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
25 const char * const virtio_user_backend_strings[] = {
26 [VIRTIO_USER_BACKEND_UNKNOWN] = "VIRTIO_USER_BACKEND_UNKNOWN",
27 [VIRTIO_USER_BACKEND_VHOST_USER] = "VHOST_USER",
28 [VIRTIO_USER_BACKEND_VHOST_KERNEL] = "VHOST_NET",
29 [VIRTIO_USER_BACKEND_VHOST_VDPA] = "VHOST_VDPA",
33 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
35 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
36 * firstly because vhost depends on this msg to allocate virtqueue
39 struct vhost_vring_file file;
42 file.index = queue_sel;
43 file.fd = dev->callfds[queue_sel];
44 ret = dev->ops->set_vring_call(dev, &file);
46 PMD_INIT_LOG(ERR, "(%s) Failed to create queue %u\n", dev->path, queue_sel);
54 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
57 struct vhost_vring_file file;
58 struct vhost_vring_state state;
59 struct vring *vring = &dev->vrings[queue_sel];
60 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
61 struct vhost_vring_addr addr = {
64 .flags = 0, /* disable log */
67 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
69 (uint64_t)(uintptr_t)pq_vring->desc;
70 addr.avail_user_addr =
71 (uint64_t)(uintptr_t)pq_vring->driver;
73 (uint64_t)(uintptr_t)pq_vring->device;
75 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
76 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
77 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
80 state.index = queue_sel;
81 state.num = vring->num;
82 ret = dev->ops->set_vring_num(dev, &state);
86 state.index = queue_sel;
87 state.num = 0; /* no reservation */
88 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
89 state.num |= (1 << 15);
90 ret = dev->ops->set_vring_base(dev, &state);
94 ret = dev->ops->set_vring_addr(dev, &addr);
98 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
99 * lastly because vhost depends on this msg to judge if
102 file.index = queue_sel;
103 file.fd = dev->kickfds[queue_sel];
104 ret = dev->ops->set_vring_kick(dev, &file);
110 PMD_INIT_LOG(ERR, "(%s) Failed to kick queue %u\n", dev->path, queue_sel);
116 virtio_user_queue_setup(struct virtio_user_dev *dev,
117 int (*fn)(struct virtio_user_dev *, uint32_t))
119 uint32_t i, queue_sel;
121 for (i = 0; i < dev->max_queue_pairs; ++i) {
122 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
123 if (fn(dev, queue_sel) < 0) {
124 PMD_DRV_LOG(ERR, "(%s) setup rx vq %u failed", dev->path, i);
128 for (i = 0; i < dev->max_queue_pairs; ++i) {
129 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
130 if (fn(dev, queue_sel) < 0) {
131 PMD_DRV_LOG(INFO, "(%s) setup tx vq %u failed", dev->path, i);
140 virtio_user_dev_set_features(struct virtio_user_dev *dev)
145 pthread_mutex_lock(&dev->mutex);
147 /* Step 0: tell vhost to create queues */
148 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
151 features = dev->features;
153 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
154 features &= ~(1ull << VIRTIO_NET_F_MAC);
155 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
156 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
157 features &= ~(1ull << VIRTIO_NET_F_STATUS);
158 ret = dev->ops->set_features(dev, features);
161 PMD_DRV_LOG(INFO, "(%s) set features: 0x%" PRIx64, dev->path, features);
163 pthread_mutex_unlock(&dev->mutex);
169 virtio_user_start_device(struct virtio_user_dev *dev)
176 * We need to make sure that the locks will be
177 * taken in the correct order to avoid deadlocks.
179 * Before releasing this lock, this thread should
180 * not trigger any memory hotplug events.
182 * This is a temporary workaround, and should be
183 * replaced when we get proper supports from the
184 * memory subsystem in the future.
186 rte_mcfg_mem_read_lock();
187 pthread_mutex_lock(&dev->mutex);
189 /* Step 2: share memory regions */
190 ret = dev->ops->set_memory_table(dev);
194 /* Step 3: kick queues */
195 ret = virtio_user_queue_setup(dev, virtio_user_kick_queue);
199 /* Step 4: enable queues
200 * we enable the 1st queue pair by default.
202 ret = dev->ops->enable_qp(dev, 0, 1);
208 pthread_mutex_unlock(&dev->mutex);
209 rte_mcfg_mem_read_unlock();
213 pthread_mutex_unlock(&dev->mutex);
214 rte_mcfg_mem_read_unlock();
216 PMD_INIT_LOG(ERR, "(%s) Failed to start device\n", dev->path);
218 /* TODO: free resource here or caller to check */
222 int virtio_user_stop_device(struct virtio_user_dev *dev)
224 struct vhost_vring_state state;
228 pthread_mutex_lock(&dev->mutex);
232 for (i = 0; i < dev->max_queue_pairs; ++i) {
233 ret = dev->ops->enable_qp(dev, i, 0);
238 /* Stop the backend. */
239 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
241 ret = dev->ops->get_vring_base(dev, &state);
243 PMD_DRV_LOG(ERR, "(%s) get_vring_base failed, index=%u", dev->path, i);
248 dev->started = false;
251 pthread_mutex_unlock(&dev->mutex);
255 pthread_mutex_unlock(&dev->mutex);
257 PMD_INIT_LOG(ERR, "(%s) Failed to stop device\n", dev->path);
263 parse_mac(struct virtio_user_dev *dev, const char *mac)
265 struct rte_ether_addr tmp;
270 if (rte_ether_unformat_addr(mac, &tmp) == 0) {
271 memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
272 dev->mac_specified = 1;
274 /* ignore the wrong mac, use random mac */
275 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
280 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
286 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
287 if (i >= dev->max_queue_pairs * 2) {
288 dev->kickfds[i] = -1;
289 dev->callfds[i] = -1;
293 /* May use invalid flag, but some backend uses kickfd and
294 * callfd as criteria to judge if dev is alive. so finally we
297 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
299 PMD_DRV_LOG(ERR, "(%s) callfd error, %s", dev->path, strerror(errno));
302 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
305 PMD_DRV_LOG(ERR, "(%s) kickfd error, %s", dev->path, strerror(errno));
308 dev->callfds[i] = callfd;
309 dev->kickfds[i] = kickfd;
312 if (i < VIRTIO_MAX_VIRTQUEUES) {
313 for (j = 0; j < i; ++j) {
314 close(dev->callfds[j]);
315 close(dev->kickfds[j]);
325 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
328 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
330 if (!eth_dev->intr_handle) {
331 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
332 if (!eth_dev->intr_handle) {
333 PMD_DRV_LOG(ERR, "(%s) failed to allocate intr_handle", dev->path);
336 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
339 for (i = 0; i < dev->max_queue_pairs; ++i)
340 eth_dev->intr_handle->efds[i] = dev->callfds[i];
341 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
342 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
343 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
344 /* For virtio vdev, no need to read counter for clean */
345 eth_dev->intr_handle->efd_counter_size = 0;
346 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
352 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
354 size_t len __rte_unused,
357 struct virtio_user_dev *dev = arg;
358 struct rte_memseg_list *msl;
362 /* ignore externally allocated memory */
363 msl = rte_mem_virt2memseg_list(addr);
367 pthread_mutex_lock(&dev->mutex);
369 if (dev->started == false)
372 /* Step 1: pause the active queues */
373 for (i = 0; i < dev->queue_pairs; i++) {
374 ret = dev->ops->enable_qp(dev, i, 0);
379 /* Step 2: update memory regions */
380 ret = dev->ops->set_memory_table(dev);
384 /* Step 3: resume the active queues */
385 for (i = 0; i < dev->queue_pairs; i++) {
386 ret = dev->ops->enable_qp(dev, i, 1);
392 pthread_mutex_unlock(&dev->mutex);
395 PMD_DRV_LOG(ERR, "(%s) Failed to update memory table\n", dev->path);
399 virtio_user_dev_setup(struct virtio_user_dev *dev)
401 if (dev->is_server) {
402 if (dev->backend_type != VIRTIO_USER_BACKEND_VHOST_USER) {
403 PMD_DRV_LOG(ERR, "Server mode only supports vhost-user!");
408 switch (dev->backend_type) {
409 case VIRTIO_USER_BACKEND_VHOST_USER:
410 dev->ops = &virtio_ops_user;
412 case VIRTIO_USER_BACKEND_VHOST_KERNEL:
413 dev->ops = &virtio_ops_kernel;
415 case VIRTIO_USER_BACKEND_VHOST_VDPA:
416 dev->ops = &virtio_ops_vdpa;
419 PMD_DRV_LOG(ERR, "(%s) Unknown backend type", dev->path);
423 if (dev->ops->setup(dev) < 0) {
424 PMD_INIT_LOG(ERR, "(%s) Failed to setup backend\n", dev->path);
428 if (virtio_user_dev_init_notify(dev) < 0) {
429 PMD_INIT_LOG(ERR, "(%s) Failed to init notifiers\n", dev->path);
433 if (virtio_user_fill_intr_handle(dev) < 0) {
434 PMD_INIT_LOG(ERR, "(%s) Failed to init interrupt handler\n", dev->path);
441 /* Use below macro to filter features from vhost backend */
442 #define VIRTIO_USER_SUPPORTED_FEATURES \
443 (1ULL << VIRTIO_NET_F_MAC | \
444 1ULL << VIRTIO_NET_F_STATUS | \
445 1ULL << VIRTIO_NET_F_MQ | \
446 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
447 1ULL << VIRTIO_NET_F_CTRL_VQ | \
448 1ULL << VIRTIO_NET_F_CTRL_RX | \
449 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
450 1ULL << VIRTIO_NET_F_CSUM | \
451 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
452 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
453 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
454 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
455 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
456 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
457 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
458 1ULL << VIRTIO_F_IN_ORDER | \
459 1ULL << VIRTIO_F_VERSION_1 | \
460 1ULL << VIRTIO_F_RING_PACKED)
463 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
464 int cq, int queue_size, const char *mac, char **ifname,
465 int server, int mrg_rxbuf, int in_order, int packed_vq,
466 enum virtio_user_backend_type backend_type)
468 uint64_t backend_features;
470 pthread_mutex_init(&dev->mutex, NULL);
471 strlcpy(dev->path, path, PATH_MAX);
473 dev->max_queue_pairs = queues;
474 dev->queue_pairs = 1; /* mq disabled by default */
475 dev->queue_size = queue_size;
476 dev->is_server = server;
477 dev->mac_specified = 0;
478 dev->frontend_features = 0;
479 dev->unsupported_features = 0;
480 dev->backend_type = backend_type;
485 dev->ifname = *ifname;
489 if (virtio_user_dev_setup(dev) < 0) {
490 PMD_INIT_LOG(ERR, "(%s) backend set up fails", dev->path);
494 if (dev->ops->set_owner(dev) < 0) {
495 PMD_INIT_LOG(ERR, "(%s) Failed to set backend owner", dev->path);
499 if (dev->ops->get_backend_features(&backend_features) < 0) {
500 PMD_INIT_LOG(ERR, "(%s) Failed to get backend features", dev->path);
504 dev->unsupported_features = ~(VIRTIO_USER_SUPPORTED_FEATURES | backend_features);
506 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
507 PMD_INIT_LOG(ERR, "(%s) Failed to get device features", dev->path);
512 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
515 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
518 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
520 if (dev->mac_specified)
521 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
523 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
526 /* device does not really need to know anything about CQ,
527 * so if necessary, we just claim to support CQ
529 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
531 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
532 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
533 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
534 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
535 dev->unsupported_features |=
536 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
537 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
538 dev->unsupported_features |=
539 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
542 /* The backend will not report this feature, we add it explicitly */
543 if (dev->backend_type == VIRTIO_USER_BACKEND_VHOST_USER)
544 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
548 * (frontend_features | backend_features) & ~unsupported_features;
550 dev->device_features |= dev->frontend_features;
551 dev->device_features &= ~dev->unsupported_features;
553 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
554 virtio_user_mem_event_cb, dev)) {
555 if (rte_errno != ENOTSUP) {
556 PMD_INIT_LOG(ERR, "(%s) Failed to register mem event callback\n",
566 virtio_user_dev_uninit(struct virtio_user_dev *dev)
570 virtio_user_stop_device(dev);
572 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
574 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
575 close(dev->callfds[i]);
576 close(dev->kickfds[i]);
584 dev->ops->destroy(dev);
588 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
593 if (q_pairs > dev->max_queue_pairs) {
594 PMD_INIT_LOG(ERR, "(%s) multi-q config %u, but only %u supported",
595 dev->path, q_pairs, dev->max_queue_pairs);
599 for (i = 0; i < q_pairs; ++i)
600 ret |= dev->ops->enable_qp(dev, i, 1);
601 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
602 ret |= dev->ops->enable_qp(dev, i, 0);
604 dev->queue_pairs = q_pairs;
610 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
613 struct virtio_net_ctrl_hdr *hdr;
614 virtio_net_ctrl_ack status = ~0;
615 uint16_t i, idx_data, idx_status;
616 uint32_t n_descs = 0;
618 /* locate desc for header, data, and status */
619 idx_data = vring->desc[idx_hdr].next;
623 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
624 i = vring->desc[i].next;
628 /* locate desc for status */
632 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
633 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
634 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
637 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
638 status = virtio_user_handle_mq(dev, queues);
639 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
640 hdr->class == VIRTIO_NET_CTRL_MAC ||
641 hdr->class == VIRTIO_NET_CTRL_VLAN) {
646 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
652 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
654 uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
656 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
657 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
661 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
662 struct vring_packed *vring,
665 struct virtio_net_ctrl_hdr *hdr;
666 virtio_net_ctrl_ack status = ~0;
667 uint16_t idx_data, idx_status;
668 /* initialize to one, header is first */
669 uint32_t n_descs = 1;
671 /* locate desc for header, data, and status */
672 idx_data = idx_hdr + 1;
673 if (idx_data >= dev->queue_size)
674 idx_data -= dev->queue_size;
678 idx_status = idx_data;
679 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
681 if (idx_status >= dev->queue_size)
682 idx_status -= dev->queue_size;
686 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
687 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
688 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
691 queues = *(uint16_t *)(uintptr_t)
692 vring->desc[idx_data].addr;
693 status = virtio_user_handle_mq(dev, queues);
694 } else if (hdr->class == VIRTIO_NET_CTRL_RX ||
695 hdr->class == VIRTIO_NET_CTRL_MAC ||
696 hdr->class == VIRTIO_NET_CTRL_VLAN) {
701 *(virtio_net_ctrl_ack *)(uintptr_t)
702 vring->desc[idx_status].addr = status;
704 /* Update used descriptor */
705 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
706 vring->desc[idx_hdr].len = sizeof(status);
712 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
714 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
715 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
716 uint16_t n_descs, flags;
718 /* Perform a load-acquire barrier in desc_is_avail to
719 * enforce the ordering between desc flags and desc
722 while (desc_is_avail(&vring->desc[vq->used_idx],
723 vq->used_wrap_counter)) {
725 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
728 flags = VRING_DESC_F_WRITE;
729 if (vq->used_wrap_counter)
730 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
732 __atomic_store_n(&vring->desc[vq->used_idx].flags, flags,
735 vq->used_idx += n_descs;
736 if (vq->used_idx >= dev->queue_size) {
737 vq->used_idx -= dev->queue_size;
738 vq->used_wrap_counter ^= 1;
744 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
746 uint16_t avail_idx, desc_idx;
747 struct vring_used_elem *uep;
749 struct vring *vring = &dev->vrings[queue_idx];
751 /* Consume avail ring, using used ring idx as first one */
752 while (__atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
753 != vring->avail->idx) {
754 avail_idx = __atomic_load_n(&vring->used->idx, __ATOMIC_RELAXED)
756 desc_idx = vring->avail->ring[avail_idx];
758 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
760 /* Update used ring */
761 uep = &vring->used->ring[avail_idx];
765 __atomic_add_fetch(&vring->used->idx, 1, __ATOMIC_RELAXED);
770 virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status)
774 pthread_mutex_lock(&dev->mutex);
775 dev->status = status;
776 ret = dev->ops->set_status(dev, status);
777 if (ret && ret != -ENOTSUP)
778 PMD_INIT_LOG(ERR, "(%s) Failed to set backend status\n", dev->path);
780 pthread_mutex_unlock(&dev->mutex);
785 virtio_user_dev_update_status(struct virtio_user_dev *dev)
790 pthread_mutex_lock(&dev->mutex);
792 ret = dev->ops->get_status(dev, &status);
794 dev->status = status;
795 PMD_INIT_LOG(DEBUG, "Updated Device Status(0x%08x):\n"
797 "\t-ACKNOWLEDGE: %u\n"
800 "\t-FEATURES_OK: %u\n"
801 "\t-DEVICE_NEED_RESET: %u\n"
804 (dev->status == VIRTIO_CONFIG_STATUS_RESET),
805 !!(dev->status & VIRTIO_CONFIG_STATUS_ACK),
806 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER),
807 !!(dev->status & VIRTIO_CONFIG_STATUS_DRIVER_OK),
808 !!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK),
809 !!(dev->status & VIRTIO_CONFIG_STATUS_DEV_NEED_RESET),
810 !!(dev->status & VIRTIO_CONFIG_STATUS_FAILED));
811 } else if (ret != -ENOTSUP) {
812 PMD_INIT_LOG(ERR, "(%s) Failed to get backend status\n", dev->path);
815 pthread_mutex_unlock(&dev->mutex);
820 virtio_user_dev_update_link_state(struct virtio_user_dev *dev)
822 if (dev->ops->update_link_state)
823 return dev->ops->update_link_state(dev);
829 virtio_user_dev_reset_queues_packed(struct rte_eth_dev *eth_dev)
831 struct virtio_user_dev *dev = eth_dev->data->dev_private;
832 struct virtio_hw *hw = &dev->hw;
833 struct virtnet_rx *rxvq;
834 struct virtnet_tx *txvq;
837 /* Add lock to avoid queue contention. */
838 rte_spinlock_lock(&hw->state_lock);
842 * Waiting for datapath to complete before resetting queues.
843 * 1 ms should be enough for the ongoing Tx/Rx function to finish.
847 /* Vring reset for each Tx queue and Rx queue. */
848 for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
849 rxvq = eth_dev->data->rx_queues[i];
850 virtqueue_rxvq_reset_packed(rxvq->vq);
851 virtio_dev_rx_queue_setup_finish(eth_dev, i);
854 for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
855 txvq = eth_dev->data->tx_queues[i];
856 virtqueue_txvq_reset_packed(txvq->vq);
860 rte_spinlock_unlock(&hw->state_lock);
864 virtio_user_dev_delayed_handler(void *param)
866 struct virtio_user_dev *dev = param;
867 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
869 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
870 PMD_DRV_LOG(ERR, "interrupt disable failed");
873 rte_intr_callback_unregister(eth_dev->intr_handle,
874 virtio_interrupt_handler, eth_dev);
875 if (dev->is_server) {
876 if (dev->ops->server_disconnect)
877 dev->ops->server_disconnect(dev);
878 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
879 rte_intr_callback_register(eth_dev->intr_handle,
880 virtio_interrupt_handler, eth_dev);
881 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
882 PMD_DRV_LOG(ERR, "interrupt enable failed");
889 virtio_user_dev_server_reconnect(struct virtio_user_dev *dev)
892 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
893 struct virtio_hw *hw = &dev->hw;
895 if (!dev->ops->server_reconnect) {
896 PMD_DRV_LOG(ERR, "(%s) Missing server reconnect callback", dev->path);
900 if (dev->ops->server_reconnect(dev)) {
901 PMD_DRV_LOG(ERR, "(%s) Reconnect callback call failed", dev->path);
905 old_status = dev->status;
909 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_ACK);
911 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER);
913 if (dev->ops->get_features(dev, &dev->device_features) < 0) {
914 PMD_INIT_LOG(ERR, "get_features failed: %s",
919 dev->device_features |= dev->frontend_features;
921 /* unmask vhost-user unsupported features */
922 dev->device_features &= ~(dev->unsupported_features);
924 dev->features &= dev->device_features;
926 /* For packed ring, resetting queues is required in reconnection. */
927 if (virtio_with_packed_queue(hw) &&
928 (old_status & VIRTIO_CONFIG_STATUS_DRIVER_OK)) {
929 PMD_INIT_LOG(NOTICE, "Packets on the fly will be dropped"
930 " when packed ring reconnecting.");
931 virtio_user_dev_reset_queues_packed(eth_dev);
934 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_FEATURES_OK);
936 /* Start the device */
937 virtio_set_status(hw, VIRTIO_CONFIG_STATUS_DRIVER_OK);
941 if (dev->queue_pairs > 1) {
942 ret = virtio_user_handle_mq(dev, dev->queue_pairs);
944 PMD_INIT_LOG(ERR, "Fails to enable multi-queue pairs!");
948 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
949 if (rte_intr_disable(eth_dev->intr_handle) < 0) {
950 PMD_DRV_LOG(ERR, "interrupt disable failed");
953 rte_intr_callback_unregister(eth_dev->intr_handle,
954 virtio_interrupt_handler,
957 eth_dev->intr_handle->fd = dev->ops->get_intr_fd(dev);
958 rte_intr_callback_register(eth_dev->intr_handle,
959 virtio_interrupt_handler, eth_dev);
961 if (rte_intr_enable(eth_dev->intr_handle) < 0) {
962 PMD_DRV_LOG(ERR, "interrupt enable failed");
966 PMD_INIT_LOG(NOTICE, "server mode virtio-user reconnection succeeds!");