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)
134 * We need to make sure that the locks will be
135 * taken in the correct order to avoid deadlocks.
137 * Before releasing this lock, this thread should
138 * not trigger any memory hotplug events.
140 * This is a temporary workaround, and should be
141 * replaced when we get proper supports from the
142 * memory subsystem in the future.
144 rte_mcfg_mem_read_lock();
145 pthread_mutex_lock(&dev->mutex);
147 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
150 /* Step 0: tell vhost to create queues */
151 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
154 /* Step 1: set features */
155 features = dev->features;
156 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
157 features &= ~(1ull << VIRTIO_NET_F_MAC);
158 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
159 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
160 features &= ~(1ull << VIRTIO_NET_F_STATUS);
161 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
164 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
166 /* Step 2: share memory regions */
167 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
171 /* Step 3: kick queues */
172 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
175 /* Step 4: enable queues
176 * we enable the 1st queue pair by default.
178 dev->ops->enable_qp(dev, 0, 1);
181 pthread_mutex_unlock(&dev->mutex);
182 rte_mcfg_mem_read_unlock();
186 pthread_mutex_unlock(&dev->mutex);
187 rte_mcfg_mem_read_unlock();
188 /* TODO: free resource here or caller to check */
192 int virtio_user_stop_device(struct virtio_user_dev *dev)
194 struct vhost_vring_state state;
198 pthread_mutex_lock(&dev->mutex);
202 for (i = 0; i < dev->max_queue_pairs; ++i)
203 dev->ops->enable_qp(dev, i, 0);
205 /* Stop the backend. */
206 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
208 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
210 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
217 dev->started = false;
219 pthread_mutex_unlock(&dev->mutex);
225 parse_mac(struct virtio_user_dev *dev, const char *mac)
228 uint32_t tmp[RTE_ETHER_ADDR_LEN];
233 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
234 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
235 if (r == RTE_ETHER_ADDR_LEN) {
236 for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
237 dev->mac_addr[i] = (uint8_t)tmp[i];
238 dev->mac_specified = 1;
240 /* ignore the wrong mac, use random mac */
241 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
246 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
252 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
253 if (i >= dev->max_queue_pairs * 2) {
254 dev->kickfds[i] = -1;
255 dev->callfds[i] = -1;
259 /* May use invalid flag, but some backend uses kickfd and
260 * callfd as criteria to judge if dev is alive. so finally we
263 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
265 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
268 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
270 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
273 dev->callfds[i] = callfd;
274 dev->kickfds[i] = kickfd;
277 if (i < VIRTIO_MAX_VIRTQUEUES) {
278 for (j = 0; j <= i; ++j) {
279 close(dev->callfds[j]);
280 close(dev->kickfds[j]);
290 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
293 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
295 if (!eth_dev->intr_handle) {
296 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
297 if (!eth_dev->intr_handle) {
298 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
301 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
304 for (i = 0; i < dev->max_queue_pairs; ++i)
305 eth_dev->intr_handle->efds[i] = dev->callfds[i];
306 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
307 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
308 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
309 /* For virtio vdev, no need to read counter for clean */
310 eth_dev->intr_handle->efd_counter_size = 0;
311 eth_dev->intr_handle->fd = -1;
312 if (dev->vhostfd >= 0)
313 eth_dev->intr_handle->fd = dev->vhostfd;
314 else if (dev->is_server)
315 eth_dev->intr_handle->fd = dev->listenfd;
321 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
322 const void *addr __rte_unused,
323 size_t len __rte_unused,
326 struct virtio_user_dev *dev = arg;
327 struct rte_memseg_list *msl;
330 /* ignore externally allocated memory */
331 msl = rte_mem_virt2memseg_list(addr);
335 pthread_mutex_lock(&dev->mutex);
337 if (dev->started == false)
340 /* Step 1: pause the active queues */
341 for (i = 0; i < dev->queue_pairs; i++)
342 dev->ops->enable_qp(dev, i, 0);
344 /* Step 2: update memory regions */
345 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
347 /* Step 3: resume the active queues */
348 for (i = 0; i < dev->queue_pairs; i++)
349 dev->ops->enable_qp(dev, i, 1);
352 pthread_mutex_unlock(&dev->mutex);
356 virtio_user_dev_setup(struct virtio_user_dev *dev)
361 dev->vhostfds = NULL;
364 if (dev->is_server) {
365 if (access(dev->path, F_OK) == 0 &&
366 !is_vhost_user_by_type(dev->path)) {
367 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
370 dev->ops = &virtio_ops_user;
372 if (is_vhost_user_by_type(dev->path)) {
373 dev->ops = &virtio_ops_user;
375 dev->ops = &virtio_ops_kernel;
377 dev->vhostfds = malloc(dev->max_queue_pairs *
379 dev->tapfds = malloc(dev->max_queue_pairs *
381 if (!dev->vhostfds || !dev->tapfds) {
382 PMD_INIT_LOG(ERR, "Failed to malloc");
386 for (q = 0; q < dev->max_queue_pairs; ++q) {
387 dev->vhostfds[q] = -1;
393 if (dev->ops->setup(dev) < 0)
396 if (virtio_user_dev_init_notify(dev) < 0)
399 if (virtio_user_fill_intr_handle(dev) < 0)
405 /* Use below macro to filter features from vhost backend */
406 #define VIRTIO_USER_SUPPORTED_FEATURES \
407 (1ULL << VIRTIO_NET_F_MAC | \
408 1ULL << VIRTIO_NET_F_STATUS | \
409 1ULL << VIRTIO_NET_F_MQ | \
410 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
411 1ULL << VIRTIO_NET_F_CTRL_VQ | \
412 1ULL << VIRTIO_NET_F_CTRL_RX | \
413 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
414 1ULL << VIRTIO_NET_F_CSUM | \
415 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
416 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
417 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
418 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
419 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
420 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
421 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
422 1ULL << VIRTIO_F_IN_ORDER | \
423 1ULL << VIRTIO_F_VERSION_1 | \
424 1ULL << VIRTIO_F_RING_PACKED)
427 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
428 int cq, int queue_size, const char *mac, char **ifname,
429 int server, int mrg_rxbuf, int in_order, int packed_vq)
431 pthread_mutex_init(&dev->mutex, NULL);
432 strlcpy(dev->path, path, PATH_MAX);
434 dev->max_queue_pairs = queues;
435 dev->queue_pairs = 1; /* mq disabled by default */
436 dev->queue_size = queue_size;
437 dev->is_server = server;
438 dev->mac_specified = 0;
439 dev->frontend_features = 0;
440 dev->unsupported_features = ~VIRTIO_USER_SUPPORTED_FEATURES;
444 dev->ifname = *ifname;
448 if (virtio_user_dev_setup(dev) < 0) {
449 PMD_INIT_LOG(ERR, "backend set up fails");
453 if (!dev->is_server) {
454 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
456 PMD_INIT_LOG(ERR, "set_owner fails: %s",
461 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
462 &dev->device_features) < 0) {
463 PMD_INIT_LOG(ERR, "get_features failed: %s",
468 /* We just pretend vhost-user can support all these features.
469 * Note that this could be problematic that if some feature is
470 * negotiated but not supported by the vhost-user which comes
473 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
477 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
480 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
483 dev->unsupported_features |= (1ull << VIRTIO_F_RING_PACKED);
485 if (dev->mac_specified)
486 dev->frontend_features |= (1ull << VIRTIO_NET_F_MAC);
488 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
491 /* device does not really need to know anything about CQ,
492 * so if necessary, we just claim to support CQ
494 dev->frontend_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
496 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
497 /* Also disable features that depend on VIRTIO_NET_F_CTRL_VQ */
498 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
499 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
500 dev->unsupported_features |=
501 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
502 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
503 dev->unsupported_features |=
504 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
507 /* The backend will not report this feature, we add it explicitly */
508 if (is_vhost_user_by_type(dev->path))
509 dev->frontend_features |= (1ull << VIRTIO_NET_F_STATUS);
513 * (frontend_features | backend_features) & ~unsupported_features;
515 dev->device_features |= dev->frontend_features;
516 dev->device_features &= ~dev->unsupported_features;
518 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
519 virtio_user_mem_event_cb, dev)) {
520 if (rte_errno != ENOTSUP) {
521 PMD_INIT_LOG(ERR, "Failed to register mem event"
531 virtio_user_dev_uninit(struct virtio_user_dev *dev)
535 virtio_user_stop_device(dev);
537 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
539 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
540 close(dev->callfds[i]);
541 close(dev->kickfds[i]);
546 if (dev->is_server && dev->listenfd >= 0) {
547 close(dev->listenfd);
552 for (i = 0; i < dev->max_queue_pairs; ++i)
553 close(dev->vhostfds[i]);
565 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
570 if (q_pairs > dev->max_queue_pairs) {
571 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
572 q_pairs, dev->max_queue_pairs);
576 /* Server mode can't enable queue pairs if vhostfd is invalid,
577 * always return 0 in this case.
579 if (!dev->is_server || dev->vhostfd >= 0) {
580 for (i = 0; i < q_pairs; ++i)
581 ret |= dev->ops->enable_qp(dev, i, 1);
582 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
583 ret |= dev->ops->enable_qp(dev, i, 0);
585 dev->queue_pairs = q_pairs;
591 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
594 struct virtio_net_ctrl_hdr *hdr;
595 virtio_net_ctrl_ack status = ~0;
596 uint16_t i, idx_data, idx_status;
597 uint32_t n_descs = 0;
599 /* locate desc for header, data, and status */
600 idx_data = vring->desc[idx_hdr].next;
604 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
605 i = vring->desc[i].next;
609 /* locate desc for status */
613 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
614 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
615 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
618 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
619 status = virtio_user_handle_mq(dev, queues);
623 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
629 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
631 uint16_t flags = desc->flags;
633 return wrap_counter == !!(flags & VRING_PACKED_DESC_F_AVAIL) &&
634 wrap_counter != !!(flags & VRING_PACKED_DESC_F_USED);
638 virtio_user_handle_ctrl_msg_packed(struct virtio_user_dev *dev,
639 struct vring_packed *vring,
642 struct virtio_net_ctrl_hdr *hdr;
643 virtio_net_ctrl_ack status = ~0;
644 uint16_t idx_data, idx_status;
645 /* initialize to one, header is first */
646 uint32_t n_descs = 1;
648 /* locate desc for header, data, and status */
649 idx_data = idx_hdr + 1;
650 if (idx_data >= dev->queue_size)
651 idx_data -= dev->queue_size;
655 idx_status = idx_data;
656 while (vring->desc[idx_status].flags & VRING_DESC_F_NEXT) {
658 if (idx_status >= dev->queue_size)
659 idx_status -= dev->queue_size;
663 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
664 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
665 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
668 queues = *(uint16_t *)(uintptr_t)
669 vring->desc[idx_data].addr;
670 status = virtio_user_handle_mq(dev, queues);
674 *(virtio_net_ctrl_ack *)(uintptr_t)
675 vring->desc[idx_status].addr = status;
677 /* Update used descriptor */
678 vring->desc[idx_hdr].id = vring->desc[idx_status].id;
679 vring->desc[idx_hdr].len = sizeof(status);
685 virtio_user_handle_cq_packed(struct virtio_user_dev *dev, uint16_t queue_idx)
687 struct virtio_user_queue *vq = &dev->packed_queues[queue_idx];
688 struct vring_packed *vring = &dev->packed_vrings[queue_idx];
689 uint16_t n_descs, flags;
691 while (desc_is_avail(&vring->desc[vq->used_idx],
692 vq->used_wrap_counter)) {
694 n_descs = virtio_user_handle_ctrl_msg_packed(dev, vring,
697 flags = VRING_DESC_F_WRITE;
698 if (vq->used_wrap_counter)
699 flags |= VRING_PACKED_DESC_F_AVAIL_USED;
702 vring->desc[vq->used_idx].flags = flags;
704 vq->used_idx += n_descs;
705 if (vq->used_idx >= dev->queue_size) {
706 vq->used_idx -= dev->queue_size;
707 vq->used_wrap_counter ^= 1;
713 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
715 uint16_t avail_idx, desc_idx;
716 struct vring_used_elem *uep;
718 struct vring *vring = &dev->vrings[queue_idx];
720 /* Consume avail ring, using used ring idx as first one */
721 while (vring->used->idx != vring->avail->idx) {
722 avail_idx = (vring->used->idx) & (vring->num - 1);
723 desc_idx = vring->avail->ring[avail_idx];
725 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
727 /* Update used ring */
728 uep = &vring->used->ring[avail_idx];