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>
48 #include <rte_tailq.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_string_fns.h>
53 #include <rte_errno.h>
54 #include <rte_rwlock.h>
55 #include <rte_spinlock.h>
59 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
61 #define MAX_DEPTH_TBL24 24
68 /* Macro to enable/disable run-time checks. */
69 #if defined(RTE_LIBRTE_LPM_DEBUG)
70 #include <rte_debug.h>
71 #define VERIFY_DEPTH(depth) do { \
72 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
73 rte_panic("LPM: Invalid depth (%u) at line %d", \
74 (unsigned)(depth), __LINE__); \
77 #define VERIFY_DEPTH(depth)
81 * Converts a given depth value to its corresponding mask value.
83 * depth (IN) : range = 1 - 32
84 * mask (OUT) : 32bit mask
86 static uint32_t __attribute__((pure))
87 depth_to_mask(uint8_t depth)
91 /* To calculate a mask start with a 1 on the left hand side and right
92 * shift while populating the left hand side with 1's
94 return (int)0x80000000 >> (depth - 1);
98 * Converts given depth value to its corresponding range value.
100 static inline uint32_t __attribute__((pure))
101 depth_to_range(uint8_t depth)
106 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
108 if (depth <= MAX_DEPTH_TBL24)
109 return 1 << (MAX_DEPTH_TBL24 - depth);
111 /* Else if depth is greater than 24 */
112 return (1 << (RTE_LPM_MAX_DEPTH - depth));
116 * Find an existing lpm table and return a pointer to it.
119 rte_lpm_find_existing(const char *name)
121 struct rte_lpm *l = NULL;
122 struct rte_tailq_entry *te;
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,
127 rte_lpm_list)) == NULL) {
128 rte_errno = E_RTE_NO_TAILQ;
132 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
133 TAILQ_FOREACH(te, lpm_list, next) {
134 l = (struct rte_lpm *) te->data;
135 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
138 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
149 * Allocates memory for LPM object
152 rte_lpm_create(const char *name, int socket_id, int max_rules,
153 __rte_unused int flags)
155 char mem_name[RTE_LPM_NAMESIZE];
156 struct rte_lpm *lpm = NULL;
157 struct rte_tailq_entry *te;
159 struct rte_lpm_list *lpm_list;
161 /* check that we have an initialised tail queue */
162 if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM,
163 rte_lpm_list)) == NULL) {
164 rte_errno = E_RTE_NO_TAILQ;
168 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
169 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
171 /* Check user arguments. */
172 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
177 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
179 /* Determine the amount of memory to allocate. */
180 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
182 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
184 /* guarantee there's no existing */
185 TAILQ_FOREACH(te, lpm_list, next) {
186 lpm = (struct rte_lpm *) te->data;
187 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
193 /* allocate tailq entry */
194 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
196 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
200 /* Allocate memory to store the LPM data structures. */
201 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
202 RTE_CACHE_LINE_SIZE, socket_id);
204 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
209 /* Save user arguments. */
210 lpm->max_rules = max_rules;
211 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
213 te->data = (void *) lpm;
215 TAILQ_INSERT_TAIL(lpm_list, te, next);
218 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
224 * Deallocates memory for given LPM table.
227 rte_lpm_free(struct rte_lpm *lpm)
229 struct rte_lpm_list *lpm_list;
230 struct rte_tailq_entry *te;
232 /* Check user arguments. */
236 /* check that we have an initialised tail queue */
238 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
239 rte_errno = E_RTE_NO_TAILQ;
243 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
245 /* find our tailq entry */
246 TAILQ_FOREACH(te, lpm_list, next) {
247 if (te->data == (void *) lpm)
251 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
255 TAILQ_REMOVE(lpm_list, te, next);
257 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
264 * Adds a rule to the rule table.
266 * NOTE: The rule table is split into 32 groups. Each group contains rules that
267 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
268 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
269 * to refer to depth 1 because even though the depth range is 1 - 32, depths
270 * are stored in the rule table from 0 - 31.
271 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
273 static inline int32_t
274 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
277 uint32_t rule_gindex, rule_index, last_rule;
282 /* Scan through rule group to see if rule already exists. */
283 if (lpm->rule_info[depth - 1].used_rules > 0) {
285 /* rule_gindex stands for rule group index. */
286 rule_gindex = lpm->rule_info[depth - 1].first_rule;
287 /* Initialise rule_index to point to start of rule group. */
288 rule_index = rule_gindex;
289 /* Last rule = Last used rule in this rule group. */
290 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
292 for (; rule_index < last_rule; rule_index++) {
294 /* If rule already exists update its next_hop and return. */
295 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
296 lpm->rules_tbl[rule_index].next_hop = next_hop;
302 /* Calculate the position in which the rule will be stored. */
305 for (i = depth - 1; i > 0; i--) {
306 if (lpm->rule_info[i - 1].used_rules > 0) {
307 rule_index = lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules;
311 if (rule_index == lpm->max_rules)
314 lpm->rule_info[depth - 1].first_rule = rule_index;
317 /* Make room for the new rule in the array. */
318 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
319 if (lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
322 if (lpm->rule_info[i - 1].used_rules > 0) {
323 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules]
324 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
325 lpm->rule_info[i - 1].first_rule++;
329 /* Add the new rule. */
330 lpm->rules_tbl[rule_index].ip = ip_masked;
331 lpm->rules_tbl[rule_index].next_hop = next_hop;
333 /* Increment the used rules counter for this rule group. */
334 lpm->rule_info[depth - 1].used_rules++;
340 * Delete a rule from the rule table.
341 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
344 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
350 lpm->rules_tbl[rule_index] = lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
351 + lpm->rule_info[depth - 1].used_rules - 1];
353 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
354 if (lpm->rule_info[i].used_rules > 0) {
355 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
356 lpm->rules_tbl[lpm->rule_info[i].first_rule + lpm->rule_info[i].used_rules - 1];
357 lpm->rule_info[i].first_rule--;
361 lpm->rule_info[depth - 1].used_rules--;
365 * Finds a rule in rule table.
366 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
368 static inline int32_t
369 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
371 uint32_t rule_gindex, last_rule, rule_index;
375 rule_gindex = lpm->rule_info[depth - 1].first_rule;
376 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
378 /* Scan used rules at given depth to find rule. */
379 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
380 /* If rule is found return the rule index. */
381 if (lpm->rules_tbl[rule_index].ip == ip_masked)
385 /* If rule is not found return -E_RTE_NO_TAILQ. */
386 return -E_RTE_NO_TAILQ;
390 * Find, clean and allocate a tbl8.
392 static inline int32_t
393 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
395 uint32_t tbl8_gindex; /* tbl8 group index. */
396 struct rte_lpm_tbl8_entry *tbl8_entry;
398 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
399 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
401 tbl8_entry = &tbl8[tbl8_gindex *
402 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
403 /* If a free tbl8 group is found clean it and set as VALID. */
404 if (!tbl8_entry->valid_group) {
405 memset(&tbl8_entry[0], 0,
406 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
407 sizeof(tbl8_entry[0]));
409 tbl8_entry->valid_group = VALID;
411 /* Return group index for allocated tbl8 group. */
416 /* If there are no tbl8 groups free then return error. */
421 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
423 /* Set tbl8 group invalid*/
424 tbl8[tbl8_group_start].valid_group = INVALID;
427 static inline int32_t
428 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
431 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
433 /* Calculate the index into Table24. */
434 tbl24_index = ip >> 8;
435 tbl24_range = depth_to_range(depth);
437 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
439 * For invalid OR valid and non-extended tbl 24 entries set
442 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
443 lpm->tbl24[i].depth <= depth)) {
445 struct rte_lpm_tbl24_entry new_tbl24_entry = {
446 { .next_hop = next_hop, },
452 /* Setting tbl24 entry in one go to avoid race
454 lpm->tbl24[i] = new_tbl24_entry;
459 /* If tbl24 entry is valid and extended calculate the index
461 tbl8_index = lpm->tbl24[i].tbl8_gindex *
462 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
463 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
465 for (j = tbl8_index; j < tbl8_group_end; j++) {
466 if (!lpm->tbl8[j].valid ||
467 lpm->tbl8[j].depth <= depth) {
468 struct rte_lpm_tbl8_entry new_tbl8_entry = {
470 .valid_group = VALID,
472 .next_hop = next_hop,
476 * Setting tbl8 entry in one go to avoid race
479 lpm->tbl8[j] = new_tbl8_entry;
489 static inline int32_t
490 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
493 uint32_t tbl24_index;
494 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
497 tbl24_index = (ip_masked >> 8);
498 tbl8_range = depth_to_range(depth);
500 if (!lpm->tbl24[tbl24_index].valid) {
501 /* Search for a free tbl8 group. */
502 tbl8_group_index = tbl8_alloc(lpm->tbl8);
504 /* Check tbl8 allocation was successful. */
505 if (tbl8_group_index < 0) {
506 return tbl8_group_index;
509 /* Find index into tbl8 and range. */
510 tbl8_index = (tbl8_group_index *
511 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
514 /* Set tbl8 entry. */
515 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
516 lpm->tbl8[i].depth = depth;
517 lpm->tbl8[i].next_hop = next_hop;
518 lpm->tbl8[i].valid = VALID;
522 * Update tbl24 entry to point to new tbl8 entry. Note: The
523 * ext_flag and tbl8_index need to be updated simultaneously,
524 * so assign whole structure in one go
527 struct rte_lpm_tbl24_entry new_tbl24_entry = {
528 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
534 lpm->tbl24[tbl24_index] = new_tbl24_entry;
536 }/* If valid entry but not extended calculate the index into Table8. */
537 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
538 /* Search for free tbl8 group. */
539 tbl8_group_index = tbl8_alloc(lpm->tbl8);
541 if (tbl8_group_index < 0) {
542 return tbl8_group_index;
545 tbl8_group_start = tbl8_group_index *
546 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
547 tbl8_group_end = tbl8_group_start +
548 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
550 /* Populate new tbl8 with tbl24 value. */
551 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
552 lpm->tbl8[i].valid = VALID;
553 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
554 lpm->tbl8[i].next_hop =
555 lpm->tbl24[tbl24_index].next_hop;
558 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
560 /* Insert new rule into the tbl8 entry. */
561 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
562 if (!lpm->tbl8[i].valid ||
563 lpm->tbl8[i].depth <= depth) {
564 lpm->tbl8[i].valid = VALID;
565 lpm->tbl8[i].depth = depth;
566 lpm->tbl8[i].next_hop = next_hop;
573 * Update tbl24 entry to point to new tbl8 entry. Note: The
574 * ext_flag and tbl8_index need to be updated simultaneously,
575 * so assign whole structure in one go.
578 struct rte_lpm_tbl24_entry new_tbl24_entry = {
579 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
585 lpm->tbl24[tbl24_index] = new_tbl24_entry;
589 * If it is valid, extended entry calculate the index into tbl8.
591 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
592 tbl8_group_start = tbl8_group_index *
593 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
594 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
596 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
598 if (!lpm->tbl8[i].valid ||
599 lpm->tbl8[i].depth <= depth) {
600 struct rte_lpm_tbl8_entry new_tbl8_entry = {
603 .next_hop = next_hop,
604 .valid_group = lpm->tbl8[i].valid_group,
608 * Setting tbl8 entry in one go to avoid race
611 lpm->tbl8[i] = new_tbl8_entry;
625 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
628 int32_t rule_index, status = 0;
631 /* Check user arguments. */
632 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
635 ip_masked = ip & depth_to_mask(depth);
637 /* Add the rule to the rule table. */
638 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
640 /* If the is no space available for new rule return error. */
641 if (rule_index < 0) {
645 if (depth <= MAX_DEPTH_TBL24) {
646 status = add_depth_small(lpm, ip_masked, depth, next_hop);
648 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
649 status = add_depth_big(lpm, ip_masked, depth, next_hop);
652 * If add fails due to exhaustion of tbl8 extensions delete
653 * rule that was added to rule table.
656 rule_delete(lpm, rule_index, depth);
666 * Look for a rule in the high-level rules table
669 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
675 /* Check user arguments. */
677 (next_hop == NULL) ||
678 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
681 /* Look for the rule using rule_find. */
682 ip_masked = ip & depth_to_mask(depth);
683 rule_index = rule_find(lpm, ip_masked, depth);
685 if (rule_index >= 0) {
686 *next_hop = lpm->rules_tbl[rule_index].next_hop;
690 /* If rule is not found return 0. */
694 static inline int32_t
695 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
701 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
702 ip_masked = ip & depth_to_mask(prev_depth);
704 rule_index = rule_find(lpm, ip_masked, prev_depth);
706 if (rule_index >= 0) {
707 *sub_rule_depth = prev_depth;
715 static inline int32_t
716 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
717 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
719 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
721 /* Calculate the range and index into Table24. */
722 tbl24_range = depth_to_range(depth);
723 tbl24_index = (ip_masked >> 8);
726 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
727 * and a positive number indicates a sub_rule_index.
729 if (sub_rule_index < 0) {
731 * If no replacement rule exists then invalidate entries
732 * associated with this rule.
734 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
736 if (lpm->tbl24[i].ext_entry == 0 &&
737 lpm->tbl24[i].depth <= depth ) {
738 lpm->tbl24[i].valid = INVALID;
742 * If TBL24 entry is extended, then there has
743 * to be a rule with depth >= 25 in the
744 * associated TBL8 group.
747 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
748 tbl8_index = tbl8_group_index *
749 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
751 for (j = tbl8_index; j < (tbl8_index +
752 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
754 if (lpm->tbl8[j].depth <= depth)
755 lpm->tbl8[j].valid = INVALID;
762 * If a replacement rule exists then modify entries
763 * associated with this rule.
766 struct rte_lpm_tbl24_entry new_tbl24_entry = {
767 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
770 .depth = sub_rule_depth,
773 struct rte_lpm_tbl8_entry new_tbl8_entry = {
775 .depth = sub_rule_depth,
776 .next_hop = lpm->rules_tbl
777 [sub_rule_index].next_hop,
780 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
782 if (lpm->tbl24[i].ext_entry == 0 &&
783 lpm->tbl24[i].depth <= depth ) {
784 lpm->tbl24[i] = new_tbl24_entry;
788 * If TBL24 entry is extended, then there has
789 * to be a rule with depth >= 25 in the
790 * associated TBL8 group.
793 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
794 tbl8_index = tbl8_group_index *
795 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
797 for (j = tbl8_index; j < (tbl8_index +
798 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
800 if (lpm->tbl8[j].depth <= depth)
801 lpm->tbl8[j] = new_tbl8_entry;
811 * Checks if table 8 group can be recycled.
813 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
814 * Return of -EINVAL means tbl8 is empty and thus can be recycled
815 * Return of value > -1 means tbl8 is in use but has all the same values and
816 * thus can be recycled
818 static inline int32_t
819 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
821 uint32_t tbl8_group_end, i;
822 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
825 * Check the first entry of the given tbl8. If it is invalid we know
826 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
827 * (As they would affect all entries in a tbl8) and thus this table
828 * can not be recycled.
830 if (tbl8[tbl8_group_start].valid) {
832 * If first entry is valid check if the depth is less than 24
833 * and if so check the rest of the entries to verify that they
834 * are all of this depth.
836 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
837 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
841 tbl8[tbl8_group_start].depth) {
846 /* If all entries are the same return the tb8 index */
847 return tbl8_group_start;
853 * If the first entry is invalid check if the rest of the entries in
854 * the tbl8 are invalid.
856 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
860 /* If no valid entries are found then return -EINVAL. */
864 static inline int32_t
865 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
866 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
868 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
870 int32_t tbl8_recycle_index;
873 * Calculate the index into tbl24 and range. Note: All depths larger
874 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
876 tbl24_index = ip_masked >> 8;
878 /* Calculate the index into tbl8 and range. */
879 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
880 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
881 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
882 tbl8_range = depth_to_range(depth);
884 if (sub_rule_index < 0) {
886 * Loop through the range of entries on tbl8 for which the
887 * rule_to_delete must be removed or modified.
889 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
890 if (lpm->tbl8[i].depth <= depth)
891 lpm->tbl8[i].valid = INVALID;
895 /* Set new tbl8 entry. */
896 struct rte_lpm_tbl8_entry new_tbl8_entry = {
898 .depth = sub_rule_depth,
899 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
900 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
904 * Loop through the range of entries on tbl8 for which the
905 * rule_to_delete must be modified.
907 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
908 if (lpm->tbl8[i].depth <= depth)
909 lpm->tbl8[i] = new_tbl8_entry;
914 * Check if there are any valid entries in this tbl8 group. If all
915 * tbl8 entries are invalid we can free the tbl8 and invalidate the
916 * associated tbl24 entry.
919 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
921 if (tbl8_recycle_index == -EINVAL){
922 /* Set tbl24 before freeing tbl8 to avoid race condition. */
923 lpm->tbl24[tbl24_index].valid = 0;
924 tbl8_free(lpm->tbl8, tbl8_group_start);
926 else if (tbl8_recycle_index > -1) {
927 /* Update tbl24 entry. */
928 struct rte_lpm_tbl24_entry new_tbl24_entry = {
929 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
932 .depth = lpm->tbl8[tbl8_recycle_index].depth,
935 /* Set tbl24 before freeing tbl8 to avoid race condition. */
936 lpm->tbl24[tbl24_index] = new_tbl24_entry;
937 tbl8_free(lpm->tbl8, tbl8_group_start);
947 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
949 int32_t rule_to_delete_index, sub_rule_index;
951 uint8_t sub_rule_depth;
953 * Check input arguments. Note: IP must be a positive integer of 32
954 * bits in length therefore it need not be checked.
956 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
960 ip_masked = ip & depth_to_mask(depth);
963 * Find the index of the input rule, that needs to be deleted, in the
966 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
969 * Check if rule_to_delete_index was found. If no rule was found the
970 * function rule_find returns -E_RTE_NO_TAILQ.
972 if (rule_to_delete_index < 0)
973 return -E_RTE_NO_TAILQ;
975 /* Delete the rule from the rule table. */
976 rule_delete(lpm, rule_to_delete_index, depth);
979 * Find rule to replace the rule_to_delete. If there is no rule to
980 * replace the rule_to_delete we return -1 and invalidate the table
981 * entries associated with this rule.
984 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
987 * If the input depth value is less than 25 use function
988 * delete_depth_small otherwise use delete_depth_big.
990 if (depth <= MAX_DEPTH_TBL24) {
991 return delete_depth_small(lpm, ip_masked, depth,
992 sub_rule_index, sub_rule_depth);
994 else { /* If depth > MAX_DEPTH_TBL24 */
995 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
1000 * Delete all rules from the LPM table.
1003 rte_lpm_delete_all(struct rte_lpm *lpm)
1005 /* Zero rule information. */
1006 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1009 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1012 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1014 /* Delete all rules form the rules table. */
1015 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);