Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause 2 : : * Copyright(c) 2023 Intel Corporation 3 : : */ 4 : : #ifndef _GRO_TCP_H_ 5 : : #define _GRO_TCP_H_ 6 : : 7 : : #define INVALID_ARRAY_INDEX 0xffffffffUL 8 : : 9 : : #include <rte_tcp.h> 10 : : 11 : : /* 12 : : * The max length of a IPv4 packet, which includes the length of the L3 13 : : * header, the L4 header and the data payload. 14 : : */ 15 : : #define MAX_IP_PKT_LENGTH UINT16_MAX 16 : : 17 : : /* The maximum TCP header length */ 18 : : #define MAX_TCP_HLEN 60 19 : : #define INVALID_TCP_HDRLEN(len) \ 20 : : (((len) < sizeof(struct rte_tcp_hdr)) || ((len) > MAX_TCP_HLEN)) 21 : : 22 : : struct cmn_tcp_key { 23 : : struct rte_ether_addr eth_saddr; 24 : : struct rte_ether_addr eth_daddr; 25 : : uint32_t recv_ack; 26 : : uint16_t src_port; 27 : : uint16_t dst_port; 28 : : }; 29 : : 30 : : #define ASSIGN_COMMON_TCP_KEY(k1, k2) \ 31 : : do {\ 32 : : rte_ether_addr_copy(&(k1->eth_saddr), &(k2->eth_saddr)); \ 33 : : rte_ether_addr_copy(&(k1->eth_daddr), &(k2->eth_daddr)); \ 34 : : k2->recv_ack = k1->recv_ack; \ 35 : : k2->src_port = k1->src_port; \ 36 : : k2->dst_port = k1->dst_port; \ 37 : : } while (0) 38 : : 39 : : struct gro_tcp_item { 40 : : /* 41 : : * The first MBUF segment of the packet. If the value 42 : : * is NULL, it means the item is empty. 43 : : */ 44 : : struct rte_mbuf *firstseg; 45 : : /* The last MBUF segment of the packet */ 46 : : struct rte_mbuf *lastseg; 47 : : /* 48 : : * The time when the first packet is inserted into the table. 49 : : * This value won't be updated, even if the packet is merged 50 : : * with other packets. 51 : : */ 52 : : uint64_t start_time; 53 : : /* 54 : : * next_pkt_idx is used to chain the packets that 55 : : * are in the same flow but can't be merged together 56 : : * (e.g. caused by packet reordering). 57 : : */ 58 : : uint32_t next_pkt_idx; 59 : : /* TCP sequence number of the packet */ 60 : : uint32_t sent_seq; 61 : : union { 62 : : /* IPv4 ID of the packet */ 63 : : uint16_t ip_id; 64 : : /* Unused field for IPv6 */ 65 : : uint16_t unused; 66 : : } l3; 67 : : /* the number of merged packets */ 68 : : uint16_t nb_merged; 69 : : /* Indicate if IPv4 ID can be ignored */ 70 : : uint8_t is_atomic; 71 : : }; 72 : : 73 : : /* 74 : : * Merge two TCP packets without updating checksums. 75 : : * If cmp is larger than 0, append the new packet to the 76 : : * original packet. Otherwise, pre-pend the new packet to 77 : : * the original packet. 78 : : */ 79 : : static inline int 80 : 0 : merge_two_tcp_packets(struct gro_tcp_item *item, 81 : : struct rte_mbuf *pkt, 82 : : int cmp, 83 : : uint32_t sent_seq, 84 : : uint16_t ip_id, 85 : : uint16_t l2_offset) 86 : : { 87 : : struct rte_mbuf *pkt_head, *pkt_tail, *lastseg; 88 : : uint16_t hdr_len, l2_len; 89 : : 90 [ # # ]: 0 : if (cmp > 0) { 91 : 0 : pkt_head = item->firstseg; 92 : : pkt_tail = pkt; 93 : : } else { 94 : : pkt_head = pkt; 95 : 0 : pkt_tail = item->firstseg; 96 : : } 97 : : 98 : : /* check if the IPv4 packet length is greater than the max value */ 99 : 0 : hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len + 100 : 0 : pkt_head->l4_len; 101 [ # # ]: 0 : l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len; 102 [ # # ]: 0 : if (unlikely(pkt_head->pkt_len - l2_len + pkt_tail->pkt_len - 103 : : hdr_len > MAX_IP_PKT_LENGTH)) 104 : : return 0; 105 : : 106 [ # # ]: 0 : if (unlikely(pkt_head->nb_segs >= 20)) 107 : : return 0; 108 : : 109 : : /* remove the packet header for the tail packet */ 110 : : rte_pktmbuf_adj(pkt_tail, hdr_len); 111 : : 112 : : /* chain two packets together */ 113 [ # # ]: 0 : if (cmp > 0) { 114 : 0 : item->lastseg->next = pkt; 115 : 0 : item->lastseg = rte_pktmbuf_lastseg(pkt); 116 : : /* update IP ID to the larger value */ 117 : 0 : item->l3.ip_id = ip_id; 118 : : } else { 119 : : lastseg = rte_pktmbuf_lastseg(pkt); 120 : 0 : lastseg->next = item->firstseg; 121 : 0 : item->firstseg = pkt; 122 : : /* update sent_seq to the smaller value */ 123 : 0 : item->sent_seq = sent_seq; 124 : 0 : item->l3.ip_id = ip_id; 125 : : } 126 : 0 : item->nb_merged++; 127 : : 128 : : /* update MBUF metadata for the merged packet */ 129 : 0 : pkt_head->nb_segs += pkt_tail->nb_segs; 130 : 0 : pkt_head->pkt_len += pkt_tail->pkt_len; 131 : : 132 : 0 : return 1; 133 : : } 134 : : 135 : : /* 136 : : * Check if two TCP packets are neighbors. 137 : : */ 138 : : static inline int 139 : 0 : check_seq_option(struct gro_tcp_item *item, 140 : : struct rte_tcp_hdr *tcph, 141 : : uint32_t sent_seq, 142 : : uint16_t ip_id, 143 : : uint16_t tcp_hl, 144 : : uint16_t tcp_dl, 145 : : uint16_t l2_offset, 146 : : uint8_t is_atomic) 147 : : { 148 : 0 : struct rte_mbuf *pkt_orig = item->firstseg; 149 : : char *iph_orig; 150 : : struct rte_tcp_hdr *tcph_orig; 151 : : uint16_t len, tcp_hl_orig; 152 : : 153 : 0 : iph_orig = (char *)(rte_pktmbuf_mtod(pkt_orig, char *) + 154 : 0 : l2_offset + pkt_orig->l2_len); 155 : 0 : tcph_orig = (struct rte_tcp_hdr *)(iph_orig + pkt_orig->l3_len); 156 : 0 : tcp_hl_orig = pkt_orig->l4_len; 157 : : 158 : : /* Check if TCP option fields equal */ 159 : 0 : len = RTE_MAX(tcp_hl, tcp_hl_orig) - sizeof(struct rte_tcp_hdr); 160 [ # # # # ]: 0 : if ((tcp_hl != tcp_hl_orig) || ((len > 0) && 161 [ # # ]: 0 : (memcmp(tcph + 1, tcph_orig + 1, 162 : : len) != 0))) 163 : : return 0; 164 : : 165 : : /* Don't merge packets whose DF bits are different */ 166 [ # # ]: 0 : if (unlikely(item->is_atomic ^ is_atomic)) 167 : : return 0; 168 : : 169 : : /* check if the two packets are neighbors */ 170 : 0 : len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len - 171 : 0 : pkt_orig->l3_len - tcp_hl_orig; 172 [ # # # # ]: 0 : if ((sent_seq == item->sent_seq + len) && (is_atomic || 173 [ # # ]: 0 : (ip_id == item->l3.ip_id + 1))) 174 : : /* append the new packet */ 175 : : return 1; 176 [ # # # # ]: 0 : else if ((sent_seq + tcp_dl == item->sent_seq) && (is_atomic || 177 [ # # ]: 0 : (ip_id + item->nb_merged == item->l3.ip_id))) 178 : : /* pre-pend the new packet */ 179 : 0 : return -1; 180 : : 181 : : return 0; 182 : : } 183 : : 184 : : static inline int 185 : : is_same_common_tcp_key(struct cmn_tcp_key *k1, struct cmn_tcp_key *k2) 186 : : { 187 [ # # ]: 0 : return (!memcmp(k1, k2, sizeof(struct cmn_tcp_key))); 188 : : } 189 : : 190 : : #endif