Imported Upstream version 16.07-rc1
[deb_dpdk.git] / drivers / net / virtio / virtio_user / vhost.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _VHOST_NET_USER_H
35 #define _VHOST_NET_USER_H
36
37 #include <stdint.h>
38 #include <linux/types.h>
39 #include <linux/ioctl.h>
40
41 #include "../virtio_pci.h"
42 #include "../virtio_logs.h"
43 #include "../virtqueue.h"
44
45 #define VHOST_MEMORY_MAX_NREGIONS 8
46
47 struct vhost_vring_state {
48         unsigned int index;
49         unsigned int num;
50 };
51
52 struct vhost_vring_file {
53         unsigned int index;
54         int fd;
55 };
56
57 struct vhost_vring_addr {
58         unsigned int index;
59         /* Option flags. */
60         unsigned int flags;
61         /* Flag values: */
62         /* Whether log address is valid. If set enables logging. */
63 #define VHOST_VRING_F_LOG 0
64
65         /* Start of array of descriptors (virtually contiguous) */
66         uint64_t desc_user_addr;
67         /* Used structure address. Must be 32 bit aligned */
68         uint64_t used_user_addr;
69         /* Available structure address. Must be 16 bit aligned */
70         uint64_t avail_user_addr;
71         /* Logging support. */
72         /* Log writes to used structure, at offset calculated from specified
73          * address. Address must be 32 bit aligned.
74          */
75         uint64_t log_guest_addr;
76 };
77
78 enum vhost_user_request {
79         VHOST_USER_NONE = 0,
80         VHOST_USER_GET_FEATURES = 1,
81         VHOST_USER_SET_FEATURES = 2,
82         VHOST_USER_SET_OWNER = 3,
83         VHOST_USER_RESET_OWNER = 4,
84         VHOST_USER_SET_MEM_TABLE = 5,
85         VHOST_USER_SET_LOG_BASE = 6,
86         VHOST_USER_SET_LOG_FD = 7,
87         VHOST_USER_SET_VRING_NUM = 8,
88         VHOST_USER_SET_VRING_ADDR = 9,
89         VHOST_USER_SET_VRING_BASE = 10,
90         VHOST_USER_GET_VRING_BASE = 11,
91         VHOST_USER_SET_VRING_KICK = 12,
92         VHOST_USER_SET_VRING_CALL = 13,
93         VHOST_USER_SET_VRING_ERR = 14,
94         VHOST_USER_GET_PROTOCOL_FEATURES = 15,
95         VHOST_USER_SET_PROTOCOL_FEATURES = 16,
96         VHOST_USER_GET_QUEUE_NUM = 17,
97         VHOST_USER_SET_VRING_ENABLE = 18,
98         VHOST_USER_MAX
99 };
100
101 struct vhost_memory_region {
102         uint64_t guest_phys_addr;
103         uint64_t memory_size; /* bytes */
104         uint64_t userspace_addr;
105         uint64_t mmap_offset;
106 };
107
108 struct vhost_memory {
109         uint32_t nregions;
110         uint32_t padding;
111         struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
112 };
113
114 struct vhost_user_msg {
115         enum vhost_user_request request;
116
117 #define VHOST_USER_VERSION_MASK     0x3
118 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
119         uint32_t flags;
120         uint32_t size; /* the following payload size */
121         union {
122 #define VHOST_USER_VRING_IDX_MASK   0xff
123 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
124                 uint64_t u64;
125                 struct vhost_vring_state state;
126                 struct vhost_vring_addr addr;
127                 struct vhost_memory memory;
128         } payload;
129         int fds[VHOST_MEMORY_MAX_NREGIONS];
130 } __attribute((packed));
131
132 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
133 #define VHOST_USER_PAYLOAD_SIZE \
134         (sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
135
136 /* The version of the protocol we support */
137 #define VHOST_USER_VERSION    0x1
138
139 #define VHOST_USER_F_PROTOCOL_FEATURES 30
140 #define VHOST_USER_MQ (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
141
142 int vhost_user_sock(int vhostfd, enum vhost_user_request req, void *arg);
143 int vhost_user_setup(const char *path);
144 int vhost_user_enable_queue_pair(int vhostfd, uint16_t pair_idx, int enable);
145
146 #endif