virtio: Extend vhost multi-queues support beyond 8 queue pairs
[vpp.git] / src / 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
18 #include <vnet/devices/virtio/virtio_std.h>
19 #include <vnet/devices/virtio/vhost_std.h>
20
21 /* vhost-user data structures */
22
23 #define VHOST_MEMORY_MAX_NREGIONS       8
24 #define VHOST_USER_MSG_HDR_SZ           12
25 #define VHOST_VRING_INIT_MQ_PAIR_SZ     8       //8TX + 8RX
26
27 /*
28  * qid is one byte in size in the spec. Please see VHOST_USER_SET_VRING_CALL,
29  * VHOST_USER_SET_VRING_KICK, and VHOST_USER_SET_VRING_ERR.
30  * The max number for q pair is naturally 128.
31  */
32 #define VHOST_VRING_MAX_MQ_PAIR_SZ      128
33 #define VHOST_VRING_IDX_RX(qid)         (2*qid)
34 #define VHOST_VRING_IDX_TX(qid)         (2*qid + 1)
35
36 #define VHOST_USER_VRING_NOFD_MASK      0x100
37
38 #define VHOST_USER_PROTOCOL_F_MQ   0
39 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
40 #define VHOST_VRING_F_LOG 0
41
42 #define VHOST_USER_PROTOCOL_FEATURES   ((1ULL << VHOST_USER_PROTOCOL_F_MQ) |    \
43                                         (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
44
45 #define vu_log_debug(dev, f, ...) \
46 {                                                                             \
47   vlib_log(VLIB_LOG_LEVEL_DEBUG, vhost_user_main.log_default, "%U: " f,       \
48            format_vnet_hw_if_index_name, vnet_get_main(),                     \
49            dev->hw_if_index, ##__VA_ARGS__);                                  \
50 };
51
52 #define vu_log_warn(dev, f, ...) \
53 {                                                                             \
54   vlib_log(VLIB_LOG_LEVEL_WARNING, vhost_user_main.log_default, "%U: " f,     \
55            format_vnet_hw_if_index_name, vnet_get_main(),                     \
56            dev->hw_if_index, ##__VA_ARGS__);                                  \
57 };
58 #define vu_log_err(dev, f, ...) \
59 {                                                                             \
60   vlib_log(VLIB_LOG_LEVEL_ERR, vhost_user_main.log_default, "%U: " f,         \
61            format_vnet_hw_if_index_name, vnet_get_main(),                     \
62            dev->hw_if_index, ##__VA_ARGS__);                                  \
63 };
64
65 #define UNIX_GET_FD(unixfd_idx) ({ \
66     typeof(unixfd_idx) __unixfd_idx = (unixfd_idx); \
67     (__unixfd_idx != ~0) ? \
68         pool_elt_at_index (file_main.file_pool, \
69                            __unixfd_idx)->file_descriptor : -1; })
70
71 #define foreach_virtio_trace_flags \
72   _ (SIMPLE_CHAINED, 0, "Simple descriptor chaining") \
73   _ (SINGLE_DESC,  1, "Single descriptor packet") \
74   _ (INDIRECT, 2, "Indirect descriptor") \
75   _ (MAP_ERROR, 4, "Memory mapping error")
76
77 typedef enum
78 {
79 #define _(n,i,s) VIRTIO_TRACE_F_##n,
80   foreach_virtio_trace_flags
81 #undef _
82 } virtio_trace_flag_t;
83
84 #define FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS \
85   (VIRTIO_FEATURE (VIRTIO_NET_F_CSUM) |            \
86    VIRTIO_FEATURE (VIRTIO_NET_F_HOST_UFO) |        \
87    VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO4) |       \
88    VIRTIO_FEATURE (VIRTIO_NET_F_HOST_TSO6))
89
90 #define FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS \
91   (VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_CSUM) |       \
92    VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_UFO) |        \
93    VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO4) |       \
94    VIRTIO_FEATURE (VIRTIO_NET_F_GUEST_TSO6))
95
96 #define FEATURE_VIRTIO_NET_F_HOST_GUEST_TSO_FEATURE_BITS \
97   (FEATURE_VIRTIO_NET_F_HOST_TSO_FEATURE_BITS |          \
98    FEATURE_VIRTIO_NET_F_GUEST_TSO_FEATURE_BITS)
99
100 int vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
101                           const char *sock_filename, u8 is_server,
102                           u32 * sw_if_index, u64 feature_mask,
103                           u8 renumber, u32 custom_dev_instance, u8 * hwaddr,
104                           u8 enable_gso, u8 enable_packed);
105 int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
106                           const char *sock_filename, u8 is_server,
107                           u32 sw_if_index, u64 feature_mask,
108                           u8 renumber, u32 custom_dev_instance,
109                           u8 enable_gso, u8 enable_packed);
110 int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
111                           u32 sw_if_index);
112
113 /* *INDENT-OFF* */
114 typedef struct vhost_user_memory_region
115 {
116   u64 guest_phys_addr;
117   u64 memory_size;
118   u64 userspace_addr;
119   u64 mmap_offset;
120 } __attribute ((packed)) vhost_user_memory_region_t;
121
122 typedef struct vhost_user_memory
123 {
124   u32 nregions;
125   u32 padding;
126   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
127 } __attribute ((packed)) vhost_user_memory_t;
128
129 typedef enum vhost_user_req
130 {
131   VHOST_USER_NONE = 0,
132   VHOST_USER_GET_FEATURES = 1,
133   VHOST_USER_SET_FEATURES = 2,
134   VHOST_USER_SET_OWNER = 3,
135   VHOST_USER_RESET_OWNER = 4,
136   VHOST_USER_SET_MEM_TABLE = 5,
137   VHOST_USER_SET_LOG_BASE = 6,
138   VHOST_USER_SET_LOG_FD = 7,
139   VHOST_USER_SET_VRING_NUM = 8,
140   VHOST_USER_SET_VRING_ADDR = 9,
141   VHOST_USER_SET_VRING_BASE = 10,
142   VHOST_USER_GET_VRING_BASE = 11,
143   VHOST_USER_SET_VRING_KICK = 12,
144   VHOST_USER_SET_VRING_CALL = 13,
145   VHOST_USER_SET_VRING_ERR = 14,
146   VHOST_USER_GET_PROTOCOL_FEATURES = 15,
147   VHOST_USER_SET_PROTOCOL_FEATURES = 16,
148   VHOST_USER_GET_QUEUE_NUM = 17,
149   VHOST_USER_SET_VRING_ENABLE = 18,
150   VHOST_USER_MAX
151 } vhost_user_req_t;
152
153 typedef struct vhost_user_msg {
154   vhost_user_req_t request;
155   u32 flags;
156   u32 size;
157   union
158     {
159       u64 u64;
160       vhost_vring_state_t state;
161       vhost_vring_addr_t addr;
162       vhost_user_memory_t memory;
163       vhost_user_log_t log;
164     };
165 } __attribute ((packed)) vhost_user_msg_t;
166 /* *INDENT-ON* */
167
168 typedef struct
169 {
170   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
171   u16 qsz_mask;
172   u16 last_avail_idx;
173   u16 last_used_idx;
174   u16 n_since_last_int;
175   union
176   {
177     vring_desc_t *desc;
178     vring_packed_desc_t *packed_desc;
179   };
180   union
181   {
182     vring_avail_t *avail;
183     vring_desc_event_t *avail_event;
184   };
185   union
186   {
187     vring_used_t *used;
188     vring_desc_event_t *used_event;
189   };
190   uword desc_user_addr;
191   uword used_user_addr;
192   uword avail_user_addr;
193   f64 int_deadline;
194   u8 started;
195   u8 enabled;
196   u8 log_used;
197   clib_spinlock_t vring_lock;
198
199   //Put non-runtime in a different cache line
200     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
201   int errfd;
202   u32 callfd_idx;
203   u32 kickfd_idx;
204   u64 log_guest_addr;
205
206   /* The rx queue policy (interrupt/adaptive/polling) for this queue */
207   u32 mode;
208
209   /*
210    * It contains the device queue number. -1 if it does not. The idea is
211    * to not invoke vnet_hw_interface_assign_rx_thread and
212    * vnet_hw_interface_unassign_rx_thread more than once for the duration of
213    * the interface even if it is disconnected and reconnected.
214    */
215   i16 qid;
216
217   u16 used_wrap_counter;
218   u16 avail_wrap_counter;
219 } vhost_user_vring_t;
220
221 #define VHOST_USER_EVENT_START_TIMER 1
222 #define VHOST_USER_EVENT_STOP_TIMER  2
223
224 typedef struct
225 {
226   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
227   u32 is_ready;
228   u32 admin_up;
229   u32 unix_server_index;
230   u32 clib_file_index;
231   char sock_filename[256];
232   int sock_errno;
233   uword if_index;
234   u32 hw_if_index, sw_if_index;
235
236   //Feature negotiation
237   u64 features;
238   u64 feature_mask;
239   u64 protocol_features;
240
241   //Memory region information
242   u32 nregions;
243   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
244   void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
245   u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS];
246   u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS];
247   u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];
248
249   //Virtual rings
250   vhost_user_vring_t *vrings;
251
252   /*
253    * vrings is a dynamic array. It may have more elements than it is
254    * currently used. num_qid indicates the current total qid's in the
255    * vrings. For example, vec_len(vrings) = 64, num_qid = 60, so the
256    * current valid/used qid is (0, 59) in the vrings array.
257    */
258   u32 num_qid;
259
260   int virtio_net_hdr_sz;
261   int is_any_layout;
262
263   void *log_base_addr;
264   u64 log_size;
265
266   /* Whether to use spinlock or per_cpu_tx_qid assignment */
267   u8 use_tx_spinlock;
268   u16 *per_cpu_tx_qid;
269
270   u8 enable_gso;
271
272   /* Packed ring configured */
273   u8 enable_packed;
274
275 } vhost_user_intf_t;
276
277 typedef struct
278 {
279   uword dst;
280   uword src;
281   u32 len;
282 } vhost_copy_t;
283
284 typedef struct
285 {
286   u16 qid; /** The interface queue index (Not the virtio vring idx) */
287   u16 device_index; /** The device index */
288   u32 virtio_ring_flags; /** Runtime queue flags  **/
289   u16 first_desc_len; /** Length of the first data descriptor **/
290   virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/
291 } vhost_trace_t;
292
293 #define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2)
294 #define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE)
295
296 typedef struct
297 {
298   u32 rx_buffers_len;
299   u32 rx_buffers[VHOST_USER_RX_BUFFERS_N];
300
301   virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE];
302   vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N];
303
304   /* This is here so it doesn't end-up
305    * using stack or registers. */
306   vhost_trace_t *current_trace;
307
308   u32 *to_next_list;
309   vlib_buffer_t **rx_buffers_pdesc;
310 } vhost_cpu_t;
311
312 typedef struct
313 {
314   mhash_t if_index_by_sock_name;
315   u32 mtu_bytes;
316   vhost_user_intf_t *vhost_user_interfaces;
317   u32 *show_dev_instance_by_real_dev_instance;
318   u32 coalesce_frames;
319   f64 coalesce_time;
320   int dont_dump_vhost_user_memory;
321
322   /** Per-CPU data for vhost-user */
323   vhost_cpu_t *cpus;
324
325   /** Pseudo random iterator */
326   u32 random;
327
328   /* The number of rx interface/queue pairs in interrupt mode */
329   u32 ifq_count;
330
331   /* logging */
332   vlib_log_class_t log_default;
333
334   /* gso interface count */
335   u32 gso_count;
336 } vhost_user_main_t;
337
338 typedef struct
339 {
340   u8 if_name[64];
341   u32 sw_if_index;
342   u32 virtio_net_hdr_sz;
343   u64 features;
344   u8 is_server;
345   u8 sock_filename[256];
346   u32 num_regions;
347   int sock_errno;
348 } vhost_user_intf_details_t;
349
350 int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
351                          vhost_user_intf_details_t ** out_vuids);
352
353 extern vlib_node_registration_t vhost_user_send_interrupt_node;
354 extern vnet_device_class_t vhost_user_device_class;
355 extern vlib_node_registration_t vhost_user_input_node;
356 extern vhost_user_main_t vhost_user_main;
357
358 #endif
359
360 /*
361  * fd.io coding-style-patch-verification: ON
362  *
363  * Local Variables:
364  * eval: (c-set-style "gnu")
365  * End:
366  */