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 | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 23 | #include <vnet/gso/gro.h> |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 24 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 25 | #define foreach_virtio_if_flag \ |
| 26 | _(0, ADMIN_UP, "admin-up") \ |
| 27 | _(1, DELETING, "deleting") |
| 28 | |
| 29 | typedef enum |
| 30 | { |
| 31 | #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a), |
| 32 | foreach_virtio_if_flag |
| 33 | #undef _ |
| 34 | } virtio_if_flag_t; |
| 35 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 36 | #define TX_QUEUE(X) ((X*2) + 1) |
| 37 | #define RX_QUEUE(X) (X*2) |
| 38 | #define TX_QUEUE_ACCESS(X) (X/2) |
| 39 | #define RX_QUEUE_ACCESS(X) (X/2) |
| 40 | |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 41 | #define VIRTIO_NUM_RX_DESC 256 |
| 42 | #define VIRTIO_NUM_TX_DESC 256 |
| 43 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 44 | #define foreach_virtio_if_types \ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 45 | _ (TAP, 0) \ |
| 46 | _ (TUN, 1) \ |
| 47 | _ (PCI, 2) |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 48 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 49 | typedef enum |
| 50 | { |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 51 | #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b), |
| 52 | foreach_virtio_if_types |
| 53 | #undef _ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 54 | VIRTIO_IF_N_TYPES = (1 << 3), |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 55 | } virtio_if_type_t; |
| 56 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 57 | #define VIRTIO_RING_FLAG_MASK_INT 1 |
| 58 | |
| 59 | typedef struct |
| 60 | { |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 61 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 62 | clib_spinlock_t lockp; |
Mohsin Kazmi | a7a2281 | 2020-08-31 17:17:16 +0200 | [diff] [blame] | 63 | vring_desc_t *desc; |
| 64 | vring_used_t *used; |
| 65 | vring_avail_t *avail; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 66 | u16 desc_in_use; |
| 67 | u16 desc_next; |
| 68 | int kick_fd; |
| 69 | int call_fd; |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 70 | u8 buffer_pool_index; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 71 | u16 size; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 72 | u16 queue_id; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 73 | #define VRING_TX_OUT_OF_ORDER 1 |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 74 | u16 flags; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 75 | u32 *buffers; |
| 76 | u16 last_used_idx; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 77 | u16 last_kick_avail_idx; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 78 | u32 call_file_index; |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 79 | gro_flow_table_t *flow_table; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 80 | } virtio_vring_t; |
| 81 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 82 | typedef union |
| 83 | { |
| 84 | struct |
| 85 | { |
| 86 | u16 domain; |
| 87 | u8 bus; |
| 88 | u8 slot:5; |
| 89 | u8 function:3; |
| 90 | }; |
| 91 | u32 as_u32; |
| 92 | } pci_addr_t; |
| 93 | |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 94 | /* forward declaration */ |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 95 | typedef struct _virtio_pci_func virtio_pci_func_t; |
| 96 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 97 | typedef struct |
| 98 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 99 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 100 | u64 features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 101 | u32 flags; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 102 | u32 per_interface_next_index; |
| 103 | u16 num_rxqs; |
| 104 | u16 num_txqs; |
| 105 | virtio_vring_t *rxq_vrings; |
| 106 | virtio_vring_t *txq_vrings; |
| 107 | int gso_enabled; |
| 108 | int csum_offload_enabled; |
| 109 | union |
| 110 | { |
| 111 | int *tap_fds; |
| 112 | struct |
| 113 | { |
| 114 | u32 pci_dev_handle; |
| 115 | u32 msix_enabled; |
| 116 | }; |
| 117 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 118 | u16 virtio_net_hdr_sz; |
| 119 | virtio_if_type_t type; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 120 | |
| 121 | u32 hw_if_index; |
| 122 | u32 sw_if_index; |
| 123 | |
| 124 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 125 | int packet_coalesce; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 126 | u32 dev_instance; |
| 127 | u32 numa_node; |
| 128 | u64 remote_features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 129 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 130 | /* error */ |
| 131 | clib_error_t *error; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 132 | u8 mac_addr[6]; |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 133 | union |
| 134 | { |
| 135 | struct /* tun/tap interface */ |
| 136 | { |
| 137 | ip6_address_t host_ip6_addr; |
| 138 | int *vhost_fds; |
| 139 | u8 *host_if_name; |
| 140 | u8 *net_ns; |
| 141 | u8 *host_bridge; |
| 142 | u8 host_mac_addr[6]; |
| 143 | u32 id; |
| 144 | u32 host_mtu_size; |
| 145 | u32 tap_flags; |
| 146 | int ifindex; |
| 147 | ip4_address_t host_ip4_addr; |
| 148 | u8 host_ip4_prefix_len; |
| 149 | u8 host_ip6_prefix_len; |
| 150 | }; |
| 151 | struct /* native virtio */ |
| 152 | { |
| 153 | void *bar; |
| 154 | virtio_vring_t *cxq_vring; |
| 155 | pci_addr_t pci_addr; |
| 156 | u32 bar_id; |
| 157 | u32 notify_off_multiplier; |
| 158 | u32 is_modern; |
| 159 | u16 common_offset; |
| 160 | u16 notify_offset; |
| 161 | u16 device_offset; |
| 162 | u16 isr_offset; |
| 163 | u16 max_queue_pairs; |
| 164 | u16 msix_table_size; |
| 165 | u8 support_int_mode; /* support interrupt mode */ |
| 166 | u8 status; |
| 167 | }; |
| 168 | }; |
Mohsin Kazmi | a0a6833 | 2020-07-16 12:55:42 +0000 | [diff] [blame] | 169 | const virtio_pci_func_t *virtio_pci_func; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 170 | } virtio_if_t; |
| 171 | |
| 172 | typedef struct |
| 173 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 174 | /* logging */ |
| 175 | vlib_log_class_t log_default; |
| 176 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 177 | virtio_if_t *interfaces; |
| 178 | } virtio_main_t; |
| 179 | |
| 180 | extern virtio_main_t virtio_main; |
| 181 | extern vnet_device_class_t virtio_device_class; |
| 182 | extern vlib_node_registration_t virtio_input_node; |
| 183 | |
| 184 | clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, |
| 185 | u16 sz); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 186 | clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, |
| 187 | u32 idx); |
| 188 | clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, |
| 189 | u32 idx); |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 190 | void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, |
| 191 | u32 idx); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 192 | extern void virtio_free_used_desc (vlib_main_t * vm, virtio_vring_t * vring); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 193 | extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring); |
| 194 | extern void virtio_set_net_hdr_size (virtio_if_t * vif); |
| 195 | extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, |
| 196 | u32 type); |
Mohsin Kazmi | 9e2a785 | 2020-08-13 18:57:26 +0200 | [diff] [blame] | 197 | extern void virtio_set_packet_coalesce (virtio_if_t * vif); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 198 | extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, |
| 199 | virtio_if_t * vif, u16 queue_id); |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 200 | extern void virtio_pci_modern_notify_queue (vlib_main_t * vm, |
| 201 | virtio_if_t * vif, u16 queue_id); |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 202 | format_function_t format_virtio_device_name; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 203 | format_function_t format_virtio_log_name; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 204 | |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 205 | static_always_inline void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 206 | 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] | 207 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 208 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
Mohsin Kazmi | 379aac3 | 2020-08-20 10:25:12 +0200 | [diff] [blame] | 209 | { |
| 210 | if (vif->is_modern) |
| 211 | virtio_pci_modern_notify_queue (vm, vif, vring->queue_id); |
| 212 | else |
| 213 | virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id); |
| 214 | } |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 215 | else |
| 216 | { |
| 217 | u64 x = 1; |
| 218 | int __clib_unused r; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 219 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 220 | r = write (vring->kick_fd, &x, sizeof (x)); |
| 221 | vring->last_kick_avail_idx = vring->avail->idx; |
| 222 | } |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 225 | #define virtio_log_debug(vif, f, ...) \ |
| 226 | { \ |
| 227 | vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \ |
| 228 | "%U: " f, format_virtio_log_name, vif, \ |
| 229 | ##__VA_ARGS__); \ |
| 230 | }; |
| 231 | |
| 232 | #define virtio_log_warning(vif, f, ...) \ |
| 233 | { \ |
| 234 | vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \ |
| 235 | "%U: " f, format_virtio_log_name, vif, \ |
| 236 | ##__VA_ARGS__); \ |
| 237 | }; |
| 238 | |
| 239 | #define virtio_log_error(vif, f, ...) \ |
| 240 | { \ |
| 241 | vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \ |
| 242 | "%U: " f, format_virtio_log_name, vif, \ |
| 243 | ##__VA_ARGS__); \ |
| 244 | }; |
| 245 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 246 | #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */ |
| 247 | |
| 248 | /* |
| 249 | * fd.io coding-style-patch-verification: ON |
| 250 | * |
| 251 | * Local Variables: |
| 252 | * eval: (c-set-style "gnu") |
| 253 | * End: |
| 254 | */ |