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 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
747 if (lpm->tbl24[i].valid_group == 1) {
748 /* If tbl24 entry is valid and extended calculate the
751 tbl8_index = lpm->tbl24[i].group_idx *
752 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
753 tbl8_group_end = tbl8_index +
754 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
756 for (j = tbl8_index; j < tbl8_group_end; j++) {
757 if (!lpm->tbl8[j].valid ||
758 lpm->tbl8[j].depth <= depth) {
759 struct rte_lpm_tbl_entry_v20
762 .valid_group = VALID,
765 new_tbl8_entry.next_hop = next_hop;
768 * Setting tbl8 entry in one go to avoid
771 lpm->tbl8[j] = new_tbl8_entry;
782 static __rte_noinline int32_t
783 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
786 #define group_idx next_hop
787 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
789 /* Calculate the index into Table24. */
790 tbl24_index = ip >> 8;
791 tbl24_range = depth_to_range(depth);
793 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
795 * For invalid OR valid and non-extended tbl 24 entries set
798 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
799 lpm->tbl24[i].depth <= depth)) {
801 struct rte_lpm_tbl_entry new_tbl24_entry = {
802 .next_hop = next_hop,
808 /* Setting tbl24 entry in one go to avoid race
811 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
817 if (lpm->tbl24[i].valid_group == 1) {
818 /* If tbl24 entry is valid and extended calculate the
821 tbl8_index = lpm->tbl24[i].group_idx *
822 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
823 tbl8_group_end = tbl8_index +
824 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
826 for (j = tbl8_index; j < tbl8_group_end; j++) {
827 if (!lpm->tbl8[j].valid ||
828 lpm->tbl8[j].depth <= depth) {
829 struct rte_lpm_tbl_entry
832 .valid_group = VALID,
834 .next_hop = next_hop,
838 * Setting tbl8 entry in one go to avoid
841 lpm->tbl8[j] = new_tbl8_entry;
852 static __rte_noinline int32_t
853 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
856 uint32_t tbl24_index;
857 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
860 tbl24_index = (ip_masked >> 8);
861 tbl8_range = depth_to_range(depth);
863 if (!lpm->tbl24[tbl24_index].valid) {
864 /* Search for a free tbl8 group. */
865 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
867 /* Check tbl8 allocation was successful. */
868 if (tbl8_group_index < 0) {
869 return tbl8_group_index;
872 /* Find index into tbl8 and range. */
873 tbl8_index = (tbl8_group_index *
874 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
877 /* Set tbl8 entry. */
878 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
879 lpm->tbl8[i].depth = depth;
880 lpm->tbl8[i].next_hop = next_hop;
881 lpm->tbl8[i].valid = VALID;
885 * Update tbl24 entry to point to new tbl8 entry. Note: The
886 * ext_flag and tbl8_index need to be updated simultaneously,
887 * so assign whole structure in one go
890 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
891 .group_idx = (uint8_t)tbl8_group_index,
897 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
900 } /* If valid entry but not extended calculate the index into Table8. */
901 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
902 /* Search for free tbl8 group. */
903 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
905 if (tbl8_group_index < 0) {
906 return tbl8_group_index;
909 tbl8_group_start = tbl8_group_index *
910 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
911 tbl8_group_end = tbl8_group_start +
912 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
914 /* Populate new tbl8 with tbl24 value. */
915 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
916 lpm->tbl8[i].valid = VALID;
917 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
918 lpm->tbl8[i].next_hop =
919 lpm->tbl24[tbl24_index].next_hop;
922 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
924 /* Insert new rule into the tbl8 entry. */
925 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
926 lpm->tbl8[i].valid = VALID;
927 lpm->tbl8[i].depth = depth;
928 lpm->tbl8[i].next_hop = next_hop;
932 * Update tbl24 entry to point to new tbl8 entry. Note: The
933 * ext_flag and tbl8_index need to be updated simultaneously,
934 * so assign whole structure in one go.
937 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
938 .group_idx = (uint8_t)tbl8_group_index,
944 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
948 * If it is valid, extended entry calculate the index into tbl8.
950 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
951 tbl8_group_start = tbl8_group_index *
952 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
953 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
955 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
957 if (!lpm->tbl8[i].valid ||
958 lpm->tbl8[i].depth <= depth) {
959 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
962 .valid_group = lpm->tbl8[i].valid_group,
964 new_tbl8_entry.next_hop = next_hop;
966 * Setting tbl8 entry in one go to avoid race
969 lpm->tbl8[i] = new_tbl8_entry;
979 static __rte_noinline int32_t
980 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
983 #define group_idx next_hop
984 uint32_t tbl24_index;
985 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
988 tbl24_index = (ip_masked >> 8);
989 tbl8_range = depth_to_range(depth);
991 if (!lpm->tbl24[tbl24_index].valid) {
992 /* Search for a free tbl8 group. */
993 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
995 /* Check tbl8 allocation was successful. */
996 if (tbl8_group_index < 0) {
997 return tbl8_group_index;
1000 /* Find index into tbl8 and range. */
1001 tbl8_index = (tbl8_group_index *
1002 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1005 /* Set tbl8 entry. */
1006 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1007 lpm->tbl8[i].depth = depth;
1008 lpm->tbl8[i].next_hop = next_hop;
1009 lpm->tbl8[i].valid = VALID;
1013 * Update tbl24 entry to point to new tbl8 entry. Note: The
1014 * ext_flag and tbl8_index need to be updated simultaneously,
1015 * so assign whole structure in one go
1018 struct rte_lpm_tbl_entry new_tbl24_entry = {
1019 .group_idx = tbl8_group_index,
1025 /* The tbl24 entry must be written only after the
1026 * tbl8 entries are written.
1028 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
1031 } /* If valid entry but not extended calculate the index into Table8. */
1032 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1033 /* Search for free tbl8 group. */
1034 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1036 if (tbl8_group_index < 0) {
1037 return tbl8_group_index;
1040 tbl8_group_start = tbl8_group_index *
1041 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1042 tbl8_group_end = tbl8_group_start +
1043 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1045 /* Populate new tbl8 with tbl24 value. */
1046 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1047 lpm->tbl8[i].valid = VALID;
1048 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1049 lpm->tbl8[i].next_hop =
1050 lpm->tbl24[tbl24_index].next_hop;
1053 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1055 /* Insert new rule into the tbl8 entry. */
1056 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1057 lpm->tbl8[i].valid = VALID;
1058 lpm->tbl8[i].depth = depth;
1059 lpm->tbl8[i].next_hop = next_hop;
1063 * Update tbl24 entry to point to new tbl8 entry. Note: The
1064 * ext_flag and tbl8_index need to be updated simultaneously,
1065 * so assign whole structure in one go.
1068 struct rte_lpm_tbl_entry new_tbl24_entry = {
1069 .group_idx = tbl8_group_index,
1075 /* The tbl24 entry must be written only after the
1076 * tbl8 entries are written.
1078 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
1082 * If it is valid, extended entry calculate the index into tbl8.
1084 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1085 tbl8_group_start = tbl8_group_index *
1086 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1087 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1089 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1091 if (!lpm->tbl8[i].valid ||
1092 lpm->tbl8[i].depth <= depth) {
1093 struct rte_lpm_tbl_entry new_tbl8_entry = {
1096 .next_hop = next_hop,
1097 .valid_group = lpm->tbl8[i].valid_group,
1101 * Setting tbl8 entry in one go to avoid race
1104 lpm->tbl8[i] = new_tbl8_entry;
1118 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1121 int32_t rule_index, status = 0;
1124 /* Check user arguments. */
1125 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1128 ip_masked = ip & depth_to_mask(depth);
1130 /* Add the rule to the rule table. */
1131 rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1133 /* If the is no space available for new rule return error. */
1134 if (rule_index < 0) {
1138 if (depth <= MAX_DEPTH_TBL24) {
1139 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1140 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1141 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1144 * If add fails due to exhaustion of tbl8 extensions delete
1145 * rule that was added to rule table.
1148 rule_delete_v20(lpm, rule_index, depth);
1156 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1159 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1162 int32_t rule_index, status = 0;
1165 /* Check user arguments. */
1166 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1169 ip_masked = ip & depth_to_mask(depth);
1171 /* Add the rule to the rule table. */
1172 rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1174 /* If the is no space available for new rule return error. */
1175 if (rule_index < 0) {
1179 if (depth <= MAX_DEPTH_TBL24) {
1180 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1181 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1182 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1185 * If add fails due to exhaustion of tbl8 extensions delete
1186 * rule that was added to rule table.
1189 rule_delete_v1604(lpm, rule_index, depth);
1197 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1198 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1199 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1202 * Look for a rule in the high-level rules table
1205 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1211 /* Check user arguments. */
1212 if ((lpm == NULL) ||
1213 (next_hop == NULL) ||
1214 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1217 /* Look for the rule using rule_find. */
1218 ip_masked = ip & depth_to_mask(depth);
1219 rule_index = rule_find_v20(lpm, ip_masked, depth);
1221 if (rule_index >= 0) {
1222 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1226 /* If rule is not found return 0. */
1229 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1232 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1238 /* Check user arguments. */
1239 if ((lpm == NULL) ||
1240 (next_hop == NULL) ||
1241 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1244 /* Look for the rule using rule_find. */
1245 ip_masked = ip & depth_to_mask(depth);
1246 rule_index = rule_find_v1604(lpm, ip_masked, depth);
1248 if (rule_index >= 0) {
1249 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1253 /* If rule is not found return 0. */
1256 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1257 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1258 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1261 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1262 uint8_t *sub_rule_depth)
1268 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1269 ip_masked = ip & depth_to_mask(prev_depth);
1271 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1273 if (rule_index >= 0) {
1274 *sub_rule_depth = prev_depth;
1283 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1284 uint8_t *sub_rule_depth)
1290 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1291 ip_masked = ip & depth_to_mask(prev_depth);
1293 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1295 if (rule_index >= 0) {
1296 *sub_rule_depth = prev_depth;
1305 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1306 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1308 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1310 /* Calculate the range and index into Table24. */
1311 tbl24_range = depth_to_range(depth);
1312 tbl24_index = (ip_masked >> 8);
1315 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1316 * and a positive number indicates a sub_rule_index.
1318 if (sub_rule_index < 0) {
1320 * If no replacement rule exists then invalidate entries
1321 * associated with this rule.
1323 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1325 if (lpm->tbl24[i].valid_group == 0 &&
1326 lpm->tbl24[i].depth <= depth) {
1327 struct rte_lpm_tbl_entry_v20
1328 zero_tbl24_entry = {
1333 zero_tbl24_entry.next_hop = 0;
1334 __atomic_store(&lpm->tbl24[i],
1335 &zero_tbl24_entry, __ATOMIC_RELEASE);
1336 } else if (lpm->tbl24[i].valid_group == 1) {
1338 * If TBL24 entry is extended, then there has
1339 * to be a rule with depth >= 25 in the
1340 * associated TBL8 group.
1343 tbl8_group_index = lpm->tbl24[i].group_idx;
1344 tbl8_index = tbl8_group_index *
1345 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1347 for (j = tbl8_index; j < (tbl8_index +
1348 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1350 if (lpm->tbl8[j].depth <= depth)
1351 lpm->tbl8[j].valid = INVALID;
1357 * If a replacement rule exists then modify entries
1358 * associated with this rule.
1361 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1362 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1365 .depth = sub_rule_depth,
1368 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1370 .valid_group = VALID,
1371 .depth = sub_rule_depth,
1373 new_tbl8_entry.next_hop =
1374 lpm->rules_tbl[sub_rule_index].next_hop;
1376 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1378 if (lpm->tbl24[i].valid_group == 0 &&
1379 lpm->tbl24[i].depth <= depth) {
1380 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
1382 } else if (lpm->tbl24[i].valid_group == 1) {
1384 * If TBL24 entry is extended, then there has
1385 * to be a rule with depth >= 25 in the
1386 * associated TBL8 group.
1389 tbl8_group_index = lpm->tbl24[i].group_idx;
1390 tbl8_index = tbl8_group_index *
1391 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1393 for (j = tbl8_index; j < (tbl8_index +
1394 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1396 if (lpm->tbl8[j].depth <= depth)
1397 lpm->tbl8[j] = new_tbl8_entry;
1407 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1408 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1410 #define group_idx next_hop
1411 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1413 /* Calculate the range and index into Table24. */
1414 tbl24_range = depth_to_range(depth);
1415 tbl24_index = (ip_masked >> 8);
1416 struct rte_lpm_tbl_entry zero_tbl24_entry = {0};
1419 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1420 * and a positive number indicates a sub_rule_index.
1422 if (sub_rule_index < 0) {
1424 * If no replacement rule exists then invalidate entries
1425 * associated with this rule.
1427 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1429 if (lpm->tbl24[i].valid_group == 0 &&
1430 lpm->tbl24[i].depth <= depth) {
1431 __atomic_store(&lpm->tbl24[i],
1432 &zero_tbl24_entry, __ATOMIC_RELEASE);
1433 } else if (lpm->tbl24[i].valid_group == 1) {
1435 * If TBL24 entry is extended, then there has
1436 * to be a rule with depth >= 25 in the
1437 * associated TBL8 group.
1440 tbl8_group_index = lpm->tbl24[i].group_idx;
1441 tbl8_index = tbl8_group_index *
1442 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1444 for (j = tbl8_index; j < (tbl8_index +
1445 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1447 if (lpm->tbl8[j].depth <= depth)
1448 lpm->tbl8[j].valid = INVALID;
1454 * If a replacement rule exists then modify entries
1455 * associated with this rule.
1458 struct rte_lpm_tbl_entry new_tbl24_entry = {
1459 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1462 .depth = sub_rule_depth,
1465 struct rte_lpm_tbl_entry new_tbl8_entry = {
1467 .valid_group = VALID,
1468 .depth = sub_rule_depth,
1469 .next_hop = lpm->rules_tbl
1470 [sub_rule_index].next_hop,
1473 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1475 if (lpm->tbl24[i].valid_group == 0 &&
1476 lpm->tbl24[i].depth <= depth) {
1477 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
1479 } else if (lpm->tbl24[i].valid_group == 1) {
1481 * If TBL24 entry is extended, then there has
1482 * to be a rule with depth >= 25 in the
1483 * associated TBL8 group.
1486 tbl8_group_index = lpm->tbl24[i].group_idx;
1487 tbl8_index = tbl8_group_index *
1488 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1490 for (j = tbl8_index; j < (tbl8_index +
1491 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1493 if (lpm->tbl8[j].depth <= depth)
1494 lpm->tbl8[j] = new_tbl8_entry;
1504 * Checks if table 8 group can be recycled.
1506 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1507 * Return of -EINVAL means tbl8 is empty and thus can be recycled
1508 * Return of value > -1 means tbl8 is in use but has all the same values and
1509 * thus can be recycled
1512 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1513 uint32_t tbl8_group_start)
1515 uint32_t tbl8_group_end, i;
1516 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1519 * Check the first entry of the given tbl8. If it is invalid we know
1520 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1521 * (As they would affect all entries in a tbl8) and thus this table
1522 * can not be recycled.
1524 if (tbl8[tbl8_group_start].valid) {
1526 * If first entry is valid check if the depth is less than 24
1527 * and if so check the rest of the entries to verify that they
1528 * are all of this depth.
1530 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1531 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1534 if (tbl8[i].depth !=
1535 tbl8[tbl8_group_start].depth) {
1540 /* If all entries are the same return the tb8 index */
1541 return tbl8_group_start;
1547 * If the first entry is invalid check if the rest of the entries in
1548 * the tbl8 are invalid.
1550 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1554 /* If no valid entries are found then return -EINVAL. */
1559 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1560 uint32_t tbl8_group_start)
1562 uint32_t tbl8_group_end, i;
1563 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1566 * Check the first entry of the given tbl8. If it is invalid we know
1567 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1568 * (As they would affect all entries in a tbl8) and thus this table
1569 * can not be recycled.
1571 if (tbl8[tbl8_group_start].valid) {
1573 * If first entry is valid check if the depth is less than 24
1574 * and if so check the rest of the entries to verify that they
1575 * are all of this depth.
1577 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1578 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1581 if (tbl8[i].depth !=
1582 tbl8[tbl8_group_start].depth) {
1587 /* If all entries are the same return the tb8 index */
1588 return tbl8_group_start;
1594 * If the first entry is invalid check if the rest of the entries in
1595 * the tbl8 are invalid.
1597 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1601 /* If no valid entries are found then return -EINVAL. */
1606 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1607 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1609 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1611 int32_t tbl8_recycle_index;
1614 * Calculate the index into tbl24 and range. Note: All depths larger
1615 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1617 tbl24_index = ip_masked >> 8;
1619 /* Calculate the index into tbl8 and range. */
1620 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1621 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1622 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1623 tbl8_range = depth_to_range(depth);
1625 if (sub_rule_index < 0) {
1627 * Loop through the range of entries on tbl8 for which the
1628 * rule_to_delete must be removed or modified.
1630 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1631 if (lpm->tbl8[i].depth <= depth)
1632 lpm->tbl8[i].valid = INVALID;
1635 /* Set new tbl8 entry. */
1636 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1638 .depth = sub_rule_depth,
1639 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1642 new_tbl8_entry.next_hop =
1643 lpm->rules_tbl[sub_rule_index].next_hop;
1645 * Loop through the range of entries on tbl8 for which the
1646 * rule_to_delete must be modified.
1648 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1649 if (lpm->tbl8[i].depth <= depth)
1650 lpm->tbl8[i] = new_tbl8_entry;
1655 * Check if there are any valid entries in this tbl8 group. If all
1656 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1657 * associated tbl24 entry.
1660 tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1662 if (tbl8_recycle_index == -EINVAL) {
1663 /* Set tbl24 before freeing tbl8 to avoid race condition.
1664 * Prevent the free of the tbl8 group from hoisting.
1666 lpm->tbl24[tbl24_index].valid = 0;
1667 __atomic_thread_fence(__ATOMIC_RELEASE);
1668 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1669 } else if (tbl8_recycle_index > -1) {
1670 /* Update tbl24 entry. */
1671 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1672 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1675 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1678 /* Set tbl24 before freeing tbl8 to avoid race condition.
1679 * Prevent the free of the tbl8 group from hoisting.
1681 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1682 __atomic_thread_fence(__ATOMIC_RELEASE);
1683 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1690 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1691 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1693 #define group_idx next_hop
1694 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1696 int32_t tbl8_recycle_index;
1699 * Calculate the index into tbl24 and range. Note: All depths larger
1700 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1702 tbl24_index = ip_masked >> 8;
1704 /* Calculate the index into tbl8 and range. */
1705 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1706 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1707 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1708 tbl8_range = depth_to_range(depth);
1710 if (sub_rule_index < 0) {
1712 * Loop through the range of entries on tbl8 for which the
1713 * rule_to_delete must be removed or modified.
1715 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1716 if (lpm->tbl8[i].depth <= depth)
1717 lpm->tbl8[i].valid = INVALID;
1720 /* Set new tbl8 entry. */
1721 struct rte_lpm_tbl_entry new_tbl8_entry = {
1723 .depth = sub_rule_depth,
1724 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1725 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1729 * Loop through the range of entries on tbl8 for which the
1730 * rule_to_delete must be modified.
1732 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1733 if (lpm->tbl8[i].depth <= depth)
1734 lpm->tbl8[i] = new_tbl8_entry;
1739 * Check if there are any valid entries in this tbl8 group. If all
1740 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1741 * associated tbl24 entry.
1744 tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1746 if (tbl8_recycle_index == -EINVAL) {
1747 /* Set tbl24 before freeing tbl8 to avoid race condition.
1748 * Prevent the free of the tbl8 group from hoisting.
1750 lpm->tbl24[tbl24_index].valid = 0;
1751 __atomic_thread_fence(__ATOMIC_RELEASE);
1752 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1753 } else if (tbl8_recycle_index > -1) {
1754 /* Update tbl24 entry. */
1755 struct rte_lpm_tbl_entry new_tbl24_entry = {
1756 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1759 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1762 /* Set tbl24 before freeing tbl8 to avoid race condition.
1763 * Prevent the free of the tbl8 group from hoisting.
1765 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1766 __atomic_thread_fence(__ATOMIC_RELEASE);
1767 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1777 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1779 int32_t rule_to_delete_index, sub_rule_index;
1781 uint8_t sub_rule_depth;
1783 * Check input arguments. Note: IP must be a positive integer of 32
1784 * bits in length therefore it need not be checked.
1786 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1790 ip_masked = ip & depth_to_mask(depth);
1793 * Find the index of the input rule, that needs to be deleted, in the
1796 rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1799 * Check if rule_to_delete_index was found. If no rule was found the
1800 * function rule_find returns -EINVAL.
1802 if (rule_to_delete_index < 0)
1805 /* Delete the rule from the rule table. */
1806 rule_delete_v20(lpm, rule_to_delete_index, depth);
1809 * Find rule to replace the rule_to_delete. If there is no rule to
1810 * replace the rule_to_delete we return -1 and invalidate the table
1811 * entries associated with this rule.
1814 sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1817 * If the input depth value is less than 25 use function
1818 * delete_depth_small otherwise use delete_depth_big.
1820 if (depth <= MAX_DEPTH_TBL24) {
1821 return delete_depth_small_v20(lpm, ip_masked, depth,
1822 sub_rule_index, sub_rule_depth);
1823 } else { /* If depth > MAX_DEPTH_TBL24 */
1824 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1828 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1831 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1833 int32_t rule_to_delete_index, sub_rule_index;
1835 uint8_t sub_rule_depth;
1837 * Check input arguments. Note: IP must be a positive integer of 32
1838 * bits in length therefore it need not be checked.
1840 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1844 ip_masked = ip & depth_to_mask(depth);
1847 * Find the index of the input rule, that needs to be deleted, in the
1850 rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1853 * Check if rule_to_delete_index was found. If no rule was found the
1854 * function rule_find returns -EINVAL.
1856 if (rule_to_delete_index < 0)
1859 /* Delete the rule from the rule table. */
1860 rule_delete_v1604(lpm, rule_to_delete_index, depth);
1863 * Find rule to replace the rule_to_delete. If there is no rule to
1864 * replace the rule_to_delete we return -1 and invalidate the table
1865 * entries associated with this rule.
1868 sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1871 * If the input depth value is less than 25 use function
1872 * delete_depth_small otherwise use delete_depth_big.
1874 if (depth <= MAX_DEPTH_TBL24) {
1875 return delete_depth_small_v1604(lpm, ip_masked, depth,
1876 sub_rule_index, sub_rule_depth);
1877 } else { /* If depth > MAX_DEPTH_TBL24 */
1878 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1882 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1883 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1884 uint8_t depth), rte_lpm_delete_v1604);
1887 * Delete all rules from the LPM table.
1890 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1892 /* Zero rule information. */
1893 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1896 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1899 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1901 /* Delete all rules form the rules table. */
1902 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1904 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1907 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1909 /* Zero rule information. */
1910 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1913 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1916 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1917 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1919 /* Delete all rules form the rules table. */
1920 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1922 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1923 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1924 rte_lpm_delete_all_v1604);