1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
5 #include <sys/socket.h>
14 #include <rte_alarm.h>
15 #include <rte_string_fns.h>
16 #include <rte_fbarray.h>
19 #include "virtio_user_dev.h"
21 struct vhost_user_data {
24 uint64_t protocol_features;
27 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
28 #define VHOST_USER_F_PROTOCOL_FEATURES 30
31 /** Protocol features. */
32 #ifndef VHOST_USER_PROTOCOL_F_MQ
33 #define VHOST_USER_PROTOCOL_F_MQ 0
36 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
37 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
40 #ifndef VHOST_USER_PROTOCOL_F_STATUS
41 #define VHOST_USER_PROTOCOL_F_STATUS 16
44 #define VHOST_USER_SUPPORTED_PROTOCOL_FEATURES \
45 (1ULL << VHOST_USER_PROTOCOL_F_MQ | \
46 1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK | \
47 1ULL << VHOST_USER_PROTOCOL_F_STATUS)
49 /* The version of the protocol we support */
50 #define VHOST_USER_VERSION 0x1
52 #define VHOST_MEMORY_MAX_NREGIONS 8
56 struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
59 enum vhost_user_request {
61 VHOST_USER_GET_FEATURES = 1,
62 VHOST_USER_SET_FEATURES = 2,
63 VHOST_USER_SET_OWNER = 3,
64 VHOST_USER_RESET_OWNER = 4,
65 VHOST_USER_SET_MEM_TABLE = 5,
66 VHOST_USER_SET_LOG_BASE = 6,
67 VHOST_USER_SET_LOG_FD = 7,
68 VHOST_USER_SET_VRING_NUM = 8,
69 VHOST_USER_SET_VRING_ADDR = 9,
70 VHOST_USER_SET_VRING_BASE = 10,
71 VHOST_USER_GET_VRING_BASE = 11,
72 VHOST_USER_SET_VRING_KICK = 12,
73 VHOST_USER_SET_VRING_CALL = 13,
74 VHOST_USER_SET_VRING_ERR = 14,
75 VHOST_USER_GET_PROTOCOL_FEATURES = 15,
76 VHOST_USER_SET_PROTOCOL_FEATURES = 16,
77 VHOST_USER_GET_QUEUE_NUM = 17,
78 VHOST_USER_SET_VRING_ENABLE = 18,
79 VHOST_USER_SET_STATUS = 39,
80 VHOST_USER_GET_STATUS = 40,
84 struct vhost_user_msg {
85 enum vhost_user_request request;
87 #define VHOST_USER_VERSION_MASK 0x3
88 #define VHOST_USER_REPLY_MASK (0x1 << 2)
89 #define VHOST_USER_NEED_REPLY_MASK (0x1 << 3)
91 uint32_t size; /* the following payload size */
93 #define VHOST_USER_VRING_IDX_MASK 0xff
94 #define VHOST_USER_VRING_NOFD_MASK (0x1 << 8)
96 struct vhost_vring_state state;
97 struct vhost_vring_addr addr;
98 struct vhost_memory memory;
102 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
103 #define VHOST_USER_PAYLOAD_SIZE \
104 (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
107 vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
112 size_t fd_size = fd_num * sizeof(int);
113 char control[CMSG_SPACE(fd_size)];
114 struct cmsghdr *cmsg;
116 memset(&msgh, 0, sizeof(msgh));
117 memset(control, 0, sizeof(control));
119 iov.iov_base = (uint8_t *)msg;
120 iov.iov_len = VHOST_USER_HDR_SIZE + msg->size;
124 msgh.msg_control = control;
125 msgh.msg_controllen = sizeof(control);
127 cmsg = CMSG_FIRSTHDR(&msgh);
128 cmsg->cmsg_len = CMSG_LEN(fd_size);
129 cmsg->cmsg_level = SOL_SOCKET;
130 cmsg->cmsg_type = SCM_RIGHTS;
131 memcpy(CMSG_DATA(cmsg), fds, fd_size);
134 r = sendmsg(fd, &msgh, 0);
135 } while (r < 0 && errno == EINTR);
138 PMD_DRV_LOG(ERR, "Failed to send msg: %s", strerror(errno));
144 vhost_user_read(int fd, struct vhost_user_msg *msg)
146 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
147 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
149 ret = recv(fd, (void *)msg, sz_hdr, 0);
151 PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", strerror(errno));
153 } else if (ret < sz_hdr) {
154 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
159 /* validate msg flags */
160 if (msg->flags != (valid_flags)) {
161 PMD_DRV_LOG(ERR, "Failed to recv msg: flags 0x%x instead of 0x%x.",
162 msg->flags, valid_flags);
166 sz_payload = msg->size;
168 if ((size_t)sz_payload > sizeof(msg->payload)) {
169 PMD_DRV_LOG(ERR, "Payload size overflow, header says %d but max %zu",
170 sz_payload, sizeof(msg->payload));
175 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
177 PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", strerror(errno));
179 } else if (ret < sz_payload) {
180 PMD_DRV_LOG(ERR, "Failed to recv msg payload: %d instead of %u.",
190 vhost_user_check_reply_ack(struct virtio_user_dev *dev, struct vhost_user_msg *msg)
192 struct vhost_user_data *data = dev->backend_data;
193 enum vhost_user_request req = msg->request;
196 if (!(msg->flags & VHOST_USER_NEED_REPLY_MASK))
199 ret = vhost_user_read(data->vhostfd, msg);
201 PMD_DRV_LOG(ERR, "Failed to read reply-ack");
205 if (req != msg->request) {
206 PMD_DRV_LOG(ERR, "Unexpected reply-ack request type (%d)", msg->request);
210 if (msg->size != sizeof(msg->payload.u64)) {
211 PMD_DRV_LOG(ERR, "Unexpected reply-ack payload size (%u)", msg->size);
215 if (msg->payload.u64) {
216 PMD_DRV_LOG(ERR, "Slave replied NACK to request type (%d)", msg->request);
224 vhost_user_set_owner(struct virtio_user_dev *dev)
227 struct vhost_user_data *data = dev->backend_data;
228 struct vhost_user_msg msg = {
229 .request = VHOST_USER_SET_OWNER,
230 .flags = VHOST_USER_VERSION,
233 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
235 PMD_DRV_LOG(ERR, "Failed to set owner");
243 vhost_user_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
246 struct vhost_user_data *data = dev->backend_data;
247 struct vhost_user_msg msg = {
248 .request = VHOST_USER_GET_PROTOCOL_FEATURES,
249 .flags = VHOST_USER_VERSION,
252 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
256 ret = vhost_user_read(data->vhostfd, &msg);
260 if (msg.request != VHOST_USER_GET_PROTOCOL_FEATURES) {
261 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
265 if (msg.size != sizeof(*features)) {
266 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
270 *features = msg.payload.u64;
274 PMD_DRV_LOG(ERR, "Failed to get backend protocol features");
280 vhost_user_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
283 struct vhost_user_data *data = dev->backend_data;
284 struct vhost_user_msg msg = {
285 .request = VHOST_USER_SET_PROTOCOL_FEATURES,
286 .flags = VHOST_USER_VERSION,
287 .size = sizeof(features),
288 .payload.u64 = features,
291 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
293 PMD_DRV_LOG(ERR, "Failed to set protocol features");
301 vhost_user_get_features(struct virtio_user_dev *dev, uint64_t *features)
304 struct vhost_user_data *data = dev->backend_data;
305 struct vhost_user_msg msg = {
306 .request = VHOST_USER_GET_FEATURES,
307 .flags = VHOST_USER_VERSION,
310 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
314 ret = vhost_user_read(data->vhostfd, &msg);
318 if (msg.request != VHOST_USER_GET_FEATURES) {
319 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
323 if (msg.size != sizeof(*features)) {
324 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
328 *features = msg.payload.u64;
330 if (!(*features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
333 /* Negotiate protocol features */
334 ret = vhost_user_get_protocol_features(dev, &data->protocol_features);
338 data->protocol_features &= VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
340 ret = vhost_user_set_protocol_features(dev, data->protocol_features);
344 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
345 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
349 PMD_DRV_LOG(ERR, "Failed to get backend features");
355 vhost_user_set_features(struct virtio_user_dev *dev, uint64_t features)
358 struct vhost_user_data *data = dev->backend_data;
359 struct vhost_user_msg msg = {
360 .request = VHOST_USER_SET_FEATURES,
361 .flags = VHOST_USER_VERSION,
362 .size = sizeof(features),
363 .payload.u64 = features,
366 msg.payload.u64 |= dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
368 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
370 PMD_DRV_LOG(ERR, "Failed to set features");
378 struct vhost_memory *vm;
384 update_memory_region(const struct rte_memseg_list *msl __rte_unused,
385 const struct rte_memseg *ms, void *arg)
387 struct walk_arg *wa = arg;
388 struct vhost_memory_region *mr;
389 uint64_t start_addr, end_addr;
393 fd = rte_memseg_get_fd_thread_unsafe(ms);
395 PMD_DRV_LOG(ERR, "Failed to get fd, ms=%p rte_errno=%d",
400 if (rte_memseg_get_fd_offset_thread_unsafe(ms, &offset) < 0) {
401 PMD_DRV_LOG(ERR, "Failed to get offset, ms=%p rte_errno=%d",
406 start_addr = (uint64_t)(uintptr_t)ms->addr;
407 end_addr = start_addr + ms->len;
409 for (i = 0; i < wa->region_nr; i++) {
410 if (wa->fds[i] != fd)
413 mr = &wa->vm->regions[i];
415 if (mr->userspace_addr + mr->memory_size < end_addr)
416 mr->memory_size = end_addr - mr->userspace_addr;
418 if (mr->userspace_addr > start_addr) {
419 mr->userspace_addr = start_addr;
420 mr->guest_phys_addr = start_addr;
423 if (mr->mmap_offset > offset)
424 mr->mmap_offset = offset;
426 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
427 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
428 mr->mmap_offset, mr->userspace_addr,
434 if (i >= VHOST_MEMORY_MAX_NREGIONS) {
435 PMD_DRV_LOG(ERR, "Too many memory regions");
439 mr = &wa->vm->regions[i];
442 mr->guest_phys_addr = start_addr;
443 mr->userspace_addr = start_addr;
444 mr->memory_size = ms->len;
445 mr->mmap_offset = offset;
447 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
448 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
449 mr->mmap_offset, mr->userspace_addr,
458 vhost_user_set_memory_table(struct virtio_user_dev *dev)
461 int fds[VHOST_MEMORY_MAX_NREGIONS];
463 struct vhost_user_data *data = dev->backend_data;
464 struct vhost_user_msg msg = {
465 .request = VHOST_USER_SET_MEM_TABLE,
466 .flags = VHOST_USER_VERSION,
469 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
470 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
473 wa.vm = &msg.payload.memory;
477 * The memory lock has already been taken by memory subsystem
478 * or virtio_user_start_device().
480 ret = rte_memseg_walk_thread_unsafe(update_memory_region, &wa);
484 fd_num = wa.region_nr;
485 msg.payload.memory.nregions = wa.region_nr;
486 msg.payload.memory.padding = 0;
488 msg.size = sizeof(msg.payload.memory.nregions);
489 msg.size += sizeof(msg.payload.memory.padding);
490 msg.size += fd_num * sizeof(struct vhost_memory_region);
492 ret = vhost_user_write(data->vhostfd, &msg, fds, fd_num);
496 return vhost_user_check_reply_ack(dev, &msg);
498 PMD_DRV_LOG(ERR, "Failed to set memory table");
503 vhost_user_set_vring(struct virtio_user_dev *dev, enum vhost_user_request req,
504 struct vhost_vring_state *state)
507 struct vhost_user_data *data = dev->backend_data;
508 struct vhost_user_msg msg = {
510 .flags = VHOST_USER_VERSION,
511 .size = sizeof(*state),
512 .payload.state = *state,
515 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
517 PMD_DRV_LOG(ERR, "Failed to set vring state (request %d)", req);
525 vhost_user_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
527 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, state);
531 vhost_user_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
533 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_NUM, state);
537 vhost_user_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
539 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_BASE, state);
543 vhost_user_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
546 struct vhost_user_msg msg;
547 struct vhost_user_data *data = dev->backend_data;
548 unsigned int index = state->index;
550 ret = vhost_user_set_vring(dev, VHOST_USER_GET_VRING_BASE, state);
552 PMD_DRV_LOG(ERR, "Failed to send request");
556 ret = vhost_user_read(data->vhostfd, &msg);
558 PMD_DRV_LOG(ERR, "Failed to read reply");
562 if (msg.request != VHOST_USER_GET_VRING_BASE) {
563 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
567 if (msg.size != sizeof(*state)) {
568 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
572 if (msg.payload.state.index != index) {
573 PMD_DRV_LOG(ERR, "Unexpected ring index (%u)", state->index);
577 *state = msg.payload.state;
581 PMD_DRV_LOG(ERR, "Failed to get vring base");
586 vhost_user_set_vring_file(struct virtio_user_dev *dev, enum vhost_user_request req,
587 struct vhost_vring_file *file)
592 struct vhost_user_data *data = dev->backend_data;
593 struct vhost_user_msg msg = {
595 .flags = VHOST_USER_VERSION,
596 .size = sizeof(msg.payload.u64),
597 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
603 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
605 ret = vhost_user_write(data->vhostfd, &msg, &fd, num_fd);
607 PMD_DRV_LOG(ERR, "Failed to set vring file (request %d)", req);
615 vhost_user_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
617 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
621 vhost_user_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
623 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
628 vhost_user_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
631 struct vhost_user_data *data = dev->backend_data;
632 struct vhost_user_msg msg = {
633 .request = VHOST_USER_SET_VRING_ADDR,
634 .flags = VHOST_USER_VERSION,
635 .size = sizeof(*addr),
636 .payload.addr = *addr,
639 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
641 PMD_DRV_LOG(ERR, "Failed to send vring addresses");
649 vhost_user_get_status(struct virtio_user_dev *dev, uint8_t *status)
652 struct vhost_user_data *data = dev->backend_data;
653 struct vhost_user_msg msg = {
654 .request = VHOST_USER_GET_STATUS,
655 .flags = VHOST_USER_VERSION,
659 * If features have not been negotiated, we don't know if the backend
660 * supports protocol features
662 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
665 /* Status protocol feature requires protocol features support */
666 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
669 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
672 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
674 PMD_DRV_LOG(ERR, "Failed to send request");
678 ret = vhost_user_read(data->vhostfd, &msg);
680 PMD_DRV_LOG(ERR, "Failed to recv request");
684 if (msg.request != VHOST_USER_GET_STATUS) {
685 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
689 if (msg.size != sizeof(msg.payload.u64)) {
690 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
694 *status = (uint8_t)msg.payload.u64;
698 PMD_DRV_LOG(ERR, "Failed to get device status");
703 vhost_user_set_status(struct virtio_user_dev *dev, uint8_t status)
706 struct vhost_user_data *data = dev->backend_data;
707 struct vhost_user_msg msg = {
708 .request = VHOST_USER_SET_STATUS,
709 .flags = VHOST_USER_VERSION,
710 .size = sizeof(msg.payload.u64),
711 .payload.u64 = status,
715 * If features have not been negotiated, we don't know if the backend
716 * supports protocol features
718 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
721 /* Status protocol feature requires protocol features support */
722 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
725 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
728 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
729 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
731 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
733 PMD_DRV_LOG(ERR, "Failed to send get status request");
737 return vhost_user_check_reply_ack(dev, &msg);
740 #define MAX_VIRTIO_USER_BACKLOG 1
742 vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
746 struct vhost_user_data *data = dev->backend_data;
747 int fd = data->listenfd;
749 ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
751 PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again",
752 dev->path, strerror(errno));
755 ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
759 PMD_DRV_LOG(NOTICE, "(%s) waiting for client connection...", dev->path);
760 data->vhostfd = accept(fd, NULL, NULL);
761 if (data->vhostfd < 0) {
762 PMD_DRV_LOG(ERR, "Failed to accept initial client connection (%s)",
767 flag = fcntl(fd, F_GETFL);
768 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
769 PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
777 vhost_user_server_disconnect(struct virtio_user_dev *dev)
779 struct vhost_user_data *data = dev->backend_data;
781 if (data->vhostfd < 0) {
782 PMD_DRV_LOG(ERR, "(%s) Expected valid Vhost FD", dev->path);
786 close(data->vhostfd);
793 vhost_user_server_reconnect(struct virtio_user_dev *dev)
795 struct vhost_user_data *data = dev->backend_data;
798 fd = accept(data->listenfd, NULL, NULL);
808 * Set up environment to talk with a vhost user backend.
815 vhost_user_setup(struct virtio_user_dev *dev)
819 struct sockaddr_un un;
820 struct vhost_user_data *data;
822 data = malloc(sizeof(*data));
824 PMD_DRV_LOG(ERR, "(%s) Failed to allocate Vhost-user data", dev->path);
828 memset(data, 0, sizeof(*data));
830 dev->backend_data = data;
835 fd = socket(AF_UNIX, SOCK_STREAM, 0);
837 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
841 flag = fcntl(fd, F_GETFD);
843 PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno));
844 else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
845 PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno));
847 memset(&un, 0, sizeof(un));
848 un.sun_family = AF_UNIX;
849 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
851 if (dev->is_server) {
853 if (vhost_user_start_server(dev, &un) < 0) {
854 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
858 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
859 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
871 dev->backend_data = NULL;
877 vhost_user_destroy(struct virtio_user_dev *dev)
879 struct vhost_user_data *data = dev->backend_data;
884 if (data->vhostfd >= 0) {
885 close(data->vhostfd);
889 if (data->listenfd >= 0) {
890 close(data->listenfd);
895 dev->backend_data = NULL;
901 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
905 struct vhost_user_data *data = dev->backend_data;
908 if (data->vhostfd < 0)
911 if (dev->qp_enabled[pair_idx] == enable)
914 for (i = 0; i < 2; ++i) {
915 struct vhost_vring_state state = {
916 .index = pair_idx * 2 + i,
920 if (vhost_user_set_vring_enable(dev, &state))
924 dev->qp_enabled[pair_idx] = enable;
929 vhost_user_get_backend_features(uint64_t *features)
931 *features = 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
937 vhost_user_update_link_state(struct virtio_user_dev *dev)
939 struct vhost_user_data *data = dev->backend_data;
942 if (data->vhostfd >= 0) {
946 flags = fcntl(data->vhostfd, F_GETFL);
947 if (fcntl(data->vhostfd, F_SETFL, flags | O_NONBLOCK) == -1) {
948 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
952 r = recv(data->vhostfd, buf, 128, MSG_PEEK);
953 if (r == 0 || (r < 0 && errno != EAGAIN)) {
954 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
955 PMD_DRV_LOG(ERR, "virtio-user port %u is down", dev->hw.port_id);
957 /* This function could be called in the process
958 * of interrupt handling, callback cannot be
959 * unregistered here, set an alarm to do it.
962 virtio_user_dev_delayed_disconnect_handler,
965 dev->net_status |= VIRTIO_NET_S_LINK_UP;
968 if (fcntl(data->vhostfd, F_SETFL,
969 flags & ~O_NONBLOCK) == -1) {
970 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
973 } else if (dev->is_server) {
974 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
975 if (virtio_user_dev_server_reconnect(dev) >= 0)
976 dev->net_status |= VIRTIO_NET_S_LINK_UP;
983 vhost_user_get_intr_fd(struct virtio_user_dev *dev)
985 struct vhost_user_data *data = dev->backend_data;
987 if (dev->is_server && data->vhostfd == -1)
988 return data->listenfd;
990 return data->vhostfd;
993 struct virtio_user_backend_ops virtio_ops_user = {
994 .setup = vhost_user_setup,
995 .destroy = vhost_user_destroy,
996 .get_backend_features = vhost_user_get_backend_features,
997 .set_owner = vhost_user_set_owner,
998 .get_features = vhost_user_get_features,
999 .set_features = vhost_user_set_features,
1000 .set_memory_table = vhost_user_set_memory_table,
1001 .set_vring_num = vhost_user_set_vring_num,
1002 .set_vring_base = vhost_user_set_vring_base,
1003 .get_vring_base = vhost_user_get_vring_base,
1004 .set_vring_call = vhost_user_set_vring_call,
1005 .set_vring_kick = vhost_user_set_vring_kick,
1006 .set_vring_addr = vhost_user_set_vring_addr,
1007 .get_status = vhost_user_get_status,
1008 .set_status = vhost_user_set_status,
1009 .enable_qp = vhost_user_enable_queue_pair,
1010 .update_link_state = vhost_user_update_link_state,
1011 .server_disconnect = vhost_user_server_disconnect,
1012 .server_reconnect = vhost_user_server_reconnect,
1013 .get_intr_fd = vhost_user_get_intr_fd,