virtio: split gso and checksum offload functionality
[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 <linux/virtio_config.h>
22 #include <linux/virtio_net.h>
23 #include <linux/virtio_pci.h>
24 #include <linux/virtio_ring.h>
25
26 #define foreach_virtio_net_features      \
27   _ (VIRTIO_NET_F_CSUM, 0)      /* Host handles pkts w/ partial csum */ \
28   _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \
29   _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \
30   _ (VIRTIO_NET_F_MTU, 3)       /* Initial MTU advice. */ \
31   _ (VIRTIO_NET_F_MAC, 5)       /* Host has given MAC address. */ \
32   _ (VIRTIO_NET_F_GSO, 6)       /* Host handles pkts w/ any GSO. */ \
33   _ (VIRTIO_NET_F_GUEST_TSO4, 7)        /* Guest can handle TSOv4 in. */ \
34   _ (VIRTIO_NET_F_GUEST_TSO6, 8)        /* Guest can handle TSOv6 in. */ \
35   _ (VIRTIO_NET_F_GUEST_ECN, 9) /* Guest can handle TSO[6] w/ ECN in. */ \
36   _ (VIRTIO_NET_F_GUEST_UFO, 10)        /* Guest can handle UFO in. */ \
37   _ (VIRTIO_NET_F_HOST_TSO4, 11)        /* Host can handle TSOv4 in. */ \
38   _ (VIRTIO_NET_F_HOST_TSO6, 12)        /* Host can handle TSOv6 in. */ \
39   _ (VIRTIO_NET_F_HOST_ECN, 13) /* Host can handle TSO[6] w/ ECN in. */ \
40   _ (VIRTIO_NET_F_HOST_UFO, 14) /* Host can handle UFO in. */ \
41   _ (VIRTIO_NET_F_MRG_RXBUF, 15)        /* Host can merge receive buffers. */ \
42   _ (VIRTIO_NET_F_STATUS, 16)   /* virtio_net_config.status available */ \
43   _ (VIRTIO_NET_F_CTRL_VQ, 17)  /* Control channel available */ \
44   _ (VIRTIO_NET_F_CTRL_RX, 18)  /* Control channel RX mode support */ \
45   _ (VIRTIO_NET_F_CTRL_VLAN, 19)        /* Control channel VLAN filtering */ \
46   _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20)    /* Extra RX mode control support */ \
47   _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21)   /* Guest can announce device on the network */ \
48   _ (VIRTIO_NET_F_MQ, 22)               /* Device supports Receive Flow Steering */ \
49   _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23)    /* Set MAC address */ \
50   _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \
51   _ (VHOST_F_LOG_ALL, 26)      /* Log all write descriptors */ \
52   _ (VIRTIO_F_ANY_LAYOUT, 27)  /* Can the device handle any descriptor layout */ \
53   _ (VIRTIO_RING_F_INDIRECT_DESC, 28)   /* Support indirect buffer descriptors */ \
54   _ (VIRTIO_RING_F_EVENT_IDX, 29)       /* The Guest publishes the used index for which it expects an interrupt \
55  * at the end of the avail ring. Host should ignore the avail->flags field. */ \
56 /* The Host publishes the avail index for which it expects a kick \
57  * at the end of the used ring. Guest should ignore the used->flags field. */ \
58   _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \
59   _ (VIRTIO_F_VERSION_1, 32)
60
61
62 #define foreach_virtio_if_flag          \
63   _(0, ADMIN_UP, "admin-up")            \
64   _(1, DELETING, "deleting")
65
66 typedef enum
67 {
68 #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
69   foreach_virtio_if_flag
70 #undef _
71 } virtio_if_flag_t;
72
73 #define VIRTIO_NUM_RX_DESC 256
74 #define VIRTIO_NUM_TX_DESC 256
75
76 #define VIRTIO_FEATURE(X) (1ULL << X)
77
78 #define TX_QUEUE(X) ((X*2) + 1)
79 #define RX_QUEUE(X) (X*2)
80 #define TX_QUEUE_ACCESS(X) (X/2)
81 #define RX_QUEUE_ACCESS(X) (X/2)
82
83 typedef enum
84 {
85   VIRTIO_IF_TYPE_TAP = 1,
86   VIRTIO_IF_TYPE_PCI,
87   VIRTIO_IF_N_TYPES,
88 } virtio_if_type_t;
89
90
91 typedef struct
92 {
93   u8 mac[6];
94   u16 status;
95   u16 max_virtqueue_pairs;
96   u16 mtu;
97 } virtio_net_config_t;
98
99 #define VIRTIO_RING_FLAG_MASK_INT 1
100
101 typedef struct
102 {
103   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
104   struct vring_desc *desc;
105   struct vring_used *used;
106   struct vring_avail *avail;
107   clib_spinlock_t lockp;
108   u16 desc_in_use;
109   u16 desc_next;
110   int kick_fd;
111   int call_fd;
112   u8 buffer_pool_index;
113   u16 size;
114   u16 queue_id;
115   u16 flags;
116   u32 call_file_index;
117   u32 *buffers;
118   u16 last_used_idx;
119   u16 last_kick_avail_idx;
120 } virtio_vring_t;
121
122 typedef union
123 {
124   struct
125   {
126     u16 domain;
127     u8 bus;
128     u8 slot:5;
129     u8 function:3;
130   };
131   u32 as_u32;
132 } pci_addr_t;
133
134 typedef struct
135 {
136   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
137   u32 flags;
138
139   u32 dev_instance;
140   u32 hw_if_index;
141   u32 sw_if_index;
142   u32 numa_node;
143   u16 virtio_net_hdr_sz;
144   virtio_if_type_t type;
145   union
146   {
147     u32 id;
148     pci_addr_t pci_addr;
149   };
150   u32 per_interface_next_index;
151   int *vhost_fds;
152   int tap_fd;
153   u32 msix_enabled;
154   u32 pci_dev_handle;
155   virtio_vring_t *rxq_vrings;
156   virtio_vring_t *txq_vrings;
157   u64 features, remote_features;
158
159   /* error */
160   clib_error_t *error;
161   u8 support_int_mode;          /* support interrupt mode */
162   u16 max_queue_pairs;
163   u16 num_rxqs;
164   u16 num_txqs;
165   u8 status;
166   u8 mac_addr[6];
167   u8 *host_if_name;
168   u8 *net_ns;
169   u8 *host_bridge;
170   u8 host_mac_addr[6];
171   ip4_address_t host_ip4_addr;
172   u8 host_ip4_prefix_len;
173   ip6_address_t host_ip6_addr;
174   u8 host_ip6_prefix_len;
175   u32 host_mtu_size;
176   int gso_enabled;
177   int csum_offload_enabled;
178   int ifindex;
179   virtio_vring_t *cxq_vring;
180 } virtio_if_t;
181
182 typedef struct
183 {
184   /* logging */
185   vlib_log_class_t log_default;
186
187   virtio_if_t *interfaces;
188 } virtio_main_t;
189
190 extern virtio_main_t virtio_main;
191 extern vnet_device_class_t virtio_device_class;
192 extern vlib_node_registration_t virtio_input_node;
193
194 clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
195                                  u16 sz);
196 clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
197                                     u32 idx);
198 clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
199                                     u32 idx);
200 void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
201                                  u32 idx);
202 extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
203 extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
204 extern void virtio_set_net_hdr_size (virtio_if_t * vif);
205 extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
206                          u32 type);
207 extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
208                                             virtio_if_t * vif, u16 queue_id);
209 format_function_t format_virtio_device_name;
210 format_function_t format_virtio_log_name;
211
212 static_always_inline void
213 virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
214 {
215   if (vif->type == VIRTIO_IF_TYPE_PCI)
216     virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
217   else
218     {
219       u64 x = 1;
220       int __clib_unused r;
221
222       r = write (vring->kick_fd, &x, sizeof (x));
223       vring->last_kick_avail_idx = vring->avail->idx;
224     }
225 }
226
227
228 #define virtio_log_debug(vif, f, ...)                           \
229 {                                                               \
230   vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default,       \
231            "%U: " f, format_virtio_log_name, vif,               \
232            ##__VA_ARGS__);                                      \
233 };
234
235 #define virtio_log_warning(vif, f, ...)                         \
236 {                                                               \
237   vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default,     \
238            "%U: " f, format_virtio_log_name, vif,               \
239            ##__VA_ARGS__);                                      \
240 };
241
242 #define virtio_log_error(vif, f, ...)                           \
243 {                                                               \
244   vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default,         \
245            "%U: " f, format_virtio_log_name, vif,               \
246            ##__VA_ARGS__);                                      \
247 };
248
249 #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
250
251 /*
252  * fd.io coding-style-patch-verification: ON
253  *
254  * Local Variables:
255  * eval: (c-set-style "gnu")
256  * End:
257  */