1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
5 #include <sys/socket.h>
14 #include <rte_string_fns.h>
15 #include <rte_fbarray.h>
18 #include "virtio_user_dev.h"
20 /* The version of the protocol we support */
21 #define VHOST_USER_VERSION 0x1
23 #define VHOST_MEMORY_MAX_NREGIONS 8
27 struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
30 struct vhost_user_msg {
31 enum vhost_user_request request;
33 #define VHOST_USER_VERSION_MASK 0x3
34 #define VHOST_USER_REPLY_MASK (0x1 << 2)
35 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
37 uint32_t size; /* the following payload size */
39 #define VHOST_USER_VRING_IDX_MASK 0xff
40 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
42 struct vhost_vring_state state;
43 struct vhost_vring_addr addr;
44 struct vhost_memory memory;
46 int fds[VHOST_MEMORY_MAX_NREGIONS];
49 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
50 #define VHOST_USER_PAYLOAD_SIZE \
51 (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
54 vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
59 size_t fd_size = fd_num * sizeof(int);
60 char control[CMSG_SPACE(fd_size)];
63 memset(&msgh, 0, sizeof(msgh));
64 memset(control, 0, sizeof(control));
66 iov.iov_base = (uint8_t *)msg;
67 iov.iov_len = VHOST_USER_HDR_SIZE + msg->size;
71 msgh.msg_control = control;
72 msgh.msg_controllen = sizeof(control);
74 cmsg = CMSG_FIRSTHDR(&msgh);
75 cmsg->cmsg_len = CMSG_LEN(fd_size);
76 cmsg->cmsg_level = SOL_SOCKET;
77 cmsg->cmsg_type = SCM_RIGHTS;
78 memcpy(CMSG_DATA(cmsg), fds, fd_size);
81 r = sendmsg(fd, &msgh, 0);
82 } while (r < 0 && errno == EINTR);
85 PMD_DRV_LOG(ERR, "Failed to send msg: %s", strerror(errno));
91 vhost_user_read(int fd, struct vhost_user_msg *msg)
93 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
94 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
96 ret = recv(fd, (void *)msg, sz_hdr, 0);
98 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
103 /* validate msg flags */
104 if (msg->flags != (valid_flags)) {
105 PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
106 msg->flags, valid_flags);
110 sz_payload = msg->size;
112 if ((size_t)sz_payload > sizeof(msg->payload))
116 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
117 if (ret < sz_payload) {
119 "Failed to recv msg payload: %d instead of %d.",
132 vhost_user_check_reply_ack(struct virtio_user_dev *dev, struct vhost_user_msg *msg)
134 enum vhost_user_request req = msg->request;
137 if (!(msg->flags & VHOST_USER_NEED_REPLY_MASK))
140 ret = vhost_user_read(dev->vhostfd, msg);
142 PMD_DRV_LOG(ERR, "Failed to read reply-ack");
146 if (req != msg->request) {
147 PMD_DRV_LOG(ERR, "Unexpected reply-ack request type (%d)", msg->request);
151 if (msg->size != sizeof(msg->payload.u64)) {
152 PMD_DRV_LOG(ERR, "Unexpected reply-ack payload size (%u)", msg->size);
156 if (msg->payload.u64) {
157 PMD_DRV_LOG(ERR, "Slave replied NACK to request type (%d)", msg->request);
165 vhost_user_set_owner(struct virtio_user_dev *dev)
168 struct vhost_user_msg msg = {
169 .request = VHOST_USER_SET_OWNER,
170 .flags = VHOST_USER_VERSION,
173 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
175 PMD_DRV_LOG(ERR, "Failed to set owner");
183 vhost_user_get_features(struct virtio_user_dev *dev, uint64_t *features)
186 struct vhost_user_msg msg = {
187 .request = VHOST_USER_GET_FEATURES,
188 .flags = VHOST_USER_VERSION,
191 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
195 ret = vhost_user_read(dev->vhostfd, &msg);
199 if (msg.request != VHOST_USER_GET_FEATURES) {
200 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
204 if (msg.size != sizeof(*features)) {
205 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
209 *features = msg.payload.u64;
213 PMD_DRV_LOG(ERR, "Failed to get backend features");
219 vhost_user_set_features(struct virtio_user_dev *dev, uint64_t features)
222 struct vhost_user_msg msg = {
223 .request = VHOST_USER_SET_FEATURES,
224 .flags = VHOST_USER_VERSION,
225 .size = sizeof(features),
226 .payload.u64 = features,
229 msg.payload.u64 |= dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
231 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
233 PMD_DRV_LOG(ERR, "Failed to set features");
241 vhost_user_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
244 struct vhost_user_msg msg = {
245 .request = VHOST_USER_GET_PROTOCOL_FEATURES,
246 .flags = VHOST_USER_VERSION,
249 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
253 ret = vhost_user_read(dev->vhostfd, &msg);
257 if (msg.request != VHOST_USER_GET_PROTOCOL_FEATURES) {
258 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
262 if (msg.size != sizeof(*features)) {
263 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
267 *features = msg.payload.u64;
271 PMD_DRV_LOG(ERR, "Failed to get backend protocol features");
277 vhost_user_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
280 struct vhost_user_msg msg = {
281 .request = VHOST_USER_SET_PROTOCOL_FEATURES,
282 .flags = VHOST_USER_VERSION,
283 .size = sizeof(features),
284 .payload.u64 = features,
287 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
289 PMD_DRV_LOG(ERR, "Failed to set protocol features");
297 struct vhost_memory *vm;
303 update_memory_region(const struct rte_memseg_list *msl __rte_unused,
304 const struct rte_memseg *ms, void *arg)
306 struct walk_arg *wa = arg;
307 struct vhost_memory_region *mr;
308 uint64_t start_addr, end_addr;
312 fd = rte_memseg_get_fd_thread_unsafe(ms);
314 PMD_DRV_LOG(ERR, "Failed to get fd, ms=%p rte_errno=%d",
319 if (rte_memseg_get_fd_offset_thread_unsafe(ms, &offset) < 0) {
320 PMD_DRV_LOG(ERR, "Failed to get offset, ms=%p rte_errno=%d",
325 start_addr = (uint64_t)(uintptr_t)ms->addr;
326 end_addr = start_addr + ms->len;
328 for (i = 0; i < wa->region_nr; i++) {
329 if (wa->fds[i] != fd)
332 mr = &wa->vm->regions[i];
334 if (mr->userspace_addr + mr->memory_size < end_addr)
335 mr->memory_size = end_addr - mr->userspace_addr;
337 if (mr->userspace_addr > start_addr) {
338 mr->userspace_addr = start_addr;
339 mr->guest_phys_addr = start_addr;
342 if (mr->mmap_offset > offset)
343 mr->mmap_offset = offset;
345 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
346 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
347 mr->mmap_offset, mr->userspace_addr,
353 if (i >= VHOST_MEMORY_MAX_NREGIONS) {
354 PMD_DRV_LOG(ERR, "Too many memory regions");
358 mr = &wa->vm->regions[i];
361 mr->guest_phys_addr = start_addr;
362 mr->userspace_addr = start_addr;
363 mr->memory_size = ms->len;
364 mr->mmap_offset = offset;
366 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
367 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
368 mr->mmap_offset, mr->userspace_addr,
377 vhost_user_set_memory_table(struct virtio_user_dev *dev)
380 int fds[VHOST_MEMORY_MAX_NREGIONS];
382 struct vhost_user_msg msg = {
383 .request = VHOST_USER_SET_MEM_TABLE,
384 .flags = VHOST_USER_VERSION,
387 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
388 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
391 wa.vm = &msg.payload.memory;
395 * The memory lock has already been taken by memory subsystem
396 * or virtio_user_start_device().
398 ret = rte_memseg_walk_thread_unsafe(update_memory_region, &wa);
402 fd_num = wa.region_nr;
403 msg.payload.memory.nregions = wa.region_nr;
404 msg.payload.memory.padding = 0;
406 msg.size = sizeof(msg.payload.memory.nregions);
407 msg.size += sizeof(msg.payload.memory.padding);
408 msg.size += fd_num * sizeof(struct vhost_memory_region);
410 ret = vhost_user_write(dev->vhostfd, &msg, fds, fd_num);
414 return vhost_user_check_reply_ack(dev, &msg);
416 PMD_DRV_LOG(ERR, "Failed to set memory table");
421 vhost_user_set_vring(struct virtio_user_dev *dev, enum vhost_user_request req,
422 struct vhost_vring_state *state)
425 struct vhost_user_msg msg = {
427 .flags = VHOST_USER_VERSION,
428 .size = sizeof(*state),
429 .payload.state = *state,
432 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
434 PMD_DRV_LOG(ERR, "Failed to set vring state (request %d)", req);
442 vhost_user_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
444 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, state);
448 vhost_user_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
450 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_NUM, state);
454 vhost_user_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
456 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_BASE, state);
460 vhost_user_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
463 struct vhost_user_msg msg;
464 unsigned int index = state->index;
466 ret = vhost_user_set_vring(dev, VHOST_USER_GET_VRING_BASE, state);
468 PMD_DRV_LOG(ERR, "Failed to send request");
472 ret = vhost_user_read(dev->vhostfd, &msg);
474 PMD_DRV_LOG(ERR, "Failed to read reply");
478 if (msg.request != VHOST_USER_GET_VRING_BASE) {
479 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
483 if (msg.size != sizeof(*state)) {
484 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
488 if (msg.payload.state.index != index) {
489 PMD_DRV_LOG(ERR, "Unexpected ring index (%u)", state->index);
493 *state = msg.payload.state;
497 PMD_DRV_LOG(ERR, "Failed to get vring base");
501 static struct vhost_user_msg m;
503 const char * const vhost_msg_strings[] = {
504 [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
505 [VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
506 [VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
507 [VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
508 [VHOST_USER_SET_STATUS] = "VHOST_SET_STATUS",
509 [VHOST_USER_GET_STATUS] = "VHOST_GET_STATUS",
513 vhost_user_sock(struct virtio_user_dev *dev,
514 enum vhost_user_request req,
517 struct vhost_user_msg msg;
518 struct vhost_vring_file *file = 0;
520 int has_reply_ack = 0;
521 int fds[VHOST_MEMORY_MAX_NREGIONS];
523 int vhostfd = dev->vhostfd;
527 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
529 if (dev->is_server && vhostfd < 0)
532 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
536 msg.flags = VHOST_USER_VERSION;
540 case VHOST_USER_GET_STATUS:
541 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK) ||
542 (!(dev->protocol_features &
543 (1ULL << VHOST_USER_PROTOCOL_F_STATUS))))
548 case VHOST_USER_SET_STATUS:
549 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK) ||
550 (!(dev->protocol_features &
551 (1ULL << VHOST_USER_PROTOCOL_F_STATUS))))
555 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
557 case VHOST_USER_SET_LOG_BASE:
558 msg.payload.u64 = *((__u64 *)arg);
559 msg.size = sizeof(m.payload.u64);
562 case VHOST_USER_SET_FEATURES:
563 msg.payload.u64 = *((__u64 *)arg) | (dev->device_features &
564 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES));
565 msg.size = sizeof(m.payload.u64);
568 case VHOST_USER_RESET_OWNER:
571 case VHOST_USER_SET_LOG_FD:
572 fds[fd_num++] = *((int *)arg);
575 case VHOST_USER_SET_VRING_ADDR:
576 memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
577 msg.size = sizeof(m.payload.addr);
580 case VHOST_USER_SET_VRING_KICK:
581 case VHOST_USER_SET_VRING_CALL:
582 case VHOST_USER_SET_VRING_ERR:
584 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
585 msg.size = sizeof(m.payload.u64);
587 fds[fd_num++] = file->fd;
589 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
593 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
597 if (vhost_user_write(vhostfd, &msg, fds, fd_num) < 0) {
598 PMD_DRV_LOG(ERR, "%s failed: %s",
599 vhost_msg_strings[req], strerror(errno));
603 if (need_reply || msg.flags & VHOST_USER_NEED_REPLY_MASK) {
604 if (vhost_user_read(vhostfd, &msg) < 0) {
605 PMD_DRV_LOG(ERR, "Received msg failed: %s",
610 if (req != msg.request) {
611 PMD_DRV_LOG(ERR, "Received unexpected msg type");
616 case VHOST_USER_GET_STATUS:
617 if (msg.size != sizeof(m.payload.u64)) {
618 PMD_DRV_LOG(ERR, "Received bad msg size");
621 *((__u64 *)arg) = msg.payload.u64;
624 /* Reply-ack handling */
625 if (msg.size != sizeof(m.payload.u64)) {
626 PMD_DRV_LOG(ERR, "Received bad msg size");
630 if (msg.payload.u64 != 0) {
631 PMD_DRV_LOG(ERR, "Slave replied NACK");
642 #define MAX_VIRTIO_USER_BACKLOG 1
644 virtio_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
648 int fd = dev->listenfd;
650 ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
652 PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again\n",
653 dev->path, strerror(errno));
656 ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
660 flag = fcntl(fd, F_GETFL);
661 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
662 PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
670 * Set up environment to talk with a vhost user backend.
677 vhost_user_setup(struct virtio_user_dev *dev)
681 struct sockaddr_un un;
683 fd = socket(AF_UNIX, SOCK_STREAM, 0);
685 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
689 flag = fcntl(fd, F_GETFD);
690 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
691 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
693 memset(&un, 0, sizeof(un));
694 un.sun_family = AF_UNIX;
695 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
697 if (dev->is_server) {
699 if (virtio_user_start_server(dev, &un) < 0) {
700 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
706 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
707 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
718 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
724 if (dev->qp_enabled[pair_idx] == enable)
727 for (i = 0; i < 2; ++i) {
728 struct vhost_vring_state state = {
729 .index = pair_idx * 2 + i,
733 if (vhost_user_set_vring_enable(dev, &state))
737 dev->qp_enabled[pair_idx] = enable;
741 struct virtio_user_backend_ops virtio_ops_user = {
742 .setup = vhost_user_setup,
743 .set_owner = vhost_user_set_owner,
744 .get_features = vhost_user_get_features,
745 .set_features = vhost_user_set_features,
746 .get_protocol_features = vhost_user_get_protocol_features,
747 .set_protocol_features = vhost_user_set_protocol_features,
748 .set_memory_table = vhost_user_set_memory_table,
749 .set_vring_num = vhost_user_set_vring_num,
750 .set_vring_base = vhost_user_set_vring_base,
751 .get_vring_base = vhost_user_get_vring_base,
752 .send_request = vhost_user_sock,
753 .enable_qp = vhost_user_enable_queue_pair