gro: cleanup
[dpdk.git] / lib / librte_gro / gro_tcp4.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #include <rte_malloc.h>
6 #include <rte_mbuf.h>
7 #include <rte_cycles.h>
8 #include <rte_ethdev.h>
9 #include <rte_ip.h>
10 #include <rte_tcp.h>
11
12 #include "gro_tcp4.h"
13
14 void *
15 gro_tcp4_tbl_create(uint16_t socket_id,
16                 uint16_t max_flow_num,
17                 uint16_t max_item_per_flow)
18 {
19         struct gro_tcp4_tbl *tbl;
20         size_t size;
21         uint32_t entries_num, i;
22
23         entries_num = max_flow_num * max_item_per_flow;
24         entries_num = RTE_MIN(entries_num, GRO_TCP4_TBL_MAX_ITEM_NUM);
25
26         if (entries_num == 0)
27                 return NULL;
28
29         tbl = rte_zmalloc_socket(__func__,
30                         sizeof(struct gro_tcp4_tbl),
31                         RTE_CACHE_LINE_SIZE,
32                         socket_id);
33         if (tbl == NULL)
34                 return NULL;
35
36         size = sizeof(struct gro_tcp4_item) * entries_num;
37         tbl->items = rte_zmalloc_socket(__func__,
38                         size,
39                         RTE_CACHE_LINE_SIZE,
40                         socket_id);
41         if (tbl->items == NULL) {
42                 rte_free(tbl);
43                 return NULL;
44         }
45         tbl->max_item_num = entries_num;
46
47         size = sizeof(struct gro_tcp4_flow) * entries_num;
48         tbl->flows = rte_zmalloc_socket(__func__,
49                         size,
50                         RTE_CACHE_LINE_SIZE,
51                         socket_id);
52         if (tbl->flows == NULL) {
53                 rte_free(tbl->items);
54                 rte_free(tbl);
55                 return NULL;
56         }
57         /* INVALID_ARRAY_INDEX indicates an empty flow */
58         for (i = 0; i < entries_num; i++)
59                 tbl->flows[i].start_index = INVALID_ARRAY_INDEX;
60         tbl->max_flow_num = entries_num;
61
62         return tbl;
63 }
64
65 void
66 gro_tcp4_tbl_destroy(void *tbl)
67 {
68         struct gro_tcp4_tbl *tcp_tbl = tbl;
69
70         if (tcp_tbl) {
71                 rte_free(tcp_tbl->items);
72                 rte_free(tcp_tbl->flows);
73         }
74         rte_free(tcp_tbl);
75 }
76
77 /*
78  * merge two TCP/IPv4 packets without updating checksums.
79  * If cmp is larger than 0, append the new packet to the
80  * original packet. Otherwise, pre-pend the new packet to
81  * the original packet.
82  */
83 static inline int
84 merge_two_tcp4_packets(struct gro_tcp4_item *item,
85                 struct rte_mbuf *pkt,
86                 int cmp,
87                 uint32_t sent_seq,
88                 uint16_t ip_id)
89 {
90         struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
91         uint16_t hdr_len;
92
93         if (cmp > 0) {
94                 pkt_head = item->firstseg;
95                 pkt_tail = pkt;
96         } else {
97                 pkt_head = pkt;
98                 pkt_tail = item->firstseg;
99         }
100
101         /* check if the IPv4 packet length is greater than the max value */
102         hdr_len = pkt_head->l2_len + pkt_head->l3_len + pkt_head->l4_len;
103         if (unlikely(pkt_head->pkt_len - pkt_head->l2_len + pkt_tail->pkt_len -
104                                 hdr_len > MAX_IPV4_PKT_LENGTH))
105                 return 0;
106
107         /* remove the packet header for the tail packet */
108         rte_pktmbuf_adj(pkt_tail, hdr_len);
109
110         /* chain two packets together */
111         if (cmp > 0) {
112                 item->lastseg->next = pkt;
113                 item->lastseg = rte_pktmbuf_lastseg(pkt);
114                 /* update IP ID to the larger value */
115                 item->ip_id = ip_id;
116         } else {
117                 lastseg = rte_pktmbuf_lastseg(pkt);
118                 lastseg->next = item->firstseg;
119                 item->firstseg = pkt;
120                 /* update sent_seq to the smaller value */
121                 item->sent_seq = sent_seq;
122         }
123         item->nb_merged++;
124
125         /* update mbuf metadata for the merged packet */
126         pkt_head->nb_segs += pkt_tail->nb_segs;
127         pkt_head->pkt_len += pkt_tail->pkt_len;
128
129         return 1;
130 }
131
132 /*
133  * Check if two TCP/IPv4 packets are neighbors.
134  */
135 static inline int
136 check_seq_option(struct gro_tcp4_item *item,
137                 struct tcp_hdr *tcph,
138                 uint32_t sent_seq,
139                 uint16_t ip_id,
140                 uint16_t tcp_hl,
141                 uint16_t tcp_dl)
142 {
143         struct rte_mbuf *pkt_orig = item->firstseg;
144         struct ipv4_hdr *iph_orig;
145         struct tcp_hdr *tcph_orig;
146         uint16_t len, tcp_hl_orig;
147
148         iph_orig = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt_orig, char *) +
149                         pkt_orig->l2_len);
150         tcph_orig = (struct tcp_hdr *)((char *)iph_orig + pkt_orig->l3_len);
151         tcp_hl_orig = pkt_orig->l4_len;
152
153         /* Check if TCP option fields equal */
154         len = RTE_MAX(tcp_hl, tcp_hl_orig) - sizeof(struct tcp_hdr);
155         if ((tcp_hl != tcp_hl_orig) ||
156                         ((len > 0) && (memcmp(tcph + 1, tcph_orig + 1,
157                                         len) != 0)))
158                 return 0;
159
160         /* check if the two packets are neighbors */
161         len = pkt_orig->pkt_len - pkt_orig->l2_len - pkt_orig->l3_len -
162                 tcp_hl_orig;
163         if ((sent_seq == item->sent_seq + len) && (ip_id == item->ip_id + 1))
164                 /* append the new packet */
165                 return 1;
166         else if ((sent_seq + tcp_dl == item->sent_seq) &&
167                         (ip_id + item->nb_merged == item->ip_id))
168                 /* pre-pend the new packet */
169                 return -1;
170
171         return 0;
172 }
173
174 static inline uint32_t
175 find_an_empty_item(struct gro_tcp4_tbl *tbl)
176 {
177         uint32_t i;
178         uint32_t max_item_num = tbl->max_item_num;
179
180         for (i = 0; i < max_item_num; i++)
181                 if (tbl->items[i].firstseg == NULL)
182                         return i;
183         return INVALID_ARRAY_INDEX;
184 }
185
186 static inline uint32_t
187 find_an_empty_flow(struct gro_tcp4_tbl *tbl)
188 {
189         uint32_t i;
190         uint32_t max_flow_num = tbl->max_flow_num;
191
192         for (i = 0; i < max_flow_num; i++)
193                 if (tbl->flows[i].start_index == INVALID_ARRAY_INDEX)
194                         return i;
195         return INVALID_ARRAY_INDEX;
196 }
197
198 static inline uint32_t
199 insert_new_item(struct gro_tcp4_tbl *tbl,
200                 struct rte_mbuf *pkt,
201                 uint64_t start_time,
202                 uint32_t prev_idx,
203                 uint32_t sent_seq,
204                 uint16_t ip_id)
205 {
206         uint32_t item_idx;
207
208         item_idx = find_an_empty_item(tbl);
209         if (item_idx == INVALID_ARRAY_INDEX)
210                 return INVALID_ARRAY_INDEX;
211
212         tbl->items[item_idx].firstseg = pkt;
213         tbl->items[item_idx].lastseg = rte_pktmbuf_lastseg(pkt);
214         tbl->items[item_idx].start_time = start_time;
215         tbl->items[item_idx].next_pkt_idx = INVALID_ARRAY_INDEX;
216         tbl->items[item_idx].sent_seq = sent_seq;
217         tbl->items[item_idx].ip_id = ip_id;
218         tbl->items[item_idx].nb_merged = 1;
219         tbl->item_num++;
220
221         /* if the previous packet exists, chain them together. */
222         if (prev_idx != INVALID_ARRAY_INDEX) {
223                 tbl->items[item_idx].next_pkt_idx =
224                         tbl->items[prev_idx].next_pkt_idx;
225                 tbl->items[prev_idx].next_pkt_idx = item_idx;
226         }
227
228         return item_idx;
229 }
230
231 static inline uint32_t
232 delete_item(struct gro_tcp4_tbl *tbl, uint32_t item_idx,
233                 uint32_t prev_item_idx)
234 {
235         uint32_t next_idx = tbl->items[item_idx].next_pkt_idx;
236
237         /* NULL indicates an empty item */
238         tbl->items[item_idx].firstseg = NULL;
239         tbl->item_num--;
240         if (prev_item_idx != INVALID_ARRAY_INDEX)
241                 tbl->items[prev_item_idx].next_pkt_idx = next_idx;
242
243         return next_idx;
244 }
245
246 static inline uint32_t
247 insert_new_flow(struct gro_tcp4_tbl *tbl,
248                 struct tcp4_flow_key *src,
249                 uint32_t item_idx)
250 {
251         struct tcp4_flow_key *dst;
252         uint32_t flow_idx;
253
254         flow_idx = find_an_empty_flow(tbl);
255         if (unlikely(flow_idx == INVALID_ARRAY_INDEX))
256                 return INVALID_ARRAY_INDEX;
257
258         dst = &(tbl->flows[flow_idx].key);
259
260         ether_addr_copy(&(src->eth_saddr), &(dst->eth_saddr));
261         ether_addr_copy(&(src->eth_daddr), &(dst->eth_daddr));
262         dst->ip_src_addr = src->ip_src_addr;
263         dst->ip_dst_addr = src->ip_dst_addr;
264         dst->recv_ack = src->recv_ack;
265         dst->src_port = src->src_port;
266         dst->dst_port = src->dst_port;
267
268         tbl->flows[flow_idx].start_index = item_idx;
269         tbl->flow_num++;
270
271         return flow_idx;
272 }
273
274 /*
275  * Check if two TCP/IPv4 packets belong to the same flow.
276  */
277 static inline int
278 is_same_tcp4_flow(struct tcp4_flow_key k1, struct tcp4_flow_key k2)
279 {
280         return (is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) &&
281                         is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) &&
282                         (k1.ip_src_addr == k2.ip_src_addr) &&
283                         (k1.ip_dst_addr == k2.ip_dst_addr) &&
284                         (k1.recv_ack == k2.recv_ack) &&
285                         (k1.src_port == k2.src_port) &&
286                         (k1.dst_port == k2.dst_port));
287 }
288
289 /*
290  * update the packet length for the flushed packet.
291  */
292 static inline void
293 update_header(struct gro_tcp4_item *item)
294 {
295         struct ipv4_hdr *ipv4_hdr;
296         struct rte_mbuf *pkt = item->firstseg;
297
298         ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
299                         pkt->l2_len);
300         ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len -
301                         pkt->l2_len);
302 }
303
304 int32_t
305 gro_tcp4_reassemble(struct rte_mbuf *pkt,
306                 struct gro_tcp4_tbl *tbl,
307                 uint64_t start_time)
308 {
309         struct ether_hdr *eth_hdr;
310         struct ipv4_hdr *ipv4_hdr;
311         struct tcp_hdr *tcp_hdr;
312         uint32_t sent_seq;
313         uint16_t tcp_dl, ip_id, hdr_len;
314
315         struct tcp4_flow_key key;
316         uint32_t cur_idx, prev_idx, item_idx;
317         uint32_t i, max_flow_num, remaining_flow_num;
318         int cmp;
319         uint8_t find;
320
321         eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
322         ipv4_hdr = (struct ipv4_hdr *)((char *)eth_hdr + pkt->l2_len);
323         tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + pkt->l3_len);
324         hdr_len = pkt->l2_len + pkt->l3_len + pkt->l4_len;
325
326         /*
327          * Don't process the packet which has FIN, SYN, RST, PSH, URG, ECE
328          * or CWR set.
329          */
330         if (tcp_hdr->tcp_flags != TCP_ACK_FLAG)
331                 return -1;
332         /*
333          * Don't process the packet whose payload length is less than or
334          * equal to 0.
335          */
336         tcp_dl = pkt->pkt_len - hdr_len;
337         if (tcp_dl <= 0)
338                 return -1;
339
340         ip_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
341         sent_seq = rte_be_to_cpu_32(tcp_hdr->sent_seq);
342
343         ether_addr_copy(&(eth_hdr->s_addr), &(key.eth_saddr));
344         ether_addr_copy(&(eth_hdr->d_addr), &(key.eth_daddr));
345         key.ip_src_addr = ipv4_hdr->src_addr;
346         key.ip_dst_addr = ipv4_hdr->dst_addr;
347         key.src_port = tcp_hdr->src_port;
348         key.dst_port = tcp_hdr->dst_port;
349         key.recv_ack = tcp_hdr->recv_ack;
350
351         /* Search for a matched flow. */
352         max_flow_num = tbl->max_flow_num;
353         remaining_flow_num = tbl->flow_num;
354         find = 0;
355         for (i = 0; i < max_flow_num && remaining_flow_num; i++) {
356                 if (tbl->flows[i].start_index != INVALID_ARRAY_INDEX) {
357                         if (is_same_tcp4_flow(tbl->flows[i].key, key)) {
358                                 find = 1;
359                                 break;
360                         }
361                         remaining_flow_num--;
362                 }
363         }
364
365         /*
366          * Fail to find a matched flow. Insert a new flow and store the
367          * packet into the flow.
368          */
369         if (find == 0) {
370                 item_idx = insert_new_item(tbl, pkt, start_time,
371                                 INVALID_ARRAY_INDEX, sent_seq, ip_id);
372                 if (item_idx == INVALID_ARRAY_INDEX)
373                         return -1;
374                 if (insert_new_flow(tbl, &key, item_idx) ==
375                                 INVALID_ARRAY_INDEX) {
376                         /*
377                          * Fail to insert a new flow, so delete the
378                          * stored packet.
379                          */
380                         delete_item(tbl, item_idx, INVALID_ARRAY_INDEX);
381                         return -1;
382                 }
383                 return 0;
384         }
385
386         /*
387          * Check all packets in the flow and try to find a neighbor for
388          * the input packet.
389          */
390         cur_idx = tbl->flows[i].start_index;
391         prev_idx = cur_idx;
392         do {
393                 cmp = check_seq_option(&(tbl->items[cur_idx]), tcp_hdr,
394                                 sent_seq, ip_id, pkt->l4_len, tcp_dl);
395                 if (cmp) {
396                         if (merge_two_tcp4_packets(&(tbl->items[cur_idx]),
397                                                 pkt, cmp, sent_seq, ip_id))
398                                 return 1;
399                         /*
400                          * Fail to merge the two packets, as the packet
401                          * length is greater than the max value. Store
402                          * the packet into the flow.
403                          */
404                         if (insert_new_item(tbl, pkt, start_time, prev_idx,
405                                                 sent_seq, ip_id) ==
406                                         INVALID_ARRAY_INDEX)
407                                 return -1;
408                         return 0;
409                 }
410                 prev_idx = cur_idx;
411                 cur_idx = tbl->items[cur_idx].next_pkt_idx;
412         } while (cur_idx != INVALID_ARRAY_INDEX);
413
414         /* Fail to find a neighbor, so store the packet into the flow. */
415         if (insert_new_item(tbl, pkt, start_time, prev_idx, sent_seq,
416                                 ip_id) == INVALID_ARRAY_INDEX)
417                 return -1;
418
419         return 0;
420 }
421
422 uint16_t
423 gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
424                 uint64_t flush_timestamp,
425                 struct rte_mbuf **out,
426                 uint16_t nb_out)
427 {
428         uint16_t k = 0;
429         uint32_t i, j;
430         uint32_t max_flow_num = tbl->max_flow_num;
431
432         for (i = 0; i < max_flow_num; i++) {
433                 if (unlikely(tbl->flow_num == 0))
434                         return k;
435
436                 j = tbl->flows[i].start_index;
437                 while (j != INVALID_ARRAY_INDEX) {
438                         if (tbl->items[j].start_time <= flush_timestamp) {
439                                 out[k++] = tbl->items[j].firstseg;
440                                 if (tbl->items[j].nb_merged > 1)
441                                         update_header(&(tbl->items[j]));
442                                 /*
443                                  * Delete the packet and get the next
444                                  * packet in the flow.
445                                  */
446                                 j = delete_item(tbl, j, INVALID_ARRAY_INDEX);
447                                 tbl->flows[i].start_index = j;
448                                 if (j == INVALID_ARRAY_INDEX)
449                                         tbl->flow_num--;
450
451                                 if (unlikely(k == nb_out))
452                                         return k;
453                         } else
454                                 /*
455                                  * The left packets in this flow won't be
456                                  * timeout. Go to check other flows.
457                                  */
458                                 break;
459                 }
460         }
461         return k;
462 }
463
464 uint32_t
465 gro_tcp4_tbl_pkt_count(void *tbl)
466 {
467         struct gro_tcp4_tbl *gro_tbl = tbl;
468
469         if (gro_tbl)
470                 return gro_tbl->item_num;
471
472         return 0;
473 }