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 */ \ |
| 52 | _ (VIRTIO_F_ANY_LAYOUT, 27) /* Can the device handle any descripor layout */ \ |
| 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 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 61 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 62 | #define foreach_virtio_if_flag \ |
| 63 | _(0, ADMIN_UP, "admin-up") \ |
| 64 | _(1, DELETING, "deleting") |
| 65 | |
| 66 | typedef enum |
| 67 | { |
| 68 | #define _(a, b, c) VIRTIO_IF_FLAG_##b = (1 << a), |
| 69 | foreach_virtio_if_flag |
| 70 | #undef _ |
| 71 | } virtio_if_flag_t; |
| 72 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 73 | #define VIRTIO_NUM_RX_DESC 256 |
| 74 | #define VIRTIO_NUM_TX_DESC 256 |
| 75 | |
| 76 | #define VIRTIO_FEATURE(X) (1ULL << X) |
| 77 | |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 78 | #define TX_QUEUE(X) ((X*2) + 1) |
| 79 | #define RX_QUEUE(X) (X*2) |
| 80 | #define TX_QUEUE_ACCESS(X) (X/2) |
| 81 | #define RX_QUEUE_ACCESS(X) (X/2) |
| 82 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 83 | typedef enum |
| 84 | { |
| 85 | VIRTIO_IF_TYPE_TAP, |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 86 | VIRTIO_IF_TYPE_PCI, |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 87 | VIRTIO_IF_N_TYPES, |
| 88 | } virtio_if_type_t; |
| 89 | |
| 90 | |
| 91 | typedef struct |
| 92 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 93 | u8 mac[6]; |
| 94 | u16 status; |
| 95 | u16 max_virtqueue_pairs; |
| 96 | u16 mtu; |
| 97 | } virtio_net_config_t; |
| 98 | |
| 99 | #define VIRTIO_RING_FLAG_MASK_INT 1 |
| 100 | |
| 101 | typedef struct |
| 102 | { |
David Johnson | d9818dd | 2018-12-14 14:53:41 -0500 | [diff] [blame] | 103 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 104 | struct vring_desc *desc; |
| 105 | struct vring_used *used; |
| 106 | struct vring_avail *avail; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 107 | clib_spinlock_t lockp; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 108 | u16 desc_in_use; |
| 109 | u16 desc_next; |
| 110 | int kick_fd; |
| 111 | int call_fd; |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 112 | u8 buffer_pool_index; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 113 | u16 size; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 114 | u16 queue_id; |
| 115 | u16 flags; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 116 | u32 call_file_index; |
| 117 | u32 *buffers; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 118 | u32 *indirect_buffers; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 119 | u16 last_used_idx; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 120 | u16 last_kick_avail_idx; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 121 | } virtio_vring_t; |
| 122 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 123 | typedef union |
| 124 | { |
| 125 | struct |
| 126 | { |
| 127 | u16 domain; |
| 128 | u8 bus; |
| 129 | u8 slot:5; |
| 130 | u8 function:3; |
| 131 | }; |
| 132 | u32 as_u32; |
| 133 | } pci_addr_t; |
| 134 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 135 | typedef struct |
| 136 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 137 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 138 | u32 flags; |
Damjan Marion | 829ee53 | 2018-02-16 16:13:32 +0100 | [diff] [blame] | 139 | clib_spinlock_t lockp; |
| 140 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 141 | u32 dev_instance; |
| 142 | u32 hw_if_index; |
| 143 | u32 sw_if_index; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 144 | u32 numa_node; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 145 | u16 virtio_net_hdr_sz; |
| 146 | virtio_if_type_t type; |
| 147 | union |
| 148 | { |
| 149 | u32 id; |
| 150 | pci_addr_t pci_addr; |
| 151 | }; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 152 | u32 per_interface_next_index; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 153 | union |
| 154 | { |
| 155 | int fd; |
| 156 | u32 msix_enabled; |
| 157 | }; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 158 | union |
| 159 | { |
| 160 | int tap_fd; |
| 161 | u32 pci_dev_handle; |
| 162 | }; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 163 | virtio_vring_t *rxq_vrings; |
| 164 | virtio_vring_t *txq_vrings; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 165 | u64 features, remote_features; |
| 166 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 167 | /* error */ |
| 168 | clib_error_t *error; |
Mohsin Kazmi | b74fe32 | 2019-01-31 13:50:56 +0000 | [diff] [blame] | 169 | u8 support_int_mode; /* support interrupt mode */ |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 170 | u16 max_queue_pairs; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 171 | u16 num_rxqs; |
| 172 | u16 num_txqs; |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 173 | u8 status; |
| 174 | u8 mac_addr[6]; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 175 | u8 *host_if_name; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 176 | u8 *net_ns; |
Milan Lenco | 73e7f42 | 2017-12-14 10:04:25 +0100 | [diff] [blame] | 177 | u8 *host_bridge; |
| 178 | u8 host_mac_addr[6]; |
| 179 | ip4_address_t host_ip4_addr; |
| 180 | u8 host_ip4_prefix_len; |
| 181 | ip6_address_t host_ip6_addr; |
| 182 | u8 host_ip6_prefix_len; |
Mohsin Kazmi | e4ac48e | 2019-04-17 12:06:57 +0200 | [diff] [blame] | 183 | u32 tap_file_index; |
Andrew Yourtchenko | 6a7cff7 | 2018-10-12 16:09:22 +0200 | [diff] [blame] | 184 | int gso_enabled; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 185 | int ifindex; |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 186 | virtio_vring_t *cxq_vring; |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 187 | } virtio_if_t; |
| 188 | |
| 189 | typedef struct |
| 190 | { |
Mohsin Kazmi | 33cc5cf | 2019-01-21 15:19:39 +0000 | [diff] [blame] | 191 | /* logging */ |
| 192 | vlib_log_class_t log_default; |
| 193 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 194 | virtio_if_t *interfaces; |
| 195 | } virtio_main_t; |
| 196 | |
| 197 | extern virtio_main_t virtio_main; |
| 198 | extern vnet_device_class_t virtio_device_class; |
| 199 | extern vlib_node_registration_t virtio_input_node; |
| 200 | |
| 201 | clib_error_t *virtio_vring_init (vlib_main_t * vm, virtio_if_t * vif, u16 idx, |
| 202 | u16 sz); |
Mohsin Kazmi | 09a3bc5 | 2019-04-02 11:45:08 +0000 | [diff] [blame] | 203 | clib_error_t *virtio_vring_free_rx (vlib_main_t * vm, virtio_if_t * vif, |
| 204 | u32 idx); |
| 205 | clib_error_t *virtio_vring_free_tx (vlib_main_t * vm, virtio_if_t * vif, |
| 206 | u32 idx); |
Mohsin Kazmi | 80659b4 | 2019-01-31 13:18:00 +0000 | [diff] [blame] | 207 | void virtio_vring_set_numa_node (vlib_main_t * vm, virtio_if_t * vif, |
| 208 | u32 idx); |
Steven | a624dbe | 2018-01-09 11:13:29 -0800 | [diff] [blame] | 209 | 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] | 210 | extern void virtio_free_rx_buffers (vlib_main_t * vm, virtio_vring_t * vring); |
| 211 | extern void virtio_set_net_hdr_size (virtio_if_t * vif); |
| 212 | extern void virtio_show (vlib_main_t * vm, u32 * hw_if_indices, u8 show_descr, |
| 213 | u32 type); |
| 214 | extern void virtio_pci_legacy_notify_queue (vlib_main_t * vm, |
| 215 | virtio_if_t * vif, u16 queue_id); |
Damjan Marion | ceab788 | 2018-01-19 20:56:12 +0100 | [diff] [blame] | 216 | format_function_t format_virtio_device_name; |
| 217 | |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 218 | static_always_inline void |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 219 | 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] | 220 | { |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 221 | if (vif->type == VIRTIO_IF_TYPE_PCI) |
| 222 | virtio_pci_legacy_notify_queue (vm, vif, vring->queue_id); |
| 223 | else |
| 224 | { |
| 225 | u64 x = 1; |
| 226 | int __clib_unused r; |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 227 | |
Mohsin Kazmi | d6c15af | 2018-10-23 18:00:47 +0200 | [diff] [blame] | 228 | r = write (vring->kick_fd, &x, sizeof (x)); |
| 229 | vring->last_kick_avail_idx = vring->avail->idx; |
| 230 | } |
Damjan Marion | e40231b | 2018-12-20 10:44:47 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Damjan Marion | 8389fb9 | 2017-10-13 18:29:53 +0200 | [diff] [blame] | 233 | #endif /* _VNET_DEVICES_VIRTIO_VIRTIO_H_ */ |
| 234 | |
| 235 | /* |
| 236 | * fd.io coding-style-patch-verification: ON |
| 237 | * |
| 238 | * Local Variables: |
| 239 | * eval: (c-set-style "gnu") |
| 240 | * End: |
| 241 | */ |