1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
11 #define INVALID_ARRAY_INDEX 0xffffffffUL
12 #define GRO_TCP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
15 * The max length of a IPv4 packet, which includes the length of the L3
16 * header, the L4 header and the data payload.
18 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
20 /* Header fields representing a TCP/IPv4 flow */
21 struct tcp4_flow_key {
22 struct ether_addr eth_saddr;
23 struct ether_addr eth_daddr;
32 struct gro_tcp4_flow {
33 struct tcp4_flow_key key;
35 * The index of the first packet in the flow.
36 * INVALID_ARRAY_INDEX indicates an empty flow.
41 struct gro_tcp4_item {
43 * The first MBUF segment of the packet. If the value
44 * is NULL, it means the item is empty.
46 struct rte_mbuf *firstseg;
47 /* The last MBUF segment of the packet */
48 struct rte_mbuf *lastseg;
50 * The time when the first packet is inserted into the table.
51 * This value won't be updated, even if the packet is merged
56 * next_pkt_idx is used to chain the packets that
57 * are in the same flow but can't be merged together
58 * (e.g. caused by packet reordering).
60 uint32_t next_pkt_idx;
61 /* TCP sequence number of the packet */
63 /* IPv4 ID of the packet */
65 /* the number of merged packets */
67 /* Indicate if IPv4 ID can be ignored */
72 * TCP/IPv4 reassembly table structure.
76 struct gro_tcp4_item *items;
78 struct gro_tcp4_flow *flows;
79 /* current item number */
81 /* current flow num */
84 uint32_t max_item_num;
86 uint32_t max_flow_num;
90 * This function creates a TCP/IPv4 reassembly table.
93 * Socket index for allocating the TCP/IPv4 reassemble table
95 * The maximum number of flows in the TCP/IPv4 GRO table
96 * @param max_item_per_flow
97 * The maximum number of packets per flow
100 * - Return the table pointer on success.
101 * - Return NULL on failure.
103 void *gro_tcp4_tbl_create(uint16_t socket_id,
104 uint16_t max_flow_num,
105 uint16_t max_item_per_flow);
108 * This function destroys a TCP/IPv4 reassembly table.
111 * Pointer pointing to the TCP/IPv4 reassembly table.
113 void gro_tcp4_tbl_destroy(void *tbl);
116 * This function merges a TCP/IPv4 packet. It doesn't process the packet,
117 * which has SYN, FIN, RST, PSH, CWR, ECE or URG set, or doesn't have
120 * This function doesn't check if the packet has correct checksums and
121 * doesn't re-calculate checksums for the merged packet. Additionally,
122 * it assumes the packets are complete (i.e., MF==0 && frag_off==0),
123 * when IP fragmentation is possible (i.e., DF==0). It returns the
124 * packet, if the packet has invalid parameters (e.g. SYN bit is set)
125 * or there is no available space in the table.
128 * Packet to reassemble
130 * Pointer pointing to the TCP/IPv4 reassembly table
132 * The time when the packet is inserted into the table
135 * - Return a positive value if the packet is merged.
136 * - Return zero if the packet isn't merged but stored in the table.
137 * - Return a negative value for invalid parameters or no available
138 * space in the table.
140 int32_t gro_tcp4_reassemble(struct rte_mbuf *pkt,
141 struct gro_tcp4_tbl *tbl,
142 uint64_t start_time);
145 * This function flushes timeout packets in a TCP/IPv4 reassembly table,
146 * and without updating checksums.
149 * TCP/IPv4 reassembly table pointer
150 * @param flush_timestamp
151 * Flush packets which are inserted into the table before or at the
154 * Pointer array used to keep flushed packets
156 * The element number in 'out'. It also determines the maximum number of
157 * packets that can be flushed finally.
160 * The number of flushed packets
162 uint16_t gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
163 uint64_t flush_timestamp,
164 struct rte_mbuf **out,
168 * This function returns the number of the packets in a TCP/IPv4
172 * TCP/IPv4 reassembly table pointer
175 * The number of packets in the table
177 uint32_t gro_tcp4_tbl_pkt_count(void *tbl);
180 * Check if two TCP/IPv4 packets belong to the same flow.
183 is_same_tcp4_flow(struct tcp4_flow_key k1, struct tcp4_flow_key k2)
185 return (is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
186 is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
187 (k1.ip_src_addr == k2.ip_src_addr) &&
188 (k1.ip_dst_addr == k2.ip_dst_addr) &&
189 (k1.recv_ack == k2.recv_ack) &&
190 (k1.src_port == k2.src_port) &&
191 (k1.dst_port == k2.dst_port));
195 * Merge two TCP/IPv4 packets without updating checksums.
196 * If cmp is larger than 0, append the new packet to the
197 * original packet. Otherwise, pre-pend the new packet to
198 * the original packet.
201 merge_two_tcp4_packets(struct gro_tcp4_item *item,
202 struct rte_mbuf *pkt,
208 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
209 uint16_t hdr_len, l2_len;
212 pkt_head = item->firstseg;
216 pkt_tail = item->firstseg;
219 /* check if the IPv4 packet length is greater than the max value */
220 hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len +
222 l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
223 if (unlikely(pkt_head->pkt_len - l2_len + pkt_tail->pkt_len -
224 hdr_len > MAX_IPV4_PKT_LENGTH))
227 /* remove the packet header for the tail packet */
228 rte_pktmbuf_adj(pkt_tail, hdr_len);
230 /* chain two packets together */
232 item->lastseg->next = pkt;
233 item->lastseg = rte_pktmbuf_lastseg(pkt);
234 /* update IP ID to the larger value */
237 lastseg = rte_pktmbuf_lastseg(pkt);
238 lastseg->next = item->firstseg;
239 item->firstseg = pkt;
240 /* update sent_seq to the smaller value */
241 item->sent_seq = sent_seq;
246 /* update MBUF metadata for the merged packet */
247 pkt_head->nb_segs += pkt_tail->nb_segs;
248 pkt_head->pkt_len += pkt_tail->pkt_len;
254 * Check if two TCP/IPv4 packets are neighbors.
257 check_seq_option(struct gro_tcp4_item *item,
258 struct tcp_hdr *tcph,
266 struct rte_mbuf *pkt_orig = item->firstseg;
267 struct ipv4_hdr *iph_orig;
268 struct tcp_hdr *tcph_orig;
269 uint16_t len, tcp_hl_orig;
271 iph_orig = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt_orig, char *) +
272 l2_offset + pkt_orig->l2_len);
273 tcph_orig = (struct tcp_hdr *)((char *)iph_orig + pkt_orig->l3_len);
274 tcp_hl_orig = pkt_orig->l4_len;
276 /* Check if TCP option fields equal */
277 len = RTE_MAX(tcp_hl, tcp_hl_orig) - sizeof(struct tcp_hdr);
278 if ((tcp_hl != tcp_hl_orig) || ((len > 0) &&
279 (memcmp(tcph + 1, tcph_orig + 1,
283 /* Don't merge packets whose DF bits are different */
284 if (unlikely(item->is_atomic ^ is_atomic))
287 /* check if the two packets are neighbors */
288 len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
289 pkt_orig->l3_len - tcp_hl_orig;
290 if ((sent_seq == item->sent_seq + len) && (is_atomic ||
291 (ip_id == item->ip_id + 1)))
292 /* append the new packet */
294 else if ((sent_seq + tcp_dl == item->sent_seq) && (is_atomic ||
295 (ip_id + item->nb_merged == item->ip_id)))
296 /* pre-pend the new packet */