blob: b00e1eceb85871141dba39e3e1f121e4bf26cbce [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 Kazmia7a22812020-08-31 17:17:16 +020021#include <vnet/devices/virtio/virtio_std.h>
22#include <vnet/devices/virtio/vhost_std.h>
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020023#include <vnet/gso/gro.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020024
Damjan Marion8389fb92017-10-13 18:29:53 +020025#define foreach_virtio_if_flag \
26 _(0, ADMIN_UP, "admin-up") \
27 _(1, DELETING, "deleting")
28
29typedef enum
30{
31#define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
32 foreach_virtio_if_flag
33#undef _
34} virtio_if_flag_t;
35
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000036#define TX_QUEUE(X) ((X*2) + 1)
37#define RX_QUEUE(X) (X*2)
38#define TX_QUEUE_ACCESS(X) (X/2)
39#define RX_QUEUE_ACCESS(X) (X/2)
40
Mohsin Kazmia0a68332020-07-16 12:55:42 +000041#define VIRTIO_NUM_RX_DESC 256
42#define VIRTIO_NUM_TX_DESC 256
43
Mohsin Kazmi206acf82020-04-06 14:19:54 +020044#define foreach_virtio_if_types \
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020045 _ (TAP, 0) \
46 _ (TUN, 1) \
47 _ (PCI, 2)
Mohsin Kazmi206acf82020-04-06 14:19:54 +020048
Damjan Marion8389fb92017-10-13 18:29:53 +020049typedef enum
50{
Mohsin Kazmi206acf82020-04-06 14:19:54 +020051#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
52 foreach_virtio_if_types
53#undef _
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020054 VIRTIO_IF_N_TYPES = (1 << 3),
Damjan Marion8389fb92017-10-13 18:29:53 +020055} virtio_if_type_t;
56
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020057#define VIRTIO_RING_FLAG_MASK_INT 1
58
59typedef struct
60{
David Johnsond9818dd2018-12-14 14:53:41 -050061 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +020062 clib_spinlock_t lockp;
Mohsin Kazmia7a22812020-08-31 17:17:16 +020063 vring_desc_t *desc;
64 vring_used_t *used;
65 vring_avail_t *avail;
Damjan Marion8389fb92017-10-13 18:29:53 +020066 u16 desc_in_use;
67 u16 desc_next;
68 int kick_fd;
69 int call_fd;
Mohsin Kazmi80659b42019-01-31 13:18:00 +000070 u8 buffer_pool_index;
Damjan Marion8389fb92017-10-13 18:29:53 +020071 u16 size;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020072 u16 queue_id;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +010073#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020074 u16 flags;
Damjan Marion8389fb92017-10-13 18:29:53 +020075 u32 *buffers;
76 u16 last_used_idx;
Damjan Marione40231b2018-12-20 10:44:47 +010077 u16 last_kick_avail_idx;
Mohsin Kazmi38b09682020-06-03 18:20:17 +020078 u32 call_file_index;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020079 gro_flow_table_t *flow_table;
Damjan Marion8389fb92017-10-13 18:29:53 +020080} virtio_vring_t;
81
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020082typedef union
83{
84 struct
85 {
86 u16 domain;
87 u8 bus;
88 u8 slot:5;
89 u8 function:3;
90 };
91 u32 as_u32;
92} pci_addr_t;
93
Mohsin Kazmi379aac32020-08-20 10:25:12 +020094/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +000095typedef struct _virtio_pci_func virtio_pci_func_t;
96
Damjan Marion8389fb92017-10-13 18:29:53 +020097typedef struct
98{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020099 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200100 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200101 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200102 u32 per_interface_next_index;
103 u16 num_rxqs;
104 u16 num_txqs;
105 virtio_vring_t *rxq_vrings;
106 virtio_vring_t *txq_vrings;
107 int gso_enabled;
108 int csum_offload_enabled;
109 union
110 {
111 int *tap_fds;
112 struct
113 {
114 u32 pci_dev_handle;
115 u32 msix_enabled;
116 };
117 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200118 u16 virtio_net_hdr_sz;
119 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200120
121 u32 hw_if_index;
122 u32 sw_if_index;
123
124 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200125 int packet_coalesce;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200126 u32 dev_instance;
127 u32 numa_node;
128 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200129
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200130 /* error */
131 clib_error_t *error;
BenoƮt Ganne3f0ae662020-09-09 12:50:07 +0200132 union
133 {
134 struct
135 {
136 u32 mac_addr32;
137 u16 mac_addr16;
138 };
139 u8 mac_addr[6];
140 };
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200141 union
142 {
143 struct /* tun/tap interface */
144 {
145 ip6_address_t host_ip6_addr;
146 int *vhost_fds;
147 u8 *host_if_name;
148 u8 *net_ns;
149 u8 *host_bridge;
150 u8 host_mac_addr[6];
151 u32 id;
152 u32 host_mtu_size;
153 u32 tap_flags;
154 int ifindex;
155 ip4_address_t host_ip4_addr;
156 u8 host_ip4_prefix_len;
157 u8 host_ip6_prefix_len;
158 };
159 struct /* native virtio */
160 {
161 void *bar;
162 virtio_vring_t *cxq_vring;
163 pci_addr_t pci_addr;
164 u32 bar_id;
165 u32 notify_off_multiplier;
166 u32 is_modern;
167 u16 common_offset;
168 u16 notify_offset;
169 u16 device_offset;
170 u16 isr_offset;
171 u16 max_queue_pairs;
172 u16 msix_table_size;
173 u8 support_int_mode; /* support interrupt mode */
174 u8 status;
175 };
176 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000177 const virtio_pci_func_t *virtio_pci_func;
Damjan Marion8389fb92017-10-13 18:29:53 +0200178} virtio_if_t;
179
180typedef struct
181{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000182 /* logging */
183 vlib_log_class_t log_default;
184
Damjan Marion8389fb92017-10-13 18:29:53 +0200185 virtio_if_t *interfaces;
186} virtio_main_t;
187
188extern virtio_main_t virtio_main;
189extern vnet_device_class_t virtio_device_class;
190extern vlib_node_registration_t virtio_input_node;
191
192clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
193 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000194clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
195 u32 idx);
196clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
197 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000198void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
199 u32 idx);
Stevena624dbe2018-01-09 11:13:29 -0800200extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200201extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
202extern void virtio_set_net_hdr_size (virtio_if_t * vif);
203extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
204 u32 type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200205extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200206extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
207 virtio_if_t * vif, u16 queue_id);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200208extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
209 virtio_if_t * vif, u16 queue_id);
Damjan Marionceab7882018-01-19 20:56:12 +0100210format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100211format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100212
Damjan Marione40231b2018-12-20 10:44:47 +0100213static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200214virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100215{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200216 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200217 {
218 if (vif->is_modern)
219 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id);
220 else
221 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id);
222 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200223 else
224 {
225 u64 x = 1;
226 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100227
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200228 r = write (vring->kick_fd, &x, sizeof (x));
229 vring->last_kick_avail_idx = vring->avail->idx;
230 }
Damjan Marione40231b2018-12-20 10:44:47 +0100231}
232
Damjan Marionf41244f2019-11-08 17:41:06 +0100233#define virtio_log_debug(vif, f, ...) \
234{ \
235 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
236 "%U: " f, format_virtio_log_name, vif, \
237 ##__VA_ARGS__); \
238};
239
240#define virtio_log_warning(vif, f, ...) \
241{ \
242 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
243 "%U: " f, format_virtio_log_name, vif, \
244 ##__VA_ARGS__); \
245};
246
247#define virtio_log_error(vif, f, ...) \
248{ \
249 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
250 "%U: " f, format_virtio_log_name, vif, \
251 ##__VA_ARGS__); \
252};
253
Damjan Marion8389fb92017-10-13 18:29:53 +0200254#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
255
256/*
257 * fd.io coding-style-patch-verification: ON
258 *
259 * Local Variables:
260 * eval: (c-set-style "gnu")
261 * End:
262 */