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