drivers: remove blank line at EOF
[dpdk.git] / lib / librte_lpm / rte_lpm.c
index ea4d234..6b7b28a 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
  */
 
 #include <string.h>
@@ -36,7 +7,6 @@
 #include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include <errno.h>
 #include <sys/queue.h>
 
 #include <rte_log.h>
@@ -44,7 +14,6 @@
 #include <rte_common.h>
 #include <rte_memory.h>        /* for definition of RTE_CACHE_LINE_SIZE */
 #include <rte_malloc.h>
-#include <rte_memzone.h>
 #include <rte_eal.h>
 #include <rte_eal_memconfig.h>
 #include <rte_per_lcore.h>
@@ -130,7 +99,7 @@ rte_lpm_find_existing_v20(const char *name)
 
        rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
        TAILQ_FOREACH(te, lpm_list, next) {
-               l = (struct rte_lpm_v20 *) te->data;
+               l = te->data;
                if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
@@ -156,7 +125,7 @@ rte_lpm_find_existing_v1604(const char *name)
 
        rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
        TAILQ_FOREACH(te, lpm_list, next) {
-               l = (struct rte_lpm *) te->data;
+               l = te->data;
                if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
@@ -205,34 +174,40 @@ rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
 
        /* guarantee there's no existing */
        TAILQ_FOREACH(te, lpm_list, next) {
-               lpm = (struct rte_lpm_v20 *) te->data;
+               lpm = te->data;
                if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
-       if (te != NULL)
+
+       if (te != NULL) {
+               lpm = NULL;
+               rte_errno = EEXIST;
                goto exit;
+       }
 
        /* allocate tailq entry */
        te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
        if (te == NULL) {
                RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
+               rte_errno = ENOMEM;
                goto exit;
        }
 
        /* Allocate memory to store the LPM data structures. */
-       lpm = (struct rte_lpm_v20 *)rte_zmalloc_socket(mem_name, mem_size,
+       lpm = rte_zmalloc_socket(mem_name, mem_size,
                        RTE_CACHE_LINE_SIZE, socket_id);
        if (lpm == NULL) {
                RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
                rte_free(te);
+               rte_errno = ENOMEM;
                goto exit;
        }
 
        /* Save user arguments. */
        lpm->max_rules = max_rules;
-       snprintf(lpm->name, sizeof(lpm->name), "%s", name);
+       strlcpy(lpm->name, name, sizeof(lpm->name));
 
-       te->data = (void *) lpm;
+       te->data = lpm;
 
        TAILQ_INSERT_TAIL(lpm_list, te, next);
 
@@ -244,13 +219,13 @@ exit:
 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
 
 struct rte_lpm *
-rte_lpm_create_v1604(const char *name, int socket_id, int max_rules,
-               __rte_unused int flags)
+rte_lpm_create_v1604(const char *name, int socket_id,
+               const struct rte_lpm_config *config)
 {
        char mem_name[RTE_LPM_NAMESIZE];
        struct rte_lpm *lpm = NULL;
        struct rte_tailq_entry *te;
-       uint32_t mem_size;
+       uint32_t mem_size, rules_size, tbl8s_size;
        struct rte_lpm_list *lpm_list;
 
        lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
@@ -258,7 +233,8 @@ rte_lpm_create_v1604(const char *name, int socket_id, int max_rules,
        RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
 
        /* Check user arguments. */
-       if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
+       if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
+                       || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
                rte_errno = EINVAL;
                return NULL;
        }
@@ -266,40 +242,75 @@ rte_lpm_create_v1604(const char *name, int socket_id, int max_rules,
        snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
 
        /* Determine the amount of memory to allocate. */
-       mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
+       mem_size = sizeof(*lpm);
+       rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
+       tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
+                       RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
 
        rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 
        /* guarantee there's no existing */
        TAILQ_FOREACH(te, lpm_list, next) {
-               lpm = (struct rte_lpm *) te->data;
+               lpm = te->data;
                if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
-       if (te != NULL)
+
+       if (te != NULL) {
+               lpm = NULL;
+               rte_errno = EEXIST;
                goto exit;
+       }
 
        /* allocate tailq entry */
        te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
        if (te == NULL) {
                RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
+               rte_errno = ENOMEM;
                goto exit;
        }
 
        /* Allocate memory to store the LPM data structures. */
-       lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
+       lpm = rte_zmalloc_socket(mem_name, mem_size,
                        RTE_CACHE_LINE_SIZE, socket_id);
        if (lpm == NULL) {
                RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
                rte_free(te);
+               rte_errno = ENOMEM;
+               goto exit;
+       }
+
+       lpm->rules_tbl = rte_zmalloc_socket(NULL,
+                       (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
+
+       if (lpm->rules_tbl == NULL) {
+               RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
+               rte_free(lpm);
+               lpm = NULL;
+               rte_free(te);
+               rte_errno = ENOMEM;
+               goto exit;
+       }
+
+       lpm->tbl8 = rte_zmalloc_socket(NULL,
+                       (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
+
+       if (lpm->tbl8 == NULL) {
+               RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
+               rte_free(lpm->rules_tbl);
+               rte_free(lpm);
+               lpm = NULL;
+               rte_free(te);
+               rte_errno = ENOMEM;
                goto exit;
        }
 
        /* Save user arguments. */
-       lpm->max_rules = max_rules;
-       snprintf(lpm->name, sizeof(lpm->name), "%s", name);
+       lpm->max_rules = config->max_rules;
+       lpm->number_tbl8s = config->number_tbl8s;
+       strlcpy(lpm->name, name, sizeof(lpm->name));
 
-       te->data = (void *) lpm;
+       te->data = lpm;
 
        TAILQ_INSERT_TAIL(lpm_list, te, next);
 
@@ -311,7 +322,7 @@ exit:
 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
 MAP_STATIC_SYMBOL(
        struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
-                       int max_rules, int flags), rte_lpm_create_v1604);
+                       const struct rte_lpm_config *config), rte_lpm_create_v1604);
 
 /*
  * Deallocates memory for given LPM table.
@@ -335,12 +346,8 @@ rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
                if (te->data == (void *) lpm)
                        break;
        }
-       if (te == NULL) {
-               rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
-               return;
-       }
-
-       TAILQ_REMOVE(lpm_list, te, next);
+       if (te != NULL)
+               TAILQ_REMOVE(lpm_list, te, next);
 
        rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
@@ -368,15 +375,13 @@ rte_lpm_free_v1604(struct rte_lpm *lpm)
                if (te->data == (void *) lpm)
                        break;
        }
-       if (te == NULL) {
-               rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
-               return;
-       }
-
-       TAILQ_REMOVE(lpm_list, te, next);
+       if (te != NULL)
+               TAILQ_REMOVE(lpm_list, te, next);
 
        rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
+       rte_free(lpm->tbl8);
+       rte_free(lpm->rules_tbl);
        rte_free(lpm);
        rte_free(te);
 }
@@ -665,14 +670,13 @@ tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
 }
 
 static inline int32_t
-tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8)
+tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
 {
        uint32_t group_idx; /* tbl8 group index. */
        struct rte_lpm_tbl_entry *tbl8_entry;
 
        /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
-       for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
-                       group_idx++) {
+       for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
                tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
                /* If a free tbl8 group is found clean it and set as VALID. */
                if (!tbl8_entry->valid_group) {
@@ -724,11 +728,11 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
                                lpm->tbl24[i].depth <= depth)) {
 
                        struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
-                               { .next_hop = next_hop, },
                                .valid = VALID,
                                .valid_group = 0,
                                .depth = depth,
                        };
+                       new_tbl24_entry.next_hop = next_hop;
 
                        /* Setting tbl24 entry in one go to avoid race
                         * conditions
@@ -755,8 +759,8 @@ add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
                                                .valid = VALID,
                                                .valid_group = VALID,
                                                .depth = depth,
-                                               .next_hop = next_hop,
                                        };
+                                       new_tbl8_entry.next_hop = next_hop;
 
                                        /*
                                         * Setting tbl8 entry in one go to avoid
@@ -881,7 +885,7 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
-                       { .group_idx = (uint8_t)tbl8_group_index, },
+                       .group_idx = (uint8_t)tbl8_group_index,
                        .valid = VALID,
                        .valid_group = 1,
                        .depth = 0,
@@ -915,14 +919,9 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
 
                /* Insert new rule into the tbl8 entry. */
                for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
-                       if (!lpm->tbl8[i].valid ||
-                                       lpm->tbl8[i].depth <= depth) {
-                               lpm->tbl8[i].valid = VALID;
-                               lpm->tbl8[i].depth = depth;
-                               lpm->tbl8[i].next_hop = next_hop;
-
-                               continue;
-                       }
+                       lpm->tbl8[i].valid = VALID;
+                       lpm->tbl8[i].depth = depth;
+                       lpm->tbl8[i].next_hop = next_hop;
                }
 
                /*
@@ -932,7 +931,7 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
-                               { .group_idx = (uint8_t)tbl8_group_index, },
+                               .group_idx = (uint8_t)tbl8_group_index,
                                .valid = VALID,
                                .valid_group = 1,
                                .depth = 0,
@@ -955,10 +954,9 @@ add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
                                struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
                                        .valid = VALID,
                                        .depth = depth,
-                                       .next_hop = next_hop,
                                        .valid_group = lpm->tbl8[i].valid_group,
                                };
-
+                               new_tbl8_entry.next_hop = next_hop;
                                /*
                                 * Setting tbl8 entry in one go to avoid race
                                 * condition
@@ -987,7 +985,7 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 
        if (!lpm->tbl24[tbl24_index].valid) {
                /* Search for a free tbl8 group. */
-               tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8);
+               tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
 
                /* Check tbl8 allocation was successful. */
                if (tbl8_group_index < 0) {
@@ -1013,7 +1011,7 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl_entry new_tbl24_entry = {
-                       .group_idx = (uint8_t)tbl8_group_index,
+                       .group_idx = tbl8_group_index,
                        .valid = VALID,
                        .valid_group = 1,
                        .depth = 0,
@@ -1024,7 +1022,7 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
        } /* If valid entry but not extended calculate the index into Table8. */
        else if (lpm->tbl24[tbl24_index].valid_group == 0) {
                /* Search for free tbl8 group. */
-               tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8);
+               tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
 
                if (tbl8_group_index < 0) {
                        return tbl8_group_index;
@@ -1047,14 +1045,9 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 
                /* Insert new rule into the tbl8 entry. */
                for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
-                       if (!lpm->tbl8[i].valid ||
-                                       lpm->tbl8[i].depth <= depth) {
-                               lpm->tbl8[i].valid = VALID;
-                               lpm->tbl8[i].depth = depth;
-                               lpm->tbl8[i].next_hop = next_hop;
-
-                               continue;
-                       }
+                       lpm->tbl8[i].valid = VALID;
+                       lpm->tbl8[i].depth = depth;
+                       lpm->tbl8[i].next_hop = next_hop;
                }
 
                /*
@@ -1064,7 +1057,7 @@ add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl_entry new_tbl24_entry = {
-                               .group_idx = (uint8_t)tbl8_group_index,
+                               .group_idx = tbl8_group_index,
                                .valid = VALID,
                                .valid_group = 1,
                                .depth = 0,
@@ -1345,7 +1338,7 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
                 */
 
                struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
-                       {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
+                       .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
                        .valid = VALID,
                        .valid_group = 0,
                        .depth = sub_rule_depth,
@@ -1355,9 +1348,9 @@ delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
                        .valid = VALID,
                        .valid_group = VALID,
                        .depth = sub_rule_depth,
-                       .next_hop = lpm->rules_tbl
-                       [sub_rule_index].next_hop,
                };
+               new_tbl8_entry.next_hop =
+                               lpm->rules_tbl[sub_rule_index].next_hop;
 
                for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
 
@@ -1509,7 +1502,7 @@ tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
                 * and if so check the rest of the entries to verify that they
                 * are all of this depth.
                 */
-               if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
+               if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
                        for (i = (tbl8_group_start + 1); i < tbl8_group_end;
                                        i++) {
 
@@ -1556,7 +1549,7 @@ tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
                 * and if so check the rest of the entries to verify that they
                 * are all of this depth.
                 */
-               if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
+               if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
                        for (i = (tbl8_group_start + 1); i < tbl8_group_end;
                                        i++) {
 
@@ -1619,9 +1612,10 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
                        .valid = VALID,
                        .depth = sub_rule_depth,
                        .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
-                       .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
                };
 
+               new_tbl8_entry.next_hop =
+                               lpm->rules_tbl[sub_rule_index].next_hop;
                /*
                 * Loop through the range of entries on tbl8 for which the
                 * rule_to_delete must be modified.
@@ -1647,7 +1641,7 @@ delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
        } else if (tbl8_recycle_index > -1) {
                /* Update tbl24 entry. */
                struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
-                       { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
+                       .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
                        .valid = VALID,
                        .valid_group = 0,
                        .depth = lpm->tbl8[tbl8_recycle_index].depth,
@@ -1882,7 +1876,8 @@ rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
        memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
 
        /* Zero tbl8. */
-       memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
+       memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
+                       * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
 
        /* Delete all rules form the rules table. */
        memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);