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");
502 vhost_user_set_vring_file(struct virtio_user_dev *dev, enum vhost_user_request req,
503 struct vhost_vring_file *file)
508 struct vhost_user_msg msg = {
510 .flags = VHOST_USER_VERSION,
511 .size = sizeof(msg.payload.u64),
512 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
518 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
520 ret = vhost_user_write(dev->vhostfd, &msg, &fd, num_fd);
522 PMD_DRV_LOG(ERR, "Failed to set vring file (request %d)", req);
530 vhost_user_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
532 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
536 vhost_user_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
538 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
543 vhost_user_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
546 struct vhost_user_msg msg = {
547 .request = VHOST_USER_SET_VRING_ADDR,
548 .flags = VHOST_USER_VERSION,
549 .size = sizeof(*addr),
550 .payload.addr = *addr,
553 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
555 PMD_DRV_LOG(ERR, "Failed to send vring addresses");
563 vhost_user_get_status(struct virtio_user_dev *dev, uint8_t *status)
566 struct vhost_user_msg msg = {
567 .request = VHOST_USER_GET_STATUS,
568 .flags = VHOST_USER_VERSION,
572 * If features have not been negotiated, we don't know if the backend
573 * supports protocol features
575 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
578 /* Status protocol feature requires protocol features support */
579 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
582 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
585 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
587 PMD_DRV_LOG(ERR, "Failed to send request");
591 ret = vhost_user_read(dev->vhostfd, &msg);
593 PMD_DRV_LOG(ERR, "Failed to recv request");
597 if (msg.request != VHOST_USER_GET_STATUS) {
598 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
602 if (msg.size != sizeof(msg.payload.u64)) {
603 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
607 *status = (uint8_t)msg.payload.u64;
611 PMD_DRV_LOG(ERR, "Failed to get device status");
616 vhost_user_set_status(struct virtio_user_dev *dev, uint8_t status)
619 struct vhost_user_msg msg = {
620 .request = VHOST_USER_SET_STATUS,
621 .flags = VHOST_USER_VERSION,
622 .size = sizeof(msg.payload.u64),
623 .payload.u64 = status,
627 * If features have not been negotiated, we don't know if the backend
628 * supports protocol features
630 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
633 /* Status protocol feature requires protocol features support */
634 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
637 if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
640 if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
641 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
643 ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
645 PMD_DRV_LOG(ERR, "Failed to send get status request");
649 return vhost_user_check_reply_ack(dev, &msg);
652 static struct vhost_user_msg m;
654 const char * const vhost_msg_strings[] = {
655 [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
659 vhost_user_sock(struct virtio_user_dev *dev,
660 enum vhost_user_request req,
663 struct vhost_user_msg msg;
664 struct vhost_vring_file *file = 0;
666 int fds[VHOST_MEMORY_MAX_NREGIONS];
668 int vhostfd = dev->vhostfd;
672 PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
674 if (dev->is_server && vhostfd < 0)
678 msg.flags = VHOST_USER_VERSION;
682 case VHOST_USER_SET_LOG_BASE:
683 msg.payload.u64 = *((__u64 *)arg);
684 msg.size = sizeof(m.payload.u64);
687 case VHOST_USER_SET_FEATURES:
688 msg.payload.u64 = *((__u64 *)arg) | (dev->device_features &
689 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES));
690 msg.size = sizeof(m.payload.u64);
693 case VHOST_USER_RESET_OWNER:
696 case VHOST_USER_SET_LOG_FD:
697 fds[fd_num++] = *((int *)arg);
700 case VHOST_USER_SET_VRING_ERR:
702 msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
703 msg.size = sizeof(m.payload.u64);
705 fds[fd_num++] = file->fd;
707 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
711 PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
715 if (vhost_user_write(vhostfd, &msg, fds, fd_num) < 0) {
716 PMD_DRV_LOG(ERR, "%s failed: %s",
717 vhost_msg_strings[req], strerror(errno));
721 if (need_reply || msg.flags & VHOST_USER_NEED_REPLY_MASK) {
722 if (vhost_user_read(vhostfd, &msg) < 0) {
723 PMD_DRV_LOG(ERR, "Received msg failed: %s",
728 if (req != msg.request) {
729 PMD_DRV_LOG(ERR, "Received unexpected msg type");
735 /* Reply-ack handling */
736 if (msg.size != sizeof(m.payload.u64)) {
737 PMD_DRV_LOG(ERR, "Received bad msg size");
741 if (msg.payload.u64 != 0) {
742 PMD_DRV_LOG(ERR, "Slave replied NACK");
753 #define MAX_VIRTIO_USER_BACKLOG 1
755 virtio_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
759 int fd = dev->listenfd;
761 ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
763 PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again\n",
764 dev->path, strerror(errno));
767 ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
771 flag = fcntl(fd, F_GETFL);
772 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
773 PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
781 * Set up environment to talk with a vhost user backend.
788 vhost_user_setup(struct virtio_user_dev *dev)
792 struct sockaddr_un un;
794 fd = socket(AF_UNIX, SOCK_STREAM, 0);
796 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
800 flag = fcntl(fd, F_GETFD);
801 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
802 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
804 memset(&un, 0, sizeof(un));
805 un.sun_family = AF_UNIX;
806 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
808 if (dev->is_server) {
810 if (virtio_user_start_server(dev, &un) < 0) {
811 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
817 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
818 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
829 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
835 if (dev->qp_enabled[pair_idx] == enable)
838 for (i = 0; i < 2; ++i) {
839 struct vhost_vring_state state = {
840 .index = pair_idx * 2 + i,
844 if (vhost_user_set_vring_enable(dev, &state))
848 dev->qp_enabled[pair_idx] = enable;
852 struct virtio_user_backend_ops virtio_ops_user = {
853 .setup = vhost_user_setup,
854 .set_owner = vhost_user_set_owner,
855 .get_features = vhost_user_get_features,
856 .set_features = vhost_user_set_features,
857 .get_protocol_features = vhost_user_get_protocol_features,
858 .set_protocol_features = vhost_user_set_protocol_features,
859 .set_memory_table = vhost_user_set_memory_table,
860 .set_vring_num = vhost_user_set_vring_num,
861 .set_vring_base = vhost_user_set_vring_base,
862 .get_vring_base = vhost_user_get_vring_base,
863 .set_vring_call = vhost_user_set_vring_call,
864 .set_vring_kick = vhost_user_set_vring_kick,
865 .set_vring_addr = vhost_user_set_vring_addr,
866 .get_status = vhost_user_get_status,
867 .set_status = vhost_user_set_status,
868 .send_request = vhost_user_sock,
869 .enable_qp = vhost_user_enable_queue_pair