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;
100 int fds[VHOST_MEMORY_MAX_NREGIONS];
103 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
104 #define VHOST_USER_PAYLOAD_SIZE \
105 (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
108 vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
113 size_t fd_size = fd_num * sizeof(int);
114 char control[CMSG_SPACE(fd_size)];
115 struct cmsghdr *cmsg;
117 memset(&msgh, 0, sizeof(msgh));
118 memset(control, 0, sizeof(control));
120 iov.iov_base = (uint8_t *)msg;
121 iov.iov_len = VHOST_USER_HDR_SIZE + msg->size;
125 msgh.msg_control = control;
126 msgh.msg_controllen = sizeof(control);
128 cmsg = CMSG_FIRSTHDR(&msgh);
129 cmsg->cmsg_len = CMSG_LEN(fd_size);
130 cmsg->cmsg_level = SOL_SOCKET;
131 cmsg->cmsg_type = SCM_RIGHTS;
132 memcpy(CMSG_DATA(cmsg), fds, fd_size);
135 r = sendmsg(fd, &msgh, 0);
136 } while (r < 0 && errno == EINTR);
139 PMD_DRV_LOG(ERR, "Failed to send msg: %s", strerror(errno));
145 vhost_user_read(int fd, struct vhost_user_msg *msg)
147 uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
148 int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
150 ret = recv(fd, (void *)msg, sz_hdr, 0);
152 PMD_DRV_LOG(ERR, "Failed to recv msg header: %s", strerror(errno));
154 } else if (ret < sz_hdr) {
155 PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
160 /* validate msg flags */
161 if (msg->flags != (valid_flags)) {
162 PMD_DRV_LOG(ERR, "Failed to recv msg: flags 0x%x instead of 0x%x.",
163 msg->flags, valid_flags);
167 sz_payload = msg->size;
169 if ((size_t)sz_payload > sizeof(msg->payload)) {
170 PMD_DRV_LOG(ERR, "Payload size overflow, header says %d but max %zu",
171 sz_payload, sizeof(msg->payload));
176 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
178 PMD_DRV_LOG(ERR, "Failed to recv msg payload: %s", strerror(errno));
180 } else if (ret < sz_payload) {
181 PMD_DRV_LOG(ERR, "Failed to recv msg payload: %d instead of %u.",
191 vhost_user_check_reply_ack(struct virtio_user_dev *dev, struct vhost_user_msg *msg)
193 struct vhost_user_data *data = dev->backend_data;
194 enum vhost_user_request req = msg->request;
197 if (!(msg->flags & VHOST_USER_NEED_REPLY_MASK))
200 ret = vhost_user_read(data->vhostfd, msg);
202 PMD_DRV_LOG(ERR, "Failed to read reply-ack");
206 if (req != msg->request) {
207 PMD_DRV_LOG(ERR, "Unexpected reply-ack request type (%d)", msg->request);
211 if (msg->size != sizeof(msg->payload.u64)) {
212 PMD_DRV_LOG(ERR, "Unexpected reply-ack payload size (%u)", msg->size);
216 if (msg->payload.u64) {
217 PMD_DRV_LOG(ERR, "Slave replied NACK to request type (%d)", msg->request);
225 vhost_user_set_owner(struct virtio_user_dev *dev)
228 struct vhost_user_data *data = dev->backend_data;
229 struct vhost_user_msg msg = {
230 .request = VHOST_USER_SET_OWNER,
231 .flags = VHOST_USER_VERSION,
234 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
236 PMD_DRV_LOG(ERR, "Failed to set owner");
244 vhost_user_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
247 struct vhost_user_data *data = dev->backend_data;
248 struct vhost_user_msg msg = {
249 .request = VHOST_USER_GET_PROTOCOL_FEATURES,
250 .flags = VHOST_USER_VERSION,
253 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
257 ret = vhost_user_read(data->vhostfd, &msg);
261 if (msg.request != VHOST_USER_GET_PROTOCOL_FEATURES) {
262 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
266 if (msg.size != sizeof(*features)) {
267 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
271 *features = msg.payload.u64;
275 PMD_DRV_LOG(ERR, "Failed to get backend protocol features");
281 vhost_user_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
284 struct vhost_user_data *data = dev->backend_data;
285 struct vhost_user_msg msg = {
286 .request = VHOST_USER_SET_PROTOCOL_FEATURES,
287 .flags = VHOST_USER_VERSION,
288 .size = sizeof(features),
289 .payload.u64 = features,
292 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
294 PMD_DRV_LOG(ERR, "Failed to set protocol features");
302 vhost_user_get_features(struct virtio_user_dev *dev, uint64_t *features)
305 struct vhost_user_data *data = dev->backend_data;
306 struct vhost_user_msg msg = {
307 .request = VHOST_USER_GET_FEATURES,
308 .flags = VHOST_USER_VERSION,
311 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
315 ret = vhost_user_read(data->vhostfd, &msg);
319 if (msg.request != VHOST_USER_GET_FEATURES) {
320 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
324 if (msg.size != sizeof(*features)) {
325 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
329 *features = msg.payload.u64;
331 if (!(*features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
334 /* Negotiate protocol features */
335 ret = vhost_user_get_protocol_features(dev, &data->protocol_features);
339 data->protocol_features &= VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
341 ret = vhost_user_set_protocol_features(dev, data->protocol_features);
345 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
346 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
350 PMD_DRV_LOG(ERR, "Failed to get backend features");
356 vhost_user_set_features(struct virtio_user_dev *dev, uint64_t features)
359 struct vhost_user_data *data = dev->backend_data;
360 struct vhost_user_msg msg = {
361 .request = VHOST_USER_SET_FEATURES,
362 .flags = VHOST_USER_VERSION,
363 .size = sizeof(features),
364 .payload.u64 = features,
367 msg.payload.u64 |= dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
369 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
371 PMD_DRV_LOG(ERR, "Failed to set features");
379 struct vhost_memory *vm;
385 update_memory_region(const struct rte_memseg_list *msl __rte_unused,
386 const struct rte_memseg *ms, void *arg)
388 struct walk_arg *wa = arg;
389 struct vhost_memory_region *mr;
390 uint64_t start_addr, end_addr;
394 fd = rte_memseg_get_fd_thread_unsafe(ms);
396 PMD_DRV_LOG(ERR, "Failed to get fd, ms=%p rte_errno=%d",
401 if (rte_memseg_get_fd_offset_thread_unsafe(ms, &offset) < 0) {
402 PMD_DRV_LOG(ERR, "Failed to get offset, ms=%p rte_errno=%d",
407 start_addr = (uint64_t)(uintptr_t)ms->addr;
408 end_addr = start_addr + ms->len;
410 for (i = 0; i < wa->region_nr; i++) {
411 if (wa->fds[i] != fd)
414 mr = &wa->vm->regions[i];
416 if (mr->userspace_addr + mr->memory_size < end_addr)
417 mr->memory_size = end_addr - mr->userspace_addr;
419 if (mr->userspace_addr > start_addr) {
420 mr->userspace_addr = start_addr;
421 mr->guest_phys_addr = start_addr;
424 if (mr->mmap_offset > offset)
425 mr->mmap_offset = offset;
427 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
428 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
429 mr->mmap_offset, mr->userspace_addr,
435 if (i >= VHOST_MEMORY_MAX_NREGIONS) {
436 PMD_DRV_LOG(ERR, "Too many memory regions");
440 mr = &wa->vm->regions[i];
443 mr->guest_phys_addr = start_addr;
444 mr->userspace_addr = start_addr;
445 mr->memory_size = ms->len;
446 mr->mmap_offset = offset;
448 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
449 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
450 mr->mmap_offset, mr->userspace_addr,
459 vhost_user_set_memory_table(struct virtio_user_dev *dev)
462 int fds[VHOST_MEMORY_MAX_NREGIONS];
464 struct vhost_user_data *data = dev->backend_data;
465 struct vhost_user_msg msg = {
466 .request = VHOST_USER_SET_MEM_TABLE,
467 .flags = VHOST_USER_VERSION,
470 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
471 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
474 wa.vm = &msg.payload.memory;
478 * The memory lock has already been taken by memory subsystem
479 * or virtio_user_start_device().
481 ret = rte_memseg_walk_thread_unsafe(update_memory_region, &wa);
485 fd_num = wa.region_nr;
486 msg.payload.memory.nregions = wa.region_nr;
487 msg.payload.memory.padding = 0;
489 msg.size = sizeof(msg.payload.memory.nregions);
490 msg.size += sizeof(msg.payload.memory.padding);
491 msg.size += fd_num * sizeof(struct vhost_memory_region);
493 ret = vhost_user_write(data->vhostfd, &msg, fds, fd_num);
497 return vhost_user_check_reply_ack(dev, &msg);
499 PMD_DRV_LOG(ERR, "Failed to set memory table");
504 vhost_user_set_vring(struct virtio_user_dev *dev, enum vhost_user_request req,
505 struct vhost_vring_state *state)
508 struct vhost_user_data *data = dev->backend_data;
509 struct vhost_user_msg msg = {
511 .flags = VHOST_USER_VERSION,
512 .size = sizeof(*state),
513 .payload.state = *state,
516 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
518 PMD_DRV_LOG(ERR, "Failed to set vring state (request %d)", req);
526 vhost_user_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
528 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, state);
532 vhost_user_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
534 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_NUM, state);
538 vhost_user_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
540 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_BASE, state);
544 vhost_user_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
547 struct vhost_user_msg msg;
548 struct vhost_user_data *data = dev->backend_data;
549 unsigned int index = state->index;
551 ret = vhost_user_set_vring(dev, VHOST_USER_GET_VRING_BASE, state);
553 PMD_DRV_LOG(ERR, "Failed to send request");
557 ret = vhost_user_read(data->vhostfd, &msg);
559 PMD_DRV_LOG(ERR, "Failed to read reply");
563 if (msg.request != VHOST_USER_GET_VRING_BASE) {
564 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
568 if (msg.size != sizeof(*state)) {
569 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
573 if (msg.payload.state.index != index) {
574 PMD_DRV_LOG(ERR, "Unexpected ring index (%u)", state->index);
578 *state = msg.payload.state;
582 PMD_DRV_LOG(ERR, "Failed to get vring base");
587 vhost_user_set_vring_file(struct virtio_user_dev *dev, enum vhost_user_request req,
588 struct vhost_vring_file *file)
593 struct vhost_user_data *data = dev->backend_data;
594 struct vhost_user_msg msg = {
596 .flags = VHOST_USER_VERSION,
597 .size = sizeof(msg.payload.u64),
598 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
604 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
606 ret = vhost_user_write(data->vhostfd, &msg, &fd, num_fd);
608 PMD_DRV_LOG(ERR, "Failed to set vring file (request %d)", req);
616 vhost_user_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
618 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
622 vhost_user_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
624 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
629 vhost_user_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
632 struct vhost_user_data *data = dev->backend_data;
633 struct vhost_user_msg msg = {
634 .request = VHOST_USER_SET_VRING_ADDR,
635 .flags = VHOST_USER_VERSION,
636 .size = sizeof(*addr),
637 .payload.addr = *addr,
640 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
642 PMD_DRV_LOG(ERR, "Failed to send vring addresses");
650 vhost_user_get_status(struct virtio_user_dev *dev, uint8_t *status)
653 struct vhost_user_data *data = dev->backend_data;
654 struct vhost_user_msg msg = {
655 .request = VHOST_USER_GET_STATUS,
656 .flags = VHOST_USER_VERSION,
660 * If features have not been negotiated, we don't know if the backend
661 * supports protocol features
663 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
666 /* Status protocol feature requires protocol features support */
667 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
670 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
673 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
675 PMD_DRV_LOG(ERR, "Failed to send request");
679 ret = vhost_user_read(data->vhostfd, &msg);
681 PMD_DRV_LOG(ERR, "Failed to recv request");
685 if (msg.request != VHOST_USER_GET_STATUS) {
686 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
690 if (msg.size != sizeof(msg.payload.u64)) {
691 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
695 *status = (uint8_t)msg.payload.u64;
699 PMD_DRV_LOG(ERR, "Failed to get device status");
704 vhost_user_set_status(struct virtio_user_dev *dev, uint8_t status)
707 struct vhost_user_data *data = dev->backend_data;
708 struct vhost_user_msg msg = {
709 .request = VHOST_USER_SET_STATUS,
710 .flags = VHOST_USER_VERSION,
711 .size = sizeof(msg.payload.u64),
712 .payload.u64 = status,
716 * If features have not been negotiated, we don't know if the backend
717 * supports protocol features
719 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
722 /* Status protocol feature requires protocol features support */
723 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
726 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
729 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
730 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
732 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
734 PMD_DRV_LOG(ERR, "Failed to send get status request");
738 return vhost_user_check_reply_ack(dev, &msg);
741 #define MAX_VIRTIO_USER_BACKLOG 1
743 vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
747 struct vhost_user_data *data = dev->backend_data;
748 int fd = data->listenfd;
750 ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
752 PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again",
753 dev->path, strerror(errno));
756 ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
760 PMD_DRV_LOG(NOTICE, "(%s) waiting for client connection...", dev->path);
761 data->vhostfd = accept(fd, NULL, NULL);
762 if (data->vhostfd < 0) {
763 PMD_DRV_LOG(ERR, "Failed to accept initial client connection (%s)",
768 flag = fcntl(fd, F_GETFL);
769 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
770 PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
778 vhost_user_server_disconnect(struct virtio_user_dev *dev)
780 struct vhost_user_data *data = dev->backend_data;
782 if (data->vhostfd < 0) {
783 PMD_DRV_LOG(ERR, "(%s) Expected valid Vhost FD", dev->path);
787 close(data->vhostfd);
794 vhost_user_server_reconnect(struct virtio_user_dev *dev)
796 struct vhost_user_data *data = dev->backend_data;
799 fd = accept(data->listenfd, NULL, NULL);
809 * Set up environment to talk with a vhost user backend.
816 vhost_user_setup(struct virtio_user_dev *dev)
820 struct sockaddr_un un;
821 struct vhost_user_data *data;
823 data = malloc(sizeof(*data));
825 PMD_DRV_LOG(ERR, "(%s) Failed to allocate Vhost-user data", dev->path);
829 memset(data, 0, sizeof(*data));
831 dev->backend_data = data;
836 fd = socket(AF_UNIX, SOCK_STREAM, 0);
838 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
842 flag = fcntl(fd, F_GETFD);
844 PMD_DRV_LOG(WARNING, "fcntl get fd failed, %s", strerror(errno));
845 else if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
846 PMD_DRV_LOG(WARNING, "fcntl set fd failed, %s", strerror(errno));
848 memset(&un, 0, sizeof(un));
849 un.sun_family = AF_UNIX;
850 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
852 if (dev->is_server) {
854 if (vhost_user_start_server(dev, &un) < 0) {
855 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
859 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
860 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
872 dev->backend_data = NULL;
878 vhost_user_destroy(struct virtio_user_dev *dev)
880 struct vhost_user_data *data = dev->backend_data;
885 if (data->vhostfd >= 0) {
886 close(data->vhostfd);
890 if (data->listenfd >= 0) {
891 close(data->listenfd);
896 dev->backend_data = NULL;
902 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
906 struct vhost_user_data *data = dev->backend_data;
909 if (data->vhostfd < 0)
912 if (dev->qp_enabled[pair_idx] == enable)
915 for (i = 0; i < 2; ++i) {
916 struct vhost_vring_state state = {
917 .index = pair_idx * 2 + i,
921 if (vhost_user_set_vring_enable(dev, &state))
925 dev->qp_enabled[pair_idx] = enable;
930 vhost_user_get_backend_features(uint64_t *features)
932 *features = 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
938 vhost_user_update_link_state(struct virtio_user_dev *dev)
940 struct vhost_user_data *data = dev->backend_data;
943 if (data->vhostfd >= 0) {
947 flags = fcntl(data->vhostfd, F_GETFL);
948 if (fcntl(data->vhostfd, F_SETFL, flags | O_NONBLOCK) == -1) {
949 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
953 r = recv(data->vhostfd, buf, 128, MSG_PEEK);
954 if (r == 0 || (r < 0 && errno != EAGAIN)) {
955 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
956 PMD_DRV_LOG(ERR, "virtio-user port %u is down", dev->hw.port_id);
958 /* This function could be called in the process
959 * of interrupt handling, callback cannot be
960 * unregistered here, set an alarm to do it.
963 virtio_user_dev_delayed_disconnect_handler,
966 dev->net_status |= VIRTIO_NET_S_LINK_UP;
969 if (fcntl(data->vhostfd, F_SETFL,
970 flags & ~O_NONBLOCK) == -1) {
971 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
974 } else if (dev->is_server) {
975 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
976 if (virtio_user_dev_server_reconnect(dev) >= 0)
977 dev->net_status |= VIRTIO_NET_S_LINK_UP;
984 vhost_user_get_intr_fd(struct virtio_user_dev *dev)
986 struct vhost_user_data *data = dev->backend_data;
988 if (dev->is_server && data->vhostfd == -1)
989 return data->listenfd;
991 return data->vhostfd;
994 struct virtio_user_backend_ops virtio_ops_user = {
995 .setup = vhost_user_setup,
996 .destroy = vhost_user_destroy,
997 .get_backend_features = vhost_user_get_backend_features,
998 .set_owner = vhost_user_set_owner,
999 .get_features = vhost_user_get_features,
1000 .set_features = vhost_user_set_features,
1001 .set_memory_table = vhost_user_set_memory_table,
1002 .set_vring_num = vhost_user_set_vring_num,
1003 .set_vring_base = vhost_user_set_vring_base,
1004 .get_vring_base = vhost_user_get_vring_base,
1005 .set_vring_call = vhost_user_set_vring_call,
1006 .set_vring_kick = vhost_user_set_vring_kick,
1007 .set_vring_addr = vhost_user_set_vring_addr,
1008 .get_status = vhost_user_get_status,
1009 .set_status = vhost_user_set_status,
1010 .enable_qp = vhost_user_enable_queue_pair,
1011 .update_link_state = vhost_user_update_link_state,
1012 .server_disconnect = vhost_user_server_disconnect,
1013 .server_reconnect = vhost_user_server_reconnect,
1014 .get_intr_fd = vhost_user_get_intr_fd,