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