15c55e819SKumara Parameshwaran /* SPDX-License-Identifier: BSD-3-Clause
25c55e819SKumara Parameshwaran * Copyright(c) 2023 Intel Corporation
35c55e819SKumara Parameshwaran */
45c55e819SKumara Parameshwaran #ifndef _GRO_TCP_H_
55c55e819SKumara Parameshwaran #define _GRO_TCP_H_
65c55e819SKumara Parameshwaran
75c55e819SKumara Parameshwaran #define INVALID_ARRAY_INDEX 0xffffffffUL
85c55e819SKumara Parameshwaran
95c55e819SKumara Parameshwaran #include <rte_tcp.h>
105c55e819SKumara Parameshwaran
115c55e819SKumara Parameshwaran /*
125c55e819SKumara Parameshwaran * The max length of a IPv4 packet, which includes the length of the L3
135c55e819SKumara Parameshwaran * header, the L4 header and the data payload.
145c55e819SKumara Parameshwaran */
155c55e819SKumara Parameshwaran #define MAX_IP_PKT_LENGTH UINT16_MAX
165c55e819SKumara Parameshwaran
175c55e819SKumara Parameshwaran /* The maximum TCP header length */
185c55e819SKumara Parameshwaran #define MAX_TCP_HLEN 60
195c55e819SKumara Parameshwaran #define INVALID_TCP_HDRLEN(len) \
205c55e819SKumara Parameshwaran (((len) < sizeof(struct rte_tcp_hdr)) || ((len) > MAX_TCP_HLEN))
215c55e819SKumara Parameshwaran
22*547f2943SKumara Parameshwaran #define VALID_GRO_TCP_FLAGS (RTE_TCP_ACK_FLAG | RTE_TCP_PSH_FLAG | RTE_TCP_FIN_FLAG)
23*547f2943SKumara Parameshwaran
245c55e819SKumara Parameshwaran struct cmn_tcp_key {
255c55e819SKumara Parameshwaran struct rte_ether_addr eth_saddr;
265c55e819SKumara Parameshwaran struct rte_ether_addr eth_daddr;
275c55e819SKumara Parameshwaran uint32_t recv_ack;
285c55e819SKumara Parameshwaran uint16_t src_port;
295c55e819SKumara Parameshwaran uint16_t dst_port;
305c55e819SKumara Parameshwaran };
315c55e819SKumara Parameshwaran
325c55e819SKumara Parameshwaran #define ASSIGN_COMMON_TCP_KEY(k1, k2) \
335c55e819SKumara Parameshwaran do {\
345c55e819SKumara Parameshwaran rte_ether_addr_copy(&(k1->eth_saddr), &(k2->eth_saddr)); \
355c55e819SKumara Parameshwaran rte_ether_addr_copy(&(k1->eth_daddr), &(k2->eth_daddr)); \
365c55e819SKumara Parameshwaran k2->recv_ack = k1->recv_ack; \
375c55e819SKumara Parameshwaran k2->src_port = k1->src_port; \
385c55e819SKumara Parameshwaran k2->dst_port = k1->dst_port; \
395c55e819SKumara Parameshwaran } while (0)
405c55e819SKumara Parameshwaran
415c55e819SKumara Parameshwaran struct gro_tcp_item {
425c55e819SKumara Parameshwaran /*
435c55e819SKumara Parameshwaran * The first MBUF segment of the packet. If the value
445c55e819SKumara Parameshwaran * is NULL, it means the item is empty.
455c55e819SKumara Parameshwaran */
465c55e819SKumara Parameshwaran struct rte_mbuf *firstseg;
475c55e819SKumara Parameshwaran /* The last MBUF segment of the packet */
485c55e819SKumara Parameshwaran struct rte_mbuf *lastseg;
495c55e819SKumara Parameshwaran /*
505c55e819SKumara Parameshwaran * The time when the first packet is inserted into the table.
515c55e819SKumara Parameshwaran * This value won't be updated, even if the packet is merged
525c55e819SKumara Parameshwaran * with other packets.
535c55e819SKumara Parameshwaran */
545c55e819SKumara Parameshwaran uint64_t start_time;
555c55e819SKumara Parameshwaran /*
565c55e819SKumara Parameshwaran * next_pkt_idx is used to chain the packets that
575c55e819SKumara Parameshwaran * are in the same flow but can't be merged together
585c55e819SKumara Parameshwaran * (e.g. caused by packet reordering).
595c55e819SKumara Parameshwaran */
605c55e819SKumara Parameshwaran uint32_t next_pkt_idx;
615c55e819SKumara Parameshwaran /* TCP sequence number of the packet */
625c55e819SKumara Parameshwaran uint32_t sent_seq;
635c55e819SKumara Parameshwaran union {
645c55e819SKumara Parameshwaran /* IPv4 ID of the packet */
655c55e819SKumara Parameshwaran uint16_t ip_id;
665c55e819SKumara Parameshwaran /* Unused field for IPv6 */
675c55e819SKumara Parameshwaran uint16_t unused;
685c55e819SKumara Parameshwaran } l3;
695c55e819SKumara Parameshwaran /* the number of merged packets */
705c55e819SKumara Parameshwaran uint16_t nb_merged;
715c55e819SKumara Parameshwaran /* Indicate if IPv4 ID can be ignored */
725c55e819SKumara Parameshwaran uint8_t is_atomic;
735c55e819SKumara Parameshwaran };
745c55e819SKumara Parameshwaran
755c55e819SKumara Parameshwaran /*
765c55e819SKumara Parameshwaran * Merge two TCP packets without updating checksums.
775c55e819SKumara Parameshwaran * If cmp is larger than 0, append the new packet to the
785c55e819SKumara Parameshwaran * original packet. Otherwise, pre-pend the new packet to
795c55e819SKumara Parameshwaran * the original packet.
805c55e819SKumara Parameshwaran */
815c55e819SKumara Parameshwaran static inline int
merge_two_tcp_packets(struct gro_tcp_item * item,struct rte_mbuf * pkt,int cmp,uint32_t sent_seq,uint8_t tcp_flags,uint16_t ip_id,uint16_t l2_offset)825c55e819SKumara Parameshwaran merge_two_tcp_packets(struct gro_tcp_item *item,
835c55e819SKumara Parameshwaran struct rte_mbuf *pkt,
845c55e819SKumara Parameshwaran int cmp,
855c55e819SKumara Parameshwaran uint32_t sent_seq,
86*547f2943SKumara Parameshwaran uint8_t tcp_flags,
875c55e819SKumara Parameshwaran uint16_t ip_id,
885c55e819SKumara Parameshwaran uint16_t l2_offset)
895c55e819SKumara Parameshwaran {
905c55e819SKumara Parameshwaran struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
915c55e819SKumara Parameshwaran uint16_t hdr_len, l2_len;
92*547f2943SKumara Parameshwaran struct rte_tcp_hdr *tcp_hdr;
935c55e819SKumara Parameshwaran
945c55e819SKumara Parameshwaran if (cmp > 0) {
955c55e819SKumara Parameshwaran pkt_head = item->firstseg;
965c55e819SKumara Parameshwaran pkt_tail = pkt;
975c55e819SKumara Parameshwaran } else {
985c55e819SKumara Parameshwaran pkt_head = pkt;
995c55e819SKumara Parameshwaran pkt_tail = item->firstseg;
1005c55e819SKumara Parameshwaran }
1015c55e819SKumara Parameshwaran
1025c55e819SKumara Parameshwaran /* check if the IPv4 packet length is greater than the max value */
1035c55e819SKumara Parameshwaran hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len +
1045c55e819SKumara Parameshwaran pkt_head->l4_len;
1055c55e819SKumara Parameshwaran l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
1065c55e819SKumara Parameshwaran if (unlikely(pkt_head->pkt_len - l2_len + pkt_tail->pkt_len -
1075c55e819SKumara Parameshwaran hdr_len > MAX_IP_PKT_LENGTH))
1085c55e819SKumara Parameshwaran return 0;
1095c55e819SKumara Parameshwaran
1105c55e819SKumara Parameshwaran if (unlikely(pkt_head->nb_segs >= 20))
1115c55e819SKumara Parameshwaran return 0;
1125c55e819SKumara Parameshwaran
1135c55e819SKumara Parameshwaran /* remove the packet header for the tail packet */
1145c55e819SKumara Parameshwaran rte_pktmbuf_adj(pkt_tail, hdr_len);
1155c55e819SKumara Parameshwaran
1165c55e819SKumara Parameshwaran /* chain two packets together */
1175c55e819SKumara Parameshwaran if (cmp > 0) {
1185c55e819SKumara Parameshwaran item->lastseg->next = pkt;
1195c55e819SKumara Parameshwaran item->lastseg = rte_pktmbuf_lastseg(pkt);
1205c55e819SKumara Parameshwaran /* update IP ID to the larger value */
1215c55e819SKumara Parameshwaran item->l3.ip_id = ip_id;
1225c55e819SKumara Parameshwaran } else {
1235c55e819SKumara Parameshwaran lastseg = rte_pktmbuf_lastseg(pkt);
1245c55e819SKumara Parameshwaran lastseg->next = item->firstseg;
1255c55e819SKumara Parameshwaran item->firstseg = pkt;
1265c55e819SKumara Parameshwaran /* update sent_seq to the smaller value */
1275c55e819SKumara Parameshwaran item->sent_seq = sent_seq;
1285c55e819SKumara Parameshwaran item->l3.ip_id = ip_id;
1295c55e819SKumara Parameshwaran }
1305c55e819SKumara Parameshwaran item->nb_merged++;
1315c55e819SKumara Parameshwaran
1325c55e819SKumara Parameshwaran /* update MBUF metadata for the merged packet */
1335c55e819SKumara Parameshwaran pkt_head->nb_segs += pkt_tail->nb_segs;
1345c55e819SKumara Parameshwaran pkt_head->pkt_len += pkt_tail->pkt_len;
135*547f2943SKumara Parameshwaran if (tcp_flags != RTE_TCP_ACK_FLAG) {
136*547f2943SKumara Parameshwaran tcp_hdr = rte_pktmbuf_mtod_offset(pkt, struct rte_tcp_hdr *,
137*547f2943SKumara Parameshwaran l2_offset + pkt_head->l2_len + pkt_head->l3_len);
138*547f2943SKumara Parameshwaran tcp_hdr->tcp_flags |= tcp_flags;
139*547f2943SKumara Parameshwaran }
1405c55e819SKumara Parameshwaran
1415c55e819SKumara Parameshwaran return 1;
1425c55e819SKumara Parameshwaran }
1435c55e819SKumara Parameshwaran
1445c55e819SKumara Parameshwaran /*
1455c55e819SKumara Parameshwaran * Check if two TCP packets are neighbors.
1465c55e819SKumara Parameshwaran */
1475c55e819SKumara Parameshwaran static inline int
check_seq_option(struct gro_tcp_item * item,struct rte_tcp_hdr * tcph,uint32_t sent_seq,uint16_t ip_id,uint16_t tcp_hl,uint16_t tcp_dl,uint16_t l2_offset,uint8_t is_atomic)1485c55e819SKumara Parameshwaran check_seq_option(struct gro_tcp_item *item,
1495c55e819SKumara Parameshwaran struct rte_tcp_hdr *tcph,
1505c55e819SKumara Parameshwaran uint32_t sent_seq,
1515c55e819SKumara Parameshwaran uint16_t ip_id,
1525c55e819SKumara Parameshwaran uint16_t tcp_hl,
1535c55e819SKumara Parameshwaran uint16_t tcp_dl,
1545c55e819SKumara Parameshwaran uint16_t l2_offset,
1555c55e819SKumara Parameshwaran uint8_t is_atomic)
1565c55e819SKumara Parameshwaran {
1575c55e819SKumara Parameshwaran struct rte_mbuf *pkt_orig = item->firstseg;
1585c55e819SKumara Parameshwaran char *iph_orig;
1595c55e819SKumara Parameshwaran struct rte_tcp_hdr *tcph_orig;
1605c55e819SKumara Parameshwaran uint16_t len, tcp_hl_orig;
1615c55e819SKumara Parameshwaran
16263a98ffeSStephen Hemminger iph_orig = rte_pktmbuf_mtod_offset(pkt_orig, char *,
1635c55e819SKumara Parameshwaran l2_offset + pkt_orig->l2_len);
1645c55e819SKumara Parameshwaran tcph_orig = (struct rte_tcp_hdr *)(iph_orig + pkt_orig->l3_len);
1655c55e819SKumara Parameshwaran tcp_hl_orig = pkt_orig->l4_len;
1665c55e819SKumara Parameshwaran
1675c55e819SKumara Parameshwaran /* Check if TCP option fields equal */
1685c55e819SKumara Parameshwaran len = RTE_MAX(tcp_hl, tcp_hl_orig) - sizeof(struct rte_tcp_hdr);
1695c55e819SKumara Parameshwaran if ((tcp_hl != tcp_hl_orig) || ((len > 0) &&
1705c55e819SKumara Parameshwaran (memcmp(tcph + 1, tcph_orig + 1,
1715c55e819SKumara Parameshwaran len) != 0)))
1725c55e819SKumara Parameshwaran return 0;
1735c55e819SKumara Parameshwaran
1745c55e819SKumara Parameshwaran /* Don't merge packets whose DF bits are different */
1755c55e819SKumara Parameshwaran if (unlikely(item->is_atomic ^ is_atomic))
1765c55e819SKumara Parameshwaran return 0;
1775c55e819SKumara Parameshwaran
1785c55e819SKumara Parameshwaran /* check if the two packets are neighbors */
1795c55e819SKumara Parameshwaran len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
1805c55e819SKumara Parameshwaran pkt_orig->l3_len - tcp_hl_orig;
1815c55e819SKumara Parameshwaran if ((sent_seq == item->sent_seq + len) && (is_atomic ||
1825c55e819SKumara Parameshwaran (ip_id == item->l3.ip_id + 1)))
1835c55e819SKumara Parameshwaran /* append the new packet */
1845c55e819SKumara Parameshwaran return 1;
1855c55e819SKumara Parameshwaran else if ((sent_seq + tcp_dl == item->sent_seq) && (is_atomic ||
1865c55e819SKumara Parameshwaran (ip_id + item->nb_merged == item->l3.ip_id)))
1875c55e819SKumara Parameshwaran /* pre-pend the new packet */
1885c55e819SKumara Parameshwaran return -1;
1895c55e819SKumara Parameshwaran
1905c55e819SKumara Parameshwaran return 0;
1915c55e819SKumara Parameshwaran }
1925c55e819SKumara Parameshwaran
1935c55e819SKumara Parameshwaran static inline int
is_same_common_tcp_key(struct cmn_tcp_key * k1,struct cmn_tcp_key * k2)1945c55e819SKumara Parameshwaran is_same_common_tcp_key(struct cmn_tcp_key *k1, struct cmn_tcp_key *k2)
1955c55e819SKumara Parameshwaran {
1965c55e819SKumara Parameshwaran return (!memcmp(k1, k2, sizeof(struct cmn_tcp_key)));
1975c55e819SKumara Parameshwaran }
1985c55e819SKumara Parameshwaran
1995c55e819SKumara Parameshwaran #endif
200