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