4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_debug.h>
37 #include <rte_tailq.h>
39 #include "ip_frag_common.h"
42 * Reassemble fragments into one packet.
45 ipv4_frag_reassemble(const struct ip_frag_pkt *fp)
47 struct ipv4_hdr *ip_hdr;
48 struct rte_mbuf *m, *prev;
49 uint32_t i, n, ofs, first_len;
51 first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
54 /*start from the last fragment. */
55 m = fp->frags[IP_LAST_FRAG_IDX].mb;
56 ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
58 while (ofs != first_len) {
62 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
64 /* previous fragment found. */
65 if(fp->frags[i].ofs + fp->frags[i].len == ofs) {
67 ip_frag_chain(fp->frags[i].mb, m);
69 /* update our last fragment and offset. */
71 ofs = fp->frags[i].ofs;
75 /* error - hole in the packet. */
81 /* chain with the first fragment. */
82 ip_frag_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
83 m = fp->frags[IP_FIRST_FRAG_IDX].mb;
85 /* update mbuf fields for reassembled packet. */
86 m->ol_flags |= PKT_TX_IP_CKSUM;
88 /* update ipv4 header for the reassmebled packet */
89 ip_hdr = (struct ipv4_hdr*)(rte_pktmbuf_mtod(m, uint8_t *) +
92 ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size +
94 ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset &
95 rte_cpu_to_be_16(IPV4_HDR_DF_FLAG));
96 ip_hdr->hdr_checksum = 0;
102 * Process new mbuf with fragment of IPV4 packet.
103 * Incoming mbuf should have it's l2_len/l3_len fields setuped correclty.
105 * Table where to lookup/add the fragmented packet.
107 * Incoming mbuf with IPV4 fragment.
109 * Fragment arrival timestamp.
111 * Pointer to the IPV4 header inside the fragment.
113 * Pointer to mbuf for reassebled packet, or NULL if:
114 * - an error occured.
115 * - not all fragments of the packet are collected yet.
118 rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
119 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
120 struct ipv4_hdr *ip_hdr)
122 struct ip_frag_pkt *fp;
123 struct ip_frag_key key;
126 uint16_t flag_offset, ip_ofs, ip_flag;
128 flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset);
129 ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
130 ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
132 psd = (uint64_t *)&ip_hdr->src_addr;
133 /* use first 8 bytes only */
134 key.src_dst[0] = psd[0];
135 key.id = ip_hdr->packet_id;
136 key.key_len = IPV4_KEYLEN;
138 ip_ofs *= IPV4_HDR_OFFSET_UNITS;
139 ip_len = (uint16_t)(rte_be_to_cpu_16(ip_hdr->total_length) -
142 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
143 "mbuf: %p, tms: %" PRIu64
144 ", key: <%" PRIx64 ", %#x>, ofs: %u, len: %u, flags: %#x\n"
145 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
146 "max_entries: %u, use_entries: %u\n\n",
148 mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, ip_flag,
149 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
152 /* try to find/add entry into the fragment's table. */
153 if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) {
154 IP_FRAG_MBUF2DR(dr, mb);
158 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
159 "tbl: %p, max_entries: %u, use_entries: %u\n"
160 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
161 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
163 tbl, tbl->max_entries, tbl->use_entries,
164 fp, fp->key.src_dst[0], fp->key.id, fp->start,
165 fp->total_size, fp->frag_size, fp->last_idx);
168 /* process the fragmented packet. */
169 mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag);
170 ip_frag_inuse(tbl, fp);
172 IP_FRAG_LOG(DEBUG, "%s:%d:\n"
174 "tbl: %p, max_entries: %u, use_entries: %u\n"
175 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
176 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
177 __func__, __LINE__, mb,
178 tbl, tbl->max_entries, tbl->use_entries,
179 fp, fp->key.src_dst[0], fp->key.id, fp->start,
180 fp->total_size, fp->frag_size, fp->last_idx);