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_eal_memconfig.h>
19 #include "virtio_user_dev.h"
20 #include "../virtio_ethdev.h"
22 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
25 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
27 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
28 * firstly because vhost depends on this msg to allocate virtqueue
31 struct vhost_vring_file file;
33 file.index = queue_sel;
34 file.fd = dev->callfds[queue_sel];
35 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
41 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
43 struct vhost_vring_file file;
44 struct vhost_vring_state state;
45 struct vring *vring = &dev->vrings[queue_sel];
46 struct vhost_vring_addr addr = {
48 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
49 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
50 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
52 .flags = 0, /* disable log */
55 state.index = queue_sel;
56 state.num = vring->num;
57 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
59 state.index = queue_sel;
60 state.num = 0; /* no reservation */
61 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
63 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
65 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
66 * lastly because vhost depends on this msg to judge if
69 file.index = queue_sel;
70 file.fd = dev->kickfds[queue_sel];
71 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
77 virtio_user_queue_setup(struct virtio_user_dev *dev,
78 int (*fn)(struct virtio_user_dev *, uint32_t))
80 uint32_t i, queue_sel;
82 for (i = 0; i < dev->max_queue_pairs; ++i) {
83 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
84 if (fn(dev, queue_sel) < 0) {
85 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
89 for (i = 0; i < dev->max_queue_pairs; ++i) {
90 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
91 if (fn(dev, queue_sel) < 0) {
92 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
101 is_vhost_user_by_type(const char *path)
105 if (stat(path, &sb) == -1)
108 return S_ISSOCK(sb.st_mode);
112 virtio_user_start_device(struct virtio_user_dev *dev)
114 struct rte_mem_config *mcfg = rte_eal_get_configuration()->mem_config;
121 * We need to make sure that the locks will be
122 * taken in the correct order to avoid deadlocks.
124 * Before releasing this lock, this thread should
125 * not trigger any memory hotplug events.
127 * This is a temporary workaround, and should be
128 * replaced when we get proper supports from the
129 * memory subsystem in the future.
131 rte_rwlock_read_lock(&mcfg->memory_hotplug_lock);
132 pthread_mutex_lock(&dev->mutex);
134 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
137 /* Do not check return as already done in init, or reset in stop */
138 dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL);
140 /* Step 0: tell vhost to create queues */
141 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
144 /* Step 1: set features */
145 features = dev->features;
146 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
147 features &= ~(1ull << VIRTIO_NET_F_MAC);
148 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
149 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
150 features &= ~(1ull << VIRTIO_NET_F_STATUS);
151 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
154 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
156 /* Step 2: share memory regions */
157 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
161 /* Step 3: kick queues */
162 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
165 /* Step 4: enable queues
166 * we enable the 1st queue pair by default.
168 dev->ops->enable_qp(dev, 0, 1);
171 pthread_mutex_unlock(&dev->mutex);
172 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
176 pthread_mutex_unlock(&dev->mutex);
177 rte_rwlock_read_unlock(&mcfg->memory_hotplug_lock);
178 /* TODO: free resource here or caller to check */
182 int virtio_user_stop_device(struct virtio_user_dev *dev)
186 pthread_mutex_lock(&dev->mutex);
190 for (i = 0; i < dev->max_queue_pairs; ++i)
191 dev->ops->enable_qp(dev, i, 0);
193 if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) {
194 PMD_DRV_LOG(INFO, "Failed to reset the device\n");
195 pthread_mutex_unlock(&dev->mutex);
198 dev->started = false;
200 pthread_mutex_unlock(&dev->mutex);
206 parse_mac(struct virtio_user_dev *dev, const char *mac)
209 uint32_t tmp[ETHER_ADDR_LEN];
214 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
215 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
216 if (r == ETHER_ADDR_LEN) {
217 for (i = 0; i < ETHER_ADDR_LEN; ++i)
218 dev->mac_addr[i] = (uint8_t)tmp[i];
219 dev->mac_specified = 1;
221 /* ignore the wrong mac, use random mac */
222 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
227 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
233 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
234 if (i >= dev->max_queue_pairs * 2) {
235 dev->kickfds[i] = -1;
236 dev->callfds[i] = -1;
240 /* May use invalid flag, but some backend uses kickfd and
241 * callfd as criteria to judge if dev is alive. so finally we
244 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
246 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
249 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
251 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
254 dev->callfds[i] = callfd;
255 dev->kickfds[i] = kickfd;
258 if (i < VIRTIO_MAX_VIRTQUEUES) {
259 for (j = 0; j <= i; ++j) {
260 close(dev->callfds[j]);
261 close(dev->kickfds[j]);
271 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
274 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
276 if (!eth_dev->intr_handle) {
277 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
278 if (!eth_dev->intr_handle) {
279 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
282 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
285 for (i = 0; i < dev->max_queue_pairs; ++i)
286 eth_dev->intr_handle->efds[i] = dev->callfds[i];
287 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
288 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
289 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
290 /* For virtio vdev, no need to read counter for clean */
291 eth_dev->intr_handle->efd_counter_size = 0;
292 eth_dev->intr_handle->fd = -1;
293 if (dev->vhostfd >= 0)
294 eth_dev->intr_handle->fd = dev->vhostfd;
295 else if (dev->is_server)
296 eth_dev->intr_handle->fd = dev->listenfd;
302 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
303 const void *addr __rte_unused,
304 size_t len __rte_unused,
307 struct virtio_user_dev *dev = arg;
308 struct rte_memseg_list *msl;
311 /* ignore externally allocated memory */
312 msl = rte_mem_virt2memseg_list(addr);
316 pthread_mutex_lock(&dev->mutex);
318 if (dev->started == false)
321 /* Step 1: pause the active queues */
322 for (i = 0; i < dev->queue_pairs; i++)
323 dev->ops->enable_qp(dev, i, 0);
325 /* Step 2: update memory regions */
326 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
328 /* Step 3: resume the active queues */
329 for (i = 0; i < dev->queue_pairs; i++)
330 dev->ops->enable_qp(dev, i, 1);
333 pthread_mutex_unlock(&dev->mutex);
337 virtio_user_dev_setup(struct virtio_user_dev *dev)
342 dev->vhostfds = NULL;
345 if (dev->is_server) {
346 if (access(dev->path, F_OK) == 0 &&
347 !is_vhost_user_by_type(dev->path)) {
348 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
351 dev->ops = &virtio_ops_user;
353 if (is_vhost_user_by_type(dev->path)) {
354 dev->ops = &virtio_ops_user;
356 dev->ops = &virtio_ops_kernel;
358 dev->vhostfds = malloc(dev->max_queue_pairs *
360 dev->tapfds = malloc(dev->max_queue_pairs *
362 if (!dev->vhostfds || !dev->tapfds) {
363 PMD_INIT_LOG(ERR, "Failed to malloc");
367 for (q = 0; q < dev->max_queue_pairs; ++q) {
368 dev->vhostfds[q] = -1;
374 if (dev->ops->setup(dev) < 0)
377 if (virtio_user_dev_init_notify(dev) < 0)
380 if (virtio_user_fill_intr_handle(dev) < 0)
386 /* Use below macro to filter features from vhost backend */
387 #define VIRTIO_USER_SUPPORTED_FEATURES \
388 (1ULL << VIRTIO_NET_F_MAC | \
389 1ULL << VIRTIO_NET_F_STATUS | \
390 1ULL << VIRTIO_NET_F_MQ | \
391 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
392 1ULL << VIRTIO_NET_F_CTRL_VQ | \
393 1ULL << VIRTIO_NET_F_CTRL_RX | \
394 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
395 1ULL << VIRTIO_NET_F_CSUM | \
396 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
397 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
398 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
399 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
400 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
401 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
402 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
403 1ULL << VIRTIO_F_IN_ORDER | \
404 1ULL << VIRTIO_F_VERSION_1)
407 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
408 int cq, int queue_size, const char *mac, char **ifname,
409 int mrg_rxbuf, int in_order)
411 pthread_mutex_init(&dev->mutex, NULL);
412 snprintf(dev->path, PATH_MAX, "%s", path);
414 dev->max_queue_pairs = queues;
415 dev->queue_pairs = 1; /* mq disabled by default */
416 dev->queue_size = queue_size;
417 dev->mac_specified = 0;
418 dev->unsupported_features = 0;
422 dev->ifname = *ifname;
426 if (virtio_user_dev_setup(dev) < 0) {
427 PMD_INIT_LOG(ERR, "backend set up fails");
431 if (!dev->is_server) {
432 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
434 PMD_INIT_LOG(ERR, "set_owner fails: %s",
439 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
440 &dev->device_features) < 0) {
441 PMD_INIT_LOG(ERR, "get_features failed: %s",
446 /* We just pretend vhost-user can support all these features.
447 * Note that this could be problematic that if some feature is
448 * negotiated but not supported by the vhost-user which comes
451 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
455 dev->device_features &= ~(1ull << VIRTIO_NET_F_MRG_RXBUF);
456 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
460 dev->device_features &= ~(1ull << VIRTIO_F_IN_ORDER);
461 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
464 if (dev->mac_specified) {
465 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
467 dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC);
468 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
472 /* device does not really need to know anything about CQ,
473 * so if necessary, we just claim to support CQ
475 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
477 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
478 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
479 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
480 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
481 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
482 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
483 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
484 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
485 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
486 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
487 dev->unsupported_features |=
488 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
489 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
490 dev->unsupported_features |=
491 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
494 /* The backend will not report this feature, we add it explicitly */
495 if (is_vhost_user_by_type(dev->path))
496 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
498 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
499 dev->unsupported_features |= ~VIRTIO_USER_SUPPORTED_FEATURES;
501 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
502 virtio_user_mem_event_cb, dev)) {
503 if (rte_errno != ENOTSUP) {
504 PMD_INIT_LOG(ERR, "Failed to register mem event"
514 virtio_user_dev_uninit(struct virtio_user_dev *dev)
518 virtio_user_stop_device(dev);
520 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
522 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
523 close(dev->callfds[i]);
524 close(dev->kickfds[i]);
529 if (dev->is_server && dev->listenfd >= 0) {
530 close(dev->listenfd);
535 for (i = 0; i < dev->max_queue_pairs; ++i)
536 close(dev->vhostfds[i]);
548 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
553 if (q_pairs > dev->max_queue_pairs) {
554 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
555 q_pairs, dev->max_queue_pairs);
559 /* Server mode can't enable queue pairs if vhostfd is invalid,
560 * always return 0 in this case.
562 if (!dev->is_server || dev->vhostfd >= 0) {
563 for (i = 0; i < q_pairs; ++i)
564 ret |= dev->ops->enable_qp(dev, i, 1);
565 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
566 ret |= dev->ops->enable_qp(dev, i, 0);
568 dev->queue_pairs = q_pairs;
574 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
577 struct virtio_net_ctrl_hdr *hdr;
578 virtio_net_ctrl_ack status = ~0;
579 uint16_t i, idx_data, idx_status;
580 uint32_t n_descs = 0;
582 /* locate desc for header, data, and status */
583 idx_data = vring->desc[idx_hdr].next;
587 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
588 i = vring->desc[i].next;
592 /* locate desc for status */
596 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
597 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
598 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
601 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
602 status = virtio_user_handle_mq(dev, queues);
606 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
612 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
614 uint16_t avail_idx, desc_idx;
615 struct vring_used_elem *uep;
617 struct vring *vring = &dev->vrings[queue_idx];
619 /* Consume avail ring, using used ring idx as first one */
620 while (vring->used->idx != vring->avail->idx) {
621 avail_idx = (vring->used->idx) & (vring->num - 1);
622 desc_idx = vring->avail->ring[avail_idx];
624 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
626 /* Update used ring */
627 uep = &vring->used->ring[avail_idx];