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