4 * Copyright(c) 2010-2012 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.
41 #include <sys/queue.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_common.h>
46 #include <rte_memory.h> /* for definition of CACHE_LINE_SIZE */
47 #include <rte_malloc.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_string_fns.h>
54 #include <rte_errno.h>
58 TAILQ_HEAD(rte_lpm_list, rte_lpm);
60 #define MAX_DEPTH_TBL24 24
67 /* Macro to enable/disable run-time checks. */
68 #if defined(RTE_LIBRTE_LPM_DEBUG)
69 #include <rte_debug.h>
70 #define VERIFY_DEPTH(depth) do { \
71 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
72 rte_panic("LPM: Invalid depth (%u) at line %d", \
73 (unsigned)(depth), __LINE__); \
76 #define VERIFY_DEPTH(depth)
80 * Converts a given depth value to its corresponding mask value.
82 * depth (IN) : range = 1 - 32
83 * mask (OUT) : 32bit mask
85 static uint32_t __attribute__((pure))
86 depth_to_mask(uint8_t depth)
90 /* To calculate a mask start with a 1 on the left hand side and right
91 * shift while populating the left hand side with 1's
93 return (int)0x80000000 >> (depth - 1);
97 * Converts given depth value to its corresponding range value.
99 static inline uint32_t __attribute__((pure))
100 depth_to_range(uint8_t depth)
105 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
107 if (depth <= MAX_DEPTH_TBL24)
108 return 1 << (MAX_DEPTH_TBL24 - depth);
110 /* Else if depth is greater than 24 */
111 return (1 << (RTE_LPM_MAX_DEPTH - depth));
115 * Find an existing lpm table and return a pointer to it.
118 rte_lpm_find_existing(const char *name)
121 struct rte_lpm_list *lpm_list;
123 /* check that we have an initialised tail queue */
124 if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
125 rte_errno = E_RTE_NO_TAILQ;
129 TAILQ_FOREACH(l, lpm_list, next) {
130 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
141 * Allocates memory for LPM object
144 rte_lpm_create(const char *name, int socket_id, int max_rules,
147 char mem_name[RTE_LPM_NAMESIZE];
148 struct rte_lpm *lpm = NULL;
150 struct rte_lpm_list *lpm_list;
152 /* check that we have an initialised tail queue */
154 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
155 rte_errno = E_RTE_NO_TAILQ;
159 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
160 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
162 /* Check user arguments. */
163 if ((name == NULL) || (socket_id < -1) || (max_rules == 0) ||
164 (mem_location != RTE_LPM_HEAP &&
165 mem_location != RTE_LPM_MEMZONE)){
170 rte_snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
173 * Pad out max_rules so that each depth is given the same number of
176 if (max_rules % RTE_LPM_MAX_DEPTH) {
177 max_rules += RTE_LPM_MAX_DEPTH -
178 (max_rules % RTE_LPM_MAX_DEPTH);
181 /* Determine the amount of memory to allocate. */
182 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
184 /* guarantee there's no existing */
185 TAILQ_FOREACH(lpm, lpm_list, next) {
186 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
192 /* Allocate memory to store the LPM data structures. */
193 if (mem_location == RTE_LPM_MEMZONE) {
194 const struct rte_memzone *mz;
195 uint32_t mz_flags = 0;
197 mz = rte_memzone_reserve(mem_name, mem_size, socket_id,
200 RTE_LOG(ERR, LPM, "LPM memzone creation failed\n");
204 memset(mz->addr, 0, mem_size);
205 lpm = (struct rte_lpm *) mz->addr;
209 lpm = (struct rte_lpm *)rte_zmalloc(mem_name, mem_size,
212 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
217 /* Save user arguments. */
218 lpm->max_rules_per_depth = max_rules / RTE_LPM_MAX_DEPTH;
219 rte_snprintf(lpm->name, sizeof(lpm->name), "%s", name);
220 lpm->mem_location = mem_location;
222 TAILQ_INSERT_TAIL(lpm_list, lpm, next);
228 * Deallocates memory for given LPM table.
231 rte_lpm_free(struct rte_lpm *lpm)
233 /* Check user arguments. */
237 /* Note: Its is currently not possible to free a memzone. */
238 if (lpm->mem_location == RTE_LPM_HEAP){
239 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
245 * Adds a rule to the rule table.
247 * NOTE: The rule table is split into 32 groups. Each group contains rules that
248 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
249 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
250 * to refer to depth 1 because even though the depth range is 1 - 32, depths
251 * are stored in the rule table from 0 - 31.
252 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
254 static inline int32_t
255 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
258 uint32_t rule_gindex, rule_index, last_rule;
262 /* rule_gindex stands for rule group index. */
263 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
264 /* Initialise rule_index to point to start of rule group. */
265 rule_index = rule_gindex;
266 /* Last rule = Last used rule in this rule group. */
267 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
269 /* Scan through rule group to see if rule already exists. */
270 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
272 /* If rule already exists update its next_hop and return. */
273 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
274 lpm->rules_tbl[rule_index].next_hop = next_hop;
281 * If rule does not exist check if there is space to add a new rule to
282 * this rule group. If there is no space return error. */
283 if (lpm->used_rules_at_depth[depth - 1] == lpm->max_rules_per_depth) {
287 /* If there is space for the new rule add it. */
288 lpm->rules_tbl[rule_index].ip = ip_masked;
289 lpm->rules_tbl[rule_index].next_hop = next_hop;
291 /* Increment the used rules counter for this rule group. */
292 lpm->used_rules_at_depth[depth - 1]++;
298 * Delete a rule from the rule table.
299 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
302 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
304 uint32_t rule_gindex, last_rule_index;
308 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
309 last_rule_index = rule_gindex +
310 (lpm->used_rules_at_depth[depth - 1]) - 1;
312 * Overwrite redundant rule with last rule in group and decrement rule
315 lpm->rules_tbl[rule_index] = lpm->rules_tbl[last_rule_index];
316 lpm->used_rules_at_depth[depth - 1]--;
321 * Finds a rule in rule table.
322 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
324 static inline int32_t
325 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
327 uint32_t rule_gindex, last_rule, rule_index;
331 rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
332 last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
334 /* Scan used rules at given depth to find rule. */
335 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
336 /* If rule is found return the rule index. */
337 if (lpm->rules_tbl[rule_index].ip == ip_masked)
341 /* If rule is not found return -E_RTE_NO_TAILQ. */
342 return -E_RTE_NO_TAILQ;
346 * Find, clean and allocate a tbl8.
348 static inline int32_t
349 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
351 uint32_t tbl8_gindex; /* tbl8 group index. */
352 struct rte_lpm_tbl8_entry *tbl8_entry;
354 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
355 for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
357 tbl8_entry = &tbl8[tbl8_gindex *
358 RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
359 /* If a free tbl8 group is found clean it and set as VALID. */
360 if (!tbl8_entry->valid_group) {
361 memset(&tbl8_entry[0], 0,
362 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
363 sizeof(tbl8_entry[0]));
365 tbl8_entry->valid_group = VALID;
367 /* Return group index for allocated tbl8 group. */
372 /* If there are no tbl8 groups free then return error. */
377 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
379 /* Set tbl8 group invalid*/
380 tbl8[tbl8_group_start].valid_group = INVALID;
383 static inline int32_t
384 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
387 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
389 /* Calculate the index into Table24. */
390 tbl24_index = ip >> 8;
391 tbl24_range = depth_to_range(depth);
393 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
395 * For invalid OR valid and non-extended tbl 24 entries set
398 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
399 lpm->tbl24[i].depth <= depth)) {
401 struct rte_lpm_tbl24_entry new_tbl24_entry = {
405 { .next_hop = next_hop, }
408 /* Setting tbl24 entry in one go to avoid race
410 lpm->tbl24[i] = new_tbl24_entry;
415 /* If tbl24 entry is valid and extended calculate the index
417 tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex *
418 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
419 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
421 for (j = tbl8_index; j < tbl8_group_end; j++) {
422 if (!lpm->tbl8[j].valid ||
423 lpm->tbl8[j].depth <= depth) {
424 struct rte_lpm_tbl8_entry new_tbl8_entry = {
426 .valid_group = VALID,
428 .next_hop = next_hop,
432 * Setting tbl8 entry in one go to avoid race
435 lpm->tbl8[j] = new_tbl8_entry;
445 static inline int32_t
446 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
449 uint32_t tbl24_index;
450 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
453 tbl24_index = (ip_masked >> 8);
454 tbl8_range = depth_to_range(depth);
456 if (!lpm->tbl24[tbl24_index].valid) {
457 /* Search for a free tbl8 group. */
458 tbl8_group_index = tbl8_alloc(lpm->tbl8);
460 /* Check tbl8 allocation was successful. */
461 if (tbl8_group_index < 0) {
462 return tbl8_group_index;
465 /* Find index into tbl8 and range. */
466 tbl8_index = (tbl8_group_index *
467 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
470 /* Set tbl8 entry. */
471 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
472 lpm->tbl8[i].depth = depth;
473 lpm->tbl8[i].next_hop = next_hop;
474 lpm->tbl8[i].valid = VALID;
478 * Update tbl24 entry to point to new tbl8 entry. Note: The
479 * ext_flag and tbl8_index need to be updated simultaneously,
480 * so assign whole structure in one go
483 struct rte_lpm_tbl24_entry new_tbl24_entry = {
487 { .tbl8_gindex = (uint8_t)tbl8_group_index, }
490 lpm->tbl24[tbl24_index] = new_tbl24_entry;
492 }/* If valid entry but not extended calculate the index into Table8. */
493 else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
494 /* Search for free tbl8 group. */
495 tbl8_group_index = tbl8_alloc(lpm->tbl8);
497 if (tbl8_group_index < 0) {
498 return tbl8_group_index;
501 tbl8_group_start = tbl8_group_index *
502 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
503 tbl8_group_end = tbl8_group_start +
504 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
506 /* Populate new tbl8 with tbl24 value. */
507 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
508 lpm->tbl8[i].valid = VALID;
509 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
510 lpm->tbl8[i].next_hop =
511 lpm->tbl24[tbl24_index].next_hop;
514 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
516 /* Insert new rule into the tbl8 entry. */
517 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
518 if (!lpm->tbl8[i].valid ||
519 lpm->tbl8[i].depth <= depth) {
520 lpm->tbl8[i].valid = VALID;
521 lpm->tbl8[i].depth = depth;
522 lpm->tbl8[i].next_hop = next_hop;
529 * Update tbl24 entry to point to new tbl8 entry. Note: The
530 * ext_flag and tbl8_index need to be updated simultaneously,
531 * so assign whole structure in one go.
534 struct rte_lpm_tbl24_entry new_tbl24_entry = {
538 { .tbl8_gindex = (uint8_t)tbl8_group_index, }
541 lpm->tbl24[tbl24_index] = new_tbl24_entry;
545 * If it is valid, extended entry calculate the index into tbl8.
547 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
548 tbl8_group_start = tbl8_group_index *
549 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
550 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
552 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
554 if (!lpm->tbl8[i].valid ||
555 lpm->tbl8[i].depth <= depth) {
556 struct rte_lpm_tbl8_entry new_tbl8_entry = {
559 .next_hop = next_hop,
563 * Setting tbl8 entry in one go to avoid race
566 lpm->tbl8[i] = new_tbl8_entry;
580 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
583 int32_t rule_index, status = 0;
586 /* Check user arguments. */
587 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
590 ip_masked = ip & depth_to_mask(depth);
592 /* Add the rule to the rule table. */
593 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
595 /* If the is no space available for new rule return error. */
596 if (rule_index < 0) {
600 if (depth <= MAX_DEPTH_TBL24) {
601 status = add_depth_small(lpm, ip_masked, depth, next_hop);
603 else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
604 status = add_depth_big(lpm, ip_masked, depth, next_hop);
607 * If add fails due to exhaustion of tbl8 extensions delete
608 * rule that was added to rule table.
611 rule_delete(lpm, rule_index, depth);
620 static inline int32_t
621 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
627 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
628 ip_masked = ip & depth_to_mask(prev_depth);
630 rule_index = rule_find(lpm, ip_masked, prev_depth);
639 static inline int32_t
640 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
641 uint8_t depth, int32_t sub_rule_index)
643 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
646 /* Calculate the range and index into Table24. */
647 tbl24_range = depth_to_range(depth);
648 tbl24_index = (ip_masked >> 8);
651 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
652 * and a positive number indicates a sub_rule_index.
654 if (sub_rule_index < 0) {
656 * If no replacement rule exists then invalidate entries
657 * associated with this rule.
659 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
661 if (lpm->tbl24[i].ext_entry == 0 &&
662 lpm->tbl24[i].depth <= depth ) {
663 lpm->tbl24[i].valid = INVALID;
667 * If TBL24 entry is extended, then there has
668 * to be a rule with depth >= 25 in the
669 * associated TBL8 group.
672 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
673 tbl8_index = tbl8_group_index *
674 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
676 for (j = tbl8_index; j < (tbl8_index +
677 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
679 if (lpm->tbl8[j].depth <= depth)
680 lpm->tbl8[j].valid = INVALID;
687 * If a replacement rule exists then modify entries
688 * associated with this rule.
691 /* Calculate depth of sub_rule. */
692 new_depth = (uint8_t) (sub_rule_index /
693 lpm->max_rules_per_depth);
695 struct rte_lpm_tbl24_entry new_tbl24_entry = {
699 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,}
702 struct rte_lpm_tbl8_entry new_tbl8_entry = {
705 .next_hop = lpm->rules_tbl
706 [sub_rule_index].next_hop,
709 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
711 if (lpm->tbl24[i].ext_entry == 0 &&
712 lpm->tbl24[i].depth <= depth ) {
713 lpm->tbl24[i] = new_tbl24_entry;
717 * If TBL24 entry is extended, then there has
718 * to be a rule with depth >= 25 in the
719 * associated TBL8 group.
722 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
723 tbl8_index = tbl8_group_index *
724 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
726 for (j = tbl8_index; j < (tbl8_index +
727 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
729 if (lpm->tbl8[j].depth <= depth)
730 lpm->tbl8[j] = new_tbl8_entry;
740 * Checks if table 8 group can be recycled.
742 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
743 * Return of -EINVAL means tbl8 is empty and thus can be recycled
744 * Return of value > -1 means tbl8 is in use but has all the same values and
745 * thus can be recycled
747 static inline int32_t
748 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
750 uint32_t tbl8_group_end, i;
751 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
754 * Check the first entry of the given tbl8. If it is invalid we know
755 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
756 * (As they would affect all entries in a tbl8) and thus this table
757 * can not be recycled.
759 if (tbl8[tbl8_group_start].valid) {
761 * If first entry is valid check if the depth is less than 24
762 * and if so check the rest of the entries to verify that they
763 * are all of this depth.
765 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
766 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
770 tbl8[tbl8_group_start].depth) {
775 /* If all entries are the same return the tb8 index */
776 return tbl8_group_start;
782 * If the first entry is invalid check if the rest of the entries in
783 * the tbl8 are invalid.
785 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
789 /* If no valid entries are found then return -EINVAL. */
793 static inline int32_t
794 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
795 uint8_t depth, int32_t sub_rule_index)
797 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
800 int32_t tbl8_recycle_index;
803 * Calculate the index into tbl24 and range. Note: All depths larger
804 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
806 tbl24_index = ip_masked >> 8;
808 /* Calculate the index into tbl8 and range. */
809 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
810 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
811 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
812 tbl8_range = depth_to_range(depth);
814 if (sub_rule_index < 0) {
816 * Loop through the range of entries on tbl8 for which the
817 * rule_to_delete must be removed or modified.
819 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
820 if (lpm->tbl8[i].depth <= depth)
821 lpm->tbl8[i].valid = INVALID;
825 new_depth = (uint8_t)(sub_rule_index /
826 lpm->max_rules_per_depth);
828 /* Set new tbl8 entry. */
829 struct rte_lpm_tbl8_entry new_tbl8_entry = {
832 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
836 * Loop through the range of entries on tbl8 for which the
837 * rule_to_delete must be modified.
839 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
840 if (lpm->tbl8[i].depth <= depth)
841 lpm->tbl8[i] = new_tbl8_entry;
846 * Check if there are any valid entries in this tbl8 group. If all
847 * tbl8 entries are invalid we can free the tbl8 and invalidate the
848 * associated tbl24 entry.
851 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
853 if (tbl8_recycle_index == -EINVAL){
854 /* Set tbl24 before freeing tbl8 to avoid race condition. */
855 lpm->tbl24[tbl24_index].valid = 0;
856 tbl8_free(lpm->tbl8, tbl8_group_start);
858 else if (tbl8_recycle_index > -1) {
859 /* Update tbl24 entry. */
860 struct rte_lpm_tbl24_entry new_tbl24_entry = {
863 .depth = lpm->tbl8[tbl8_recycle_index].depth,
864 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, }
867 /* Set tbl24 before freeing tbl8 to avoid race condition. */
868 lpm->tbl24[tbl24_index] = new_tbl24_entry;
869 tbl8_free(lpm->tbl8, tbl8_group_start);
879 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
881 int32_t rule_to_delete_index, sub_rule_index;
884 * Check input arguments. Note: IP must be a positive integer of 32
885 * bits in length therefore it need not be checked.
887 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
891 ip_masked = ip & depth_to_mask(depth);
894 * Find the index of the input rule, that needs to be deleted, in the
897 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
900 * Check if rule_to_delete_index was found. If no rule was found the
901 * function rule_find returns -E_RTE_NO_TAILQ.
903 if (rule_to_delete_index < 0)
904 return -E_RTE_NO_TAILQ;
906 /* Delete the rule from the rule table. */
907 rule_delete(lpm, rule_to_delete_index, depth);
910 * Find rule to replace the rule_to_delete. If there is no rule to
911 * replace the rule_to_delete we return -1 and invalidate the table
912 * entries associated with this rule.
914 sub_rule_index = find_previous_rule(lpm, ip, depth);
917 * If the input depth value is less than 25 use function
918 * delete_depth_small otherwise use delete_depth_big.
920 if (depth <= MAX_DEPTH_TBL24) {
921 return delete_depth_small(lpm, ip_masked, depth,
924 else { /* If depth > MAX_DEPTH_TBL24 */
925 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index);
930 * Delete all rules from the LPM table.
933 rte_lpm_delete_all(struct rte_lpm *lpm)
935 /* Zero used rules counter. */
936 memset(lpm->used_rules_at_depth, 0, sizeof(lpm->used_rules_at_depth));
939 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
942 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
944 /* Delete all rules form the rules table. */
945 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) *
946 (lpm->max_rules_per_depth * RTE_LPM_MAX_DEPTH));