1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include "ip_frag_common.h"
12 * Reassemble fragments into one packet.
15 ipv4_frag_reassemble(struct ip_frag_pkt *fp)
17 struct ipv4_hdr *ip_hdr;
18 struct rte_mbuf *m, *prev;
19 uint32_t i, n, ofs, first_len;
20 uint32_t curr_idx = 0;
22 first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
25 /*start from the last fragment. */
26 m = fp->frags[IP_LAST_FRAG_IDX].mb;
27 ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
28 curr_idx = IP_LAST_FRAG_IDX;
30 while (ofs != first_len) {
34 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
36 /* previous fragment found. */
37 if(fp->frags[i].ofs + fp->frags[i].len == ofs) {
39 RTE_ASSERT(curr_idx != i);
41 /* adjust start of the last fragment data. */
43 (uint16_t)(m->l2_len + m->l3_len));
44 rte_pktmbuf_chain(fp->frags[i].mb, m);
46 /* this mbuf should not be accessed directly */
47 fp->frags[curr_idx].mb = NULL;
50 /* update our last fragment and offset. */
52 ofs = fp->frags[i].ofs;
56 /* error - hole in the packet. */
62 /* chain with the first fragment. */
63 rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
64 rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
65 fp->frags[curr_idx].mb = NULL;
66 m = fp->frags[IP_FIRST_FRAG_IDX].mb;
67 fp->frags[IP_FIRST_FRAG_IDX].mb = NULL;
69 /* update mbuf fields for reassembled packet. */
70 m->ol_flags |= PKT_TX_IP_CKSUM;
72 /* update ipv4 header for the reassembled packet */
73 ip_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
75 ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size +
77 ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset &
78 rte_cpu_to_be_16(IPV4_HDR_DF_FLAG));
79 ip_hdr->hdr_checksum = 0;
85 * Process new mbuf with fragment of IPV4 packet.
86 * Incoming mbuf should have it's l2_len/l3_len fields setuped correclty.
88 * Table where to lookup/add the fragmented packet.
90 * Incoming mbuf with IPV4 fragment.
92 * Fragment arrival timestamp.
94 * Pointer to the IPV4 header inside the fragment.
96 * Pointer to mbuf for reassembled packet, or NULL if:
97 * - an error occurred.
98 * - not all fragments of the packet are collected yet.
101 rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
102 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
103 struct ipv4_hdr *ip_hdr)
105 struct ip_frag_pkt *fp;
106 struct ip_frag_key key;
107 const unaligned_uint64_t *psd;
108 uint16_t flag_offset, ip_ofs, ip_flag;
111 flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset);
112 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
113 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
115 psd = (unaligned_uint64_t *)&ip_hdr->src_addr;
116 /* use first 8 bytes only */
117 key.src_dst[0] = psd[0];
118 key.id = ip_hdr->packet_id;
119 key.key_len = IPV4_KEYLEN;
121 ip_ofs *= IPV4_HDR_OFFSET_UNITS;
122 ip_len = rte_be_to_cpu_16(ip_hdr->total_length) - mb->l3_len;
124 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
125 "mbuf: %p, tms: %" PRIu64
126 ", key: <%" PRIx64 ", %#x>, ofs: %u, len: %d, flags: %#x\n"
127 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
128 "max_entries: %u, use_entries: %u\n\n",
130 mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, ip_flag,
131 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
134 /* check that fragment length is greater then zero. */
136 IP_FRAG_MBUF2DR(dr, mb);
140 /* try to find/add entry into the fragment's table. */
141 if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) {
142 IP_FRAG_MBUF2DR(dr, mb);
146 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
147 "tbl: %p, max_entries: %u, use_entries: %u\n"
148 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
149 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
151 tbl, tbl->max_entries, tbl->use_entries,
152 fp, fp->key.src_dst[0], fp->key.id, fp->start,
153 fp->total_size, fp->frag_size, fp->last_idx);
156 /* process the fragmented packet. */
157 mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag);
158 ip_frag_inuse(tbl, fp);
160 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
162 "tbl: %p, max_entries: %u, use_entries: %u\n"
163 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
164 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
165 __func__, __LINE__, mb,
166 tbl, tbl->max_entries, tbl->use_entries,
167 fp, fp->key.src_dst[0], fp->key.id, fp->start,
168 fp->total_size, fp->frag_size, fp->last_idx);