xref: /dpdk/lib/vhost/vhost_user.h (revision 7af3e7aaf9141e78858a38c0d6e32b46a196c08f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4 
5 #ifndef _VHOST_NET_USER_H
6 #define _VHOST_NET_USER_H
7 
8 #include <stdint.h>
9 
10 #include "rte_vhost.h"
11 
12 /* refer to hw/virtio/vhost-user.c */
13 
14 #define VHOST_MEMORY_MAX_NREGIONS 8
15 
16 #define VHOST_USER_PROTOCOL_FEATURES	((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
17 					 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |\
18 					 (1ULL << VHOST_USER_PROTOCOL_F_RARP) | \
19 					 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK) | \
20 					 (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU) | \
21 					 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
22 					 (1ULL << VHOST_USER_PROTOCOL_F_CRYPTO_SESSION) | \
23 					 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
24 					 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
25 					 (1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT) | \
26 					 (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
27 
28 typedef enum VhostUserRequest {
29 	VHOST_USER_NONE = 0,
30 	VHOST_USER_GET_FEATURES = 1,
31 	VHOST_USER_SET_FEATURES = 2,
32 	VHOST_USER_SET_OWNER = 3,
33 	VHOST_USER_RESET_OWNER = 4,
34 	VHOST_USER_SET_MEM_TABLE = 5,
35 	VHOST_USER_SET_LOG_BASE = 6,
36 	VHOST_USER_SET_LOG_FD = 7,
37 	VHOST_USER_SET_VRING_NUM = 8,
38 	VHOST_USER_SET_VRING_ADDR = 9,
39 	VHOST_USER_SET_VRING_BASE = 10,
40 	VHOST_USER_GET_VRING_BASE = 11,
41 	VHOST_USER_SET_VRING_KICK = 12,
42 	VHOST_USER_SET_VRING_CALL = 13,
43 	VHOST_USER_SET_VRING_ERR = 14,
44 	VHOST_USER_GET_PROTOCOL_FEATURES = 15,
45 	VHOST_USER_SET_PROTOCOL_FEATURES = 16,
46 	VHOST_USER_GET_QUEUE_NUM = 17,
47 	VHOST_USER_SET_VRING_ENABLE = 18,
48 	VHOST_USER_SEND_RARP = 19,
49 	VHOST_USER_NET_SET_MTU = 20,
50 	VHOST_USER_SET_SLAVE_REQ_FD = 21,
51 	VHOST_USER_IOTLB_MSG = 22,
52 	VHOST_USER_GET_CONFIG = 24,
53 	VHOST_USER_SET_CONFIG = 25,
54 	VHOST_USER_CRYPTO_CREATE_SESS = 26,
55 	VHOST_USER_CRYPTO_CLOSE_SESS = 27,
56 	VHOST_USER_POSTCOPY_ADVISE = 28,
57 	VHOST_USER_POSTCOPY_LISTEN = 29,
58 	VHOST_USER_POSTCOPY_END = 30,
59 	VHOST_USER_GET_INFLIGHT_FD = 31,
60 	VHOST_USER_SET_INFLIGHT_FD = 32,
61 	VHOST_USER_SET_STATUS = 39,
62 	VHOST_USER_GET_STATUS = 40,
63 } VhostUserRequest;
64 
65 typedef enum VhostUserSlaveRequest {
66 	VHOST_USER_SLAVE_NONE = 0,
67 	VHOST_USER_SLAVE_IOTLB_MSG = 1,
68 	VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
69 	VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
70 } VhostUserSlaveRequest;
71 
72 typedef struct VhostUserMemoryRegion {
73 	uint64_t guest_phys_addr;
74 	uint64_t memory_size;
75 	uint64_t userspace_addr;
76 	uint64_t mmap_offset;
77 } VhostUserMemoryRegion;
78 
79 typedef struct VhostUserMemory {
80 	uint32_t nregions;
81 	uint32_t padding;
82 	VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
83 } VhostUserMemory;
84 
85 typedef struct VhostUserLog {
86 	uint64_t mmap_size;
87 	uint64_t mmap_offset;
88 } VhostUserLog;
89 
90 /* Comply with Cryptodev-Linux */
91 #define VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH	512
92 #define VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH	64
93 
94 /* Same structure as vhost-user backend session info */
95 typedef struct VhostUserCryptoSessionParam {
96 	int64_t session_id;
97 	uint32_t op_code;
98 	uint32_t cipher_algo;
99 	uint32_t cipher_key_len;
100 	uint32_t hash_algo;
101 	uint32_t digest_len;
102 	uint32_t auth_key_len;
103 	uint32_t aad_len;
104 	uint8_t op_type;
105 	uint8_t dir;
106 	uint8_t hash_mode;
107 	uint8_t chaining_dir;
108 	uint8_t *ciphe_key;
109 	uint8_t *auth_key;
110 	uint8_t cipher_key_buf[VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH];
111 	uint8_t auth_key_buf[VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH];
112 } VhostUserCryptoSessionParam;
113 
114 typedef struct VhostUserVringArea {
115 	uint64_t u64;
116 	uint64_t size;
117 	uint64_t offset;
118 } VhostUserVringArea;
119 
120 typedef struct VhostUserInflight {
121 	uint64_t mmap_size;
122 	uint64_t mmap_offset;
123 	uint16_t num_queues;
124 	uint16_t queue_size;
125 } VhostUserInflight;
126 
127 #define VHOST_USER_MAX_CONFIG_SIZE		256
128 
129 /** Get/set config msg payload */
130 struct vhost_user_config {
131 	uint32_t offset;
132 	uint32_t size;
133 	uint32_t flags;
134 	uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
135 };
136 
137 typedef struct VhostUserMsg {
138 	union {
139 		uint32_t master; /* a VhostUserRequest value */
140 		uint32_t slave;  /* a VhostUserSlaveRequest value*/
141 	} request;
142 
143 #define VHOST_USER_VERSION_MASK     0x3
144 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
145 #define VHOST_USER_NEED_REPLY		(0x1 << 3)
146 	uint32_t flags;
147 	uint32_t size; /* the following payload size */
148 	union {
149 #define VHOST_USER_VRING_IDX_MASK   0xff
150 #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
151 		uint64_t u64;
152 		struct vhost_vring_state state;
153 		struct vhost_vring_addr addr;
154 		VhostUserMemory memory;
155 		VhostUserLog    log;
156 		struct vhost_iotlb_msg iotlb;
157 		VhostUserCryptoSessionParam crypto_session;
158 		VhostUserVringArea area;
159 		VhostUserInflight inflight;
160 		struct vhost_user_config cfg;
161 	} payload;
162 	/* Nothing should be added after the payload */
163 } __rte_packed VhostUserMsg;
164 
165 /* Note: this structure and VhostUserMsg can't be changed carelessly as
166  * external message handlers rely on them.
167  */
168 struct __rte_packed vhu_msg_context {
169 	VhostUserMsg msg;
170 	int fds[VHOST_MEMORY_MAX_NREGIONS];
171 	int fd_num;
172 };
173 
174 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
175 
176 /* The version of the protocol we support */
177 #define VHOST_USER_VERSION    0x1
178 
179 
180 /* vhost_user.c */
181 int vhost_user_msg_handler(int vid, int fd);
182 int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm);
183 
184 /* socket.c */
185 int read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
186 		int *fd_num);
187 int send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num);
188 
189 #endif
190