86c364a935f470074f0380cc7b8c5036021d5715
[dpdk.git] / lib / librte_vhost / vhost_user.h
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 #include <linux/vhost.h>
10
11 #include "rte_vhost.h"
12
13 /* refer to hw/virtio/vhost-user.c */
14
15 #define VHOST_MEMORY_MAX_NREGIONS 8
16
17 #define VHOST_USER_PROTOCOL_FEATURES    ((1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
18                                          (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |\
19                                          (1ULL << VHOST_USER_PROTOCOL_F_RARP) | \
20                                          (1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK) | \
21                                          (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU) | \
22                                          (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
23                                          (1ULL << VHOST_USER_PROTOCOL_F_CRYPTO_SESSION) | \
24                                          (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
25                                          (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
26                                          (1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT))
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_CRYPTO_CREATE_SESS = 26,
53         VHOST_USER_CRYPTO_CLOSE_SESS = 27,
54         VHOST_USER_POSTCOPY_ADVISE = 28,
55         VHOST_USER_POSTCOPY_LISTEN = 29,
56         VHOST_USER_POSTCOPY_END = 30,
57         VHOST_USER_GET_INFLIGHT_FD = 31,
58         VHOST_USER_SET_INFLIGHT_FD = 32,
59         VHOST_USER_MAX = 33
60 } VhostUserRequest;
61
62 typedef enum VhostUserSlaveRequest {
63         VHOST_USER_SLAVE_NONE = 0,
64         VHOST_USER_SLAVE_IOTLB_MSG = 1,
65         VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
66         VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
67         VHOST_USER_SLAVE_MAX
68 } VhostUserSlaveRequest;
69
70 typedef struct VhostUserMemoryRegion {
71         uint64_t guest_phys_addr;
72         uint64_t memory_size;
73         uint64_t userspace_addr;
74         uint64_t mmap_offset;
75 } VhostUserMemoryRegion;
76
77 typedef struct VhostUserMemory {
78         uint32_t nregions;
79         uint32_t padding;
80         VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
81 } VhostUserMemory;
82
83 typedef struct VhostUserLog {
84         uint64_t mmap_size;
85         uint64_t mmap_offset;
86 } VhostUserLog;
87
88 /* Comply with Cryptodev-Linux */
89 #define VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH   512
90 #define VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH 64
91
92 /* Same structure as vhost-user backend session info */
93 typedef struct VhostUserCryptoSessionParam {
94         int64_t session_id;
95         uint32_t op_code;
96         uint32_t cipher_algo;
97         uint32_t cipher_key_len;
98         uint32_t hash_algo;
99         uint32_t digest_len;
100         uint32_t auth_key_len;
101         uint32_t aad_len;
102         uint8_t op_type;
103         uint8_t dir;
104         uint8_t hash_mode;
105         uint8_t chaining_dir;
106         uint8_t *ciphe_key;
107         uint8_t *auth_key;
108         uint8_t cipher_key_buf[VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH];
109         uint8_t auth_key_buf[VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH];
110 } VhostUserCryptoSessionParam;
111
112 typedef struct VhostUserVringArea {
113         uint64_t u64;
114         uint64_t size;
115         uint64_t offset;
116 } VhostUserVringArea;
117
118 typedef struct VhostUserInflight {
119         uint64_t mmap_size;
120         uint64_t mmap_offset;
121         uint16_t num_queues;
122         uint16_t queue_size;
123 } VhostUserInflight;
124
125 typedef struct VhostUserMsg {
126         union {
127                 uint32_t master; /* a VhostUserRequest value */
128                 uint32_t slave;  /* a VhostUserSlaveRequest value*/
129         } request;
130
131 #define VHOST_USER_VERSION_MASK     0x3
132 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
133 #define VHOST_USER_NEED_REPLY           (0x1 << 3)
134         uint32_t flags;
135         uint32_t size; /* the following payload size */
136         union {
137 #define VHOST_USER_VRING_IDX_MASK   0xff
138 #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
139                 uint64_t u64;
140                 struct vhost_vring_state state;
141                 struct vhost_vring_addr addr;
142                 VhostUserMemory memory;
143                 VhostUserLog    log;
144                 struct vhost_iotlb_msg iotlb;
145                 VhostUserCryptoSessionParam crypto_session;
146                 VhostUserVringArea area;
147                 VhostUserInflight inflight;
148         } payload;
149         int fds[VHOST_MEMORY_MAX_NREGIONS];
150         int fd_num;
151 } __attribute((packed)) VhostUserMsg;
152
153 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
154
155 /* The version of the protocol we support */
156 #define VHOST_USER_VERSION    0x1
157
158
159 /* vhost_user.c */
160 int vhost_user_msg_handler(int vid, int fd);
161 int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm);
162
163 /* socket.c */
164 int read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
165                 int *fd_num);
166 int send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num);
167
168 #endif