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