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>
28 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
30 static struct rte_tailq_elem rte_lpm_tailq = {
33 EAL_REGISTER_TAILQ(rte_lpm_tailq)
35 #define MAX_DEPTH_TBL24 24
42 /* Macro to enable/disable run-time checks. */
43 #if defined(RTE_LIBRTE_LPM_DEBUG)
44 #include <rte_debug.h>
45 #define VERIFY_DEPTH(depth) do { \
46 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
47 rte_panic("LPM: Invalid depth (%u) at line %d", \
48 (unsigned)(depth), __LINE__); \
51 #define VERIFY_DEPTH(depth)
55 * Converts a given depth value to its corresponding mask value.
57 * depth (IN) : range = 1 - 32
58 * mask (OUT) : 32bit mask
60 static uint32_t __attribute__((pure))
61 depth_to_mask(uint8_t depth)
65 /* To calculate a mask start with a 1 on the left hand side and right
66 * shift while populating the left hand side with 1's
68 return (int)0x80000000 >> (depth - 1);
72 * Converts given depth value to its corresponding range value.
74 static uint32_t __attribute__((pure))
75 depth_to_range(uint8_t depth)
80 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
82 if (depth <= MAX_DEPTH_TBL24)
83 return 1 << (MAX_DEPTH_TBL24 - depth);
85 /* Else if depth is greater than 24 */
86 return 1 << (RTE_LPM_MAX_DEPTH - depth);
90 * Find an existing lpm table and return a pointer to it.
93 rte_lpm_find_existing_v20(const char *name)
95 struct rte_lpm_v20 *l = NULL;
96 struct rte_tailq_entry *te;
97 struct rte_lpm_list *lpm_list;
99 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
101 rte_mcfg_tailq_read_lock();
102 TAILQ_FOREACH(te, lpm_list, next) {
104 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
107 rte_mcfg_tailq_read_unlock();
116 VERSION_SYMBOL(rte_lpm_find_existing, _v20, 2.0);
119 rte_lpm_find_existing_v1604(const char *name)
121 struct rte_lpm *l = NULL;
122 struct rte_tailq_entry *te;
123 struct rte_lpm_list *lpm_list;
125 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
127 rte_mcfg_tailq_read_lock();
128 TAILQ_FOREACH(te, lpm_list, next) {
130 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
133 rte_mcfg_tailq_read_unlock();
142 BIND_DEFAULT_SYMBOL(rte_lpm_find_existing, _v1604, 16.04);
143 MAP_STATIC_SYMBOL(struct rte_lpm *rte_lpm_find_existing(const char *name),
144 rte_lpm_find_existing_v1604);
147 * Allocates memory for LPM object
150 rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
151 __rte_unused int flags)
153 char mem_name[RTE_LPM_NAMESIZE];
154 struct rte_lpm_v20 *lpm = NULL;
155 struct rte_tailq_entry *te;
157 struct rte_lpm_list *lpm_list;
159 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
161 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry_v20) != 2);
163 /* Check user arguments. */
164 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
169 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
171 /* Determine the amount of memory to allocate. */
172 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
174 rte_mcfg_tailq_write_lock();
176 /* guarantee there's no existing */
177 TAILQ_FOREACH(te, lpm_list, next) {
179 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
189 /* allocate tailq entry */
190 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
192 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
197 /* Allocate memory to store the LPM data structures. */
198 lpm = rte_zmalloc_socket(mem_name, mem_size,
199 RTE_CACHE_LINE_SIZE, socket_id);
201 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
207 /* Save user arguments. */
208 lpm->max_rules = max_rules;
209 strlcpy(lpm->name, name, sizeof(lpm->name));
213 TAILQ_INSERT_TAIL(lpm_list, te, next);
216 rte_mcfg_tailq_write_unlock();
220 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
223 rte_lpm_create_v1604(const char *name, int socket_id,
224 const struct rte_lpm_config *config)
226 char mem_name[RTE_LPM_NAMESIZE];
227 struct rte_lpm *lpm = NULL;
228 struct rte_tailq_entry *te;
229 uint32_t mem_size, rules_size, tbl8s_size;
230 struct rte_lpm_list *lpm_list;
232 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
234 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
236 /* Check user arguments. */
237 if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
238 || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
243 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
245 /* Determine the amount of memory to allocate. */
246 mem_size = sizeof(*lpm);
247 rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
248 tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
249 RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
251 rte_mcfg_tailq_write_lock();
253 /* guarantee there's no existing */
254 TAILQ_FOREACH(te, lpm_list, next) {
256 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
266 /* allocate tailq entry */
267 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
269 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
274 /* Allocate memory to store the LPM data structures. */
275 lpm = rte_zmalloc_socket(mem_name, mem_size,
276 RTE_CACHE_LINE_SIZE, socket_id);
278 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
284 lpm->rules_tbl = rte_zmalloc_socket(NULL,
285 (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
287 if (lpm->rules_tbl == NULL) {
288 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
296 lpm->tbl8 = rte_zmalloc_socket(NULL,
297 (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
299 if (lpm->tbl8 == NULL) {
300 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
301 rte_free(lpm->rules_tbl);
309 /* Save user arguments. */
310 lpm->max_rules = config->max_rules;
311 lpm->number_tbl8s = config->number_tbl8s;
312 strlcpy(lpm->name, name, sizeof(lpm->name));
316 TAILQ_INSERT_TAIL(lpm_list, te, next);
319 rte_mcfg_tailq_write_unlock();
323 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
325 struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
326 const struct rte_lpm_config *config), rte_lpm_create_v1604);
329 * Deallocates memory for given LPM table.
332 rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
334 struct rte_lpm_list *lpm_list;
335 struct rte_tailq_entry *te;
337 /* Check user arguments. */
341 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
343 rte_mcfg_tailq_write_lock();
345 /* find our tailq entry */
346 TAILQ_FOREACH(te, lpm_list, next) {
347 if (te->data == (void *) lpm)
351 TAILQ_REMOVE(lpm_list, te, next);
353 rte_mcfg_tailq_write_unlock();
358 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
361 rte_lpm_free_v1604(struct rte_lpm *lpm)
363 struct rte_lpm_list *lpm_list;
364 struct rte_tailq_entry *te;
366 /* Check user arguments. */
370 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
372 rte_mcfg_tailq_write_lock();
374 /* find our tailq entry */
375 TAILQ_FOREACH(te, lpm_list, next) {
376 if (te->data == (void *) lpm)
380 TAILQ_REMOVE(lpm_list, te, next);
382 rte_mcfg_tailq_write_unlock();
385 rte_free(lpm->rules_tbl);
389 BIND_DEFAULT_SYMBOL(rte_lpm_free, _v1604, 16.04);
390 MAP_STATIC_SYMBOL(void rte_lpm_free(struct rte_lpm *lpm),
394 * Adds a rule to the rule table.
396 * NOTE: The rule table is split into 32 groups. Each group contains rules that
397 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
398 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
399 * to refer to depth 1 because even though the depth range is 1 - 32, depths
400 * are stored in the rule table from 0 - 31.
401 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
404 rule_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
407 uint32_t rule_gindex, rule_index, last_rule;
412 /* Scan through rule group to see if rule already exists. */
413 if (lpm->rule_info[depth - 1].used_rules > 0) {
415 /* rule_gindex stands for rule group index. */
416 rule_gindex = lpm->rule_info[depth - 1].first_rule;
417 /* Initialise rule_index to point to start of rule group. */
418 rule_index = rule_gindex;
419 /* Last rule = Last used rule in this rule group. */
420 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
422 for (; rule_index < last_rule; rule_index++) {
424 /* If rule already exists update its next_hop and return. */
425 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
426 lpm->rules_tbl[rule_index].next_hop = next_hop;
432 if (rule_index == lpm->max_rules)
435 /* Calculate the position in which the rule will be stored. */
438 for (i = depth - 1; i > 0; i--) {
439 if (lpm->rule_info[i - 1].used_rules > 0) {
440 rule_index = lpm->rule_info[i - 1].first_rule
441 + lpm->rule_info[i - 1].used_rules;
445 if (rule_index == lpm->max_rules)
448 lpm->rule_info[depth - 1].first_rule = rule_index;
451 /* Make room for the new rule in the array. */
452 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
453 if (lpm->rule_info[i - 1].first_rule
454 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
457 if (lpm->rule_info[i - 1].used_rules > 0) {
458 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
459 + lpm->rule_info[i - 1].used_rules]
460 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
461 lpm->rule_info[i - 1].first_rule++;
465 /* Add the new rule. */
466 lpm->rules_tbl[rule_index].ip = ip_masked;
467 lpm->rules_tbl[rule_index].next_hop = next_hop;
469 /* Increment the used rules counter for this rule group. */
470 lpm->rule_info[depth - 1].used_rules++;
476 rule_add_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
479 uint32_t rule_gindex, rule_index, last_rule;
484 /* Scan through rule group to see if rule already exists. */
485 if (lpm->rule_info[depth - 1].used_rules > 0) {
487 /* rule_gindex stands for rule group index. */
488 rule_gindex = lpm->rule_info[depth - 1].first_rule;
489 /* Initialise rule_index to point to start of rule group. */
490 rule_index = rule_gindex;
491 /* Last rule = Last used rule in this rule group. */
492 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
494 for (; rule_index < last_rule; rule_index++) {
496 /* If rule already exists update its next_hop and return. */
497 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
498 lpm->rules_tbl[rule_index].next_hop = next_hop;
504 if (rule_index == lpm->max_rules)
507 /* Calculate the position in which the rule will be stored. */
510 for (i = depth - 1; i > 0; i--) {
511 if (lpm->rule_info[i - 1].used_rules > 0) {
512 rule_index = lpm->rule_info[i - 1].first_rule
513 + lpm->rule_info[i - 1].used_rules;
517 if (rule_index == lpm->max_rules)
520 lpm->rule_info[depth - 1].first_rule = rule_index;
523 /* Make room for the new rule in the array. */
524 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
525 if (lpm->rule_info[i - 1].first_rule
526 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
529 if (lpm->rule_info[i - 1].used_rules > 0) {
530 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
531 + lpm->rule_info[i - 1].used_rules]
532 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
533 lpm->rule_info[i - 1].first_rule++;
537 /* Add the new rule. */
538 lpm->rules_tbl[rule_index].ip = ip_masked;
539 lpm->rules_tbl[rule_index].next_hop = next_hop;
541 /* Increment the used rules counter for this rule group. */
542 lpm->rule_info[depth - 1].used_rules++;
548 * Delete a rule from the rule table.
549 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
552 rule_delete_v20(struct rte_lpm_v20 *lpm, int32_t rule_index, uint8_t depth)
558 lpm->rules_tbl[rule_index] =
559 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
560 + lpm->rule_info[depth - 1].used_rules - 1];
562 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
563 if (lpm->rule_info[i].used_rules > 0) {
564 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
565 lpm->rules_tbl[lpm->rule_info[i].first_rule
566 + lpm->rule_info[i].used_rules - 1];
567 lpm->rule_info[i].first_rule--;
571 lpm->rule_info[depth - 1].used_rules--;
575 rule_delete_v1604(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
581 lpm->rules_tbl[rule_index] =
582 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
583 + lpm->rule_info[depth - 1].used_rules - 1];
585 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
586 if (lpm->rule_info[i].used_rules > 0) {
587 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
588 lpm->rules_tbl[lpm->rule_info[i].first_rule
589 + lpm->rule_info[i].used_rules - 1];
590 lpm->rule_info[i].first_rule--;
594 lpm->rule_info[depth - 1].used_rules--;
598 * Finds a rule in rule table.
599 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
602 rule_find_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth)
604 uint32_t rule_gindex, last_rule, rule_index;
608 rule_gindex = lpm->rule_info[depth - 1].first_rule;
609 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
611 /* Scan used rules at given depth to find rule. */
612 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
613 /* If rule is found return the rule index. */
614 if (lpm->rules_tbl[rule_index].ip == ip_masked)
618 /* If rule is not found return -EINVAL. */
623 rule_find_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
625 uint32_t rule_gindex, last_rule, rule_index;
629 rule_gindex = lpm->rule_info[depth - 1].first_rule;
630 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
632 /* Scan used rules at given depth to find rule. */
633 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
634 /* If rule is found return the rule index. */
635 if (lpm->rules_tbl[rule_index].ip == ip_masked)
639 /* If rule is not found return -EINVAL. */
644 * Find, clean and allocate a tbl8.
647 tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
649 uint32_t group_idx; /* tbl8 group index. */
650 struct rte_lpm_tbl_entry_v20 *tbl8_entry;
652 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
653 for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
655 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
656 /* If a free tbl8 group is found clean it and set as VALID. */
657 if (!tbl8_entry->valid_group) {
658 memset(&tbl8_entry[0], 0,
659 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
660 sizeof(tbl8_entry[0]));
662 tbl8_entry->valid_group = VALID;
664 /* Return group index for allocated tbl8 group. */
669 /* If there are no tbl8 groups free then return error. */
674 tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
676 uint32_t group_idx; /* tbl8 group index. */
677 struct rte_lpm_tbl_entry *tbl8_entry;
679 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
680 for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
681 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
682 /* If a free tbl8 group is found clean it and set as VALID. */
683 if (!tbl8_entry->valid_group) {
684 memset(&tbl8_entry[0], 0,
685 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
686 sizeof(tbl8_entry[0]));
688 tbl8_entry->valid_group = VALID;
690 /* Return group index for allocated tbl8 group. */
695 /* If there are no tbl8 groups free then return error. */
700 tbl8_free_v20(struct rte_lpm_tbl_entry_v20 *tbl8, uint32_t tbl8_group_start)
702 /* Set tbl8 group invalid*/
703 tbl8[tbl8_group_start].valid_group = INVALID;
707 tbl8_free_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
709 /* Set tbl8 group invalid*/
710 tbl8[tbl8_group_start].valid_group = INVALID;
713 static __rte_noinline int32_t
714 add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
717 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
719 /* Calculate the index into Table24. */
720 tbl24_index = ip >> 8;
721 tbl24_range = depth_to_range(depth);
723 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
725 * For invalid OR valid and non-extended tbl 24 entries set
728 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
729 lpm->tbl24[i].depth <= depth)) {
731 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
736 new_tbl24_entry.next_hop = next_hop;
738 /* Setting tbl24 entry in one go to avoid race
741 lpm->tbl24[i] = new_tbl24_entry;
746 if (lpm->tbl24[i].valid_group == 1) {
747 /* If tbl24 entry is valid and extended calculate the
750 tbl8_index = lpm->tbl24[i].group_idx *
751 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
752 tbl8_group_end = tbl8_index +
753 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
755 for (j = tbl8_index; j < tbl8_group_end; j++) {
756 if (!lpm->tbl8[j].valid ||
757 lpm->tbl8[j].depth <= depth) {
758 struct rte_lpm_tbl_entry_v20
761 .valid_group = VALID,
764 new_tbl8_entry.next_hop = next_hop;
767 * Setting tbl8 entry in one go to avoid
770 lpm->tbl8[j] = new_tbl8_entry;
781 static __rte_noinline int32_t
782 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
785 #define group_idx next_hop
786 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
788 /* Calculate the index into Table24. */
789 tbl24_index = ip >> 8;
790 tbl24_range = depth_to_range(depth);
792 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
794 * For invalid OR valid and non-extended tbl 24 entries set
797 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
798 lpm->tbl24[i].depth <= depth)) {
800 struct rte_lpm_tbl_entry new_tbl24_entry = {
801 .next_hop = next_hop,
807 /* Setting tbl24 entry in one go to avoid race
810 lpm->tbl24[i] = new_tbl24_entry;
815 if (lpm->tbl24[i].valid_group == 1) {
816 /* If tbl24 entry is valid and extended calculate the
819 tbl8_index = lpm->tbl24[i].group_idx *
820 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
821 tbl8_group_end = tbl8_index +
822 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
824 for (j = tbl8_index; j < tbl8_group_end; j++) {
825 if (!lpm->tbl8[j].valid ||
826 lpm->tbl8[j].depth <= depth) {
827 struct rte_lpm_tbl_entry
830 .valid_group = VALID,
832 .next_hop = next_hop,
836 * Setting tbl8 entry in one go to avoid
839 lpm->tbl8[j] = new_tbl8_entry;
850 static __rte_noinline int32_t
851 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
854 uint32_t tbl24_index;
855 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
858 tbl24_index = (ip_masked >> 8);
859 tbl8_range = depth_to_range(depth);
861 if (!lpm->tbl24[tbl24_index].valid) {
862 /* Search for a free tbl8 group. */
863 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
865 /* Check tbl8 allocation was successful. */
866 if (tbl8_group_index < 0) {
867 return tbl8_group_index;
870 /* Find index into tbl8 and range. */
871 tbl8_index = (tbl8_group_index *
872 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
875 /* Set tbl8 entry. */
876 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
877 lpm->tbl8[i].depth = depth;
878 lpm->tbl8[i].next_hop = next_hop;
879 lpm->tbl8[i].valid = VALID;
883 * Update tbl24 entry to point to new tbl8 entry. Note: The
884 * ext_flag and tbl8_index need to be updated simultaneously,
885 * so assign whole structure in one go
888 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
889 .group_idx = (uint8_t)tbl8_group_index,
895 lpm->tbl24[tbl24_index] = new_tbl24_entry;
897 } /* If valid entry but not extended calculate the index into Table8. */
898 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
899 /* Search for free tbl8 group. */
900 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
902 if (tbl8_group_index < 0) {
903 return tbl8_group_index;
906 tbl8_group_start = tbl8_group_index *
907 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
908 tbl8_group_end = tbl8_group_start +
909 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
911 /* Populate new tbl8 with tbl24 value. */
912 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
913 lpm->tbl8[i].valid = VALID;
914 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
915 lpm->tbl8[i].next_hop =
916 lpm->tbl24[tbl24_index].next_hop;
919 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
921 /* Insert new rule into the tbl8 entry. */
922 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
923 lpm->tbl8[i].valid = VALID;
924 lpm->tbl8[i].depth = depth;
925 lpm->tbl8[i].next_hop = next_hop;
929 * Update tbl24 entry to point to new tbl8 entry. Note: The
930 * ext_flag and tbl8_index need to be updated simultaneously,
931 * so assign whole structure in one go.
934 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
935 .group_idx = (uint8_t)tbl8_group_index,
941 lpm->tbl24[tbl24_index] = new_tbl24_entry;
944 * If it is valid, extended entry calculate the index into tbl8.
946 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
947 tbl8_group_start = tbl8_group_index *
948 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
949 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
951 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
953 if (!lpm->tbl8[i].valid ||
954 lpm->tbl8[i].depth <= depth) {
955 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
958 .valid_group = lpm->tbl8[i].valid_group,
960 new_tbl8_entry.next_hop = next_hop;
962 * Setting tbl8 entry in one go to avoid race
965 lpm->tbl8[i] = new_tbl8_entry;
975 static __rte_noinline int32_t
976 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
979 #define group_idx next_hop
980 uint32_t tbl24_index;
981 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
984 tbl24_index = (ip_masked >> 8);
985 tbl8_range = depth_to_range(depth);
987 if (!lpm->tbl24[tbl24_index].valid) {
988 /* Search for a free tbl8 group. */
989 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
991 /* Check tbl8 allocation was successful. */
992 if (tbl8_group_index < 0) {
993 return tbl8_group_index;
996 /* Find index into tbl8 and range. */
997 tbl8_index = (tbl8_group_index *
998 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1001 /* Set tbl8 entry. */
1002 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1003 lpm->tbl8[i].depth = depth;
1004 lpm->tbl8[i].next_hop = next_hop;
1005 lpm->tbl8[i].valid = VALID;
1009 * Update tbl24 entry to point to new tbl8 entry. Note: The
1010 * ext_flag and tbl8_index need to be updated simultaneously,
1011 * so assign whole structure in one go
1014 struct rte_lpm_tbl_entry new_tbl24_entry = {
1015 .group_idx = tbl8_group_index,
1021 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1023 } /* If valid entry but not extended calculate the index into Table8. */
1024 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1025 /* Search for free tbl8 group. */
1026 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1028 if (tbl8_group_index < 0) {
1029 return tbl8_group_index;
1032 tbl8_group_start = tbl8_group_index *
1033 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1034 tbl8_group_end = tbl8_group_start +
1035 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1037 /* Populate new tbl8 with tbl24 value. */
1038 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1039 lpm->tbl8[i].valid = VALID;
1040 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1041 lpm->tbl8[i].next_hop =
1042 lpm->tbl24[tbl24_index].next_hop;
1045 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1047 /* Insert new rule into the tbl8 entry. */
1048 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1049 lpm->tbl8[i].valid = VALID;
1050 lpm->tbl8[i].depth = depth;
1051 lpm->tbl8[i].next_hop = next_hop;
1055 * Update tbl24 entry to point to new tbl8 entry. Note: The
1056 * ext_flag and tbl8_index need to be updated simultaneously,
1057 * so assign whole structure in one go.
1060 struct rte_lpm_tbl_entry new_tbl24_entry = {
1061 .group_idx = tbl8_group_index,
1067 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1070 * If it is valid, extended entry calculate the index into tbl8.
1072 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1073 tbl8_group_start = tbl8_group_index *
1074 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1075 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1077 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1079 if (!lpm->tbl8[i].valid ||
1080 lpm->tbl8[i].depth <= depth) {
1081 struct rte_lpm_tbl_entry new_tbl8_entry = {
1084 .next_hop = next_hop,
1085 .valid_group = lpm->tbl8[i].valid_group,
1089 * Setting tbl8 entry in one go to avoid race
1092 lpm->tbl8[i] = new_tbl8_entry;
1106 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1109 int32_t rule_index, status = 0;
1112 /* Check user arguments. */
1113 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1116 ip_masked = ip & depth_to_mask(depth);
1118 /* Add the rule to the rule table. */
1119 rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1121 /* If the is no space available for new rule return error. */
1122 if (rule_index < 0) {
1126 if (depth <= MAX_DEPTH_TBL24) {
1127 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1128 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1129 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1132 * If add fails due to exhaustion of tbl8 extensions delete
1133 * rule that was added to rule table.
1136 rule_delete_v20(lpm, rule_index, depth);
1144 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1147 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1150 int32_t rule_index, status = 0;
1153 /* Check user arguments. */
1154 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1157 ip_masked = ip & depth_to_mask(depth);
1159 /* Add the rule to the rule table. */
1160 rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1162 /* If the is no space available for new rule return error. */
1163 if (rule_index < 0) {
1167 if (depth <= MAX_DEPTH_TBL24) {
1168 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1169 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1170 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1173 * If add fails due to exhaustion of tbl8 extensions delete
1174 * rule that was added to rule table.
1177 rule_delete_v1604(lpm, rule_index, depth);
1185 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1186 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1187 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1190 * Look for a rule in the high-level rules table
1193 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1199 /* Check user arguments. */
1200 if ((lpm == NULL) ||
1201 (next_hop == NULL) ||
1202 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1205 /* Look for the rule using rule_find. */
1206 ip_masked = ip & depth_to_mask(depth);
1207 rule_index = rule_find_v20(lpm, ip_masked, depth);
1209 if (rule_index >= 0) {
1210 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1214 /* If rule is not found return 0. */
1217 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1220 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1226 /* Check user arguments. */
1227 if ((lpm == NULL) ||
1228 (next_hop == NULL) ||
1229 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1232 /* Look for the rule using rule_find. */
1233 ip_masked = ip & depth_to_mask(depth);
1234 rule_index = rule_find_v1604(lpm, ip_masked, depth);
1236 if (rule_index >= 0) {
1237 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1241 /* If rule is not found return 0. */
1244 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1245 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1246 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1249 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1250 uint8_t *sub_rule_depth)
1256 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1257 ip_masked = ip & depth_to_mask(prev_depth);
1259 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1261 if (rule_index >= 0) {
1262 *sub_rule_depth = prev_depth;
1271 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1272 uint8_t *sub_rule_depth)
1278 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1279 ip_masked = ip & depth_to_mask(prev_depth);
1281 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1283 if (rule_index >= 0) {
1284 *sub_rule_depth = prev_depth;
1293 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1294 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1296 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1298 /* Calculate the range and index into Table24. */
1299 tbl24_range = depth_to_range(depth);
1300 tbl24_index = (ip_masked >> 8);
1303 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1304 * and a positive number indicates a sub_rule_index.
1306 if (sub_rule_index < 0) {
1308 * If no replacement rule exists then invalidate entries
1309 * associated with this rule.
1311 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1313 if (lpm->tbl24[i].valid_group == 0 &&
1314 lpm->tbl24[i].depth <= depth) {
1315 lpm->tbl24[i].valid = INVALID;
1316 } else if (lpm->tbl24[i].valid_group == 1) {
1318 * If TBL24 entry is extended, then there has
1319 * to be a rule with depth >= 25 in the
1320 * associated TBL8 group.
1323 tbl8_group_index = lpm->tbl24[i].group_idx;
1324 tbl8_index = tbl8_group_index *
1325 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1327 for (j = tbl8_index; j < (tbl8_index +
1328 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1330 if (lpm->tbl8[j].depth <= depth)
1331 lpm->tbl8[j].valid = INVALID;
1337 * If a replacement rule exists then modify entries
1338 * associated with this rule.
1341 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1342 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1345 .depth = sub_rule_depth,
1348 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1350 .valid_group = VALID,
1351 .depth = sub_rule_depth,
1353 new_tbl8_entry.next_hop =
1354 lpm->rules_tbl[sub_rule_index].next_hop;
1356 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1358 if (lpm->tbl24[i].valid_group == 0 &&
1359 lpm->tbl24[i].depth <= depth) {
1360 lpm->tbl24[i] = new_tbl24_entry;
1361 } else if (lpm->tbl24[i].valid_group == 1) {
1363 * If TBL24 entry is extended, then there has
1364 * to be a rule with depth >= 25 in the
1365 * associated TBL8 group.
1368 tbl8_group_index = lpm->tbl24[i].group_idx;
1369 tbl8_index = tbl8_group_index *
1370 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1372 for (j = tbl8_index; j < (tbl8_index +
1373 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1375 if (lpm->tbl8[j].depth <= depth)
1376 lpm->tbl8[j] = new_tbl8_entry;
1386 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1387 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1389 #define group_idx next_hop
1390 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1392 /* Calculate the range and index into Table24. */
1393 tbl24_range = depth_to_range(depth);
1394 tbl24_index = (ip_masked >> 8);
1397 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1398 * and a positive number indicates a sub_rule_index.
1400 if (sub_rule_index < 0) {
1402 * If no replacement rule exists then invalidate entries
1403 * associated with this rule.
1405 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1407 if (lpm->tbl24[i].valid_group == 0 &&
1408 lpm->tbl24[i].depth <= depth) {
1409 lpm->tbl24[i].valid = INVALID;
1410 } else if (lpm->tbl24[i].valid_group == 1) {
1412 * If TBL24 entry is extended, then there has
1413 * to be a rule with depth >= 25 in the
1414 * associated TBL8 group.
1417 tbl8_group_index = lpm->tbl24[i].group_idx;
1418 tbl8_index = tbl8_group_index *
1419 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1421 for (j = tbl8_index; j < (tbl8_index +
1422 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1424 if (lpm->tbl8[j].depth <= depth)
1425 lpm->tbl8[j].valid = INVALID;
1431 * If a replacement rule exists then modify entries
1432 * associated with this rule.
1435 struct rte_lpm_tbl_entry new_tbl24_entry = {
1436 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1439 .depth = sub_rule_depth,
1442 struct rte_lpm_tbl_entry new_tbl8_entry = {
1444 .valid_group = VALID,
1445 .depth = sub_rule_depth,
1446 .next_hop = lpm->rules_tbl
1447 [sub_rule_index].next_hop,
1450 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1452 if (lpm->tbl24[i].valid_group == 0 &&
1453 lpm->tbl24[i].depth <= depth) {
1454 lpm->tbl24[i] = new_tbl24_entry;
1455 } else if (lpm->tbl24[i].valid_group == 1) {
1457 * If TBL24 entry is extended, then there has
1458 * to be a rule with depth >= 25 in the
1459 * associated TBL8 group.
1462 tbl8_group_index = lpm->tbl24[i].group_idx;
1463 tbl8_index = tbl8_group_index *
1464 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1466 for (j = tbl8_index; j < (tbl8_index +
1467 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1469 if (lpm->tbl8[j].depth <= depth)
1470 lpm->tbl8[j] = new_tbl8_entry;
1480 * Checks if table 8 group can be recycled.
1482 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1483 * Return of -EINVAL means tbl8 is empty and thus can be recycled
1484 * Return of value > -1 means tbl8 is in use but has all the same values and
1485 * thus can be recycled
1488 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1489 uint32_t tbl8_group_start)
1491 uint32_t tbl8_group_end, i;
1492 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1495 * Check the first entry of the given tbl8. If it is invalid we know
1496 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1497 * (As they would affect all entries in a tbl8) and thus this table
1498 * can not be recycled.
1500 if (tbl8[tbl8_group_start].valid) {
1502 * If first entry is valid check if the depth is less than 24
1503 * and if so check the rest of the entries to verify that they
1504 * are all of this depth.
1506 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1507 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1510 if (tbl8[i].depth !=
1511 tbl8[tbl8_group_start].depth) {
1516 /* If all entries are the same return the tb8 index */
1517 return tbl8_group_start;
1523 * If the first entry is invalid check if the rest of the entries in
1524 * the tbl8 are invalid.
1526 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1530 /* If no valid entries are found then return -EINVAL. */
1535 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1536 uint32_t tbl8_group_start)
1538 uint32_t tbl8_group_end, i;
1539 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1542 * Check the first entry of the given tbl8. If it is invalid we know
1543 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1544 * (As they would affect all entries in a tbl8) and thus this table
1545 * can not be recycled.
1547 if (tbl8[tbl8_group_start].valid) {
1549 * If first entry is valid check if the depth is less than 24
1550 * and if so check the rest of the entries to verify that they
1551 * are all of this depth.
1553 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1554 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1557 if (tbl8[i].depth !=
1558 tbl8[tbl8_group_start].depth) {
1563 /* If all entries are the same return the tb8 index */
1564 return tbl8_group_start;
1570 * If the first entry is invalid check if the rest of the entries in
1571 * the tbl8 are invalid.
1573 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1577 /* If no valid entries are found then return -EINVAL. */
1582 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1583 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1585 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1587 int32_t tbl8_recycle_index;
1590 * Calculate the index into tbl24 and range. Note: All depths larger
1591 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1593 tbl24_index = ip_masked >> 8;
1595 /* Calculate the index into tbl8 and range. */
1596 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1597 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1598 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1599 tbl8_range = depth_to_range(depth);
1601 if (sub_rule_index < 0) {
1603 * Loop through the range of entries on tbl8 for which the
1604 * rule_to_delete must be removed or modified.
1606 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1607 if (lpm->tbl8[i].depth <= depth)
1608 lpm->tbl8[i].valid = INVALID;
1611 /* Set new tbl8 entry. */
1612 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1614 .depth = sub_rule_depth,
1615 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1618 new_tbl8_entry.next_hop =
1619 lpm->rules_tbl[sub_rule_index].next_hop;
1621 * Loop through the range of entries on tbl8 for which the
1622 * rule_to_delete must be modified.
1624 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1625 if (lpm->tbl8[i].depth <= depth)
1626 lpm->tbl8[i] = new_tbl8_entry;
1631 * Check if there are any valid entries in this tbl8 group. If all
1632 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1633 * associated tbl24 entry.
1636 tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1638 if (tbl8_recycle_index == -EINVAL) {
1639 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1640 lpm->tbl24[tbl24_index].valid = 0;
1641 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1642 } else if (tbl8_recycle_index > -1) {
1643 /* Update tbl24 entry. */
1644 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1645 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1648 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1651 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1652 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1653 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1660 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1661 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1663 #define group_idx next_hop
1664 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1666 int32_t tbl8_recycle_index;
1669 * Calculate the index into tbl24 and range. Note: All depths larger
1670 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1672 tbl24_index = ip_masked >> 8;
1674 /* Calculate the index into tbl8 and range. */
1675 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1676 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1677 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1678 tbl8_range = depth_to_range(depth);
1680 if (sub_rule_index < 0) {
1682 * Loop through the range of entries on tbl8 for which the
1683 * rule_to_delete must be removed or modified.
1685 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1686 if (lpm->tbl8[i].depth <= depth)
1687 lpm->tbl8[i].valid = INVALID;
1690 /* Set new tbl8 entry. */
1691 struct rte_lpm_tbl_entry new_tbl8_entry = {
1693 .depth = sub_rule_depth,
1694 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1695 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1699 * Loop through the range of entries on tbl8 for which the
1700 * rule_to_delete must be modified.
1702 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1703 if (lpm->tbl8[i].depth <= depth)
1704 lpm->tbl8[i] = new_tbl8_entry;
1709 * Check if there are any valid entries in this tbl8 group. If all
1710 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1711 * associated tbl24 entry.
1714 tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1716 if (tbl8_recycle_index == -EINVAL) {
1717 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1718 lpm->tbl24[tbl24_index].valid = 0;
1719 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1720 } else if (tbl8_recycle_index > -1) {
1721 /* Update tbl24 entry. */
1722 struct rte_lpm_tbl_entry new_tbl24_entry = {
1723 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1726 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1729 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1730 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1731 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1741 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1743 int32_t rule_to_delete_index, sub_rule_index;
1745 uint8_t sub_rule_depth;
1747 * Check input arguments. Note: IP must be a positive integer of 32
1748 * bits in length therefore it need not be checked.
1750 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1754 ip_masked = ip & depth_to_mask(depth);
1757 * Find the index of the input rule, that needs to be deleted, in the
1760 rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1763 * Check if rule_to_delete_index was found. If no rule was found the
1764 * function rule_find returns -EINVAL.
1766 if (rule_to_delete_index < 0)
1769 /* Delete the rule from the rule table. */
1770 rule_delete_v20(lpm, rule_to_delete_index, depth);
1773 * Find rule to replace the rule_to_delete. If there is no rule to
1774 * replace the rule_to_delete we return -1 and invalidate the table
1775 * entries associated with this rule.
1778 sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1781 * If the input depth value is less than 25 use function
1782 * delete_depth_small otherwise use delete_depth_big.
1784 if (depth <= MAX_DEPTH_TBL24) {
1785 return delete_depth_small_v20(lpm, ip_masked, depth,
1786 sub_rule_index, sub_rule_depth);
1787 } else { /* If depth > MAX_DEPTH_TBL24 */
1788 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1792 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1795 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1797 int32_t rule_to_delete_index, sub_rule_index;
1799 uint8_t sub_rule_depth;
1801 * Check input arguments. Note: IP must be a positive integer of 32
1802 * bits in length therefore it need not be checked.
1804 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1808 ip_masked = ip & depth_to_mask(depth);
1811 * Find the index of the input rule, that needs to be deleted, in the
1814 rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1817 * Check if rule_to_delete_index was found. If no rule was found the
1818 * function rule_find returns -EINVAL.
1820 if (rule_to_delete_index < 0)
1823 /* Delete the rule from the rule table. */
1824 rule_delete_v1604(lpm, rule_to_delete_index, depth);
1827 * Find rule to replace the rule_to_delete. If there is no rule to
1828 * replace the rule_to_delete we return -1 and invalidate the table
1829 * entries associated with this rule.
1832 sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1835 * If the input depth value is less than 25 use function
1836 * delete_depth_small otherwise use delete_depth_big.
1838 if (depth <= MAX_DEPTH_TBL24) {
1839 return delete_depth_small_v1604(lpm, ip_masked, depth,
1840 sub_rule_index, sub_rule_depth);
1841 } else { /* If depth > MAX_DEPTH_TBL24 */
1842 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1846 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1847 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1848 uint8_t depth), rte_lpm_delete_v1604);
1851 * Delete all rules from the LPM table.
1854 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1856 /* Zero rule information. */
1857 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1860 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1863 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1865 /* Delete all rules form the rules table. */
1866 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1868 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1871 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1873 /* Zero rule information. */
1874 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1877 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1880 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1881 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1883 /* Delete all rules form the rules table. */
1884 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1886 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1887 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1888 rte_lpm_delete_all_v1604);