net/virtio: add virtio-user ops to set owner
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_user.c
index 350eed4..ea3bd4c 100644 (file)
@@ -51,7 +51,7 @@ struct vhost_user_msg {
        (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
 
 static int
-vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
+vhost_user_write(int fd, struct vhost_user_msg *msg, int *fds, int fd_num)
 {
        int r;
        struct msghdr msgh;
@@ -63,8 +63,8 @@ vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
        memset(&msgh, 0, sizeof(msgh));
        memset(control, 0, sizeof(control));
 
-       iov.iov_base = (uint8_t *)buf;
-       iov.iov_len = len;
+       iov.iov_base = (uint8_t *)msg;
+       iov.iov_len = VHOST_USER_HDR_SIZE + msg->size;
 
        msgh.msg_iov = &iov;
        msgh.msg_iovlen = 1;
@@ -125,6 +125,24 @@ fail:
        return -1;
 }
 
+static int
+vhost_user_set_owner(struct virtio_user_dev *dev)
+{
+       int ret;
+       struct vhost_user_msg msg = {
+               .request = VHOST_USER_SET_OWNER,
+               .flags = VHOST_USER_VERSION,
+       };
+
+       ret = vhost_user_write(dev->vhostfd, &msg, NULL, 0);
+       if (ret < 0) {
+               PMD_DRV_LOG(ERR, "Failed to set owner");
+               return -1;
+       }
+
+       return 0;
+}
+
 struct walk_arg {
        struct vhost_memory *vm;
        int *fds;
@@ -230,7 +248,6 @@ prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
 static struct vhost_user_msg m;
 
 const char * const vhost_msg_strings[] = {
-       [VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
        [VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
        [VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
        [VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
@@ -259,7 +276,6 @@ vhost_user_sock(struct virtio_user_dev *dev,
        int has_reply_ack = 0;
        int fds[VHOST_MEMORY_MAX_NREGIONS];
        int fd_num = 0;
-       int len;
        int vhostfd = dev->vhostfd;
 
        RTE_SET_USED(m);
@@ -309,7 +325,6 @@ vhost_user_sock(struct virtio_user_dev *dev,
                msg.size = sizeof(m.payload.u64);
                break;
 
-       case VHOST_USER_SET_OWNER:
        case VHOST_USER_RESET_OWNER:
                break;
 
@@ -364,8 +379,7 @@ vhost_user_sock(struct virtio_user_dev *dev,
                return -1;
        }
 
-       len = VHOST_USER_HDR_SIZE + msg.size;
-       if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
+       if (vhost_user_write(vhostfd, &msg, fds, fd_num) < 0) {
                PMD_DRV_LOG(ERR, "%s failed: %s",
                            vhost_msg_strings[req], strerror(errno));
                return -1;
@@ -521,6 +535,7 @@ vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
 
 struct virtio_user_backend_ops virtio_ops_user = {
        .setup = vhost_user_setup,
+       .set_owner = vhost_user_set_owner,
        .send_request = vhost_user_sock,
        .enable_qp = vhost_user_enable_queue_pair
 };