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_tbl_entry) != 2);
164 /* Check user arguments. */
165 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
170 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
172 /* Determine the amount of memory to allocate. */
173 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
175 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
177 /* guarantee there's no existing */
178 TAILQ_FOREACH(te, lpm_list, next) {
179 lpm = (struct rte_lpm *) te->data;
180 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
186 /* allocate tailq entry */
187 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
189 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
193 /* Allocate memory to store the LPM data structures. */
194 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
195 RTE_CACHE_LINE_SIZE, socket_id);
197 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
202 /* Save user arguments. */
203 lpm->max_rules = max_rules;
204 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
206 te->data = (void *) lpm;
208 TAILQ_INSERT_TAIL(lpm_list, te, 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 struct rte_lpm_list *lpm_list;
223 struct rte_tailq_entry *te;
225 /* Check user arguments. */
229 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
231 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
233 /* find our tailq entry */
234 TAILQ_FOREACH(te, lpm_list, next) {
235 if (te->data == (void *) lpm)
239 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
243 TAILQ_REMOVE(lpm_list, te, next);
245 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
252 * Adds a rule to the rule table.
254 * NOTE: The rule table is split into 32 groups. Each group contains rules that
255 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
256 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
257 * to refer to depth 1 because even though the depth range is 1 - 32, depths
258 * are stored in the rule table from 0 - 31.
259 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
261 static inline int32_t
262 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
265 uint32_t rule_gindex, rule_index, last_rule;
270 /* Scan through rule group to see if rule already exists. */
271 if (lpm->rule_info[depth - 1].used_rules > 0) {
273 /* rule_gindex stands for rule group index. */
274 rule_gindex = lpm->rule_info[depth - 1].first_rule;
275 /* Initialise rule_index to point to start of rule group. */
276 rule_index = rule_gindex;
277 /* Last rule = Last used rule in this rule group. */
278 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
280 for (; rule_index < last_rule; rule_index++) {
282 /* If rule already exists update its next_hop and return. */
283 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
284 lpm->rules_tbl[rule_index].next_hop = next_hop;
290 if (rule_index == lpm->max_rules)
293 /* Calculate the position in which the rule will be stored. */
296 for (i = depth - 1; i > 0; i--) {
297 if (lpm->rule_info[i - 1].used_rules > 0) {
298 rule_index = lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules;
302 if (rule_index == lpm->max_rules)
305 lpm->rule_info[depth - 1].first_rule = rule_index;
308 /* Make room for the new rule in the array. */
309 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
310 if (lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
313 if (lpm->rule_info[i - 1].used_rules > 0) {
314 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules]
315 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
316 lpm->rule_info[i - 1].first_rule++;
320 /* Add the new rule. */
321 lpm->rules_tbl[rule_index].ip = ip_masked;
322 lpm->rules_tbl[rule_index].next_hop = next_hop;
324 /* Increment the used rules counter for this rule group. */
325 lpm->rule_info[depth - 1].used_rules++;
331 * Delete a rule from the rule table.
332 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
335 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
341 lpm->rules_tbl[rule_index] = lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
342 + lpm->rule_info[depth - 1].used_rules - 1];
344 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
345 if (lpm->rule_info[i].used_rules > 0) {
346 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
347 lpm->rules_tbl[lpm->rule_info[i].first_rule + lpm->rule_info[i].used_rules - 1];
348 lpm->rule_info[i].first_rule--;
352 lpm->rule_info[depth - 1].used_rules--;
356 * Finds a rule in rule table.
357 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
359 static inline int32_t
360 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
362 uint32_t rule_gindex, last_rule, rule_index;
366 rule_gindex = lpm->rule_info[depth - 1].first_rule;
367 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
369 /* Scan used rules at given depth to find rule. */
370 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
371 /* If rule is found return the rule index. */
372 if (lpm->rules_tbl[rule_index].ip == ip_masked)
376 /* If rule is not found return -EINVAL. */
381 * Find, clean and allocate a tbl8.
383 static inline int32_t
384 tbl8_alloc(struct rte_lpm_tbl_entry *tbl8)
386 uint32_t group_idx; /* tbl8 group index. */
387 struct rte_lpm_tbl_entry *tbl8_entry;
389 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
390 for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
392 tbl8_entry = &tbl8[group_idx *
393 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
394 /* If a free tbl8 group is found clean it and set as VALID. */
395 if (!tbl8_entry->valid_group) {
396 memset(&tbl8_entry[0], 0,
397 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
398 sizeof(tbl8_entry[0]));
400 tbl8_entry->valid_group = VALID;
402 /* Return group index for allocated tbl8 group. */
407 /* If there are no tbl8 groups free then return error. */
412 tbl8_free(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
414 /* Set tbl8 group invalid*/
415 tbl8[tbl8_group_start].valid_group = INVALID;
418 static inline int32_t
419 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
422 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
424 /* Calculate the index into Table24. */
425 tbl24_index = ip >> 8;
426 tbl24_range = depth_to_range(depth);
428 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
430 * For invalid OR valid and non-extended tbl 24 entries set
433 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
434 lpm->tbl24[i].depth <= depth)) {
436 struct rte_lpm_tbl_entry new_tbl24_entry = {
437 { .next_hop = next_hop, },
443 /* Setting tbl24 entry in one go to avoid race
446 lpm->tbl24[i] = new_tbl24_entry;
451 if (lpm->tbl24[i].valid_group == 1) {
452 /* If tbl24 entry is valid and extended calculate the
455 tbl8_index = lpm->tbl24[i].group_idx *
456 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
457 tbl8_group_end = tbl8_index +
458 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
460 for (j = tbl8_index; j < tbl8_group_end; j++) {
461 if (!lpm->tbl8[j].valid ||
462 lpm->tbl8[j].depth <= depth) {
463 struct rte_lpm_tbl_entry
466 .valid_group = VALID,
468 .next_hop = next_hop,
472 * Setting tbl8 entry in one go to avoid
475 lpm->tbl8[j] = new_tbl8_entry;
486 static inline int32_t
487 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
490 uint32_t tbl24_index;
491 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
494 tbl24_index = (ip_masked >> 8);
495 tbl8_range = depth_to_range(depth);
497 if (!lpm->tbl24[tbl24_index].valid) {
498 /* Search for a free tbl8 group. */
499 tbl8_group_index = tbl8_alloc(lpm->tbl8);
501 /* Check tbl8 allocation was successful. */
502 if (tbl8_group_index < 0) {
503 return tbl8_group_index;
506 /* Find index into tbl8 and range. */
507 tbl8_index = (tbl8_group_index *
508 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
511 /* Set tbl8 entry. */
512 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
513 lpm->tbl8[i].depth = depth;
514 lpm->tbl8[i].next_hop = next_hop;
515 lpm->tbl8[i].valid = VALID;
519 * Update tbl24 entry to point to new tbl8 entry. Note: The
520 * ext_flag and tbl8_index need to be updated simultaneously,
521 * so assign whole structure in one go
524 struct rte_lpm_tbl_entry new_tbl24_entry = {
525 { .group_idx = (uint8_t)tbl8_group_index, },
531 lpm->tbl24[tbl24_index] = new_tbl24_entry;
533 }/* If valid entry but not extended calculate the index into Table8. */
534 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
535 /* Search for free tbl8 group. */
536 tbl8_group_index = tbl8_alloc(lpm->tbl8);
538 if (tbl8_group_index < 0) {
539 return tbl8_group_index;
542 tbl8_group_start = tbl8_group_index *
543 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
544 tbl8_group_end = tbl8_group_start +
545 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
547 /* Populate new tbl8 with tbl24 value. */
548 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
549 lpm->tbl8[i].valid = VALID;
550 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
551 lpm->tbl8[i].next_hop =
552 lpm->tbl24[tbl24_index].next_hop;
555 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
557 /* Insert new rule into the tbl8 entry. */
558 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
559 if (!lpm->tbl8[i].valid ||
560 lpm->tbl8[i].depth <= depth) {
561 lpm->tbl8[i].valid = VALID;
562 lpm->tbl8[i].depth = depth;
563 lpm->tbl8[i].next_hop = next_hop;
570 * Update tbl24 entry to point to new tbl8 entry. Note: The
571 * ext_flag and tbl8_index need to be updated simultaneously,
572 * so assign whole structure in one go.
575 struct rte_lpm_tbl_entry new_tbl24_entry = {
576 { .group_idx = (uint8_t)tbl8_group_index, },
582 lpm->tbl24[tbl24_index] = new_tbl24_entry;
586 * If it is valid, extended entry calculate the index into tbl8.
588 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
589 tbl8_group_start = tbl8_group_index *
590 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
591 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
593 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
595 if (!lpm->tbl8[i].valid ||
596 lpm->tbl8[i].depth <= depth) {
597 struct rte_lpm_tbl_entry new_tbl8_entry = {
600 .next_hop = next_hop,
601 .valid_group = lpm->tbl8[i].valid_group,
605 * Setting tbl8 entry in one go to avoid race
608 lpm->tbl8[i] = new_tbl8_entry;
622 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
625 int32_t rule_index, status = 0;
628 /* Check user arguments. */
629 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
632 ip_masked = ip & depth_to_mask(depth);
634 /* Add the rule to the rule table. */
635 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
637 /* If the is no space available for new rule return error. */
638 if (rule_index < 0) {
642 if (depth <= MAX_DEPTH_TBL24) {
643 status = add_depth_small(lpm, ip_masked, depth, next_hop);
645 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
646 status = add_depth_big(lpm, ip_masked, depth, next_hop);
649 * If add fails due to exhaustion of tbl8 extensions delete
650 * rule that was added to rule table.
653 rule_delete(lpm, rule_index, depth);
663 * Look for a rule in the high-level rules table
666 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
672 /* Check user arguments. */
674 (next_hop == NULL) ||
675 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
678 /* Look for the rule using rule_find. */
679 ip_masked = ip & depth_to_mask(depth);
680 rule_index = rule_find(lpm, ip_masked, depth);
682 if (rule_index >= 0) {
683 *next_hop = lpm->rules_tbl[rule_index].next_hop;
687 /* If rule is not found return 0. */
691 static inline int32_t
692 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
698 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
699 ip_masked = ip & depth_to_mask(prev_depth);
701 rule_index = rule_find(lpm, ip_masked, prev_depth);
703 if (rule_index >= 0) {
704 *sub_rule_depth = prev_depth;
712 static inline int32_t
713 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
714 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
716 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
718 /* Calculate the range and index into Table24. */
719 tbl24_range = depth_to_range(depth);
720 tbl24_index = (ip_masked >> 8);
723 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
724 * and a positive number indicates a sub_rule_index.
726 if (sub_rule_index < 0) {
728 * If no replacement rule exists then invalidate entries
729 * associated with this rule.
731 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
733 if (lpm->tbl24[i].valid_group == 0 &&
734 lpm->tbl24[i].depth <= depth ) {
735 lpm->tbl24[i].valid = INVALID;
736 } else if (lpm->tbl24[i].valid_group == 1) {
738 * If TBL24 entry is extended, then there has
739 * to be a rule with depth >= 25 in the
740 * associated TBL8 group.
743 tbl8_group_index = lpm->tbl24[i].group_idx;
744 tbl8_index = tbl8_group_index *
745 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
747 for (j = tbl8_index; j < (tbl8_index +
748 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
750 if (lpm->tbl8[j].depth <= depth)
751 lpm->tbl8[j].valid = INVALID;
758 * If a replacement rule exists then modify entries
759 * associated with this rule.
762 struct rte_lpm_tbl_entry new_tbl24_entry = {
763 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
766 .depth = sub_rule_depth,
769 struct rte_lpm_tbl_entry new_tbl8_entry = {
771 .valid_group = VALID,
772 .depth = sub_rule_depth,
773 .next_hop = lpm->rules_tbl
774 [sub_rule_index].next_hop,
777 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
779 if (lpm->tbl24[i].valid_group == 0 &&
780 lpm->tbl24[i].depth <= depth ) {
781 lpm->tbl24[i] = new_tbl24_entry;
782 } else if (lpm->tbl24[i].valid_group == 1) {
784 * If TBL24 entry is extended, then there has
785 * to be a rule with depth >= 25 in the
786 * associated TBL8 group.
789 tbl8_group_index = lpm->tbl24[i].group_idx;
790 tbl8_index = tbl8_group_index *
791 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
793 for (j = tbl8_index; j < (tbl8_index +
794 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
796 if (lpm->tbl8[j].depth <= depth)
797 lpm->tbl8[j] = new_tbl8_entry;
807 * Checks if table 8 group can be recycled.
809 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
810 * Return of -EINVAL means tbl8 is empty and thus can be recycled
811 * Return of value > -1 means tbl8 is in use but has all the same values and
812 * thus can be recycled
814 static inline int32_t
815 tbl8_recycle_check(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
817 uint32_t tbl8_group_end, i;
818 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
821 * Check the first entry of the given tbl8. If it is invalid we know
822 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
823 * (As they would affect all entries in a tbl8) and thus this table
824 * can not be recycled.
826 if (tbl8[tbl8_group_start].valid) {
828 * If first entry is valid check if the depth is less than 24
829 * and if so check the rest of the entries to verify that they
830 * are all of this depth.
832 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
833 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
837 tbl8[tbl8_group_start].depth) {
842 /* If all entries are the same return the tb8 index */
843 return tbl8_group_start;
849 * If the first entry is invalid check if the rest of the entries in
850 * the tbl8 are invalid.
852 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
856 /* If no valid entries are found then return -EINVAL. */
860 static inline int32_t
861 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
862 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
864 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
866 int32_t tbl8_recycle_index;
869 * Calculate the index into tbl24 and range. Note: All depths larger
870 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
872 tbl24_index = ip_masked >> 8;
874 /* Calculate the index into tbl8 and range. */
875 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
876 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
877 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
878 tbl8_range = depth_to_range(depth);
880 if (sub_rule_index < 0) {
882 * Loop through the range of entries on tbl8 for which the
883 * rule_to_delete must be removed or modified.
885 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
886 if (lpm->tbl8[i].depth <= depth)
887 lpm->tbl8[i].valid = INVALID;
891 /* Set new tbl8 entry. */
892 struct rte_lpm_tbl_entry new_tbl8_entry = {
894 .depth = sub_rule_depth,
895 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
896 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
900 * Loop through the range of entries on tbl8 for which the
901 * rule_to_delete must be modified.
903 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
904 if (lpm->tbl8[i].depth <= depth)
905 lpm->tbl8[i] = new_tbl8_entry;
910 * Check if there are any valid entries in this tbl8 group. If all
911 * tbl8 entries are invalid we can free the tbl8 and invalidate the
912 * associated tbl24 entry.
915 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
917 if (tbl8_recycle_index == -EINVAL){
918 /* Set tbl24 before freeing tbl8 to avoid race condition. */
919 lpm->tbl24[tbl24_index].valid = 0;
920 tbl8_free(lpm->tbl8, tbl8_group_start);
922 else if (tbl8_recycle_index > -1) {
923 /* Update tbl24 entry. */
924 struct rte_lpm_tbl_entry new_tbl24_entry = {
925 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
928 .depth = lpm->tbl8[tbl8_recycle_index].depth,
931 /* Set tbl24 before freeing tbl8 to avoid race condition. */
932 lpm->tbl24[tbl24_index] = new_tbl24_entry;
933 tbl8_free(lpm->tbl8, tbl8_group_start);
943 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
945 int32_t rule_to_delete_index, sub_rule_index;
947 uint8_t sub_rule_depth;
949 * Check input arguments. Note: IP must be a positive integer of 32
950 * bits in length therefore it need not be checked.
952 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
956 ip_masked = ip & depth_to_mask(depth);
959 * Find the index of the input rule, that needs to be deleted, in the
962 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
965 * Check if rule_to_delete_index was found. If no rule was found the
966 * function rule_find returns -EINVAL.
968 if (rule_to_delete_index < 0)
971 /* Delete the rule from the rule table. */
972 rule_delete(lpm, rule_to_delete_index, depth);
975 * Find rule to replace the rule_to_delete. If there is no rule to
976 * replace the rule_to_delete we return -1 and invalidate the table
977 * entries associated with this rule.
980 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
983 * If the input depth value is less than 25 use function
984 * delete_depth_small otherwise use delete_depth_big.
986 if (depth <= MAX_DEPTH_TBL24) {
987 return delete_depth_small(lpm, ip_masked, depth,
988 sub_rule_index, sub_rule_depth);
990 else { /* If depth > MAX_DEPTH_TBL24 */
991 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
996 * Delete all rules from the LPM table.
999 rte_lpm_delete_all(struct rte_lpm *lpm)
1001 /* Zero rule information. */
1002 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1005 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1008 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1010 /* Delete all rules form the rules table. */
1011 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);