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