1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_malloc.h>
7 #include <rte_cycles.h>
8 #include <rte_ethdev.h>
15 gro_tcp4_tbl_create(uint16_t socket_id,
16 uint16_t max_flow_num,
17 uint16_t max_item_per_flow)
19 struct gro_tcp4_tbl *tbl;
21 uint32_t entries_num, i;
23 entries_num = max_flow_num * max_item_per_flow;
24 entries_num = RTE_MIN(entries_num, GRO_TCP4_TBL_MAX_ITEM_NUM);
29 tbl = rte_zmalloc_socket(__func__,
30 sizeof(struct gro_tcp4_tbl),
36 size = sizeof(struct gro_tcp4_item) * entries_num;
37 tbl->items = rte_zmalloc_socket(__func__,
41 if (tbl->items == NULL) {
45 tbl->max_item_num = entries_num;
47 size = sizeof(struct gro_tcp4_flow) * entries_num;
48 tbl->flows = rte_zmalloc_socket(__func__,
52 if (tbl->flows == NULL) {
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;
66 gro_tcp4_tbl_destroy(void *tbl)
68 struct gro_tcp4_tbl *tcp_tbl = tbl;
71 rte_free(tcp_tbl->items);
72 rte_free(tcp_tbl->flows);
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.
84 merge_two_tcp4_packets(struct gro_tcp4_item *item,
90 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
94 pkt_head = item->firstseg;
98 pkt_tail = item->firstseg;
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))
107 /* remove the packet header for the tail packet */
108 rte_pktmbuf_adj(pkt_tail, hdr_len);
110 /* chain two packets together */
112 item->lastseg->next = pkt;
113 item->lastseg = rte_pktmbuf_lastseg(pkt);
114 /* update IP ID to the larger value */
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;
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;
133 * Check if two TCP/IPv4 packets are neighbors.
136 check_seq_option(struct gro_tcp4_item *item,
137 struct tcp_hdr *tcph,
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;
148 iph_orig = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt_orig, char *) +
150 tcph_orig = (struct tcp_hdr *)((char *)iph_orig + pkt_orig->l3_len);
151 tcp_hl_orig = pkt_orig->l4_len;
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,
160 /* check if the two packets are neighbors */
161 len = pkt_orig->pkt_len - pkt_orig->l2_len - pkt_orig->l3_len -
163 if ((sent_seq == item->sent_seq + len) && (ip_id == item->ip_id + 1))
164 /* append the new packet */
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 */
174 static inline uint32_t
175 find_an_empty_item(struct gro_tcp4_tbl *tbl)
178 uint32_t max_item_num = tbl->max_item_num;
180 for (i = 0; i < max_item_num; i++)
181 if (tbl->items[i].firstseg == NULL)
183 return INVALID_ARRAY_INDEX;
186 static inline uint32_t
187 find_an_empty_flow(struct gro_tcp4_tbl *tbl)
190 uint32_t max_flow_num = tbl->max_flow_num;
192 for (i = 0; i < max_flow_num; i++)
193 if (tbl->flows[i].start_index == INVALID_ARRAY_INDEX)
195 return INVALID_ARRAY_INDEX;
198 static inline uint32_t
199 insert_new_item(struct gro_tcp4_tbl *tbl,
200 struct rte_mbuf *pkt,
208 item_idx = find_an_empty_item(tbl);
209 if (item_idx == INVALID_ARRAY_INDEX)
210 return INVALID_ARRAY_INDEX;
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;
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;
231 static inline uint32_t
232 delete_item(struct gro_tcp4_tbl *tbl, uint32_t item_idx,
233 uint32_t prev_item_idx)
235 uint32_t next_idx = tbl->items[item_idx].next_pkt_idx;
237 /* NULL indicates an empty item */
238 tbl->items[item_idx].firstseg = NULL;
240 if (prev_item_idx != INVALID_ARRAY_INDEX)
241 tbl->items[prev_item_idx].next_pkt_idx = next_idx;
246 static inline uint32_t
247 insert_new_flow(struct gro_tcp4_tbl *tbl,
248 struct tcp4_flow_key *src,
251 struct tcp4_flow_key *dst;
254 flow_idx = find_an_empty_flow(tbl);
255 if (unlikely(flow_idx == INVALID_ARRAY_INDEX))
256 return INVALID_ARRAY_INDEX;
258 dst = &(tbl->flows[flow_idx].key);
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;
268 tbl->flows[flow_idx].start_index = item_idx;
275 * Check if two TCP/IPv4 packets belong to the same flow.
278 is_same_tcp4_flow(struct tcp4_flow_key k1, struct tcp4_flow_key k2)
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));
290 * update the packet length for the flushed packet.
293 update_header(struct gro_tcp4_item *item)
295 struct ipv4_hdr *ipv4_hdr;
296 struct rte_mbuf *pkt = item->firstseg;
298 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
300 ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len -
305 gro_tcp4_reassemble(struct rte_mbuf *pkt,
306 struct gro_tcp4_tbl *tbl,
309 struct ether_hdr *eth_hdr;
310 struct ipv4_hdr *ipv4_hdr;
311 struct tcp_hdr *tcp_hdr;
313 uint16_t tcp_dl, ip_id, hdr_len;
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;
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;
327 * Don't process the packet which has FIN, SYN, RST, PSH, URG, ECE
330 if (tcp_hdr->tcp_flags != TCP_ACK_FLAG)
333 * Don't process the packet whose payload length is less than or
336 tcp_dl = pkt->pkt_len - hdr_len;
340 ip_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
341 sent_seq = rte_be_to_cpu_32(tcp_hdr->sent_seq);
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;
351 /* Search for a matched flow. */
352 max_flow_num = tbl->max_flow_num;
353 remaining_flow_num = tbl->flow_num;
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)) {
361 remaining_flow_num--;
366 * Fail to find a matched flow. Insert a new flow and store the
367 * packet into the flow.
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)
374 if (insert_new_flow(tbl, &key, item_idx) ==
375 INVALID_ARRAY_INDEX) {
377 * Fail to insert a new flow, so delete the
380 delete_item(tbl, item_idx, INVALID_ARRAY_INDEX);
387 * Check all packets in the flow and try to find a neighbor for
390 cur_idx = tbl->flows[i].start_index;
393 cmp = check_seq_option(&(tbl->items[cur_idx]), tcp_hdr,
394 sent_seq, ip_id, pkt->l4_len, tcp_dl);
396 if (merge_two_tcp4_packets(&(tbl->items[cur_idx]),
397 pkt, cmp, sent_seq, ip_id))
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.
404 if (insert_new_item(tbl, pkt, start_time, prev_idx,
411 cur_idx = tbl->items[cur_idx].next_pkt_idx;
412 } while (cur_idx != INVALID_ARRAY_INDEX);
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)
423 gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
424 uint64_t flush_timestamp,
425 struct rte_mbuf **out,
430 uint32_t max_flow_num = tbl->max_flow_num;
432 for (i = 0; i < max_flow_num; i++) {
433 if (unlikely(tbl->flow_num == 0))
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]));
443 * Delete the packet and get the next
444 * packet in the flow.
446 j = delete_item(tbl, j, INVALID_ARRAY_INDEX);
447 tbl->flows[i].start_index = j;
448 if (j == INVALID_ARRAY_INDEX)
451 if (unlikely(k == nb_out))
455 * The left packets in this flow won't be
456 * timeout. Go to check other flows.
465 gro_tcp4_tbl_pkt_count(void *tbl)
467 struct gro_tcp4_tbl *gro_tbl = tbl;
470 return gro_tbl->item_num;