Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 1 | /* |
| 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 Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 21 | #include <vnet/devices/virtio/virtio_std.h> |
| 22 | #include <vnet/devices/virtio/vhost_std.h> |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 23 | #include <vnet/devices/virtio/virtio_buffering.h> |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 24 | #include <vnet/gso/gro.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 25 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 26 | #define foreach_virtio_if_flag \ |
| 27 | _(0, ADMIN_UP, "admin-up") \ |
| 28 | _(1, DELETING, "deleting") |
| 29 | |
| 30 | typedef 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 Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 37 | #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 Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 42 | #define VIRTIO_NUM_RX_DESC 256 |
| 43 | #define VIRTIO_NUM_TX_DESC 256 |
| 44 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 45 | #define foreach_virtio_if_types \ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 46 | _ (TAP, 0) \ |
| 47 | _ (TUN, 1) \ |
| 48 | _ (PCI, 2) |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 49 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 50 | typedef enum |
| 51 | { |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 52 | #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b), |
| 53 | foreach_virtio_if_types |
| 54 | #undef _ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 55 | VIRTIO_IF_N_TYPES = (1 << 3), |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 56 | } virtio_if_type_t; |
| 57 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 58 | #define VIRTIO_RING_FLAG_MASK_INT 1 |
| 59 | |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 60 | #define VIRTIO_EVENT_START_TIMER 1 |
| 61 | #define VIRTIO_EVENT_STOP_TIMER 2 |
| 62 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 63 | typedef struct |
| 64 | { |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 65 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 66 | clib_spinlock_t lockp; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 67 | 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 Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 85 | u32 queue_index; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 86 | u16 desc_in_use; |
| 87 | u16 desc_next; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 88 | u16 last_used_idx; |
| 89 | u16 last_kick_avail_idx; |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 90 | union |
| 91 | { |
| 92 | struct |
| 93 | { |
| 94 | int kick_fd; |
| 95 | int call_fd; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 96 | u32 call_file_index; |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 97 | }; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 98 | struct |
| 99 | { |
| 100 | u16 avail_wrap_counter; |
| 101 | u16 used_wrap_counter; |
| 102 | u16 queue_notify_offset; |
| 103 | }; |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 104 | }; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 105 | #define VRING_TX_OUT_OF_ORDER 1 |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 106 | u16 flags; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 107 | u8 buffer_pool_index; |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 108 | vnet_hw_if_rx_mode mode; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 109 | virtio_vring_buffering_t *buffering; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 110 | gro_flow_table_t *flow_table; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 111 | } virtio_vring_t; |
| 112 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 113 | typedef union |
| 114 | { |
| 115 | struct |
| 116 | { |
| 117 | u16 domain; |
| 118 | u8 bus; |
| 119 | u8 slot:5; |
| 120 | u8 function:3; |
| 121 | }; |
| 122 | u32 as_u32; |
| 123 | } pci_addr_t; |
| 124 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 125 | /* forward declaration */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 126 | typedef struct _virtio_pci_func virtio_pci_func_t; |
| 127 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 128 | typedef struct |
| 129 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 130 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 131 | u64 features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 132 | u32 flags; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 133 | u32 per_interface_next_index; |
| 134 | u16 num_rxqs; |
| 135 | u16 num_txqs; |
| 136 | virtio_vring_t *rxq_vrings; |
| 137 | virtio_vring_t *txq_vrings; |
| 138 | int gso_enabled; |
| 139 | int csum_offload_enabled; |
| 140 | union |
| 141 | { |
| 142 | int *tap_fds; |
| 143 | struct |
| 144 | { |
| 145 | u32 pci_dev_handle; |
| 146 | u32 msix_enabled; |
| 147 | }; |
| 148 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 149 | u16 virtio_net_hdr_sz; |
| 150 | virtio_if_type_t type; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 151 | |
| 152 | u32 hw_if_index; |
| 153 | u32 sw_if_index; |
| 154 | |
| 155 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 156 | int packet_coalesce; |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 157 | int packet_buffering; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 158 | u32 dev_instance; |
| 159 | u32 numa_node; |
| 160 | u64 remote_features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 161 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 162 | /* error */ |
| 163 | clib_error_t *error; |
Benoît Ganne | 3f0ae66 | 2020-09-09 12:50:07 +0200 | [diff] [blame] | 164 | union |
| 165 | { |
| 166 | struct |
| 167 | { |
| 168 | u32 mac_addr32; |
| 169 | u16 mac_addr16; |
| 170 | }; |
| 171 | u8 mac_addr[6]; |
| 172 | }; |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 173 | union |
| 174 | { |
| 175 | struct /* tun/tap interface */ |
| 176 | { |
| 177 | ip6_address_t host_ip6_addr; |
| 178 | int *vhost_fds; |
| 179 | u8 *host_if_name; |
| 180 | u8 *net_ns; |
| 181 | u8 *host_bridge; |
| 182 | u8 host_mac_addr[6]; |
| 183 | u32 id; |
| 184 | u32 host_mtu_size; |
| 185 | u32 tap_flags; |
| 186 | int ifindex; |
| 187 | ip4_address_t host_ip4_addr; |
| 188 | u8 host_ip4_prefix_len; |
| 189 | u8 host_ip6_prefix_len; |
Matthew Smith | bd50ed1 | 2020-07-24 13:38:03 -0500 | [diff] [blame] | 190 | u8 host_carrier_up; /* host tun/tap driver link carrier state */ |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 191 | }; |
| 192 | struct /* native virtio */ |
| 193 | { |
| 194 | void *bar; |
| 195 | virtio_vring_t *cxq_vring; |
| 196 | pci_addr_t pci_addr; |
| 197 | u32 bar_id; |
| 198 | u32 notify_off_multiplier; |
| 199 | u32 is_modern; |
| 200 | u16 common_offset; |
| 201 | u16 notify_offset; |
| 202 | u16 device_offset; |
| 203 | u16 isr_offset; |
| 204 | u16 max_queue_pairs; |
| 205 | u16 msix_table_size; |
| 206 | u8 support_int_mode; /* support interrupt mode */ |
| 207 | u8 status; |
| 208 | }; |
| 209 | }; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 210 | const virtio_pci_func_t *virtio_pci_func; |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 211 | int is_packed; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 212 | } virtio_if_t; |
| 213 | |
| 214 | typedef struct |
| 215 | { |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 216 | u32 interrupt_queues_count; |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 217 | /* logging */ |
| 218 | vlib_log_class_t log_default; |
| 219 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 220 | virtio_if_t *interfaces; |
| 221 | } virtio_main_t; |
| 222 | |
| 223 | extern virtio_main_t virtio_main; |
| 224 | extern vnet_device_class_t virtio_device_class; |
| 225 | extern vlib_node_registration_t virtio_input_node; |
Mohsin Kazmi | a5203b5 | 2020-10-12 13:01:24 +0200 | [diff] [blame] | 226 | extern vlib_node_registration_t virtio_send_interrupt_node; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 227 | |
| 228 | clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, |
| 229 | u16 sz); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 230 | clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, |
| 231 | u32 idx); |
| 232 | clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, |
| 233 | u32 idx); |
Damjan Marion | 9410053 | 2020-11-06 23:25:57 +0100 | [diff] [blame] | 234 | void virtio_vring_set_rx_queues (vlib_main_t *vm, virtio_if_t *vif); |
Mohsin Kazmi | b977d3f | 2020-11-16 16:49:30 +0100 | [diff] [blame] | 235 | extern void virtio_free_buffers (vlib_main_t * vm, virtio_vring_t * vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 236 | extern void virtio_set_net_hdr_size (virtio_if_t * vif); |
| 237 | extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, |
| 238 | u32 type); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 239 | extern void virtio_set_packet_coalesce (virtio_if_t * vif); |
Mohsin Kazmi | e347acb | 2020-09-28 10:26:33 +0000 | [diff] [blame] | 240 | clib_error_t *virtio_set_packet_buffering (virtio_if_t * vif, u16 size); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 241 | extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 242 | virtio_if_t * vif, u16 queue_id, |
| 243 | u16 queue_notify_offset); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 244 | extern void virtio_pci_modern_notify_queue (vlib_main_t * vm, |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 245 | virtio_if_t * vif, u16 queue_id, |
| 246 | u16 queue_notify_offset); |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 247 | format_function_t format_virtio_device_name; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 248 | format_function_t format_virtio_log_name; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 249 | |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 250 | static_always_inline void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 251 | virtio_kick (vlib_main_t * vm, virtio_vring_t * vring, virtio_if_t * vif) |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 252 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 253 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 254 | { |
| 255 | if (vif->is_modern) |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 256 | virtio_pci_modern_notify_queue (vm, vif, vring->queue_id, |
| 257 | vring->queue_notify_offset); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 258 | else |
Mohsin Kazmi | 162a296 | 2020-09-29 10:01:25 +0000 | [diff] [blame] | 259 | virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id, |
| 260 | vring->queue_notify_offset); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 261 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 262 | else |
| 263 | { |
| 264 | u64 x = 1; |
| 265 | int __clib_unused r; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 266 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 267 | r = write (vring->kick_fd, &x, sizeof (x)); |
| 268 | vring->last_kick_avail_idx = vring->avail->idx; |
| 269 | } |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 272 | #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 Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 293 | #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 | */ |