ip_frag: refactor reassembly code into a proper library
[dpdk.git] / lib / librte_ip_frag / rte_ip_frag_common.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 #include <stdint.h>
36 #include <stdio.h>
37
38 #include <rte_memory.h>
39 #include <rte_log.h>
40 #include <rte_byteorder.h>
41
42 #include "rte_ip_frag.h"
43 #include "ip_frag_common.h"
44
45 #define IP_FRAG_HASH_FNUM       2
46
47 /* free mbufs from death row */
48 void
49 rte_ip_frag_free_death_row(struct rte_ip_frag_death_row *dr,
50                 uint32_t prefetch)
51 {
52         uint32_t i, k, n;
53
54         k = RTE_MIN(prefetch, dr->cnt);
55         n = dr->cnt;
56
57         for (i = 0; i != k; i++)
58                 rte_prefetch0(dr->row[i]);
59
60         for (i = 0; i != n - k; i++) {
61                 rte_prefetch0(dr->row[i + k]);
62                 rte_pktmbuf_free(dr->row[i]);
63         }
64
65         for (; i != n; i++)
66                 rte_pktmbuf_free(dr->row[i]);
67
68         dr->cnt = 0;
69 }
70
71 /* create fragmentation table */
72 struct rte_ip_frag_tbl *
73 rte_ip_frag_table_create(uint32_t bucket_num, uint32_t bucket_entries,
74         uint32_t max_entries, uint64_t max_cycles, int socket_id)
75 {
76         struct rte_ip_frag_tbl *tbl;
77         size_t sz;
78         uint64_t nb_entries;
79
80         nb_entries = rte_align32pow2(bucket_num);
81         nb_entries *= bucket_entries;
82         nb_entries *= IP_FRAG_HASH_FNUM;
83
84         /* check input parameters. */
85         if (rte_is_power_of_2(bucket_entries) == 0 ||
86                         nb_entries > UINT32_MAX || nb_entries == 0 ||
87                         nb_entries < max_entries) {
88                 RTE_LOG(ERR, USER1, "%s: invalid input parameter\n", __func__);
89                 return (NULL);
90         }
91
92         sz = sizeof (*tbl) + nb_entries * sizeof (tbl->pkt[0]);
93         if ((tbl = rte_zmalloc_socket(__func__, sz, CACHE_LINE_SIZE,
94                         socket_id)) == NULL) {
95                 RTE_LOG(ERR, USER1,
96                         "%s: allocation of %zu bytes at socket %d failed do\n",
97                         __func__, sz, socket_id);
98                 return (NULL);
99         }
100
101         RTE_LOG(INFO, USER1, "%s: allocated of %zu bytes at socket %d\n",
102                 __func__, sz, socket_id);
103
104         tbl->max_cycles = max_cycles;
105         tbl->max_entries = max_entries;
106         tbl->nb_entries = (uint32_t)nb_entries;
107         tbl->nb_buckets = bucket_num;
108         tbl->bucket_entries = bucket_entries;
109         tbl->entry_mask = (tbl->nb_entries - 1) & ~(tbl->bucket_entries  - 1);
110
111         TAILQ_INIT(&(tbl->lru));
112         return (tbl);
113 }
114
115 /* dump frag table statistics to file */
116 void
117 rte_ip_frag_table_statistics_dump(FILE *f, const struct rte_ip_frag_tbl *tbl)
118 {
119         uint64_t fail_total, fail_nospace;
120
121         fail_total = tbl->stat.fail_total;
122         fail_nospace = tbl->stat.fail_nospace;
123
124         fprintf(f, "max entries:\t%u;\n"
125                 "entries in use:\t%u;\n"
126                 "finds/inserts:\t%" PRIu64 ";\n"
127                 "entries added:\t%" PRIu64 ";\n"
128                 "entries deleted by timeout:\t%" PRIu64 ";\n"
129                 "entries reused by timeout:\t%" PRIu64 ";\n"
130                 "total add failures:\t%" PRIu64 ";\n"
131                 "add no-space failures:\t%" PRIu64 ";\n"
132                 "add hash-collisions failures:\t%" PRIu64 ";\n",
133                 tbl->max_entries,
134                 tbl->use_entries,
135                 tbl->stat.find_num,
136                 tbl->stat.add_num,
137                 tbl->stat.del_num,
138                 tbl->stat.reuse_num,
139                 fail_total,
140                 fail_nospace,
141                 fail_total - fail_nospace);
142 }