blob: 431b1d25c268b935492871699c499919a9092560 [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 Kazmidd0144a2022-09-14 11:25:54 +0000113 u64 total_packets;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000114} vnet_virtio_vring_t;
Damjan Marion8389fb92017-10-13 18:29:53 +0200115
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200116typedef union
117{
118 struct
119 {
120 u16 domain;
121 u8 bus;
122 u8 slot:5;
123 u8 function:3;
124 };
125 u32 as_u32;
126} pci_addr_t;
127
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200128/* forward declaration */
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000129typedef struct _virtio_pci_func virtio_pci_func_t;
130
Damjan Marion8389fb92017-10-13 18:29:53 +0200131typedef struct
132{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200133 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200134 u64 features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200135 u32 flags;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200136 u32 per_interface_next_index;
137 u16 num_rxqs;
138 u16 num_txqs;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000139 vnet_virtio_vring_t *rxq_vrings;
140 vnet_virtio_vring_t *txq_vrings;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200141 int gso_enabled;
142 int csum_offload_enabled;
143 union
144 {
145 int *tap_fds;
146 struct
147 {
148 u32 pci_dev_handle;
149 u32 msix_enabled;
150 };
151 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200152 u16 virtio_net_hdr_sz;
153 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200154
155 u32 hw_if_index;
156 u32 sw_if_index;
157
158 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200159 int packet_coalesce;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000160 int packet_buffering;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200161 u32 dev_instance;
162 u32 numa_node;
163 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200164
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200165 /* error */
166 clib_error_t *error;
Benoît Ganne3f0ae662020-09-09 12:50:07 +0200167 union
168 {
169 struct
170 {
171 u32 mac_addr32;
172 u16 mac_addr16;
173 };
174 u8 mac_addr[6];
175 };
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200176 union
177 {
178 struct /* tun/tap interface */
179 {
180 ip6_address_t host_ip6_addr;
181 int *vhost_fds;
182 u8 *host_if_name;
183 u8 *net_ns;
184 u8 *host_bridge;
185 u8 host_mac_addr[6];
186 u32 id;
187 u32 host_mtu_size;
188 u32 tap_flags;
189 int ifindex;
190 ip4_address_t host_ip4_addr;
191 u8 host_ip4_prefix_len;
192 u8 host_ip6_prefix_len;
Matthew Smithbd50ed12020-07-24 13:38:03 -0500193 u8 host_carrier_up; /* host tun/tap driver link carrier state */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200194 };
195 struct /* native virtio */
196 {
197 void *bar;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000198 vnet_virtio_vring_t *cxq_vring;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200199 pci_addr_t pci_addr;
200 u32 bar_id;
201 u32 notify_off_multiplier;
202 u32 is_modern;
203 u16 common_offset;
204 u16 notify_offset;
205 u16 device_offset;
206 u16 isr_offset;
207 u16 max_queue_pairs;
208 u16 msix_table_size;
209 u8 support_int_mode; /* support interrupt mode */
210 u8 status;
211 };
212 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000213 const virtio_pci_func_t *virtio_pci_func;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100214 int is_packed;
Damjan Marion8389fb92017-10-13 18:29:53 +0200215} virtio_if_t;
216
217typedef struct
218{
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000219 u32 gro_or_buffering_if_count;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000220 /* logging */
221 vlib_log_class_t log_default;
222
Damjan Marion8389fb92017-10-13 18:29:53 +0200223 virtio_if_t *interfaces;
Filip Tehlar9f562cd2021-06-23 00:23:05 +0000224 u16 msg_id_base;
Damjan Marion8389fb92017-10-13 18:29:53 +0200225} virtio_main_t;
226
227extern virtio_main_t virtio_main;
228extern vnet_device_class_t virtio_device_class;
229extern vlib_node_registration_t virtio_input_node;
230
231clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
232 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000233clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
234 u32 idx);
235clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
236 u32 idx);
Damjan Marion94100532020-11-06 23:25:57 +0100237void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000238void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000239extern void virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200240extern void virtio_set_net_hdr_size (virtio_if_t * vif);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000241extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
242 virtio_if_type_t type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200243extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000244clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200245extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000246 virtio_if_t * vif, u16 queue_id,
247 u16 queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200248extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000249 virtio_if_t * vif, u16 queue_id,
250 u16 queue_notify_offset);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000251extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif);
252extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif);
253
Damjan Marionceab7882018-01-19 20:56:12 +0100254format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100255format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100256
Damjan Marione40231b2018-12-20 10:44:47 +0100257static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000258virtio_kick (vlib_main_t *vm, vnet_virtio_vring_t *vring, virtio_if_t *vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100259{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200260 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200261 {
262 if (vif->is_modern)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000263 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
264 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200265 else
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000266 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
267 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200268 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200269 else
270 {
271 u64 x = 1;
272 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100273
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200274 r = write (vring->kick_fd, &x, sizeof (x));
275 vring->last_kick_avail_idx = vring->avail->idx;
276 }
Damjan Marione40231b2018-12-20 10:44:47 +0100277}
278
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000279static_always_inline u8
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000280virtio_txq_is_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000281{
282 if (vring)
283 return (vring->flags & VRING_TX_SCHEDULED);
284 return 1;
285}
286
287static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000288virtio_txq_set_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000289{
290 if (vring)
291 vring->flags |= VRING_TX_SCHEDULED;
292}
293
294static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000295virtio_txq_clear_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000296{
297 if (vring)
298 vring->flags &= ~VRING_TX_SCHEDULED;
299}
300
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000301static_always_inline void
302vnet_virtio_vring_init (vnet_virtio_vring_t *vring, u16 queue_size, void *p,
303 u32 align)
304{
305 vring->queue_size = queue_size;
306 vring->desc = p;
307 vring->avail =
308 (vnet_virtio_vring_avail_t *) ((char *) p +
309 queue_size *
310 sizeof (vnet_virtio_vring_desc_t));
311 vring->used =
312 (vnet_virtio_vring_used_t
313 *) ((char *) p + ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
314 sizeof (u16) * (3 + queue_size) + align - 1) &
315 ~(align - 1)));
316 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
317}
318
319static_always_inline u16
320vnet_virtio_vring_size (u16 queue_size, u32 align)
321{
322 return ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
323 sizeof (u16) * (3 + queue_size) + align - 1) &
324 ~(align - 1)) +
325 sizeof (u16) * 3 +
326 sizeof (vnet_virtio_vring_used_elem_t) * queue_size;
327}
328
Damjan Marionf41244f2019-11-08 17:41:06 +0100329#define virtio_log_debug(vif, f, ...) \
330{ \
331 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
332 "%U: " f, format_virtio_log_name, vif, \
333 ##__VA_ARGS__); \
334};
335
336#define virtio_log_warning(vif, f, ...) \
337{ \
338 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
339 "%U: " f, format_virtio_log_name, vif, \
340 ##__VA_ARGS__); \
341};
342
343#define virtio_log_error(vif, f, ...) \
344{ \
345 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
346 "%U: " f, format_virtio_log_name, vif, \
347 ##__VA_ARGS__); \
348};
349
Damjan Marion8389fb92017-10-13 18:29:53 +0200350#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
351
352/*
353 * fd.io coding-style-patch-verification: ON
354 *
355 * Local Variables:
356 * eval: (c-set-style "gnu")
357 * End:
358 */