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 if (rule_index == lpm->max_rules)
305 /* Calculate the position in which the rule will be stored. */
308 for (i = depth - 1; i > 0; i--) {
309 if (lpm->rule_info[i - 1].used_rules > 0) {
310 rule_index = lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules;
314 if (rule_index == lpm->max_rules)
317 lpm->rule_info[depth - 1].first_rule = rule_index;
320 /* Make room for the new rule in the array. */
321 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
322 if (lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
325 if (lpm->rule_info[i - 1].used_rules > 0) {
326 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules]
327 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
328 lpm->rule_info[i - 1].first_rule++;
332 /* Add the new rule. */
333 lpm->rules_tbl[rule_index].ip = ip_masked;
334 lpm->rules_tbl[rule_index].next_hop = next_hop;
336 /* Increment the used rules counter for this rule group. */
337 lpm->rule_info[depth - 1].used_rules++;
343 * Delete a rule from the rule table.
344 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
347 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
353 lpm->rules_tbl[rule_index] = lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
354 + lpm->rule_info[depth - 1].used_rules - 1];
356 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
357 if (lpm->rule_info[i].used_rules > 0) {
358 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
359 lpm->rules_tbl[lpm->rule_info[i].first_rule + lpm->rule_info[i].used_rules - 1];
360 lpm->rule_info[i].first_rule--;
364 lpm->rule_info[depth - 1].used_rules--;
368 * Finds a rule in rule table.
369 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
371 static inline int32_t
372 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
374 uint32_t rule_gindex, last_rule, rule_index;
378 rule_gindex = lpm->rule_info[depth - 1].first_rule;
379 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
381 /* Scan used rules at given depth to find rule. */
382 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
383 /* If rule is found return the rule index. */
384 if (lpm->rules_tbl[rule_index].ip == ip_masked)
388 /* If rule is not found return -E_RTE_NO_TAILQ. */
389 return -E_RTE_NO_TAILQ;
393 * Find, clean and allocate a tbl8.
395 static inline int32_t
396 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
398 uint32_t tbl8_gindex; /* tbl8 group index. */
399 struct rte_lpm_tbl8_entry *tbl8_entry;
401 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
402 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
404 tbl8_entry = &tbl8[tbl8_gindex *
405 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
406 /* If a free tbl8 group is found clean it and set as VALID. */
407 if (!tbl8_entry->valid_group) {
408 memset(&tbl8_entry[0], 0,
409 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
410 sizeof(tbl8_entry[0]));
412 tbl8_entry->valid_group = VALID;
414 /* Return group index for allocated tbl8 group. */
419 /* If there are no tbl8 groups free then return error. */
424 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
426 /* Set tbl8 group invalid*/
427 tbl8[tbl8_group_start].valid_group = INVALID;
430 static inline int32_t
431 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
434 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
436 /* Calculate the index into Table24. */
437 tbl24_index = ip >> 8;
438 tbl24_range = depth_to_range(depth);
440 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
442 * For invalid OR valid and non-extended tbl 24 entries set
445 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
446 lpm->tbl24[i].depth <= depth)) {
448 struct rte_lpm_tbl24_entry new_tbl24_entry = {
449 { .next_hop = next_hop, },
455 /* Setting tbl24 entry in one go to avoid race
457 lpm->tbl24[i] = new_tbl24_entry;
462 /* If tbl24 entry is valid and extended calculate the index
464 tbl8_index = lpm->tbl24[i].tbl8_gindex *
465 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
466 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
468 for (j = tbl8_index; j < tbl8_group_end; j++) {
469 if (!lpm->tbl8[j].valid ||
470 lpm->tbl8[j].depth <= depth) {
471 struct rte_lpm_tbl8_entry new_tbl8_entry = {
473 .valid_group = VALID,
475 .next_hop = next_hop,
479 * Setting tbl8 entry in one go to avoid race
482 lpm->tbl8[j] = new_tbl8_entry;
492 static inline int32_t
493 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
496 uint32_t tbl24_index;
497 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
500 tbl24_index = (ip_masked >> 8);
501 tbl8_range = depth_to_range(depth);
503 if (!lpm->tbl24[tbl24_index].valid) {
504 /* Search for a free tbl8 group. */
505 tbl8_group_index = tbl8_alloc(lpm->tbl8);
507 /* Check tbl8 allocation was successful. */
508 if (tbl8_group_index < 0) {
509 return tbl8_group_index;
512 /* Find index into tbl8 and range. */
513 tbl8_index = (tbl8_group_index *
514 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
517 /* Set tbl8 entry. */
518 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
519 lpm->tbl8[i].depth = depth;
520 lpm->tbl8[i].next_hop = next_hop;
521 lpm->tbl8[i].valid = VALID;
525 * Update tbl24 entry to point to new tbl8 entry. Note: The
526 * ext_flag and tbl8_index need to be updated simultaneously,
527 * so assign whole structure in one go
530 struct rte_lpm_tbl24_entry new_tbl24_entry = {
531 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
537 lpm->tbl24[tbl24_index] = new_tbl24_entry;
539 }/* If valid entry but not extended calculate the index into Table8. */
540 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
541 /* Search for free tbl8 group. */
542 tbl8_group_index = tbl8_alloc(lpm->tbl8);
544 if (tbl8_group_index < 0) {
545 return tbl8_group_index;
548 tbl8_group_start = tbl8_group_index *
549 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
550 tbl8_group_end = tbl8_group_start +
551 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
553 /* Populate new tbl8 with tbl24 value. */
554 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
555 lpm->tbl8[i].valid = VALID;
556 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
557 lpm->tbl8[i].next_hop =
558 lpm->tbl24[tbl24_index].next_hop;
561 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
563 /* Insert new rule into the tbl8 entry. */
564 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
565 if (!lpm->tbl8[i].valid ||
566 lpm->tbl8[i].depth <= depth) {
567 lpm->tbl8[i].valid = VALID;
568 lpm->tbl8[i].depth = depth;
569 lpm->tbl8[i].next_hop = next_hop;
576 * Update tbl24 entry to point to new tbl8 entry. Note: The
577 * ext_flag and tbl8_index need to be updated simultaneously,
578 * so assign whole structure in one go.
581 struct rte_lpm_tbl24_entry new_tbl24_entry = {
582 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
588 lpm->tbl24[tbl24_index] = new_tbl24_entry;
592 * If it is valid, extended entry calculate the index into tbl8.
594 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
595 tbl8_group_start = tbl8_group_index *
596 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
597 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
599 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
601 if (!lpm->tbl8[i].valid ||
602 lpm->tbl8[i].depth <= depth) {
603 struct rte_lpm_tbl8_entry new_tbl8_entry = {
606 .next_hop = next_hop,
607 .valid_group = lpm->tbl8[i].valid_group,
611 * Setting tbl8 entry in one go to avoid race
614 lpm->tbl8[i] = new_tbl8_entry;
628 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
631 int32_t rule_index, status = 0;
634 /* Check user arguments. */
635 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
638 ip_masked = ip & depth_to_mask(depth);
640 /* Add the rule to the rule table. */
641 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
643 /* If the is no space available for new rule return error. */
644 if (rule_index < 0) {
648 if (depth <= MAX_DEPTH_TBL24) {
649 status = add_depth_small(lpm, ip_masked, depth, next_hop);
651 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
652 status = add_depth_big(lpm, ip_masked, depth, next_hop);
655 * If add fails due to exhaustion of tbl8 extensions delete
656 * rule that was added to rule table.
659 rule_delete(lpm, rule_index, depth);
669 * Look for a rule in the high-level rules table
672 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
678 /* Check user arguments. */
680 (next_hop == NULL) ||
681 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
684 /* Look for the rule using rule_find. */
685 ip_masked = ip & depth_to_mask(depth);
686 rule_index = rule_find(lpm, ip_masked, depth);
688 if (rule_index >= 0) {
689 *next_hop = lpm->rules_tbl[rule_index].next_hop;
693 /* If rule is not found return 0. */
697 static inline int32_t
698 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
704 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
705 ip_masked = ip & depth_to_mask(prev_depth);
707 rule_index = rule_find(lpm, ip_masked, prev_depth);
709 if (rule_index >= 0) {
710 *sub_rule_depth = prev_depth;
718 static inline int32_t
719 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
720 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
722 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
724 /* Calculate the range and index into Table24. */
725 tbl24_range = depth_to_range(depth);
726 tbl24_index = (ip_masked >> 8);
729 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
730 * and a positive number indicates a sub_rule_index.
732 if (sub_rule_index < 0) {
734 * If no replacement rule exists then invalidate entries
735 * associated with this rule.
737 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
739 if (lpm->tbl24[i].ext_entry == 0 &&
740 lpm->tbl24[i].depth <= depth ) {
741 lpm->tbl24[i].valid = INVALID;
745 * If TBL24 entry is extended, then there has
746 * to be a rule with depth >= 25 in the
747 * associated TBL8 group.
750 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
751 tbl8_index = tbl8_group_index *
752 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
754 for (j = tbl8_index; j < (tbl8_index +
755 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
757 if (lpm->tbl8[j].depth <= depth)
758 lpm->tbl8[j].valid = INVALID;
765 * If a replacement rule exists then modify entries
766 * associated with this rule.
769 struct rte_lpm_tbl24_entry new_tbl24_entry = {
770 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
773 .depth = sub_rule_depth,
776 struct rte_lpm_tbl8_entry new_tbl8_entry = {
778 .depth = sub_rule_depth,
779 .next_hop = lpm->rules_tbl
780 [sub_rule_index].next_hop,
783 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
785 if (lpm->tbl24[i].ext_entry == 0 &&
786 lpm->tbl24[i].depth <= depth ) {
787 lpm->tbl24[i] = new_tbl24_entry;
791 * If TBL24 entry is extended, then there has
792 * to be a rule with depth >= 25 in the
793 * associated TBL8 group.
796 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
797 tbl8_index = tbl8_group_index *
798 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
800 for (j = tbl8_index; j < (tbl8_index +
801 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
803 if (lpm->tbl8[j].depth <= depth)
804 lpm->tbl8[j] = new_tbl8_entry;
814 * Checks if table 8 group can be recycled.
816 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
817 * Return of -EINVAL means tbl8 is empty and thus can be recycled
818 * Return of value > -1 means tbl8 is in use but has all the same values and
819 * thus can be recycled
821 static inline int32_t
822 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
824 uint32_t tbl8_group_end, i;
825 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
828 * Check the first entry of the given tbl8. If it is invalid we know
829 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
830 * (As they would affect all entries in a tbl8) and thus this table
831 * can not be recycled.
833 if (tbl8[tbl8_group_start].valid) {
835 * If first entry is valid check if the depth is less than 24
836 * and if so check the rest of the entries to verify that they
837 * are all of this depth.
839 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
840 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
844 tbl8[tbl8_group_start].depth) {
849 /* If all entries are the same return the tb8 index */
850 return tbl8_group_start;
856 * If the first entry is invalid check if the rest of the entries in
857 * the tbl8 are invalid.
859 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
863 /* If no valid entries are found then return -EINVAL. */
867 static inline int32_t
868 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
869 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
871 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
873 int32_t tbl8_recycle_index;
876 * Calculate the index into tbl24 and range. Note: All depths larger
877 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
879 tbl24_index = ip_masked >> 8;
881 /* Calculate the index into tbl8 and range. */
882 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
883 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
884 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
885 tbl8_range = depth_to_range(depth);
887 if (sub_rule_index < 0) {
889 * Loop through the range of entries on tbl8 for which the
890 * rule_to_delete must be removed or modified.
892 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
893 if (lpm->tbl8[i].depth <= depth)
894 lpm->tbl8[i].valid = INVALID;
898 /* Set new tbl8 entry. */
899 struct rte_lpm_tbl8_entry new_tbl8_entry = {
901 .depth = sub_rule_depth,
902 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
903 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
907 * Loop through the range of entries on tbl8 for which the
908 * rule_to_delete must be modified.
910 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
911 if (lpm->tbl8[i].depth <= depth)
912 lpm->tbl8[i] = new_tbl8_entry;
917 * Check if there are any valid entries in this tbl8 group. If all
918 * tbl8 entries are invalid we can free the tbl8 and invalidate the
919 * associated tbl24 entry.
922 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
924 if (tbl8_recycle_index == -EINVAL){
925 /* Set tbl24 before freeing tbl8 to avoid race condition. */
926 lpm->tbl24[tbl24_index].valid = 0;
927 tbl8_free(lpm->tbl8, tbl8_group_start);
929 else if (tbl8_recycle_index > -1) {
930 /* Update tbl24 entry. */
931 struct rte_lpm_tbl24_entry new_tbl24_entry = {
932 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
935 .depth = lpm->tbl8[tbl8_recycle_index].depth,
938 /* Set tbl24 before freeing tbl8 to avoid race condition. */
939 lpm->tbl24[tbl24_index] = new_tbl24_entry;
940 tbl8_free(lpm->tbl8, tbl8_group_start);
950 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
952 int32_t rule_to_delete_index, sub_rule_index;
954 uint8_t sub_rule_depth;
956 * Check input arguments. Note: IP must be a positive integer of 32
957 * bits in length therefore it need not be checked.
959 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
963 ip_masked = ip & depth_to_mask(depth);
966 * Find the index of the input rule, that needs to be deleted, in the
969 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
972 * Check if rule_to_delete_index was found. If no rule was found the
973 * function rule_find returns -E_RTE_NO_TAILQ.
975 if (rule_to_delete_index < 0)
976 return -E_RTE_NO_TAILQ;
978 /* Delete the rule from the rule table. */
979 rule_delete(lpm, rule_to_delete_index, depth);
982 * Find rule to replace the rule_to_delete. If there is no rule to
983 * replace the rule_to_delete we return -1 and invalidate the table
984 * entries associated with this rule.
987 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
990 * If the input depth value is less than 25 use function
991 * delete_depth_small otherwise use delete_depth_big.
993 if (depth <= MAX_DEPTH_TBL24) {
994 return delete_depth_small(lpm, ip_masked, depth,
995 sub_rule_index, sub_rule_depth);
997 else { /* If depth > MAX_DEPTH_TBL24 */
998 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
1003 * Delete all rules from the LPM table.
1006 rte_lpm_delete_all(struct rte_lpm *lpm)
1008 /* Zero rule information. */
1009 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1012 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1015 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1017 /* Delete all rules form the rules table. */
1018 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);