af128c9f7f87cd5527a8fe468882b0ce1cb0447b
[dpdk.git] / lib / librte_gro / gro_tcp4.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef _GRO_TCP4_H_
6 #define _GRO_TCP4_H_
7
8 #define INVALID_ARRAY_INDEX 0xffffffffUL
9 #define GRO_TCP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
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_IPV4_PKT_LENGTH UINT16_MAX
16
17 /* Header fields representing a TCP/IPv4 flow */
18 struct tcp4_flow_key {
19         struct ether_addr eth_saddr;
20         struct ether_addr eth_daddr;
21         uint32_t ip_src_addr;
22         uint32_t ip_dst_addr;
23
24         uint32_t recv_ack;
25         uint16_t src_port;
26         uint16_t dst_port;
27 };
28
29 struct gro_tcp4_flow {
30         struct tcp4_flow_key key;
31         /*
32          * The index of the first packet in the flow.
33          * INVALID_ARRAY_INDEX indicates an empty flow.
34          */
35         uint32_t start_index;
36 };
37
38 struct gro_tcp4_item {
39         /*
40          * The first MBUF segment of the packet. If the value
41          * is NULL, it means the item is empty.
42          */
43         struct rte_mbuf *firstseg;
44         /* The last MBUF segment of the packet */
45         struct rte_mbuf *lastseg;
46         /*
47          * The time when the first packet is inserted into the table.
48          * This value won't be updated, even if the packet is merged
49          * with other packets.
50          */
51         uint64_t start_time;
52         /*
53          * next_pkt_idx is used to chain the packets that
54          * are in the same flow but can't be merged together
55          * (e.g. caused by packet reordering).
56          */
57         uint32_t next_pkt_idx;
58         /* TCP sequence number of the packet */
59         uint32_t sent_seq;
60         /* IPv4 ID of the packet */
61         uint16_t ip_id;
62         /* the number of merged packets */
63         uint16_t nb_merged;
64         /* Indicate if IPv4 ID can be ignored */
65         uint8_t is_atomic;
66 };
67
68 /*
69  * TCP/IPv4 reassembly table structure.
70  */
71 struct gro_tcp4_tbl {
72         /* item array */
73         struct gro_tcp4_item *items;
74         /* flow array */
75         struct gro_tcp4_flow *flows;
76         /* current item number */
77         uint32_t item_num;
78         /* current flow num */
79         uint32_t flow_num;
80         /* item array size */
81         uint32_t max_item_num;
82         /* flow array size */
83         uint32_t max_flow_num;
84 };
85
86 /**
87  * This function creates a TCP/IPv4 reassembly table.
88  *
89  * @param socket_id
90  *  Socket index for allocating the TCP/IPv4 reassemble table
91  * @param max_flow_num
92  *  The maximum number of flows in the TCP/IPv4 GRO table
93  * @param max_item_per_flow
94  *  The maximum number of packets per flow
95  *
96  * @return
97  *  - Return the table pointer on success.
98  *  - Return NULL on failure.
99  */
100 void *gro_tcp4_tbl_create(uint16_t socket_id,
101                 uint16_t max_flow_num,
102                 uint16_t max_item_per_flow);
103
104 /**
105  * This function destroys a TCP/IPv4 reassembly table.
106  *
107  * @param tbl
108  *  Pointer pointing to the TCP/IPv4 reassembly table.
109  */
110 void gro_tcp4_tbl_destroy(void *tbl);
111
112 /**
113  * This function merges a TCP/IPv4 packet. It doesn't process the packet,
114  * which has SYN, FIN, RST, PSH, CWR, ECE or URG set, or doesn't have
115  * payload.
116  *
117  * This function doesn't check if the packet has correct checksums and
118  * doesn't re-calculate checksums for the merged packet. Additionally,
119  * it assumes the packets are complete (i.e., MF==0 && frag_off==0),
120  * when IP fragmentation is possible (i.e., DF==0). It returns the
121  * packet, if the packet has invalid parameters (e.g. SYN bit is set)
122  * or there is no available space in the table.
123  *
124  * @param pkt
125  *  Packet to reassemble
126  * @param tbl
127  *  Pointer pointing to the TCP/IPv4 reassembly table
128  * @start_time
129  *  The time when the packet is inserted into the table
130  *
131  * @return
132  *  - Return a positive value if the packet is merged.
133  *  - Return zero if the packet isn't merged but stored in the table.
134  *  - Return a negative value for invalid parameters or no available
135  *    space in the table.
136  */
137 int32_t gro_tcp4_reassemble(struct rte_mbuf *pkt,
138                 struct gro_tcp4_tbl *tbl,
139                 uint64_t start_time);
140
141 /**
142  * This function flushes timeout packets in a TCP/IPv4 reassembly table,
143  * and without updating checksums.
144  *
145  * @param tbl
146  *  TCP/IPv4 reassembly table pointer
147  * @param flush_timestamp
148  *  Flush packets which are inserted into the table before or at the
149  *  flush_timestamp.
150  * @param out
151  *  Pointer array used to keep flushed packets
152  * @param nb_out
153  *  The element number in 'out'. It also determines the maximum number of
154  *  packets that can be flushed finally.
155  *
156  * @return
157  *  The number of flushed packets
158  */
159 uint16_t gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
160                 uint64_t flush_timestamp,
161                 struct rte_mbuf **out,
162                 uint16_t nb_out);
163
164 /**
165  * This function returns the number of the packets in a TCP/IPv4
166  * reassembly table.
167  *
168  * @param tbl
169  *  TCP/IPv4 reassembly table pointer
170  *
171  * @return
172  *  The number of packets in the table
173  */
174 uint32_t gro_tcp4_tbl_pkt_count(void *tbl);
175 #endif