Enable all vrings if PROTOCOL_FEATURES are not negotiated
[vpp.git] / vnet / vnet / devices / virtio / vhost-user.h
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __VIRTIO_VHOST_USER_H__
16 #define __VIRTIO_VHOST_USER_H__
17 /* vhost-user data structures */
18
19 #define VHOST_MEMORY_MAX_NREGIONS       8
20 #define VHOST_USER_MSG_HDR_SZ           12
21 #define VHOST_VRING_MAX_SIZE            32768
22 #define VHOST_NET_VRING_IDX_RX          0
23 #define VHOST_NET_VRING_IDX_TX          1
24 #define VHOST_NET_VRING_NUM             2
25
26 #define VIRTQ_DESC_F_NEXT               1
27 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
28
29 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
30 #define VHOST_USER_F_PROTOCOL_FEATURES  30
31 #define VHOST_USER_PROTOCOL_F_MQ   0
32 #define VHOST_USER_PROTOCOL_FEATURES   (1ULL << VHOST_USER_PROTOCOL_F_MQ)
33
34 /* If multiqueue is provided by host, then we suppport it. */
35 #define VIRTIO_NET_CTRL_MQ   4
36 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET        0
37 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN        1
38 #define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX        0x8000
39 #endif
40
41 #define foreach_virtio_net_feature      \
42  _ (VIRTIO_NET_F_MRG_RXBUF, 15)         \
43  _ (VIRTIO_F_ANY_LAYOUT, 27)
44
45 typedef enum {
46 #define _(f,n) FEAT_##f = (n),
47   foreach_virtio_net_feature
48 #undef _
49 } virtio_net_feature_t;
50
51 int vhost_user_create_if(vnet_main_t * vnm, vlib_main_t * vm, 
52     const char * sock_filename, u8 is_server,
53     u32 * sw_if_index, u64 feature_mask,
54     u8 renumber, u32 custom_dev_instance, u8 *hwaddr);
55 int vhost_user_modify_if(vnet_main_t * vnm, vlib_main_t * vm,
56     const char * sock_filename, u8 is_server,
57     u32 sw_if_index, u64 feature_mask,
58     u8 renumber, u32 custom_dev_instance);
59 int vhost_user_delete_if(vnet_main_t * vnm, vlib_main_t * vm, u32 sw_if_index);
60
61 typedef struct vhost_user_memory_region {
62   u64 guest_phys_addr;
63   u64 memory_size;
64   u64 userspace_addr;
65   u64 mmap_offset;
66 } vhost_user_memory_region_t;
67
68 typedef struct vhost_user_memory {
69   u32 nregions;
70   u32 padding;
71   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
72 } vhost_user_memory_t;
73
74 typedef struct vhost_vring_state { 
75   unsigned int index, num;
76 } vhost_vring_state_t;
77
78 typedef struct vhost_vring_addr {
79   unsigned int index, flags;
80   u64 desc_user_addr, used_user_addr, avail_user_addr, log_guest_addr;
81 } vhost_vring_addr_t;
82
83 typedef enum vhost_user_req {
84   VHOST_USER_NONE = 0,
85   VHOST_USER_GET_FEATURES = 1,
86   VHOST_USER_SET_FEATURES = 2,
87   VHOST_USER_SET_OWNER = 3,
88   VHOST_USER_RESET_OWNER = 4,
89   VHOST_USER_SET_MEM_TABLE = 5,
90   VHOST_USER_SET_LOG_BASE = 6,
91   VHOST_USER_SET_LOG_FD = 7,
92   VHOST_USER_SET_VRING_NUM = 8,
93   VHOST_USER_SET_VRING_ADDR = 9,
94   VHOST_USER_SET_VRING_BASE = 10,
95   VHOST_USER_GET_VRING_BASE = 11,
96   VHOST_USER_SET_VRING_KICK = 12,
97   VHOST_USER_SET_VRING_CALL = 13,
98   VHOST_USER_SET_VRING_ERR = 14,
99 #if RTE_VERSION >= RTE_VERSION_NUM(2, 2, 0, 0)
100   VHOST_USER_GET_PROTOCOL_FEATURES = 15,
101   VHOST_USER_SET_PROTOCOL_FEATURES = 16,
102   VHOST_USER_GET_QUEUE_NUM = 17,
103   VHOST_USER_SET_VRING_ENABLE = 18,
104 #endif
105   VHOST_USER_MAX
106 } vhost_user_req_t;
107
108 // vring_desc I/O buffer descriptor
109 typedef struct {
110   uint64_t addr;  // packet data buffer address
111   uint32_t len;   // packet data buffer size
112   uint16_t flags; // (see below)
113   uint16_t next;  // optional index next descriptor in chain
114 } __attribute ((packed)) vring_desc_t;
115
116 typedef struct {
117   uint16_t flags;
118   uint16_t idx;
119   uint16_t ring[VHOST_VRING_MAX_SIZE];
120 } __attribute ((packed)) vring_avail_t;
121
122 typedef struct {
123   uint16_t flags;
124   uint16_t idx;
125   struct /* vring_used_elem */ {
126     uint32_t id; 
127     uint32_t len; 
128   } ring[VHOST_VRING_MAX_SIZE];
129 } __attribute ((packed)) vring_used_t;
130
131 typedef struct {
132   u8 flags;
133   u8 gso_type;
134   u16 hdr_len;
135   u16 gso_size;
136   u16 csum_start;
137   u16 csum_offset;
138 } __attribute ((packed)) virtio_net_hdr_t;
139
140 typedef struct  {
141   virtio_net_hdr_t hdr;
142   u16 num_buffers;
143 } __attribute ((packed)) virtio_net_hdr_mrg_rxbuf_t;
144
145 typedef struct vhost_user_msg {
146     vhost_user_req_t request;
147     u32 flags;
148     u32 size;
149     union {
150         u64 u64;
151         vhost_vring_state_t state;
152         vhost_vring_addr_t addr;
153         vhost_user_memory_t memory;
154     };
155 } __attribute ((packed)) vhost_user_msg_t;
156
157 typedef struct {
158   u32 qsz;
159   u16 last_avail_idx;
160   u16 last_used_idx;
161   vring_desc_t *desc;
162   vring_avail_t *avail;
163   vring_used_t *used;
164   int callfd;
165   int kickfd;
166   int errfd;
167   u32 callfd_idx;
168   u32 n_since_last_int;
169   f64 int_deadline;
170 } vhost_user_vring_t;
171
172 typedef struct {
173   CLIB_CACHE_LINE_ALIGN_MARK(cacheline0);
174   volatile u32 * lockp;
175   u32 is_up;
176   u32 admin_up;
177   u32 unix_fd;
178   u32 unix_file_index;
179   u32 client_fd;
180   char sock_filename[256];
181   int sock_errno;
182   u8 sock_is_server;
183   u32 hw_if_index, sw_if_index;
184   u8 active;
185   
186   u32 nregions;
187   u64 features;
188   u64 feature_mask;
189   u32 num_vrings;
190   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
191   void * region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
192   u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];
193   vhost_user_vring_t vrings[2];
194   int virtio_net_hdr_sz;
195   int is_any_layout;
196   u32 * d_trace_buffers;
197 } vhost_user_intf_t;
198
199 typedef struct {
200   u32 ** rx_buffers;
201   u32 mtu_bytes;
202   vhost_user_intf_t * vhost_user_interfaces;
203   u32 * vhost_user_inactive_interfaces_index;
204   uword * vhost_user_interface_index_by_listener_fd;
205   uword * vhost_user_interface_index_by_sock_fd;
206   uword * vhost_user_interface_index_by_sw_if_index;
207   u32 * show_dev_instance_by_real_dev_instance;
208   u32 coalesce_frames;
209   f64 coalesce_time;
210   int dont_dump_vhost_user_memory;
211 } vhost_user_main_t;
212
213 typedef struct {
214     u8 if_name[64];
215     u32 sw_if_index;
216     u32 virtio_net_hdr_sz;
217     u64 features;
218     u8 is_server;
219     u8 sock_filename[256];
220     u32 num_regions;
221     int sock_errno;
222 } vhost_user_intf_details_t;
223
224 int vhost_user_dump_ifs(vnet_main_t * vnm, vlib_main_t * vm,
225         vhost_user_intf_details_t **out_vuids);
226
227 // CLI commands to be used from dpdk
228 clib_error_t *
229 vhost_user_connect_command_fn (vlib_main_t * vm,
230                  unformat_input_t * input,
231                  vlib_cli_command_t * cmd);
232 clib_error_t *
233 vhost_user_delete_command_fn (vlib_main_t * vm,
234                  unformat_input_t * input,
235                  vlib_cli_command_t * cmd);
236 clib_error_t *
237 show_vhost_user_command_fn (vlib_main_t * vm,
238                  unformat_input_t * input,
239                  vlib_cli_command_t * cmd);
240
241 #endif