1 /*- 2 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef _IF_VTNETVAR_H 28 #define _IF_VTNETVAR_H 29 30 struct vtnet_statistics { 31 uint64_t mbuf_alloc_failed; 32 33 uint64_t rx_frame_too_large; 34 uint64_t rx_enq_replacement_failed; 35 uint64_t rx_mergeable_failed; 36 uint64_t rx_csum_bad_ethtype; 37 uint64_t rx_csum_bad_ipproto; 38 uint64_t rx_csum_bad_offset; 39 uint64_t rx_csum_failed; 40 uint64_t rx_csum_offloaded; 41 uint64_t rx_task_rescheduled; 42 43 uint64_t tx_csum_offloaded; 44 uint64_t tx_tso_offloaded; 45 uint64_t tx_csum_bad_ethtype; 46 uint64_t tx_tso_bad_ethtype; 47 uint64_t tx_defragged; 48 uint64_t tx_defrag_failed; 49 uint64_t tx_task_rescheduled; 50 }; 51 52 struct irqmap { 53 int irq; 54 driver_intr_t *handler; 55 }; 56 57 struct vtnet_softc { 58 device_t vtnet_dev; 59 struct ifnet *vtnet_ifp; 60 struct lwkt_serialize vtnet_slz; 61 struct lwkt_serialize vtnet_rx_slz; 62 struct lwkt_serialize vtnet_tx_slz; 63 struct lwkt_serialize *serializes[3]; 64 65 uint32_t vtnet_flags; 66 #define VTNET_FLAG_LINK 0x0001 67 #define VTNET_FLAG_SUSPENDED 0x0002 68 #define VTNET_FLAG_MAC 0x0004 69 #define VTNET_FLAG_CTRL_VQ 0x0008 70 #define VTNET_FLAG_CTRL_RX 0x0010 71 #define VTNET_FLAG_CTRL_MAC 0x0020 72 #define VTNET_FLAG_VLAN_FILTER 0x0040 73 #define VTNET_FLAG_TSO_ECN 0x0080 74 #define VTNET_FLAG_MRG_RXBUFS 0x0100 75 #define VTNET_FLAG_LRO_NOMRG 0x0200 76 #define VTNET_FLAG_INDIRECT 0x0400 77 78 struct virtqueue *vtnet_rx_vq; 79 struct virtqueue *vtnet_tx_vq; 80 struct virtqueue *vtnet_ctrl_vq; 81 82 /* controlq, rx, tx and config_change */ 83 int vtnet_cpus[4]; 84 int vtnet_nintr; 85 86 struct irqmap vtnet_irqmap[2]; 87 struct lwkt_serialize *vtnet_intr_slz[3]; 88 89 int vtnet_txhdrcount; 90 struct vtnet_tx_header *vtnet_txhdrarea; 91 SLIST_HEAD(, vtnet_tx_header) 92 vtnet_txhdr_free; 93 struct vtnet_mac_filter *vtnet_macfilter; 94 95 int vtnet_hdr_size; 96 int vtnet_tx_size; 97 int vtnet_tx_nsegs; 98 int vtnet_rx_process_limit; 99 int vtnet_rx_nsegs; 100 int vtnet_rx_mbuf_size; 101 int vtnet_rx_mbuf_count; 102 int vtnet_if_flags; 103 struct ifsubq_watchdog vtnet_tx_watchdog; 104 uint64_t vtnet_features; 105 106 struct vtnet_statistics vtnet_stats; 107 108 eventhandler_tag vtnet_vlan_attach; 109 eventhandler_tag vtnet_vlan_detach; 110 111 struct ifmedia vtnet_media; 112 /* 113 * Fake media type; the host does not provide us with 114 * any real media information. 115 */ 116 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX) 117 char vtnet_hwaddr[ETHER_ADDR_LEN]; 118 119 /* 120 * During reset, the host's VLAN filtering table is lost. The 121 * array below is used to restore all the VLANs configured on 122 * this interface after a reset. 123 */ 124 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32) 125 int vtnet_nvlans; 126 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE]; 127 }; 128 129 /* 130 * When mergeable buffers are not negotiated, the vtnet_rx_header structure 131 * below is placed at the beginning of the mbuf data. Use 2 bytes of pad to 132 * both keep the VirtIO header and the data non-contiguous and to keep the 133 * frame's payload 4 byte aligned. 134 * 135 * When mergeable buffers are negotiated, the host puts the VirtIO header in 136 * the beginning of the first mbuf's data. 137 */ 138 #define VTNET_RX_HEADER_PAD 2 139 struct vtnet_rx_header { 140 struct virtio_net_hdr vrh_hdr; 141 char vrh_pad[VTNET_RX_HEADER_PAD]; 142 } __packed; 143 144 /* 145 * For each outgoing frame, the vtnet_tx_header below is allocated from 146 * a free-list. 147 */ 148 struct vtnet_tx_header { 149 union { 150 struct virtio_net_hdr hdr; 151 struct virtio_net_hdr_mrg_rxbuf mhdr; 152 } vth_uhdr; 153 154 struct mbuf *vth_mbuf; 155 SLIST_ENTRY(vtnet_tx_header) link; 156 }; 157 158 /* 159 * The VirtIO specification does not place a limit on the number of MAC 160 * addresses the guest driver may request to be filtered. In practice, 161 * the host is constrained by available resources. To simplify this driver, 162 * impose a reasonably high limit of MAC addresses we will filter before 163 * falling back to promiscuous or all-multicast modes. 164 */ 165 #define VTNET_MAX_MAC_ENTRIES 128 166 167 struct vtnet_mac_table { 168 uint32_t nentries; 169 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN]; 170 } __packed; 171 172 struct vtnet_mac_filter { 173 struct vtnet_mac_table vmf_unicast; 174 uint32_t vmf_pad; /* Make tables non-contiguous. */ 175 struct vtnet_mac_table vmf_multicast; 176 }; 177 178 #define VTNET_WATCHDOG_TIMEOUT 5 179 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP) 180 181 /* Features desired/implemented by this driver. */ 182 #define VTNET_FEATURES \ 183 (VIRTIO_NET_F_MAC | \ 184 VIRTIO_NET_F_STATUS | \ 185 VIRTIO_NET_F_CTRL_VQ | \ 186 VIRTIO_NET_F_CTRL_RX | \ 187 VIRTIO_NET_F_CTRL_MAC_ADDR | \ 188 VIRTIO_NET_F_CTRL_VLAN | \ 189 VIRTIO_NET_F_CSUM | \ 190 VIRTIO_NET_F_HOST_TSO4 | \ 191 VIRTIO_NET_F_HOST_TSO6 | \ 192 VIRTIO_NET_F_HOST_ECN | \ 193 VIRTIO_NET_F_GUEST_CSUM | \ 194 VIRTIO_NET_F_GUEST_TSO4 | \ 195 VIRTIO_NET_F_GUEST_TSO6 | \ 196 VIRTIO_NET_F_GUEST_ECN | \ 197 VIRTIO_NET_F_MRG_RXBUF | \ 198 VIRTIO_RING_F_INDIRECT_DESC) 199 200 /* 201 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us 202 * frames larger than 1514 bytes. We do not yet support software LRO 203 * via tcp_lro_rx(). 204 */ 205 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \ 206 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN) 207 208 #define VTNET_MAX_MTU 65536 209 #define VTNET_MAX_RX_SIZE 65550 210 211 /* 212 * Used to preallocate the Vq indirect descriptors. The first segment 213 * is reserved for the header. 214 */ 215 #define VTNET_MRG_RX_SEGS 1 216 #define VTNET_MIN_RX_SEGS 2 217 #define VTNET_MAX_RX_SEGS 34 218 #define VTNET_MIN_TX_SEGS 4 219 #define VTNET_MAX_TX_SEGS 64 220 221 #if 0 /* Copied from FreeBSD's net/if.h at some time, but not usable. */ 222 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */ 223 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */ 224 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */ 225 #endif 226 227 /* 228 * This allows lots of code to at least build, even if it's never executed 229 * at the moment. 230 */ 231 #ifndef IFCAP_VLAN_HWFILTER 232 #define IFCAP_VLAN_HWFILTER 0 233 #endif 234 #ifndef IFCAP_VLAN_HWTSO 235 #define IFCAP_VLAN_HWTSO 0 236 #endif 237 238 /* 239 * Assert we can receive and transmit the maximum with regular 240 * size clusters. 241 */ 242 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE); 243 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU); 244 245 /* 246 * Determine how many mbufs are in each receive buffer. For LRO without 247 * mergeable buffers, we must allocate an mbuf chain large enough to 248 * hold both the vtnet_rx_header and the maximum receivable data. 249 */ 250 #define VTNET_NEEDED_RX_MBUFS(_sc) \ 251 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \ 252 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \ 253 (_sc)->vtnet_rx_mbuf_size) 254 255 #endif /* _IF_VTNETVAR_H */ 256