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>
27 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
29 static struct rte_tailq_elem rte_lpm_tailq = {
32 EAL_REGISTER_TAILQ(rte_lpm_tailq)
34 #define MAX_DEPTH_TBL24 24
41 /* Macro to enable/disable run-time checks. */
42 #if defined(RTE_LIBRTE_LPM_DEBUG)
43 #include <rte_debug.h>
44 #define VERIFY_DEPTH(depth) do { \
45 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
46 rte_panic("LPM: Invalid depth (%u) at line %d", \
47 (unsigned)(depth), __LINE__); \
50 #define VERIFY_DEPTH(depth)
54 * Converts a given depth value to its corresponding mask value.
56 * depth (IN) : range = 1 - 32
57 * mask (OUT) : 32bit mask
59 static uint32_t __attribute__((pure))
60 depth_to_mask(uint8_t depth)
64 /* To calculate a mask start with a 1 on the left hand side and right
65 * shift while populating the left hand side with 1's
67 return (int)0x80000000 >> (depth - 1);
71 * Converts given depth value to its corresponding range value.
73 static inline uint32_t __attribute__((pure))
74 depth_to_range(uint8_t depth)
79 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
81 if (depth <= MAX_DEPTH_TBL24)
82 return 1 << (MAX_DEPTH_TBL24 - depth);
84 /* Else if depth is greater than 24 */
85 return 1 << (RTE_LPM_MAX_DEPTH - depth);
89 * Find an existing lpm table and return a pointer to it.
92 rte_lpm_find_existing_v20(const char *name)
94 struct rte_lpm_v20 *l = NULL;
95 struct rte_tailq_entry *te;
96 struct rte_lpm_list *lpm_list;
98 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
100 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
101 TAILQ_FOREACH(te, lpm_list, next) {
103 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
106 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
115 VERSION_SYMBOL(rte_lpm_find_existing, _v20, 2.0);
118 rte_lpm_find_existing_v1604(const char *name)
120 struct rte_lpm *l = NULL;
121 struct rte_tailq_entry *te;
122 struct rte_lpm_list *lpm_list;
124 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
126 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
127 TAILQ_FOREACH(te, lpm_list, next) {
129 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
132 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
141 BIND_DEFAULT_SYMBOL(rte_lpm_find_existing, _v1604, 16.04);
142 MAP_STATIC_SYMBOL(struct rte_lpm *rte_lpm_find_existing(const char *name),
143 rte_lpm_find_existing_v1604);
146 * Allocates memory for LPM object
149 rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
150 __rte_unused int flags)
152 char mem_name[RTE_LPM_NAMESIZE];
153 struct rte_lpm_v20 *lpm = NULL;
154 struct rte_tailq_entry *te;
156 struct rte_lpm_list *lpm_list;
158 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
160 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry_v20) != 2);
162 /* Check user arguments. */
163 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
168 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
170 /* Determine the amount of memory to allocate. */
171 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
173 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
175 /* guarantee there's no existing */
176 TAILQ_FOREACH(te, lpm_list, next) {
178 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
188 /* allocate tailq entry */
189 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
191 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
196 /* Allocate memory to store the LPM data structures. */
197 lpm = rte_zmalloc_socket(mem_name, mem_size,
198 RTE_CACHE_LINE_SIZE, socket_id);
200 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
206 /* Save user arguments. */
207 lpm->max_rules = max_rules;
208 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
212 TAILQ_INSERT_TAIL(lpm_list, te, next);
215 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
219 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
222 rte_lpm_create_v1604(const char *name, int socket_id,
223 const struct rte_lpm_config *config)
225 char mem_name[RTE_LPM_NAMESIZE];
226 struct rte_lpm *lpm = NULL;
227 struct rte_tailq_entry *te;
228 uint32_t mem_size, rules_size, tbl8s_size;
229 struct rte_lpm_list *lpm_list;
231 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
233 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
235 /* Check user arguments. */
236 if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
237 || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
242 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
244 /* Determine the amount of memory to allocate. */
245 mem_size = sizeof(*lpm);
246 rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
247 tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
248 RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
250 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
252 /* guarantee there's no existing */
253 TAILQ_FOREACH(te, lpm_list, next) {
255 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
265 /* allocate tailq entry */
266 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
268 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
273 /* Allocate memory to store the LPM data structures. */
274 lpm = rte_zmalloc_socket(mem_name, mem_size,
275 RTE_CACHE_LINE_SIZE, socket_id);
277 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
283 lpm->rules_tbl = rte_zmalloc_socket(NULL,
284 (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
286 if (lpm->rules_tbl == NULL) {
287 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
295 lpm->tbl8 = rte_zmalloc_socket(NULL,
296 (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
298 if (lpm->tbl8 == NULL) {
299 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
300 rte_free(lpm->rules_tbl);
308 /* Save user arguments. */
309 lpm->max_rules = config->max_rules;
310 lpm->number_tbl8s = config->number_tbl8s;
311 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
315 TAILQ_INSERT_TAIL(lpm_list, te, next);
318 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
322 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
324 struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
325 const struct rte_lpm_config *config), rte_lpm_create_v1604);
328 * Deallocates memory for given LPM table.
331 rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
333 struct rte_lpm_list *lpm_list;
334 struct rte_tailq_entry *te;
336 /* Check user arguments. */
340 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
342 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
344 /* find our tailq entry */
345 TAILQ_FOREACH(te, lpm_list, next) {
346 if (te->data == (void *) lpm)
350 TAILQ_REMOVE(lpm_list, te, next);
352 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
357 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
360 rte_lpm_free_v1604(struct rte_lpm *lpm)
362 struct rte_lpm_list *lpm_list;
363 struct rte_tailq_entry *te;
365 /* Check user arguments. */
369 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
371 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
373 /* find our tailq entry */
374 TAILQ_FOREACH(te, lpm_list, next) {
375 if (te->data == (void *) lpm)
379 TAILQ_REMOVE(lpm_list, te, next);
381 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
384 rte_free(lpm->rules_tbl);
388 BIND_DEFAULT_SYMBOL(rte_lpm_free, _v1604, 16.04);
389 MAP_STATIC_SYMBOL(void rte_lpm_free(struct rte_lpm *lpm),
393 * Adds a rule to the rule table.
395 * NOTE: The rule table is split into 32 groups. Each group contains rules that
396 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
397 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
398 * to refer to depth 1 because even though the depth range is 1 - 32, depths
399 * are stored in the rule table from 0 - 31.
400 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
402 static inline int32_t
403 rule_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
406 uint32_t rule_gindex, rule_index, last_rule;
411 /* Scan through rule group to see if rule already exists. */
412 if (lpm->rule_info[depth - 1].used_rules > 0) {
414 /* rule_gindex stands for rule group index. */
415 rule_gindex = lpm->rule_info[depth - 1].first_rule;
416 /* Initialise rule_index to point to start of rule group. */
417 rule_index = rule_gindex;
418 /* Last rule = Last used rule in this rule group. */
419 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
421 for (; rule_index < last_rule; rule_index++) {
423 /* If rule already exists update its next_hop and return. */
424 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
425 lpm->rules_tbl[rule_index].next_hop = next_hop;
431 if (rule_index == lpm->max_rules)
434 /* Calculate the position in which the rule will be stored. */
437 for (i = depth - 1; i > 0; i--) {
438 if (lpm->rule_info[i - 1].used_rules > 0) {
439 rule_index = lpm->rule_info[i - 1].first_rule
440 + lpm->rule_info[i - 1].used_rules;
444 if (rule_index == lpm->max_rules)
447 lpm->rule_info[depth - 1].first_rule = rule_index;
450 /* Make room for the new rule in the array. */
451 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
452 if (lpm->rule_info[i - 1].first_rule
453 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
456 if (lpm->rule_info[i - 1].used_rules > 0) {
457 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
458 + lpm->rule_info[i - 1].used_rules]
459 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
460 lpm->rule_info[i - 1].first_rule++;
464 /* Add the new rule. */
465 lpm->rules_tbl[rule_index].ip = ip_masked;
466 lpm->rules_tbl[rule_index].next_hop = next_hop;
468 /* Increment the used rules counter for this rule group. */
469 lpm->rule_info[depth - 1].used_rules++;
474 static inline int32_t
475 rule_add_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
478 uint32_t rule_gindex, rule_index, last_rule;
483 /* Scan through rule group to see if rule already exists. */
484 if (lpm->rule_info[depth - 1].used_rules > 0) {
486 /* rule_gindex stands for rule group index. */
487 rule_gindex = lpm->rule_info[depth - 1].first_rule;
488 /* Initialise rule_index to point to start of rule group. */
489 rule_index = rule_gindex;
490 /* Last rule = Last used rule in this rule group. */
491 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
493 for (; rule_index < last_rule; rule_index++) {
495 /* If rule already exists update its next_hop and return. */
496 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
497 lpm->rules_tbl[rule_index].next_hop = next_hop;
503 if (rule_index == lpm->max_rules)
506 /* Calculate the position in which the rule will be stored. */
509 for (i = depth - 1; i > 0; i--) {
510 if (lpm->rule_info[i - 1].used_rules > 0) {
511 rule_index = lpm->rule_info[i - 1].first_rule
512 + lpm->rule_info[i - 1].used_rules;
516 if (rule_index == lpm->max_rules)
519 lpm->rule_info[depth - 1].first_rule = rule_index;
522 /* Make room for the new rule in the array. */
523 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
524 if (lpm->rule_info[i - 1].first_rule
525 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
528 if (lpm->rule_info[i - 1].used_rules > 0) {
529 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
530 + lpm->rule_info[i - 1].used_rules]
531 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
532 lpm->rule_info[i - 1].first_rule++;
536 /* Add the new rule. */
537 lpm->rules_tbl[rule_index].ip = ip_masked;
538 lpm->rules_tbl[rule_index].next_hop = next_hop;
540 /* Increment the used rules counter for this rule group. */
541 lpm->rule_info[depth - 1].used_rules++;
547 * Delete a rule from the rule table.
548 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
551 rule_delete_v20(struct rte_lpm_v20 *lpm, int32_t rule_index, uint8_t depth)
557 lpm->rules_tbl[rule_index] =
558 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
559 + lpm->rule_info[depth - 1].used_rules - 1];
561 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
562 if (lpm->rule_info[i].used_rules > 0) {
563 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
564 lpm->rules_tbl[lpm->rule_info[i].first_rule
565 + lpm->rule_info[i].used_rules - 1];
566 lpm->rule_info[i].first_rule--;
570 lpm->rule_info[depth - 1].used_rules--;
574 rule_delete_v1604(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
580 lpm->rules_tbl[rule_index] =
581 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
582 + lpm->rule_info[depth - 1].used_rules - 1];
584 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
585 if (lpm->rule_info[i].used_rules > 0) {
586 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
587 lpm->rules_tbl[lpm->rule_info[i].first_rule
588 + lpm->rule_info[i].used_rules - 1];
589 lpm->rule_info[i].first_rule--;
593 lpm->rule_info[depth - 1].used_rules--;
597 * Finds a rule in rule table.
598 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
600 static inline int32_t
601 rule_find_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth)
603 uint32_t rule_gindex, last_rule, rule_index;
607 rule_gindex = lpm->rule_info[depth - 1].first_rule;
608 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
610 /* Scan used rules at given depth to find rule. */
611 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
612 /* If rule is found return the rule index. */
613 if (lpm->rules_tbl[rule_index].ip == ip_masked)
617 /* If rule is not found return -EINVAL. */
621 static inline int32_t
622 rule_find_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
624 uint32_t rule_gindex, last_rule, rule_index;
628 rule_gindex = lpm->rule_info[depth - 1].first_rule;
629 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
631 /* Scan used rules at given depth to find rule. */
632 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
633 /* If rule is found return the rule index. */
634 if (lpm->rules_tbl[rule_index].ip == ip_masked)
638 /* If rule is not found return -EINVAL. */
643 * Find, clean and allocate a tbl8.
645 static inline int32_t
646 tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
648 uint32_t group_idx; /* tbl8 group index. */
649 struct rte_lpm_tbl_entry_v20 *tbl8_entry;
651 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
652 for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
654 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
655 /* If a free tbl8 group is found clean it and set as VALID. */
656 if (!tbl8_entry->valid_group) {
657 memset(&tbl8_entry[0], 0,
658 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
659 sizeof(tbl8_entry[0]));
661 tbl8_entry->valid_group = VALID;
663 /* Return group index for allocated tbl8 group. */
668 /* If there are no tbl8 groups free then return error. */
672 static inline int32_t
673 tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
675 uint32_t group_idx; /* tbl8 group index. */
676 struct rte_lpm_tbl_entry *tbl8_entry;
678 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
679 for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
680 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
681 /* If a free tbl8 group is found clean it and set as VALID. */
682 if (!tbl8_entry->valid_group) {
683 memset(&tbl8_entry[0], 0,
684 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
685 sizeof(tbl8_entry[0]));
687 tbl8_entry->valid_group = VALID;
689 /* Return group index for allocated tbl8 group. */
694 /* If there are no tbl8 groups free then return error. */
699 tbl8_free_v20(struct rte_lpm_tbl_entry_v20 *tbl8, uint32_t tbl8_group_start)
701 /* Set tbl8 group invalid*/
702 tbl8[tbl8_group_start].valid_group = INVALID;
706 tbl8_free_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
708 /* Set tbl8 group invalid*/
709 tbl8[tbl8_group_start].valid_group = INVALID;
712 static inline int32_t
713 add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
716 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
718 /* Calculate the index into Table24. */
719 tbl24_index = ip >> 8;
720 tbl24_range = depth_to_range(depth);
722 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
724 * For invalid OR valid and non-extended tbl 24 entries set
727 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
728 lpm->tbl24[i].depth <= depth)) {
730 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
735 new_tbl24_entry.next_hop = next_hop;
737 /* Setting tbl24 entry in one go to avoid race
740 lpm->tbl24[i] = new_tbl24_entry;
745 if (lpm->tbl24[i].valid_group == 1) {
746 /* If tbl24 entry is valid and extended calculate the
749 tbl8_index = lpm->tbl24[i].group_idx *
750 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
751 tbl8_group_end = tbl8_index +
752 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
754 for (j = tbl8_index; j < tbl8_group_end; j++) {
755 if (!lpm->tbl8[j].valid ||
756 lpm->tbl8[j].depth <= depth) {
757 struct rte_lpm_tbl_entry_v20
760 .valid_group = VALID,
763 new_tbl8_entry.next_hop = next_hop;
766 * Setting tbl8 entry in one go to avoid
769 lpm->tbl8[j] = new_tbl8_entry;
780 static inline int32_t
781 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
784 #define group_idx next_hop
785 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
787 /* Calculate the index into Table24. */
788 tbl24_index = ip >> 8;
789 tbl24_range = depth_to_range(depth);
791 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
793 * For invalid OR valid and non-extended tbl 24 entries set
796 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
797 lpm->tbl24[i].depth <= depth)) {
799 struct rte_lpm_tbl_entry new_tbl24_entry = {
800 .next_hop = next_hop,
806 /* Setting tbl24 entry in one go to avoid race
809 lpm->tbl24[i] = new_tbl24_entry;
814 if (lpm->tbl24[i].valid_group == 1) {
815 /* If tbl24 entry is valid and extended calculate the
818 tbl8_index = lpm->tbl24[i].group_idx *
819 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
820 tbl8_group_end = tbl8_index +
821 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
823 for (j = tbl8_index; j < tbl8_group_end; j++) {
824 if (!lpm->tbl8[j].valid ||
825 lpm->tbl8[j].depth <= depth) {
826 struct rte_lpm_tbl_entry
829 .valid_group = VALID,
831 .next_hop = next_hop,
835 * Setting tbl8 entry in one go to avoid
838 lpm->tbl8[j] = new_tbl8_entry;
849 static inline int32_t
850 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
853 uint32_t tbl24_index;
854 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
857 tbl24_index = (ip_masked >> 8);
858 tbl8_range = depth_to_range(depth);
860 if (!lpm->tbl24[tbl24_index].valid) {
861 /* Search for a free tbl8 group. */
862 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
864 /* Check tbl8 allocation was successful. */
865 if (tbl8_group_index < 0) {
866 return tbl8_group_index;
869 /* Find index into tbl8 and range. */
870 tbl8_index = (tbl8_group_index *
871 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
874 /* Set tbl8 entry. */
875 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
876 lpm->tbl8[i].depth = depth;
877 lpm->tbl8[i].next_hop = next_hop;
878 lpm->tbl8[i].valid = VALID;
882 * Update tbl24 entry to point to new tbl8 entry. Note: The
883 * ext_flag and tbl8_index need to be updated simultaneously,
884 * so assign whole structure in one go
887 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
888 .group_idx = (uint8_t)tbl8_group_index,
894 lpm->tbl24[tbl24_index] = new_tbl24_entry;
896 } /* If valid entry but not extended calculate the index into Table8. */
897 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
898 /* Search for free tbl8 group. */
899 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
901 if (tbl8_group_index < 0) {
902 return tbl8_group_index;
905 tbl8_group_start = tbl8_group_index *
906 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
907 tbl8_group_end = tbl8_group_start +
908 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
910 /* Populate new tbl8 with tbl24 value. */
911 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
912 lpm->tbl8[i].valid = VALID;
913 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
914 lpm->tbl8[i].next_hop =
915 lpm->tbl24[tbl24_index].next_hop;
918 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
920 /* Insert new rule into the tbl8 entry. */
921 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
922 lpm->tbl8[i].valid = VALID;
923 lpm->tbl8[i].depth = depth;
924 lpm->tbl8[i].next_hop = next_hop;
928 * Update tbl24 entry to point to new tbl8 entry. Note: The
929 * ext_flag and tbl8_index need to be updated simultaneously,
930 * so assign whole structure in one go.
933 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
934 .group_idx = (uint8_t)tbl8_group_index,
940 lpm->tbl24[tbl24_index] = new_tbl24_entry;
943 * If it is valid, extended entry calculate the index into tbl8.
945 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
946 tbl8_group_start = tbl8_group_index *
947 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
948 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
950 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
952 if (!lpm->tbl8[i].valid ||
953 lpm->tbl8[i].depth <= depth) {
954 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
957 .valid_group = lpm->tbl8[i].valid_group,
959 new_tbl8_entry.next_hop = next_hop;
961 * Setting tbl8 entry in one go to avoid race
964 lpm->tbl8[i] = new_tbl8_entry;
974 static inline int32_t
975 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
978 #define group_idx next_hop
979 uint32_t tbl24_index;
980 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
983 tbl24_index = (ip_masked >> 8);
984 tbl8_range = depth_to_range(depth);
986 if (!lpm->tbl24[tbl24_index].valid) {
987 /* Search for a free tbl8 group. */
988 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
990 /* Check tbl8 allocation was successful. */
991 if (tbl8_group_index < 0) {
992 return tbl8_group_index;
995 /* Find index into tbl8 and range. */
996 tbl8_index = (tbl8_group_index *
997 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1000 /* Set tbl8 entry. */
1001 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1002 lpm->tbl8[i].depth = depth;
1003 lpm->tbl8[i].next_hop = next_hop;
1004 lpm->tbl8[i].valid = VALID;
1008 * Update tbl24 entry to point to new tbl8 entry. Note: The
1009 * ext_flag and tbl8_index need to be updated simultaneously,
1010 * so assign whole structure in one go
1013 struct rte_lpm_tbl_entry new_tbl24_entry = {
1014 .group_idx = tbl8_group_index,
1020 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1022 } /* If valid entry but not extended calculate the index into Table8. */
1023 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1024 /* Search for free tbl8 group. */
1025 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1027 if (tbl8_group_index < 0) {
1028 return tbl8_group_index;
1031 tbl8_group_start = tbl8_group_index *
1032 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1033 tbl8_group_end = tbl8_group_start +
1034 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1036 /* Populate new tbl8 with tbl24 value. */
1037 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1038 lpm->tbl8[i].valid = VALID;
1039 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1040 lpm->tbl8[i].next_hop =
1041 lpm->tbl24[tbl24_index].next_hop;
1044 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1046 /* Insert new rule into the tbl8 entry. */
1047 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1048 lpm->tbl8[i].valid = VALID;
1049 lpm->tbl8[i].depth = depth;
1050 lpm->tbl8[i].next_hop = next_hop;
1054 * Update tbl24 entry to point to new tbl8 entry. Note: The
1055 * ext_flag and tbl8_index need to be updated simultaneously,
1056 * so assign whole structure in one go.
1059 struct rte_lpm_tbl_entry new_tbl24_entry = {
1060 .group_idx = tbl8_group_index,
1066 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1069 * If it is valid, extended entry calculate the index into tbl8.
1071 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1072 tbl8_group_start = tbl8_group_index *
1073 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1074 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1076 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1078 if (!lpm->tbl8[i].valid ||
1079 lpm->tbl8[i].depth <= depth) {
1080 struct rte_lpm_tbl_entry new_tbl8_entry = {
1083 .next_hop = next_hop,
1084 .valid_group = lpm->tbl8[i].valid_group,
1088 * Setting tbl8 entry in one go to avoid race
1091 lpm->tbl8[i] = new_tbl8_entry;
1105 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1108 int32_t rule_index, status = 0;
1111 /* Check user arguments. */
1112 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1115 ip_masked = ip & depth_to_mask(depth);
1117 /* Add the rule to the rule table. */
1118 rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1120 /* If the is no space available for new rule return error. */
1121 if (rule_index < 0) {
1125 if (depth <= MAX_DEPTH_TBL24) {
1126 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1127 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1128 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1131 * If add fails due to exhaustion of tbl8 extensions delete
1132 * rule that was added to rule table.
1135 rule_delete_v20(lpm, rule_index, depth);
1143 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1146 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1149 int32_t rule_index, status = 0;
1152 /* Check user arguments. */
1153 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1156 ip_masked = ip & depth_to_mask(depth);
1158 /* Add the rule to the rule table. */
1159 rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1161 /* If the is no space available for new rule return error. */
1162 if (rule_index < 0) {
1166 if (depth <= MAX_DEPTH_TBL24) {
1167 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1168 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1169 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1172 * If add fails due to exhaustion of tbl8 extensions delete
1173 * rule that was added to rule table.
1176 rule_delete_v1604(lpm, rule_index, depth);
1184 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1185 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1186 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1189 * Look for a rule in the high-level rules table
1192 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1198 /* Check user arguments. */
1199 if ((lpm == NULL) ||
1200 (next_hop == NULL) ||
1201 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1204 /* Look for the rule using rule_find. */
1205 ip_masked = ip & depth_to_mask(depth);
1206 rule_index = rule_find_v20(lpm, ip_masked, depth);
1208 if (rule_index >= 0) {
1209 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1213 /* If rule is not found return 0. */
1216 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1219 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1225 /* Check user arguments. */
1226 if ((lpm == NULL) ||
1227 (next_hop == NULL) ||
1228 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1231 /* Look for the rule using rule_find. */
1232 ip_masked = ip & depth_to_mask(depth);
1233 rule_index = rule_find_v1604(lpm, ip_masked, depth);
1235 if (rule_index >= 0) {
1236 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1240 /* If rule is not found return 0. */
1243 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1244 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1245 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1247 static inline int32_t
1248 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1249 uint8_t *sub_rule_depth)
1255 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1256 ip_masked = ip & depth_to_mask(prev_depth);
1258 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1260 if (rule_index >= 0) {
1261 *sub_rule_depth = prev_depth;
1269 static inline int32_t
1270 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1271 uint8_t *sub_rule_depth)
1277 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1278 ip_masked = ip & depth_to_mask(prev_depth);
1280 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1282 if (rule_index >= 0) {
1283 *sub_rule_depth = prev_depth;
1291 static inline int32_t
1292 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1293 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1295 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1297 /* Calculate the range and index into Table24. */
1298 tbl24_range = depth_to_range(depth);
1299 tbl24_index = (ip_masked >> 8);
1302 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1303 * and a positive number indicates a sub_rule_index.
1305 if (sub_rule_index < 0) {
1307 * If no replacement rule exists then invalidate entries
1308 * associated with this rule.
1310 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1312 if (lpm->tbl24[i].valid_group == 0 &&
1313 lpm->tbl24[i].depth <= depth) {
1314 lpm->tbl24[i].valid = INVALID;
1315 } else if (lpm->tbl24[i].valid_group == 1) {
1317 * If TBL24 entry is extended, then there has
1318 * to be a rule with depth >= 25 in the
1319 * associated TBL8 group.
1322 tbl8_group_index = lpm->tbl24[i].group_idx;
1323 tbl8_index = tbl8_group_index *
1324 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1326 for (j = tbl8_index; j < (tbl8_index +
1327 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1329 if (lpm->tbl8[j].depth <= depth)
1330 lpm->tbl8[j].valid = INVALID;
1336 * If a replacement rule exists then modify entries
1337 * associated with this rule.
1340 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1341 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1344 .depth = sub_rule_depth,
1347 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1349 .valid_group = VALID,
1350 .depth = sub_rule_depth,
1352 new_tbl8_entry.next_hop =
1353 lpm->rules_tbl[sub_rule_index].next_hop;
1355 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1357 if (lpm->tbl24[i].valid_group == 0 &&
1358 lpm->tbl24[i].depth <= depth) {
1359 lpm->tbl24[i] = new_tbl24_entry;
1360 } else if (lpm->tbl24[i].valid_group == 1) {
1362 * If TBL24 entry is extended, then there has
1363 * to be a rule with depth >= 25 in the
1364 * associated TBL8 group.
1367 tbl8_group_index = lpm->tbl24[i].group_idx;
1368 tbl8_index = tbl8_group_index *
1369 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1371 for (j = tbl8_index; j < (tbl8_index +
1372 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1374 if (lpm->tbl8[j].depth <= depth)
1375 lpm->tbl8[j] = new_tbl8_entry;
1384 static inline int32_t
1385 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1386 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1388 #define group_idx next_hop
1389 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1391 /* Calculate the range and index into Table24. */
1392 tbl24_range = depth_to_range(depth);
1393 tbl24_index = (ip_masked >> 8);
1396 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1397 * and a positive number indicates a sub_rule_index.
1399 if (sub_rule_index < 0) {
1401 * If no replacement rule exists then invalidate entries
1402 * associated with this rule.
1404 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1406 if (lpm->tbl24[i].valid_group == 0 &&
1407 lpm->tbl24[i].depth <= depth) {
1408 lpm->tbl24[i].valid = INVALID;
1409 } else if (lpm->tbl24[i].valid_group == 1) {
1411 * If TBL24 entry is extended, then there has
1412 * to be a rule with depth >= 25 in the
1413 * associated TBL8 group.
1416 tbl8_group_index = lpm->tbl24[i].group_idx;
1417 tbl8_index = tbl8_group_index *
1418 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1420 for (j = tbl8_index; j < (tbl8_index +
1421 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1423 if (lpm->tbl8[j].depth <= depth)
1424 lpm->tbl8[j].valid = INVALID;
1430 * If a replacement rule exists then modify entries
1431 * associated with this rule.
1434 struct rte_lpm_tbl_entry new_tbl24_entry = {
1435 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1438 .depth = sub_rule_depth,
1441 struct rte_lpm_tbl_entry new_tbl8_entry = {
1443 .valid_group = VALID,
1444 .depth = sub_rule_depth,
1445 .next_hop = lpm->rules_tbl
1446 [sub_rule_index].next_hop,
1449 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1451 if (lpm->tbl24[i].valid_group == 0 &&
1452 lpm->tbl24[i].depth <= depth) {
1453 lpm->tbl24[i] = new_tbl24_entry;
1454 } else if (lpm->tbl24[i].valid_group == 1) {
1456 * If TBL24 entry is extended, then there has
1457 * to be a rule with depth >= 25 in the
1458 * associated TBL8 group.
1461 tbl8_group_index = lpm->tbl24[i].group_idx;
1462 tbl8_index = tbl8_group_index *
1463 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1465 for (j = tbl8_index; j < (tbl8_index +
1466 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1468 if (lpm->tbl8[j].depth <= depth)
1469 lpm->tbl8[j] = new_tbl8_entry;
1479 * Checks if table 8 group can be recycled.
1481 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1482 * Return of -EINVAL means tbl8 is empty and thus can be recycled
1483 * Return of value > -1 means tbl8 is in use but has all the same values and
1484 * thus can be recycled
1486 static inline int32_t
1487 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1488 uint32_t tbl8_group_start)
1490 uint32_t tbl8_group_end, i;
1491 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1494 * Check the first entry of the given tbl8. If it is invalid we know
1495 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1496 * (As they would affect all entries in a tbl8) and thus this table
1497 * can not be recycled.
1499 if (tbl8[tbl8_group_start].valid) {
1501 * If first entry is valid check if the depth is less than 24
1502 * and if so check the rest of the entries to verify that they
1503 * are all of this depth.
1505 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1506 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1509 if (tbl8[i].depth !=
1510 tbl8[tbl8_group_start].depth) {
1515 /* If all entries are the same return the tb8 index */
1516 return tbl8_group_start;
1522 * If the first entry is invalid check if the rest of the entries in
1523 * the tbl8 are invalid.
1525 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1529 /* If no valid entries are found then return -EINVAL. */
1533 static inline int32_t
1534 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1535 uint32_t tbl8_group_start)
1537 uint32_t tbl8_group_end, i;
1538 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1541 * Check the first entry of the given tbl8. If it is invalid we know
1542 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1543 * (As they would affect all entries in a tbl8) and thus this table
1544 * can not be recycled.
1546 if (tbl8[tbl8_group_start].valid) {
1548 * If first entry is valid check if the depth is less than 24
1549 * and if so check the rest of the entries to verify that they
1550 * are all of this depth.
1552 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1553 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1556 if (tbl8[i].depth !=
1557 tbl8[tbl8_group_start].depth) {
1562 /* If all entries are the same return the tb8 index */
1563 return tbl8_group_start;
1569 * If the first entry is invalid check if the rest of the entries in
1570 * the tbl8 are invalid.
1572 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1576 /* If no valid entries are found then return -EINVAL. */
1580 static inline int32_t
1581 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1582 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1584 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1586 int32_t tbl8_recycle_index;
1589 * Calculate the index into tbl24 and range. Note: All depths larger
1590 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1592 tbl24_index = ip_masked >> 8;
1594 /* Calculate the index into tbl8 and range. */
1595 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1596 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1597 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1598 tbl8_range = depth_to_range(depth);
1600 if (sub_rule_index < 0) {
1602 * Loop through the range of entries on tbl8 for which the
1603 * rule_to_delete must be removed or modified.
1605 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1606 if (lpm->tbl8[i].depth <= depth)
1607 lpm->tbl8[i].valid = INVALID;
1610 /* Set new tbl8 entry. */
1611 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1613 .depth = sub_rule_depth,
1614 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1617 new_tbl8_entry.next_hop =
1618 lpm->rules_tbl[sub_rule_index].next_hop;
1620 * Loop through the range of entries on tbl8 for which the
1621 * rule_to_delete must be modified.
1623 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1624 if (lpm->tbl8[i].depth <= depth)
1625 lpm->tbl8[i] = new_tbl8_entry;
1630 * Check if there are any valid entries in this tbl8 group. If all
1631 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1632 * associated tbl24 entry.
1635 tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1637 if (tbl8_recycle_index == -EINVAL) {
1638 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1639 lpm->tbl24[tbl24_index].valid = 0;
1640 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1641 } else if (tbl8_recycle_index > -1) {
1642 /* Update tbl24 entry. */
1643 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1644 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1647 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1650 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1651 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1652 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1658 static inline int32_t
1659 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1660 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1662 #define group_idx next_hop
1663 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1665 int32_t tbl8_recycle_index;
1668 * Calculate the index into tbl24 and range. Note: All depths larger
1669 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1671 tbl24_index = ip_masked >> 8;
1673 /* Calculate the index into tbl8 and range. */
1674 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1675 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1676 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1677 tbl8_range = depth_to_range(depth);
1679 if (sub_rule_index < 0) {
1681 * Loop through the range of entries on tbl8 for which the
1682 * rule_to_delete must be removed or modified.
1684 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1685 if (lpm->tbl8[i].depth <= depth)
1686 lpm->tbl8[i].valid = INVALID;
1689 /* Set new tbl8 entry. */
1690 struct rte_lpm_tbl_entry new_tbl8_entry = {
1692 .depth = sub_rule_depth,
1693 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1694 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1698 * Loop through the range of entries on tbl8 for which the
1699 * rule_to_delete must be modified.
1701 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1702 if (lpm->tbl8[i].depth <= depth)
1703 lpm->tbl8[i] = new_tbl8_entry;
1708 * Check if there are any valid entries in this tbl8 group. If all
1709 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1710 * associated tbl24 entry.
1713 tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1715 if (tbl8_recycle_index == -EINVAL) {
1716 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1717 lpm->tbl24[tbl24_index].valid = 0;
1718 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1719 } else if (tbl8_recycle_index > -1) {
1720 /* Update tbl24 entry. */
1721 struct rte_lpm_tbl_entry new_tbl24_entry = {
1722 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1725 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1728 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1729 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1730 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1740 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1742 int32_t rule_to_delete_index, sub_rule_index;
1744 uint8_t sub_rule_depth;
1746 * Check input arguments. Note: IP must be a positive integer of 32
1747 * bits in length therefore it need not be checked.
1749 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1753 ip_masked = ip & depth_to_mask(depth);
1756 * Find the index of the input rule, that needs to be deleted, in the
1759 rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1762 * Check if rule_to_delete_index was found. If no rule was found the
1763 * function rule_find returns -EINVAL.
1765 if (rule_to_delete_index < 0)
1768 /* Delete the rule from the rule table. */
1769 rule_delete_v20(lpm, rule_to_delete_index, depth);
1772 * Find rule to replace the rule_to_delete. If there is no rule to
1773 * replace the rule_to_delete we return -1 and invalidate the table
1774 * entries associated with this rule.
1777 sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1780 * If the input depth value is less than 25 use function
1781 * delete_depth_small otherwise use delete_depth_big.
1783 if (depth <= MAX_DEPTH_TBL24) {
1784 return delete_depth_small_v20(lpm, ip_masked, depth,
1785 sub_rule_index, sub_rule_depth);
1786 } else { /* If depth > MAX_DEPTH_TBL24 */
1787 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1791 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1794 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1796 int32_t rule_to_delete_index, sub_rule_index;
1798 uint8_t sub_rule_depth;
1800 * Check input arguments. Note: IP must be a positive integer of 32
1801 * bits in length therefore it need not be checked.
1803 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1807 ip_masked = ip & depth_to_mask(depth);
1810 * Find the index of the input rule, that needs to be deleted, in the
1813 rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1816 * Check if rule_to_delete_index was found. If no rule was found the
1817 * function rule_find returns -EINVAL.
1819 if (rule_to_delete_index < 0)
1822 /* Delete the rule from the rule table. */
1823 rule_delete_v1604(lpm, rule_to_delete_index, depth);
1826 * Find rule to replace the rule_to_delete. If there is no rule to
1827 * replace the rule_to_delete we return -1 and invalidate the table
1828 * entries associated with this rule.
1831 sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1834 * If the input depth value is less than 25 use function
1835 * delete_depth_small otherwise use delete_depth_big.
1837 if (depth <= MAX_DEPTH_TBL24) {
1838 return delete_depth_small_v1604(lpm, ip_masked, depth,
1839 sub_rule_index, sub_rule_depth);
1840 } else { /* If depth > MAX_DEPTH_TBL24 */
1841 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1845 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1846 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1847 uint8_t depth), rte_lpm_delete_v1604);
1850 * Delete all rules from the LPM table.
1853 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1855 /* Zero rule information. */
1856 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1859 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1862 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1864 /* Delete all rules form the rules table. */
1865 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1867 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1870 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1872 /* Zero rule information. */
1873 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1876 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1879 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1880 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1882 /* Delete all rules form the rules table. */
1883 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1885 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1886 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1887 rte_lpm_delete_all_v1604);