4 * Copyright(c) 2017 Intel Corporation. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
33 #include <rte_malloc.h>
35 #include <rte_cycles.h>
36 #include <rte_ethdev.h>
43 gro_tcp4_tbl_create(uint16_t socket_id,
44 uint16_t max_flow_num,
45 uint16_t max_item_per_flow)
47 struct gro_tcp4_tbl *tbl;
49 uint32_t entries_num, i;
51 entries_num = max_flow_num * max_item_per_flow;
52 entries_num = RTE_MIN(entries_num, GRO_TCP4_TBL_MAX_ITEM_NUM);
57 tbl = rte_zmalloc_socket(__func__,
58 sizeof(struct gro_tcp4_tbl),
64 size = sizeof(struct gro_tcp4_item) * entries_num;
65 tbl->items = rte_zmalloc_socket(__func__,
69 if (tbl->items == NULL) {
73 tbl->max_item_num = entries_num;
75 size = sizeof(struct gro_tcp4_key) * entries_num;
76 tbl->keys = rte_zmalloc_socket(__func__,
80 if (tbl->keys == NULL) {
85 /* INVALID_ARRAY_INDEX indicates empty key */
86 for (i = 0; i < entries_num; i++)
87 tbl->keys[i].start_index = INVALID_ARRAY_INDEX;
88 tbl->max_key_num = entries_num;
94 gro_tcp4_tbl_destroy(void *tbl)
96 struct gro_tcp4_tbl *tcp_tbl = tbl;
99 rte_free(tcp_tbl->items);
100 rte_free(tcp_tbl->keys);
106 * merge two TCP/IPv4 packets without updating checksums.
107 * If cmp is larger than 0, append the new packet to the
108 * original packet. Otherwise, pre-pend the new packet to
109 * the original packet.
112 merge_two_tcp4_packets(struct gro_tcp4_item *item_src,
113 struct rte_mbuf *pkt,
118 struct rte_mbuf *pkt_head, *pkt_tail, *lastseg;
119 uint16_t tcp_datalen;
122 pkt_head = item_src->firstseg;
126 pkt_tail = item_src->firstseg;
129 /* check if the packet length will be beyond the max value */
130 tcp_datalen = pkt_tail->pkt_len - pkt_tail->l2_len -
131 pkt_tail->l3_len - pkt_tail->l4_len;
132 if (pkt_head->pkt_len - pkt_head->l2_len + tcp_datalen >
136 /* remove packet header for the tail packet */
137 rte_pktmbuf_adj(pkt_tail,
142 /* chain two packets together */
144 item_src->lastseg->next = pkt;
145 item_src->lastseg = rte_pktmbuf_lastseg(pkt);
146 /* update IP ID to the larger value */
147 item_src->ip_id = ip_id;
149 lastseg = rte_pktmbuf_lastseg(pkt);
150 lastseg->next = item_src->firstseg;
151 item_src->firstseg = pkt;
152 /* update sent_seq to the smaller value */
153 item_src->sent_seq = sent_seq;
155 item_src->nb_merged++;
157 /* update mbuf metadata for the merged packet */
158 pkt_head->nb_segs += pkt_tail->nb_segs;
159 pkt_head->pkt_len += pkt_tail->pkt_len;
165 check_seq_option(struct gro_tcp4_item *item,
166 struct tcp_hdr *tcp_hdr,
172 struct rte_mbuf *pkt0 = item->firstseg;
173 struct ipv4_hdr *ipv4_hdr0;
174 struct tcp_hdr *tcp_hdr0;
175 uint16_t tcp_hl0, tcp_dl0;
178 ipv4_hdr0 = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt0, char *) +
180 tcp_hdr0 = (struct tcp_hdr *)((char *)ipv4_hdr0 + pkt0->l3_len);
181 tcp_hl0 = pkt0->l4_len;
183 /* check if TCP option fields equal. If not, return 0. */
184 len = RTE_MAX(tcp_hl, tcp_hl0) - sizeof(struct tcp_hdr);
185 if ((tcp_hl != tcp_hl0) ||
186 ((len > 0) && (memcmp(tcp_hdr + 1,
191 /* check if the two packets are neighbors */
192 tcp_dl0 = pkt0->pkt_len - pkt0->l2_len - pkt0->l3_len - tcp_hl0;
193 if ((sent_seq == (item->sent_seq + tcp_dl0)) &&
194 (ip_id == (item->ip_id + 1)))
195 /* append the new packet */
197 else if (((sent_seq + tcp_dl) == item->sent_seq) &&
198 ((ip_id + item->nb_merged) == item->ip_id))
199 /* pre-pend the new packet */
205 static inline uint32_t
206 find_an_empty_item(struct gro_tcp4_tbl *tbl)
209 uint32_t max_item_num = tbl->max_item_num;
211 for (i = 0; i < max_item_num; i++)
212 if (tbl->items[i].firstseg == NULL)
214 return INVALID_ARRAY_INDEX;
217 static inline uint32_t
218 find_an_empty_key(struct gro_tcp4_tbl *tbl)
221 uint32_t max_key_num = tbl->max_key_num;
223 for (i = 0; i < max_key_num; i++)
224 if (tbl->keys[i].start_index == INVALID_ARRAY_INDEX)
226 return INVALID_ARRAY_INDEX;
229 static inline uint32_t
230 insert_new_item(struct gro_tcp4_tbl *tbl,
231 struct rte_mbuf *pkt,
239 item_idx = find_an_empty_item(tbl);
240 if (item_idx == INVALID_ARRAY_INDEX)
241 return INVALID_ARRAY_INDEX;
243 tbl->items[item_idx].firstseg = pkt;
244 tbl->items[item_idx].lastseg = rte_pktmbuf_lastseg(pkt);
245 tbl->items[item_idx].start_time = start_time;
246 tbl->items[item_idx].next_pkt_idx = INVALID_ARRAY_INDEX;
247 tbl->items[item_idx].sent_seq = sent_seq;
248 tbl->items[item_idx].ip_id = ip_id;
249 tbl->items[item_idx].nb_merged = 1;
252 /* if the previous packet exists, chain the new one with it */
253 if (prev_idx != INVALID_ARRAY_INDEX) {
254 tbl->items[item_idx].next_pkt_idx =
255 tbl->items[prev_idx].next_pkt_idx;
256 tbl->items[prev_idx].next_pkt_idx = item_idx;
262 static inline uint32_t
263 delete_item(struct gro_tcp4_tbl *tbl, uint32_t item_idx,
264 uint32_t prev_item_idx)
266 uint32_t next_idx = tbl->items[item_idx].next_pkt_idx;
268 /* set NULL to firstseg to indicate it's an empty item */
269 tbl->items[item_idx].firstseg = NULL;
271 if (prev_item_idx != INVALID_ARRAY_INDEX)
272 tbl->items[prev_item_idx].next_pkt_idx = next_idx;
277 static inline uint32_t
278 insert_new_key(struct gro_tcp4_tbl *tbl,
279 struct tcp4_key *key_src,
282 struct tcp4_key *key_dst;
285 key_idx = find_an_empty_key(tbl);
286 if (key_idx == INVALID_ARRAY_INDEX)
287 return INVALID_ARRAY_INDEX;
289 key_dst = &(tbl->keys[key_idx].key);
291 ether_addr_copy(&(key_src->eth_saddr), &(key_dst->eth_saddr));
292 ether_addr_copy(&(key_src->eth_daddr), &(key_dst->eth_daddr));
293 key_dst->ip_src_addr = key_src->ip_src_addr;
294 key_dst->ip_dst_addr = key_src->ip_dst_addr;
295 key_dst->recv_ack = key_src->recv_ack;
296 key_dst->src_port = key_src->src_port;
297 key_dst->dst_port = key_src->dst_port;
299 /* non-INVALID_ARRAY_INDEX value indicates this key is valid */
300 tbl->keys[key_idx].start_index = item_idx;
307 is_same_key(struct tcp4_key k1, struct tcp4_key k2)
309 if (is_same_ether_addr(&k1.eth_saddr, &k2.eth_saddr) == 0)
312 if (is_same_ether_addr(&k1.eth_daddr, &k2.eth_daddr) == 0)
315 return ((k1.ip_src_addr == k2.ip_src_addr) &&
316 (k1.ip_dst_addr == k2.ip_dst_addr) &&
317 (k1.recv_ack == k2.recv_ack) &&
318 (k1.src_port == k2.src_port) &&
319 (k1.dst_port == k2.dst_port));
323 * update packet length for the flushed packet.
326 update_header(struct gro_tcp4_item *item)
328 struct ipv4_hdr *ipv4_hdr;
329 struct rte_mbuf *pkt = item->firstseg;
331 ipv4_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(pkt, char *) +
333 ipv4_hdr->total_length = rte_cpu_to_be_16(pkt->pkt_len -
338 gro_tcp4_reassemble(struct rte_mbuf *pkt,
339 struct gro_tcp4_tbl *tbl,
342 struct ether_hdr *eth_hdr;
343 struct ipv4_hdr *ipv4_hdr;
344 struct tcp_hdr *tcp_hdr;
346 uint16_t tcp_dl, ip_id;
349 uint32_t cur_idx, prev_idx, item_idx;
350 uint32_t i, max_key_num;
353 eth_hdr = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
354 ipv4_hdr = (struct ipv4_hdr *)((char *)eth_hdr + pkt->l2_len);
355 tcp_hdr = (struct tcp_hdr *)((char *)ipv4_hdr + pkt->l3_len);
358 * if FIN, SYN, RST, PSH, URG, ECE or
359 * CWR is set, return immediately.
361 if (tcp_hdr->tcp_flags != TCP_ACK_FLAG)
363 /* if payload length is 0, return immediately */
364 tcp_dl = rte_be_to_cpu_16(ipv4_hdr->total_length) - pkt->l3_len -
369 ip_id = rte_be_to_cpu_16(ipv4_hdr->packet_id);
370 sent_seq = rte_be_to_cpu_32(tcp_hdr->sent_seq);
372 ether_addr_copy(&(eth_hdr->s_addr), &(key.eth_saddr));
373 ether_addr_copy(&(eth_hdr->d_addr), &(key.eth_daddr));
374 key.ip_src_addr = ipv4_hdr->src_addr;
375 key.ip_dst_addr = ipv4_hdr->dst_addr;
376 key.src_port = tcp_hdr->src_port;
377 key.dst_port = tcp_hdr->dst_port;
378 key.recv_ack = tcp_hdr->recv_ack;
380 /* search for a key */
381 max_key_num = tbl->max_key_num;
382 for (i = 0; i < max_key_num; i++) {
383 if ((tbl->keys[i].start_index != INVALID_ARRAY_INDEX) &&
384 is_same_key(tbl->keys[i].key, key))
388 /* can't find a key, so insert a new key and a new item. */
389 if (i == tbl->max_key_num) {
390 item_idx = insert_new_item(tbl, pkt, ip_id, sent_seq,
391 INVALID_ARRAY_INDEX, start_time);
392 if (item_idx == INVALID_ARRAY_INDEX)
394 if (insert_new_key(tbl, &key, item_idx) ==
395 INVALID_ARRAY_INDEX) {
397 * fail to insert a new key, so
398 * delete the inserted item
400 delete_item(tbl, item_idx, INVALID_ARRAY_INDEX);
406 /* traverse all packets in the item group to find one to merge */
407 cur_idx = tbl->keys[i].start_index;
410 cmp = check_seq_option(&(tbl->items[cur_idx]), tcp_hdr,
411 pkt->l4_len, tcp_dl, ip_id, sent_seq);
413 if (merge_two_tcp4_packets(&(tbl->items[cur_idx]),
418 * fail to merge two packets since the packet
419 * length will be greater than the max value.
420 * So insert the packet into the item group.
422 if (insert_new_item(tbl, pkt, ip_id, sent_seq,
423 prev_idx, start_time) ==
429 cur_idx = tbl->items[cur_idx].next_pkt_idx;
430 } while (cur_idx != INVALID_ARRAY_INDEX);
433 * can't find a packet in the item group to merge,
434 * so insert the packet into the item group.
436 if (insert_new_item(tbl, pkt, ip_id, sent_seq, prev_idx,
437 start_time) == INVALID_ARRAY_INDEX)
444 gro_tcp4_tbl_timeout_flush(struct gro_tcp4_tbl *tbl,
445 uint64_t flush_timestamp,
446 struct rte_mbuf **out,
451 uint32_t max_key_num = tbl->max_key_num;
453 for (i = 0; i < max_key_num; i++) {
454 /* all keys have been checked, return immediately */
455 if (tbl->key_num == 0)
458 j = tbl->keys[i].start_index;
459 while (j != INVALID_ARRAY_INDEX) {
460 if (tbl->items[j].start_time <= flush_timestamp) {
461 out[k++] = tbl->items[j].firstseg;
462 if (tbl->items[j].nb_merged > 1)
463 update_header(&(tbl->items[j]));
465 * delete the item and get
466 * the next packet index
468 j = delete_item(tbl, j,
469 INVALID_ARRAY_INDEX);
472 * delete the key as all of
473 * packets are flushed
475 if (j == INVALID_ARRAY_INDEX) {
476 tbl->keys[i].start_index =
480 /* update start_index of the key */
481 tbl->keys[i].start_index = j;
487 * left packets of this key won't be
488 * timeout, so go to check other keys.
497 gro_tcp4_tbl_pkt_count(void *tbl)
499 struct gro_tcp4_tbl *gro_tbl = tbl;
502 return gro_tbl->item_num;