lib: fix cache alignment of structures
[dpdk.git] / lib / librte_table / rte_table_acl.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_log.h>
42
43 #include "rte_table_acl.h"
44 #include <rte_ether.h>
45
46 struct rte_table_acl {
47         /* Low-level ACL table */
48         char name[2][RTE_ACL_NAMESIZE];
49         struct rte_acl_param acl_params; /* for creating low level acl table */
50         struct rte_acl_config cfg; /* Holds the field definitions (metadata) */
51         struct rte_acl_ctx *ctx;
52         uint32_t name_id;
53
54         /* Input parameters */
55         uint32_t n_rules;
56         uint32_t entry_size;
57
58         /* Internal tables */
59         uint8_t *action_table;
60         struct rte_acl_rule **acl_rule_list; /* Array of pointers to rules */
61         uint8_t *acl_rule_memory; /* Memory to store the rules */
62
63         /* Memory to store the action table and stack of free entries */
64         uint8_t memory[0] __rte_cache_aligned;
65 };
66
67
68 static void *
69 rte_table_acl_create(
70         void *params,
71         int socket_id,
72         uint32_t entry_size)
73 {
74         struct rte_table_acl_params *p = (struct rte_table_acl_params *) params;
75         struct rte_table_acl *acl;
76         uint32_t action_table_size, acl_rule_list_size, acl_rule_memory_size;
77         uint32_t total_size;
78
79         RTE_BUILD_BUG_ON(((sizeof(struct rte_table_acl) % RTE_CACHE_LINE_SIZE)
80                 != 0));
81
82         /* Check input parameters */
83         if (p == NULL) {
84                 RTE_LOG(ERR, TABLE, "%s: Invalid value for params\n", __func__);
85                 return NULL;
86         }
87         if (p->name == NULL) {
88                 RTE_LOG(ERR, TABLE, "%s: Invalid value for name\n", __func__);
89                 return NULL;
90         }
91         if (p->n_rules == 0) {
92                 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rules\n",
93                         __func__);
94                 return NULL;
95         }
96         if ((p->n_rule_fields == 0) ||
97             (p->n_rule_fields > RTE_ACL_MAX_FIELDS)) {
98                 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rule_fields\n",
99                         __func__);
100                 return NULL;
101         }
102
103         entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
104
105         /* Memory allocation */
106         action_table_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules * entry_size);
107         acl_rule_list_size =
108                 RTE_CACHE_LINE_ROUNDUP(p->n_rules * sizeof(struct rte_acl_rule *));
109         acl_rule_memory_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules *
110                 RTE_ACL_RULE_SZ(p->n_rule_fields));
111         total_size = sizeof(struct rte_table_acl) + action_table_size +
112                 acl_rule_list_size + acl_rule_memory_size;
113
114         acl = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
115                 socket_id);
116         if (acl == NULL) {
117                 RTE_LOG(ERR, TABLE,
118                         "%s: Cannot allocate %u bytes for ACL table\n",
119                         __func__, total_size);
120                 return NULL;
121         }
122
123         acl->action_table = &acl->memory[0];
124         acl->acl_rule_list =
125                 (struct rte_acl_rule **) &acl->memory[action_table_size];
126         acl->acl_rule_memory = (uint8_t *)
127                 &acl->memory[action_table_size + acl_rule_list_size];
128
129         /* Initialization of internal fields */
130         snprintf(acl->name[0], RTE_ACL_NAMESIZE, "%s_a", p->name);
131         snprintf(acl->name[1], RTE_ACL_NAMESIZE, "%s_b", p->name);
132         acl->name_id = 1;
133
134         acl->acl_params.name = acl->name[acl->name_id];
135         acl->acl_params.socket_id = socket_id;
136         acl->acl_params.rule_size = RTE_ACL_RULE_SZ(p->n_rule_fields);
137         acl->acl_params.max_rule_num = p->n_rules;
138
139         acl->cfg.num_categories = 1;
140         acl->cfg.num_fields = p->n_rule_fields;
141         memcpy(&acl->cfg.defs[0], &p->field_format[0],
142                 p->n_rule_fields * sizeof(struct rte_acl_field_def));
143
144         acl->ctx = NULL;
145
146         acl->n_rules = p->n_rules;
147         acl->entry_size = entry_size;
148
149         return acl;
150 }
151
152 static int
153 rte_table_acl_free(void *table)
154 {
155         struct rte_table_acl *acl = (struct rte_table_acl *) table;
156
157         /* Check input parameters */
158         if (table == NULL) {
159                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
160                 return -EINVAL;
161         }
162
163         /* Free previously allocated resources */
164         if (acl->ctx != NULL)
165                 rte_acl_free(acl->ctx);
166
167         rte_free(acl);
168
169         return 0;
170 }
171
172 RTE_ACL_RULE_DEF(rte_pipeline_acl_rule, RTE_ACL_MAX_FIELDS);
173
174 static int
175 rte_table_acl_build(struct rte_table_acl *acl, struct rte_acl_ctx **acl_ctx)
176 {
177         struct rte_acl_ctx *ctx = NULL;
178         uint32_t n_rules, i;
179         int status;
180
181         /* Create low level ACL table */
182         ctx = rte_acl_create(&acl->acl_params);
183         if (ctx == NULL) {
184                 RTE_LOG(ERR, TABLE, "%s: Cannot create low level ACL table\n",
185                         __func__);
186                 return -1;
187         }
188
189         /* Add rules to low level ACL table */
190         n_rules = 0;
191         for (i = 1; i < acl->n_rules; i++) {
192                 if (acl->acl_rule_list[i] != NULL) {
193                         status = rte_acl_add_rules(ctx, acl->acl_rule_list[i],
194                                 1);
195                         if (status != 0) {
196                                 RTE_LOG(ERR, TABLE,
197                                 "%s: Cannot add rule to low level ACL table\n",
198                                         __func__);
199                                 rte_acl_free(ctx);
200                                 return -1;
201                         }
202
203                         n_rules++;
204                 }
205         }
206
207         if (n_rules == 0) {
208                 rte_acl_free(ctx);
209                 *acl_ctx = NULL;
210                 return 0;
211         }
212
213         /* Build low level ACl table */
214         status = rte_acl_build(ctx, &acl->cfg);
215         if (status != 0) {
216                 RTE_LOG(ERR, TABLE,
217                         "%s: Cannot build the low level ACL table\n",
218                         __func__);
219                 rte_acl_free(ctx);
220                 return -1;
221         }
222
223         rte_acl_dump(ctx);
224
225         *acl_ctx = ctx;
226         return 0;
227 }
228
229 static int
230 rte_table_acl_entry_add(
231         void *table,
232         void *key,
233         void *entry,
234         int *key_found,
235         void **entry_ptr)
236 {
237         struct rte_table_acl *acl = (struct rte_table_acl *) table;
238         struct rte_table_acl_rule_add_params *rule =
239                 (struct rte_table_acl_rule_add_params *) key;
240         struct rte_pipeline_acl_rule acl_rule;
241         struct rte_acl_rule *rule_location;
242         struct rte_acl_ctx *ctx;
243         uint32_t free_pos, free_pos_valid, i;
244         int status;
245
246         /* Check input parameters */
247         if (table == NULL) {
248                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
249                 return -EINVAL;
250         }
251         if (key == NULL) {
252                 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
253                 return -EINVAL;
254         }
255         if (entry == NULL) {
256                 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
257                 return -EINVAL;
258         }
259         if (key_found == NULL) {
260                 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
261                         __func__);
262                 return -EINVAL;
263         }
264         if (entry_ptr == NULL) {
265                 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
266                         __func__);
267                 return -EINVAL;
268         }
269         if (rule->priority > RTE_ACL_MAX_PRIORITY) {
270                 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
271                 return -EINVAL;
272         }
273
274         /* Setup rule data structure */
275         memset(&acl_rule, 0, sizeof(acl_rule));
276         acl_rule.data.category_mask = 1;
277         acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
278         acl_rule.data.userdata = 0; /* To be set up later */
279         memcpy(&acl_rule.field[0],
280                 &rule->field_value[0],
281                 acl->cfg.num_fields * sizeof(struct rte_acl_field));
282
283         /* Look to see if the rule exists already in the table */
284         free_pos = 0;
285         free_pos_valid = 0;
286         for (i = 1; i < acl->n_rules; i++) {
287                 if (acl->acl_rule_list[i] == NULL) {
288                         if (free_pos_valid == 0) {
289                                 free_pos = i;
290                                 free_pos_valid = 1;
291                         }
292
293                         continue;
294                 }
295
296                 /* Compare the key fields */
297                 status = memcmp(&acl->acl_rule_list[i]->field[0],
298                         &rule->field_value[0],
299                         acl->cfg.num_fields * sizeof(struct rte_acl_field));
300
301                 /* Rule found: update data associated with the rule */
302                 if (status == 0) {
303                         *key_found = 1;
304                         *entry_ptr = &acl->memory[i * acl->entry_size];
305                         memcpy(*entry_ptr, entry, acl->entry_size);
306
307                         return 0;
308                 }
309         }
310
311         /* Return if max rules */
312         if (free_pos_valid == 0) {
313                 RTE_LOG(ERR, TABLE, "%s: Max number of rules reached\n",
314                         __func__);
315                 return -ENOSPC;
316         }
317
318         /* Add the new rule to the rule set */
319         acl_rule.data.userdata = free_pos;
320         rule_location = (struct rte_acl_rule *)
321                 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
322         memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
323         acl->acl_rule_list[free_pos] = rule_location;
324
325         /* Build low level ACL table */
326         acl->name_id ^= 1;
327         acl->acl_params.name = acl->name[acl->name_id];
328         status = rte_table_acl_build(acl, &ctx);
329         if (status != 0) {
330                 /* Roll back changes */
331                 acl->acl_rule_list[free_pos] = NULL;
332                 acl->name_id ^= 1;
333
334                 return -EINVAL;
335         }
336
337         /* Commit changes */
338         if (acl->ctx != NULL)
339                 rte_acl_free(acl->ctx);
340         acl->ctx = ctx;
341         *key_found = 0;
342         *entry_ptr = &acl->memory[free_pos * acl->entry_size];
343         memcpy(*entry_ptr, entry, acl->entry_size);
344
345         return 0;
346 }
347
348 static int
349 rte_table_acl_entry_delete(
350         void *table,
351         void *key,
352         int *key_found,
353         void *entry)
354 {
355         struct rte_table_acl *acl = (struct rte_table_acl *) table;
356         struct rte_table_acl_rule_delete_params *rule =
357                 (struct rte_table_acl_rule_delete_params *) key;
358         struct rte_acl_rule *deleted_rule = NULL;
359         struct rte_acl_ctx *ctx;
360         uint32_t pos, pos_valid, i;
361         int status;
362
363         /* Check input parameters */
364         if (table == NULL) {
365                 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
366                 return -EINVAL;
367         }
368         if (key == NULL) {
369                 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
370                 return -EINVAL;
371         }
372         if (key_found == NULL) {
373                 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
374                         __func__);
375                 return -EINVAL;
376         }
377
378         /* Look for the rule in the table */
379         pos = 0;
380         pos_valid = 0;
381         for (i = 1; i < acl->n_rules; i++) {
382                 if (acl->acl_rule_list[i] != NULL) {
383                         /* Compare the key fields */
384                         status = memcmp(&acl->acl_rule_list[i]->field[0],
385                                 &rule->field_value[0], acl->cfg.num_fields *
386                                 sizeof(struct rte_acl_field));
387
388                         /* Rule found: remove from table */
389                         if (status == 0) {
390                                 pos = i;
391                                 pos_valid = 1;
392
393                                 deleted_rule = acl->acl_rule_list[i];
394                                 acl->acl_rule_list[i] = NULL;
395                         }
396                 }
397         }
398
399         /* Return if rule not found */
400         if (pos_valid == 0) {
401                 *key_found = 0;
402                 return 0;
403         }
404
405         /* Build low level ACL table */
406         acl->name_id ^= 1;
407         acl->acl_params.name = acl->name[acl->name_id];
408         status = rte_table_acl_build(acl, &ctx);
409         if (status != 0) {
410                 /* Roll back changes */
411                 acl->acl_rule_list[pos] = deleted_rule;
412                 acl->name_id ^= 1;
413
414                 return -EINVAL;
415         }
416
417         /* Commit changes */
418         if (acl->ctx != NULL)
419                 rte_acl_free(acl->ctx);
420
421         acl->ctx = ctx;
422         *key_found = 1;
423         if (entry != NULL)
424                 memcpy(entry, &acl->memory[pos * acl->entry_size],
425                         acl->entry_size);
426
427         return 0;
428 }
429
430 static int
431 rte_table_acl_lookup(
432         void *table,
433         struct rte_mbuf **pkts,
434         uint64_t pkts_mask,
435         uint64_t *lookup_hit_mask,
436         void **entries)
437 {
438         struct rte_table_acl *acl = (struct rte_table_acl *) table;
439         const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
440         uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
441         uint64_t pkts_out_mask;
442         uint32_t n_pkts, i, j;
443
444         /* Input conversion */
445         for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
446                 __builtin_clzll(pkts_mask)); i++) {
447                 uint64_t pkt_mask = 1LLU << i;
448
449                 if (pkt_mask & pkts_mask) {
450                         pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
451                         j++;
452                 }
453         }
454         n_pkts = j;
455
456         /* Low-level ACL table lookup */
457         if (acl->ctx != NULL)
458                 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
459         else
460                 n_pkts = 0;
461
462         /* Output conversion */
463         pkts_out_mask = 0;
464         for (i = 0; i < n_pkts; i++) {
465                 uint32_t action_table_pos = results[i];
466                 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
467                 uint64_t pkt_mask = 1LLU << pkt_pos;
468
469                 pkts_mask &= ~pkt_mask;
470
471                 if (action_table_pos != RTE_ACL_INVALID_USERDATA) {
472                         pkts_out_mask |= pkt_mask;
473                         entries[pkt_pos] = (void *)
474                                 &acl->memory[action_table_pos *
475                                 acl->entry_size];
476                         rte_prefetch0(entries[pkt_pos]);
477                 }
478         }
479
480         *lookup_hit_mask = pkts_out_mask;
481
482         return 0;
483 }
484
485 struct rte_table_ops rte_table_acl_ops = {
486         .f_create = rte_table_acl_create,
487         .f_free = rte_table_acl_free,
488         .f_add = rte_table_acl_entry_add,
489         .f_delete = rte_table_acl_entry_delete,
490         .f_lookup = rte_table_acl_lookup,
491 };