blob: 7a5dcd83fe347586aa177f3f3857cdbec11b3d14 [file] [log] [blame]
Damjan Marion8389fb92017-10-13 18:29:53 +02001/*
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
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020021#include <linux/virtio_config.h>
22#include <linux/virtio_net.h>
23#include <linux/virtio_pci.h>
24#include <linux/virtio_ring.h>
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020025#include <vnet/gso/gro.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020026
Damjan Marion8389fb92017-10-13 18:29:53 +020027#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 */ \
Paul Vinciguerra97c998c2019-10-29 16:11:09 -040053 _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descriptor layout */ \
Damjan Marion8389fb92017-10-13 18:29:53 +020054 _ (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) \
Mohsin Kazmi379aac32020-08-20 10:25:12 +020060 _ (VIRTIO_F_VERSION_1, 32) /* v1.0 compliant. */ \
Damjan Marion8389fb92017-10-13 18:29:53 +020061
62#define foreach_virtio_if_flag \
63 _(0, ADMIN_UP, "admin-up") \
64 _(1, DELETING, "deleting")
65
66typedef 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
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020073#define VIRTIO_FEATURE(X) (1ULL << X)
74
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000075#define TX_QUEUE(X) ((X*2) + 1)
76#define RX_QUEUE(X) (X*2)
77#define TX_QUEUE_ACCESS(X) (X/2)
78#define RX_QUEUE_ACCESS(X) (X/2)
79
Mohsin Kazmia0a68332020-07-16 12:55:42 +000080#define VIRTIO_NUM_RX_DESC 256
81#define VIRTIO_NUM_TX_DESC 256
82
Mohsin Kazmi206acf82020-04-06 14:19:54 +020083#define foreach_virtio_if_types \
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020084 _ (TAP, 0) \
85 _ (TUN, 1) \
86 _ (PCI, 2)
Mohsin Kazmi206acf82020-04-06 14:19:54 +020087
Damjan Marion8389fb92017-10-13 18:29:53 +020088typedef enum
89{
Mohsin Kazmi206acf82020-04-06 14:19:54 +020090#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
91 foreach_virtio_if_types
92#undef _
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020093 VIRTIO_IF_N_TYPES = (1 << 3),
Damjan Marion8389fb92017-10-13 18:29:53 +020094} virtio_if_type_t;
95
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020096#define VIRTIO_RING_FLAG_MASK_INT 1
97
98typedef struct
99{
David Johnsond9818dd2018-12-14 14:53:41 -0500100 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200101 clib_spinlock_t lockp;
Damjan Marion8389fb92017-10-13 18:29:53 +0200102 struct vring_desc *desc;
103 struct vring_used *used;
104 struct vring_avail *avail;
105 u16 desc_in_use;
106 u16 desc_next;
107 int kick_fd;
108 int call_fd;
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000109 u8 buffer_pool_index;
Damjan Marion8389fb92017-10-13 18:29:53 +0200110 u16 size;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200111 u16 queue_id;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100112#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200113 u16 flags;
Damjan Marion8389fb92017-10-13 18:29:53 +0200114 u32 *buffers;
115 u16 last_used_idx;
Damjan Marione40231b2018-12-20 10:44:47 +0100116 u16 last_kick_avail_idx;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200117 u32 call_file_index;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200118 gro_flow_table_t *flow_table;
Damjan Marion8389fb92017-10-13 18:29:53 +0200119} virtio_vring_t;
120
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200121typedef union
122{
123 struct
124 {
125 u16 domain;
126 u8 bus;
127 u8 slot:5;
128 u8 function:3;
129 };
130 u32 as_u32;
131} pci_addr_t;
132
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200133/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000134typedef struct _virtio_pci_func virtio_pci_func_t;
135
Damjan Marion8389fb92017-10-13 18:29:53 +0200136typedef struct
137{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200138 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200139 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200140 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200141 u32 per_interface_next_index;
142 u16 num_rxqs;
143 u16 num_txqs;
144 virtio_vring_t *rxq_vrings;
145 virtio_vring_t *txq_vrings;
146 int gso_enabled;
147 int csum_offload_enabled;
148 union
149 {
150 int *tap_fds;
151 struct
152 {
153 u32 pci_dev_handle;
154 u32 msix_enabled;
155 };
156 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200157 u16 virtio_net_hdr_sz;
158 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200159
160 u32 hw_if_index;
161 u32 sw_if_index;
162
163 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200164 int packet_coalesce;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200165 u32 dev_instance;
166 u32 numa_node;
167 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200168
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200169 /* error */
170 clib_error_t *error;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200171 u8 mac_addr[6];
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200172 union
173 {
174 struct /* tun/tap interface */
175 {
176 ip6_address_t host_ip6_addr;
177 int *vhost_fds;
178 u8 *host_if_name;
179 u8 *net_ns;
180 u8 *host_bridge;
181 u8 host_mac_addr[6];
182 u32 id;
183 u32 host_mtu_size;
184 u32 tap_flags;
185 int ifindex;
186 ip4_address_t host_ip4_addr;
187 u8 host_ip4_prefix_len;
188 u8 host_ip6_prefix_len;
189 };
190 struct /* native virtio */
191 {
192 void *bar;
193 virtio_vring_t *cxq_vring;
194 pci_addr_t pci_addr;
195 u32 bar_id;
196 u32 notify_off_multiplier;
197 u32 is_modern;
198 u16 common_offset;
199 u16 notify_offset;
200 u16 device_offset;
201 u16 isr_offset;
202 u16 max_queue_pairs;
203 u16 msix_table_size;
204 u8 support_int_mode; /* support interrupt mode */
205 u8 status;
206 };
207 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000208 const virtio_pci_func_t *virtio_pci_func;
Damjan Marion8389fb92017-10-13 18:29:53 +0200209} virtio_if_t;
210
211typedef struct
212{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000213 /* logging */
214 vlib_log_class_t log_default;
215
Damjan Marion8389fb92017-10-13 18:29:53 +0200216 virtio_if_t *interfaces;
217} virtio_main_t;
218
219extern virtio_main_t virtio_main;
220extern vnet_device_class_t virtio_device_class;
221extern vlib_node_registration_t virtio_input_node;
222
223clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
224 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000225clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
226 u32 idx);
227clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
228 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000229void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
230 u32 idx);
Stevena624dbe2018-01-09 11:13:29 -0800231extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200232extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
233extern void virtio_set_net_hdr_size (virtio_if_t * vif);
234extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
235 u32 type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200236extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200237extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
238 virtio_if_t * vif, u16 queue_id);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200239extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
240 virtio_if_t * vif, u16 queue_id);
Damjan Marionceab7882018-01-19 20:56:12 +0100241format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100242format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100243
Damjan Marione40231b2018-12-20 10:44:47 +0100244static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200245virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100246{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200247 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200248 {
249 if (vif->is_modern)
250 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id);
251 else
252 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
253 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200254 else
255 {
256 u64 x = 1;
257 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100258
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200259 r = write (vring->kick_fd, &x, sizeof (x));
260 vring->last_kick_avail_idx = vring->avail->idx;
261 }
Damjan Marione40231b2018-12-20 10:44:47 +0100262}
263
Damjan Marionf41244f2019-11-08 17:41:06 +0100264#define virtio_log_debug(vif, f, ...) \
265{ \
266 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
267 "%U: " f, format_virtio_log_name, vif, \
268 ##__VA_ARGS__); \
269};
270
271#define virtio_log_warning(vif, f, ...) \
272{ \
273 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
274 "%U: " f, format_virtio_log_name, vif, \
275 ##__VA_ARGS__); \
276};
277
278#define virtio_log_error(vif, f, ...) \
279{ \
280 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
281 "%U: " f, format_virtio_log_name, vif, \
282 ##__VA_ARGS__); \
283};
284
Damjan Marion8389fb92017-10-13 18:29:53 +0200285#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
286
287/*
288 * fd.io coding-style-patch-verification: ON
289 *
290 * Local Variables:
291 * eval: (c-set-style "gnu")
292 * End:
293 */