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