f41dcee319e91bba53705d877c6b374b411fc4f3
[dpdk.git] / lib / librte_gro / gro_tcp4.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2017 Intel Corporation. All rights reserved.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Intel Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _GRO_TCP4_H_
34 #define _GRO_TCP4_H_
35
36 #define INVALID_ARRAY_INDEX 0xffffffffUL
37 #define GRO_TCP4_TBL_MAX_ITEM_NUM (1024UL * 1024UL)
38
39 /*
40  * the max L3 length of a TCP/IPv4 packet. The L3 length
41  * is the sum of ipv4 header, tcp header and L4 payload.
42  */
43 #define TCP4_MAX_L3_LENGTH UINT16_MAX
44
45 /* criteria of mergeing packets */
46 struct tcp4_key {
47         struct ether_addr eth_saddr;
48         struct ether_addr eth_daddr;
49         uint32_t ip_src_addr;
50         uint32_t ip_dst_addr;
51
52         uint32_t recv_ack;
53         uint16_t src_port;
54         uint16_t dst_port;
55 };
56
57 struct gro_tcp4_key {
58         struct tcp4_key key;
59         /*
60          * the index of the first packet in the item group.
61          * If the value is INVALID_ARRAY_INDEX, it means
62          * the key is empty.
63          */
64         uint32_t start_index;
65 };
66
67 struct gro_tcp4_item {
68         /*
69          * first segment of the packet. If the value
70          * is NULL, it means the item is empty.
71          */
72         struct rte_mbuf *firstseg;
73         /* last segment of the packet */
74         struct rte_mbuf *lastseg;
75         /*
76          * the time when the first packet is inserted
77          * into the table. If a packet in the table is
78          * merged with an incoming packet, this value
79          * won't be updated. We set this value only
80          * when the first packet is inserted into the
81          * table.
82          */
83         uint64_t start_time;
84         /*
85          * we use next_pkt_idx to chain the packets that
86          * have same key value but can't be merged together.
87          */
88         uint32_t next_pkt_idx;
89         /* the sequence number of the packet */
90         uint32_t sent_seq;
91         /* the IP ID of the packet */
92         uint16_t ip_id;
93         /* the number of merged packets */
94         uint16_t nb_merged;
95 };
96
97 /*
98  * TCP/IPv4 reassembly table structure.
99  */
100 struct gro_tcp4_tbl {
101         /* item array */
102         struct gro_tcp4_item *items;
103         /* key array */
104         struct gro_tcp4_key *keys;
105         /* current item number */
106         uint32_t item_num;
107         /* current key num */
108         uint32_t key_num;
109         /* item array size */
110         uint32_t max_item_num;
111         /* key array size */
112         uint32_t max_key_num;
113 };
114
115 /**
116  * This function creates a TCP/IPv4 reassembly table.
117  *
118  * @param socket_id
119  *  socket index for allocating TCP/IPv4 reassemblt table
120  * @param max_flow_num
121  *  the maximum number of flows in the TCP/IPv4 GRO table
122  * @param max_item_per_flow
123  *  the maximum packet number per flow.
124  *
125  * @return
126  *  if create successfully, return a pointer which points to the
127  *  created TCP/IPv4 GRO table. Otherwise, return NULL.
128  */
129 void *gro_tcp4_tbl_create(uint16_t socket_id,
130                 uint16_t max_flow_num,
131                 uint16_t max_item_per_flow);
132
133 /**
134  * This function destroys a TCP/IPv4 reassembly table.
135  *
136  * @param tbl
137  *  a pointer points to the TCP/IPv4 reassembly table.
138  */
139 void gro_tcp4_tbl_destroy(void *tbl);
140
141 /**
142  * This function searches for a packet in the TCP/IPv4 reassembly table
143  * to merge with the inputted one. To merge two packets is to chain them
144  * together and update packet headers. Packets, whose SYN, FIN, RST, PSH
145  * CWR, ECE or URG bit is set, are returned immediately. Packets which
146  * only have packet headers (i.e. without data) are also returned
147  * immediately. Otherwise, the packet is either merged, or inserted into
148  * the table. Besides, if there is no available space to insert the
149  * packet, this function returns immediately too.
150  *
151  * This function assumes the inputted packet is with correct IPv4 and
152  * TCP checksums. And if two packets are merged, it won't re-calculate
153  * IPv4 and TCP checksums. Besides, if the inputted packet is IP
154  * fragmented, it assumes the packet is complete (with TCP header).
155  *
156  * @param pkt
157  *  packet to reassemble.
158  * @param tbl
159  *  a pointer that points to a TCP/IPv4 reassembly table.
160  * @start_time
161  *  the start time that the packet is inserted into the table
162  *
163  * @return
164  *  if the packet doesn't have data, or SYN, FIN, RST, PSH, CWR, ECE
165  *  or URG bit is set, or there is no available space in the table to
166  *  insert a new item or a new key, return a negative value. If the
167  *  packet is merged successfully, return an positive value. If the
168  *  packet is inserted into the table, return 0.
169  */
170 int32_t gro_tcp4_reassemble(struct rte_mbuf *pkt,
171                 struct gro_tcp4_tbl *tbl,
172                 uint64_t start_time);
173
174 /**
175  * This function flushes timeout packets in a TCP/IPv4 reassembly table
176  * to applications, and without updating checksums for merged packets.
177  * The max number of flushed timeout packets is the element number of
178  * the array which is used to keep flushed packets.
179  *
180  * @param tbl
181  *  a pointer that points to a TCP GRO table.
182  * @param flush_timestamp
183  *  this function flushes packets which are inserted into the table
184  *  before or at the flush_timestamp.
185  * @param out
186  *  pointer array which is used to keep flushed packets.
187  * @param nb_out
188  *  the element number of out. It's also the max number of timeout
189  *  packets that can be flushed finally.
190  *
191  * @return
192  *  the number of packets that are returned.
193  */
194 uint16_t gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
195                 uint64_t flush_timestamp,
196                 struct rte_mbuf **out,
197                 uint16_t nb_out);
198
199 /**
200  * This function returns the number of the packets in a TCP/IPv4
201  * reassembly table.
202  *
203  * @param tbl
204  *  pointer points to a TCP/IPv4 reassembly table.
205  *
206  * @return
207  *  the number of packets in the table
208  */
209 uint32_t gro_tcp4_tbl_pkt_count(void *tbl);
210 #endif