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