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_IN_ORDER | \
375 1ULL << VIRTIO_F_VERSION_1)
378 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
379 int cq, int queue_size, const char *mac, char **ifname,
380 int mrg_rxbuf, int in_order)
382 pthread_mutex_init(&dev->mutex, NULL);
383 snprintf(dev->path, PATH_MAX, "%s", path);
385 dev->max_queue_pairs = queues;
386 dev->queue_pairs = 1; /* mq disabled by default */
387 dev->queue_size = queue_size;
388 dev->mac_specified = 0;
389 dev->unsupported_features = 0;
393 dev->ifname = *ifname;
397 if (virtio_user_dev_setup(dev) < 0) {
398 PMD_INIT_LOG(ERR, "backend set up fails");
402 if (!dev->is_server) {
403 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER,
405 PMD_INIT_LOG(ERR, "set_owner fails: %s",
410 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
411 &dev->device_features) < 0) {
412 PMD_INIT_LOG(ERR, "get_features failed: %s",
417 /* We just pretend vhost-user can support all these features.
418 * Note that this could be problematic that if some feature is
419 * negotiated but not supported by the vhost-user which comes
422 dev->device_features = VIRTIO_USER_SUPPORTED_FEATURES;
426 dev->device_features &= ~(1ull << VIRTIO_NET_F_MRG_RXBUF);
427 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
431 dev->device_features &= ~(1ull << VIRTIO_F_IN_ORDER);
432 dev->unsupported_features |= (1ull << VIRTIO_F_IN_ORDER);
435 if (dev->mac_specified) {
436 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
438 dev->device_features &= ~(1ull << VIRTIO_NET_F_MAC);
439 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MAC);
443 /* device does not really need to know anything about CQ,
444 * so if necessary, we just claim to support CQ
446 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
448 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
449 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
450 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
451 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
452 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
453 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
454 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
455 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
456 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_RX);
457 dev->unsupported_features |= (1ull << VIRTIO_NET_F_CTRL_VLAN);
458 dev->unsupported_features |=
459 (1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
460 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
461 dev->unsupported_features |=
462 (1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
465 /* The backend will not report this feature, we add it explicitly */
466 if (is_vhost_user_by_type(dev->path))
467 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
469 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
470 dev->unsupported_features |= ~VIRTIO_USER_SUPPORTED_FEATURES;
472 if (rte_mem_event_callback_register(VIRTIO_USER_MEM_EVENT_CLB_NAME,
473 virtio_user_mem_event_cb, dev)) {
474 if (rte_errno != ENOTSUP) {
475 PMD_INIT_LOG(ERR, "Failed to register mem event"
485 virtio_user_dev_uninit(struct virtio_user_dev *dev)
489 virtio_user_stop_device(dev);
491 rte_mem_event_callback_unregister(VIRTIO_USER_MEM_EVENT_CLB_NAME, dev);
493 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
494 close(dev->callfds[i]);
495 close(dev->kickfds[i]);
500 if (dev->is_server && dev->listenfd >= 0) {
501 close(dev->listenfd);
506 for (i = 0; i < dev->max_queue_pairs; ++i)
507 close(dev->vhostfds[i]);
519 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
524 if (q_pairs > dev->max_queue_pairs) {
525 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
526 q_pairs, dev->max_queue_pairs);
530 /* Server mode can't enable queue pairs if vhostfd is invalid,
531 * always return 0 in this case.
533 if (dev->vhostfd >= 0) {
534 for (i = 0; i < q_pairs; ++i)
535 ret |= dev->ops->enable_qp(dev, i, 1);
536 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
537 ret |= dev->ops->enable_qp(dev, i, 0);
538 } else if (!dev->is_server) {
541 dev->queue_pairs = q_pairs;
547 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
550 struct virtio_net_ctrl_hdr *hdr;
551 virtio_net_ctrl_ack status = ~0;
552 uint16_t i, idx_data, idx_status;
553 uint32_t n_descs = 0;
555 /* locate desc for header, data, and status */
556 idx_data = vring->desc[idx_hdr].next;
560 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
561 i = vring->desc[i].next;
565 /* locate desc for status */
569 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
570 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
571 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
574 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
575 status = virtio_user_handle_mq(dev, queues);
579 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
585 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
587 uint16_t avail_idx, desc_idx;
588 struct vring_used_elem *uep;
590 struct vring *vring = &dev->vrings[queue_idx];
592 /* Consume avail ring, using used ring idx as first one */
593 while (vring->used->idx != vring->avail->idx) {
594 avail_idx = (vring->used->idx) & (vring->num - 1);
595 desc_idx = vring->avail->ring[avail_idx];
597 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
599 /* Update used ring */
600 uep = &vring->used->ring[avail_idx];