xref: /dpdk/lib/vhost/vhost_user.h (revision fba9875559906e04eaeb74532f4cfd51194259a2)
199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson  * Copyright(c) 2010-2018 Intel Corporation
399a2dd95SBruce Richardson  */
499a2dd95SBruce Richardson 
599a2dd95SBruce Richardson #ifndef _VHOST_NET_USER_H
699a2dd95SBruce Richardson #define _VHOST_NET_USER_H
799a2dd95SBruce Richardson 
899a2dd95SBruce Richardson #include <stdint.h>
999a2dd95SBruce Richardson 
1099a2dd95SBruce Richardson #include "rte_vhost.h"
1199a2dd95SBruce Richardson 
1299a2dd95SBruce Richardson /* refer to hw/virtio/vhost-user.c */
1399a2dd95SBruce Richardson 
1499a2dd95SBruce Richardson #define VHOST_MEMORY_MAX_NREGIONS 8
1599a2dd95SBruce Richardson 
164789eb43SMaxime Coquelin #define VHOST_USER_NET_SUPPORTED_FEATURES \
174789eb43SMaxime Coquelin 	(VIRTIO_NET_SUPPORTED_FEATURES | \
184789eb43SMaxime Coquelin 	 (1ULL << VIRTIO_F_RING_PACKED) | \
194789eb43SMaxime Coquelin 	 (1ULL << VIRTIO_NET_F_MTU) | \
204789eb43SMaxime Coquelin 	 (1ULL << VHOST_F_LOG_ALL) | \
214789eb43SMaxime Coquelin 	 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
224789eb43SMaxime Coquelin 	 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
234789eb43SMaxime Coquelin 	 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE))
244789eb43SMaxime Coquelin 
2599a2dd95SBruce Richardson #define VHOST_USER_PROTOCOL_FEATURES	((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
2699a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |\
2799a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_RARP) | \
2899a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK) | \
2999a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU) | \
30f1eb477eSNobuhiro Miki 					 (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_REQ) | \
3199a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_CRYPTO_SESSION) | \
32f1eb477eSNobuhiro Miki 					 (1ULL << VHOST_USER_PROTOCOL_F_BACKEND_SEND_FD) | \
3399a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
3499a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT) | \
3599a2dd95SBruce Richardson 					 (1ULL << VHOST_USER_PROTOCOL_F_STATUS))
3699a2dd95SBruce Richardson 
3799a2dd95SBruce Richardson typedef enum VhostUserRequest {
3899a2dd95SBruce Richardson 	VHOST_USER_NONE = 0,
3999a2dd95SBruce Richardson 	VHOST_USER_GET_FEATURES = 1,
4099a2dd95SBruce Richardson 	VHOST_USER_SET_FEATURES = 2,
4199a2dd95SBruce Richardson 	VHOST_USER_SET_OWNER = 3,
4299a2dd95SBruce Richardson 	VHOST_USER_RESET_OWNER = 4,
4399a2dd95SBruce Richardson 	VHOST_USER_SET_MEM_TABLE = 5,
4499a2dd95SBruce Richardson 	VHOST_USER_SET_LOG_BASE = 6,
4599a2dd95SBruce Richardson 	VHOST_USER_SET_LOG_FD = 7,
4699a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_NUM = 8,
4799a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_ADDR = 9,
4899a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_BASE = 10,
4999a2dd95SBruce Richardson 	VHOST_USER_GET_VRING_BASE = 11,
5099a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_KICK = 12,
5199a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_CALL = 13,
5299a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_ERR = 14,
5399a2dd95SBruce Richardson 	VHOST_USER_GET_PROTOCOL_FEATURES = 15,
5499a2dd95SBruce Richardson 	VHOST_USER_SET_PROTOCOL_FEATURES = 16,
5599a2dd95SBruce Richardson 	VHOST_USER_GET_QUEUE_NUM = 17,
5699a2dd95SBruce Richardson 	VHOST_USER_SET_VRING_ENABLE = 18,
5799a2dd95SBruce Richardson 	VHOST_USER_SEND_RARP = 19,
5899a2dd95SBruce Richardson 	VHOST_USER_NET_SET_MTU = 20,
59f1eb477eSNobuhiro Miki 	VHOST_USER_SET_BACKEND_REQ_FD = 21,
6099a2dd95SBruce Richardson 	VHOST_USER_IOTLB_MSG = 22,
617a3ac984SAndy Pei 	VHOST_USER_GET_CONFIG = 24,
627a3ac984SAndy Pei 	VHOST_USER_SET_CONFIG = 25,
6399a2dd95SBruce Richardson 	VHOST_USER_CRYPTO_CREATE_SESS = 26,
6499a2dd95SBruce Richardson 	VHOST_USER_CRYPTO_CLOSE_SESS = 27,
6599a2dd95SBruce Richardson 	VHOST_USER_POSTCOPY_ADVISE = 28,
6699a2dd95SBruce Richardson 	VHOST_USER_POSTCOPY_LISTEN = 29,
6799a2dd95SBruce Richardson 	VHOST_USER_POSTCOPY_END = 30,
6899a2dd95SBruce Richardson 	VHOST_USER_GET_INFLIGHT_FD = 31,
6999a2dd95SBruce Richardson 	VHOST_USER_SET_INFLIGHT_FD = 32,
7099a2dd95SBruce Richardson 	VHOST_USER_SET_STATUS = 39,
7199a2dd95SBruce Richardson 	VHOST_USER_GET_STATUS = 40,
7299a2dd95SBruce Richardson } VhostUserRequest;
7399a2dd95SBruce Richardson 
7471998eb6SNobuhiro Miki typedef enum VhostUserBackendRequest {
75f1eb477eSNobuhiro Miki 	VHOST_USER_BACKEND_NONE = 0,
76f1eb477eSNobuhiro Miki 	VHOST_USER_BACKEND_IOTLB_MSG = 1,
77f1eb477eSNobuhiro Miki 	VHOST_USER_BACKEND_CONFIG_CHANGE_MSG = 2,
78f1eb477eSNobuhiro Miki 	VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG = 3,
7971998eb6SNobuhiro Miki } VhostUserBackendRequest;
8099a2dd95SBruce Richardson 
8199a2dd95SBruce Richardson typedef struct VhostUserMemoryRegion {
8299a2dd95SBruce Richardson 	uint64_t guest_phys_addr;
8399a2dd95SBruce Richardson 	uint64_t memory_size;
8499a2dd95SBruce Richardson 	uint64_t userspace_addr;
8599a2dd95SBruce Richardson 	uint64_t mmap_offset;
8699a2dd95SBruce Richardson } VhostUserMemoryRegion;
8799a2dd95SBruce Richardson 
8899a2dd95SBruce Richardson typedef struct VhostUserMemory {
8999a2dd95SBruce Richardson 	uint32_t nregions;
9099a2dd95SBruce Richardson 	uint32_t padding;
9199a2dd95SBruce Richardson 	VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
9299a2dd95SBruce Richardson } VhostUserMemory;
9399a2dd95SBruce Richardson 
9499a2dd95SBruce Richardson typedef struct VhostUserLog {
9599a2dd95SBruce Richardson 	uint64_t mmap_size;
9699a2dd95SBruce Richardson 	uint64_t mmap_offset;
9799a2dd95SBruce Richardson } VhostUserLog;
9899a2dd95SBruce Richardson 
9999a2dd95SBruce Richardson /* Comply with Cryptodev-Linux */
10099a2dd95SBruce Richardson #define VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH	512
10199a2dd95SBruce Richardson #define VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH	64
10299a2dd95SBruce Richardson 
10399a2dd95SBruce Richardson /* Same structure as vhost-user backend session info */
10499a2dd95SBruce Richardson typedef struct VhostUserCryptoSessionParam {
10599a2dd95SBruce Richardson 	int64_t session_id;
10699a2dd95SBruce Richardson 	uint32_t op_code;
10799a2dd95SBruce Richardson 	uint32_t cipher_algo;
10899a2dd95SBruce Richardson 	uint32_t cipher_key_len;
10999a2dd95SBruce Richardson 	uint32_t hash_algo;
11099a2dd95SBruce Richardson 	uint32_t digest_len;
11199a2dd95SBruce Richardson 	uint32_t auth_key_len;
11299a2dd95SBruce Richardson 	uint32_t aad_len;
11399a2dd95SBruce Richardson 	uint8_t op_type;
11499a2dd95SBruce Richardson 	uint8_t dir;
11599a2dd95SBruce Richardson 	uint8_t hash_mode;
11699a2dd95SBruce Richardson 	uint8_t chaining_dir;
11799a2dd95SBruce Richardson 	uint8_t *ciphe_key;
11899a2dd95SBruce Richardson 	uint8_t *auth_key;
11999a2dd95SBruce Richardson 	uint8_t cipher_key_buf[VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH];
12099a2dd95SBruce Richardson 	uint8_t auth_key_buf[VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH];
12199a2dd95SBruce Richardson } VhostUserCryptoSessionParam;
12299a2dd95SBruce Richardson 
12399a2dd95SBruce Richardson typedef struct VhostUserVringArea {
12499a2dd95SBruce Richardson 	uint64_t u64;
12599a2dd95SBruce Richardson 	uint64_t size;
12699a2dd95SBruce Richardson 	uint64_t offset;
12799a2dd95SBruce Richardson } VhostUserVringArea;
12899a2dd95SBruce Richardson 
12999a2dd95SBruce Richardson typedef struct VhostUserInflight {
13099a2dd95SBruce Richardson 	uint64_t mmap_size;
13199a2dd95SBruce Richardson 	uint64_t mmap_offset;
13299a2dd95SBruce Richardson 	uint16_t num_queues;
13399a2dd95SBruce Richardson 	uint16_t queue_size;
13499a2dd95SBruce Richardson } VhostUserInflight;
13599a2dd95SBruce Richardson 
1367a3ac984SAndy Pei #define VHOST_USER_MAX_CONFIG_SIZE		256
1377a3ac984SAndy Pei 
1387a3ac984SAndy Pei /** Get/set config msg payload */
1397a3ac984SAndy Pei struct vhost_user_config {
1407a3ac984SAndy Pei 	uint32_t offset;
1417a3ac984SAndy Pei 	uint32_t size;
1427a3ac984SAndy Pei 	uint32_t flags;
1437a3ac984SAndy Pei 	uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
1447a3ac984SAndy Pei };
1457a3ac984SAndy Pei 
146*fba98755SAndre Muezerie typedef struct __rte_packed_begin VhostUserMsg {
14799a2dd95SBruce Richardson 	union {
14871998eb6SNobuhiro Miki 		uint32_t frontend; /* a VhostUserRequest value */
14971998eb6SNobuhiro Miki 		uint32_t backend;  /* a VhostUserBackendRequest value*/
15099a2dd95SBruce Richardson 	} request;
15199a2dd95SBruce Richardson 
15299a2dd95SBruce Richardson #define VHOST_USER_VERSION_MASK     0x3
15399a2dd95SBruce Richardson #define VHOST_USER_REPLY_MASK       (0x1 << 2)
15499a2dd95SBruce Richardson #define VHOST_USER_NEED_REPLY		(0x1 << 3)
15599a2dd95SBruce Richardson 	uint32_t flags;
15699a2dd95SBruce Richardson 	uint32_t size; /* the following payload size */
15799a2dd95SBruce Richardson 	union {
15899a2dd95SBruce Richardson #define VHOST_USER_VRING_IDX_MASK   0xff
15999a2dd95SBruce Richardson #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
16099a2dd95SBruce Richardson 		uint64_t u64;
16199a2dd95SBruce Richardson 		struct vhost_vring_state state;
16299a2dd95SBruce Richardson 		struct vhost_vring_addr addr;
16399a2dd95SBruce Richardson 		VhostUserMemory memory;
16499a2dd95SBruce Richardson 		VhostUserLog    log;
16599a2dd95SBruce Richardson 		struct vhost_iotlb_msg iotlb;
16699a2dd95SBruce Richardson 		VhostUserCryptoSessionParam crypto_session;
16799a2dd95SBruce Richardson 		VhostUserVringArea area;
16899a2dd95SBruce Richardson 		VhostUserInflight inflight;
1697a3ac984SAndy Pei 		struct vhost_user_config cfg;
17099a2dd95SBruce Richardson 	} payload;
1715e0099dcSChristophe Fontaine 	/* Nothing should be added after the payload */
172*fba98755SAndre Muezerie } __rte_packed_end VhostUserMsg;
1735e0099dcSChristophe Fontaine 
174762cdb7bSDavid Marchand /* Note: this structure and VhostUserMsg can't be changed carelessly as
175762cdb7bSDavid Marchand  * external message handlers rely on them.
176762cdb7bSDavid Marchand  */
177*fba98755SAndre Muezerie struct __rte_packed_begin vhu_msg_context {
178762cdb7bSDavid Marchand 	VhostUserMsg msg;
17999a2dd95SBruce Richardson 	int fds[VHOST_MEMORY_MAX_NREGIONS];
18099a2dd95SBruce Richardson 	int fd_num;
181*fba98755SAndre Muezerie } __rte_packed_end;
18299a2dd95SBruce Richardson 
18399a2dd95SBruce Richardson #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
18499a2dd95SBruce Richardson 
18599a2dd95SBruce Richardson /* The version of the protocol we support */
18699a2dd95SBruce Richardson #define VHOST_USER_VERSION    0x1
18799a2dd95SBruce Richardson 
18899a2dd95SBruce Richardson 
18999a2dd95SBruce Richardson /* vhost_user.c */
19099a2dd95SBruce Richardson int vhost_user_msg_handler(int vid, int fd);
19199a2dd95SBruce Richardson 
19299a2dd95SBruce Richardson /* socket.c */
193c85c35b1SMaxime Coquelin int read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
19499a2dd95SBruce Richardson 		int *fd_num);
195c85c35b1SMaxime Coquelin int send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num);
1964dbf9316SMaxime Coquelin int vhost_user_new_device(void);
19799a2dd95SBruce Richardson 
19899a2dd95SBruce Richardson #endif
199