update copyright date to 2013
[dpdk.git] / lib / librte_lpm / rte_lpm.c
index 4269b3c..95e1546 100644 (file)
@@ -1,7 +1,7 @@
 /*-
  *   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 
@@ -30,7 +30,6 @@
  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  * 
- *  version: DPDK.L.1.2.3-3
  */
 
 #include <string.h>
 #include <rte_memzone.h>
 #include <rte_tailq.h>
 #include <rte_eal.h>
+#include <rte_eal_memconfig.h>
 #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"
 
 TAILQ_HEAD(rte_lpm_list, rte_lpm);
-
-/* global list of ring (used for debug/dump) */
-static struct rte_lpm_list *lpm_list;
-
-#define CHECK_LPM_LIST_CREATED() do { \
-       if (lpm_list == NULL) \
-               if ((lpm_list = RTE_TAILQ_RESERVE("RTE_LPM", rte_lpm_list)) == NULL){ \
-                       rte_errno = E_RTE_NO_TAILQ; \
-               return NULL; \
-       } \
-} while (0)
-
 #define MAX_DEPTH_TBL24 24
 
 enum valid_flag {
@@ -78,17 +69,17 @@ enum valid_flag {
 /* Macro to enable/disable run-time checks. */
 #if defined(RTE_LIBRTE_LPM_DEBUG)
 #include <rte_debug.h>
-#define VERIFY_DEPTH(depth) do {                                                  \
-       if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH))                          \
-               rte_panic("LPM: Invalid depth (%u) at line %d", depth, __LINE__); \
+#define VERIFY_DEPTH(depth) do {                                \
+       if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH))        \
+               rte_panic("LPM: Invalid depth (%u) at line %d", \
+                               (unsigned)(depth), __LINE__);   \
 } while (0)
 #else
 #define VERIFY_DEPTH(depth)
 #endif
 
 /*
- * Function Name: depth_to_mask
- * Usage       : Converts a given depth value to its corresponding mask value.
+ * Converts a given depth value to its corresponding mask value.
  *
  * depth  (IN)         : range = 1 - 32
  * mask   (OUT)                : 32bit mask
@@ -105,11 +96,7 @@ depth_to_mask(uint8_t depth)
 }
 
 /*
- * Function Name: depth_to_range
- * Usage       : Converts given depth value to its corresponding range value.
- *
- * (IN)  depth
- * (OUT) mask
+ * Converts given depth value to its corresponding range value.
  */
 static inline uint32_t __attribute__((pure))
 depth_to_range(uint8_t depth)
@@ -133,14 +120,20 @@ struct rte_lpm *
 rte_lpm_find_existing(const char *name)
 {
        struct rte_lpm *l;
+       struct rte_lpm_list *lpm_list;
 
        /* check that we have an initialised tail queue */
-       CHECK_LPM_LIST_CREATED();
+       if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
+               rte_errno = E_RTE_NO_TAILQ;
+               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;
@@ -149,35 +142,29 @@ rte_lpm_find_existing(const char *name)
 }
 
 /*
- * Function Name       : rte_lpm_create
- * Usage               : Allocates memory for LPM object
- *
- * rte_lpm (RETURN)
+ * Allocates memory for LPM object
  */
 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;
        uint32_t mem_size;
-
-       /* check that we have access to create things in shared memory. */
-       if (rte_eal_process_type() == RTE_PROC_SECONDARY){
-               rte_errno = E_RTE_SECONDARY;
-               return NULL;
-       }
+       struct rte_lpm_list *lpm_list;
 
        /* check that we have an initialised tail queue */
-       CHECK_LPM_LIST_CREATED();
+       if ((lpm_list = 
+            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
+               rte_errno = E_RTE_NO_TAILQ;
+               return NULL;    
+       }
 
        RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
        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;
        }
@@ -196,44 +183,38 @@ 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);
 
-       /* 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;
+       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;
        }
-       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;
-               }
+       if (lpm != NULL)
+               goto exit;
+
+       /* Allocate memory to store the LPM data structures. */
+       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;
 }
 
 /*
- * Function Name       : free
- * Usage: Deallocates memory for given LPM table.
+ * Deallocates memory for given LPM table.
  */
 void
 rte_lpm_free(struct rte_lpm *lpm)
