New upstream version 18.08
[deb_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_MAX = 28
54 } VhostUserRequest;
55
56 typedef enum VhostUserSlaveRequest {
57         VHOST_USER_SLAVE_NONE = 0,
58         VHOST_USER_SLAVE_IOTLB_MSG = 1,
59         VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
60         VHOST_USER_SLAVE_MAX
61 } VhostUserSlaveRequest;
62
63 typedef struct VhostUserMemoryRegion {
64         uint64_t guest_phys_addr;
65         uint64_t memory_size;
66         uint64_t userspace_addr;
67         uint64_t mmap_offset;
68 } VhostUserMemoryRegion;
69
70 typedef struct VhostUserMemory {
71         uint32_t nregions;
72         uint32_t padding;
73         VhostUserMemoryRegion regions[VHOST_MEMORY_MAX_NREGIONS];
74 } VhostUserMemory;
75
76 typedef struct VhostUserLog {
77         uint64_t mmap_size;
78         uint64_t mmap_offset;
79 } VhostUserLog;
80
81 /* Comply with Cryptodev-Linux */
82 #define VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH   512
83 #define VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH 64
84
85 /* Same structure as vhost-user backend session info */
86 typedef struct VhostUserCryptoSessionParam {
87         int64_t session_id;
88         uint32_t op_code;
89         uint32_t cipher_algo;
90         uint32_t cipher_key_len;
91         uint32_t hash_algo;
92         uint32_t digest_len;
93         uint32_t auth_key_len;
94         uint32_t aad_len;
95         uint8_t op_type;
96         uint8_t dir;
97         uint8_t hash_mode;
98         uint8_t chaining_dir;
99         uint8_t *ciphe_key;
100         uint8_t *auth_key;
101         uint8_t cipher_key_buf[VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH];
102         uint8_t auth_key_buf[VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH];
103 } VhostUserCryptoSessionParam;
104
105 typedef struct VhostUserVringArea {
106         uint64_t u64;
107         uint64_t size;
108         uint64_t offset;
109 } VhostUserVringArea;
110
111 typedef struct VhostUserMsg {
112         union {
113                 uint32_t master; /* a VhostUserRequest value */
114                 uint32_t slave;  /* a VhostUserSlaveRequest value*/
115         } request;
116
117 #define VHOST_USER_VERSION_MASK     0x3
118 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
119 #define VHOST_USER_NEED_REPLY           (0x1 << 3)
120         uint32_t flags;
121         uint32_t size; /* the following payload size */
122         union {
123 #define VHOST_USER_VRING_IDX_MASK   0xff
124 #define VHOST_USER_VRING_NOFD_MASK  (0x1<<8)
125                 uint64_t u64;
126                 struct vhost_vring_state state;
127                 struct vhost_vring_addr addr;
128                 VhostUserMemory memory;
129                 VhostUserLog    log;
130                 struct vhost_iotlb_msg iotlb;
131                 VhostUserCryptoSessionParam crypto_session;
132                 VhostUserVringArea area;
133         } payload;
134         int fds[VHOST_MEMORY_MAX_NREGIONS];
135 } __attribute((packed)) VhostUserMsg;
136
137 #define VHOST_USER_HDR_SIZE offsetof(VhostUserMsg, payload.u64)
138
139 /* The version of the protocol we support */
140 #define VHOST_USER_VERSION    0x1
141
142
143 /* vhost_user.c */
144 int vhost_user_msg_handler(int vid, int fd);
145 int vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm);
146 int vhost_user_host_notifier_ctrl(int vid, bool enable);
147
148 /* socket.c */
149 int read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num);
150 int send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num);
151
152 #endif