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>
55 #include <rte_rwlock.h>
56 #include <rte_spinlock.h>
60 TAILQ_HEAD(rte_lpm_list, rte_lpm);
62 #define MAX_DEPTH_TBL24 24
69 /* Macro to enable/disable run-time checks. */
70 #if defined(RTE_LIBRTE_LPM_DEBUG)
71 #include <rte_debug.h>
72 #define VERIFY_DEPTH(depth) do { \
73 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
74 rte_panic("LPM: Invalid depth (%u) at line %d", \
75 (unsigned)(depth), __LINE__); \
78 #define VERIFY_DEPTH(depth)
82 * Converts a given depth value to its corresponding mask value.
84 * depth (IN) : range = 1 - 32
85 * mask (OUT) : 32bit mask
87 static uint32_t __attribute__((pure))
88 depth_to_mask(uint8_t depth)
92 /* To calculate a mask start with a 1 on the left hand side and right
93 * shift while populating the left hand side with 1's
95 return (int)0x80000000 >> (depth - 1);
99 * Converts given depth value to its corresponding range value.
101 static inline uint32_t __attribute__((pure))
102 depth_to_range(uint8_t depth)
107 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
109 if (depth <= MAX_DEPTH_TBL24)
110 return 1 << (MAX_DEPTH_TBL24 - depth);
112 /* Else if depth is greater than 24 */
113 return (1 << (RTE_LPM_MAX_DEPTH - depth));
117 * Find an existing lpm table and return a pointer to it.
120 rte_lpm_find_existing(const char *name)
123 struct rte_lpm_list *lpm_list;
125 /* check that we have an initialised tail queue */
126 if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
127 rte_errno = E_RTE_NO_TAILQ;
131 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
132 TAILQ_FOREACH(l, lpm_list, next) {
133 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
136 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
145 * Allocates memory for LPM object
148 rte_lpm_create(const char *name, int socket_id, int max_rules,
149 __rte_unused int flags)
151 char mem_name[RTE_LPM_NAMESIZE];
152 struct rte_lpm *lpm = NULL;
154 struct rte_lpm_list *lpm_list;
156 /* check that we have an initialised tail queue */
158 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
159 rte_errno = E_RTE_NO_TAILQ;
163 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
164 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
166 /* Check user arguments. */
167 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
172 rte_snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
175 * Pad out max_rules so that each depth is given the same number of
178 if (max_rules % RTE_LPM_MAX_DEPTH) {
179 max_rules += RTE_LPM_MAX_DEPTH -
180 (max_rules % RTE_LPM_MAX_DEPTH);
183 /* Determine the amount of memory to allocate. */
184 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
186 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
188 /* guarantee there's no existing */
189 TAILQ_FOREACH(lpm, lpm_list, next) {
190 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
196 /* Allocate memory to store the LPM data structures. */
197 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
198 CACHE_LINE_SIZE, socket_id);
200 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
204 /* Save user arguments. */
205 lpm->max_rules_per_depth = max_rules / RTE_LPM_MAX_DEPTH;
206 rte_snprintf(lpm->name, sizeof(lpm->name), "%s", name);
208 TAILQ_INSERT_TAIL(lpm_list, lpm, next);
211 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
217 * Deallocates memory for given LPM table.
220 rte_lpm_free(struct rte_lpm *lpm)
222 /* Check user arguments. */
226 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
231 * Adds a rule to the rule table.
233 * NOTE: The rule table is split into 32 groups. Each group contains rules that
234 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
235 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
236 * to refer to depth 1 because even though the depth range is 1 - 32, depths
237 * are stored in the rule table from 0 - 31.
238 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
240 static inline int32_t
241 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
244 uint32_t rule_gindex, rule_index, last_rule;
248 /* rule_gindex stands for rule group index. */
249 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
250 /* Initialise rule_index to point to start of rule group. */
251 rule_index = rule_gindex;
252 /* Last rule = Last used rule in this rule group. */
253 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
255 /* Scan through rule group to see if rule already exists. */
256 for (; rule_index < last_rule; rule_index++) {
258 /* If rule already exists update its next_hop and return. */
259 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
260 lpm->rules_tbl[rule_index].next_hop = next_hop;
267 * If rule does not exist check if there is space to add a new rule to
268 * this rule group. If there is no space return error. */
269 if (lpm->used_rules_at_depth[depth - 1] == lpm->max_rules_per_depth) {
273 /* If there is space for the new rule add it. */
274 lpm->rules_tbl[rule_index].ip = ip_masked;
275 lpm->rules_tbl[rule_index].next_hop = next_hop;
277 /* Increment the used rules counter for this rule group. */
278 lpm->used_rules_at_depth[depth - 1]++;
284 * Delete a rule from the rule table.
285 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
288 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
290 uint32_t rule_gindex, last_rule_index;
294 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
295 last_rule_index = rule_gindex +
296 (lpm->used_rules_at_depth[depth - 1]) - 1;
298 * Overwrite redundant rule with last rule in group and decrement rule
301 lpm->rules_tbl[rule_index] = lpm->rules_tbl[last_rule_index];
302 lpm->used_rules_at_depth[depth - 1]--;
307 * Finds a rule in rule table.
308 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
310 static inline int32_t
311 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
313 uint32_t rule_gindex, last_rule, rule_index;
317 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
318 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
320 /* Scan used rules at given depth to find rule. */
321 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
322 /* If rule is found return the rule index. */
323 if (lpm->rules_tbl[rule_index].ip == ip_masked)
327 /* If rule is not found return -E_RTE_NO_TAILQ. */
328 return -E_RTE_NO_TAILQ;
332 * Find, clean and allocate a tbl8.
334 static inline int32_t
335 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
337 uint32_t tbl8_gindex; /* tbl8 group index. */
338 struct rte_lpm_tbl8_entry *tbl8_entry;
340 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
341 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
343 tbl8_entry = &tbl8[tbl8_gindex *
344 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
345 /* If a free tbl8 group is found clean it and set as VALID. */
346 if (!tbl8_entry->valid_group) {
347 memset(&tbl8_entry[0], 0,
348 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
349 sizeof(tbl8_entry[0]));
351 tbl8_entry->valid_group = VALID;
353 /* Return group index for allocated tbl8 group. */
358 /* If there are no tbl8 groups free then return error. */
363 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
365 /* Set tbl8 group invalid*/
366 tbl8[tbl8_group_start].valid_group = INVALID;
369 static inline int32_t
370 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
373 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
375 /* Calculate the index into Table24. */
376 tbl24_index = ip >> 8;
377 tbl24_range = depth_to_range(depth);
379 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
381 * For invalid OR valid and non-extended tbl 24 entries set
384 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
385 lpm->tbl24[i].depth <= depth)) {
387 struct rte_lpm_tbl24_entry new_tbl24_entry = {
388 { .next_hop = next_hop, },
394 /* Setting tbl24 entry in one go to avoid race
396 lpm->tbl24[i] = new_tbl24_entry;
401 /* If tbl24 entry is valid and extended calculate the index
403 tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex *
404 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
405 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
407 for (j = tbl8_index; j < tbl8_group_end; j++) {
408 if (!lpm->tbl8[j].valid ||
409 lpm->tbl8[j].depth <= depth) {
410 struct rte_lpm_tbl8_entry new_tbl8_entry = {
412 .valid_group = VALID,
414 .next_hop = next_hop,
418 * Setting tbl8 entry in one go to avoid race
421 lpm->tbl8[j] = new_tbl8_entry;
431 static inline int32_t
432 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
435 uint32_t tbl24_index;
436 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
439 tbl24_index = (ip_masked >> 8);
440 tbl8_range = depth_to_range(depth);
442 if (!lpm->tbl24[tbl24_index].valid) {
443 /* Search for a free tbl8 group. */
444 tbl8_group_index = tbl8_alloc(lpm->tbl8);
446 /* Check tbl8 allocation was successful. */
447 if (tbl8_group_index < 0) {
448 return tbl8_group_index;
451 /* Find index into tbl8 and range. */
452 tbl8_index = (tbl8_group_index *
453 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
456 /* Set tbl8 entry. */
457 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
458 lpm->tbl8[i].depth = depth;
459 lpm->tbl8[i].next_hop = next_hop;
460 lpm->tbl8[i].valid = VALID;
464 * Update tbl24 entry to point to new tbl8 entry. Note: The
465 * ext_flag and tbl8_index need to be updated simultaneously,
466 * so assign whole structure in one go
469 struct rte_lpm_tbl24_entry new_tbl24_entry = {
470 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
476 lpm->tbl24[tbl24_index] = new_tbl24_entry;
478 }/* If valid entry but not extended calculate the index into Table8. */
479 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
480 /* Search for free tbl8 group. */
481 tbl8_group_index = tbl8_alloc(lpm->tbl8);
483 if (tbl8_group_index < 0) {
484 return tbl8_group_index;
487 tbl8_group_start = tbl8_group_index *
488 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
489 tbl8_group_end = tbl8_group_start +
490 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
492 /* Populate new tbl8 with tbl24 value. */
493 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
494 lpm->tbl8[i].valid = VALID;
495 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
496 lpm->tbl8[i].next_hop =
497 lpm->tbl24[tbl24_index].next_hop;
500 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
502 /* Insert new rule into the tbl8 entry. */
503 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
504 if (!lpm->tbl8[i].valid ||
505 lpm->tbl8[i].depth <= depth) {
506 lpm->tbl8[i].valid = VALID;
507 lpm->tbl8[i].depth = depth;
508 lpm->tbl8[i].next_hop = next_hop;
515 * Update tbl24 entry to point to new tbl8 entry. Note: The
516 * ext_flag and tbl8_index need to be updated simultaneously,
517 * so assign whole structure in one go.
520 struct rte_lpm_tbl24_entry new_tbl24_entry = {
521 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
527 lpm->tbl24[tbl24_index] = new_tbl24_entry;
531 * If it is valid, extended entry calculate the index into tbl8.
533 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
534 tbl8_group_start = tbl8_group_index *
535 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
536 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
538 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
540 if (!lpm->tbl8[i].valid ||
541 lpm->tbl8[i].depth <= depth) {
542 struct rte_lpm_tbl8_entry new_tbl8_entry = {
545 .next_hop = next_hop,
549 * Setting tbl8 entry in one go to avoid race
552 lpm->tbl8[i] = new_tbl8_entry;
566 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
569 int32_t rule_index, status = 0;
572 /* Check user arguments. */
573 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
576 ip_masked = ip & depth_to_mask(depth);
578 /* Add the rule to the rule table. */
579 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
581 /* If the is no space available for new rule return error. */
582 if (rule_index < 0) {
586 if (depth <= MAX_DEPTH_TBL24) {
587 status = add_depth_small(lpm, ip_masked, depth, next_hop);
589 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
590 status = add_depth_big(lpm, ip_masked, depth, next_hop);
593 * If add fails due to exhaustion of tbl8 extensions delete
594 * rule that was added to rule table.
597 rule_delete(lpm, rule_index, depth);
606 static inline int32_t
607 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
613 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
614 ip_masked = ip & depth_to_mask(prev_depth);
616 rule_index = rule_find(lpm, ip_masked, prev_depth);
625 static inline int32_t
626 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
627 uint8_t depth, int32_t sub_rule_index)
629 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
632 /* Calculate the range and index into Table24. */
633 tbl24_range = depth_to_range(depth);
634 tbl24_index = (ip_masked >> 8);
637 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
638 * and a positive number indicates a sub_rule_index.
640 if (sub_rule_index < 0) {
642 * If no replacement rule exists then invalidate entries
643 * associated with this rule.
645 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
647 if (lpm->tbl24[i].ext_entry == 0 &&
648 lpm->tbl24[i].depth <= depth ) {
649 lpm->tbl24[i].valid = INVALID;
653 * If TBL24 entry is extended, then there has
654 * to be a rule with depth >= 25 in the
655 * associated TBL8 group.
658 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
659 tbl8_index = tbl8_group_index *
660 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
662 for (j = tbl8_index; j < (tbl8_index +
663 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
665 if (lpm->tbl8[j].depth <= depth)
666 lpm->tbl8[j].valid = INVALID;
673 * If a replacement rule exists then modify entries
674 * associated with this rule.
677 /* Calculate depth of sub_rule. */
678 new_depth = (uint8_t) (sub_rule_index /
679 lpm->max_rules_per_depth);
681 struct rte_lpm_tbl24_entry new_tbl24_entry = {
682 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
688 struct rte_lpm_tbl8_entry new_tbl8_entry = {
691 .next_hop = lpm->rules_tbl
692 [sub_rule_index].next_hop,
695 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
697 if (lpm->tbl24[i].ext_entry == 0 &&
698 lpm->tbl24[i].depth <= depth ) {
699 lpm->tbl24[i] = new_tbl24_entry;
703 * If TBL24 entry is extended, then there has
704 * to be a rule with depth >= 25 in the
705 * associated TBL8 group.
708 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
709 tbl8_index = tbl8_group_index *
710 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
712 for (j = tbl8_index; j < (tbl8_index +
713 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
715 if (lpm->tbl8[j].depth <= depth)
716 lpm->tbl8[j] = new_tbl8_entry;
726 * Checks if table 8 group can be recycled.
728 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
729 * Return of -EINVAL means tbl8 is empty and thus can be recycled
730 * Return of value > -1 means tbl8 is in use but has all the same values and
731 * thus can be recycled
733 static inline int32_t
734 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
736 uint32_t tbl8_group_end, i;
737 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
740 * Check the first entry of the given tbl8. If it is invalid we know
741 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
742 * (As they would affect all entries in a tbl8) and thus this table
743 * can not be recycled.
745 if (tbl8[tbl8_group_start].valid) {
747 * If first entry is valid check if the depth is less than 24
748 * and if so check the rest of the entries to verify that they
749 * are all of this depth.
751 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
752 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
756 tbl8[tbl8_group_start].depth) {
761 /* If all entries are the same return the tb8 index */
762 return tbl8_group_start;
768 * If the first entry is invalid check if the rest of the entries in
769 * the tbl8 are invalid.
771 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
775 /* If no valid entries are found then return -EINVAL. */
779 static inline int32_t
780 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
781 uint8_t depth, int32_t sub_rule_index)
783 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
786 int32_t tbl8_recycle_index;
789 * Calculate the index into tbl24 and range. Note: All depths larger
790 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
792 tbl24_index = ip_masked >> 8;
794 /* Calculate the index into tbl8 and range. */
795 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
796 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
797 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
798 tbl8_range = depth_to_range(depth);
800 if (sub_rule_index < 0) {
802 * Loop through the range of entries on tbl8 for which the
803 * rule_to_delete must be removed or modified.
805 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
806 if (lpm->tbl8[i].depth <= depth)
807 lpm->tbl8[i].valid = INVALID;
811 new_depth = (uint8_t)(sub_rule_index /
812 lpm->max_rules_per_depth);
814 /* Set new tbl8 entry. */
815 struct rte_lpm_tbl8_entry new_tbl8_entry = {
818 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
822 * Loop through the range of entries on tbl8 for which the
823 * rule_to_delete must be modified.
825 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
826 if (lpm->tbl8[i].depth <= depth)
827 lpm->tbl8[i] = new_tbl8_entry;
832 * Check if there are any valid entries in this tbl8 group. If all
833 * tbl8 entries are invalid we can free the tbl8 and invalidate the
834 * associated tbl24 entry.
837 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
839 if (tbl8_recycle_index == -EINVAL){
840 /* Set tbl24 before freeing tbl8 to avoid race condition. */
841 lpm->tbl24[tbl24_index].valid = 0;
842 tbl8_free(lpm->tbl8, tbl8_group_start);
844 else if (tbl8_recycle_index > -1) {
845 /* Update tbl24 entry. */
846 struct rte_lpm_tbl24_entry new_tbl24_entry = {
847 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
850 .depth = lpm->tbl8[tbl8_recycle_index].depth,
853 /* Set tbl24 before freeing tbl8 to avoid race condition. */
854 lpm->tbl24[tbl24_index] = new_tbl24_entry;
855 tbl8_free(lpm->tbl8, tbl8_group_start);
865 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
867 int32_t rule_to_delete_index, sub_rule_index;
870 * Check input arguments. Note: IP must be a positive integer of 32
871 * bits in length therefore it need not be checked.
873 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
877 ip_masked = ip & depth_to_mask(depth);
880 * Find the index of the input rule, that needs to be deleted, in the
883 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
886 * Check if rule_to_delete_index was found. If no rule was found the
887 * function rule_find returns -E_RTE_NO_TAILQ.
889 if (rule_to_delete_index < 0)
890 return -E_RTE_NO_TAILQ;
892 /* Delete the rule from the rule table. */
893 rule_delete(lpm, rule_to_delete_index, depth);
896 * Find rule to replace the rule_to_delete. If there is no rule to
897 * replace the rule_to_delete we return -1 and invalidate the table
898 * entries associated with this rule.
900 sub_rule_index = find_previous_rule(lpm, ip, depth);
903 * If the input depth value is less than 25 use function
904 * delete_depth_small otherwise use delete_depth_big.
906 if (depth <= MAX_DEPTH_TBL24) {
907 return delete_depth_small(lpm, ip_masked, depth,
910 else { /* If depth > MAX_DEPTH_TBL24 */
911 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index);
916 * Delete all rules from the LPM table.
919 rte_lpm_delete_all(struct rte_lpm *lpm)
921 /* Zero used rules counter. */
922 memset(lpm->used_rules_at_depth, 0, sizeof(lpm->used_rules_at_depth));
925 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
928 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
930 /* Delete all rules form the rules table. */
931 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) *
932 (lpm->max_rules_per_depth * RTE_LPM_MAX_DEPTH));