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
446 lpm->tbl24[i] = new_tbl24_entry;
451 /* If tbl24 entry is valid and extended calculate the index
453 tbl8_index = lpm->tbl24[i].tbl8_gindex *
454 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
455 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
457 for (j = tbl8_index; j < tbl8_group_end; j++) {
458 if (!lpm->tbl8[j].valid ||
459 lpm->tbl8[j].depth <= depth) {
460 struct rte_lpm_tbl8_entry new_tbl8_entry = {
462 .valid_group = VALID,
464 .next_hop = next_hop,
468 * Setting tbl8 entry in one go to avoid race
471 lpm->tbl8[j] = new_tbl8_entry;
481 static inline int32_t
482 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
485 uint32_t tbl24_index;
486 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
489 tbl24_index = (ip_masked >> 8);
490 tbl8_range = depth_to_range(depth);
492 if (!lpm->tbl24[tbl24_index].valid) {
493 /* Search for a free tbl8 group. */
494 tbl8_group_index = tbl8_alloc(lpm->tbl8);
496 /* Check tbl8 allocation was successful. */
497 if (tbl8_group_index < 0) {
498 return tbl8_group_index;
501 /* Find index into tbl8 and range. */
502 tbl8_index = (tbl8_group_index *
503 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
506 /* Set tbl8 entry. */
507 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
508 lpm->tbl8[i].depth = depth;
509 lpm->tbl8[i].next_hop = next_hop;
510 lpm->tbl8[i].valid = VALID;
514 * Update tbl24 entry to point to new tbl8 entry. Note: The
515 * ext_flag and tbl8_index need to be updated simultaneously,
516 * so assign whole structure in one go
519 struct rte_lpm_tbl24_entry new_tbl24_entry = {
520 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
526 lpm->tbl24[tbl24_index] = new_tbl24_entry;
528 }/* If valid entry but not extended calculate the index into Table8. */
529 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
530 /* Search for free tbl8 group. */
531 tbl8_group_index = tbl8_alloc(lpm->tbl8);
533 if (tbl8_group_index < 0) {
534 return tbl8_group_index;
537 tbl8_group_start = tbl8_group_index *
538 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
539 tbl8_group_end = tbl8_group_start +
540 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
542 /* Populate new tbl8 with tbl24 value. */
543 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
544 lpm->tbl8[i].valid = VALID;
545 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
546 lpm->tbl8[i].next_hop =
547 lpm->tbl24[tbl24_index].next_hop;
550 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
552 /* Insert new rule into the tbl8 entry. */
553 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
554 if (!lpm->tbl8[i].valid ||
555 lpm->tbl8[i].depth <= depth) {
556 lpm->tbl8[i].valid = VALID;
557 lpm->tbl8[i].depth = depth;
558 lpm->tbl8[i].next_hop = next_hop;
565 * Update tbl24 entry to point to new tbl8 entry. Note: The
566 * ext_flag and tbl8_index need to be updated simultaneously,
567 * so assign whole structure in one go.
570 struct rte_lpm_tbl24_entry new_tbl24_entry = {
571 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
577 lpm->tbl24[tbl24_index] = new_tbl24_entry;
581 * If it is valid, extended entry calculate the index into tbl8.
583 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
584 tbl8_group_start = tbl8_group_index *
585 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
586 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
588 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
590 if (!lpm->tbl8[i].valid ||
591 lpm->tbl8[i].depth <= depth) {
592 struct rte_lpm_tbl8_entry new_tbl8_entry = {
595 .next_hop = next_hop,
596 .valid_group = lpm->tbl8[i].valid_group,
600 * Setting tbl8 entry in one go to avoid race
603 lpm->tbl8[i] = new_tbl8_entry;
617 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
620 int32_t rule_index, status = 0;
623 /* Check user arguments. */
624 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
627 ip_masked = ip & depth_to_mask(depth);
629 /* Add the rule to the rule table. */
630 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
632 /* If the is no space available for new rule return error. */
633 if (rule_index < 0) {
637 if (depth <= MAX_DEPTH_TBL24) {
638 status = add_depth_small(lpm, ip_masked, depth, next_hop);
640 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
641 status = add_depth_big(lpm, ip_masked, depth, next_hop);
644 * If add fails due to exhaustion of tbl8 extensions delete
645 * rule that was added to rule table.
648 rule_delete(lpm, rule_index, depth);
658 * Look for a rule in the high-level rules table
661 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
667 /* Check user arguments. */
669 (next_hop == NULL) ||
670 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
673 /* Look for the rule using rule_find. */
674 ip_masked = ip & depth_to_mask(depth);
675 rule_index = rule_find(lpm, ip_masked, depth);
677 if (rule_index >= 0) {
678 *next_hop = lpm->rules_tbl[rule_index].next_hop;
682 /* If rule is not found return 0. */
686 static inline int32_t
687 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
693 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
694 ip_masked = ip & depth_to_mask(prev_depth);
696 rule_index = rule_find(lpm, ip_masked, prev_depth);
698 if (rule_index >= 0) {
699 *sub_rule_depth = prev_depth;
707 static inline int32_t
708 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
709 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
711 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
713 /* Calculate the range and index into Table24. */
714 tbl24_range = depth_to_range(depth);
715 tbl24_index = (ip_masked >> 8);
718 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
719 * and a positive number indicates a sub_rule_index.
721 if (sub_rule_index < 0) {
723 * If no replacement rule exists then invalidate entries
724 * associated with this rule.
726 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
728 if (lpm->tbl24[i].ext_entry == 0 &&
729 lpm->tbl24[i].depth <= depth ) {
730 lpm->tbl24[i].valid = INVALID;
734 * If TBL24 entry is extended, then there has
735 * to be a rule with depth >= 25 in the
736 * associated TBL8 group.
739 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
740 tbl8_index = tbl8_group_index *
741 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
743 for (j = tbl8_index; j < (tbl8_index +
744 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
746 if (lpm->tbl8[j].depth <= depth)
747 lpm->tbl8[j].valid = INVALID;
754 * If a replacement rule exists then modify entries
755 * associated with this rule.
758 struct rte_lpm_tbl24_entry new_tbl24_entry = {
759 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
762 .depth = sub_rule_depth,
765 struct rte_lpm_tbl8_entry new_tbl8_entry = {
767 .depth = sub_rule_depth,
768 .next_hop = lpm->rules_tbl
769 [sub_rule_index].next_hop,
772 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
774 if (lpm->tbl24[i].ext_entry == 0 &&
775 lpm->tbl24[i].depth <= depth ) {
776 lpm->tbl24[i] = new_tbl24_entry;
780 * If TBL24 entry is extended, then there has
781 * to be a rule with depth >= 25 in the
782 * associated TBL8 group.
785 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
786 tbl8_index = tbl8_group_index *
787 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
789 for (j = tbl8_index; j < (tbl8_index +
790 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
792 if (lpm->tbl8[j].depth <= depth)
793 lpm->tbl8[j] = new_tbl8_entry;
803 * Checks if table 8 group can be recycled.
805 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
806 * Return of -EINVAL means tbl8 is empty and thus can be recycled
807 * Return of value > -1 means tbl8 is in use but has all the same values and
808 * thus can be recycled
810 static inline int32_t
811 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
813 uint32_t tbl8_group_end, i;
814 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
817 * Check the first entry of the given tbl8. If it is invalid we know
818 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
819 * (As they would affect all entries in a tbl8) and thus this table
820 * can not be recycled.
822 if (tbl8[tbl8_group_start].valid) {
824 * If first entry is valid check if the depth is less than 24
825 * and if so check the rest of the entries to verify that they
826 * are all of this depth.
828 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
829 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
833 tbl8[tbl8_group_start].depth) {
838 /* If all entries are the same return the tb8 index */
839 return tbl8_group_start;
845 * If the first entry is invalid check if the rest of the entries in
846 * the tbl8 are invalid.
848 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
852 /* If no valid entries are found then return -EINVAL. */
856 static inline int32_t
857 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
858 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
860 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
862 int32_t tbl8_recycle_index;
865 * Calculate the index into tbl24 and range. Note: All depths larger
866 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
868 tbl24_index = ip_masked >> 8;
870 /* Calculate the index into tbl8 and range. */
871 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
872 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
873 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
874 tbl8_range = depth_to_range(depth);
876 if (sub_rule_index < 0) {
878 * Loop through the range of entries on tbl8 for which the
879 * rule_to_delete must be removed or modified.
881 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
882 if (lpm->tbl8[i].depth <= depth)
883 lpm->tbl8[i].valid = INVALID;
887 /* Set new tbl8 entry. */
888 struct rte_lpm_tbl8_entry new_tbl8_entry = {
890 .depth = sub_rule_depth,
891 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
892 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
896 * Loop through the range of entries on tbl8 for which the
897 * rule_to_delete must be modified.
899 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
900 if (lpm->tbl8[i].depth <= depth)
901 lpm->tbl8[i] = new_tbl8_entry;
906 * Check if there are any valid entries in this tbl8 group. If all
907 * tbl8 entries are invalid we can free the tbl8 and invalidate the
908 * associated tbl24 entry.
911 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
913 if (tbl8_recycle_index == -EINVAL){
914 /* Set tbl24 before freeing tbl8 to avoid race condition. */
915 lpm->tbl24[tbl24_index].valid = 0;
916 tbl8_free(lpm->tbl8, tbl8_group_start);
918 else if (tbl8_recycle_index > -1) {
919 /* Update tbl24 entry. */
920 struct rte_lpm_tbl24_entry new_tbl24_entry = {
921 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
924 .depth = lpm->tbl8[tbl8_recycle_index].depth,
927 /* Set tbl24 before freeing tbl8 to avoid race condition. */
928 lpm->tbl24[tbl24_index] = new_tbl24_entry;
929 tbl8_free(lpm->tbl8, tbl8_group_start);
939 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
941 int32_t rule_to_delete_index, sub_rule_index;
943 uint8_t sub_rule_depth;
945 * Check input arguments. Note: IP must be a positive integer of 32
946 * bits in length therefore it need not be checked.
948 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
952 ip_masked = ip & depth_to_mask(depth);
955 * Find the index of the input rule, that needs to be deleted, in the
958 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
961 * Check if rule_to_delete_index was found. If no rule was found the
962 * function rule_find returns -EINVAL.
964 if (rule_to_delete_index < 0)
967 /* Delete the rule from the rule table. */
968 rule_delete(lpm, rule_to_delete_index, depth);
971 * Find rule to replace the rule_to_delete. If there is no rule to
972 * replace the rule_to_delete we return -1 and invalidate the table
973 * entries associated with this rule.
976 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
979 * If the input depth value is less than 25 use function
980 * delete_depth_small otherwise use delete_depth_big.
982 if (depth <= MAX_DEPTH_TBL24) {
983 return delete_depth_small(lpm, ip_masked, depth,
984 sub_rule_index, sub_rule_depth);
986 else { /* If depth > MAX_DEPTH_TBL24 */
987 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
992 * Delete all rules from the LPM table.
995 rte_lpm_delete_all(struct rte_lpm *lpm)
997 /* Zero rule information. */
998 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1001 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1004 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1006 /* Delete all rules form the rules table. */
1007 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);