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