eal: add and use unaligned integer types
[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(const 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
50         first_len = fp->frags[IP_FIRST_FRAG_IDX].len;
51         n = fp->last_idx - 1;
52
53         /*start from the last fragment. */
54         m = fp->frags[IP_LAST_FRAG_IDX].mb;
55         ofs = fp->frags[IP_LAST_FRAG_IDX].ofs;
56
57         while (ofs != first_len) {
58
59                 prev = m;
60
61                 for (i = n; i != IP_FIRST_FRAG_IDX && ofs != first_len; i--) {
62
63                         /* previous fragment found. */
64                         if(fp->frags[i].ofs + fp->frags[i].len == ofs) {
65
66                                 ip_frag_chain(fp->frags[i].mb, m);
67
68                                 /* update our last fragment and offset. */
69                                 m = fp->frags[i].mb;
70                                 ofs = fp->frags[i].ofs;
71                         }
72                 }
73
74                 /* error - hole in the packet. */
75                 if (m == prev) {
76                         return NULL;
77                 }
78         }
79
80         /* chain with the first fragment. */
81         ip_frag_chain(fp->frags[IP_FIRST_FRAG_IDX].mb, m);
82         m = fp->frags[IP_FIRST_FRAG_IDX].mb;
83
84         /* update mbuf fields for reassembled packet. */
85         m->ol_flags |= PKT_TX_IP_CKSUM;
86
87         /* update ipv4 header for the reassmebled packet */
88         ip_hdr = (struct ipv4_hdr*)(rte_pktmbuf_mtod(m, uint8_t *) +
89                 m->l2_len);
90
91         ip_hdr->total_length = rte_cpu_to_be_16((uint16_t)(fp->total_size +
92                 m->l3_len));
93         ip_hdr->fragment_offset = (uint16_t)(ip_hdr->fragment_offset &
94                 rte_cpu_to_be_16(IPV4_HDR_DF_FLAG));
95         ip_hdr->hdr_checksum = 0;
96
97         return m;
98 }
99
100 /*
101  * Process new mbuf with fragment of IPV4 packet.
102  * Incoming mbuf should have it's l2_len/l3_len fields setuped correclty.
103  * @param tbl
104  *   Table where to lookup/add the fragmented packet.
105  * @param mb
106  *   Incoming mbuf with IPV4 fragment.
107  * @param tms
108  *   Fragment arrival timestamp.
109  * @param ip_hdr
110  *   Pointer to the IPV4 header inside the fragment.
111  * @return
112  *   Pointer to mbuf for reassebled packet, or NULL if:
113  *   - an error occured.
114  *   - not all fragments of the packet are collected yet.
115  */
116 struct rte_mbuf *
117 rte_ipv4_frag_reassemble_packet(struct rte_ip_frag_tbl *tbl,
118                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb, uint64_t tms,
119                 struct ipv4_hdr *ip_hdr)
120 {
121         struct ip_frag_pkt *fp;
122         struct ip_frag_key key;
123         const unaligned_uint64_t *psd;
124         uint16_t ip_len;
125         uint16_t flag_offset, ip_ofs, ip_flag;
126
127         flag_offset = rte_be_to_cpu_16(ip_hdr->fragment_offset);
128         ip_ofs = (uint16_t)(flag_offset & IPV4_HDR_OFFSET_MASK);
129         ip_flag = (uint16_t)(flag_offset & IPV4_HDR_MF_FLAG);
130
131         psd = (unaligned_uint64_t *)&ip_hdr->src_addr;
132         /* use first 8 bytes only */
133         key.src_dst[0] = psd[0];
134         key.id = ip_hdr->packet_id;
135         key.key_len = IPV4_KEYLEN;
136
137         ip_ofs *= IPV4_HDR_OFFSET_UNITS;
138         ip_len = (uint16_t)(rte_be_to_cpu_16(ip_hdr->total_length) -
139                 mb->l3_len);
140
141         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
142                 "mbuf: %p, tms: %" PRIu64
143                 ", key: <%" PRIx64 ", %#x>, ofs: %u, len: %u, flags: %#x\n"
144                 "tbl: %p, max_cycles: %" PRIu64 ", entry_mask: %#x, "
145                 "max_entries: %u, use_entries: %u\n\n",
146                 __func__, __LINE__,
147                 mb, tms, key.src_dst[0], key.id, ip_ofs, ip_len, ip_flag,
148                 tbl, tbl->max_cycles, tbl->entry_mask, tbl->max_entries,
149                 tbl->use_entries);
150
151         /* try to find/add entry into the fragment's table. */
152         if ((fp = ip_frag_find(tbl, dr, &key, tms)) == NULL) {
153                 IP_FRAG_MBUF2DR(dr, mb);
154                 return NULL;
155         }
156
157         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
158                 "tbl: %p, max_entries: %u, use_entries: %u\n"
159                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
160                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
161                 __func__, __LINE__,
162                 tbl, tbl->max_entries, tbl->use_entries,
163                 fp, fp->key.src_dst[0], fp->key.id, fp->start,
164                 fp->total_size, fp->frag_size, fp->last_idx);
165
166
167         /* process the fragmented packet. */
168         mb = ip_frag_process(fp, dr, mb, ip_ofs, ip_len, ip_flag);
169         ip_frag_inuse(tbl, fp);
170
171         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
172                 "mbuf: %p\n"
173                 "tbl: %p, max_entries: %u, use_entries: %u\n"
174                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, start: %" PRIu64
175                 ", total_size: %u, frag_size: %u, last_idx: %u\n\n",
176                 __func__, __LINE__, mb,
177                 tbl, tbl->max_entries, tbl->use_entries,
178                 fp, fp->key.src_dst[0], fp->key.id, fp->start,
179                 fp->total_size, fp->frag_size, fp->last_idx);
180
181         return mb;
182 }