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 vtnet_softc { 53 device_t vtnet_dev; 54 struct ifnet *vtnet_ifp; 55 struct lwkt_serialize vtnet_slz; 56 57 uint32_t vtnet_flags; 58 #define VTNET_FLAG_LINK 0x0001 59 #define VTNET_FLAG_SUSPENDED 0x0002 60 #define VTNET_FLAG_MAC 0x0004 61 #define VTNET_FLAG_CTRL_VQ 0x0008 62 #define VTNET_FLAG_CTRL_RX 0x0010 63 #define VTNET_FLAG_CTRL_MAC 0x0020 64 #define VTNET_FLAG_VLAN_FILTER 0x0040 65 #define VTNET_FLAG_TSO_ECN 0x0080 66 #define VTNET_FLAG_MRG_RXBUFS 0x0100 67 #define VTNET_FLAG_LRO_NOMRG 0x0200 68 #define VTNET_FLAG_INDIRECT 0x0400 69 70 struct virtqueue *vtnet_rx_vq; 71 struct virtqueue *vtnet_tx_vq; 72 struct virtqueue *vtnet_ctrl_vq; 73 74 int vtnet_txhdrcount; 75 struct vtnet_tx_header *vtnet_txhdrarea; 76 SLIST_HEAD(, vtnet_tx_header) 77 vtnet_txhdr_free; 78 struct vtnet_mac_filter *vtnet_macfilter; 79 80 int vtnet_hdr_size; 81 int vtnet_tx_size; 82 int vtnet_tx_nsegs; 83 int vtnet_rx_process_limit; 84 int vtnet_rx_nsegs; 85 int vtnet_rx_mbuf_size; 86 int vtnet_rx_mbuf_count; 87 int vtnet_if_flags; 88 int vtnet_watchdog_timer; 89 uint64_t vtnet_features; 90 91 struct task vtnet_cfgchg_task; 92 93 struct vtnet_statistics vtnet_stats; 94 95 struct callout vtnet_tick_ch; 96 97 eventhandler_tag vtnet_vlan_attach; 98 eventhandler_tag vtnet_vlan_detach; 99 100 struct ifmedia vtnet_media; 101 /* 102 * Fake media type; the host does not provide us with 103 * any real media information. 104 */ 105 #define VTNET_MEDIATYPE (IFM_ETHER | IFM_1000_T | IFM_FDX) 106 char vtnet_hwaddr[ETHER_ADDR_LEN]; 107 108 /* 109 * During reset, the host's VLAN filtering table is lost. The 110 * array below is used to restore all the VLANs configured on 111 * this interface after a reset. 112 */ 113 #define VTNET_VLAN_SHADOW_SIZE (4096 / 32) 114 int vtnet_nvlans; 115 uint32_t vtnet_vlan_shadow[VTNET_VLAN_SHADOW_SIZE]; 116 }; 117 118 /* 119 * When mergeable buffers are not negotiated, the vtnet_rx_header structure 120 * below is placed at the beginning of the mbuf data. Use 2 bytes of pad to 121 * both keep the VirtIO header and the data non-contiguous and to keep the 122 * frame's payload 4 byte aligned. 123 * 124 * When mergeable buffers are negotiated, the host puts the VirtIO header in 125 * the beginning of the first mbuf's data. 126 */ 127 #define VTNET_RX_HEADER_PAD 2 128 struct vtnet_rx_header { 129 struct virtio_net_hdr vrh_hdr; 130 char vrh_pad[VTNET_RX_HEADER_PAD]; 131 } __packed; 132 133 /* 134 * For each outgoing frame, the vtnet_tx_header below is allocated from 135 * a free-list. 136 */ 137 struct vtnet_tx_header { 138 union { 139 struct virtio_net_hdr hdr; 140 struct virtio_net_hdr_mrg_rxbuf mhdr; 141 } vth_uhdr; 142 143 struct mbuf *vth_mbuf; 144 SLIST_ENTRY(vtnet_tx_header) link; 145 }; 146 147 /* 148 * The VirtIO specification does not place a limit on the number of MAC 149 * addresses the guest driver may request to be filtered. In practice, 150 * the host is constrained by available resources. To simplify this driver, 151 * impose a reasonably high limit of MAC addresses we will filter before 152 * falling back to promiscuous or all-multicast modes. 153 */ 154 #define VTNET_MAX_MAC_ENTRIES 128 155 156 struct vtnet_mac_table { 157 uint32_t nentries; 158 uint8_t macs[VTNET_MAX_MAC_ENTRIES][ETHER_ADDR_LEN]; 159 } __packed; 160 161 struct vtnet_mac_filter { 162 struct vtnet_mac_table vmf_unicast; 163 uint32_t vmf_pad; /* Make tables non-contiguous. */ 164 struct vtnet_mac_table vmf_multicast; 165 }; 166 167 #define VTNET_WATCHDOG_TIMEOUT 5 168 #define VTNET_CSUM_OFFLOAD (CSUM_TCP | CSUM_UDP) 169 170 /* Features desired/implemented by this driver. */ 171 #define VTNET_FEATURES \ 172 (VIRTIO_NET_F_MAC | \ 173 VIRTIO_NET_F_STATUS | \ 174 VIRTIO_NET_F_CTRL_VQ | \ 175 VIRTIO_NET_F_CTRL_RX | \ 176 VIRTIO_NET_F_CTRL_MAC_ADDR | \ 177 VIRTIO_NET_F_CTRL_VLAN | \ 178 VIRTIO_NET_F_CSUM | \ 179 VIRTIO_NET_F_HOST_TSO4 | \ 180 VIRTIO_NET_F_HOST_TSO6 | \ 181 VIRTIO_NET_F_HOST_ECN | \ 182 VIRTIO_NET_F_GUEST_CSUM | \ 183 VIRTIO_NET_F_GUEST_TSO4 | \ 184 VIRTIO_NET_F_GUEST_TSO6 | \ 185 VIRTIO_NET_F_GUEST_ECN | \ 186 VIRTIO_NET_F_MRG_RXBUF | \ 187 VIRTIO_RING_F_INDIRECT_DESC) 188 189 /* 190 * The VIRTIO_NET_F_GUEST_TSO[46] features permit the host to send us 191 * frames larger than 1514 bytes. We do not yet support software LRO 192 * via tcp_lro_rx(). 193 */ 194 #define VTNET_LRO_FEATURES (VIRTIO_NET_F_GUEST_TSO4 | \ 195 VIRTIO_NET_F_GUEST_TSO6 | VIRTIO_NET_F_GUEST_ECN) 196 197 #define VTNET_MAX_MTU 65536 198 #define VTNET_MAX_RX_SIZE 65550 199 200 /* 201 * Used to preallocate the Vq indirect descriptors. The first segment 202 * is reserved for the header. 203 */ 204 #define VTNET_MRG_RX_SEGS 1 205 #define VTNET_MIN_RX_SEGS 2 206 #define VTNET_MAX_RX_SEGS 34 207 #define VTNET_MIN_TX_SEGS 4 208 #define VTNET_MAX_TX_SEGS 64 209 210 #if 0 /* Copied from FreeBSD's net/if.h at some time, but not usable. */ 211 #define IFCAP_LRO 0x00400 /* can do Large Receive Offload */ 212 #define IFCAP_VLAN_HWFILTER 0x10000 /* interface hw can filter vlan tag */ 213 #define IFCAP_VLAN_HWTSO 0x40000 /* can do IFCAP_TSO on VLANs */ 214 #endif 215 216 /* 217 * This allows lots of code to at least build, even if it's never executed 218 * at the moment. 219 */ 220 #ifndef IFCAP_VLAN_HWFILTER 221 #define IFCAP_VLAN_HWFILTER 0 222 #endif 223 #ifndef IFCAP_VLAN_HWTSO 224 #define IFCAP_VLAN_HWTSO 0 225 #endif 226 227 /* 228 * Assert we can receive and transmit the maximum with regular 229 * size clusters. 230 */ 231 CTASSERT(((VTNET_MAX_RX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_RX_SIZE); 232 CTASSERT(((VTNET_MAX_TX_SEGS - 1) * MCLBYTES) >= VTNET_MAX_MTU); 233 234 /* 235 * Determine how many mbufs are in each receive buffer. For LRO without 236 * mergeable buffers, we must allocate an mbuf chain large enough to 237 * hold both the vtnet_rx_header and the maximum receivable data. 238 */ 239 #define VTNET_NEEDED_RX_MBUFS(_sc) \ 240 ((_sc)->vtnet_flags & VTNET_FLAG_LRO_NOMRG) == 0 ? 1 : \ 241 howmany(sizeof(struct vtnet_rx_header) + VTNET_MAX_RX_SIZE, \ 242 (_sc)->vtnet_rx_mbuf_size) 243 244 #endif /* _IF_VTNETVAR_H */ 245