lpm6: fix possible garbage in lookup
[dpdk.git] / lib / librte_lpm / rte_lpm6.c
index d72f9b3..32fdba0 100644 (file)
@@ -78,6 +78,11 @@ enum valid_flag {
 
 TAILQ_HEAD(rte_lpm6_list, rte_tailq_entry);
 
+static struct rte_tailq_elem rte_lpm6_tailq = {
+       .name = "RTE_LPM6",
+};
+EAL_REGISTER_TAILQ(rte_lpm6_tailq)
+
 /** Tbl entry structure. It is the same for both tbl24 and tbl8 */
 struct rte_lpm6_tbl_entry {
        uint32_t next_hop:      21;  /**< Next hop / next table to be checked. */
@@ -150,12 +155,7 @@ rte_lpm6_create(const char *name, int socket_id,
        uint64_t mem_size, rules_size;
        struct rte_lpm6_list *lpm_list;
 
-       /* Check that we have an initialised tail queue */
-       if ((lpm_list =
-            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM6, rte_lpm6_list)) == NULL) {
-               rte_errno = E_RTE_NO_TAILQ;
-               return NULL;
-       }
+       lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
 
        RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_tbl_entry) != sizeof(uint32_t));
 
@@ -182,8 +182,11 @@ rte_lpm6_create(const char *name, int socket_id,
                if (strncmp(name, lpm->name, RTE_LPM6_NAMESIZE) == 0)
                        break;
        }
-       if (te != NULL)
+       lpm = NULL;
+       if (te != NULL) {
+               rte_errno = EEXIST;
                goto exit;
+       }
 
        /* allocate tailq entry */
        te = rte_zmalloc("LPM6_TAILQ_ENTRY", sizeof(*te), 0);
@@ -206,8 +209,9 @@ rte_lpm6_create(const char *name, int socket_id,
                        (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
 
        if (lpm->rules_tbl == NULL) {
-               RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
+               RTE_LOG(ERR, LPM, "LPM rules_tbl allocation failed\n");
                rte_free(lpm);
+               lpm = NULL;
                rte_free(te);
                goto exit;
        }
@@ -237,12 +241,7 @@ rte_lpm6_find_existing(const char *name)
        struct rte_tailq_entry *te;
        struct rte_lpm6_list *lpm_list;
 
-       /* Check that we have an initialised tail queue */
-       if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM6,
-                       rte_lpm6_list)) == NULL) {
-               rte_errno = E_RTE_NO_TAILQ;
-               return NULL;
-       }
+       lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
 
        rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
        TAILQ_FOREACH(te, lpm_list, next) {
@@ -273,12 +272,7 @@ rte_lpm6_free(struct rte_lpm6 *lpm)
        if (lpm == NULL)
                return;
 
-       /* check that we have an initialised tail queue */
-       if ((lpm_list =
-            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm6_list)) == NULL) {
-               rte_errno = E_RTE_NO_TAILQ;
-               return;
-       }
+       lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
 
        rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
 
@@ -287,15 +281,13 @@ rte_lpm6_free(struct rte_lpm6 *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->rules_tbl);
        rte_free(lpm);
        rte_free(te);
 }
@@ -609,7 +601,7 @@ int
 rte_lpm6_lookup(const struct rte_lpm6 *lpm, uint8_t *ip, uint8_t *next_hop)
 {
        const struct rte_lpm6_tbl_entry *tbl;
-       const struct rte_lpm6_tbl_entry *tbl_next;
+       const struct rte_lpm6_tbl_entry *tbl_next = NULL;
        int status;
        uint8_t first_byte;
        uint32_t tbl24_index;
@@ -644,7 +636,7 @@ rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
 {
        unsigned i;
        const struct rte_lpm6_tbl_entry *tbl;
-       const struct rte_lpm6_tbl_entry *tbl_next;
+       const struct rte_lpm6_tbl_entry *tbl_next = NULL;
        uint32_t tbl24_index;
        uint8_t first_byte, next_hop;
        int status;