wireguard: add async mode for encryption packets
[vpp.git] / src / vnet / devices / virtio / virtio.h
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #ifndef _VNET_DEVICES_VIRTIO_VIRTIO_H_
19 #define _VNET_DEVICES_VIRTIO_VIRTIO_H_
20
21 #include <vnet/devices/virtio/virtio_std.h>
22 #include <vnet/devices/virtio/vhost_std.h>
23 #include <vnet/devices/virtio/virtio_buffering.h>
24 #include <vnet/gso/gro.h>
25
26 #define foreach_virtio_if_flag          \
27   _(0, ADMIN_UP, "admin-up")            \
28   _(1, DELETING, "deleting")
29
30 typedef enum
31 {
32 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
33   foreach_virtio_if_flag
34 #undef _
35 } virtio_if_flag_t;
36
37 #define TX_QUEUE(X) ((X*2) + 1)
38 #define RX_QUEUE(X) (X*2)
39 #define TX_QUEUE_ACCESS(X) (X/2)
40 #define RX_QUEUE_ACCESS(X) (X/2)
41
42 #define VIRTIO_NUM_RX_DESC 256
43 #define VIRTIO_NUM_TX_DESC 256
44
45 #define foreach_virtio_if_types \
46   _ (TAP, 0)                    \
47   _ (TUN, 1)                    \
48   _ (PCI, 2)
49
50 typedef enum
51 {
52 #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
53   foreach_virtio_if_types
54 #undef _
55     VIRTIO_IF_N_TYPES = (1 << 3),
56 } virtio_if_type_t;
57
58 #define VIRTIO_RING_FLAG_MASK_INT 1
59
60 #define VIRTIO_EVENT_START_TIMER 1
61 #define VIRTIO_EVENT_STOP_TIMER 2
62
63 typedef struct
64 {
65   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
66   clib_spinlock_t lockp;
67   union
68   {
69     struct
70     {
71       vring_desc_t *desc;
72       vring_used_t *used;
73       vring_avail_t *avail;
74     };
75     struct
76     {
77       vring_packed_desc_t *packed_desc;
78       vring_desc_event_t *driver_event;
79       vring_desc_event_t *device_event;
80     };
81   };
82   u32 *buffers;
83   u16 size;
84   u16 queue_id;
85   u32 queue_index;
86   u16 desc_in_use;
87   u16 desc_next;
88   u16 last_used_idx;
89   u16 last_kick_avail_idx;
90   union
91   {
92     struct
93     {
94       int kick_fd;
95       int call_fd;
96       u32 call_file_index;
97     };
98     struct
99     {
100       u16 avail_wrap_counter;
101       u16 used_wrap_counter;
102       u16 queue_notify_offset;
103     };
104   };
105 #define VRING_TX_OUT_OF_ORDER 1
106 #define VRING_TX_SCHEDULED    2
107   u16 flags;
108   u8 buffer_pool_index;
109   vnet_hw_if_rx_mode mode;
110   virtio_vring_buffering_t *buffering;
111   gro_flow_table_t *flow_table;
112 } virtio_vring_t;
113
114 typedef union
115 {
116   struct
117   {
118     u16 domain;
119     u8 bus;
120     u8 slot:5;
121     u8 function:3;
122   };
123   u32 as_u32;
124 } pci_addr_t;
125
126 /* forward declaration */
127 typedef struct _virtio_pci_func virtio_pci_func_t;
128
129 typedef struct
130 {
131   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
132   u64 features;
133   u32 flags;
134   u32 per_interface_next_index;
135   u16 num_rxqs;
136   u16 num_txqs;
137   virtio_vring_t *rxq_vrings;
138   virtio_vring_t *txq_vrings;
139   int gso_enabled;
140   int csum_offload_enabled;
141   union
142   {
143     int *tap_fds;
144     struct
145     {
146       u32 pci_dev_handle;
147       u32 msix_enabled;
148     };
149   };
150   u16 virtio_net_hdr_sz;
151   virtio_if_type_t type;
152
153   u32 hw_if_index;
154   u32 sw_if_index;
155
156     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
157   int packet_coalesce;
158   int packet_buffering;
159   u32 dev_instance;
160   u32 numa_node;
161   u64 remote_features;
162
163   /* error */
164   clib_error_t *error;
165   union
166   {
167     struct
168     {
169       u32 mac_addr32;
170       u16 mac_addr16;
171     };
172     u8 mac_addr[6];
173   };
174   union
175   {
176     struct                      /* tun/tap interface */
177     {
178       ip6_address_t host_ip6_addr;
179       int *vhost_fds;
180       u8 *host_if_name;
181       u8 *net_ns;
182       u8 *host_bridge;
183       u8 host_mac_addr[6];
184       u32 id;
185       u32 host_mtu_size;
186       u32 tap_flags;
187       int ifindex;
188       ip4_address_t host_ip4_addr;
189       u8 host_ip4_prefix_len;
190       u8 host_ip6_prefix_len;
191       u8 host_carrier_up;       /* host tun/tap driver link carrier state */
192     };
193     struct                      /* native virtio */
194     {
195       void *bar;
196       virtio_vring_t *cxq_vring;
197       pci_addr_t pci_addr;
198       u32 bar_id;
199       u32 notify_off_multiplier;
200       u32 is_modern;
201       u16 common_offset;
202       u16 notify_offset;
203       u16 device_offset;
204       u16 isr_offset;
205       u16 max_queue_pairs;
206       u16 msix_table_size;
207       u8 support_int_mode;      /* support interrupt mode */
208       u8 status;
209     };
210   };
211   const virtio_pci_func_t *virtio_pci_func;
212   int is_packed;
213 } virtio_if_t;
214
215 typedef struct
216 {
217   u32 gro_or_buffering_if_count;
218   /* logging */
219   vlib_log_class_t log_default;
220
221   virtio_if_t *interfaces;
222   u16 msg_id_base;
223 } virtio_main_t;
224
225 extern virtio_main_t virtio_main;
226 extern vnet_device_class_t virtio_device_class;
227 extern vlib_node_registration_t virtio_input_node;
228
229 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
230                                  u16 sz);
231 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
232                                     u32 idx);
233 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
234                                     u32 idx);
235 void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
236 void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif);
237 extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
238 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
239 extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
240                          virtio_if_type_t type);
241 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
242 clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
243 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
244                                             virtio_if_t * vif, u16 queue_id,
245                                             u16 queue_notify_offset);
246 extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
247                                             virtio_if_t * vif, u16 queue_id,
248                                             u16 queue_notify_offset);
249 extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif);
250 extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif);
251
252 format_function_t format_virtio_device_name;
253 format_function_t format_virtio_log_name;
254
255 static_always_inline void
256 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
257 {
258   if (vif->type == VIRTIO_IF_TYPE_PCI)
259     {
260       if (vif->is_modern)
261         virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
262                                         vring->queue_notify_offset);
263       else
264         virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
265                                         vring->queue_notify_offset);
266     }
267   else
268     {
269       u64 x = 1;
270       int __clib_unused r;
271
272       r = write (vring->kick_fd, &x, sizeof (x));
273       vring->last_kick_avail_idx = vring->avail->idx;
274     }
275 }
276
277 static_always_inline u8
278 virtio_txq_is_scheduled (virtio_vring_t *vring)
279 {
280   if (vring)
281     return (vring->flags & VRING_TX_SCHEDULED);
282   return 1;
283 }
284
285 static_always_inline void
286 virtio_txq_set_scheduled (virtio_vring_t *vring)
287 {
288   if (vring)
289     vring->flags |= VRING_TX_SCHEDULED;
290 }
291
292 static_always_inline void
293 virtio_txq_clear_scheduled (virtio_vring_t *vring)
294 {
295   if (vring)
296     vring->flags &= ~VRING_TX_SCHEDULED;
297 }
298
299 #define virtio_log_debug(vif, f, ...)                           \
300 {                                                               \
301   vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default,       \
302            "%U: " f, format_virtio_log_name, vif,               \
303            ##__VA_ARGS__);                                      \
304 };
305
306 #define virtio_log_warning(vif, f, ...)                         \
307 {                                                               \
308   vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default,     \
309            "%U: " f, format_virtio_log_name, vif,               \
310            ##__VA_ARGS__);                                      \
311 };
312
313 #define virtio_log_error(vif, f, ...)                           \
314 {                                                               \
315   vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default,         \
316            "%U: " f, format_virtio_log_name, vif,               \
317            ##__VA_ARGS__);                                      \
318 };
319
320 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
321
322 /*
323  * fd.io coding-style-patch-verification: ON
324  *
325  * Local Variables:
326  * eval: (c-set-style "gnu")
327  * End:
328  */