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