73b1fe2b98bb86de9035dab0e3d9527709aa3a29
[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
27 typedef enum VhostUserRequest {
28         VHOST_USER_NONE = 0,
29         VHOST_USER_GET_FEATURES = 1,
30         VHOST_USER_SET_FEATURES = 2,
31         VHOST_USER_SET_OWNER = 3,
32         VHOST_USER_RESET_OWNER = 4,
33         VHOST_USER_SET_MEM_TABLE = 5,
34         VHOST_USER_SET_LOG_BASE = 6,
35         VHOST_USER_SET_LOG_FD = 7,
36         VHOST_USER_SET_VRING_NUM = 8,
37         VHOST_USER_SET_VRING_ADDR = 9,
38         VHOST_USER_SET_VRING_BASE = 10,
39         VHOST_USER_GET_VRING_BASE = 11,
40         VHOST_USER_SET_VRING_KICK = 12,
41         VHOST_USER_SET_VRING_CALL = 13,
42         VHOST_USER_SET_VRING_ERR = 14,
43         VHOST_USER_GET_PROTOCOL_FEATURES = 15,
44         VHOST_USER_SET_PROTOCOL_FEATURES = 16,
45         VHOST_USER_GET_QUEUE_NUM = 17,
46         VHOST_USER_SET_VRING_ENABLE = 18,
47         VHOST_USER_SEND_RARP = 19,
48         VHOST_USER_NET_SET_MTU = 20,
49         VHOST_USER_SET_SLAVE_REQ_FD = 21,
50         VHOST_USER_IOTLB_MSG = 22,
51         VHOST_USER_CRYPTO_CREATE_SESS = 26,
52         VHOST_USER_CRYPTO_CLOSE_SESS = 27,
53         VHOST_USER_POSTCOPY_ADVISE = 28,
54         VHOST_USER_POSTCOPY_LISTEN = 29,
55         VHOST_USER_POSTCOPY_END = 30,
56         VHOST_USER_MAX = 31
57 } VhostUserRequest;
58
59 typedef enum VhostUserSlaveRequest {
60         VHOST_USER_SLAVE_NONE = 0,
61         VHOST_USER_SLAVE_IOTLB_MSG = 1,
62         VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
63         VHOST_USER_SLAVE_MAX
64 } VhostUserSlaveRequest;
65
66 typedef struct VhostUserMemoryRegion {
67         uint64_t guest_phys_addr;
68         uint64_t memory_size;
69         uint64_t userspace_addr;
70         uint64_t mmap_offset;
71 } VhostUserMemoryRegion;
72
73 typedef struct VhostUserMemory {
74         uint32_t nregions;
75         uint32_t padding;
76         VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
77 } VhostUserMemory;
78
79 typedef struct VhostUserLog {
80         uint64_t mmap_size;
81         uint64_t mmap_offset;
82 } VhostUserLog;
83
84 /* Comply with Cryptodev-Linux */
85 #define VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH   512
86 #define VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH 64
87
88 /* Same structure as vhost-user backend session info */
89 typedef struct VhostUserCryptoSessionParam {
90         int64_t session_id;
91         uint32_t op_code;
92         uint32_t cipher_algo;
93         uint32_t cipher_key_len;
94         uint32_t hash_algo;
95         uint32_t digest_len;
96         uint32_t auth_key_len;
97         uint32_t aad_len;
98         uint8_t op_type;
99         uint8_t dir;
100         uint8_t hash_mode;
101         uint8_t chaining_dir;
102         uint8_t *ciphe_key;
103         uint8_t *auth_key;
104         uint8_t cipher_key_buf[VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH];
105         uint8_t auth_key_buf[VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH];
106 } VhostUserCryptoSessionParam;
107
108 typedef struct VhostUserVringArea {
109         uint64_t u64;
110         uint64_t size;
111         uint64_t offset;
112 } VhostUserVringArea;
113
114 typedef struct VhostUserMsg {
115         union {
116                 uint32_t master; /* a VhostUserRequest value */
117                 uint32_t slave;  /* a VhostUserSlaveRequest value*/
118         } request;
119
120 #define VHOST_USER_VERSION_MASK     0x3
121 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
122 #define VHOST_USER_NEED_REPLY           (0x1 << 3)
123         uint32_t flags;
124         uint32_t size; /* the following payload size */
125         union {
126 #define VHOST_USER_VRING_IDX_MASK   0xff
127 #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
128                 uint64_t u64;
129                 struct vhost_vring_state state;
130                 struct vhost_vring_addr addr;
131                 VhostUserMemory memory;
132                 VhostUserLog    log;
133                 struct vhost_iotlb_msg iotlb;
134                 VhostUserCryptoSessionParam crypto_session;
135                 VhostUserVringArea area;
136         } payload;
137         int fds[VHOST_MEMORY_MAX_NREGIONS];
138         int fd_num;
139 } __attribute((packed)) VhostUserMsg;
140
141 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
142
143 /* The version of the protocol we support */
144 #define VHOST_USER_VERSION    0x1
145
146
147 /* vhost_user.c */
148 int vhost_user_msg_handler(int vid, int fd);
149 int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm);
150 int vhost_user_host_notifier_ctrl(int vid, bool enable);
151
152 /* socket.c */
153 int read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
154                 int *fd_num);
155 int send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num);
156
157 #endif