New upstream version 18.02
[deb_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 #include <rte_ip.h>
9 #include <rte_tcp.h>
10
11 #define INVALID_ARRAY_INDEX 0xffffffffUL
12 #define GRO_TCP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
13
14 /*
15  * The max length of a IPv4 packet, which includes the length of the L3
16  * header, the L4 header and the data payload.
17  */
18 #define MAX_IPV4_PKT_LENGTH UINT16_MAX
19
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;
24         uint32_t ip_src_addr;
25         uint32_t ip_dst_addr;
26
27         uint32_t recv_ack;
28         uint16_t src_port;
29         uint16_t dst_port;
30 };
31
32 struct gro_tcp4_flow {
33         struct tcp4_flow_key key;
34         /*
35          * The index of the first packet in the flow.
36          * INVALID_ARRAY_INDEX indicates an empty flow.
37          */
38         uint32_t start_index;
39 };
40
41 struct gro_tcp4_item {
42         /*
43          * The first MBUF segment of the packet. If the value
44          * is NULL, it means the item is empty.
45          */
46         struct rte_mbuf *firstseg;
47         /* The last MBUF segment of the packet */
48         struct rte_mbuf *lastseg;
49         /*
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
52          * with other packets.
53          */
54         uint64_t start_time;
55         /*
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).
59          */
60         uint32_t next_pkt_idx;
61         /* TCP sequence number of the packet */
62         uint32_t sent_seq;
63         /* IPv4 ID of the packet */
64         uint16_t ip_id;
65         /* the number of merged packets */
66         uint16_t nb_merged;
67         /* Indicate if IPv4 ID can be ignored */
68         uint8_t is_atomic;
69 };
70
71 /*
72  * TCP/IPv4 reassembly table structure.
73  */
74 struct gro_tcp4_tbl {
75         /* item array */
76         struct gro_tcp4_item *items;
77         /* flow array */
78         struct gro_tcp4_flow *flows;
79         /* current item number */
80         uint32_t item_num;
81         /* current flow num */
82         uint32_t flow_num;
83         /* item array size */
84         uint32_t max_item_num;
85         /* flow array size */
86         uint32_t max_flow_num;
87 };
88
89 /**
90  * This function creates a TCP/IPv4 reassembly table.
91  *
92  * @param socket_id
93  *  Socket index for allocating the TCP/IPv4 reassemble table
94  * @param max_flow_num
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
98  *
99  * @return
100  *  - Return the table pointer on success.
101  *  - Return NULL on failure.
102  */
103 void *gro_tcp4_tbl_create(uint16_t socket_id,
104                 uint16_t max_flow_num,
105                 uint16_t max_item_per_flow);
106
107 /**
108  * This function destroys a TCP/IPv4 reassembly table.
109  *
110  * @param tbl
111  *  Pointer pointing to the TCP/IPv4 reassembly table.
112  */
113 void gro_tcp4_tbl_destroy(void *tbl);
114
115 /**
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
118  * payload.
119  *
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.
126  *
127  * @param pkt
128  *  Packet to reassemble
129  * @param tbl
130  *  Pointer pointing to the TCP/IPv4 reassembly table
131  * @start_time
132  *  The time when the packet is inserted into the table
133  *
134  * @return
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.
139  */
140 int32_t gro_tcp4_reassemble(struct rte_mbuf *pkt,
141                 struct gro_tcp4_tbl *tbl,
142                 uint64_t start_time);
143
144 /**
145  * This function flushes timeout packets in a TCP/IPv4 reassembly table,
146  * and without updating checksums.
147  *
148  * @param tbl
149  *  TCP/IPv4 reassembly table pointer
150  * @param flush_timestamp
151  *  Flush packets which are inserted into the table before or at the
152  *  flush_timestamp.
153  * @param out
154  *  Pointer array used to keep flushed packets
155  * @param nb_out
156  *  The element number in 'out'. It also determines the maximum number of
157  *  packets that can be flushed finally.
158  *
159  * @return
160  *  The number of flushed packets
161  */
162 uint16_t gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
163                 uint64_t flush_timestamp,
164                 struct rte_mbuf **out,
165                 uint16_t nb_out);
166
167 /**
168  * This function returns the number of the packets in a TCP/IPv4
169  * reassembly table.
170  *
171  * @param tbl
172  *  TCP/IPv4 reassembly table pointer
173  *
174  * @return
175  *  The number of packets in the table
176  */
177 uint32_t gro_tcp4_tbl_pkt_count(void *tbl);
178
179 /*
180  * Check if two TCP/IPv4 packets belong to the same flow.
181  */
182 static inline int
183 is_same_tcp4_flow(struct tcp4_flow_key k1, struct tcp4_flow_key k2)
184 {
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));
192 }
193
194 /*
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.
199  */
200 static inline int
201 merge_two_tcp4_packets(struct gro_tcp4_item *item,
202                 struct rte_mbuf *pkt,
203                 int cmp,
204                 uint32_t sent_seq,
205                 uint16_t ip_id,
206                 uint16_t l2_offset)
207 {
208         struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
209         uint16_t hdr_len, l2_len;
210
211         if (cmp > 0) {
212                 pkt_head = item->firstseg;
213                 pkt_tail = pkt;
214         } else {
215                 pkt_head = pkt;
216                 pkt_tail = item->firstseg;
217         }
218
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 +
221                 pkt_head->l4_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))
225                 return 0;
226
227         /* remove the packet header for the tail packet */
228         rte_pktmbuf_adj(pkt_tail, hdr_len);
229
230         /* chain two packets together */
231         if (cmp > 0) {
232                 item->lastseg->next = pkt;
233                 item->lastseg = rte_pktmbuf_lastseg(pkt);
234                 /* update IP ID to the larger value */
235                 item->ip_id = ip_id;
236         } else {
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;
242                 item->ip_id = ip_id;
243         }
244         item->nb_merged++;
245
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;
249
250         return 1;
251 }
252
253 /*
254  * Check if two TCP/IPv4 packets are neighbors.
255  */
256 static inline int
257 check_seq_option(struct gro_tcp4_item *item,
258                 struct tcp_hdr *tcph,
259                 uint32_t sent_seq,
260                 uint16_t ip_id,
261                 uint16_t tcp_hl,
262                 uint16_t tcp_dl,
263                 uint16_t l2_offset,
264                 uint8_t is_atomic)
265 {
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;
270
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;
275
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,
280                                         len) != 0)))
281                 return 0;
282
283         /* Don't merge packets whose DF bits are different */
284         if (unlikely(item->is_atomic ^ is_atomic))
285                 return 0;
286
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 */
293                 return 1;
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 */
297                 return -1;
298
299         return 0;
300 }
301 #endif