1 /*- 2 * Copyright (c) 2016 Solarflare Communications Inc. 3 * All rights reserved. 4 * 5 * This software was jointly developed between OKTET Labs (under contract 6 * for Solarflare) and Solarflare Communications, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright notice, 14 * this list of conditions and the following disclaimer in the documentation 15 * and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <rte_ip.h> 31 #include <rte_tcp.h> 32 33 #include "sfc.h" 34 #include "sfc_debug.h" 35 #include "sfc_tx.h" 36 #include "sfc_ev.h" 37 38 /** Standard TSO header length */ 39 #define SFC_TSOH_STD_LEN 256 40 41 /** The number of TSO option descriptors that precede the packet descriptors */ 42 #define SFC_TSO_OPDESCS_IDX_SHIFT 2 43 44 int 45 sfc_tso_alloc_tsoh_objs(struct sfc_tx_sw_desc *sw_ring, 46 unsigned int txq_entries, unsigned int socket_id) 47 { 48 unsigned int i; 49 50 for (i = 0; i < txq_entries; ++i) { 51 sw_ring[i].tsoh = rte_malloc_socket("sfc-txq-tsoh-obj", 52 SFC_TSOH_STD_LEN, 53 SFC_TX_SEG_BOUNDARY, 54 socket_id); 55 if (sw_ring[i].tsoh == NULL) 56 goto fail_alloc_tsoh_objs; 57 } 58 59 return 0; 60 61 fail_alloc_tsoh_objs: 62 while (i > 0) 63 rte_free(sw_ring[--i].tsoh); 64 65 return ENOMEM; 66 } 67 68 void 69 sfc_tso_free_tsoh_objs(struct sfc_tx_sw_desc *sw_ring, unsigned int txq_entries) 70 { 71 unsigned int i; 72 73 for (i = 0; i < txq_entries; ++i) { 74 rte_free(sw_ring[i].tsoh); 75 sw_ring[i].tsoh = NULL; 76 } 77 } 78 79 static void 80 sfc_tso_prepare_header(struct sfc_txq *txq, struct rte_mbuf **in_seg, 81 size_t *in_off, unsigned int idx, size_t bytes_left) 82 { 83 struct rte_mbuf *m = *in_seg; 84 size_t bytes_to_copy = 0; 85 uint8_t *tsoh = txq->sw_ring[idx & txq->ptr_mask].tsoh; 86 87 do { 88 bytes_to_copy = MIN(bytes_left, m->data_len); 89 90 rte_memcpy(tsoh, rte_pktmbuf_mtod(m, uint8_t *), 91 bytes_to_copy); 92 93 bytes_left -= bytes_to_copy; 94 tsoh += bytes_to_copy; 95 96 if (bytes_left > 0) { 97 m = m->next; 98 SFC_ASSERT(m != NULL); 99 } 100 } while (bytes_left > 0); 101 102 if (bytes_to_copy == m->data_len) { 103 *in_seg = m->next; 104 *in_off = 0; 105 } else { 106 *in_seg = m; 107 *in_off = bytes_to_copy; 108 } 109 } 110 111 int 112 sfc_tso_do(struct sfc_txq *txq, unsigned int idx, struct rte_mbuf **in_seg, 113 size_t *in_off, efx_desc_t **pend, unsigned int *pkt_descs, 114 size_t *pkt_len) 115 { 116 uint8_t *tsoh; 117 const struct tcp_hdr *th; 118 efsys_dma_addr_t header_paddr; 119 efsys_dma_addr_t paddr_next_frag; 120 uint16_t packet_id; 121 uint32_t sent_seq; 122 struct rte_mbuf *m = *in_seg; 123 size_t nh_off = m->l2_len; /* IP header offset */ 124 size_t tcph_off = m->l2_len + m->l3_len; /* TCP header offset */ 125 size_t header_len = m->l2_len + m->l3_len + m->l4_len; 126 const efx_nic_cfg_t *encp = efx_nic_cfg_get(txq->evq->sa->nic); 127 128 idx += SFC_TSO_OPDESCS_IDX_SHIFT; 129 130 /* Packets which have too big headers should be discarded */ 131 if (unlikely(header_len > SFC_TSOH_STD_LEN)) 132 return EMSGSIZE; 133 134 /* 135 * The TCP header must start at most 208 bytes into the frame. 136 * If it starts later than this then the NIC won't realise 137 * it's a TCP packet and TSO edits won't be applied 138 */ 139 if (unlikely(tcph_off > encp->enc_tx_tso_tcp_header_offset_limit)) 140 return EMSGSIZE; 141 142 header_paddr = rte_pktmbuf_mtophys(m); 143 paddr_next_frag = P2ROUNDUP(header_paddr + 1, SFC_TX_SEG_BOUNDARY); 144 145 /* 146 * Sometimes headers may be split across multiple mbufs. In such cases 147 * we need to glue those pieces and store them in some temporary place. 148 * Also, packet headers must be contiguous in memory, so that 149 * they can be referred to with a single DMA descriptor. Hence, handle 150 * the case where the original header crosses a 4K memory boundary 151 */ 152 if ((m->data_len < header_len) || 153 ((paddr_next_frag - header_paddr) < header_len)) { 154 sfc_tso_prepare_header(txq, in_seg, in_off, idx, header_len); 155 tsoh = txq->sw_ring[idx & txq->ptr_mask].tsoh; 156 157 header_paddr = rte_malloc_virt2phy((void *)tsoh); 158 } else { 159 if (m->data_len == header_len) { 160 *in_off = 0; 161 *in_seg = m->next; 162 } else { 163 *in_off = header_len; 164 } 165 166 tsoh = rte_pktmbuf_mtod(m, uint8_t *); 167 } 168 169 /* Handle IP header */ 170 if (m->ol_flags & PKT_TX_IPV4) { 171 const struct ipv4_hdr *iphe4; 172 173 iphe4 = (const struct ipv4_hdr *)(tsoh + nh_off); 174 rte_memcpy(&packet_id, &iphe4->packet_id, sizeof(uint16_t)); 175 packet_id = rte_be_to_cpu_16(packet_id); 176 } else if (m->ol_flags & PKT_TX_IPV6) { 177 packet_id = 0; 178 } else { 179 return EINVAL; 180 } 181 182 /* Handle TCP header */ 183 th = (const struct tcp_hdr *)(tsoh + tcph_off); 184 185 rte_memcpy(&sent_seq, &th->sent_seq, sizeof(uint32_t)); 186 sent_seq = rte_be_to_cpu_32(sent_seq); 187 188 efx_tx_qdesc_tso2_create(txq->common, packet_id, sent_seq, m->tso_segsz, 189 *pend, EFX_TX_FATSOV2_OPT_NDESCS); 190 191 *pend += EFX_TX_FATSOV2_OPT_NDESCS; 192 *pkt_descs += EFX_TX_FATSOV2_OPT_NDESCS; 193 194 efx_tx_qdesc_dma_create(txq->common, header_paddr, header_len, 195 B_FALSE, (*pend)++); 196 (*pkt_descs)++; 197 *pkt_len -= header_len; 198 199 return 0; 200 } 201