gro: support UDP/IPv4
[dpdk.git] / lib / librte_gro / gro_udp4.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Inspur Corporation
3  */
4
5 #ifndef _GRO_UDP4_H_
6 #define _GRO_UDP4_H_
7
8 #include <rte_ip.h>
9 #include <rte_udp.h>
10
11 #define INVALID_ARRAY_INDEX 0xffffffffUL
12 #define GRO_UDP4_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 UDP/IPv4 flow */
21 struct udp4_flow_key {
22         struct rte_ether_addr eth_saddr;
23         struct rte_ether_addr eth_daddr;
24         uint32_t ip_src_addr;
25         uint32_t ip_dst_addr;
26
27         /* IP fragment for UDP does not contain UDP header
28          * except the first one. But IP ID must be same.
29          */
30         uint16_t ip_id;
31 };
32
33 struct gro_udp4_flow {
34         struct udp4_flow_key key;
35         /*
36          * The index of the first packet in the flow.
37          * INVALID_ARRAY_INDEX indicates an empty flow.
38          */
39         uint32_t start_index;
40 };
41
42 struct gro_udp4_item {
43         /*
44          * The first MBUF segment of the packet. If the value
45          * is NULL, it means the item is empty.
46          */
47         struct rte_mbuf *firstseg;
48         /* The last MBUF segment of the packet */
49         struct rte_mbuf *lastseg;
50         /*
51          * The time when the first packet is inserted into the table.
52          * This value won't be updated, even if the packet is merged
53          * with other packets.
54          */
55         uint64_t start_time;
56         /*
57          * next_pkt_idx is used to chain the packets that
58          * are in the same flow but can't be merged together
59          * (e.g. caused by packet reordering).
60          */
61         uint32_t next_pkt_idx;
62         /* offset of IP fragment packet */
63         uint16_t frag_offset;
64         /* is last IP fragment? */
65         uint8_t is_last_frag;
66         /* the number of merged packets */
67         uint16_t nb_merged;
68 };
69
70 /*
71  * UDP/IPv4 reassembly table structure.
72  */
73 struct gro_udp4_tbl {
74         /* item array */
75         struct gro_udp4_item *items;
76         /* flow array */
77         struct gro_udp4_flow *flows;
78         /* current item number */
79         uint32_t item_num;
80         /* current flow num */
81         uint32_t flow_num;
82         /* item array size */
83         uint32_t max_item_num;
84         /* flow array size */
85         uint32_t max_flow_num;
86 };
87
88 /**
89  * This function creates a UDP/IPv4 reassembly table.
90  *
91  * @param socket_id
92  *  Socket index for allocating the UDP/IPv4 reassemble table
93  * @param max_flow_num
94  *  The maximum number of flows in the UDP/IPv4 GRO table
95  * @param max_item_per_flow
96  *  The maximum number of packets per flow
97  *
98  * @return
99  *  - Return the table pointer on success.
100  *  - Return NULL on failure.
101  */
102 void *gro_udp4_tbl_create(uint16_t socket_id,
103                 uint16_t max_flow_num,
104                 uint16_t max_item_per_flow);
105
106 /**
107  * This function destroys a UDP/IPv4 reassembly table.
108  *
109  * @param tbl
110  *  Pointer pointing to the UDP/IPv4 reassembly table.
111  */
112 void gro_udp4_tbl_destroy(void *tbl);
113
114 /**
115  * This function merges a UDP/IPv4 packet.
116  *
117  * This function does not check if the packet has correct checksums and
118  * does not re-calculate checksums for the merged packet. It returns the
119  * packet if it isn't UDP fragment or there is no available space in
120  * the table.
121  *
122  * @param pkt
123  *  Packet to reassemble
124  * @param tbl
125  *  Pointer pointing to the UDP/IPv4 reassembly table
126  * @start_time
127  *  The time when the packet is inserted into the table
128  *
129  * @return
130  *  - Return a positive value if the packet is merged.
131  *  - Return zero if the packet isn't merged but stored in the table.
132  *  - Return a negative value for invalid parameters or no available
133  *    space in the table.
134  */
135 int32_t gro_udp4_reassemble(struct rte_mbuf *pkt,
136                 struct gro_udp4_tbl *tbl,
137                 uint64_t start_time);
138
139 /**
140  * This function flushes timeout packets in a UDP/IPv4 reassembly table,
141  * and without updating checksums.
142  *
143  * @param tbl
144  *  UDP/IPv4 reassembly table pointer
145  * @param flush_timestamp
146  *  Flush packets which are inserted into the table before or at the
147  *  flush_timestamp.
148  * @param out
149  *  Pointer array used to keep flushed packets
150  * @param nb_out
151  *  The element number in 'out'. It also determines the maximum number of
152  *  packets that can be flushed finally.
153  *
154  * @return
155  *  The number of flushed packets
156  */
157 uint16_t gro_udp4_tbl_timeout_flush(struct gro_udp4_tbl *tbl,
158                 uint64_t flush_timestamp,
159                 struct rte_mbuf **out,
160                 uint16_t nb_out);
161
162 /**
163  * This function returns the number of the packets in a UDP/IPv4
164  * reassembly table.
165  *
166  * @param tbl
167  *  UDP/IPv4 reassembly table pointer
168  *
169  * @return
170  *  The number of packets in the table
171  */
172 uint32_t gro_udp4_tbl_pkt_count(void *tbl);
173
174 /*
175  * Check if two UDP/IPv4 packets belong to the same flow.
176  */
177 static inline int
178 is_same_udp4_flow(struct udp4_flow_key k1, struct udp4_flow_key k2)
179 {
180         return (rte_is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
181                         rte_is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
182                         (k1.ip_src_addr == k2.ip_src_addr) &&
183                         (k1.ip_dst_addr == k2.ip_dst_addr) &&
184                         (k1.ip_id == k2.ip_id));
185 }
186
187 /*
188  * Merge two UDP/IPv4 packets without updating checksums.
189  * If cmp is larger than 0, append the new packet to the
190  * original packet. Otherwise, pre-pend the new packet to
191  * the original packet.
192  */
193 static inline int
194 merge_two_udp4_packets(struct gro_udp4_item *item,
195                 struct rte_mbuf *pkt,
196                 int cmp,
197                 uint16_t frag_offset,
198                 uint8_t is_last_frag,
199                 uint16_t l2_offset)
200 {
201         struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
202         uint16_t hdr_len, l2_len;
203         uint32_t ip_len;
204
205         if (cmp > 0) {
206                 pkt_head = item->firstseg;
207                 pkt_tail = pkt;
208         } else {
209                 pkt_head = pkt;
210                 pkt_tail = item->firstseg;
211         }
212
213         /* check if the IPv4 packet length is greater than the max value */
214         hdr_len = l2_offset + pkt_head->l2_len + pkt_head->l3_len;
215         l2_len = l2_offset > 0 ? pkt_head->outer_l2_len : pkt_head->l2_len;
216         ip_len = pkt_head->pkt_len - l2_len
217                  + pkt_tail->pkt_len - hdr_len;
218         if (unlikely(ip_len > MAX_IPV4_PKT_LENGTH))
219                 return 0;
220
221         /* remove the packet header for the tail packet */
222         rte_pktmbuf_adj(pkt_tail, hdr_len);
223
224         /* chain two packets together */
225         if (cmp > 0) {
226                 item->lastseg->next = pkt;
227                 item->lastseg = rte_pktmbuf_lastseg(pkt);
228         } else {
229                 lastseg = rte_pktmbuf_lastseg(pkt);
230                 lastseg->next = item->firstseg;
231                 item->firstseg = pkt;
232                 item->frag_offset = frag_offset;
233         }
234         item->nb_merged++;
235         if (is_last_frag)
236                 item->is_last_frag = is_last_frag;
237
238         /* update MBUF metadata for the merged packet */
239         pkt_head->nb_segs += pkt_tail->nb_segs;
240         pkt_head->pkt_len += pkt_tail->pkt_len;
241
242         return 1;
243 }
244
245 /*
246  * Check if two UDP/IPv4 packets are neighbors.
247  */
248 static inline int
249 udp4_check_neighbor(struct gro_udp4_item *item,
250                 uint16_t frag_offset,
251                 uint16_t ip_dl,
252                 uint16_t l2_offset)
253 {
254         struct rte_mbuf *pkt_orig = item->firstseg;
255         uint16_t len;
256
257         /* check if the two packets are neighbors */
258         len = pkt_orig->pkt_len - l2_offset - pkt_orig->l2_len -
259                 pkt_orig->l3_len;
260         if (frag_offset == item->frag_offset + len)
261                 /* append the new packet */
262                 return 1;
263         else if (frag_offset + ip_dl == item->frag_offset)
264                 /* pre-pend the new packet */
265                 return -1;
266
267         return 0;
268 }
269
270 static inline int
271 is_ipv4_fragment(const struct rte_ipv4_hdr *hdr)
272 {
273         uint16_t flag_offset, ip_flag, ip_ofs;
274
275         flag_offset = rte_be_to_cpu_16(hdr->fragment_offset);
276         ip_ofs = (uint16_t)(flag_offset & RTE_IPV4_HDR_OFFSET_MASK);
277         ip_flag = (uint16_t)(flag_offset & RTE_IPV4_HDR_MF_FLAG);
278
279         return ip_flag != 0 || ip_ofs  != 0;
280 }
281 #endif