1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
12 #include <sys/eventfd.h>
13 #include <sys/types.h>
17 #include "virtio_user_dev.h"
18 #include "../virtio_ethdev.h"
20 #define VIRTIO_USER_MEM_EVENT_CLB_NAME "virtio_user_mem_event_clb"
23 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
25 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
26 * firstly because vhost depends on this msg to allocate virtqueue
29 struct vhost_vring_file file;
31 file.index = queue_sel;
32 file.fd = dev->callfds[queue_sel];
33 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
39 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
41 struct vhost_vring_file file;
42 struct vhost_vring_state state;
43 struct vring *vring = &dev->vrings[queue_sel];
44 struct vhost_vring_addr addr = {
46 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
47 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
48 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
50 .flags = 0, /* disable log */
53 state.index = queue_sel;
54 state.num = vring->num;
55 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
57 state.index = queue_sel;
58 state.num = 0; /* no reservation */
59 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
61 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
63 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
64 * lastly because vhost depends on this msg to judge if
67 file.index = queue_sel;
68 file.fd = dev->kickfds[queue_sel];
69 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
75 virtio_user_queue_setup(struct virtio_user_dev *dev,
76 int (*fn)(struct virtio_user_dev *, uint32_t))
78 uint32_t i, queue_sel;
80 for (i = 0; i < dev->max_queue_pairs; ++i) {
81 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
82 if (fn(dev, queue_sel) < 0) {
83 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
87 for (i = 0; i < dev->max_queue_pairs; ++i) {
88 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
89 if (fn(dev, queue_sel) < 0) {
90 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
99 is_vhost_user_by_type(const char *path)
103 if (stat(path, &sb) == -1)
106 return S_ISSOCK(sb.st_mode);
110 virtio_user_start_device(struct virtio_user_dev *dev)
115 pthread_mutex_lock(&dev->mutex);
117 if (is_vhost_user_by_type(dev->path) && dev->vhostfd < 0)
120 /* Do not check return as already done in init, or reset in stop */
121 dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL);
123 /* Step 0: tell vhost to create queues */
124 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
127 /* Step 1: set features */
128 features = dev->features;
129 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
130 features &= ~(1ull << VIRTIO_NET_F_MAC);
131 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
132 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
133 features &= ~(1ull << VIRTIO_NET_F_STATUS);
134 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
137 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
139 /* Step 2: share memory regions */
140 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
144 /* Step 3: kick queues */
145 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
148 /* Step 4: enable queues
149 * we enable the 1st queue pair by default.
151 dev->ops->enable_qp(dev, 0, 1);
154 pthread_mutex_unlock(&dev->mutex);
158 pthread_mutex_unlock(&dev->mutex);
159 /* TODO: free resource here or caller to check */
163 int virtio_user_stop_device(struct virtio_user_dev *dev)
167 pthread_mutex_lock(&dev->mutex);
168 for (i = 0; i < dev->max_queue_pairs; ++i)
169 dev->ops->enable_qp(dev, i, 0);
171 if (dev->ops->send_request(dev, VHOST_USER_RESET_OWNER, NULL) < 0) {
172 PMD_DRV_LOG(INFO, "Failed to reset the device\n");
173 pthread_mutex_unlock(&dev->mutex);
176 dev->started = false;
177 pthread_mutex_unlock(&dev->mutex);
183 parse_mac(struct virtio_user_dev *dev, const char *mac)
186 uint32_t tmp[ETHER_ADDR_LEN];
191 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
192 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
193 if (r == ETHER_ADDR_LEN) {
194 for (i = 0; i < ETHER_ADDR_LEN; ++i)
195 dev->mac_addr[i] = (uint8_t)tmp[i];
196 dev->mac_specified = 1;
198 /* ignore the wrong mac, use random mac */
199 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
204 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
210 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
211 if (i >= dev->max_queue_pairs * 2) {
212 dev->kickfds[i] = -1;
213 dev->callfds[i] = -1;
217 /* May use invalid flag, but some backend uses kickfd and
218 * callfd as criteria to judge if dev is alive. so finally we
221 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
223 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
226 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
228 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
231 dev->callfds[i] = callfd;
232 dev->kickfds[i] = kickfd;
235 if (i < VIRTIO_MAX_VIRTQUEUES) {
236 for (j = 0; j <= i; ++j) {
237 close(dev->callfds[j]);
238 close(dev->kickfds[j]);
248 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
251 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
253 if (!eth_dev->intr_handle) {
254 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
255 if (!eth_dev->intr_handle) {
256 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
259 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
262 for (i = 0; i < dev->max_queue_pairs; ++i)
263 eth_dev->intr_handle->efds[i] = dev->callfds[i];
264 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
265 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
266 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
267 /* For virtio vdev, no need to read counter for clean */
268 eth_dev->intr_handle->efd_counter_size = 0;
269 eth_dev->intr_handle->fd = -1;
270 if (dev->vhostfd >= 0)
271 eth_dev->intr_handle->fd = dev->vhostfd;
272 else if (dev->is_server)
273 eth_dev->intr_handle->fd = dev->listenfd;
279 virtio_user_mem_event_cb(enum rte_mem_event type __rte_unused,
280 const void *addr __rte_unused,
281 size_t len __rte_unused,
284 struct virtio_user_dev *dev = arg;
287 pthread_mutex_lock(&dev->mutex);
289 if (dev->started == false)
292 /* Step 1: pause the active queues */
293 for (i = 0; i < dev->queue_pairs; i++)
294 dev->ops->enable_qp(dev, i, 0);
296 /* Step 2: update memory regions */
297 dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
299 /* Step 3: resume the active queues */
300 for (i = 0; i < dev->queue_pairs; i++)
301 dev->ops->enable_qp(dev, i, 1);
304 pthread_mutex_unlock(&dev->mutex);
308 virtio_user_dev_setup(struct virtio_user_dev *dev)
313 dev->vhostfds = NULL;
316 if (dev->is_server) {
317 if (access(dev->path, F_OK) == 0 &&
318 !is_vhost_user_by_type(dev->path)) {
319 PMD_DRV_LOG(ERR, "Server mode doesn't support vhost-kernel!");
322 dev->ops = &ops_user;
324 if (is_vhost_user_by_type(dev->path)) {
325 dev->ops = &ops_user;
327 dev->ops = &ops_kernel;
329 dev->vhostfds = malloc(dev->max_queue_pairs *
331 dev->tapfds = malloc(dev->max_queue_pairs *
333 if (!dev->vhostfds || !dev->tapfds) {
334 PMD_INIT_LOG(ERR, "Failed to malloc");
338 for (q = 0; q < dev->max_queue_pairs; ++q) {
339 dev->vhostfds[q] = -1;
345 if (dev->ops->setup(dev) < 0)
348 if (virtio_user_dev_init_notify(dev) < 0)
351 if (virtio_user_fill_intr_handle(dev) < 0)
357 /* Use below macro to filter features from vhost backend */
358 #define VIRTIO_USER_SUPPORTED_FEATURES \
359 (1ULL << VIRTIO_NET_F_MAC | \
360 1ULL << VIRTIO_NET_F_STATUS | \
361 1ULL << VIRTIO_NET_F_MQ | \
362 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
363 1ULL << VIRTIO_NET_F_CTRL_VQ | \
364 1ULL << VIRTIO_NET_F_CTRL_RX | \
365 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
366 1ULL << VIRTIO_NET_F_CSUM | \
367 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
368 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
369 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
370 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
371 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
372 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
373 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
374 1ULL << VIRTIO_F_VERSION_1)
377 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
378 int cq, int queue_size, const char *mac, char **ifname)
380 pthread_mutex_init(&dev->mutex, NULL);
381 snprintf(dev->path, PATH_MAX, "%s", path);
383 dev->max_queue_pairs = queues;
384 dev->queue_pairs = 1; /* mq disabled by default */
385 dev->queue_size = queue_size;
386 dev->mac_specified = 0;
390 dev->ifname = *ifname;
394 if (virtio_user_dev_setup(dev) < 0) {
395 PMD_INIT_LOG(ERR, "backend set up fails");
399 if (!dev->is_server) {
400 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
402 PMD_INIT_LOG(ERR, "set_owner fails: %s",
407 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
408 &dev->device_features) < 0) {
409 PMD_INIT_LOG(ERR, "get_features failed: %s",
414 /* We just pretend vhost-user can support all these features.
415 * Note that this could be problematic that if some feature is
416 * negotiated but not supported by the vhost-user which comes
419 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
422 if (dev->mac_specified)
423 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
425 dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC);
428 /* device does not really need to know anything about CQ,
429 * so if necessary, we just claim to support CQ
431 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
433 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
434 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
435 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
436 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
437 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
438 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
439 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
442 /* The backend will not report this feature, we add it explicitly */
443 if (is_vhost_user_by_type(dev->path))
444 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
446 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
448 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
449 virtio_user_mem_event_cb, dev)) {
450 if (rte_errno != ENOTSUP) {
451 PMD_INIT_LOG(ERR, "Failed to register mem event"
461 virtio_user_dev_uninit(struct virtio_user_dev *dev)
465 virtio_user_stop_device(dev);
467 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
469 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
470 close(dev->callfds[i]);
471 close(dev->kickfds[i]);
476 if (dev->is_server && dev->listenfd >= 0) {
477 close(dev->listenfd);
482 for (i = 0; i < dev->max_queue_pairs; ++i)
483 close(dev->vhostfds[i]);
495 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
500 if (q_pairs > dev->max_queue_pairs) {
501 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
502 q_pairs, dev->max_queue_pairs);
506 /* Server mode can't enable queue pairs if vhostfd is invalid,
507 * always return 0 in this case.
509 if (dev->vhostfd >= 0) {
510 for (i = 0; i < q_pairs; ++i)
511 ret |= dev->ops->enable_qp(dev, i, 1);
512 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
513 ret |= dev->ops->enable_qp(dev, i, 0);
514 } else if (!dev->is_server) {
517 dev->queue_pairs = q_pairs;
523 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
526 struct virtio_net_ctrl_hdr *hdr;
527 virtio_net_ctrl_ack status = ~0;
528 uint16_t i, idx_data, idx_status;
529 uint32_t n_descs = 0;
531 /* locate desc for header, data, and status */
532 idx_data = vring->desc[idx_hdr].next;
536 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
537 i = vring->desc[i].next;
541 /* locate desc for status */
545 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
546 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
547 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
550 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
551 status = virtio_user_handle_mq(dev, queues);
555 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
561 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
563 uint16_t avail_idx, desc_idx;
564 struct vring_used_elem *uep;
566 struct vring *vring = &dev->vrings[queue_idx];
568 /* Consume avail ring, using used ring idx as first one */
569 while (vring->used->idx != vring->avail->idx) {
570 avail_idx = (vring->used->idx) & (vring->num - 1);
571 desc_idx = vring->avail->ring[avail_idx];
573 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
575 /* Update used ring */
576 uep = &vring->used->ring[avail_idx];