blob: e12198c3bf849e9b657963812b2d47fe0061cdff [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>
Neale Rannsf7040f02022-02-15 09:02:27 +000025#include <vnet/interface.h>
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020026
Damjan Marion8389fb92017-10-13 18:29:53 +020027#define foreach_virtio_if_flag \
28 _(0, ADMIN_UP, "admin-up") \
29 _(1, DELETING, "deleting")
30
31typedef enum
32{
33#define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a),
34 foreach_virtio_if_flag
35#undef _
36} virtio_if_flag_t;
37
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +000038#define TX_QUEUE(X) ((X*2) + 1)
39#define RX_QUEUE(X) (X*2)
40#define TX_QUEUE_ACCESS(X) (X/2)
41#define RX_QUEUE_ACCESS(X) (X/2)
42
Mohsin Kazmia0a68332020-07-16 12:55:42 +000043#define VIRTIO_NUM_RX_DESC 256
44#define VIRTIO_NUM_TX_DESC 256
45
Mohsin Kazmi206acf82020-04-06 14:19:54 +020046#define foreach_virtio_if_types \
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020047 _ (TAP, 0) \
48 _ (TUN, 1) \
49 _ (PCI, 2)
Mohsin Kazmi206acf82020-04-06 14:19:54 +020050
Damjan Marion8389fb92017-10-13 18:29:53 +020051typedef enum
52{
Mohsin Kazmi206acf82020-04-06 14:19:54 +020053#define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b),
54 foreach_virtio_if_types
55#undef _
Mohsin Kazmid88fc0f2020-04-30 19:05:56 +020056 VIRTIO_IF_N_TYPES = (1 << 3),
Damjan Marion8389fb92017-10-13 18:29:53 +020057} virtio_if_type_t;
58
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020059#define VIRTIO_RING_FLAG_MASK_INT 1
60
Mohsin Kazmia5203b52020-10-12 13:01:24 +020061#define VIRTIO_EVENT_START_TIMER 1
62#define VIRTIO_EVENT_STOP_TIMER 2
63
Mohsin Kazmid6c15af2018-10-23 18:00:47 +020064typedef struct
65{
David Johnsond9818dd2018-12-14 14:53:41 -050066 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +020067 clib_spinlock_t lockp;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010068 union
69 {
70 struct
71 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000072 vnet_virtio_vring_desc_t *desc;
73 vnet_virtio_vring_used_t *used;
74 vnet_virtio_vring_avail_t *avail;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010075 };
76 struct
77 {
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000078 vnet_virtio_vring_packed_desc_t *packed_desc;
79 vnet_virtio_vring_desc_event_t *driver_event;
80 vnet_virtio_vring_desc_event_t *device_event;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010081 };
82 };
83 u32 *buffers;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +000084 u16 queue_size;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010085 u16 queue_id;
Damjan Marion94100532020-11-06 23:25:57 +010086 u32 queue_index;
Damjan Marion8389fb92017-10-13 18:29:53 +020087 u16 desc_in_use;
88 u16 desc_next;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010089 u16 last_used_idx;
90 u16 last_kick_avail_idx;
Mohsin Kazmi162a2962020-09-29 10:01:25 +000091 union
92 {
93 struct
94 {
95 int kick_fd;
96 int call_fd;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010097 u32 call_file_index;
Mohsin Kazmi162a2962020-09-29 10:01:25 +000098 };
Mohsin Kazmib977d3f2020-11-16 16:49:30 +010099 struct
100 {
101 u16 avail_wrap_counter;
102 u16 used_wrap_counter;
103 u16 queue_notify_offset;
104 };
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000105 };
Mohsin Kazmi7f6d1452020-02-27 11:49:21 +0100106#define VRING_TX_OUT_OF_ORDER 1
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000107#define VRING_TX_SCHEDULED 2
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200108 u16 flags;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100109 u8 buffer_pool_index;
Mohsin Kazmia5203b52020-10-12 13:01:24 +0200110 vnet_hw_if_rx_mode mode;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000111 virtio_vring_buffering_t *buffering;
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200112 gro_flow_table_t *flow_table;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000113} vnet_virtio_vring_t;
Damjan Marion8389fb92017-10-13 18:29:53 +0200114
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200115typedef union
116{
117 struct
118 {
119 u16 domain;
120 u8 bus;
121 u8 slot:5;
122 u8 function:3;
123 };
124 u32 as_u32;
125} pci_addr_t;
126
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200127/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000128typedef struct _virtio_pci_func virtio_pci_func_t;
129
Damjan Marion8389fb92017-10-13 18:29:53 +0200130typedef struct
131{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200132 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200133 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200134 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200135 u32 per_interface_next_index;
136 u16 num_rxqs;
137 u16 num_txqs;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000138 vnet_virtio_vring_t *rxq_vrings;
139 vnet_virtio_vring_t *txq_vrings;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200140 int gso_enabled;
141 int csum_offload_enabled;
142 union
143 {
144 int *tap_fds;
145 struct
146 {
147 u32 pci_dev_handle;
148 u32 msix_enabled;
149 };
150 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200151 u16 virtio_net_hdr_sz;
152 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200153
154 u32 hw_if_index;
155 u32 sw_if_index;
156
157 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200158 int packet_coalesce;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000159 int packet_buffering;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200160 u32 dev_instance;
161 u32 numa_node;
162 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200163
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200164 /* error */
165 clib_error_t *error;
Benoît Ganne3f0ae662020-09-09 12:50:07 +0200166 union
167 {
168 struct
169 {
170 u32 mac_addr32;
171 u16 mac_addr16;
172 };
173 u8 mac_addr[6];
174 };
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200175 union
176 {
177 struct /* tun/tap interface */
178 {
179 ip6_address_t host_ip6_addr;
180 int *vhost_fds;
181 u8 *host_if_name;
182 u8 *net_ns;
183 u8 *host_bridge;
184 u8 host_mac_addr[6];
185 u32 id;
186 u32 host_mtu_size;
187 u32 tap_flags;
188 int ifindex;
189 ip4_address_t host_ip4_addr;
190 u8 host_ip4_prefix_len;
191 u8 host_ip6_prefix_len;
Matthew Smithbd50ed12020-07-24 13:38:03 -0500192 u8 host_carrier_up; /* host tun/tap driver link carrier state */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200193 };
194 struct /* native virtio */
195 {
196 void *bar;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000197 vnet_virtio_vring_t *cxq_vring;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200198 pci_addr_t pci_addr;
199 u32 bar_id;
200 u32 notify_off_multiplier;
201 u32 is_modern;
202 u16 common_offset;
203 u16 notify_offset;
204 u16 device_offset;
205 u16 isr_offset;
206 u16 max_queue_pairs;
207 u16 msix_table_size;
208 u8 support_int_mode; /* support interrupt mode */
209 u8 status;
210 };
211 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000212 const virtio_pci_func_t *virtio_pci_func;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100213 int is_packed;
Damjan Marion8389fb92017-10-13 18:29:53 +0200214} virtio_if_t;
215
216typedef struct
217{
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000218 u32 gro_or_buffering_if_count;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000219 /* logging */
220 vlib_log_class_t log_default;
221
Damjan Marion8389fb92017-10-13 18:29:53 +0200222 virtio_if_t *interfaces;
Filip Tehlar9f562cd2021-06-23 00:23:05 +0000223 u16 msg_id_base;
Damjan Marion8389fb92017-10-13 18:29:53 +0200224} virtio_main_t;
225
226extern virtio_main_t virtio_main;
227extern vnet_device_class_t virtio_device_class;
228extern vlib_node_registration_t virtio_input_node;
229
230clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
231 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000232clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
233 u32 idx);
234clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
235 u32 idx);
Damjan Marion94100532020-11-06 23:25:57 +0100236void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000237void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000238extern void virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200239extern void virtio_set_net_hdr_size (virtio_if_t * vif);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000240extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
241 virtio_if_type_t type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200242extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000243clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200244extern void virtio_pci_legacy_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);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200247extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000248 virtio_if_t * vif, u16 queue_id,
249 u16 queue_notify_offset);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000250extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif);
251extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif);
252
Damjan Marionceab7882018-01-19 20:56:12 +0100253format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100254format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100255
Damjan Marione40231b2018-12-20 10:44:47 +0100256static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000257virtio_kick (vlib_main_t *vm, vnet_virtio_vring_t *vring, virtio_if_t *vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100258{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200259 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200260 {
261 if (vif->is_modern)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000262 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
263 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200264 else
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000265 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
266 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200267 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200268 else
269 {
270 u64 x = 1;
271 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100272
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200273 r = write (vring->kick_fd, &x, sizeof (x));
274 vring->last_kick_avail_idx = vring->avail->idx;
275 }
Damjan Marione40231b2018-12-20 10:44:47 +0100276}
277
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000278static_always_inline u8
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000279virtio_txq_is_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000280{
281 if (vring)
282 return (vring->flags & VRING_TX_SCHEDULED);
283 return 1;
284}
285
286static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000287virtio_txq_set_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000288{
289 if (vring)
290 vring->flags |= VRING_TX_SCHEDULED;
291}
292
293static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000294virtio_txq_clear_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000295{
296 if (vring)
297 vring->flags &= ~VRING_TX_SCHEDULED;
298}
299
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000300static_always_inline void
301vnet_virtio_vring_init (vnet_virtio_vring_t *vring, u16 queue_size, void *p,
302 u32 align)
303{
304 vring->queue_size = queue_size;
305 vring->desc = p;
306 vring->avail =
307 (vnet_virtio_vring_avail_t *) ((char *) p +
308 queue_size *
309 sizeof (vnet_virtio_vring_desc_t));
310 vring->used =
311 (vnet_virtio_vring_used_t
312 *) ((char *) p + ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
313 sizeof (u16) * (3 + queue_size) + align - 1) &
314 ~(align - 1)));
315 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
316}
317
318static_always_inline u16
319vnet_virtio_vring_size (u16 queue_size, u32 align)
320{
321 return ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
322 sizeof (u16) * (3 + queue_size) + align - 1) &
323 ~(align - 1)) +
324 sizeof (u16) * 3 +
325 sizeof (vnet_virtio_vring_used_elem_t) * queue_size;
326}
327
Damjan Marionf41244f2019-11-08 17:41:06 +0100328#define virtio_log_debug(vif, f, ...) \
329{ \
330 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
331 "%U: " f, format_virtio_log_name, vif, \
332 ##__VA_ARGS__); \
333};
334
335#define virtio_log_warning(vif, f, ...) \
336{ \
337 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
338 "%U: " f, format_virtio_log_name, vif, \
339 ##__VA_ARGS__); \
340};
341
342#define virtio_log_error(vif, f, ...) \
343{ \
344 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
345 "%U: " f, format_virtio_log_name, vif, \
346 ##__VA_ARGS__); \
347};
348
Damjan Marion8389fb92017-10-13 18:29:53 +0200349#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
350
351/*
352 * fd.io coding-style-patch-verification: ON
353 *
354 * Local Variables:
355 * eval: (c-set-style "gnu")
356 * End:
357 */