doc: whitespace changes in licenses
[dpdk.git] / lib / librte_lpm / rte_lpm.c
index ab04993..b120ec9 100644 (file)
@@ -1,35 +1,34 @@
 /*-
  *   BSD LICENSE
  * 
- *   Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
+ *   Copyright(c) 2010-2013 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 
+ *   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 
+ *     * 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 
+ *     * 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 
+ *     * 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 
+ *   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.
- * 
  */
 
 #include <string.h>
@@ -52,6 +51,8 @@
 #include <rte_per_lcore.h>
 #include <rte_string_fns.h>
 #include <rte_errno.h>
+#include <rte_rwlock.h>
+#include <rte_spinlock.h>
 
 #include "rte_lpm.h"
 
@@ -126,10 +127,12 @@ rte_lpm_find_existing(const char *name)
                return NULL;
        }
 
+       rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
        TAILQ_FOREACH(l, lpm_list, next) {
                if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
+       rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
 
        if (l == NULL)
                rte_errno = ENOENT;
@@ -142,7 +145,7 @@ rte_lpm_find_existing(const char *name)
  */
 struct rte_lpm *
 rte_lpm_create(const char *name, int socket_id, int max_rules,
-               int mem_location)
+               __rte_unused int flags)
 {
        char mem_name[RTE_LPM_NAMESIZE];
        struct rte_lpm *lpm = NULL;
@@ -160,9 +163,7 @@ rte_lpm_create(const char *name, int socket_id, int max_rules,
        RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
 
        /* Check user arguments. */
-       if ((name == NULL) || (socket_id < -1) || (max_rules == 0) ||
-                       (mem_location != RTE_LPM_HEAP &&
-                                       mem_location != RTE_LPM_MEMZONE)){
+       if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
                rte_errno = EINVAL;
                return NULL;
        }
@@ -181,46 +182,33 @@ rte_lpm_create(const char *name, int socket_id, int max_rules,
        /* Determine the amount of memory to allocate. */
        mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
 
+       rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+
        /* guarantee there's no existing */
        TAILQ_FOREACH(lpm, lpm_list, next) {
                if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
        if (lpm != NULL)
-               return NULL;
+               goto exit;
 
        /* Allocate memory to store the LPM data structures. */
-       if (mem_location == RTE_LPM_MEMZONE) {
-               const struct rte_memzone *mz;
-               uint32_t mz_flags = 0;
-
-               mz = rte_memzone_reserve(mem_name, mem_size, socket_id,
-                               mz_flags);
-               if (mz == NULL) {
-                       RTE_LOG(ERR, LPM, "LPM memzone creation failed\n");
-                       return NULL;
-               }
-
-               memset(mz->addr, 0, mem_size);
-               lpm = (struct rte_lpm *) mz->addr;
-
-       }
-       else {
-               lpm = (struct rte_lpm *)rte_zmalloc(mem_name, mem_size,
-                       CACHE_LINE_SIZE);
-               if (lpm == NULL) {
-                       RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
-                       return NULL;
-               }
+       lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
+                       CACHE_LINE_SIZE, socket_id);
+       if (lpm == NULL) {
+               RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
+               goto exit;
        }
 
        /* Save user arguments. */
        lpm->max_rules_per_depth = max_rules / RTE_LPM_MAX_DEPTH;
        rte_snprintf(lpm->name, sizeof(lpm->name), "%s", name);
-       lpm->mem_location = mem_location;
 
        TAILQ_INSERT_TAIL(lpm_list, lpm, next);
 
+exit:  
+       rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
+
        return lpm;
 }
 
@@ -234,11 +222,8 @@ rte_lpm_free(struct rte_lpm *lpm)
        if (lpm == NULL)
                return;
 
-       /* Note: Its is currently not possible to free a memzone. */
-       if (lpm->mem_location == RTE_LPM_HEAP){
-               RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
-               rte_free(lpm);
-       }
+       RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
+       rte_free(lpm);
 }
 
 /*
@@ -267,7 +252,7 @@ rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
        last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
                
        /* Scan through rule group to see if rule already exists. */
-       for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
+       for (; rule_index < last_rule; rule_index++) {
 
                /* If rule already exists update its next_hop and return. */
                if (lpm->rules_tbl[rule_index].ip == ip_masked) {
@@ -399,10 +384,10 @@ add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
                                lpm->tbl24[i].depth <= depth)) {
 
                        struct rte_lpm_tbl24_entry new_tbl24_entry = {
+                               { .next_hop = next_hop, },
                                .valid = VALID,
                                .ext_entry = 0,
                                .depth = depth,
-                               { .next_hop = next_hop, }
                        };
 
                        /* Setting tbl24 entry in one go to avoid race
@@ -481,10 +466,10 @@ add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl24_entry new_tbl24_entry = {
+                       { .tbl8_gindex = (uint8_t)tbl8_group_index, },
                        .valid = VALID,
                        .ext_entry = 1,
                        .depth = 0,
-                       { .tbl8_gindex = (uint8_t)tbl8_group_index, }
                };
 
                lpm->tbl24[tbl24_index] = new_tbl24_entry;
@@ -532,10 +517,10 @@ add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
                 */
 
                struct rte_lpm_tbl24_entry new_tbl24_entry = {
+                               { .tbl8_gindex = (uint8_t)tbl8_group_index, },
                                .valid = VALID,
                                .ext_entry = 1,
                                .depth = 0,
-                               { .tbl8_gindex = (uint8_t)tbl8_group_index, }
                };
 
                lpm->tbl24[tbl24_index] = new_tbl24_entry;
@@ -693,10 +678,10 @@ delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
                                lpm->max_rules_per_depth);
 
                struct rte_lpm_tbl24_entry new_tbl24_entry = {
+                       {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
                        .valid = VALID,
                        .ext_entry = 0,
                        .depth = new_depth,
-                       {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,}
                };
 
                struct rte_lpm_tbl8_entry new_tbl8_entry = {
@@ -858,10 +843,10 @@ delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
        else if (tbl8_recycle_index > -1) {
                /* Update tbl24 entry. */
                struct rte_lpm_tbl24_entry new_tbl24_entry = {
+                       { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
                        .valid = VALID,
                        .ext_entry = 0,
                        .depth = lpm->tbl8[tbl8_recycle_index].depth,
-                       { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, }
                };
 
                /* Set tbl24 before freeing tbl8 to avoid race condition. */