b13308966144a8c291249719f5092b455e13cd7f
[dpdk.git] / lib / librte_ip_frag / rte_ipv4_reassembly.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stddef.h>
35
36 #include <rte_debug.h>
37
38 #include "ip_frag_common.h"
39
40 /*
41  * Reassemble fragments into one packet.
42  */
43 struct rte_mbuf *
44 ipv4_frag_reassemble(struct ip_frag_pkt *fp)
45 {
46         struct ipv4_hdr *ip_hdr;
47         struct rte_mbuf *m, *prev;
48         uint32_t i, n, ofs, first_len;
49         uint32_t curr_idx = 0;
50
51         first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
52         n = fp->last_idx - 1;
53
54         /*start from the last fragment. */
55         m = fp->frags[IP_LAST_FRAG_IDX].mb;
56         ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
57         curr_idx = IP_LAST_FRAG_IDX;
58
59         while (ofs != first_len) {
60
61                 prev = m;
62
63                 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
64
65                         /* previous fragment found. */
66                         if(fp->frags[i].ofs + fp->frags[i].len == ofs) {
67
68                                 /* adjust start of the last fragment data. */
69                                 rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
70                                 rte_pktmbuf_chain(fp->frags[i].mb, m);
71
72                                 /* this mbuf should not be accessed directly */
73                                 fp->frags[curr_idx].mb = NULL;
74                                 curr_idx = i;
75
76                                 /* update our last fragment and offset. */
77                                 m = fp->frags[i].mb;
78                                 ofs = fp->frags[i].ofs;
79                         }
80                 }
81
82                 /* error - hole in the packet. */
83                 if (m == prev) {
84                         return NULL;
85                 }
86         }
87
88         /* chain with the first fragment. */
89         rte_pktmbuf_adj(m, (uint16_t)(m->l2_len + m->l3_len));
90         rte_pktmbuf_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
91         m = fp->frags[IP_FIRST_FRAG_IDX].mb;
92
93         /* update mbuf fields for reassembled packet. */
94         m->ol_flags |= PKT_TX_IP_CKSUM;
95
96         /* update ipv4 header for the reassmebled packet */
97         ip_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *, m->l2_len);
98
99         ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size +
100                 m->l3_len));
101         ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset &
102                 rte_cpu_to_be_16(IPV4_HDR_DF_FLAG));
103         ip_hdr->hdr_checksum = 0;
104
105         return m;
106 }
107
108 /*
109  * Process new mbuf with fragment of IPV4 packet.
110  * Incoming mbuf should have it's l2_len/l3_len fields setuped correclty.
111  * @param tbl
112  *   Table where to lookup/add the fragmented packet.
113  * @param mb
114  *   Incoming mbuf with IPV4 fragment.
115  * @param tms
116  *   Fragment arrival timestamp.
117  * @param ip_hdr
118  *   Pointer to the IPV4 header inside the fragment.
119  * @return
120  *   Pointer to mbuf for reassebled packet, or NULL if:
121  *   - an error occurred.
122  *   - not all fragments of the packet are collected yet.
123  */
124 struct rte_mbuf *
125 rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
126                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
127                 struct ipv4_hdr *ip_hdr)
128 {
129         struct ip_frag_pkt *fp;
130         struct ip_frag_key key;
131         const unaligned_uint64_t *psd;
132         uint16_t ip_len;
133         uint16_t flag_offset, ip_ofs, ip_flag;
134
135         flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset);
136         ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
137         ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
138
139         psd = (unaligned_uint64_t *)&ip_hdr->src_addr;
140         /* use first 8 bytes only */
141         key.src_dst[0] = psd[0];
142         key.id = ip_hdr->packet_id;
143         key.key_len = IPV4_KEYLEN;
144
145         ip_ofs *= IPV4_HDR_OFFSET_UNITS;
146         ip_len = (uint16_t)(rte_be_to_cpu_16(ip_hdr->total_length) -
147                 mb->l3_len);
148
149         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
150                 "mbuf: %p, tms: %" PRIu64
151                 ", key: <%" PRIx64 ", %#x>, ofs: %u, len: %u, flags: %#x\n"
152                 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
153                 "max_entries: %u, use_entries: %u\n\n",
154                 __func__, __LINE__,
155                 mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, ip_flag,
156                 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
157                 tbl->use_entries);
158
159         /* try to find/add entry into the fragment's table. */
160         if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) {
161                 IP_FRAG_MBUF2DR(dr, mb);
162                 return NULL;
163         }
164
165         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
166                 "tbl: %p, max_entries: %u, use_entries: %u\n"
167                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
168                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
169                 __func__, __LINE__,
170                 tbl, tbl->max_entries, tbl->use_entries,
171                 fp, fp->key.src_dst[0], fp->key.id, fp->start,
172                 fp->total_size, fp->frag_size, fp->last_idx);
173
174
175         /* process the fragmented packet. */
176         mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag);
177         ip_frag_inuse(tbl, fp);
178
179         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
180                 "mbuf: %p\n"
181                 "tbl: %p, max_entries: %u, use_entries: %u\n"
182                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
183                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
184                 __func__, __LINE__, mb,
185                 tbl, tbl->max_entries, tbl->use_entries,
186                 fp, fp->key.src_dst[0], fp->key.id, fp->start,
187                 fp->total_size, fp->frag_size, fp->last_idx);
188
189         return mb;
190 }