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 hdr: %d instead of %d.",
157 /* validate msg flags */
158 if (msg->flags != (valid_flags)) {
159 PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
160 msg->flags, valid_flags);
164 sz_payload = msg->size;
166 if ((size_t)sz_payload > sizeof(msg->payload))
170 ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
171 if (ret < sz_payload) {
173 "Failed to recv msg payload: %d instead of %d.",
186 vhost_user_check_reply_ack(struct virtio_user_dev *dev, struct vhost_user_msg *msg)
188 struct vhost_user_data *data = dev->backend_data;
189 enum vhost_user_request req = msg->request;
192 if (!(msg->flags & VHOST_USER_NEED_REPLY_MASK))
195 ret = vhost_user_read(data->vhostfd, msg);
197 PMD_DRV_LOG(ERR, "Failed to read reply-ack");
201 if (req != msg->request) {
202 PMD_DRV_LOG(ERR, "Unexpected reply-ack request type (%d)", msg->request);
206 if (msg->size != sizeof(msg->payload.u64)) {
207 PMD_DRV_LOG(ERR, "Unexpected reply-ack payload size (%u)", msg->size);
211 if (msg->payload.u64) {
212 PMD_DRV_LOG(ERR, "Slave replied NACK to request type (%d)", msg->request);
220 vhost_user_set_owner(struct virtio_user_dev *dev)
223 struct vhost_user_data *data = dev->backend_data;
224 struct vhost_user_msg msg = {
225 .request = VHOST_USER_SET_OWNER,
226 .flags = VHOST_USER_VERSION,
229 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
231 PMD_DRV_LOG(ERR, "Failed to set owner");
239 vhost_user_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
242 struct vhost_user_data *data = dev->backend_data;
243 struct vhost_user_msg msg = {
244 .request = VHOST_USER_GET_PROTOCOL_FEATURES,
245 .flags = VHOST_USER_VERSION,
248 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
252 ret = vhost_user_read(data->vhostfd, &msg);
256 if (msg.request != VHOST_USER_GET_PROTOCOL_FEATURES) {
257 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
261 if (msg.size != sizeof(*features)) {
262 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
266 *features = msg.payload.u64;
270 PMD_DRV_LOG(ERR, "Failed to get backend protocol features");
276 vhost_user_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
279 struct vhost_user_data *data = dev->backend_data;
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(data->vhostfd, &msg, NULL, 0);
289 PMD_DRV_LOG(ERR, "Failed to set protocol features");
297 vhost_user_get_features(struct virtio_user_dev *dev, uint64_t *features)
300 struct vhost_user_data *data = dev->backend_data;
301 struct vhost_user_msg msg = {
302 .request = VHOST_USER_GET_FEATURES,
303 .flags = VHOST_USER_VERSION,
306 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
310 ret = vhost_user_read(data->vhostfd, &msg);
314 if (msg.request != VHOST_USER_GET_FEATURES) {
315 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
319 if (msg.size != sizeof(*features)) {
320 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
324 *features = msg.payload.u64;
326 if (!(*features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
329 /* Negotiate protocol features */
330 ret = vhost_user_get_protocol_features(dev, &data->protocol_features);
334 data->protocol_features &= VHOST_USER_SUPPORTED_PROTOCOL_FEATURES;
336 ret = vhost_user_set_protocol_features(dev, data->protocol_features);
340 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)))
341 dev->unsupported_features |= (1ull << VIRTIO_NET_F_MQ);
345 PMD_DRV_LOG(ERR, "Failed to get backend features");
351 vhost_user_set_features(struct virtio_user_dev *dev, uint64_t features)
354 struct vhost_user_data *data = dev->backend_data;
355 struct vhost_user_msg msg = {
356 .request = VHOST_USER_SET_FEATURES,
357 .flags = VHOST_USER_VERSION,
358 .size = sizeof(features),
359 .payload.u64 = features,
362 msg.payload.u64 |= dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
364 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
366 PMD_DRV_LOG(ERR, "Failed to set features");
374 struct vhost_memory *vm;
380 update_memory_region(const struct rte_memseg_list *msl __rte_unused,
381 const struct rte_memseg *ms, void *arg)
383 struct walk_arg *wa = arg;
384 struct vhost_memory_region *mr;
385 uint64_t start_addr, end_addr;
389 fd = rte_memseg_get_fd_thread_unsafe(ms);
391 PMD_DRV_LOG(ERR, "Failed to get fd, ms=%p rte_errno=%d",
396 if (rte_memseg_get_fd_offset_thread_unsafe(ms, &offset) < 0) {
397 PMD_DRV_LOG(ERR, "Failed to get offset, ms=%p rte_errno=%d",
402 start_addr = (uint64_t)(uintptr_t)ms->addr;
403 end_addr = start_addr + ms->len;
405 for (i = 0; i < wa->region_nr; i++) {
406 if (wa->fds[i] != fd)
409 mr = &wa->vm->regions[i];
411 if (mr->userspace_addr + mr->memory_size < end_addr)
412 mr->memory_size = end_addr - mr->userspace_addr;
414 if (mr->userspace_addr > start_addr) {
415 mr->userspace_addr = start_addr;
416 mr->guest_phys_addr = start_addr;
419 if (mr->mmap_offset > offset)
420 mr->mmap_offset = offset;
422 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
423 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
424 mr->mmap_offset, mr->userspace_addr,
430 if (i >= VHOST_MEMORY_MAX_NREGIONS) {
431 PMD_DRV_LOG(ERR, "Too many memory regions");
435 mr = &wa->vm->regions[i];
438 mr->guest_phys_addr = start_addr;
439 mr->userspace_addr = start_addr;
440 mr->memory_size = ms->len;
441 mr->mmap_offset = offset;
443 PMD_DRV_LOG(DEBUG, "index=%d fd=%d offset=0x%" PRIx64
444 " addr=0x%" PRIx64 " len=%" PRIu64, i, fd,
445 mr->mmap_offset, mr->userspace_addr,
454 vhost_user_set_memory_table(struct virtio_user_dev *dev)
457 int fds[VHOST_MEMORY_MAX_NREGIONS];
459 struct vhost_user_data *data = dev->backend_data;
460 struct vhost_user_msg msg = {
461 .request = VHOST_USER_SET_MEM_TABLE,
462 .flags = VHOST_USER_VERSION,
465 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
466 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
469 wa.vm = &msg.payload.memory;
473 * The memory lock has already been taken by memory subsystem
474 * or virtio_user_start_device().
476 ret = rte_memseg_walk_thread_unsafe(update_memory_region, &wa);
480 fd_num = wa.region_nr;
481 msg.payload.memory.nregions = wa.region_nr;
482 msg.payload.memory.padding = 0;
484 msg.size = sizeof(msg.payload.memory.nregions);
485 msg.size += sizeof(msg.payload.memory.padding);
486 msg.size += fd_num * sizeof(struct vhost_memory_region);
488 ret = vhost_user_write(data->vhostfd, &msg, fds, fd_num);
492 return vhost_user_check_reply_ack(dev, &msg);
494 PMD_DRV_LOG(ERR, "Failed to set memory table");
499 vhost_user_set_vring(struct virtio_user_dev *dev, enum vhost_user_request req,
500 struct vhost_vring_state *state)
503 struct vhost_user_data *data = dev->backend_data;
504 struct vhost_user_msg msg = {
506 .flags = VHOST_USER_VERSION,
507 .size = sizeof(*state),
508 .payload.state = *state,
511 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
513 PMD_DRV_LOG(ERR, "Failed to set vring state (request %d)", req);
521 vhost_user_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
523 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, state);
527 vhost_user_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
529 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_NUM, state);
533 vhost_user_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
535 return vhost_user_set_vring(dev, VHOST_USER_SET_VRING_BASE, state);
539 vhost_user_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
542 struct vhost_user_msg msg;
543 struct vhost_user_data *data = dev->backend_data;
544 unsigned int index = state->index;
546 ret = vhost_user_set_vring(dev, VHOST_USER_GET_VRING_BASE, state);
548 PMD_DRV_LOG(ERR, "Failed to send request");
552 ret = vhost_user_read(data->vhostfd, &msg);
554 PMD_DRV_LOG(ERR, "Failed to read reply");
558 if (msg.request != VHOST_USER_GET_VRING_BASE) {
559 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
563 if (msg.size != sizeof(*state)) {
564 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
568 if (msg.payload.state.index != index) {
569 PMD_DRV_LOG(ERR, "Unexpected ring index (%u)", state->index);
573 *state = msg.payload.state;
577 PMD_DRV_LOG(ERR, "Failed to get vring base");
582 vhost_user_set_vring_file(struct virtio_user_dev *dev, enum vhost_user_request req,
583 struct vhost_vring_file *file)
588 struct vhost_user_data *data = dev->backend_data;
589 struct vhost_user_msg msg = {
591 .flags = VHOST_USER_VERSION,
592 .size = sizeof(msg.payload.u64),
593 .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
599 msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
601 ret = vhost_user_write(data->vhostfd, &msg, &fd, num_fd);
603 PMD_DRV_LOG(ERR, "Failed to set vring file (request %d)", req);
611 vhost_user_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
613 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
617 vhost_user_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
619 return vhost_user_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
624 vhost_user_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
627 struct vhost_user_data *data = dev->backend_data;
628 struct vhost_user_msg msg = {
629 .request = VHOST_USER_SET_VRING_ADDR,
630 .flags = VHOST_USER_VERSION,
631 .size = sizeof(*addr),
632 .payload.addr = *addr,
635 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
637 PMD_DRV_LOG(ERR, "Failed to send vring addresses");
645 vhost_user_get_status(struct virtio_user_dev *dev, uint8_t *status)
648 struct vhost_user_data *data = dev->backend_data;
649 struct vhost_user_msg msg = {
650 .request = VHOST_USER_GET_STATUS,
651 .flags = VHOST_USER_VERSION,
655 * If features have not been negotiated, we don't know if the backend
656 * supports protocol features
658 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
661 /* Status protocol feature requires protocol features support */
662 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
665 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
668 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
670 PMD_DRV_LOG(ERR, "Failed to send request");
674 ret = vhost_user_read(data->vhostfd, &msg);
676 PMD_DRV_LOG(ERR, "Failed to recv request");
680 if (msg.request != VHOST_USER_GET_STATUS) {
681 PMD_DRV_LOG(ERR, "Unexpected request type (%d)", msg.request);
685 if (msg.size != sizeof(msg.payload.u64)) {
686 PMD_DRV_LOG(ERR, "Unexpected payload size (%u)", msg.size);
690 *status = (uint8_t)msg.payload.u64;
694 PMD_DRV_LOG(ERR, "Failed to get device status");
699 vhost_user_set_status(struct virtio_user_dev *dev, uint8_t status)
702 struct vhost_user_data *data = dev->backend_data;
703 struct vhost_user_msg msg = {
704 .request = VHOST_USER_SET_STATUS,
705 .flags = VHOST_USER_VERSION,
706 .size = sizeof(msg.payload.u64),
707 .payload.u64 = status,
711 * If features have not been negotiated, we don't know if the backend
712 * supports protocol features
714 if (!(dev->status & VIRTIO_CONFIG_STATUS_FEATURES_OK))
717 /* Status protocol feature requires protocol features support */
718 if (!(dev->device_features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
721 if (!(data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_STATUS)))
724 if (data->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK))
725 msg.flags |= VHOST_USER_NEED_REPLY_MASK;
727 ret = vhost_user_write(data->vhostfd, &msg, NULL, 0);
729 PMD_DRV_LOG(ERR, "Failed to send get status request");
733 return vhost_user_check_reply_ack(dev, &msg);
736 #define MAX_VIRTIO_USER_BACKLOG 1
738 vhost_user_start_server(struct virtio_user_dev *dev, struct sockaddr_un *un)
742 struct vhost_user_data *data = dev->backend_data;
743 int fd = data->listenfd;
745 ret = bind(fd, (struct sockaddr *)un, sizeof(*un));
747 PMD_DRV_LOG(ERR, "failed to bind to %s: %s; remove it and try again\n",
748 dev->path, strerror(errno));
751 ret = listen(fd, MAX_VIRTIO_USER_BACKLOG);
755 PMD_DRV_LOG(NOTICE, "(%s) waiting for client connection...", dev->path);
756 data->vhostfd = accept(fd, NULL, NULL);
757 if (data->vhostfd < 0) {
758 PMD_DRV_LOG(ERR, "Failed to accept initial client connection (%s)",
763 flag = fcntl(fd, F_GETFL);
764 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
765 PMD_DRV_LOG(ERR, "fcntl failed, %s", strerror(errno));
773 vhost_user_server_disconnect(struct virtio_user_dev *dev)
775 struct vhost_user_data *data = dev->backend_data;
777 if (data->vhostfd < 0) {
778 PMD_DRV_LOG(ERR, "(%s) Expected valid Vhost FD", dev->path);
782 close(data->vhostfd);
789 vhost_user_server_reconnect(struct virtio_user_dev *dev)
791 struct vhost_user_data *data = dev->backend_data;
794 fd = accept(data->listenfd, NULL, NULL);
804 * Set up environment to talk with a vhost user backend.
811 vhost_user_setup(struct virtio_user_dev *dev)
815 struct sockaddr_un un;
816 struct vhost_user_data *data;
818 data = malloc(sizeof(*data));
820 PMD_DRV_LOG(ERR, "(%s) Failed to allocate Vhost-user data\n", dev->path);
824 memset(data, 0, sizeof(*data));
826 dev->backend_data = data;
830 fd = socket(AF_UNIX, SOCK_STREAM, 0);
832 PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
836 flag = fcntl(fd, F_GETFD);
837 if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
838 PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
840 memset(&un, 0, sizeof(un));
841 un.sun_family = AF_UNIX;
842 strlcpy(un.sun_path, dev->path, sizeof(un.sun_path));
844 if (dev->is_server) {
846 if (vhost_user_start_server(dev, &un) < 0) {
847 PMD_DRV_LOG(ERR, "virtio-user startup fails in server mode");
851 if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
852 PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
864 dev->backend_data = NULL;
870 vhost_user_destroy(struct virtio_user_dev *dev)
872 struct vhost_user_data *data = dev->backend_data;
877 if (data->vhostfd >= 0) {
878 close(data->vhostfd);
882 if (data->listenfd >= 0) {
883 close(data->listenfd);
888 dev->backend_data = NULL;
894 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
898 struct vhost_user_data *data = dev->backend_data;
901 if (data->vhostfd < 0)
904 if (dev->qp_enabled[pair_idx] == enable)
907 for (i = 0; i < 2; ++i) {
908 struct vhost_vring_state state = {
909 .index = pair_idx * 2 + i,
913 if (vhost_user_set_vring_enable(dev, &state))
917 dev->qp_enabled[pair_idx] = enable;
922 vhost_user_get_backend_features(uint64_t *features)
924 *features = 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
930 vhost_user_update_link_state(struct virtio_user_dev *dev)
932 struct vhost_user_data *data = dev->backend_data;
935 if (data->vhostfd >= 0) {
939 flags = fcntl(data->vhostfd, F_GETFL);
940 if (fcntl(data->vhostfd, F_SETFL, flags | O_NONBLOCK) == -1) {
941 PMD_DRV_LOG(ERR, "error setting O_NONBLOCK flag");
945 r = recv(data->vhostfd, buf, 128, MSG_PEEK);
946 if (r == 0 || (r < 0 && errno != EAGAIN)) {
947 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
948 PMD_DRV_LOG(ERR, "virtio-user port %u is down", dev->port_id);
950 /* This function could be called in the process
951 * of interrupt handling, callback cannot be
952 * unregistered here, set an alarm to do it.
954 rte_eal_alarm_set(1, virtio_user_dev_delayed_handler, (void *)dev);
956 dev->net_status |= VIRTIO_NET_S_LINK_UP;
959 if (fcntl(data->vhostfd, F_SETFL,
960 flags & ~O_NONBLOCK) == -1) {
961 PMD_DRV_LOG(ERR, "error clearing O_NONBLOCK flag");
964 } else if (dev->is_server) {
965 dev->net_status &= (~VIRTIO_NET_S_LINK_UP);
966 if (virtio_user_dev_server_reconnect(dev) >= 0)
967 dev->net_status |= VIRTIO_NET_S_LINK_UP;
974 vhost_user_get_intr_fd(struct virtio_user_dev *dev)
976 struct vhost_user_data *data = dev->backend_data;
978 if (dev->is_server && data->vhostfd == -1)
979 return data->listenfd;
981 return data->vhostfd;
984 struct virtio_user_backend_ops virtio_ops_user = {
985 .setup = vhost_user_setup,
986 .destroy = vhost_user_destroy,
987 .get_backend_features = vhost_user_get_backend_features,
988 .set_owner = vhost_user_set_owner,
989 .get_features = vhost_user_get_features,
990 .set_features = vhost_user_set_features,
991 .set_memory_table = vhost_user_set_memory_table,
992 .set_vring_num = vhost_user_set_vring_num,
993 .set_vring_base = vhost_user_set_vring_base,
994 .get_vring_base = vhost_user_get_vring_base,
995 .set_vring_call = vhost_user_set_vring_call,
996 .set_vring_kick = vhost_user_set_vring_kick,
997 .set_vring_addr = vhost_user_set_vring_addr,
998 .get_status = vhost_user_get_status,
999 .set_status = vhost_user_set_status,
1000 .enable_qp = vhost_user_enable_queue_pair,
1001 .update_link_state = vhost_user_update_link_state,
1002 .server_disconnect = vhost_user_server_disconnect,
1003 .server_reconnect = vhost_user_server_reconnect,
1004 .get_intr_fd = vhost_user_get_intr_fd,