lpm: remove redundant check when adding rule
[dpdk.git] / lib / librte_lpm / rte_lpm.c
index 3dee15c..ec67765 100644 (file)
@@ -209,8 +209,11 @@ rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
                if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
-       if (te != NULL)
+       lpm = NULL;
+       if (te != NULL) {
+               rte_errno = EEXIST;
                goto exit;
+       }
 
        /* allocate tailq entry */
        te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
@@ -280,8 +283,11 @@ rte_lpm_create_v1604(const char *name, int socket_id,
                if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
                        break;
        }
-       if (te != NULL)
+       lpm = NULL;
+       if (te != NULL) {
+               rte_errno = EEXIST;
                goto exit;
+       }
 
        /* allocate tailq entry */
        te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
@@ -303,8 +309,9 @@ rte_lpm_create_v1604(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 memory allocation failed\n");
                rte_free(lpm);
+               lpm = NULL;
                rte_free(te);
                goto exit;
        }
@@ -313,8 +320,9 @@ rte_lpm_create_v1604(const char *name, int socket_id,
                        (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
 
        if (lpm->tbl8 == NULL) {
-               RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
+               RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
                rte_free(lpm);
+               lpm = NULL;
                rte_free(te);
                goto exit;
        }
@@ -365,7 +373,6 @@ rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
 
        rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
-       rte_free(lpm->rules_tbl);
        rte_free(lpm);
        rte_free(te);
 }
@@ -742,11 +749,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
@@ -773,8 +780,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
@@ -933,14 +940,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;
                }
 
                /*
@@ -973,10 +975,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
@@ -1065,14 +1066,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;
                }
 
                /*
@@ -1373,9 +1369,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++) {
 
@@ -1527,7 +1523,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++) {
 
@@ -1574,7 +1570,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++) {
 
@@ -1637,9 +1633,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.