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 | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 21 | #include <linux/virtio_config.h> |
| 22 | #include <linux/virtio_net.h> |
| 23 | #include <linux/virtio_pci.h> |
| 24 | #include <linux/virtio_ring.h> |
| 25 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 26 | #define foreach_virtio_net_features \ |
| 27 | _ (VIRTIO_NET_F_CSUM, 0) /* Host handles pkts w/ partial csum */ \ |
| 28 | _ (VIRTIO_NET_F_GUEST_CSUM, 1) /* Guest handles pkts w/ partial csum */ \ |
| 29 | _ (VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, 2) /* Dynamic offload configuration. */ \ |
| 30 | _ (VIRTIO_NET_F_MTU, 3) /* Initial MTU advice. */ \ |
| 31 | _ (VIRTIO_NET_F_MAC, 5) /* Host has given MAC address. */ \ |
| 32 | _ (VIRTIO_NET_F_GSO, 6) /* Host handles pkts w/ any GSO. */ \ |
| 33 | _ (VIRTIO_NET_F_GUEST_TSO4, 7) /* Guest can handle TSOv4 in. */ \ |
| 34 | _ (VIRTIO_NET_F_GUEST_TSO6, 8) /* Guest can handle TSOv6 in. */ \ |
| 35 | _ (VIRTIO_NET_F_GUEST_ECN, 9) /* Guest can handle TSO[6] w/ ECN in. */ \ |
| 36 | _ (VIRTIO_NET_F_GUEST_UFO, 10) /* Guest can handle UFO in. */ \ |
| 37 | _ (VIRTIO_NET_F_HOST_TSO4, 11) /* Host can handle TSOv4 in. */ \ |
| 38 | _ (VIRTIO_NET_F_HOST_TSO6, 12) /* Host can handle TSOv6 in. */ \ |
| 39 | _ (VIRTIO_NET_F_HOST_ECN, 13) /* Host can handle TSO[6] w/ ECN in. */ \ |
| 40 | _ (VIRTIO_NET_F_HOST_UFO, 14) /* Host can handle UFO in. */ \ |
| 41 | _ (VIRTIO_NET_F_MRG_RXBUF, 15) /* Host can merge receive buffers. */ \ |
| 42 | _ (VIRTIO_NET_F_STATUS, 16) /* virtio_net_config.status available */ \ |
| 43 | _ (VIRTIO_NET_F_CTRL_VQ, 17) /* Control channel available */ \ |
| 44 | _ (VIRTIO_NET_F_CTRL_RX, 18) /* Control channel RX mode support */ \ |
| 45 | _ (VIRTIO_NET_F_CTRL_VLAN, 19) /* Control channel VLAN filtering */ \ |
| 46 | _ (VIRTIO_NET_F_CTRL_RX_EXTRA, 20) /* Extra RX mode control support */ \ |
| 47 | _ (VIRTIO_NET_F_GUEST_ANNOUNCE, 21) /* Guest can announce device on the network */ \ |
| 48 | _ (VIRTIO_NET_F_MQ, 22) /* Device supports Receive Flow Steering */ \ |
| 49 | _ (VIRTIO_NET_F_CTRL_MAC_ADDR, 23) /* Set MAC address */ \ |
| 50 | _ (VIRTIO_F_NOTIFY_ON_EMPTY, 24) \ |
| 51 | _ (VHOST_F_LOG_ALL, 26) /* Log all write descriptors */ \ |
Paul Vinciguerra | 97c998c | 2019-10-29 16:11:09 -0400 | [diff] [blame] | 52 | _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descriptor layout */ \ |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 53 | _ (VIRTIO_RING_F_INDIRECT_DESC, 28) /* Support indirect buffer descriptors */ \ |
| 54 | _ (VIRTIO_RING_F_EVENT_IDX, 29) /* The Guest publishes the used index for which it expects an interrupt \ |
| 55 | * at the end of the avail ring. Host should ignore the avail->flags field. */ \ |
| 56 | /* The Host publishes the avail index for which it expects a kick \ |
| 57 | * at the end of the used ring. Guest should ignore the used->flags field. */ \ |
| 58 | _ (VHOST_USER_F_PROTOCOL_FEATURES, 30) \ |
| 59 | _ (VIRTIO_F_VERSION_1, 32) |
| 60 | |
| 61 | #define foreach_virtio_if_flag \ |
| 62 | _(0, ADMIN_UP, "admin-up") \ |
| 63 | _(1, DELETING, "deleting") |
| 64 | |
| 65 | typedef enum |
| 66 | { |
| 67 | #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a), |
| 68 | foreach_virtio_if_flag |
| 69 | #undef _ |
| 70 | } virtio_if_flag_t; |
| 71 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 72 | #define VIRTIO_NUM_RX_DESC 256 |
| 73 | #define VIRTIO_NUM_TX_DESC 256 |
| 74 | |
| 75 | #define VIRTIO_FEATURE(X) (1ULL << X) |
| 76 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 77 | #define TX_QUEUE(X) ((X*2) + 1) |
| 78 | #define RX_QUEUE(X) (X*2) |
| 79 | #define TX_QUEUE_ACCESS(X) (X/2) |
| 80 | #define RX_QUEUE_ACCESS(X) (X/2) |
| 81 | |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 82 | #define foreach_virtio_if_types \ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 83 | _ (TAP, 0) \ |
| 84 | _ (TUN, 1) \ |
| 85 | _ (PCI, 2) |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 86 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 87 | typedef enum |
| 88 | { |
Mohsin Kazmi | 206acf8 | 2020-04-06 14:19:54 +0200 | [diff] [blame] | 89 | #define _(a, b) VIRTIO_IF_TYPE_##a = (1 << b), |
| 90 | foreach_virtio_if_types |
| 91 | #undef _ |
Mohsin Kazmi | d88fc0f | 2020-04-30 19:05:56 +0200 | [diff] [blame] | 92 | VIRTIO_IF_N_TYPES = (1 << 3), |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 93 | } virtio_if_type_t; |
| 94 | |
| 95 | |
| 96 | typedef struct |
| 97 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 98 | u8 mac[6]; |
| 99 | u16 status; |
| 100 | u16 max_virtqueue_pairs; |
| 101 | u16 mtu; |
| 102 | } virtio_net_config_t; |
| 103 | |
| 104 | #define VIRTIO_RING_FLAG_MASK_INT 1 |
| 105 | |
| 106 | typedef struct |
| 107 | { |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 108 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 109 | clib_spinlock_t lockp; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 110 | struct vring_desc *desc; |
| 111 | struct vring_used *used; |
| 112 | struct vring_avail *avail; |
| 113 | u16 desc_in_use; |
| 114 | u16 desc_next; |
| 115 | int kick_fd; |
| 116 | int call_fd; |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 117 | u8 buffer_pool_index; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 118 | u16 size; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 119 | u16 queue_id; |
Mohsin Kazmi | 7f6d145 | 2020-02-27 11:49:21 +0100 | [diff] [blame] | 120 | #define VRING_TX_OUT_OF_ORDER 1 |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 121 | u16 flags; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 122 | u32 *buffers; |
| 123 | u16 last_used_idx; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 124 | u16 last_kick_avail_idx; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 125 | u32 call_file_index; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 126 | } virtio_vring_t; |
| 127 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 128 | typedef union |
| 129 | { |
| 130 | struct |
| 131 | { |
| 132 | u16 domain; |
| 133 | u8 bus; |
| 134 | u8 slot:5; |
| 135 | u8 function:3; |
| 136 | }; |
| 137 | u32 as_u32; |
| 138 | } pci_addr_t; |
| 139 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 140 | typedef struct |
| 141 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 142 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 143 | u64 features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 144 | u32 flags; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 145 | u32 per_interface_next_index; |
| 146 | u16 num_rxqs; |
| 147 | u16 num_txqs; |
| 148 | virtio_vring_t *rxq_vrings; |
| 149 | virtio_vring_t *txq_vrings; |
| 150 | int gso_enabled; |
| 151 | int csum_offload_enabled; |
| 152 | union |
| 153 | { |
| 154 | int *tap_fds; |
| 155 | struct |
| 156 | { |
| 157 | u32 pci_dev_handle; |
| 158 | u32 msix_enabled; |
| 159 | }; |
| 160 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 161 | u16 virtio_net_hdr_sz; |
| 162 | virtio_if_type_t type; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 163 | |
| 164 | u32 hw_if_index; |
| 165 | u32 sw_if_index; |
| 166 | |
| 167 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline1); |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 168 | union |
| 169 | { |
| 170 | u32 id; |
| 171 | pci_addr_t pci_addr; |
| 172 | }; |
Damjan Marion | 7c6102b | 2019-11-08 17:59:56 +0100 | [diff] [blame] | 173 | int *vhost_fds; |
Mohsin Kazmi | 38b0968 | 2020-06-03 18:20:17 +0200 | [diff] [blame] | 174 | u32 dev_instance; |
| 175 | u32 numa_node; |
| 176 | u64 remote_features; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 177 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 178 | /* error */ |
| 179 | clib_error_t *error; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 180 | u8 support_int_mode; /* support interrupt mode */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 181 | u16 max_queue_pairs; |
Mohsin Kazmi | 8046fdc | 2020-07-15 11:54:10 +0000 | [diff] [blame^] | 182 | u16 msix_table_size; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 183 | u8 status; |
| 184 | u8 mac_addr[6]; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 185 | u8 *host_if_name; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 186 | u8 *net_ns; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 187 | u8 *host_bridge; |
| 188 | u8 host_mac_addr[6]; |
| 189 | ip4_address_t host_ip4_addr; |
| 190 | u8 host_ip4_prefix_len; |
| 191 | ip6_address_t host_ip6_addr; |
| 192 | u8 host_ip6_prefix_len; |
Mohsin Kazmi | 97d54ed | 2019-06-10 11:20:15 +0200 | [diff] [blame] | 193 | u32 host_mtu_size; |
Mohsin Kazmi | 86f281a | 2020-06-30 15:28:01 +0200 | [diff] [blame] | 194 | u32 tap_flags; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 195 | int ifindex; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 196 | virtio_vring_t *cxq_vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 197 | } virtio_if_t; |
| 198 | |
| 199 | typedef struct |
| 200 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 201 | /* logging */ |
| 202 | vlib_log_class_t log_default; |
| 203 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 204 | virtio_if_t *interfaces; |
| 205 | } virtio_main_t; |
| 206 | |
| 207 | extern virtio_main_t virtio_main; |
| 208 | extern vnet_device_class_t virtio_device_class; |
| 209 | extern vlib_node_registration_t virtio_input_node; |
| 210 | |
| 211 | clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, |
| 212 | u16 sz); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 213 | clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, |
| 214 | u32 idx); |
| 215 | clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, |
| 216 | u32 idx); |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 217 | void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, |
| 218 | u32 idx); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 219 | 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] | 220 | extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring); |
| 221 | extern void virtio_set_net_hdr_size (virtio_if_t * vif); |
| 222 | extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, |
| 223 | u32 type); |
| 224 | extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, |
| 225 | virtio_if_t * vif, u16 queue_id); |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 226 | format_function_t format_virtio_device_name; |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 227 | format_function_t format_virtio_log_name; |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 228 | |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 229 | static_always_inline void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 230 | 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] | 231 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 232 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
| 233 | virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id); |
| 234 | else |
| 235 | { |
| 236 | u64 x = 1; |
| 237 | int __clib_unused r; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 238 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 239 | r = write (vring->kick_fd, &x, sizeof (x)); |
| 240 | vring->last_kick_avail_idx = vring->avail->idx; |
| 241 | } |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Damjan Marion | f41244f | 2019-11-08 17:41:06 +0100 | [diff] [blame] | 244 | |
| 245 | #define virtio_log_debug(vif, f, ...) \ |
| 246 | { \ |
| 247 | vlib_log(VLIB_LOG_LEVEL_DEBUG, virtio_main.log_default, \ |
| 248 | "%U: " f, format_virtio_log_name, vif, \ |
| 249 | ##__VA_ARGS__); \ |
| 250 | }; |
| 251 | |
| 252 | #define virtio_log_warning(vif, f, ...) \ |
| 253 | { \ |
| 254 | vlib_log(VLIB_LOG_LEVEL_WARNING, virtio_main.log_default, \ |
| 255 | "%U: " f, format_virtio_log_name, vif, \ |
| 256 | ##__VA_ARGS__); \ |
| 257 | }; |
| 258 | |
| 259 | #define virtio_log_error(vif, f, ...) \ |
| 260 | { \ |
| 261 | vlib_log(VLIB_LOG_LEVEL_ERR, virtio_main.log_default, \ |
| 262 | "%U: " f, format_virtio_log_name, vif, \ |
| 263 | ##__VA_ARGS__); \ |
| 264 | }; |
| 265 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 266 | #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */ |
| 267 | |
| 268 | /* |
| 269 | * fd.io coding-style-patch-verification: ON |
| 270 | * |
| 271 | * Local Variables: |
| 272 | * eval: (c-set-style "gnu") |
| 273 | * End: |
| 274 | */ |