nat: Include platform specific headers on FreeBSD
[vpp.git] / src / plugins / vhost / 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 <vhost/virtio_std.h>
19 #include <vhost/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
101 typedef struct
102 {
103   char *sock_filename;
104   u64 feature_mask;
105   u32 custom_dev_instance;
106   u8 hwaddr[6];
107   u8 renumber;
108   u8 is_server;
109   u8 enable_gso;
110   u8 enable_packed;
111   u8 enable_event_idx;
112   u8 use_custom_mac;
113
114   /* return */
115   u32 sw_if_index;
116 } vhost_user_create_if_args_t;
117
118 int vhost_user_create_if (vnet_main_t * vnm, vlib_main_t * vm,
119                           vhost_user_create_if_args_t * args);
120 int vhost_user_modify_if (vnet_main_t * vnm, vlib_main_t * vm,
121                           vhost_user_create_if_args_t * args);
122 int vhost_user_delete_if (vnet_main_t * vnm, vlib_main_t * vm,
123                           u32 sw_if_index);
124
125 typedef struct vhost_user_memory_region
126 {
127   u64 guest_phys_addr;
128   u64 memory_size;
129   u64 userspace_addr;
130   u64 mmap_offset;
131 } __attribute ((packed)) vhost_user_memory_region_t;
132
133 typedef struct vhost_user_memory
134 {
135   u32 nregions;
136   u32 padding;
137   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
138 } __attribute ((packed)) vhost_user_memory_t;
139
140 typedef enum vhost_user_req
141 {
142   VHOST_USER_NONE = 0,
143   VHOST_USER_GET_FEATURES = 1,
144   VHOST_USER_SET_FEATURES = 2,
145   VHOST_USER_SET_OWNER = 3,
146   VHOST_USER_RESET_OWNER = 4,
147   VHOST_USER_SET_MEM_TABLE = 5,
148   VHOST_USER_SET_LOG_BASE = 6,
149   VHOST_USER_SET_LOG_FD = 7,
150   VHOST_USER_SET_VRING_NUM = 8,
151   VHOST_USER_SET_VRING_ADDR = 9,
152   VHOST_USER_SET_VRING_BASE = 10,
153   VHOST_USER_GET_VRING_BASE = 11,
154   VHOST_USER_SET_VRING_KICK = 12,
155   VHOST_USER_SET_VRING_CALL = 13,
156   VHOST_USER_SET_VRING_ERR = 14,
157   VHOST_USER_GET_PROTOCOL_FEATURES = 15,
158   VHOST_USER_SET_PROTOCOL_FEATURES = 16,
159   VHOST_USER_GET_QUEUE_NUM = 17,
160   VHOST_USER_SET_VRING_ENABLE = 18,
161   VHOST_USER_MAX
162 } vhost_user_req_t;
163
164 typedef struct vhost_user_msg {
165   vhost_user_req_t request;
166   u32 flags;
167   u32 size;
168   union
169     {
170       u64 u64;
171       vhost_vring_state_t state;
172       vhost_vring_addr_t addr;
173       vhost_user_memory_t memory;
174       vhost_user_log_t log;
175     };
176 } __attribute ((packed)) vhost_user_msg_t;
177
178 typedef struct
179 {
180   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
181   u16 qsz_mask;
182   u16 last_avail_idx;
183   u16 last_used_idx;
184   u16 n_since_last_int;
185   union
186   {
187     vnet_virtio_vring_desc_t *desc;
188     vnet_virtio_vring_packed_desc_t *packed_desc;
189   };
190   union
191   {
192     vnet_virtio_vring_avail_t *avail;
193     vnet_virtio_vring_desc_event_t *avail_event;
194   };
195   union
196   {
197     vnet_virtio_vring_used_t *used;
198     vnet_virtio_vring_desc_event_t *used_event;
199   };
200   uword desc_user_addr;
201   uword used_user_addr;
202   uword avail_user_addr;
203   f64 int_deadline;
204   u8 started;
205   u8 enabled;
206   u8 log_used;
207   clib_spinlock_t vring_lock;
208
209   //Put non-runtime in a different cache line
210     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
211   int errfd;
212   u32 callfd_idx;
213   u32 kickfd_idx;
214   u64 log_guest_addr;
215
216   /* The rx queue policy (interrupt/adaptive/polling) for this queue */
217   u32 mode;
218
219   /*
220    * It contains the device queue number. -1 if it does not. The idea is
221    * to not invoke vnet_hw_interface_assign_rx_thread and
222    * vnet_hw_interface_unassign_rx_thread more than once for the duration of
223    * the interface even if it is disconnected and reconnected.
224    */
225   i16 qid;
226
227   u16 used_wrap_counter;
228   u16 avail_wrap_counter;
229   u16 last_kick;
230   u8 first_kick;
231   u32 queue_index;
232   u32 thread_index;
233 } vhost_user_vring_t;
234
235 #define VHOST_USER_EVENT_START_TIMER 1
236 #define VHOST_USER_EVENT_STOP_TIMER  2
237
238 typedef struct
239 {
240   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
241   u32 is_ready;
242   u32 admin_up;
243   u32 unix_server_index;
244   u32 clib_file_index;
245   char sock_filename[256];
246   int sock_errno;
247   uword if_index;
248   u32 hw_if_index, sw_if_index;
249
250   //Feature negotiation
251   u64 features;
252   u64 feature_mask;
253   u64 protocol_features;
254
255   //Memory region information
256   u32 nregions;
257   vhost_user_memory_region_t regions[VHOST_MEMORY_MAX_NREGIONS];
258   void *region_mmap_addr[VHOST_MEMORY_MAX_NREGIONS];
259   u64 region_guest_addr_lo[VHOST_MEMORY_MAX_NREGIONS];
260   u64 region_guest_addr_hi[VHOST_MEMORY_MAX_NREGIONS];
261   u32 region_mmap_fd[VHOST_MEMORY_MAX_NREGIONS];
262
263   //Virtual rings
264   vhost_user_vring_t *vrings;
265
266   /*
267    * vrings is a dynamic array. It may have more elements than it is
268    * currently used. num_qid indicates the current total qid's in the
269    * vrings. For example, vec_len(vrings) = 64, num_qid = 60, so the
270    * current valid/used qid is (0, 59) in the vrings array.
271    */
272   u32 num_qid;
273
274   int virtio_net_hdr_sz;
275   int is_any_layout;
276
277   void *log_base_addr;
278   u64 log_size;
279
280   u8 enable_gso;
281
282   /* Packed ring configured */
283   u8 enable_packed;
284
285   u8 enable_event_idx;
286 } vhost_user_intf_t;
287
288 #define FOR_ALL_VHOST_TXQ(qid, vui) for (qid = 1; qid < vui->num_qid; qid += 2)
289
290 #define FOR_ALL_VHOST_RXQ(qid, vui) for (qid = 0; qid < vui->num_qid; qid += 2)
291
292 #define FOR_ALL_VHOST_RX_TXQ(qid, vui) for (qid = 0; qid < vui->num_qid; qid++)
293
294 typedef struct
295 {
296   uword dst;
297   uword src;
298   u32 len;
299 } vhost_copy_t;
300
301 typedef struct
302 {
303   u16 qid; /** The interface queue index (Not the virtio vring idx) */
304   u16 device_index; /** The device index */
305   u32 virtio_ring_flags; /** Runtime queue flags  **/
306   u16 first_desc_len; /** Length of the first data descriptor **/
307   vnet_virtio_net_hdr_mrg_rxbuf_t hdr; /** Virtio header **/
308 } vhost_trace_t;
309
310 #define VHOST_USER_RX_BUFFERS_N (2 * VLIB_FRAME_SIZE + 2)
311 #define VHOST_USER_COPY_ARRAY_N (4 * VLIB_FRAME_SIZE)
312
313 typedef struct
314 {
315   u32 rx_buffers_len;
316   u32 rx_buffers[VHOST_USER_RX_BUFFERS_N];
317
318   vnet_virtio_net_hdr_mrg_rxbuf_t tx_headers[VLIB_FRAME_SIZE];
319   vhost_copy_t copy[VHOST_USER_COPY_ARRAY_N];
320
321   /* This is here so it doesn't end-up
322    * using stack or registers. */
323   vhost_trace_t *current_trace;
324
325   u32 *to_next_list;
326   vlib_buffer_t **rx_buffers_pdesc;
327   u32 polling_q_count;
328 } vhost_cpu_t;
329
330 typedef struct
331 {
332   mhash_t if_index_by_sock_name;
333   u32 mtu_bytes;
334   vhost_user_intf_t *vhost_user_interfaces;
335   u32 *show_dev_instance_by_real_dev_instance;
336   u32 coalesce_frames;
337   f64 coalesce_time;
338   int dont_dump_vhost_user_memory;
339
340   /** Per-CPU data for vhost-user */
341   vhost_cpu_t *cpus;
342
343   /** Pseudo random iterator */
344   u32 random;
345
346   /* The number of rx interface/queue pairs in interrupt mode */
347   u32 ifq_count;
348
349   /* logging */
350   vlib_log_class_t log_default;
351
352   /* gso interface count */
353   u32 gso_count;
354 } vhost_user_main_t;
355
356 typedef struct
357 {
358   u8 if_name[64];
359   u32 sw_if_index;
360   u32 virtio_net_hdr_sz;
361   u64 features;
362   u8 is_server;
363   u8 sock_filename[256];
364   u32 num_regions;
365   int sock_errno;
366 } vhost_user_intf_details_t;
367
368 int vhost_user_dump_ifs (vnet_main_t * vnm, vlib_main_t * vm,
369                          vhost_user_intf_details_t ** out_vuids);
370 void vhost_user_set_operation_mode (vhost_user_intf_t *vui,
371                                     vhost_user_vring_t *txvq);
372
373 extern vlib_node_registration_t vhost_user_send_interrupt_node;
374 extern vnet_device_class_t vhost_user_device_class;
375 extern vlib_node_registration_t vhost_user_input_node;
376 extern vhost_user_main_t vhost_user_main;
377
378 #endif
379
380 /*
381  * fd.io coding-style-patch-verification: ON
382  *
383  * Local Variables:
384  * eval: (c-set-style "gnu")
385  * End:
386  */