1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
10 #define INVALID_ARRAY_INDEX 0xffffffffUL
11 #define GRO_TCP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
14 * The max length of a IPv4 packet, which includes the length of the L3
15 * header, the L4 header and the data payload.
17 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
19 /* The maximum TCP header length */
20 #define MAX_TCP_HLEN 60
21 #define INVALID_TCP_HDRLEN(len) \
22 (((len) < sizeof(struct rte_tcp_hdr)) || ((len) > MAX_TCP_HLEN))
24 /* Header fields representing a TCP/IPv4 flow */
25 struct tcp4_flow_key {
26 struct rte_ether_addr eth_saddr;
27 struct rte_ether_addr eth_daddr;
36 struct gro_tcp4_flow {
37 struct tcp4_flow_key key;
39 * The index of the first packet in the flow.
40 * INVALID_ARRAY_INDEX indicates an empty flow.
45 struct gro_tcp4_item {
47 * The first MBUF segment of the packet. If the value
48 * is NULL, it means the item is empty.
50 struct rte_mbuf *firstseg;
51 /* The last MBUF segment of the packet */
52 struct rte_mbuf *lastseg;
54 * The time when the first packet is inserted into the table.
55 * This value won't be updated, even if the packet is merged
60 * next_pkt_idx is used to chain the packets that
61 * are in the same flow but can't be merged together
62 * (e.g. caused by packet reordering).
64 uint32_t next_pkt_idx;
65 /* TCP sequence number of the packet */
67 /* IPv4 ID of the packet */
69 /* the number of merged packets */
71 /* Indicate if IPv4 ID can be ignored */
76 * TCP/IPv4 reassembly table structure.
80 struct gro_tcp4_item *items;
82 struct gro_tcp4_flow *flows;
83 /* current item number */
85 /* current flow num */
88 uint32_t max_item_num;
90 uint32_t max_flow_num;
94 * This function creates a TCP/IPv4 reassembly table.
97 * Socket index for allocating the TCP/IPv4 reassemble table
99 * The maximum number of flows in the TCP/IPv4 GRO table
100 * @param max_item_per_flow
101 * The maximum number of packets per flow
104 * - Return the table pointer on success.
105 * - Return NULL on failure.
107 void *gro_tcp4_tbl_create(uint16_t socket_id,
108 uint16_t max_flow_num,
109 uint16_t max_item_per_flow);
112 * This function destroys a TCP/IPv4 reassembly table.
115 * Pointer pointing to the TCP/IPv4 reassembly table.
117 void gro_tcp4_tbl_destroy(void *tbl);
120 * This function merges a TCP/IPv4 packet. It doesn't process the packet,
121 * which has SYN, FIN, RST, PSH, CWR, ECE or URG set, or doesn't have
124 * This function doesn't check if the packet has correct checksums and
125 * doesn't re-calculate checksums for the merged packet. Additionally,
126 * it assumes the packets are complete (i.e., MF==0 && frag_off==0),
127 * when IP fragmentation is possible (i.e., DF==0). It returns the
128 * packet, if the packet has invalid parameters (e.g. SYN bit is set)
129 * or there is no available space in the table.
132 * Packet to reassemble
134 * Pointer pointing to the TCP/IPv4 reassembly table
136 * The time when the packet is inserted into the table
139 * - Return a positive value if the packet is merged.
140 * - Return zero if the packet isn't merged but stored in the table.
141 * - Return a negative value for invalid parameters or no available
142 * space in the table.
144 int32_t gro_tcp4_reassemble(struct rte_mbuf *pkt,
145 struct gro_tcp4_tbl *tbl,
146 uint64_t start_time);
149 * This function flushes timeout packets in a TCP/IPv4 reassembly table,
150 * and without updating checksums.
153 * TCP/IPv4 reassembly table pointer
154 * @param flush_timestamp
155 * Flush packets which are inserted into the table before or at the
158 * Pointer array used to keep flushed packets
160 * The element number in 'out'. It also determines the maximum number of
161 * packets that can be flushed finally.
164 * The number of flushed packets
166 uint16_t gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
167 uint64_t flush_timestamp,
168 struct rte_mbuf **out,
172 * This function returns the number of the packets in a TCP/IPv4
176 * TCP/IPv4 reassembly table pointer
179 * The number of packets in the table
181 uint32_t gro_tcp4_tbl_pkt_count(void *tbl);
184 * Check if two TCP/IPv4 packets belong to the same flow.
187 is_same_tcp4_flow(struct tcp4_flow_key k1, struct tcp4_flow_key k2)
189 return (rte_is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
190 rte_is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
191 (k1.ip_src_addr == k2.ip_src_addr) &&
192 (k1.ip_dst_addr == k2.ip_dst_addr) &&
193 (k1.recv_ack == k2.recv_ack) &&
194 (k1.src_port == k2.src_port) &&
195 (k1.dst_port == k2.dst_port));
199 * Merge two TCP/IPv4 packets without updating checksums.
200 * If cmp is larger than 0, append the new packet to the
201 * original packet. Otherwise, pre-pend the new packet to
202 * the original packet.
205 merge_two_tcp4_packets(struct gro_tcp4_item *item,
206 struct rte_mbuf *pkt,
212 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
213 uint16_t hdr_len, l2_len;
216 pkt_head = item->firstseg;
220 pkt_tail = item->firstseg;
223 /* check if the IPv4 packet length is greater than the max value */
224 hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len +
226 l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
227 if (unlikely(pkt_head->pkt_len - l2_len + pkt_tail->pkt_len -
228 hdr_len > MAX_IPV4_PKT_LENGTH))
231 /* remove the packet header for the tail packet */
232 rte_pktmbuf_adj(pkt_tail, hdr_len);
234 /* chain two packets together */
236 item->lastseg->next = pkt;
237 item->lastseg = rte_pktmbuf_lastseg(pkt);
238 /* update IP ID to the larger value */
241 lastseg = rte_pktmbuf_lastseg(pkt);
242 lastseg->next = item->firstseg;
243 item->firstseg = pkt;
244 /* update sent_seq to the smaller value */
245 item->sent_seq = sent_seq;
250 /* update MBUF metadata for the merged packet */
251 pkt_head->nb_segs += pkt_tail->nb_segs;
252 pkt_head->pkt_len += pkt_tail->pkt_len;
258 * Check if two TCP/IPv4 packets are neighbors.
261 check_seq_option(struct gro_tcp4_item *item,
262 struct rte_tcp_hdr *tcph,
270 struct rte_mbuf *pkt_orig = item->firstseg;
271 struct rte_ipv4_hdr *iph_orig;
272 struct rte_tcp_hdr *tcph_orig;
273 uint16_t len, tcp_hl_orig;
275 iph_orig = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkt_orig, char *) +
276 l2_offset + pkt_orig->l2_len);
277 tcph_orig = (struct rte_tcp_hdr *)((char *)iph_orig + pkt_orig->l3_len);
278 tcp_hl_orig = pkt_orig->l4_len;
280 /* Check if TCP option fields equal */
281 len = RTE_MAX(tcp_hl, tcp_hl_orig) - sizeof(struct rte_tcp_hdr);
282 if ((tcp_hl != tcp_hl_orig) || ((len > 0) &&
283 (memcmp(tcph + 1, tcph_orig + 1,
287 /* Don't merge packets whose DF bits are different */
288 if (unlikely(item->is_atomic ^ is_atomic))
291 /* check if the two packets are neighbors */
292 len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
293 pkt_orig->l3_len - tcp_hl_orig;
294 if ((sent_seq == item->sent_seq + len) && (is_atomic ||
295 (ip_id == item->ip_id + 1)))
296 /* append the new packet */
298 else if ((sent_seq + tcp_dl == item->sent_seq) && (is_atomic ||
299 (ip_id + item->nb_merged == item->ip_id)))
300 /* pre-pend the new packet */