mem: move librte_malloc to eal/common
[dpdk.git] / lib / librte_table / rte_table_lpm_ipv6.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 <string.h>
35 #include <stdio.h>
36
37 #include <rte_common.h>
38 #include <rte_mbuf.h>
39 #include <rte_memory.h>
40 #include <rte_malloc.h>
41 #include <rte_byteorder.h>
42 #include <rte_log.h>
43 #include <rte_lpm6.h>
44
45 #include "rte_table_lpm_ipv6.h"
46
47 #define RTE_TABLE_LPM_MAX_NEXT_HOPS                        256
48
49 #ifdef RTE_TABLE_STATS_COLLECT
50
51 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val) \
52         table->stats.n_pkts_in += val
53 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val) \
54         table->stats.n_pkts_lookup_miss += val
55
56 #else
57
58 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(table, val)
59 #define RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(table, val)
60
61 #endif
62
63 struct rte_table_lpm_ipv6 {
64         struct rte_table_stats stats;
65
66         /* Input parameters */
67         uint32_t entry_size;
68         uint32_t entry_unique_size;
69         uint32_t n_rules;
70         uint32_t offset;
71
72         /* Handle to low-level LPM table */
73         struct rte_lpm6 *lpm;
74
75         /* Next Hop Table (NHT) */
76         uint32_t nht_users[RTE_TABLE_LPM_MAX_NEXT_HOPS];
77         uint8_t nht[0] __rte_cache_aligned;
78 };
79
80 static void *
81 rte_table_lpm_ipv6_create(void *params, int socket_id, uint32_t entry_size)
82 {
83         struct rte_table_lpm_ipv6_params *p =
84                 (struct rte_table_lpm_ipv6_params *) params;
85         struct rte_table_lpm_ipv6 *lpm;
86         struct rte_lpm6_config lpm6_config;
87         uint32_t total_size, nht_size;
88
89         /* Check input parameters */
90         if (p == NULL) {
91                 RTE_LOG(ERR, TABLE, "%s: NULL input parameters\n", __func__);
92                 return NULL;
93         }
94         if (p->n_rules == 0) {
95                 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
96                 return NULL;
97         }
98         if (p->number_tbl8s == 0) {
99                 RTE_LOG(ERR, TABLE, "%s: Invalid n_rules\n", __func__);
100                 return NULL;
101         }
102         if (p->entry_unique_size == 0) {
103                 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
104                         __func__);
105                 return NULL;
106         }
107         if (p->entry_unique_size > entry_size) {
108                 RTE_LOG(ERR, TABLE, "%s: Invalid entry_unique_size\n",
109                         __func__);
110                 return NULL;
111         }
112
113         entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
114
115         /* Memory allocation */
116         nht_size = RTE_TABLE_LPM_MAX_NEXT_HOPS * entry_size;
117         total_size = sizeof(struct rte_table_lpm_ipv6) + nht_size;
118         lpm = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
119                 socket_id);
120         if (lpm == NULL) {
121                 RTE_LOG(ERR, TABLE,
122                         "%s: Cannot allocate %u bytes for LPM IPv6 table\n",
123                         __func__, total_size);
124                 return NULL;
125         }
126
127         /* LPM low-level table creation */
128         lpm6_config.max_rules = p->n_rules;
129         lpm6_config.number_tbl8s = p->number_tbl8s;
130         lpm6_config.flags = 0;
131         lpm->lpm = rte_lpm6_create("LPM IPv6", socket_id, &lpm6_config);
132         if (lpm->lpm == NULL) {
133                 rte_free(lpm);
134                 RTE_LOG(ERR, TABLE,
135                         "Unable to create low-level LPM IPv6 table\n");
136                 return NULL;
137         }
138
139         /* Memory initialization */
140         lpm->entry_size = entry_size;
141         lpm->entry_unique_size = p->entry_unique_size;
142         lpm->n_rules = p->n_rules;
143         lpm->offset = p->offset;
144
145         return lpm;
146 }
147
148 static int
149 rte_table_lpm_ipv6_free(void *table)
150 {
151         struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
152
153         /* Check input parameters */
154         if (lpm == NULL) {
155                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
156                 return -EINVAL;
157         }
158
159         /* Free previously allocated resources */
160         rte_lpm6_free(lpm->lpm);
161         rte_free(lpm);
162
163         return 0;
164 }
165
166 static int
167 nht_find_free(struct rte_table_lpm_ipv6 *lpm, uint32_t *pos)
168 {
169         uint32_t i;
170
171         for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
172                 if (lpm->nht_users[i] == 0) {
173                         *pos = i;
174                         return 1;
175                 }
176         }
177
178         return 0;
179 }
180
181 static int
182 nht_find_existing(struct rte_table_lpm_ipv6 *lpm, void *entry, uint32_t *pos)
183 {
184         uint32_t i;
185
186         for (i = 0; i < RTE_TABLE_LPM_MAX_NEXT_HOPS; i++) {
187                 uint8_t *nht_entry = &lpm->nht[i * lpm->entry_size];
188
189                 if ((lpm->nht_users[i] > 0) && (memcmp(nht_entry, entry,
190                         lpm->entry_unique_size) == 0)) {
191                         *pos = i;
192                         return 1;
193                 }
194         }
195
196         return 0;
197 }
198
199 static int
200 rte_table_lpm_ipv6_entry_add(
201         void *table,
202         void *key,
203         void *entry,
204         int *key_found,
205         void **entry_ptr)
206 {
207         struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
208         struct rte_table_lpm_ipv6_key *ip_prefix =
209                 (struct rte_table_lpm_ipv6_key *) key;
210         uint32_t nht_pos, nht_pos0_valid;
211         int status;
212         uint8_t nht_pos0;
213
214         /* Check input parameters */
215         if (lpm == NULL) {
216                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
217                 return -EINVAL;
218         }
219         if (ip_prefix == NULL) {
220                 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
221                         __func__);
222                 return -EINVAL;
223         }
224         if (entry == NULL) {
225                 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
226                 return -EINVAL;
227         }
228
229         if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
230                 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
231                         ip_prefix->depth);
232                 return -EINVAL;
233         }
234
235         /* Check if rule is already present in the table */
236         status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
237                 ip_prefix->depth, &nht_pos0);
238         nht_pos0_valid = status > 0;
239
240         /* Find existing or free NHT entry */
241         if (nht_find_existing(lpm, entry, &nht_pos) == 0) {
242                 uint8_t *nht_entry;
243
244                 if (nht_find_free(lpm, &nht_pos) == 0) {
245                         RTE_LOG(ERR, TABLE, "%s: NHT full\n", __func__);
246                         return -1;
247                 }
248
249                 nht_entry = &lpm->nht[nht_pos * lpm->entry_size];
250                 memcpy(nht_entry, entry, lpm->entry_size);
251         }
252
253         /* Add rule to low level LPM table */
254         if (rte_lpm6_add(lpm->lpm, ip_prefix->ip, ip_prefix->depth,
255                 (uint8_t) nht_pos) < 0) {
256                 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule add failed\n", __func__);
257                 return -1;
258         }
259
260         /* Commit NHT changes */
261         lpm->nht_users[nht_pos]++;
262         lpm->nht_users[nht_pos0] -= nht_pos0_valid;
263
264         *key_found = nht_pos0_valid;
265         *entry_ptr = (void *) &lpm->nht[nht_pos * lpm->entry_size];
266         return 0;
267 }
268
269 static int
270 rte_table_lpm_ipv6_entry_delete(
271         void *table,
272         void *key,
273         int *key_found,
274         void *entry)
275 {
276         struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
277         struct rte_table_lpm_ipv6_key *ip_prefix =
278                 (struct rte_table_lpm_ipv6_key *) key;
279         uint8_t nht_pos;
280         int status;
281
282         /* Check input parameters */
283         if (lpm == NULL) {
284                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
285                 return -EINVAL;
286         }
287         if (ip_prefix == NULL) {
288                 RTE_LOG(ERR, TABLE, "%s: ip_prefix parameter is NULL\n",
289                         __func__);
290                 return -EINVAL;
291         }
292         if ((ip_prefix->depth == 0) || (ip_prefix->depth > 128)) {
293                 RTE_LOG(ERR, TABLE, "%s: invalid depth (%d)\n", __func__,
294                         ip_prefix->depth);
295                 return -EINVAL;
296         }
297
298         /* Return if rule is not present in the table */
299         status = rte_lpm6_is_rule_present(lpm->lpm, ip_prefix->ip,
300                 ip_prefix->depth, &nht_pos);
301         if (status < 0) {
302                 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 algorithmic error\n",
303                         __func__);
304                 return -1;
305         }
306         if (status == 0) {
307                 *key_found = 0;
308                 return 0;
309         }
310
311         /* Delete rule from the low-level LPM table */
312         status = rte_lpm6_delete(lpm->lpm, ip_prefix->ip, ip_prefix->depth);
313         if (status) {
314                 RTE_LOG(ERR, TABLE, "%s: LPM IPv6 rule delete failed\n",
315                         __func__);
316                 return -1;
317         }
318
319         /* Commit NHT changes */
320         lpm->nht_users[nht_pos]--;
321
322         *key_found = 1;
323         if (entry)
324                 memcpy(entry, &lpm->nht[nht_pos * lpm->entry_size],
325                         lpm->entry_size);
326
327         return 0;
328 }
329
330 static int
331 rte_table_lpm_ipv6_lookup(
332         void *table,
333         struct rte_mbuf **pkts,
334         uint64_t pkts_mask,
335         uint64_t *lookup_hit_mask,
336         void **entries)
337 {
338         struct rte_table_lpm_ipv6 *lpm = (struct rte_table_lpm_ipv6 *) table;
339         uint64_t pkts_out_mask = 0;
340         uint32_t i;
341
342         __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
343         RTE_TABLE_LPM_IPV6_STATS_PKTS_IN_ADD(lpm, n_pkts_in);
344
345         pkts_out_mask = 0;
346         for (i = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
347                 __builtin_clzll(pkts_mask)); i++) {
348                 uint64_t pkt_mask = 1LLU << i;
349
350                 if (pkt_mask & pkts_mask) {
351                         struct rte_mbuf *pkt = pkts[i];
352                         uint8_t *ip = RTE_MBUF_METADATA_UINT8_PTR(pkt,
353                                 lpm->offset);
354                         int status;
355                         uint8_t nht_pos;
356
357                         status = rte_lpm6_lookup(lpm->lpm, ip, &nht_pos);
358                         if (status == 0) {
359                                 pkts_out_mask |= pkt_mask;
360                                 entries[i] = (void *) &lpm->nht[nht_pos *
361                                         lpm->entry_size];
362                         }
363                 }
364         }
365
366         *lookup_hit_mask = pkts_out_mask;
367         RTE_TABLE_LPM_IPV6_STATS_PKTS_LOOKUP_MISS(lpm, n_pkts_in - __builtin_popcountll(pkts_out_mask));
368         return 0;
369 }
370
371 static int
372 rte_table_lpm_ipv6_stats_read(void *table, struct rte_table_stats *stats, int clear)
373 {
374         struct rte_table_lpm_ipv6 *t = (struct rte_table_lpm_ipv6 *) table;
375
376         if (stats != NULL)
377                 memcpy(stats, &t->stats, sizeof(t->stats));
378
379         if (clear)
380                 memset(&t->stats, 0, sizeof(t->stats));
381
382         return 0;
383 }
384
385 struct rte_table_ops rte_table_lpm_ipv6_ops = {
386         .f_create = rte_table_lpm_ipv6_create,
387         .f_free = rte_table_lpm_ipv6_free,
388         .f_add = rte_table_lpm_ipv6_entry_add,
389         .f_delete = rte_table_lpm_ipv6_entry_delete,
390         .f_lookup = rte_table_lpm_ipv6_lookup,
391         .f_stats = rte_table_lpm_ipv6_stats_read,
392 };