blob: a8e258884a4e85179210097d8f56833c48048b83 [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;
Steven Luonge9bc3322023-12-14 08:54:55 -0800143 int rss_enabled;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200144 union
145 {
146 int *tap_fds;
147 struct
148 {
149 u32 pci_dev_handle;
150 u32 msix_enabled;
151 };
152 };
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200153 u16 virtio_net_hdr_sz;
154 virtio_if_type_t type;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200155
156 u32 hw_if_index;
157 u32 sw_if_index;
158
159 CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200160 int packet_coalesce;
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000161 int packet_buffering;
Mohsin Kazmi38b09682020-06-03 18:20:17 +0200162 u32 dev_instance;
163 u32 numa_node;
164 u64 remote_features;
Damjan Marion8389fb92017-10-13 18:29:53 +0200165
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200166 /* error */
167 clib_error_t *error;
Benoît Ganne3f0ae662020-09-09 12:50:07 +0200168 union
169 {
170 struct
171 {
172 u32 mac_addr32;
173 u16 mac_addr16;
174 };
175 u8 mac_addr[6];
176 };
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200177 union
178 {
179 struct /* tun/tap interface */
180 {
181 ip6_address_t host_ip6_addr;
182 int *vhost_fds;
183 u8 *host_if_name;
184 u8 *net_ns;
185 u8 *host_bridge;
186 u8 host_mac_addr[6];
187 u32 id;
188 u32 host_mtu_size;
189 u32 tap_flags;
190 int ifindex;
191 ip4_address_t host_ip4_addr;
192 u8 host_ip4_prefix_len;
193 u8 host_ip6_prefix_len;
Matthew Smithbd50ed12020-07-24 13:38:03 -0500194 u8 host_carrier_up; /* host tun/tap driver link carrier state */
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200195 };
196 struct /* native virtio */
197 {
198 void *bar;
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000199 vnet_virtio_vring_t *cxq_vring;
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200200 pci_addr_t pci_addr;
201 u32 bar_id;
202 u32 notify_off_multiplier;
203 u32 is_modern;
204 u16 common_offset;
205 u16 notify_offset;
206 u16 device_offset;
207 u16 isr_offset;
208 u16 max_queue_pairs;
209 u16 msix_table_size;
210 u8 support_int_mode; /* support interrupt mode */
211 u8 status;
212 };
213 };
Mohsin Kazmia0a68332020-07-16 12:55:42 +0000214 const virtio_pci_func_t *virtio_pci_func;
Mohsin Kazmib977d3f2020-11-16 16:49:30 +0100215 int is_packed;
Damjan Marion8389fb92017-10-13 18:29:53 +0200216} virtio_if_t;
217
218typedef struct
219{
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000220 u32 gro_or_buffering_if_count;
Mohsin Kazmi33cc5cf2019-01-21 15:19:39 +0000221 /* logging */
222 vlib_log_class_t log_default;
223
Damjan Marion8389fb92017-10-13 18:29:53 +0200224 virtio_if_t *interfaces;
Filip Tehlar9f562cd2021-06-23 00:23:05 +0000225 u16 msg_id_base;
Damjan Marion8389fb92017-10-13 18:29:53 +0200226} virtio_main_t;
227
228extern virtio_main_t virtio_main;
229extern vnet_device_class_t virtio_device_class;
230extern vlib_node_registration_t virtio_input_node;
231
232clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx,
233 u16 sz);
Mohsin Kazmi09a3bc52019-04-02 11:45:08 +0000234clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif,
235 u32 idx);
236clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif,
237 u32 idx);
Damjan Marion94100532020-11-06 23:25:57 +0100238void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000239void virtio_vring_set_tx_queues (vlib_main_t *vm, virtio_if_t *vif);
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000240extern void virtio_free_buffers (vlib_main_t *vm, vnet_virtio_vring_t *vring);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200241extern void virtio_set_net_hdr_size (virtio_if_t * vif);
Mohsin Kazmie4efbe72021-09-22 18:56:05 +0000242extern void virtio_show (vlib_main_t *vm, u32 *hw_if_indices, u8 show_descr,
243 virtio_if_type_t type);
Mohsin Kazmi9e2a7852020-08-13 18:57:26 +0200244extern void virtio_set_packet_coalesce (virtio_if_t * vif);
Mohsin Kazmie347acb2020-09-28 10:26:33 +0000245clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size);
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200246extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000247 virtio_if_t * vif, u16 queue_id,
248 u16 queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200249extern void virtio_pci_modern_notify_queue (vlib_main_t * vm,
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000250 virtio_if_t * vif, u16 queue_id,
251 u16 queue_notify_offset);
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000252extern void virtio_pre_input_node_enable (vlib_main_t *vm, virtio_if_t *vif);
253extern void virtio_pre_input_node_disable (vlib_main_t *vm, virtio_if_t *vif);
254
Damjan Marionceab7882018-01-19 20:56:12 +0100255format_function_t format_virtio_device_name;
Damjan Marionf41244f2019-11-08 17:41:06 +0100256format_function_t format_virtio_log_name;
Damjan Marionceab7882018-01-19 20:56:12 +0100257
Damjan Marione40231b2018-12-20 10:44:47 +0100258static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000259virtio_kick (vlib_main_t *vm, vnet_virtio_vring_t *vring, virtio_if_t *vif)
Damjan Marione40231b2018-12-20 10:44:47 +0100260{
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200261 if (vif->type == VIRTIO_IF_TYPE_PCI)
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200262 {
263 if (vif->is_modern)
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000264 virtio_pci_modern_notify_queue (vm, vif, vring->queue_id,
265 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200266 else
Mohsin Kazmi162a2962020-09-29 10:01:25 +0000267 virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id,
268 vring->queue_notify_offset);
Mohsin Kazmi379aac32020-08-20 10:25:12 +0200269 }
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200270 else
271 {
272 u64 x = 1;
273 int __clib_unused r;
Damjan Marione40231b2018-12-20 10:44:47 +0100274
Mohsin Kazmid6c15af2018-10-23 18:00:47 +0200275 r = write (vring->kick_fd, &x, sizeof (x));
276 vring->last_kick_avail_idx = vring->avail->idx;
277 }
Damjan Marione40231b2018-12-20 10:44:47 +0100278}
279
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000280static_always_inline u8
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000281virtio_txq_is_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000282{
283 if (vring)
284 return (vring->flags & VRING_TX_SCHEDULED);
285 return 1;
286}
287
288static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000289virtio_txq_set_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000290{
291 if (vring)
292 vring->flags |= VRING_TX_SCHEDULED;
293}
294
295static_always_inline void
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000296virtio_txq_clear_scheduled (vnet_virtio_vring_t *vring)
Mohsin Kazmib7e4e6d2021-12-13 18:32:42 +0000297{
298 if (vring)
299 vring->flags &= ~VRING_TX_SCHEDULED;
300}
301
Mohsin Kazmi0f8912f2022-02-01 18:35:59 +0000302static_always_inline void
303vnet_virtio_vring_init (vnet_virtio_vring_t *vring, u16 queue_size, void *p,
304 u32 align)
305{
306 vring->queue_size = queue_size;
307 vring->desc = p;
308 vring->avail =
309 (vnet_virtio_vring_avail_t *) ((char *) p +
310 queue_size *
311 sizeof (vnet_virtio_vring_desc_t));
312 vring->used =
313 (vnet_virtio_vring_used_t
314 *) ((char *) p + ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
315 sizeof (u16) * (3 + queue_size) + align - 1) &
316 ~(align - 1)));
317 vring->avail->flags = VIRTIO_RING_FLAG_MASK_INT;
318}
319
320static_always_inline u16
321vnet_virtio_vring_size (u16 queue_size, u32 align)
322{
323 return ((sizeof (vnet_virtio_vring_desc_t) * queue_size +
324 sizeof (u16) * (3 + queue_size) + align - 1) &
325 ~(align - 1)) +
326 sizeof (u16) * 3 +
327 sizeof (vnet_virtio_vring_used_elem_t) * queue_size;
328}
329
Damjan Marionf41244f2019-11-08 17:41:06 +0100330#define virtio_log_debug(vif, f, ...) \
331{ \
332 vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \
333 "%U: " f, format_virtio_log_name, vif, \
334 ##__VA_ARGS__); \
335};
336
337#define virtio_log_warning(vif, f, ...) \
338{ \
339 vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \
340 "%U: " f, format_virtio_log_name, vif, \
341 ##__VA_ARGS__); \
342};
343
344#define virtio_log_error(vif, f, ...) \
345{ \
346 vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \
347 "%U: " f, format_virtio_log_name, vif, \
348 ##__VA_ARGS__); \
349};
350
Damjan Marion8389fb92017-10-13 18:29:53 +0200351#endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */
352
353/*
354 * fd.io coding-style-patch-verification: ON
355 *
356 * Local Variables:
357 * eval: (c-set-style "gnu")
358 * End:
359 */