1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_malloc.h>
7 #include <rte_ethdev.h>
12 gro_tcp4_tbl_create(uint16_t socket_id,
13 uint16_t max_flow_num,
14 uint16_t max_item_per_flow)
16 struct gro_tcp4_tbl *tbl;
18 uint32_t entries_num, i;
20 entries_num = max_flow_num * max_item_per_flow;
21 entries_num = RTE_MIN(entries_num, GRO_TCP4_TBL_MAX_ITEM_NUM);
26 tbl = rte_zmalloc_socket(__func__,
27 sizeof(struct gro_tcp4_tbl),
33 size = sizeof(struct gro_tcp4_item) * entries_num;
34 tbl->items = rte_zmalloc_socket(__func__,
38 if (tbl->items == NULL) {
42 tbl->max_item_num = entries_num;
44 size = sizeof(struct gro_tcp4_flow) * entries_num;
45 tbl->flows = rte_zmalloc_socket(__func__,
49 if (tbl->flows == NULL) {
54 /* INVALID_ARRAY_INDEX indicates an empty flow */
55 for (i = 0; i < entries_num; i++)
56 tbl->flows[i].start_index = INVALID_ARRAY_INDEX;
57 tbl->max_flow_num = entries_num;
63 gro_tcp4_tbl_destroy(void *tbl)
65 struct gro_tcp4_tbl *tcp_tbl = tbl;
68 rte_free(tcp_tbl->items);
69 rte_free(tcp_tbl->flows);
74 static inline uint32_t
75 find_an_empty_item(struct gro_tcp4_tbl *tbl)
78 uint32_t max_item_num = tbl->max_item_num;
80 for (i = 0; i < max_item_num; i++)
81 if (tbl->items[i].firstseg == NULL)
83 return INVALID_ARRAY_INDEX;
86 static inline uint32_t
87 find_an_empty_flow(struct gro_tcp4_tbl *tbl)
90 uint32_t max_flow_num = tbl->max_flow_num;
92 for (i = 0; i < max_flow_num; i++)
93 if (tbl->flows[i].start_index == INVALID_ARRAY_INDEX)
95 return INVALID_ARRAY_INDEX;
98 static inline uint32_t
99 insert_new_item(struct gro_tcp4_tbl *tbl,
100 struct rte_mbuf *pkt,
109 item_idx = find_an_empty_item(tbl);
110 if (item_idx == INVALID_ARRAY_INDEX)
111 return INVALID_ARRAY_INDEX;
113 tbl->items[item_idx].firstseg = pkt;
114 tbl->items[item_idx].lastseg = rte_pktmbuf_lastseg(pkt);
115 tbl->items[item_idx].start_time = start_time;
116 tbl->items[item_idx].next_pkt_idx = INVALID_ARRAY_INDEX;
117 tbl->items[item_idx].sent_seq = sent_seq;
118 tbl->items[item_idx].ip_id = ip_id;
119 tbl->items[item_idx].nb_merged = 1;
120 tbl->items[item_idx].is_atomic = is_atomic;
123 /* if the previous packet exists, chain them together. */
124 if (prev_idx != INVALID_ARRAY_INDEX) {
125 tbl->items[item_idx].next_pkt_idx =
126 tbl->items[prev_idx].next_pkt_idx;
127 tbl->items[prev_idx].next_pkt_idx = item_idx;
133 static inline uint32_t
134 delete_item(struct gro_tcp4_tbl *tbl, uint32_t item_idx,
135 uint32_t prev_item_idx)
137 uint32_t next_idx = tbl->items[item_idx].next_pkt_idx;
139 /* NULL indicates an empty item */
140 tbl->items[item_idx].firstseg = NULL;
142 if (prev_item_idx != INVALID_ARRAY_INDEX)
143 tbl->items[prev_item_idx].next_pkt_idx = next_idx;
148 static inline uint32_t
149 insert_new_flow(struct gro_tcp4_tbl *tbl,
150 struct tcp4_flow_key *src,
153 struct tcp4_flow_key *dst;
156 flow_idx = find_an_empty_flow(tbl);
157 if (unlikely(flow_idx == INVALID_ARRAY_INDEX))
158 return INVALID_ARRAY_INDEX;
160 dst = &(tbl->flows[flow_idx].key);
162 rte_ether_addr_copy(&(src->eth_saddr), &(dst->eth_saddr));
163 rte_ether_addr_copy(&(src->eth_daddr), &(dst->eth_daddr));
164 dst->ip_src_addr = src->ip_src_addr;
165 dst->ip_dst_addr = src->ip_dst_addr;
166 dst->recv_ack = src->recv_ack;
167 dst->src_port = src->src_port;
168 dst->dst_port = src->dst_port;
170 tbl->flows[flow_idx].start_index = item_idx;
177 * update the packet length for the flushed packet.
180 update_header(struct gro_tcp4_item *item)
182 struct rte_ipv4_hdr *ipv4_hdr;
183 struct rte_mbuf *pkt = item->firstseg;
185 ipv4_hdr = (struct rte_ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
187 ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len -
192 gro_tcp4_reassemble(struct rte_mbuf *pkt,
193 struct gro_tcp4_tbl *tbl,
196 struct rte_ether_hdr *eth_hdr;
197 struct rte_ipv4_hdr *ipv4_hdr;
198 struct rte_tcp_hdr *tcp_hdr;
201 uint16_t ip_id, hdr_len, frag_off;
204 struct tcp4_flow_key key;
205 uint32_t cur_idx, prev_idx, item_idx;
206 uint32_t i, max_flow_num, remaining_flow_num;
211 * Don't process the packet whose TCP header length is greater
212 * than 60 bytes or less than 20 bytes.
214 if (unlikely(INVALID_TCP_HDRLEN(pkt->l4_len)))
217 eth_hdr = rte_pktmbuf_mtod(pkt, struct rte_ether_hdr *);
218 ipv4_hdr = (struct rte_ipv4_hdr *)((char *)eth_hdr + pkt->l2_len);
219 tcp_hdr = (struct rte_tcp_hdr *)((char *)ipv4_hdr + pkt->l3_len);
220 hdr_len = pkt->l2_len + pkt->l3_len + pkt->l4_len;
223 * Don't process the packet which has FIN, SYN, RST, PSH, URG, ECE
226 if (tcp_hdr->tcp_flags != RTE_TCP_ACK_FLAG)
229 * Don't process the packet whose payload length is less than or
232 tcp_dl = pkt->pkt_len - hdr_len;
237 * Save IPv4 ID for the packet whose DF bit is 0. For the packet
238 * whose DF bit is 1, IPv4 ID is ignored.
240 frag_off = rte_be_to_cpu_16(ipv4_hdr->fragment_offset);
241 is_atomic = (frag_off & RTE_IPV4_HDR_DF_FLAG) == RTE_IPV4_HDR_DF_FLAG;
242 ip_id = is_atomic ? 0 : rte_be_to_cpu_16(ipv4_hdr->packet_id);
243 sent_seq = rte_be_to_cpu_32(tcp_hdr->sent_seq);
245 rte_ether_addr_copy(&(eth_hdr->src_addr), &(key.eth_saddr));
246 rte_ether_addr_copy(&(eth_hdr->dst_addr), &(key.eth_daddr));
247 key.ip_src_addr = ipv4_hdr->src_addr;
248 key.ip_dst_addr = ipv4_hdr->dst_addr;
249 key.src_port = tcp_hdr->src_port;
250 key.dst_port = tcp_hdr->dst_port;
251 key.recv_ack = tcp_hdr->recv_ack;
253 /* Search for a matched flow. */
254 max_flow_num = tbl->max_flow_num;
255 remaining_flow_num = tbl->flow_num;
257 for (i = 0; i < max_flow_num && remaining_flow_num; i++) {
258 if (tbl->flows[i].start_index != INVALID_ARRAY_INDEX) {
259 if (is_same_tcp4_flow(tbl->flows[i].key, key)) {
263 remaining_flow_num--;
268 * Fail to find a matched flow. Insert a new flow and store the
269 * packet into the flow.
272 item_idx = insert_new_item(tbl, pkt, start_time,
273 INVALID_ARRAY_INDEX, sent_seq, ip_id,
275 if (item_idx == INVALID_ARRAY_INDEX)
277 if (insert_new_flow(tbl, &key, item_idx) ==
278 INVALID_ARRAY_INDEX) {
280 * Fail to insert a new flow, so delete the
283 delete_item(tbl, item_idx, INVALID_ARRAY_INDEX);
290 * Check all packets in the flow and try to find a neighbor for
293 cur_idx = tbl->flows[i].start_index;
296 cmp = check_seq_option(&(tbl->items[cur_idx]), tcp_hdr,
297 sent_seq, ip_id, pkt->l4_len, tcp_dl, 0,
300 if (merge_two_tcp4_packets(&(tbl->items[cur_idx]),
301 pkt, cmp, sent_seq, ip_id, 0))
304 * Fail to merge the two packets, as the packet
305 * length is greater than the max value. Store
306 * the packet into the flow.
308 if (insert_new_item(tbl, pkt, start_time, prev_idx,
309 sent_seq, ip_id, is_atomic) ==
315 cur_idx = tbl->items[cur_idx].next_pkt_idx;
316 } while (cur_idx != INVALID_ARRAY_INDEX);
318 /* Fail to find a neighbor, so store the packet into the flow. */
319 if (insert_new_item(tbl, pkt, start_time, prev_idx, sent_seq,
320 ip_id, is_atomic) == INVALID_ARRAY_INDEX)
327 gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
328 uint64_t flush_timestamp,
329 struct rte_mbuf **out,
334 uint32_t max_flow_num = tbl->max_flow_num;
336 for (i = 0; i < max_flow_num; i++) {
337 if (unlikely(tbl->flow_num == 0))
340 j = tbl->flows[i].start_index;
341 while (j != INVALID_ARRAY_INDEX) {
342 if (tbl->items[j].start_time <= flush_timestamp) {
343 out[k++] = tbl->items[j].firstseg;
344 if (tbl->items[j].nb_merged > 1)
345 update_header(&(tbl->items[j]));
347 * Delete the packet and get the next
348 * packet in the flow.
350 j = delete_item(tbl, j, INVALID_ARRAY_INDEX);
351 tbl->flows[i].start_index = j;
352 if (j == INVALID_ARRAY_INDEX)
355 if (unlikely(k == nb_out))
359 * The left packets in this flow won't be
360 * timeout. Go to check other flows.
369 gro_tcp4_tbl_pkt_count(void *tbl)
371 struct gro_tcp4_tbl *gro_tbl = tbl;
374 return gro_tbl->item_num;