@@ -242,16 +223,12 @@ 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){
-               TAILQ_REMOVE(lpm_list, lpm, next);
-               rte_free(lpm);
-       }
+       RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
+       rte_free(lpm);
 }
 
 /*
- * Function Name: rule_add
- * Usage       : Adds a rule to the rule table.
+ * Adds a rule to the rule table.
  *
  * NOTE: The rule table is split into 32 groups. Each group contains rules that
  * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
@@ -274,9 +251,9 @@ rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
        rule_index = rule_gindex;
        /* Last rule = Last used rule in this rule group. */
        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) {
@@ -304,8 +281,7 @@ rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 }
 
 /*
- * Function Name: rule_delete
- * Usage       : Delete a rule from the rule table.
+ * Delete a rule from the rule table.
  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
  */
 static inline void
@@ -328,8 +304,7 @@ rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
 
 
 /*
- * Function Name: rule_find
- * Usage       : Finds a rule in rule table.
+ * Finds a rule in rule table.
  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
  */
 static inline int32_t
@@ -354,8 +329,7 @@ rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
 }
 
 /*
- * Function Name: tbl8_alloc
- * Usage       : Find, clean and allocate a tbl8.
+ * Find, clean and allocate a tbl8.
  */
 static inline int32_t
 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
@@ -411,10 +385,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
@@ -426,7 +400,7 @@ add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
 
                /* If tbl24 entry is valid and extended calculate the index
                 * into tbl8. */
-               tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex *
+               tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex * 
                                RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
                tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
 
@@ -493,10 +467,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;
@@ -544,10 +518,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;
@@ -586,25 +560,21 @@ add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
 }
 
 /*
- * Function Name       : rte_lpm_add
- * Usage               : Add a route
- *
- *(IN) lpm_handle,
- *(IN) ip
- *(IN) depth
- *(IN) next_hop
+ * Add a route
  */
 int
 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
                uint8_t next_hop)
 {
        int32_t rule_index, status = 0;
-       uint32_t ip_masked = (ip & depth_to_mask(depth));
+       uint32_t ip_masked;
 
        /* Check user arguments. */
        if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
                return -EINVAL;
 
+       ip_masked = ip & depth_to_mask(depth);
+
        /* Add the rule to the rule table. */
        rule_index = rule_add(lpm, ip_masked, depth, next_hop);
 
@@ -673,6 +643,7 @@ delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
                 * associated with this rule.
                 */
                for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
+                       
                        if (lpm->tbl24[i].ext_entry == 0 &&
                                        lpm->tbl24[i].depth <= depth ) {
                                lpm->tbl24[i].valid = INVALID;
@@ -683,6 +654,7 @@ delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
                                 * to be a rule with depth >= 25 in the
                                 * associated TBL8 group.
                                 */
+
                                tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
                                tbl8_index = tbl8_group_index *
                                                RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
@@ -707,10 +679,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 = {
@@ -736,7 +708,7 @@ delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
                                tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
                                tbl8_index = tbl8_group_index *
                                                RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
-
+                               
                                for (j = tbl8_index; j < (tbl8_index +
                                        RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
 
@@ -751,8 +723,7 @@ delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
 }
 
 /*
- * Function Name: tbl8_recycle_check
- * Usage       : Checks if table 8 group can be recycled.
+ * Checks if table 8 group can be recycled.
  *
  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
  * Return of -EINVAL means tbl8 is empty and thus can be recycled
@@ -873,10 +844,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. */
@@ -888,12 +859,7 @@ delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
 }
 
 /*
- * Function Name: rte_lpm_delete
- * Usage       : Deletes a rule
- *
- *(IN) lpm_handle,
- *(IN) ip
- *(IN) depth
+ * Deletes a rule
  */
 int
 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
@@ -947,10 +913,7 @@ rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
 }
 
 /*
- * Function Name: rte_lpm_delete_all
- * Usage       : Delete all rules from the LPM table.
- *
- *(IN) lpm_handle
+ * Delete all rules from the LPM table.
  */
 void
 rte_lpm_delete_all(struct rte_lpm *lpm)