1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
13 #include <rte_branch_prediction.h>
14 #include <rte_common.h>
15 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
16 #include <rte_malloc.h>
18 #include <rte_eal_memconfig.h>
19 #include <rte_per_lcore.h>
20 #include <rte_string_fns.h>
21 #include <rte_errno.h>
22 #include <rte_rwlock.h>
23 #include <rte_spinlock.h>
24 #include <rte_tailq.h>
25 #include <rte_function_versioning.h>
29 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
31 static struct rte_tailq_elem rte_lpm_tailq = {
34 EAL_REGISTER_TAILQ(rte_lpm_tailq)
36 #define MAX_DEPTH_TBL24 24
43 /* Macro to enable/disable run-time checks. */
44 #if defined(RTE_LIBRTE_LPM_DEBUG)
45 #include <rte_debug.h>
46 #define VERIFY_DEPTH(depth) do { \
47 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
48 rte_panic("LPM: Invalid depth (%u) at line %d", \
49 (unsigned)(depth), __LINE__); \
52 #define VERIFY_DEPTH(depth)
56 * Converts a given depth value to its corresponding mask value.
58 * depth (IN) : range = 1 - 32
59 * mask (OUT) : 32bit mask
61 static uint32_t __attribute__((pure))
62 depth_to_mask(uint8_t depth)
66 /* To calculate a mask start with a 1 on the left hand side and right
67 * shift while populating the left hand side with 1's
69 return (int)0x80000000 >> (depth - 1);
73 * Converts given depth value to its corresponding range value.
75 static uint32_t __attribute__((pure))
76 depth_to_range(uint8_t depth)
81 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
83 if (depth <= MAX_DEPTH_TBL24)
84 return 1 << (MAX_DEPTH_TBL24 - depth);
86 /* Else if depth is greater than 24 */
87 return 1 << (RTE_LPM_MAX_DEPTH - depth);
91 * Find an existing lpm table and return a pointer to it.
94 rte_lpm_find_existing(const char *name)
96 struct rte_lpm *l = NULL;
97 struct rte_tailq_entry *te;
98 struct rte_lpm_list *lpm_list;
100 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
102 rte_mcfg_tailq_read_lock();
103 TAILQ_FOREACH(te, lpm_list, next) {
105 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
108 rte_mcfg_tailq_read_unlock();
119 * Allocates memory for LPM object
122 rte_lpm_create(const char *name, int socket_id,
123 const struct rte_lpm_config *config)
125 char mem_name[RTE_LPM_NAMESIZE];
126 struct rte_lpm *lpm = NULL;
127 struct rte_tailq_entry *te;
128 uint32_t mem_size, rules_size, tbl8s_size;
129 struct rte_lpm_list *lpm_list;
131 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
133 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
135 /* Check user arguments. */
136 if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
137 || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
142 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
144 /* Determine the amount of memory to allocate. */
145 mem_size = sizeof(*lpm);
146 rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
147 tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
148 RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
150 rte_mcfg_tailq_write_lock();
152 /* guarantee there's no existing */
153 TAILQ_FOREACH(te, lpm_list, next) {
155 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
165 /* allocate tailq entry */
166 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
168 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
173 /* Allocate memory to store the LPM data structures. */
174 lpm = rte_zmalloc_socket(mem_name, mem_size,
175 RTE_CACHE_LINE_SIZE, socket_id);
177 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
183 lpm->rules_tbl = rte_zmalloc_socket(NULL,
184 (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
186 if (lpm->rules_tbl == NULL) {
187 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
195 lpm->tbl8 = rte_zmalloc_socket(NULL,
196 (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
198 if (lpm->tbl8 == NULL) {
199 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
200 rte_free(lpm->rules_tbl);
208 /* Save user arguments. */
209 lpm->max_rules = config->max_rules;
210 lpm->number_tbl8s = config->number_tbl8s;
211 strlcpy(lpm->name, name, sizeof(lpm->name));
215 TAILQ_INSERT_TAIL(lpm_list, te, next);
218 rte_mcfg_tailq_write_unlock();
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 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
238 rte_mcfg_tailq_write_lock();
240 /* find our tailq entry */
241 TAILQ_FOREACH(te, lpm_list, next) {
242 if (te->data == (void *) lpm)
246 TAILQ_REMOVE(lpm_list, te, next);
248 rte_mcfg_tailq_write_unlock();
251 rte_free(lpm->rules_tbl);
257 * Adds a rule to the rule table.
259 * NOTE: The rule table is split into 32 groups. Each group contains rules that
260 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
261 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
262 * to refer to depth 1 because even though the depth range is 1 - 32, depths
263 * are stored in the rule table from 0 - 31.
264 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
267 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
270 uint32_t rule_gindex, rule_index, last_rule;
275 /* Scan through rule group to see if rule already exists. */
276 if (lpm->rule_info[depth - 1].used_rules > 0) {
278 /* rule_gindex stands for rule group index. */
279 rule_gindex = lpm->rule_info[depth - 1].first_rule;
280 /* Initialise rule_index to point to start of rule group. */
281 rule_index = rule_gindex;
282 /* Last rule = Last used rule in this rule group. */
283 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
285 for (; rule_index < last_rule; rule_index++) {
287 /* If rule already exists update its next_hop and return. */
288 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
289 lpm->rules_tbl[rule_index].next_hop = next_hop;
295 if (rule_index == lpm->max_rules)
298 /* Calculate the position in which the rule will be stored. */
301 for (i = depth - 1; i > 0; i--) {
302 if (lpm->rule_info[i - 1].used_rules > 0) {
303 rule_index = lpm->rule_info[i - 1].first_rule
304 + lpm->rule_info[i - 1].used_rules;
308 if (rule_index == lpm->max_rules)
311 lpm->rule_info[depth - 1].first_rule = rule_index;
314 /* Make room for the new rule in the array. */
315 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
316 if (lpm->rule_info[i - 1].first_rule
317 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
320 if (lpm->rule_info[i - 1].used_rules > 0) {
321 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
322 + lpm->rule_info[i - 1].used_rules]
323 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
324 lpm->rule_info[i - 1].first_rule++;
328 /* Add the new rule. */
329 lpm->rules_tbl[rule_index].ip = ip_masked;
330 lpm->rules_tbl[rule_index].next_hop = next_hop;
332 /* Increment the used rules counter for this rule group. */
333 lpm->rule_info[depth - 1].used_rules++;
339 * Delete a rule from the rule table.
340 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
343 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
349 lpm->rules_tbl[rule_index] =
350 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
357 + lpm->rule_info[i].used_rules - 1];
358 lpm->rule_info[i].first_rule--;
362 lpm->rule_info[depth - 1].used_rules--;
366 * Finds a rule in rule table.
367 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
370 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
372 uint32_t rule_gindex, last_rule, rule_index;
376 rule_gindex = lpm->rule_info[depth - 1].first_rule;
377 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
379 /* Scan used rules at given depth to find rule. */
380 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
381 /* If rule is found return the rule index. */
382 if (lpm->rules_tbl[rule_index].ip == ip_masked)
386 /* If rule is not found return -EINVAL. */
391 * Find, clean and allocate a tbl8.
394 tbl8_alloc(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
396 uint32_t group_idx; /* tbl8 group index. */
397 struct rte_lpm_tbl_entry *tbl8_entry;
399 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
400 for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
401 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
402 /* If a free tbl8 group is found clean it and set as VALID. */
403 if (!tbl8_entry->valid_group) {
404 struct rte_lpm_tbl_entry new_tbl8_entry = {
408 .valid_group = VALID,
411 memset(&tbl8_entry[0], 0,
412 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
413 sizeof(tbl8_entry[0]));
415 __atomic_store(tbl8_entry, &new_tbl8_entry,
418 /* Return group index for allocated tbl8 group. */
423 /* If there are no tbl8 groups free then return error. */
428 tbl8_free(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
430 /* Set tbl8 group invalid*/
431 struct rte_lpm_tbl_entry zero_tbl8_entry = {0};
433 __atomic_store(&tbl8[tbl8_group_start], &zero_tbl8_entry,
437 static __rte_noinline int32_t
438 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
441 #define group_idx next_hop
442 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
444 /* Calculate the index into Table24. */
445 tbl24_index = ip >> 8;
446 tbl24_range = depth_to_range(depth);
448 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
450 * For invalid OR valid and non-extended tbl 24 entries set
453 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
454 lpm->tbl24[i].depth <= depth)) {
456 struct rte_lpm_tbl_entry new_tbl24_entry = {
457 .next_hop = next_hop,
463 /* Setting tbl24 entry in one go to avoid race
466 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
472 if (lpm->tbl24[i].valid_group == 1) {
473 /* If tbl24 entry is valid and extended calculate the
476 tbl8_index = lpm->tbl24[i].group_idx *
477 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
478 tbl8_group_end = tbl8_index +
479 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
481 for (j = tbl8_index; j < tbl8_group_end; j++) {
482 if (!lpm->tbl8[j].valid ||
483 lpm->tbl8[j].depth <= depth) {
484 struct rte_lpm_tbl_entry
487 .valid_group = VALID,
489 .next_hop = next_hop,
493 * Setting tbl8 entry in one go to avoid
496 __atomic_store(&lpm->tbl8[j],
509 static __rte_noinline int32_t
510 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
513 #define group_idx next_hop
514 uint32_t tbl24_index;
515 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
518 tbl24_index = (ip_masked >> 8);
519 tbl8_range = depth_to_range(depth);
521 if (!lpm->tbl24[tbl24_index].valid) {
522 /* Search for a free tbl8 group. */
523 tbl8_group_index = tbl8_alloc(lpm->tbl8, lpm->number_tbl8s);
525 /* Check tbl8 allocation was successful. */
526 if (tbl8_group_index < 0) {
527 return tbl8_group_index;
530 /* Find index into tbl8 and range. */
531 tbl8_index = (tbl8_group_index *
532 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
535 /* Set tbl8 entry. */
536 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
537 struct rte_lpm_tbl_entry new_tbl8_entry = {
540 .valid_group = lpm->tbl8[i].valid_group,
541 .next_hop = next_hop,
543 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
548 * Update tbl24 entry to point to new tbl8 entry. Note: The
549 * ext_flag and tbl8_index need to be updated simultaneously,
550 * so assign whole structure in one go
553 struct rte_lpm_tbl_entry new_tbl24_entry = {
554 .group_idx = tbl8_group_index,
560 /* The tbl24 entry must be written only after the
561 * tbl8 entries are written.
563 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
566 } /* If valid entry but not extended calculate the index into Table8. */
567 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
568 /* Search for free tbl8 group. */
569 tbl8_group_index = tbl8_alloc(lpm->tbl8, lpm->number_tbl8s);
571 if (tbl8_group_index < 0) {
572 return tbl8_group_index;
575 tbl8_group_start = tbl8_group_index *
576 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
577 tbl8_group_end = tbl8_group_start +
578 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
580 /* Populate new tbl8 with tbl24 value. */
581 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
582 struct rte_lpm_tbl_entry new_tbl8_entry = {
584 .depth = lpm->tbl24[tbl24_index].depth,
585 .valid_group = lpm->tbl8[i].valid_group,
586 .next_hop = lpm->tbl24[tbl24_index].next_hop,
588 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
592 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
594 /* Insert new rule into the tbl8 entry. */
595 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
596 struct rte_lpm_tbl_entry new_tbl8_entry = {
599 .valid_group = lpm->tbl8[i].valid_group,
600 .next_hop = next_hop,
602 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
607 * Update tbl24 entry to point to new tbl8 entry. Note: The
608 * ext_flag and tbl8_index need to be updated simultaneously,
609 * so assign whole structure in one go.
612 struct rte_lpm_tbl_entry new_tbl24_entry = {
613 .group_idx = tbl8_group_index,
619 /* The tbl24 entry must be written only after the
620 * tbl8 entries are written.
622 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
626 * If it is valid, extended entry calculate the index into tbl8.
628 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
629 tbl8_group_start = tbl8_group_index *
630 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
631 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
633 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
635 if (!lpm->tbl8[i].valid ||
636 lpm->tbl8[i].depth <= depth) {
637 struct rte_lpm_tbl_entry new_tbl8_entry = {
640 .next_hop = next_hop,
641 .valid_group = lpm->tbl8[i].valid_group,
645 * Setting tbl8 entry in one go to avoid race
648 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
663 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
666 int32_t rule_index, status = 0;
669 /* Check user arguments. */
670 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
673 ip_masked = ip & depth_to_mask(depth);
675 /* Add the rule to the rule table. */
676 rule_index = rule_add(lpm, ip_masked, depth, next_hop);
678 /* If the is no space available for new rule return error. */
679 if (rule_index < 0) {
683 if (depth <= MAX_DEPTH_TBL24) {
684 status = add_depth_small(lpm, ip_masked, depth, next_hop);
685 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
686 status = add_depth_big(lpm, ip_masked, depth, next_hop);
689 * If add fails due to exhaustion of tbl8 extensions delete
690 * rule that was added to rule table.
693 rule_delete(lpm, rule_index, depth);
703 * Look for a rule in the high-level rules table
706 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
712 /* Check user arguments. */
714 (next_hop == NULL) ||
715 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
718 /* Look for the rule using rule_find. */
719 ip_masked = ip & depth_to_mask(depth);
720 rule_index = rule_find(lpm, ip_masked, depth);
722 if (rule_index >= 0) {
723 *next_hop = lpm->rules_tbl[rule_index].next_hop;
727 /* If rule is not found return 0. */
732 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
733 uint8_t *sub_rule_depth)
739 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
740 ip_masked = ip & depth_to_mask(prev_depth);
742 rule_index = rule_find(lpm, ip_masked, prev_depth);
744 if (rule_index >= 0) {
745 *sub_rule_depth = prev_depth;
754 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
755 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
757 #define group_idx next_hop
758 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
760 /* Calculate the range and index into Table24. */
761 tbl24_range = depth_to_range(depth);
762 tbl24_index = (ip_masked >> 8);
763 struct rte_lpm_tbl_entry zero_tbl24_entry = {0};
766 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
767 * and a positive number indicates a sub_rule_index.
769 if (sub_rule_index < 0) {
771 * If no replacement rule exists then invalidate entries
772 * associated with this rule.
774 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
776 if (lpm->tbl24[i].valid_group == 0 &&
777 lpm->tbl24[i].depth <= depth) {
778 __atomic_store(&lpm->tbl24[i],
779 &zero_tbl24_entry, __ATOMIC_RELEASE);
780 } else if (lpm->tbl24[i].valid_group == 1) {
782 * If TBL24 entry is extended, then there has
783 * to be a rule with depth >= 25 in the
784 * associated TBL8 group.
787 tbl8_group_index = lpm->tbl24[i].group_idx;
788 tbl8_index = tbl8_group_index *
789 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
791 for (j = tbl8_index; j < (tbl8_index +
792 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
794 if (lpm->tbl8[j].depth <= depth)
795 lpm->tbl8[j].valid = INVALID;
801 * If a replacement rule exists then modify entries
802 * associated with this rule.
805 struct rte_lpm_tbl_entry new_tbl24_entry = {
806 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
809 .depth = sub_rule_depth,
812 struct rte_lpm_tbl_entry new_tbl8_entry = {
814 .valid_group = VALID,
815 .depth = sub_rule_depth,
816 .next_hop = lpm->rules_tbl
817 [sub_rule_index].next_hop,
820 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
822 if (lpm->tbl24[i].valid_group == 0 &&
823 lpm->tbl24[i].depth <= depth) {
824 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
826 } else if (lpm->tbl24[i].valid_group == 1) {
828 * If TBL24 entry is extended, then there has
829 * to be a rule with depth >= 25 in the
830 * associated TBL8 group.
833 tbl8_group_index = lpm->tbl24[i].group_idx;
834 tbl8_index = tbl8_group_index *
835 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
837 for (j = tbl8_index; j < (tbl8_index +
838 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
840 if (lpm->tbl8[j].depth <= depth)
841 __atomic_store(&lpm->tbl8[j],
853 * Checks if table 8 group can be recycled.
855 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
856 * Return of -EINVAL means tbl8 is empty and thus can be recycled
857 * Return of value > -1 means tbl8 is in use but has all the same values and
858 * thus can be recycled
861 tbl8_recycle_check(struct rte_lpm_tbl_entry *tbl8,
862 uint32_t tbl8_group_start)
864 uint32_t tbl8_group_end, i;
865 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
868 * Check the first entry of the given tbl8. If it is invalid we know
869 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
870 * (As they would affect all entries in a tbl8) and thus this table
871 * can not be recycled.
873 if (tbl8[tbl8_group_start].valid) {
875 * If first entry is valid check if the depth is less than 24
876 * and if so check the rest of the entries to verify that they
877 * are all of this depth.
879 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
880 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
884 tbl8[tbl8_group_start].depth) {
889 /* If all entries are the same return the tb8 index */
890 return tbl8_group_start;
896 * If the first entry is invalid check if the rest of the entries in
897 * the tbl8 are invalid.
899 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
903 /* If no valid entries are found then return -EINVAL. */
908 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
909 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
911 #define group_idx next_hop
912 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
914 int32_t tbl8_recycle_index;
917 * Calculate the index into tbl24 and range. Note: All depths larger
918 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
920 tbl24_index = ip_masked >> 8;
922 /* Calculate the index into tbl8 and range. */
923 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
924 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
925 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
926 tbl8_range = depth_to_range(depth);
928 if (sub_rule_index < 0) {
930 * Loop through the range of entries on tbl8 for which the
931 * rule_to_delete must be removed or modified.
933 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
934 if (lpm->tbl8[i].depth <= depth)
935 lpm->tbl8[i].valid = INVALID;
938 /* Set new tbl8 entry. */
939 struct rte_lpm_tbl_entry new_tbl8_entry = {
941 .depth = sub_rule_depth,
942 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
943 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
947 * Loop through the range of entries on tbl8 for which the
948 * rule_to_delete must be modified.
950 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
951 if (lpm->tbl8[i].depth <= depth)
952 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
958 * Check if there are any valid entries in this tbl8 group. If all
959 * tbl8 entries are invalid we can free the tbl8 and invalidate the
960 * associated tbl24 entry.
963 tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
965 if (tbl8_recycle_index == -EINVAL) {
966 /* Set tbl24 before freeing tbl8 to avoid race condition.
967 * Prevent the free of the tbl8 group from hoisting.
969 lpm->tbl24[tbl24_index].valid = 0;
970 __atomic_thread_fence(__ATOMIC_RELEASE);
971 tbl8_free(lpm->tbl8, tbl8_group_start);
972 } else if (tbl8_recycle_index > -1) {
973 /* Update tbl24 entry. */
974 struct rte_lpm_tbl_entry new_tbl24_entry = {
975 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
978 .depth = lpm->tbl8[tbl8_recycle_index].depth,
981 /* Set tbl24 before freeing tbl8 to avoid race condition.
982 * Prevent the free of the tbl8 group from hoisting.
984 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
986 __atomic_thread_fence(__ATOMIC_RELEASE);
987 tbl8_free(lpm->tbl8, tbl8_group_start);
997 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
999 int32_t rule_to_delete_index, sub_rule_index;
1001 uint8_t sub_rule_depth;
1003 * Check input arguments. Note: IP must be a positive integer of 32
1004 * bits in length therefore it need not be checked.
1006 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1010 ip_masked = ip & depth_to_mask(depth);
1013 * Find the index of the input rule, that needs to be deleted, in the
1016 rule_to_delete_index = rule_find(lpm, ip_masked, depth);
1019 * Check if rule_to_delete_index was found. If no rule was found the
1020 * function rule_find returns -EINVAL.
1022 if (rule_to_delete_index < 0)
1025 /* Delete the rule from the rule table. */
1026 rule_delete(lpm, rule_to_delete_index, depth);
1029 * Find rule to replace the rule_to_delete. If there is no rule to
1030 * replace the rule_to_delete we return -1 and invalidate the table
1031 * entries associated with this rule.
1034 sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
1037 * If the input depth value is less than 25 use function
1038 * delete_depth_small otherwise use delete_depth_big.
1040 if (depth <= MAX_DEPTH_TBL24) {
1041 return delete_depth_small(lpm, ip_masked, depth,
1042 sub_rule_index, sub_rule_depth);
1043 } else { /* If depth > MAX_DEPTH_TBL24 */
1044 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index,
1050 * Delete all rules from the LPM table.
1053 rte_lpm_delete_all(struct rte_lpm *lpm)
1055 /* Zero rule information. */
1056 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1059 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1062 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1063 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1065 /* Delete all rules form the rules table. */
1066 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);