virtio: add packet buffering on tx
[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 typedef struct
61 {
62   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
63   clib_spinlock_t lockp;
64   vring_desc_t *desc;
65   vring_used_t *used;
66   vring_avail_t *avail;
67   u16 desc_in_use;
68   u16 desc_next;
69   int kick_fd;
70   int call_fd;
71   u8 buffer_pool_index;
72   u16 size;
73   u16 queue_id;
74 #define VRING_TX_OUT_OF_ORDER 1
75   u16 flags;
76   u32 *buffers;
77   u16 last_used_idx;
78   u16 last_kick_avail_idx;
79   u32 call_file_index;
80   virtio_vring_buffering_t *buffering;
81   gro_flow_table_t *flow_table;
82 } virtio_vring_t;
83
84 typedef union
85 {
86   struct
87   {
88     u16 domain;
89     u8 bus;
90     u8 slot:5;
91     u8 function:3;
92   };
93   u32 as_u32;
94 } pci_addr_t;
95
96 /* forward declaration */
97 typedef struct _virtio_pci_func virtio_pci_func_t;
98
99 typedef struct
100 {
101   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
102   u64 features;
103   u32 flags;
104   u32 per_interface_next_index;
105   u16 num_rxqs;
106   u16 num_txqs;
107   virtio_vring_t *rxq_vrings;
108   virtio_vring_t *txq_vrings;
109   int gso_enabled;
110   int csum_offload_enabled;
111   union
112   {
113     int *tap_fds;
114     struct
115     {
116       u32 pci_dev_handle;
117       u32 msix_enabled;
118     };
119   };
120   u16 virtio_net_hdr_sz;
121   virtio_if_type_t type;
122
123   u32 hw_if_index;
124   u32 sw_if_index;
125
126     CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
127   int packet_coalesce;
128   int packet_buffering;
129   u32 dev_instance;
130   u32 numa_node;
131   u64 remote_features;
132
133   /* error */
134   clib_error_t *error;
135   union
136   {
137     struct
138     {
139       u32 mac_addr32;
140       u16 mac_addr16;
141     };
142     u8 mac_addr[6];
143   };
144   union
145   {
146     struct                      /* tun/tap interface */
147     {
148       ip6_address_t host_ip6_addr;
149       int *vhost_fds;
150       u8 *host_if_name;
151       u8 *net_ns;
152       u8 *host_bridge;
153       u8 host_mac_addr[6];
154       u32 id;
155       u32 host_mtu_size;
156       u32 tap_flags;
157       int ifindex;
158       ip4_address_t host_ip4_addr;
159       u8 host_ip4_prefix_len;
160       u8 host_ip6_prefix_len;
161     };
162     struct                      /* native virtio */
163     {
164       void *bar;
165       virtio_vring_t *cxq_vring;
166       pci_addr_t pci_addr;
167       u32 bar_id;
168       u32 notify_off_multiplier;
169       u32 is_modern;
170       u16 common_offset;
171       u16 notify_offset;
172       u16 device_offset;
173       u16 isr_offset;
174       u16 max_queue_pairs;
175       u16 msix_table_size;
176       u8 support_int_mode;      /* support interrupt mode */
177       u8 status;
178     };
179   };
180   const virtio_pci_func_t *virtio_pci_func;
181 } virtio_if_t;
182
183 typedef struct
184 {
185   /* logging */
186   vlib_log_class_t log_default;
187
188   virtio_if_t *interfaces;
189 } virtio_main_t;
190
191 extern virtio_main_t virtio_main;
192 extern vnet_device_class_t virtio_device_class;
193 extern vlib_node_registration_t virtio_input_node;
194
195 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
196                                  u16 sz);
197 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
198                                     u32 idx);
199 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
200                                     u32 idx);
201 void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
202                                  u32 idx);
203 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
204 extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
205 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
206 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
207                          u32 type);
208 extern void virtio_set_packet_coalesce (virtio_if_t * vif);
209 clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
210 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
211                                             virtio_if_t * vif, u16 queue_id);
212 extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
213                                             virtio_if_t * vif, u16 queue_id);
214 format_function_t format_virtio_device_name;
215 format_function_t format_virtio_log_name;
216
217 static_always_inline void
218 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
219 {
220   if (vif->type == VIRTIO_IF_TYPE_PCI)
221     {
222       if (vif->is_modern)
223         virtio_pci_modern_notify_queue (vm, vif, vring->queue_id);
224       else
225         virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
226     }
227   else
228     {
229       u64 x = 1;
230       int __clib_unused r;
231
232       r = write (vring->kick_fd, &x, sizeof (x));
233       vring->last_kick_avail_idx = vring->avail->idx;
234     }
235 }
236
237 #define virtio_log_debug(vif, f, ...)                           \
238 {                                                               \
239   vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default,       \
240            "%U: " f, format_virtio_log_name, vif,               \
241            ##__VA_ARGS__);                                      \
242 };
243
244 #define virtio_log_warning(vif, f, ...)                         \
245 {                                                               \
246   vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default,     \
247            "%U: " f, format_virtio_log_name, vif,               \
248            ##__VA_ARGS__);                                      \
249 };
250
251 #define virtio_log_error(vif, f, ...)                           \
252 {                                                               \
253   vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default,         \
254            "%U: " f, format_virtio_log_name, vif,               \
255            ##__VA_ARGS__);                                      \
256 };
257
258 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
259
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "gnu")
265  * End:
266  */