1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #ifndef _IF_VTNETVAR_H 32 #define _IF_VTNETVAR_H 33 34 struct vtnet_softc; 35 36 struct vtnet_statistics { 37 uint64_t mbuf_alloc_failed; 38 39 uint64_t rx_frame_too_large; 40 uint64_t rx_enq_replacement_failed; 41 uint64_t rx_mergeable_failed; 42 uint64_t rx_csum_bad_ethtype; 43 uint64_t rx_csum_bad_ipproto; 44 uint64_t rx_csum_bad_offset; 45 uint64_t rx_csum_bad_proto; 46 uint64_t tx_csum_bad_ethtype; 47 uint64_t tx_tso_bad_ethtype; 48 uint64_t tx_tso_not_tcp; 49 uint64_t tx_defragged; 50 uint64_t tx_defrag_failed; 51 52 /* 53 * These are accumulated from each Rx/Tx queue. 54 */ 55 uint64_t rx_csum_failed; 56 uint64_t rx_csum_offloaded; 57 uint64_t rx_task_rescheduled; 58 uint64_t tx_csum_offloaded; 59 uint64_t tx_tso_offloaded; 60 uint64_t tx_task_rescheduled; 61 }; 62 63 struct vtnet_rxq_stats { 64 uint64_t vrxs_ipackets; /* if_ipackets */ 65 uint64_t vrxs_ibytes; /* if_ibytes */ 66 uint64_t vrxs_iqdrops; /* if_iqdrops */ 67 uint64_t vrxs_ierrors; /* if_ierrors */ 68 uint64_t vrxs_csum; 69 uint64_t vrxs_csum_failed; 70 uint64_t vrxs_rescheduled; 71 }; 72 73 struct vtnet_rxq { 74 struct mtx vtnrx_mtx; 75 struct vtnet_softc *vtnrx_sc; 76 struct virtqueue *vtnrx_vq; 77 struct sglist *vtnrx_sg; 78 int vtnrx_id; 79 struct vtnet_rxq_stats vtnrx_stats; 80 struct taskqueue *vtnrx_tq; 81 struct task vtnrx_intrtask; 82 #ifdef DEV_NETMAP 83 uint32_t vtnrx_nm_refill; 84 struct virtio_net_hdr_mrg_rxbuf vtnrx_shrhdr; 85 #endif /* DEV_NETMAP */ 86 char vtnrx_name[16]; 87 } __aligned(CACHE_LINE_SIZE); 88 89 #define VTNET_RXQ_LOCK(_rxq) mtx_lock(&(_rxq)->vtnrx_mtx) 90 #define VTNET_RXQ_UNLOCK(_rxq) mtx_unlock(&(_rxq)->vtnrx_mtx) 91 #define VTNET_RXQ_LOCK_ASSERT(_rxq) \ 92 mtx_assert(&(_rxq)->vtnrx_mtx, MA_OWNED) 93 #define VTNET_RXQ_LOCK_ASSERT_NOTOWNED(_rxq) \ 94 mtx_assert(&(_rxq)->vtnrx_mtx, MA_NOTOWNED) 95 96 struct vtnet_txq_stats { 97 uint64_t vtxs_opackets; /* if_opackets */ 98 uint64_t vtxs_obytes; /* if_obytes */ 99 uint64_t vtxs_omcasts; /* if_omcasts */ 100 uint64_t vtxs_csum; 101 uint64_t vtxs_tso; 102 uint64_t vtxs_rescheduled; 103 }; 104 105 struct vtnet_txq { 106 struct mtx vtntx_mtx; 107 struct vtnet_softc *vtntx_sc; 108 struct virtqueue *vtntx_vq; 109 struct sglist *vtntx_sg; 110 #ifndef VTNET_LEGACY_TX 111 struct buf_ring *vtntx_br; 112 #endif 113 int vtntx_id; 114 int vtntx_watchdog; 115 int vtntx_intr_threshold; 116 struct vtnet_txq_stats vtntx_stats; 117 struct taskqueue *vtntx_tq; 118 struct task vtntx_intrtask; 119 #ifndef VTNET_LEGACY_TX 120 struct task vtntx_defrtask; 121 #endif 122 #ifdef DEV_NETMAP 123 struct virtio_net_hdr_mrg_rxbuf vtntx_shrhdr; 124 #endif /* DEV_NETMAP */ 125 char vtntx_name[16]; 126 } __aligned(CACHE_LINE_SIZE); 127 128 #define VTNET_TXQ_LOCK(_txq) mtx_lock(&(_txq)->vtntx_mtx) 129 #define VTNET_TXQ_TRYLOCK(_txq) mtx_trylock(&(_txq)->vtntx_mtx) 130 #define VTNET_TXQ_UNLOCK(_txq) mtx_unlock(&(_txq)->vtntx_mtx) 131 #define VTNET_TXQ_LOCK_ASSERT(_txq) \ 132 mtx_assert(&(_txq)->vtntx_mtx, MA_OWNED) 133 #define VTNET_TXQ_LOCK_ASSERT_NOTOWNED(_txq) \ 134 mtx_assert(&(_txq)->vtntx_mtx, MA_NOTOWNED) 135 136 struct vtnet_softc { 137 device_t vtnet_dev; 138 struct ifnet *vtnet_ifp; 139 struct vtnet_rxq *vtnet_rxqs; 140 struct vtnet_txq *vtnet_txqs; 141 pfil_head_t vtnet_pfil; 142 143 uint32_t vtnet_flags; 144 #define VTNET_FLAG_MODERN 0x0001 145 #define VTNET_FLAG_MAC 0x0002 146 #define VTNET_FLAG_CTRL_VQ 0x0004 147 #define VTNET_FLAG_CTRL_RX 0x0008 148 #define VTNET_FLAG_CTRL_MAC 0x0010 149 #define VTNET_FLAG_VLAN_FILTER 0x0020 150 #define VTNET_FLAG_TSO_ECN 0x0040 151 #define VTNET_FLAG_MRG_RXBUFS 0x0080 152 #define VTNET_FLAG_LRO_NOMRG 0x0100 153 #define VTNET_FLAG_MQ 0x0200 154 #define VTNET_FLAG_INDIRECT 0x0400 155 #define VTNET_FLAG_EVENT_IDX 0x0800 156 #define VTNET_FLAG_SUSPENDED 0x1000 157 #define VTNET_FLAG_FIXUP_NEEDS_CSUM 0x2000 158 159 int vtnet_link_active; 160 int vtnet_hdr_size; 161 int vtnet_rx_process_limit; 162 int vtnet_rx_nsegs; 163 int vtnet_rx_nmbufs; 164 int vtnet_rx_clustersz; 165 int vtnet_tx_nsegs; 166 int vtnet_if_flags; 167 int vtnet_max_mtu; 168 int vtnet_act_vq_pairs; 169 int vtnet_max_vq_pairs; 170 int vtnet_requested_vq_pairs; 171 172 struct virtqueue *vtnet_ctrl_vq; 173 struct vtnet_mac_filter *vtnet_mac_filter; 174 uint32_t *vtnet_vlan_filter; 175 176 uint64_t vtnet_features; 177 struct vtnet_statistics vtnet_stats; 178 struct callout vtnet_tick_ch; 179 struct ifmedia vtnet_media; 180 eventhandler_tag vtnet_vlan_attach; 181 eventhandler_tag vtnet_vlan_detach; 182 183 struct mtx vtnet_mtx; 184 char vtnet_mtx_name[16]; 185 char vtnet_hwaddr[ETHER_ADDR_LEN]; 186 }; 187 188 static bool 189 vtnet_modern(struct vtnet_softc *sc) 190 { 191 return ((sc->vtnet_flags & VTNET_FLAG_MODERN) != 0); 192 } 193 194 /* 195 * Maximum number of queue pairs we will autoconfigure to. 196 */ 197 #define VTNET_MAX_QUEUE_PAIRS 32 198 199 /* 200 * Additional completed entries can appear in a virtqueue before we can 201 * reenable interrupts. Number of times to retry before scheduling the 202 * taskqueue to process the completed entries. 203 */ 204 #define VTNET_INTR_DISABLE_RETRIES 4 205 206 /* 207 * Similarly, additional completed entries can appear in a virtqueue 208 * between when lasted checked and before notifying the host. Number 209 * of times to retry before scheduling the taskqueue to process the 210 * queue. 211 */ 212 #define VTNET_NOTIFY_RETRIES 4 213 214 /* 215 * Number of words to allocate for the VLAN shadow table. There is one 216 * bit for each VLAN. 217 */ 218 #define VTNET_VLAN_FILTER_NWORDS (4096 / 32) 219 220 /* We depend on these being the same size (and same layout). */ 221 CTASSERT(sizeof(struct virtio_net_hdr_mrg_rxbuf) == 222 sizeof(struct virtio_net_hdr_v1)); 223 224 /* 225 * In legacy VirtIO when mergeable buffers are not negotiated, this structure 226 * is placed at the beginning of the mbuf data. Use 4 bytes of pad to keep 227 * both the VirtIO header and the data non-contiguous and the frame's payload 228 * 4 byte aligned. Note this padding would not be necessary if the 229 * VIRTIO_F_ANY_LAYOUT feature was negotiated (but we don't support that yet). 230 * 231 * In modern VirtIO or when mergeable buffers are negotiated, the host puts 232 * the VirtIO header in the beginning of the first mbuf's data. 233 */ 234 #define VTNET_RX_HEADER_PAD 4 235 struct vtnet_rx_header { 236 struct virtio_net_hdr vrh_hdr; 237 char vrh_pad[VTNET_RX_HEADER_PAD]; 238 } __packed; 239 240 /* 241 * For each outgoing frame, the vtnet_tx_header below is allocated from 242 * the vtnet_tx_header_zone. 243 */ 244 struct vtnet_tx_header { 245 union { 246 struct virtio_net_hdr hdr; 247 struct virtio_net_hdr_mrg_rxbuf mhdr; 248 struct virtio_net_hdr_v1 v1hdr; 249 } vth_uhdr; 250 251 struct mbuf *vth_mbuf; 252 }; 253 254 /* 255 * The VirtIO specification does not place a limit on the number of MAC 256 * addresses the guest driver may request to be filtered. In practice, 257 * the host is constrained by available resources. To simplify this driver, 258 * impose a reasonably high limit of MAC addresses we will filter before 259 * falling back to promiscuous or all-multicast modes. 260 */ 261 #define VTNET_MAX_MAC_ENTRIES 128 262 263 /* 264 * The driver version of struct virtio_net_ctrl_mac but with our predefined 265 * number of MAC addresses allocated. This structure is shared with the host, 266 * so nentries field is in the correct VirtIO endianness. 267 */ 268 struct vtnet_mac_table { 269 uint32_t nentries; 270 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN]; 271 } __packed; 272 273 struct vtnet_mac_filter { 274 struct vtnet_mac_table vmf_unicast; 275 uint32_t vmf_pad; /* Make tables non-contiguous. */ 276 struct vtnet_mac_table vmf_multicast; 277 }; 278 279 /* 280 * The MAC filter table is malloc(9)'d when needed. Ensure it will 281 * always fit in one segment. 282 */ 283 CTASSERT(sizeof(struct vtnet_mac_filter) <= PAGE_SIZE); 284 285 #define VTNET_TX_TIMEOUT 5 286 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP) 287 #define VTNET_CSUM_OFFLOAD_IPV6 (CSUM_TCP_IPV6 | CSUM_UDP_IPV6) 288 289 #define VTNET_CSUM_ALL_OFFLOAD \ 290 (VTNET_CSUM_OFFLOAD | VTNET_CSUM_OFFLOAD_IPV6 | CSUM_TSO) 291 292 #define VTNET_COMMON_FEATURES \ 293 (VIRTIO_NET_F_MAC | \ 294 VIRTIO_NET_F_STATUS | \ 295 VIRTIO_NET_F_MTU | \ 296 VIRTIO_NET_F_CTRL_VQ | \ 297 VIRTIO_NET_F_CTRL_RX | \ 298 VIRTIO_NET_F_CTRL_MAC_ADDR | \ 299 VIRTIO_NET_F_CTRL_VLAN | \ 300 VIRTIO_NET_F_CSUM | \ 301 VIRTIO_NET_F_HOST_TSO4 | \ 302 VIRTIO_NET_F_HOST_TSO6 | \ 303 VIRTIO_NET_F_HOST_ECN | \ 304 VIRTIO_NET_F_GUEST_CSUM | \ 305 VIRTIO_NET_F_GUEST_TSO4 | \ 306 VIRTIO_NET_F_GUEST_TSO6 | \ 307 VIRTIO_NET_F_GUEST_ECN | \ 308 VIRTIO_NET_F_MRG_RXBUF | \ 309 VIRTIO_NET_F_MQ | \ 310 VIRTIO_NET_F_SPEED_DUPLEX | \ 311 VIRTIO_RING_F_EVENT_IDX | \ 312 VIRTIO_RING_F_INDIRECT_DESC) 313 314 #define VTNET_MODERN_FEATURES (VTNET_COMMON_FEATURES) 315 #define VTNET_LEGACY_FEATURES (VTNET_COMMON_FEATURES | VIRTIO_NET_F_GSO) 316 317 /* 318 * The VIRTIO_NET_F_HOST_TSO[46] features permit us to send the host 319 * frames larger than 1514 bytes. 320 */ 321 #define VTNET_TSO_FEATURES (VIRTIO_NET_F_GSO | VIRTIO_NET_F_HOST_TSO4 | \ 322 VIRTIO_NET_F_HOST_TSO6 | VIRTIO_NET_F_HOST_ECN) 323 324 /* 325 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us 326 * frames larger than 1514 bytes. We do not yet support software LRO 327 * via tcp_lro_rx(). 328 */ 329 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \ 330 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN) 331 332 #define VTNET_MAX_MTU 65536 333 #define VTNET_MAX_RX_SIZE 65550 334 335 /* 336 * Used to preallocate the VQ indirect descriptors. Modern and mergeable 337 * buffers do not required one segment for the VirtIO header since it is 338 * placed inline at the beginning of the receive buffer. 339 */ 340 #define VTNET_RX_SEGS_HDR_INLINE 1 341 #define VTNET_RX_SEGS_HDR_SEPARATE 2 342 #define VTNET_RX_SEGS_LRO_NOMRG 34 343 #define VTNET_TX_SEGS_MIN 32 344 #define VTNET_TX_SEGS_MAX 64 345 346 CTASSERT(((VTNET_RX_SEGS_LRO_NOMRG - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE); 347 CTASSERT(((VTNET_TX_SEGS_MAX - 1) * MCLBYTES) >= VTNET_MAX_MTU); 348 349 /* 350 * Number of slots in the Tx bufrings. This value matches most other 351 * multiqueue drivers. 352 */ 353 #define VTNET_DEFAULT_BUFRING_SIZE 4096 354 355 #define VTNET_CORE_MTX(_sc) &(_sc)->vtnet_mtx 356 #define VTNET_CORE_LOCK(_sc) mtx_lock(VTNET_CORE_MTX((_sc))) 357 #define VTNET_CORE_UNLOCK(_sc) mtx_unlock(VTNET_CORE_MTX((_sc))) 358 #define VTNET_CORE_LOCK_DESTROY(_sc) mtx_destroy(VTNET_CORE_MTX((_sc))) 359 #define VTNET_CORE_LOCK_ASSERT(_sc) \ 360 mtx_assert(VTNET_CORE_MTX((_sc)), MA_OWNED) 361 #define VTNET_CORE_LOCK_ASSERT_NOTOWNED(_sc) \ 362 mtx_assert(VTNET_CORE_MTX((_sc)), MA_NOTOWNED) 363 364 #define VTNET_CORE_LOCK_INIT(_sc) do { \ 365 snprintf((_sc)->vtnet_mtx_name, sizeof((_sc)->vtnet_mtx_name), \ 366 "%s", device_get_nameunit((_sc)->vtnet_dev)); \ 367 mtx_init(VTNET_CORE_MTX((_sc)), (_sc)->vtnet_mtx_name, \ 368 "VTNET Core Lock", MTX_DEF); \ 369 } while (0) 370 371 /* 372 * Values for the init_mode argument of vtnet_init_locked(). 373 */ 374 #define VTNET_INIT_NETMAP_ENTER 1 375 #define VTNET_INIT_NETMAP_EXIT 2 376 377 #endif /* _IF_VTNETVAR_H */ 378