4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/queue.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_common.h>
46 #include <rte_memory.h> /* for definition of CACHE_LINE_SIZE */
47 #include <rte_malloc.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_string_fns.h>
54 #include <rte_errno.h>
58 TAILQ_HEAD(rte_lpm_list, rte_lpm);
60 #define MAX_DEPTH_TBL24 24
67 /* Macro to enable/disable run-time checks. */
68 #if defined(RTE_LIBRTE_LPM_DEBUG)
69 #include <rte_debug.h>
70 #define VERIFY_DEPTH(depth) do { \
71 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
72 rte_panic("LPM: Invalid depth (%u) at line %d", \
73 (unsigned)(depth), __LINE__); \
76 #define VERIFY_DEPTH(depth)
80 * Converts a given depth value to its corresponding mask value.
82 * depth (IN) : range = 1 - 32
83 * mask (OUT) : 32bit mask
85 static uint32_t __attribute__((pure))
86 depth_to_mask(uint8_t depth)
90 /* To calculate a mask start with a 1 on the left hand side and right
91 * shift while populating the left hand side with 1's
93 return (int)0x80000000 >> (depth - 1);
97 * Converts given depth value to its corresponding range value.
99 static inline uint32_t __attribute__((pure))
100 depth_to_range(uint8_t depth)
105 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
107 if (depth <= MAX_DEPTH_TBL24)
108 return 1 << (MAX_DEPTH_TBL24 - depth);
110 /* Else if depth is greater than 24 */
111 return (1 << (RTE_LPM_MAX_DEPTH - depth));
115 * Find an existing lpm table and return a pointer to it.
118 rte_lpm_find_existing(const char *name)
121 struct rte_lpm_list *lpm_list;
123 /* check that we have an initialised tail queue */
124 if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
125 rte_errno = E_RTE_NO_TAILQ;
129 TAILQ_FOREACH(l, lpm_list, next) {
130 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
141 * Allocates memory for LPM object
144 rte_lpm_create(const char *name, int socket_id, int max_rules,
145 __rte_unused int flags)
147 char mem_name[RTE_LPM_NAMESIZE];
148 struct rte_lpm *lpm = NULL;
150 struct rte_lpm_list *lpm_list;
152 /* check that we have an initialised tail queue */
154 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
155 rte_errno = E_RTE_NO_TAILQ;
159 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
160 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
162 /* Check user arguments. */
163 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
168 rte_snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
171 * Pad out max_rules so that each depth is given the same number of
174 if (max_rules % RTE_LPM_MAX_DEPTH) {
175 max_rules += RTE_LPM_MAX_DEPTH -
176 (max_rules % RTE_LPM_MAX_DEPTH);
179 /* Determine the amount of memory to allocate. */
180 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
182 /* guarantee there's no existing */
183 TAILQ_FOREACH(lpm, lpm_list, next) {
184 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
190 /* Allocate memory to store the LPM data structures. */
191 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
192 CACHE_LINE_SIZE, socket_id);
194 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
198 /* Save user arguments. */
199 lpm->max_rules_per_depth = max_rules / RTE_LPM_MAX_DEPTH;
200 rte_snprintf(lpm->name, sizeof(lpm->name), "%s", name);
202 TAILQ_INSERT_TAIL(lpm_list, lpm, next);
208 * Deallocates memory for given LPM table.
211 rte_lpm_free(struct rte_lpm *lpm)
213 /* Check user arguments. */
217 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
222 * Adds a rule to the rule table.
224 * NOTE: The rule table is split into 32 groups. Each group contains rules that
225 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
226 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
227 * to refer to depth 1 because even though the depth range is 1 - 32, depths
228 * are stored in the rule table from 0 - 31.
229 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
231 static inline int32_t
232 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
235 uint32_t rule_gindex, rule_index, last_rule;
239 /* rule_gindex stands for rule group index. */
240 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
241 /* Initialise rule_index to point to start of rule group. */
242 rule_index = rule_gindex;
243 /* Last rule = Last used rule in this rule group. */
244 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
246 /* Scan through rule group to see if rule already exists. */
247 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
249 /* If rule already exists update its next_hop and return. */
250 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
251 lpm->rules_tbl[rule_index].next_hop = next_hop;
258 * If rule does not exist check if there is space to add a new rule to
259 * this rule group. If there is no space return error. */
260 if (lpm->used_rules_at_depth[depth - 1] == lpm->max_rules_per_depth) {
264 /* If there is space for the new rule add it. */
265 lpm->rules_tbl[rule_index].ip = ip_masked;
266 lpm->rules_tbl[rule_index].next_hop = next_hop;
268 /* Increment the used rules counter for this rule group. */
269 lpm->used_rules_at_depth[depth - 1]++;
275 * Delete a rule from the rule table.
276 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
279 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
281 uint32_t rule_gindex, last_rule_index;
285 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
286 last_rule_index = rule_gindex +
287 (lpm->used_rules_at_depth[depth - 1]) - 1;
289 * Overwrite redundant rule with last rule in group and decrement rule
292 lpm->rules_tbl[rule_index] = lpm->rules_tbl[last_rule_index];
293 lpm->used_rules_at_depth[depth - 1]--;
298 * Finds a rule in rule table.
299 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
301 static inline int32_t
302 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
304 uint32_t rule_gindex, last_rule, rule_index;
308 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
309 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
311 /* Scan used rules at given depth to find rule. */
312 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
313 /* If rule is found return the rule index. */
314 if (lpm->rules_tbl[rule_index].ip == ip_masked)
318 /* If rule is not found return -E_RTE_NO_TAILQ. */
319 return -E_RTE_NO_TAILQ;
323 * Find, clean and allocate a tbl8.
325 static inline int32_t
326 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
328 uint32_t tbl8_gindex; /* tbl8 group index. */
329 struct rte_lpm_tbl8_entry *tbl8_entry;
331 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
332 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
334 tbl8_entry = &tbl8[tbl8_gindex *
335 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
336 /* If a free tbl8 group is found clean it and set as VALID. */
337 if (!tbl8_entry->valid_group) {
338 memset(&tbl8_entry[0], 0,
339 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
340 sizeof(tbl8_entry[0]));
342 tbl8_entry->valid_group = VALID;
344 /* Return group index for allocated tbl8 group. */
349 /* If there are no tbl8 groups free then return error. */
354 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
356 /* Set tbl8 group invalid*/
357 tbl8[tbl8_group_start].valid_group = INVALID;
360 static inline int32_t
361 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
364 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
366 /* Calculate the index into Table24. */
367 tbl24_index = ip >> 8;
368 tbl24_range = depth_to_range(depth);
370 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
372 * For invalid OR valid and non-extended tbl 24 entries set
375 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
376 lpm->tbl24[i].depth <= depth)) {
378 struct rte_lpm_tbl24_entry new_tbl24_entry = {
382 { .next_hop = next_hop, }
385 /* Setting tbl24 entry in one go to avoid race
387 lpm->tbl24[i] = new_tbl24_entry;
392 /* If tbl24 entry is valid and extended calculate the index
394 tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex *
395 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
396 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
398 for (j = tbl8_index; j < tbl8_group_end; j++) {
399 if (!lpm->tbl8[j].valid ||
400 lpm->tbl8[j].depth <= depth) {
401 struct rte_lpm_tbl8_entry new_tbl8_entry = {
403 .valid_group = VALID,
405 .next_hop = next_hop,
409 * Setting tbl8 entry in one go to avoid race
412 lpm->tbl8[j] = new_tbl8_entry;
422 static inline int32_t
423 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
426 uint32_t tbl24_index;
427 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
430 tbl24_index = (ip_masked >> 8);
431 tbl8_range = depth_to_range(depth);
433 if (!lpm->tbl24[tbl24_index].valid) {
434 /* Search for a free tbl8 group. */
435 tbl8_group_index = tbl8_alloc(lpm->tbl8);
437 /* Check tbl8 allocation was successful. */
438 if (tbl8_group_index < 0) {
439 return tbl8_group_index;
442 /* Find index into tbl8 and range. */
443 tbl8_index = (tbl8_group_index *
444 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
447 /* Set tbl8 entry. */
448 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
449 lpm->tbl8[i].depth = depth;
450 lpm->tbl8[i].next_hop = next_hop;
451 lpm->tbl8[i].valid = VALID;
455 * Update tbl24 entry to point to new tbl8 entry. Note: The
456 * ext_flag and tbl8_index need to be updated simultaneously,
457 * so assign whole structure in one go
460 struct rte_lpm_tbl24_entry new_tbl24_entry = {
464 { .tbl8_gindex = (uint8_t)tbl8_group_index, }
467 lpm->tbl24[tbl24_index] = new_tbl24_entry;
469 }/* If valid entry but not extended calculate the index into Table8. */
470 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
471 /* Search for free tbl8 group. */
472 tbl8_group_index = tbl8_alloc(lpm->tbl8);
474 if (tbl8_group_index < 0) {
475 return tbl8_group_index;
478 tbl8_group_start = tbl8_group_index *
479 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
480 tbl8_group_end = tbl8_group_start +
481 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
483 /* Populate new tbl8 with tbl24 value. */
484 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
485 lpm->tbl8[i].valid = VALID;
486 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
487 lpm->tbl8[i].next_hop =
488 lpm->tbl24[tbl24_index].next_hop;
491 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
493 /* Insert new rule into the tbl8 entry. */
494 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
495 if (!lpm->tbl8[i].valid ||
496 lpm->tbl8[i].depth <= depth) {
497 lpm->tbl8[i].valid = VALID;
498 lpm->tbl8[i].depth = depth;
499 lpm->tbl8[i].next_hop = next_hop;
506 * Update tbl24 entry to point to new tbl8 entry. Note: The
507 * ext_flag and tbl8_index need to be updated simultaneously,
508 * so assign whole structure in one go.
511 struct rte_lpm_tbl24_entry new_tbl24_entry = {
515 { .tbl8_gindex = (uint8_t)tbl8_group_index, }
518 lpm->tbl24[tbl24_index] = new_tbl24_entry;
522 * If it is valid, extended entry calculate the index into tbl8.
524 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
525 tbl8_group_start = tbl8_group_index *
526 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
527 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
529 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
531 if (!lpm->tbl8[i].valid ||
532 lpm->tbl8[i].depth <= depth) {
533 struct rte_lpm_tbl8_entry new_tbl8_entry = {
536 .next_hop = next_hop,
540 * Setting tbl8 entry in one go to avoid race
543 lpm->tbl8[i] = new_tbl8_entry;
557 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
560 int32_t rule_index, status = 0;
563 /* Check user arguments. */
564 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
567 ip_masked = ip & depth_to_mask(depth);
569 /* Add the rule to the rule table. */
570 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
572 /* If the is no space available for new rule return error. */
573 if (rule_index < 0) {
577 if (depth <= MAX_DEPTH_TBL24) {
578 status = add_depth_small(lpm, ip_masked, depth, next_hop);
580 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
581 status = add_depth_big(lpm, ip_masked, depth, next_hop);
584 * If add fails due to exhaustion of tbl8 extensions delete
585 * rule that was added to rule table.
588 rule_delete(lpm, rule_index, depth);
597 static inline int32_t
598 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
604 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
605 ip_masked = ip & depth_to_mask(prev_depth);
607 rule_index = rule_find(lpm, ip_masked, prev_depth);
616 static inline int32_t
617 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
618 uint8_t depth, int32_t sub_rule_index)
620 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
623 /* Calculate the range and index into Table24. */
624 tbl24_range = depth_to_range(depth);
625 tbl24_index = (ip_masked >> 8);
628 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
629 * and a positive number indicates a sub_rule_index.
631 if (sub_rule_index < 0) {
633 * If no replacement rule exists then invalidate entries
634 * associated with this rule.
636 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
638 if (lpm->tbl24[i].ext_entry == 0 &&
639 lpm->tbl24[i].depth <= depth ) {
640 lpm->tbl24[i].valid = INVALID;
644 * If TBL24 entry is extended, then there has
645 * to be a rule with depth >= 25 in the
646 * associated TBL8 group.
649 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
650 tbl8_index = tbl8_group_index *
651 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
653 for (j = tbl8_index; j < (tbl8_index +
654 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
656 if (lpm->tbl8[j].depth <= depth)
657 lpm->tbl8[j].valid = INVALID;
664 * If a replacement rule exists then modify entries
665 * associated with this rule.
668 /* Calculate depth of sub_rule. */
669 new_depth = (uint8_t) (sub_rule_index /
670 lpm->max_rules_per_depth);
672 struct rte_lpm_tbl24_entry new_tbl24_entry = {
676 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,}
679 struct rte_lpm_tbl8_entry new_tbl8_entry = {
682 .next_hop = lpm->rules_tbl
683 [sub_rule_index].next_hop,
686 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
688 if (lpm->tbl24[i].ext_entry == 0 &&
689 lpm->tbl24[i].depth <= depth ) {
690 lpm->tbl24[i] = new_tbl24_entry;
694 * If TBL24 entry is extended, then there has
695 * to be a rule with depth >= 25 in the
696 * associated TBL8 group.
699 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
700 tbl8_index = tbl8_group_index *
701 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
703 for (j = tbl8_index; j < (tbl8_index +
704 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
706 if (lpm->tbl8[j].depth <= depth)
707 lpm->tbl8[j] = new_tbl8_entry;
717 * Checks if table 8 group can be recycled.
719 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
720 * Return of -EINVAL means tbl8 is empty and thus can be recycled
721 * Return of value > -1 means tbl8 is in use but has all the same values and
722 * thus can be recycled
724 static inline int32_t
725 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
727 uint32_t tbl8_group_end, i;
728 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
731 * Check the first entry of the given tbl8. If it is invalid we know
732 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
733 * (As they would affect all entries in a tbl8) and thus this table
734 * can not be recycled.
736 if (tbl8[tbl8_group_start].valid) {
738 * If first entry is valid check if the depth is less than 24
739 * and if so check the rest of the entries to verify that they
740 * are all of this depth.
742 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
743 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
747 tbl8[tbl8_group_start].depth) {
752 /* If all entries are the same return the tb8 index */
753 return tbl8_group_start;
759 * If the first entry is invalid check if the rest of the entries in
760 * the tbl8 are invalid.
762 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
766 /* If no valid entries are found then return -EINVAL. */
770 static inline int32_t
771 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
772 uint8_t depth, int32_t sub_rule_index)
774 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
777 int32_t tbl8_recycle_index;
780 * Calculate the index into tbl24 and range. Note: All depths larger
781 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
783 tbl24_index = ip_masked >> 8;
785 /* Calculate the index into tbl8 and range. */
786 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
787 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
788 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
789 tbl8_range = depth_to_range(depth);
791 if (sub_rule_index < 0) {
793 * Loop through the range of entries on tbl8 for which the
794 * rule_to_delete must be removed or modified.
796 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
797 if (lpm->tbl8[i].depth <= depth)
798 lpm->tbl8[i].valid = INVALID;
802 new_depth = (uint8_t)(sub_rule_index /
803 lpm->max_rules_per_depth);
805 /* Set new tbl8 entry. */
806 struct rte_lpm_tbl8_entry new_tbl8_entry = {
809 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
813 * Loop through the range of entries on tbl8 for which the
814 * rule_to_delete must be modified.
816 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
817 if (lpm->tbl8[i].depth <= depth)
818 lpm->tbl8[i] = new_tbl8_entry;
823 * Check if there are any valid entries in this tbl8 group. If all
824 * tbl8 entries are invalid we can free the tbl8 and invalidate the
825 * associated tbl24 entry.
828 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
830 if (tbl8_recycle_index == -EINVAL){
831 /* Set tbl24 before freeing tbl8 to avoid race condition. */
832 lpm->tbl24[tbl24_index].valid = 0;
833 tbl8_free(lpm->tbl8, tbl8_group_start);
835 else if (tbl8_recycle_index > -1) {
836 /* Update tbl24 entry. */
837 struct rte_lpm_tbl24_entry new_tbl24_entry = {
840 .depth = lpm->tbl8[tbl8_recycle_index].depth,
841 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, }
844 /* Set tbl24 before freeing tbl8 to avoid race condition. */
845 lpm->tbl24[tbl24_index] = new_tbl24_entry;
846 tbl8_free(lpm->tbl8, tbl8_group_start);
856 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
858 int32_t rule_to_delete_index, sub_rule_index;
861 * Check input arguments. Note: IP must be a positive integer of 32
862 * bits in length therefore it need not be checked.
864 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
868 ip_masked = ip & depth_to_mask(depth);
871 * Find the index of the input rule, that needs to be deleted, in the
874 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
877 * Check if rule_to_delete_index was found. If no rule was found the
878 * function rule_find returns -E_RTE_NO_TAILQ.
880 if (rule_to_delete_index < 0)
881 return -E_RTE_NO_TAILQ;
883 /* Delete the rule from the rule table. */
884 rule_delete(lpm, rule_to_delete_index, depth);
887 * Find rule to replace the rule_to_delete. If there is no rule to
888 * replace the rule_to_delete we return -1 and invalidate the table
889 * entries associated with this rule.
891 sub_rule_index = find_previous_rule(lpm, ip, depth);
894 * If the input depth value is less than 25 use function
895 * delete_depth_small otherwise use delete_depth_big.
897 if (depth <= MAX_DEPTH_TBL24) {
898 return delete_depth_small(lpm, ip_masked, depth,
901 else { /* If depth > MAX_DEPTH_TBL24 */
902 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index);
907 * Delete all rules from the LPM table.
910 rte_lpm_delete_all(struct rte_lpm *lpm)
912 /* Zero used rules counter. */
913 memset(lpm->used_rules_at_depth, 0, sizeof(lpm->used_rules_at_depth));
916 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
919 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
921 /* Delete all rules form the rules table. */
922 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) *
923 (lpm->max_rules_per_depth * RTE_LPM_MAX_DEPTH));