blob: 035dc9ca40d932ca639215a542c330ef82c4aac8 [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
Mohsin Kazmia5203b52020-10-12 13:01:24 +020060#define VIRTIO_EVENT_START_TIMER 1
61#define VIRTIO_EVENT_STOP_TIMER 2
62
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020063typedef struct
64{
David Johnsond9818dd2018-12-14 14:53:41 -050065 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +020066 clib_spinlock_t lockp;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010067 union
68 {
69 struct
70 {
71 vring_desc_t *desc;
72 vring_used_t *used;
73 vring_avail_t *avail;
74 };
75 struct
76 {
77 vring_packed_desc_t *packed_desc;
78 vring_desc_event_t *driver_event;
79 vring_desc_event_t *device_event;
80 };
81 };
82 u32 *buffers;
83 u16 size;
84 u16 queue_id;
Damjan Marion8389fb92017-10-13 18:29:53 +020085 u16 desc_in_use;
86 u16 desc_next;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010087 u16 last_used_idx;
88 u16 last_kick_avail_idx;
Mohsin Kazmi162a2962020-09-29 10:01:25 +000089 union
90 {
91 struct
92 {
93 int kick_fd;
94 int call_fd;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010095 u32 call_file_index;
Mohsin Kazmi162a2962020-09-29 10:01:25 +000096 };
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010097 struct
98 {
99 u16 avail_wrap_counter;
100 u16 used_wrap_counter;
101 u16 queue_notify_offset;
102 };
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000103 };
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100104#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200105 u16 flags;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100106 u8 buffer_pool_index;
Mohsin Kazmia5203b52020-10-12 13:01:24 +0200107 vnet_hw_if_rx_mode mode;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000108 virtio_vring_buffering_t *buffering;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200109 gro_flow_table_t *flow_table;
Damjan Marion8389fb92017-10-13 18:29:53 +0200110} virtio_vring_t;
111
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200112typedef union
113{
114 struct
115 {
116 u16 domain;
117 u8 bus;
118 u8 slot:5;
119 u8 function:3;
120 };
121 u32 as_u32;
122} pci_addr_t;
123
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200124/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000125typedef struct _virtio_pci_func virtio_pci_func_t;
126
Damjan Marion8389fb92017-10-13 18:29:53 +0200127typedef struct
128{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200129 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200130 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200131 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200132 u32 per_interface_next_index;
133 u16 num_rxqs;
134 u16 num_txqs;
135 virtio_vring_t *rxq_vrings;
136 virtio_vring_t *txq_vrings;
137 int gso_enabled;
138 int csum_offload_enabled;
139 union
140 {
141 int *tap_fds;
142 struct
143 {
144 u32 pci_dev_handle;
145 u32 msix_enabled;
146 };
147 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200148 u16 virtio_net_hdr_sz;
149 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200150
151 u32 hw_if_index;
152 u32 sw_if_index;
153
154 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200155 int packet_coalesce;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000156 int packet_buffering;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200157 u32 dev_instance;
158 u32 numa_node;
159 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200160
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200161 /* error */
162 clib_error_t *error;
Benoît Ganne3f0ae662020-09-09 12:50:07 +0200163 union
164 {
165 struct
166 {
167 u32 mac_addr32;
168 u16 mac_addr16;
169 };
170 u8 mac_addr[6];
171 };
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;
Matthew Smithbd50ed12020-07-24 13:38:03 -0500189 u8 host_carrier_up; /* host tun/tap driver link carrier state */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200190 };
191 struct /* native virtio */
192 {
193 void *bar;
194 virtio_vring_t *cxq_vring;
195 pci_addr_t pci_addr;
196 u32 bar_id;
197 u32 notify_off_multiplier;
198 u32 is_modern;
199 u16 common_offset;
200 u16 notify_offset;
201 u16 device_offset;
202 u16 isr_offset;
203 u16 max_queue_pairs;
204 u16 msix_table_size;
205 u8 support_int_mode; /* support interrupt mode */
206 u8 status;
207 };
208 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000209 const virtio_pci_func_t *virtio_pci_func;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100210 int is_packed;
Damjan Marion8389fb92017-10-13 18:29:53 +0200211} virtio_if_t;
212
213typedef struct
214{
Mohsin Kazmia5203b52020-10-12 13:01:24 +0200215 u32 interrupt_queues_count;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000216 /* logging */
217 vlib_log_class_t log_default;
218
Damjan Marion8389fb92017-10-13 18:29:53 +0200219 virtio_if_t *interfaces;
220} virtio_main_t;
221
222extern virtio_main_t virtio_main;
223extern vnet_device_class_t virtio_device_class;
224extern vlib_node_registration_t virtio_input_node;
Mohsin Kazmia5203b52020-10-12 13:01:24 +0200225extern vlib_node_registration_t virtio_send_interrupt_node;
Damjan Marion8389fb92017-10-13 18:29:53 +0200226
227clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
228 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000229clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
230 u32 idx);
231clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
232 u32 idx);
Mohsin Kazmi80659b42019-01-31 13:18:00 +0000233void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif,
234 u32 idx);
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100235extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200236extern void virtio_set_net_hdr_size (virtio_if_t * vif);
237extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr,
238 u32 type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200239extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000240clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200241extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000242 virtio_if_t * vif, u16 queue_id,
243 u16 queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200244extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000245 virtio_if_t * vif, u16 queue_id,
246 u16 queue_notify_offset);
Damjan Marionceab7882018-01-19 20:56:12 +0100247format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100248format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100249
Damjan Marione40231b2018-12-20 10:44:47 +0100250static_always_inline void
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200251virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100252{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200253 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200254 {
255 if (vif->is_modern)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000256 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
257 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200258 else
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000259 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
260 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200261 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200262 else
263 {
264 u64 x = 1;
265 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100266
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200267 r = write (vring->kick_fd, &x, sizeof (x));
268 vring->last_kick_avail_idx = vring->avail->idx;
269 }
Damjan Marione40231b2018-12-20 10:44:47 +0100270}
271
Damjan Marionf41244f2019-11-08 17:41:06 +0100272#define virtio_log_debug(vif, f, ...) \
273{ \
274 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
275 "%U: " f, format_virtio_log_name, vif, \
276 ##__VA_ARGS__); \
277};
278
279#define virtio_log_warning(vif, f, ...) \
280{ \
281 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
282 "%U: " f, format_virtio_log_name, vif, \
283 ##__VA_ARGS__); \
284};
285
286#define virtio_log_error(vif, f, ...) \
287{ \
288 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
289 "%U: " f, format_virtio_log_name, vif, \
290 ##__VA_ARGS__); \
291};
292
Damjan Marion8389fb92017-10-13 18:29:53 +0200293#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
294
295/*
296 * fd.io coding-style-patch-verification: ON
297 *
298 * Local Variables:
299 * eval: (c-set-style "gnu")
300 * End:
301 */