blob: 1cca9f366a8b03dc61aa3a5d1bd6c797f4a16aff [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 Kazmie347acb2020-09-28 10:26:33 +000023#include <vnet/devices/virtio/virtio_buffering.h>
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020024#include <vnet/gso/gro.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020025
Damjan Marion8389fb92017-10-13 18:29:53 +020026#define foreach_virtio_if_flag \
27 _(0, ADMIN_UP, "admin-up") \
28 _(1, DELETING, "deleting")
29
30typedef 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
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000037#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
Mohsin Kazmia0a68332020-07-16 12:55:42 +000042#define VIRTIO_NUM_RX_DESC 256
43#define VIRTIO_NUM_TX_DESC 256
44
Mohsin Kazmi206acf82020-04-06 14:19:54 +020045#define foreach_virtio_if_types \
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020046 _ (TAP, 0) \
47 _ (TUN, 1) \
48 _ (PCI, 2)
Mohsin Kazmi206acf82020-04-06 14:19:54 +020049
Damjan Marion8389fb92017-10-13 18:29:53 +020050typedef enum
51{
Mohsin Kazmi206acf82020-04-06 14:19:54 +020052#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
53 foreach_virtio_if_types
54#undef _
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020055 VIRTIO_IF_N_TYPES = (1 << 3),
Damjan Marion8389fb92017-10-13 18:29:53 +020056} virtio_if_type_t;
57
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020058#define VIRTIO_RING_FLAG_MASK_INT 1
59
60typedef struct
61{
David Johnsond9818dd2018-12-14 14:53:41 -050062 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +020063 clib_spinlock_t lockp;
Mohsin Kazmia7a22812020-08-31 17:17:16 +020064 vring_desc_t *desc;
65 vring_used_t *used;
66 vring_avail_t *avail;
Damjan Marion8389fb92017-10-13 18:29:53 +020067 u16 desc_in_use;
68 u16 desc_next;
Mohsin Kazmi162a2962020-09-29 10:01:25 +000069 union
70 {
71 struct
72 {
73 int kick_fd;
74 int call_fd;
75 };
76 u16 queue_notify_offset;
77 };
Mohsin Kazmi80659b42019-01-31 13:18:00 +000078 u8 buffer_pool_index;
Damjan Marion8389fb92017-10-13 18:29:53 +020079 u16 size;
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020080 u16 queue_id;
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +010081#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020082 u16 flags;
Damjan Marion8389fb92017-10-13 18:29:53 +020083 u32 *buffers;
84 u16 last_used_idx;
Damjan Marione40231b2018-12-20 10:44:47 +010085 u16 last_kick_avail_idx;
Mohsin Kazmi38b09682020-06-03 18:20:17 +020086 u32 call_file_index;
Mohsin Kazmie347acb2020-09-28 10:26:33 +000087 virtio_vring_buffering_t *buffering;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +020088 gro_flow_table_t *flow_table;
Damjan Marion8389fb92017-10-13 18:29:53 +020089} virtio_vring_t;
90
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020091typedef union
92{
93 struct
94 {
95 u16 domain;
96 u8 bus;
97 u8 slot:5;
98 u8 function:3;
99 };
100 u32 as_u32;
101} pci_addr_t;
102
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200103/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000104typedef struct _virtio_pci_func virtio_pci_func_t;
105
Damjan Marion8389fb92017-10-13 18:29:53 +0200106typedef struct
107{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200108 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200109 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200110 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200111 u32 per_interface_next_index;
112 u16 num_rxqs;
113 u16 num_txqs;
114 virtio_vring_t *rxq_vrings;
115 virtio_vring_t *txq_vrings;
116 int gso_enabled;
117 int csum_offload_enabled;
118 union
119 {
120 int *tap_fds;
121 struct
122 {
123 u32 pci_dev_handle;
124 u32 msix_enabled;
125 };
126 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200127 u16 virtio_net_hdr_sz;
128 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200129
130 u32 hw_if_index;
131 u32 sw_if_index;
132
133 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200134 int packet_coalesce;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000135 int packet_buffering;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200136 u32 dev_instance;
137 u32 numa_node;
138 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200139
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200140 /* error */
141 clib_error_t *error;
Benoît Ganne3f0ae662020-09-09 12:50:07 +0200142 union
143 {
144 struct
145 {
146 u32 mac_addr32;
147 u16 mac_addr16;
148 };
149 u8 mac_addr[6];
150 };
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200151 union
152 {
153 struct /* tun/tap interface */
154 {
155 ip6_address_t host_ip6_addr;
156 int *vhost_fds;
157 u8 *host_if_name;
158 u8 *net_ns;
159 u8 *host_bridge;
160 u8 host_mac_addr[6];
161 u32 id;
162 u32 host_mtu_size;
163 u32 tap_flags;
164 int ifindex;
165 ip4_address_t host_ip4_addr;
166 u8 host_ip4_prefix_len;
167 u8 host_ip6_prefix_len;
168 };
169 struct /* native virtio */
170 {
171 void *bar;
172 virtio_vring_t *cxq_vring;
173 pci_addr_t pci_addr;
174 u32 bar_id;
175 u32 notify_off_multiplier;
176 u32 is_modern;
177 u16 common_offset;
178 u16 notify_offset;
179 u16 device_offset;
180 u16 isr_offset;
181 u16 max_queue_pairs;
182 u16 msix_table_size;
183 u8 support_int_mode; /* support interrupt mode */
184 u8 status;
185 };
186 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000187 const virtio_pci_func_t *virtio_pci_func;
Damjan Marion8389fb92017-10-13 18:29:53 +0200188} virtio_if_t;
189
190typedef struct
191{
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000192 /* logging */
193 vlib_log_class_t log_default;
194
Damjan Marion8389fb92017-10-13 18:29:53 +0200195 virtio_if_t *interfaces;
196} virtio_main_t;
197
198extern virtio_main_t virtio_main;
199extern vnet_device_class_t virtio_device_class;
200extern vlib_node_registration_t virtio_input_node;
201
202clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
203 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000204clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
205 u32 idx);
206clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
207 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000208void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
209 u32 idx);
Stevena624dbe2018-01-09 11:13:29 -0800210extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200211extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring);
212extern void virtio_set_net_hdr_size (virtio_if_t * vif);
213extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
214 u32 type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200215extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000216clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200217extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000218 virtio_if_t * vif, u16 queue_id,
219 u16 queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200220extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000221 virtio_if_t * vif, u16 queue_id,
222 u16 queue_notify_offset);
Damjan Marionceab7882018-01-19 20:56:12 +0100223format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100224format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100225
Damjan Marione40231b2018-12-20 10:44:47 +0100226static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200227virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100228{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200229 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200230 {
231 if (vif->is_modern)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000232 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
233 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200234 else
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000235 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
236 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200237 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200238 else
239 {
240 u64 x = 1;
241 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100242
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200243 r = write (vring->kick_fd, &x, sizeof (x));
244 vring->last_kick_avail_idx = vring->avail->idx;
245 }
Damjan Marione40231b2018-12-20 10:44:47 +0100246}
247
Damjan Marionf41244f2019-11-08 17:41:06 +0100248#define virtio_log_debug(vif, f, ...) \
249{ \
250 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
251 "%U: " f, format_virtio_log_name, vif, \
252 ##__VA_ARGS__); \
253};
254
255#define virtio_log_warning(vif, f, ...) \
256{ \
257 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
258 "%U: " f, format_virtio_log_name, vif, \
259 ##__VA_ARGS__); \
260};
261
262#define virtio_log_error(vif, f, ...) \
263{ \
264 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
265 "%U: " f, format_virtio_log_name, vif, \
266 ##__VA_ARGS__); \
267};
268
Damjan Marion8389fb92017-10-13 18:29:53 +0200269#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
270
271/*
272 * fd.io coding-style-patch-verification: ON
273 *
274 * Local Variables:
275 * eval: (c-set-style "gnu")
276 * End:
277 */