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"
26 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
28 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
29 * firstly because vhost depends on this msg to allocate virtqueue
32 struct vhost_vring_file file;
34 file.index = queue_sel;
35 file.fd = dev->callfds[queue_sel];
36 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
42 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
44 struct vhost_vring_file file;
45 struct vhost_vring_state state;
46 struct vring *vring = &dev->vrings[queue_sel];
47 struct vring_packed *pq_vring = &dev->packed_vrings[queue_sel];
48 struct vhost_vring_addr addr = {
51 .flags = 0, /* disable log */
54 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED)) {
56 (uint64_t)(uintptr_t)pq_vring->desc;
57 addr.avail_user_addr =
58 (uint64_t)(uintptr_t)pq_vring->driver;
60 (uint64_t)(uintptr_t)pq_vring->device;
62 addr.desc_user_addr = (uint64_t)(uintptr_t)vring->desc;
63 addr.avail_user_addr = (uint64_t)(uintptr_t)vring->avail;
64 addr.used_user_addr = (uint64_t)(uintptr_t)vring->used;
67 state.index = queue_sel;
68 state.num = vring->num;
69 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
71 state.index = queue_sel;
72 state.num = 0; /* no reservation */
73 if (dev->features & (1ULL << VIRTIO_F_RING_PACKED))
74 state.num |= (1 << 15);
75 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
77 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
79 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
80 * lastly because vhost depends on this msg to judge if
83 file.index = queue_sel;
84 file.fd = dev->kickfds[queue_sel];
85 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
91 virtio_user_queue_setup(struct virtio_user_dev *dev,
92 int (*fn)(struct virtio_user_dev *, uint32_t))
94 uint32_t i, queue_sel;
96 for (i = 0; i < dev->max_queue_pairs; ++i) {
97 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
98 if (fn(dev, queue_sel) < 0) {
99 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
103 for (i = 0; i < dev->max_queue_pairs; ++i) {
104 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
105 if (fn(dev, queue_sel) < 0) {
106 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
115 is_vhost_user_by_type(const char *path)
119 if (stat(path, &sb) == -1)
122 return S_ISSOCK(sb.st_mode);
126 virtio_user_start_device(struct virtio_user_dev *dev)
128 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
135 * We need to make sure that the locks will be
136 * taken in the correct order to avoid deadlocks.
138 * Before releasing this lock, this thread should
139 * not trigger any memory hotplug events.
141 * This is a temporary workaround, and should be
142 * replaced when we get proper supports from the
143 * memory subsystem in the future.
145 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
146 pthread_mutex_lock(&dev->mutex);
148 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
151 /* Step 0: tell vhost to create queues */
152 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
155 /* Step 1: set features */
156 features = dev->features;
157 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
158 features &= ~(1ull << VIRTIO_NET_F_MAC);
159 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
160 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
161 features &= ~(1ull << VIRTIO_NET_F_STATUS);
162 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
165 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
167 /* Step 2: share memory regions */
168 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
172 /* Step 3: kick queues */
173 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
176 /* Step 4: enable queues
177 * we enable the 1st queue pair by default.
179 dev->ops->enable_qp(dev, 0, 1);
182 pthread_mutex_unlock(&dev->mutex);
183 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
187 pthread_mutex_unlock(&dev->mutex);
188 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
189 /* TODO: free resource here or caller to check */
193 int virtio_user_stop_device(struct virtio_user_dev *dev)
195 struct vhost_vring_state state;
199 pthread_mutex_lock(&dev->mutex);
203 for (i = 0; i < dev->max_queue_pairs; ++i)
204 dev->ops->enable_qp(dev, i, 0);
206 /* Stop the backend. */
207 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
209 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
211 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
218 dev->started = false;
220 pthread_mutex_unlock(&dev->mutex);
226 parse_mac(struct virtio_user_dev *dev, const char *mac)
229 uint32_t tmp[RTE_ETHER_ADDR_LEN];
234 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
235 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
236 if (r == RTE_ETHER_ADDR_LEN) {
237 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
238 dev->mac_addr[i] = (uint8_t)tmp[i];
239 dev->mac_specified = 1;
241 /* ignore the wrong mac, use random mac */
242 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
247 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
253 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
254 if (i >= dev->max_queue_pairs * 2) {
255 dev->kickfds[i] = -1;
256 dev->callfds[i] = -1;
260 /* May use invalid flag, but some backend uses kickfd and
261 * callfd as criteria to judge if dev is alive. so finally we
264 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
266 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
269 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
271 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
274 dev->callfds[i] = callfd;
275 dev->kickfds[i] = kickfd;
278 if (i < VIRTIO_MAX_VIRTQUEUES) {
279 for (j = 0; j <= i; ++j) {
280 close(dev->callfds[j]);
281 close(dev->kickfds[j]);
291 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
294 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
296 if (!eth_dev->intr_handle) {
297 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
298 if (!eth_dev->intr_handle) {
299 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
302 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
305 for (i = 0; i < dev->max_queue_pairs; ++i)
306 eth_dev->intr_handle->efds[i] = dev->callfds[i];
307 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
308 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
309 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
310 /* For virtio vdev, no need to read counter for clean */
311 eth_dev->intr_handle->efd_counter_size = 0;
312 eth_dev->intr_handle->fd = -1;
313 if (dev->vhostfd >= 0)
314 eth_dev->intr_handle->fd = dev->vhostfd;
315 else if (dev->is_server)
316 eth_dev->intr_handle->fd = dev->listenfd;
322 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
323 const void *addr __rte_unused,
324 size_t len __rte_unused,
327 struct virtio_user_dev *dev = arg;
328 struct rte_memseg_list *msl;
331 /* ignore externally allocated memory */
332 msl = rte_mem_virt2memseg_list(addr);
336 pthread_mutex_lock(&dev->mutex);
338 if (dev->started == false)
341 /* Step 1: pause the active queues */
342 for (i = 0; i < dev->queue_pairs; i++)
343 dev->ops->enable_qp(dev, i, 0);
345 /* Step 2: update memory regions */
346 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
348 /* Step 3: resume the active queues */
349 for (i = 0; i < dev->queue_pairs; i++)
350 dev->ops->enable_qp(dev, i, 1);
353 pthread_mutex_unlock(&dev->mutex);
357 virtio_user_dev_setup(struct virtio_user_dev *dev)
362 dev->vhostfds = NULL;
365 if (dev->is_server) {
366 if (access(dev->path, F_OK) == 0 &&
367 !is_vhost_user_by_type(dev->path)) {
368 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
371 dev->ops = &virtio_ops_user;
373 if (is_vhost_user_by_type(dev->path)) {
374 dev->ops = &virtio_ops_user;
376 dev->ops = &virtio_ops_kernel;
378 dev->vhostfds = malloc(dev->max_queue_pairs *
380 dev->tapfds = malloc(dev->max_queue_pairs *
382 if (!dev->vhostfds || !dev->tapfds) {
383 PMD_INIT_LOG(ERR, "Failed to malloc");
387 for (q = 0; q < dev->max_queue_pairs; ++q) {
388 dev->vhostfds[q] = -1;
394 if (dev->ops->setup(dev) < 0)
397 if (virtio_user_dev_init_notify(dev) < 0)
400 if (virtio_user_fill_intr_handle(dev) < 0)
406 /* Use below macro to filter features from vhost backend */
407 #define VIRTIO_USER_SUPPORTED_FEATURES \
408 (1ULL << VIRTIO_NET_F_MAC | \
409 1ULL << VIRTIO_NET_F_STATUS | \
410 1ULL << VIRTIO_NET_F_MQ | \
411 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
412 1ULL << VIRTIO_NET_F_CTRL_VQ | \
413 1ULL << VIRTIO_NET_F_CTRL_RX | \
414 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
415 1ULL << VIRTIO_NET_F_CSUM | \
416 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
417 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
418 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
419 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
420 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
421 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
422 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
423 1ULL << VIRTIO_F_IN_ORDER | \
424 1ULL << VIRTIO_F_VERSION_1 | \
425 1ULL << VIRTIO_F_RING_PACKED)
428 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
429 int cq, int queue_size, const char *mac, char **ifname,
430 int server, int mrg_rxbuf, int in_order, int packed_vq)
432 pthread_mutex_init(&dev->mutex, NULL);
433 strlcpy(dev->path, path, PATH_MAX);
435 dev->max_queue_pairs = queues;
436 dev->queue_pairs = 1; /* mq disabled by default */
437 dev->queue_size = queue_size;
438 dev->is_server = server;
439 dev->mac_specified = 0;
440 dev->frontend_features = 0;
441 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
445 dev->ifname = *ifname;
449 if (virtio_user_dev_setup(dev) < 0) {
450 PMD_INIT_LOG(ERR, "backend set up fails");
454 if (!dev->is_server) {
455 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
457 PMD_INIT_LOG(ERR, "set_owner fails: %s",
462 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
463 &dev->device_features) < 0) {
464 PMD_INIT_LOG(ERR, "get_features failed: %s",
469 /* We just pretend vhost-user can support all these features.
470 * Note that this could be problematic that if some feature is
471 * negotiated but not supported by the vhost-user which comes
474 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
478 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
481 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
484 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
486 if (dev->mac_specified)
487 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
489 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
492 /* device does not really need to know anything about CQ,
493 * so if necessary, we just claim to support CQ
495 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
497 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
498 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
499 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
500 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
501 dev->unsupported_features |=
502 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
503 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
504 dev->unsupported_features |=
505 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
508 /* The backend will not report this feature, we add it explicitly */
509 if (is_vhost_user_by_type(dev->path))
510 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
514 * (frontend_features | backend_features) & ~unsupported_features;
516 dev->device_features |= dev->frontend_features;
517 dev->device_features &= ~dev->unsupported_features;
519 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
520 virtio_user_mem_event_cb, dev)) {
521 if (rte_errno != ENOTSUP) {
522 PMD_INIT_LOG(ERR, "Failed to register mem event"
532 virtio_user_dev_uninit(struct virtio_user_dev *dev)
536 virtio_user_stop_device(dev);
538 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
540 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
541 close(dev->callfds[i]);
542 close(dev->kickfds[i]);
547 if (dev->is_server && dev->listenfd >= 0) {
548 close(dev->listenfd);
553 for (i = 0; i < dev->max_queue_pairs; ++i)
554 close(dev->vhostfds[i]);
566 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
571 if (q_pairs > dev->max_queue_pairs) {
572 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
573 q_pairs, dev->max_queue_pairs);
577 /* Server mode can't enable queue pairs if vhostfd is invalid,
578 * always return 0 in this case.
580 if (!dev->is_server || dev->vhostfd >= 0) {
581 for (i = 0; i < q_pairs; ++i)
582 ret |= dev->ops->enable_qp(dev, i, 1);
583 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
584 ret |= dev->ops->enable_qp(dev, i, 0);
586 dev->queue_pairs = q_pairs;
592 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
595 struct virtio_net_ctrl_hdr *hdr;
596 virtio_net_ctrl_ack status = ~0;
597 uint16_t i, idx_data, idx_status;
598 uint32_t n_descs = 0;
600 /* locate desc for header, data, and status */
601 idx_data = vring->desc[idx_hdr].next;
605 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
606 i = vring->desc[i].next;
610 /* locate desc for status */
614 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
615 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
616 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
619 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
620 status = virtio_user_handle_mq(dev, queues);
624 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
630 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
632 uint16_t flags = desc->flags;
634 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
635 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
639 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
640 struct vring_packed *vring,
643 struct virtio_net_ctrl_hdr *hdr;
644 virtio_net_ctrl_ack status = ~0;
645 uint16_t idx_data, idx_status;
646 /* initialize to one, header is first */
647 uint32_t n_descs = 1;
649 /* locate desc for header, data, and status */
650 idx_data = idx_hdr + 1;
651 if (idx_data >= dev->queue_size)
652 idx_data -= dev->queue_size;
656 idx_status = idx_data;
657 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
659 if (idx_status >= dev->queue_size)
660 idx_status -= dev->queue_size;
664 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
665 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
666 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
669 queues = *(uint16_t *)(uintptr_t)
670 vring->desc[idx_data].addr;
671 status = virtio_user_handle_mq(dev, queues);
675 *(virtio_net_ctrl_ack *)(uintptr_t)
676 vring->desc[idx_status].addr = status;
678 /* Update used descriptor */
679 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
680 vring->desc[idx_hdr].len = sizeof(status);
686 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
688 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
689 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
690 uint16_t n_descs, flags;
692 while (desc_is_avail(&vring->desc[vq->used_idx],
693 vq->used_wrap_counter)) {
695 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
698 flags = VRING_DESC_F_WRITE;
699 if (vq->used_wrap_counter)
700 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
703 vring->desc[vq->used_idx].flags = flags;
705 vq->used_idx += n_descs;
706 if (vq->used_idx >= dev->queue_size) {
707 vq->used_idx -= dev->queue_size;
708 vq->used_wrap_counter ^= 1;
714 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
716 uint16_t avail_idx, desc_idx;
717 struct vring_used_elem *uep;
719 struct vring *vring = &dev->vrings[queue_idx];
721 /* Consume avail ring, using used ring idx as first one */
722 while (vring->used->idx != vring->avail->idx) {
723 avail_idx = (vring->used->idx) & (vring->num - 1);
724 desc_idx = vring->avail->ring[avail_idx];
726 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
728 /* Update used ring */
729 uep = &vring->used->ring[avail_idx];