4 * Copyright(c) 2010-2014 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.
40 #include <sys/queue.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_common.h>
45 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
46 #include <rte_malloc.h>
47 #include <rte_memzone.h>
49 #include <rte_eal_memconfig.h>
50 #include <rte_per_lcore.h>
51 #include <rte_string_fns.h>
52 #include <rte_errno.h>
53 #include <rte_rwlock.h>
54 #include <rte_spinlock.h>
58 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
60 static struct rte_tailq_elem rte_lpm_tailq = {
63 EAL_REGISTER_TAILQ(rte_lpm_tailq)
65 #define MAX_DEPTH_TBL24 24
72 /* Macro to enable/disable run-time checks. */
73 #if defined(RTE_LIBRTE_LPM_DEBUG)
74 #include <rte_debug.h>
75 #define VERIFY_DEPTH(depth) do { \
76 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
77 rte_panic("LPM: Invalid depth (%u) at line %d", \
78 (unsigned)(depth), __LINE__); \
81 #define VERIFY_DEPTH(depth)
85 * Converts a given depth value to its corresponding mask value.
87 * depth (IN) : range = 1 - 32
88 * mask (OUT) : 32bit mask
90 static uint32_t __attribute__((pure))
91 depth_to_mask(uint8_t depth)
95 /* To calculate a mask start with a 1 on the left hand side and right
96 * shift while populating the left hand side with 1's
98 return (int)0x80000000 >> (depth - 1);
102 * Converts given depth value to its corresponding range value.
104 static inline uint32_t __attribute__((pure))
105 depth_to_range(uint8_t depth)
110 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
112 if (depth <= MAX_DEPTH_TBL24)
113 return 1 << (MAX_DEPTH_TBL24 - depth);
115 /* Else if depth is greater than 24 */
116 return (1 << (RTE_LPM_MAX_DEPTH - depth));
120 * Find an existing lpm table and return a pointer to it.
123 rte_lpm_find_existing(const char *name)
125 struct rte_lpm *l = NULL;
126 struct rte_tailq_entry *te;
127 struct rte_lpm_list *lpm_list;
129 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
131 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
132 TAILQ_FOREACH(te, lpm_list, next) {
133 l = (struct rte_lpm *) te->data;
134 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
137 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
148 * Allocates memory for LPM object
151 rte_lpm_create(const char *name, int socket_id, int max_rules,
152 __rte_unused int flags)
154 char mem_name[RTE_LPM_NAMESIZE];
155 struct rte_lpm *lpm = NULL;
156 struct rte_tailq_entry *te;
158 struct rte_lpm_list *lpm_list;
160 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
162 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
163 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
165 /* Check user arguments. */
166 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
171 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
173 /* Determine the amount of memory to allocate. */
174 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
176 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
178 /* guarantee there's no existing */
179 TAILQ_FOREACH(te, lpm_list, next) {
180 lpm = (struct rte_lpm *) te->data;
181 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
187 /* allocate tailq entry */
188 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
190 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
194 /* Allocate memory to store the LPM data structures. */
195 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
196 RTE_CACHE_LINE_SIZE, socket_id);
198 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
203 /* Save user arguments. */
204 lpm->max_rules = max_rules;
205 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
207 te->data = (void *) lpm;
209 TAILQ_INSERT_TAIL(lpm_list, te, next);
212 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
218 * Deallocates memory for given LPM table.
221 rte_lpm_free(struct rte_lpm *lpm)
223 struct rte_lpm_list *lpm_list;
224 struct rte_tailq_entry *te;
226 /* Check user arguments. */
230 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
232 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
234 /* find our tailq entry */
235 TAILQ_FOREACH(te, lpm_list, next) {
236 if (te->data == (void *) lpm)
240 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
244 TAILQ_REMOVE(lpm_list, te, next);
246 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
253 * Adds a rule to the rule table.
255 * NOTE: The rule table is split into 32 groups. Each group contains rules that
256 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
257 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
258 * to refer to depth 1 because even though the depth range is 1 - 32, depths
259 * are stored in the rule table from 0 - 31.
260 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
262 static inline int32_t
263 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
266 uint32_t rule_gindex, rule_index, last_rule;
271 /* Scan through rule group to see if rule already exists. */
272 if (lpm->rule_info[depth - 1].used_rules > 0) {
274 /* rule_gindex stands for rule group index. */
275 rule_gindex = lpm->rule_info[depth - 1].first_rule;
276 /* Initialise rule_index to point to start of rule group. */
277 rule_index = rule_gindex;
278 /* Last rule = Last used rule in this rule group. */
279 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
281 for (; rule_index < last_rule; rule_index++) {
283 /* If rule already exists update its next_hop and return. */
284 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
285 lpm->rules_tbl[rule_index].next_hop = next_hop;
291 if (rule_index == lpm->max_rules)
294 /* Calculate the position in which the rule will be stored. */
297 for (i = depth - 1; i > 0; i--) {
298 if (lpm->rule_info[i - 1].used_rules > 0) {
299 rule_index = lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules;
303 if (rule_index == lpm->max_rules)
306 lpm->rule_info[depth - 1].first_rule = rule_index;
309 /* Make room for the new rule in the array. */
310 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
311 if (lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
314 if (lpm->rule_info[i - 1].used_rules > 0) {
315 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules]
316 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
317 lpm->rule_info[i - 1].first_rule++;
321 /* Add the new rule. */
322 lpm->rules_tbl[rule_index].ip = ip_masked;
323 lpm->rules_tbl[rule_index].next_hop = next_hop;
325 /* Increment the used rules counter for this rule group. */
326 lpm->rule_info[depth - 1].used_rules++;
332 * Delete a rule from the rule table.
333 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
336 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
342 lpm->rules_tbl[rule_index] = lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
343 + lpm->rule_info[depth - 1].used_rules - 1];
345 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
346 if (lpm->rule_info[i].used_rules > 0) {
347 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
348 lpm->rules_tbl[lpm->rule_info[i].first_rule + lpm->rule_info[i].used_rules - 1];
349 lpm->rule_info[i].first_rule--;
353 lpm->rule_info[depth - 1].used_rules--;
357 * Finds a rule in rule table.
358 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
360 static inline int32_t
361 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
363 uint32_t rule_gindex, last_rule, rule_index;
367 rule_gindex = lpm->rule_info[depth - 1].first_rule;
368 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
370 /* Scan used rules at given depth to find rule. */
371 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
372 /* If rule is found return the rule index. */
373 if (lpm->rules_tbl[rule_index].ip == ip_masked)
377 /* If rule is not found return -EINVAL. */
382 * Find, clean and allocate a tbl8.
384 static inline int32_t
385 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
387 uint32_t tbl8_gindex; /* tbl8 group index. */
388 struct rte_lpm_tbl8_entry *tbl8_entry;
390 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
391 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
393 tbl8_entry = &tbl8[tbl8_gindex *
394 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
395 /* If a free tbl8 group is found clean it and set as VALID. */
396 if (!tbl8_entry->valid_group) {
397 memset(&tbl8_entry[0], 0,
398 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
399 sizeof(tbl8_entry[0]));
401 tbl8_entry->valid_group = VALID;
403 /* Return group index for allocated tbl8 group. */
408 /* If there are no tbl8 groups free then return error. */
413 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
415 /* Set tbl8 group invalid*/
416 tbl8[tbl8_group_start].valid_group = INVALID;
419 static inline int32_t
420 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
423 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
425 /* Calculate the index into Table24. */
426 tbl24_index = ip >> 8;
427 tbl24_range = depth_to_range(depth);
429 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
431 * For invalid OR valid and non-extended tbl 24 entries set
434 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
435 lpm->tbl24[i].depth <= depth)) {
437 struct rte_lpm_tbl24_entry new_tbl24_entry = {
438 { .next_hop = next_hop, },
444 /* Setting tbl24 entry in one go to avoid race
447 lpm->tbl24[i] = new_tbl24_entry;
452 if (lpm->tbl24[i].ext_entry == 1) {
453 /* If tbl24 entry is valid and extended calculate the
456 tbl8_index = lpm->tbl24[i].tbl8_gindex *
457 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
458 tbl8_group_end = tbl8_index +
459 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
461 for (j = tbl8_index; j < tbl8_group_end; j++) {
462 if (!lpm->tbl8[j].valid ||
463 lpm->tbl8[j].depth <= depth) {
464 struct rte_lpm_tbl8_entry
467 .valid_group = VALID,
469 .next_hop = next_hop,
473 * Setting tbl8 entry in one go to avoid
476 lpm->tbl8[j] = new_tbl8_entry;
487 static inline int32_t
488 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
491 uint32_t tbl24_index;
492 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
495 tbl24_index = (ip_masked >> 8);
496 tbl8_range = depth_to_range(depth);
498 if (!lpm->tbl24[tbl24_index].valid) {
499 /* Search for a free tbl8 group. */
500 tbl8_group_index = tbl8_alloc(lpm->tbl8);
502 /* Check tbl8 allocation was successful. */
503 if (tbl8_group_index < 0) {
504 return tbl8_group_index;
507 /* Find index into tbl8 and range. */
508 tbl8_index = (tbl8_group_index *
509 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
512 /* Set tbl8 entry. */
513 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
514 lpm->tbl8[i].depth = depth;
515 lpm->tbl8[i].next_hop = next_hop;
516 lpm->tbl8[i].valid = VALID;
520 * Update tbl24 entry to point to new tbl8 entry. Note: The
521 * ext_flag and tbl8_index need to be updated simultaneously,
522 * so assign whole structure in one go
525 struct rte_lpm_tbl24_entry new_tbl24_entry = {
526 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
532 lpm->tbl24[tbl24_index] = new_tbl24_entry;
534 }/* If valid entry but not extended calculate the index into Table8. */
535 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
536 /* Search for free tbl8 group. */
537 tbl8_group_index = tbl8_alloc(lpm->tbl8);
539 if (tbl8_group_index < 0) {
540 return tbl8_group_index;
543 tbl8_group_start = tbl8_group_index *
544 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
545 tbl8_group_end = tbl8_group_start +
546 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
548 /* Populate new tbl8 with tbl24 value. */
549 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
550 lpm->tbl8[i].valid = VALID;
551 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
552 lpm->tbl8[i].next_hop =
553 lpm->tbl24[tbl24_index].next_hop;
556 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
558 /* Insert new rule into the tbl8 entry. */
559 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
560 if (!lpm->tbl8[i].valid ||
561 lpm->tbl8[i].depth <= depth) {
562 lpm->tbl8[i].valid = VALID;
563 lpm->tbl8[i].depth = depth;
564 lpm->tbl8[i].next_hop = next_hop;
571 * Update tbl24 entry to point to new tbl8 entry. Note: The
572 * ext_flag and tbl8_index need to be updated simultaneously,
573 * so assign whole structure in one go.
576 struct rte_lpm_tbl24_entry new_tbl24_entry = {
577 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
583 lpm->tbl24[tbl24_index] = new_tbl24_entry;
587 * If it is valid, extended entry calculate the index into tbl8.
589 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
590 tbl8_group_start = tbl8_group_index *
591 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
592 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
594 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
596 if (!lpm->tbl8[i].valid ||
597 lpm->tbl8[i].depth <= depth) {
598 struct rte_lpm_tbl8_entry new_tbl8_entry = {
601 .next_hop = next_hop,
602 .valid_group = lpm->tbl8[i].valid_group,
606 * Setting tbl8 entry in one go to avoid race
609 lpm->tbl8[i] = new_tbl8_entry;
623 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
626 int32_t rule_index, status = 0;
629 /* Check user arguments. */
630 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
633 ip_masked = ip & depth_to_mask(depth);
635 /* Add the rule to the rule table. */
636 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
638 /* If the is no space available for new rule return error. */
639 if (rule_index < 0) {
643 if (depth <= MAX_DEPTH_TBL24) {
644 status = add_depth_small(lpm, ip_masked, depth, next_hop);
646 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
647 status = add_depth_big(lpm, ip_masked, depth, next_hop);
650 * If add fails due to exhaustion of tbl8 extensions delete
651 * rule that was added to rule table.
654 rule_delete(lpm, rule_index, depth);
664 * Look for a rule in the high-level rules table
667 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
673 /* Check user arguments. */
675 (next_hop == NULL) ||
676 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
679 /* Look for the rule using rule_find. */
680 ip_masked = ip & depth_to_mask(depth);
681 rule_index = rule_find(lpm, ip_masked, depth);
683 if (rule_index >= 0) {
684 *next_hop = lpm->rules_tbl[rule_index].next_hop;
688 /* If rule is not found return 0. */
692 static inline int32_t
693 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
699 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
700 ip_masked = ip & depth_to_mask(prev_depth);
702 rule_index = rule_find(lpm, ip_masked, prev_depth);
704 if (rule_index >= 0) {
705 *sub_rule_depth = prev_depth;
713 static inline int32_t
714 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
715 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
717 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
719 /* Calculate the range and index into Table24. */
720 tbl24_range = depth_to_range(depth);
721 tbl24_index = (ip_masked >> 8);
724 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
725 * and a positive number indicates a sub_rule_index.
727 if (sub_rule_index < 0) {
729 * If no replacement rule exists then invalidate entries
730 * associated with this rule.
732 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
734 if (lpm->tbl24[i].ext_entry == 0 &&
735 lpm->tbl24[i].depth <= depth ) {
736 lpm->tbl24[i].valid = INVALID;
740 * If TBL24 entry is extended, then there has
741 * to be a rule with depth >= 25 in the
742 * associated TBL8 group.
745 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
746 tbl8_index = tbl8_group_index *
747 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
749 for (j = tbl8_index; j < (tbl8_index +
750 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
752 if (lpm->tbl8[j].depth <= depth)
753 lpm->tbl8[j].valid = INVALID;
760 * If a replacement rule exists then modify entries
761 * associated with this rule.
764 struct rte_lpm_tbl24_entry new_tbl24_entry = {
765 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
768 .depth = sub_rule_depth,
771 struct rte_lpm_tbl8_entry new_tbl8_entry = {
773 .depth = sub_rule_depth,
774 .next_hop = lpm->rules_tbl
775 [sub_rule_index].next_hop,
778 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
780 if (lpm->tbl24[i].ext_entry == 0 &&
781 lpm->tbl24[i].depth <= depth ) {
782 lpm->tbl24[i] = new_tbl24_entry;
786 * If TBL24 entry is extended, then there has
787 * to be a rule with depth >= 25 in the
788 * associated TBL8 group.
791 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
792 tbl8_index = tbl8_group_index *
793 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
795 for (j = tbl8_index; j < (tbl8_index +
796 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
798 if (lpm->tbl8[j].depth <= depth)
799 lpm->tbl8[j] = new_tbl8_entry;
809 * Checks if table 8 group can be recycled.
811 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
812 * Return of -EINVAL means tbl8 is empty and thus can be recycled
813 * Return of value > -1 means tbl8 is in use but has all the same values and
814 * thus can be recycled
816 static inline int32_t
817 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
819 uint32_t tbl8_group_end, i;
820 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
823 * Check the first entry of the given tbl8. If it is invalid we know
824 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
825 * (As they would affect all entries in a tbl8) and thus this table
826 * can not be recycled.
828 if (tbl8[tbl8_group_start].valid) {
830 * If first entry is valid check if the depth is less than 24
831 * and if so check the rest of the entries to verify that they
832 * are all of this depth.
834 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
835 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
839 tbl8[tbl8_group_start].depth) {
844 /* If all entries are the same return the tb8 index */
845 return tbl8_group_start;
851 * If the first entry is invalid check if the rest of the entries in
852 * the tbl8 are invalid.
854 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
858 /* If no valid entries are found then return -EINVAL. */
862 static inline int32_t
863 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
864 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
866 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
868 int32_t tbl8_recycle_index;
871 * Calculate the index into tbl24 and range. Note: All depths larger
872 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
874 tbl24_index = ip_masked >> 8;
876 /* Calculate the index into tbl8 and range. */
877 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
878 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
879 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
880 tbl8_range = depth_to_range(depth);
882 if (sub_rule_index < 0) {
884 * Loop through the range of entries on tbl8 for which the
885 * rule_to_delete must be removed or modified.
887 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
888 if (lpm->tbl8[i].depth <= depth)
889 lpm->tbl8[i].valid = INVALID;
893 /* Set new tbl8 entry. */
894 struct rte_lpm_tbl8_entry new_tbl8_entry = {
896 .depth = sub_rule_depth,
897 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
898 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
902 * Loop through the range of entries on tbl8 for which the
903 * rule_to_delete must be modified.
905 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
906 if (lpm->tbl8[i].depth <= depth)
907 lpm->tbl8[i] = new_tbl8_entry;
912 * Check if there are any valid entries in this tbl8 group. If all
913 * tbl8 entries are invalid we can free the tbl8 and invalidate the
914 * associated tbl24 entry.
917 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
919 if (tbl8_recycle_index == -EINVAL){
920 /* Set tbl24 before freeing tbl8 to avoid race condition. */
921 lpm->tbl24[tbl24_index].valid = 0;
922 tbl8_free(lpm->tbl8, tbl8_group_start);
924 else if (tbl8_recycle_index > -1) {
925 /* Update tbl24 entry. */
926 struct rte_lpm_tbl24_entry new_tbl24_entry = {
927 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
930 .depth = lpm->tbl8[tbl8_recycle_index].depth,
933 /* Set tbl24 before freeing tbl8 to avoid race condition. */
934 lpm->tbl24[tbl24_index] = new_tbl24_entry;
935 tbl8_free(lpm->tbl8, tbl8_group_start);
945 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
947 int32_t rule_to_delete_index, sub_rule_index;
949 uint8_t sub_rule_depth;
951 * Check input arguments. Note: IP must be a positive integer of 32
952 * bits in length therefore it need not be checked.
954 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
958 ip_masked = ip & depth_to_mask(depth);
961 * Find the index of the input rule, that needs to be deleted, in the
964 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
967 * Check if rule_to_delete_index was found. If no rule was found the
968 * function rule_find returns -EINVAL.
970 if (rule_to_delete_index < 0)
973 /* Delete the rule from the rule table. */
974 rule_delete(lpm, rule_to_delete_index, depth);
977 * Find rule to replace the rule_to_delete. If there is no rule to
978 * replace the rule_to_delete we return -1 and invalidate the table
979 * entries associated with this rule.
982 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
985 * If the input depth value is less than 25 use function
986 * delete_depth_small otherwise use delete_depth_big.
988 if (depth <= MAX_DEPTH_TBL24) {
989 return delete_depth_small(lpm, ip_masked, depth,
990 sub_rule_index, sub_rule_depth);
992 else { /* If depth > MAX_DEPTH_TBL24 */
993 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
998 * Delete all rules from the LPM table.
1001 rte_lpm_delete_all(struct rte_lpm *lpm)
1003 /* Zero rule information. */
1004 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1007 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1010 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1012 /* Delete all rules form the rules table. */
1013 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);