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