4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/queue.h>
42 #include <rte_branch_prediction.h>
43 #include <rte_common.h>
44 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
45 #include <rte_malloc.h>
46 #include <rte_memzone.h>
48 #include <rte_eal_memconfig.h>
49 #include <rte_per_lcore.h>
50 #include <rte_string_fns.h>
51 #include <rte_errno.h>
52 #include <rte_rwlock.h>
53 #include <rte_spinlock.h>
57 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
59 static struct rte_tailq_elem rte_lpm_tailq = {
62 EAL_REGISTER_TAILQ(rte_lpm_tailq)
64 #define MAX_DEPTH_TBL24 24
71 /* Macro to enable/disable run-time checks. */
72 #if defined(RTE_LIBRTE_LPM_DEBUG)
73 #include <rte_debug.h>
74 #define VERIFY_DEPTH(depth) do { \
75 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
76 rte_panic("LPM: Invalid depth (%u) at line %d", \
77 (unsigned)(depth), __LINE__); \
80 #define VERIFY_DEPTH(depth)
84 * Converts a given depth value to its corresponding mask value.
86 * depth (IN) : range = 1 - 32
87 * mask (OUT) : 32bit mask
89 static uint32_t __attribute__((pure))
90 depth_to_mask(uint8_t depth)
94 /* To calculate a mask start with a 1 on the left hand side and right
95 * shift while populating the left hand side with 1's
97 return (int)0x80000000 >> (depth - 1);
101 * Converts given depth value to its corresponding range value.
103 static inline uint32_t __attribute__((pure))
104 depth_to_range(uint8_t depth)
109 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
111 if (depth <= MAX_DEPTH_TBL24)
112 return 1 << (MAX_DEPTH_TBL24 - depth);
114 /* Else if depth is greater than 24 */
115 return 1 << (RTE_LPM_MAX_DEPTH - depth);
119 * Find an existing lpm table and return a pointer to it.
122 rte_lpm_find_existing_v20(const char *name)
124 struct rte_lpm_v20 *l = NULL;
125 struct rte_tailq_entry *te;
126 struct rte_lpm_list *lpm_list;
128 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
130 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
131 TAILQ_FOREACH(te, lpm_list, next) {
132 l = (struct rte_lpm_v20 *) te->data;
133 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
136 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
145 VERSION_SYMBOL(rte_lpm_find_existing, _v20, 2.0);
148 rte_lpm_find_existing_v1604(const char *name)
150 struct rte_lpm *l = NULL;
151 struct rte_tailq_entry *te;
152 struct rte_lpm_list *lpm_list;
154 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
156 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
157 TAILQ_FOREACH(te, lpm_list, next) {
158 l = (struct rte_lpm *) te->data;
159 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
162 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
171 BIND_DEFAULT_SYMBOL(rte_lpm_find_existing, _v1604, 16.04);
172 MAP_STATIC_SYMBOL(struct rte_lpm *rte_lpm_find_existing(const char *name),
173 rte_lpm_find_existing_v1604);
176 * Allocates memory for LPM object
179 rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
180 __rte_unused int flags)
182 char mem_name[RTE_LPM_NAMESIZE];
183 struct rte_lpm_v20 *lpm = NULL;
184 struct rte_tailq_entry *te;
186 struct rte_lpm_list *lpm_list;
188 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
190 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry_v20) != 2);
192 /* Check user arguments. */
193 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
198 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
200 /* Determine the amount of memory to allocate. */
201 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
203 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
205 /* guarantee there's no existing */
206 TAILQ_FOREACH(te, lpm_list, next) {
207 lpm = (struct rte_lpm_v20 *) te->data;
208 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
217 /* allocate tailq entry */
218 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
220 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
224 /* Allocate memory to store the LPM data structures. */
225 lpm = (struct rte_lpm_v20 *)rte_zmalloc_socket(mem_name, mem_size,
226 RTE_CACHE_LINE_SIZE, socket_id);
228 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
233 /* Save user arguments. */
234 lpm->max_rules = max_rules;
235 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
237 te->data = (void *) lpm;
239 TAILQ_INSERT_TAIL(lpm_list, te, next);
242 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
246 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
249 rte_lpm_create_v1604(const char *name, int socket_id,
250 const struct rte_lpm_config *config)
252 char mem_name[RTE_LPM_NAMESIZE];
253 struct rte_lpm *lpm = NULL;
254 struct rte_tailq_entry *te;
255 uint32_t mem_size, rules_size, tbl8s_size;
256 struct rte_lpm_list *lpm_list;
258 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
260 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
262 /* Check user arguments. */
263 if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
264 || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
269 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
271 /* Determine the amount of memory to allocate. */
272 mem_size = sizeof(*lpm);
273 rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
274 tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
275 RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
277 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
279 /* guarantee there's no existing */
280 TAILQ_FOREACH(te, lpm_list, next) {
281 lpm = (struct rte_lpm *) te->data;
282 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
291 /* allocate tailq entry */
292 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
294 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
298 /* Allocate memory to store the LPM data structures. */
299 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
300 RTE_CACHE_LINE_SIZE, socket_id);
302 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
307 lpm->rules_tbl = (struct rte_lpm_rule *)rte_zmalloc_socket(NULL,
308 (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
310 if (lpm->rules_tbl == NULL) {
311 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
318 lpm->tbl8 = (struct rte_lpm_tbl_entry *)rte_zmalloc_socket(NULL,
319 (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
321 if (lpm->tbl8 == NULL) {
322 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
323 rte_free(lpm->rules_tbl);
330 /* Save user arguments. */
331 lpm->max_rules = config->max_rules;
332 lpm->number_tbl8s = config->number_tbl8s;
333 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
335 te->data = (void *) lpm;
337 TAILQ_INSERT_TAIL(lpm_list, te, next);
340 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
344 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
346 struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
347 const struct rte_lpm_config *config), rte_lpm_create_v1604);
350 * Deallocates memory for given LPM table.
353 rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
355 struct rte_lpm_list *lpm_list;
356 struct rte_tailq_entry *te;
358 /* Check user arguments. */
362 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
364 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
366 /* find our tailq entry */
367 TAILQ_FOREACH(te, lpm_list, next) {
368 if (te->data == (void *) lpm)
372 TAILQ_REMOVE(lpm_list, te, next);
374 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
379 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
382 rte_lpm_free_v1604(struct rte_lpm *lpm)
384 struct rte_lpm_list *lpm_list;
385 struct rte_tailq_entry *te;
387 /* Check user arguments. */
391 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
393 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
395 /* find our tailq entry */
396 TAILQ_FOREACH(te, lpm_list, next) {
397 if (te->data == (void *) lpm)
401 TAILQ_REMOVE(lpm_list, te, next);
403 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
406 rte_free(lpm->rules_tbl);
410 BIND_DEFAULT_SYMBOL(rte_lpm_free, _v1604, 16.04);
411 MAP_STATIC_SYMBOL(void rte_lpm_free(struct rte_lpm *lpm),
415 * Adds a rule to the rule table.
417 * NOTE: The rule table is split into 32 groups. Each group contains rules that
418 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
419 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
420 * to refer to depth 1 because even though the depth range is 1 - 32, depths
421 * are stored in the rule table from 0 - 31.
422 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
424 static inline int32_t
425 rule_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
428 uint32_t rule_gindex, rule_index, last_rule;
433 /* Scan through rule group to see if rule already exists. */
434 if (lpm->rule_info[depth - 1].used_rules > 0) {
436 /* rule_gindex stands for rule group index. */
437 rule_gindex = lpm->rule_info[depth - 1].first_rule;
438 /* Initialise rule_index to point to start of rule group. */
439 rule_index = rule_gindex;
440 /* Last rule = Last used rule in this rule group. */
441 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
443 for (; rule_index < last_rule; rule_index++) {
445 /* If rule already exists update its next_hop and return. */
446 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
447 lpm->rules_tbl[rule_index].next_hop = next_hop;
453 if (rule_index == lpm->max_rules)
456 /* Calculate the position in which the rule will be stored. */
459 for (i = depth - 1; i > 0; i--) {
460 if (lpm->rule_info[i - 1].used_rules > 0) {
461 rule_index = lpm->rule_info[i - 1].first_rule
462 + lpm->rule_info[i - 1].used_rules;
466 if (rule_index == lpm->max_rules)
469 lpm->rule_info[depth - 1].first_rule = rule_index;
472 /* Make room for the new rule in the array. */
473 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
474 if (lpm->rule_info[i - 1].first_rule
475 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
478 if (lpm->rule_info[i - 1].used_rules > 0) {
479 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
480 + lpm->rule_info[i - 1].used_rules]
481 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
482 lpm->rule_info[i - 1].first_rule++;
486 /* Add the new rule. */
487 lpm->rules_tbl[rule_index].ip = ip_masked;
488 lpm->rules_tbl[rule_index].next_hop = next_hop;
490 /* Increment the used rules counter for this rule group. */
491 lpm->rule_info[depth - 1].used_rules++;
496 static inline int32_t
497 rule_add_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
500 uint32_t rule_gindex, rule_index, last_rule;
505 /* Scan through rule group to see if rule already exists. */
506 if (lpm->rule_info[depth - 1].used_rules > 0) {
508 /* rule_gindex stands for rule group index. */
509 rule_gindex = lpm->rule_info[depth - 1].first_rule;
510 /* Initialise rule_index to point to start of rule group. */
511 rule_index = rule_gindex;
512 /* Last rule = Last used rule in this rule group. */
513 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
515 for (; rule_index < last_rule; rule_index++) {
517 /* If rule already exists update its next_hop and return. */
518 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
519 lpm->rules_tbl[rule_index].next_hop = next_hop;
525 if (rule_index == lpm->max_rules)
528 /* Calculate the position in which the rule will be stored. */
531 for (i = depth - 1; i > 0; i--) {
532 if (lpm->rule_info[i - 1].used_rules > 0) {
533 rule_index = lpm->rule_info[i - 1].first_rule
534 + lpm->rule_info[i - 1].used_rules;
538 if (rule_index == lpm->max_rules)
541 lpm->rule_info[depth - 1].first_rule = rule_index;
544 /* Make room for the new rule in the array. */
545 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
546 if (lpm->rule_info[i - 1].first_rule
547 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
550 if (lpm->rule_info[i - 1].used_rules > 0) {
551 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
552 + lpm->rule_info[i - 1].used_rules]
553 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
554 lpm->rule_info[i - 1].first_rule++;
558 /* Add the new rule. */
559 lpm->rules_tbl[rule_index].ip = ip_masked;
560 lpm->rules_tbl[rule_index].next_hop = next_hop;
562 /* Increment the used rules counter for this rule group. */
563 lpm->rule_info[depth - 1].used_rules++;
569 * Delete a rule from the rule table.
570 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
573 rule_delete_v20(struct rte_lpm_v20 *lpm, int32_t rule_index, uint8_t depth)
579 lpm->rules_tbl[rule_index] =
580 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
581 + lpm->rule_info[depth - 1].used_rules - 1];
583 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
584 if (lpm->rule_info[i].used_rules > 0) {
585 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
586 lpm->rules_tbl[lpm->rule_info[i].first_rule
587 + lpm->rule_info[i].used_rules - 1];
588 lpm->rule_info[i].first_rule--;
592 lpm->rule_info[depth - 1].used_rules--;
596 rule_delete_v1604(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
602 lpm->rules_tbl[rule_index] =
603 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
604 + lpm->rule_info[depth - 1].used_rules - 1];
606 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
607 if (lpm->rule_info[i].used_rules > 0) {
608 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
609 lpm->rules_tbl[lpm->rule_info[i].first_rule
610 + lpm->rule_info[i].used_rules - 1];
611 lpm->rule_info[i].first_rule--;
615 lpm->rule_info[depth - 1].used_rules--;
619 * Finds a rule in rule table.
620 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
622 static inline int32_t
623 rule_find_v20(struct rte_lpm_v20 *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. */
643 static inline int32_t
644 rule_find_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
646 uint32_t rule_gindex, last_rule, rule_index;
650 rule_gindex = lpm->rule_info[depth - 1].first_rule;
651 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
653 /* Scan used rules at given depth to find rule. */
654 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
655 /* If rule is found return the rule index. */
656 if (lpm->rules_tbl[rule_index].ip == ip_masked)
660 /* If rule is not found return -EINVAL. */
665 * Find, clean and allocate a tbl8.
667 static inline int32_t
668 tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
670 uint32_t group_idx; /* tbl8 group index. */
671 struct rte_lpm_tbl_entry_v20 *tbl8_entry;
673 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
674 for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
676 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
677 /* If a free tbl8 group is found clean it and set as VALID. */
678 if (!tbl8_entry->valid_group) {
679 memset(&tbl8_entry[0], 0,
680 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
681 sizeof(tbl8_entry[0]));
683 tbl8_entry->valid_group = VALID;
685 /* Return group index for allocated tbl8 group. */
690 /* If there are no tbl8 groups free then return error. */
694 static inline int32_t
695 tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
697 uint32_t group_idx; /* tbl8 group index. */
698 struct rte_lpm_tbl_entry *tbl8_entry;
700 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
701 for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
702 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
703 /* If a free tbl8 group is found clean it and set as VALID. */
704 if (!tbl8_entry->valid_group) {
705 memset(&tbl8_entry[0], 0,
706 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
707 sizeof(tbl8_entry[0]));
709 tbl8_entry->valid_group = VALID;
711 /* Return group index for allocated tbl8 group. */
716 /* If there are no tbl8 groups free then return error. */
721 tbl8_free_v20(struct rte_lpm_tbl_entry_v20 *tbl8, uint32_t tbl8_group_start)
723 /* Set tbl8 group invalid*/
724 tbl8[tbl8_group_start].valid_group = INVALID;
728 tbl8_free_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
730 /* Set tbl8 group invalid*/
731 tbl8[tbl8_group_start].valid_group = INVALID;
734 static inline int32_t
735 add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
738 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
740 /* Calculate the index into Table24. */
741 tbl24_index = ip >> 8;
742 tbl24_range = depth_to_range(depth);
744 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
746 * For invalid OR valid and non-extended tbl 24 entries set
749 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
750 lpm->tbl24[i].depth <= depth)) {
752 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
757 new_tbl24_entry.next_hop = next_hop;
759 /* Setting tbl24 entry in one go to avoid race
762 lpm->tbl24[i] = new_tbl24_entry;
767 if (lpm->tbl24[i].valid_group == 1) {
768 /* If tbl24 entry is valid and extended calculate the
771 tbl8_index = lpm->tbl24[i].group_idx *
772 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
773 tbl8_group_end = tbl8_index +
774 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
776 for (j = tbl8_index; j < tbl8_group_end; j++) {
777 if (!lpm->tbl8[j].valid ||
778 lpm->tbl8[j].depth <= depth) {
779 struct rte_lpm_tbl_entry_v20
782 .valid_group = VALID,
785 new_tbl8_entry.next_hop = next_hop;
788 * Setting tbl8 entry in one go to avoid
791 lpm->tbl8[j] = new_tbl8_entry;
802 static inline int32_t
803 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
806 #define group_idx next_hop
807 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
809 /* Calculate the index into Table24. */
810 tbl24_index = ip >> 8;
811 tbl24_range = depth_to_range(depth);
813 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
815 * For invalid OR valid and non-extended tbl 24 entries set
818 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
819 lpm->tbl24[i].depth <= depth)) {
821 struct rte_lpm_tbl_entry new_tbl24_entry = {
822 .next_hop = next_hop,
828 /* Setting tbl24 entry in one go to avoid race
831 lpm->tbl24[i] = new_tbl24_entry;
836 if (lpm->tbl24[i].valid_group == 1) {
837 /* If tbl24 entry is valid and extended calculate the
840 tbl8_index = lpm->tbl24[i].group_idx *
841 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
842 tbl8_group_end = tbl8_index +
843 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
845 for (j = tbl8_index; j < tbl8_group_end; j++) {
846 if (!lpm->tbl8[j].valid ||
847 lpm->tbl8[j].depth <= depth) {
848 struct rte_lpm_tbl_entry
851 .valid_group = VALID,
853 .next_hop = next_hop,
857 * Setting tbl8 entry in one go to avoid
860 lpm->tbl8[j] = new_tbl8_entry;
871 static inline int32_t
872 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
875 uint32_t tbl24_index;
876 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
879 tbl24_index = (ip_masked >> 8);
880 tbl8_range = depth_to_range(depth);
882 if (!lpm->tbl24[tbl24_index].valid) {
883 /* Search for a free tbl8 group. */
884 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
886 /* Check tbl8 allocation was successful. */
887 if (tbl8_group_index < 0) {
888 return tbl8_group_index;
891 /* Find index into tbl8 and range. */
892 tbl8_index = (tbl8_group_index *
893 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
896 /* Set tbl8 entry. */
897 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
898 lpm->tbl8[i].depth = depth;
899 lpm->tbl8[i].next_hop = next_hop;
900 lpm->tbl8[i].valid = VALID;
904 * Update tbl24 entry to point to new tbl8 entry. Note: The
905 * ext_flag and tbl8_index need to be updated simultaneously,
906 * so assign whole structure in one go
909 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
910 { .group_idx = (uint8_t)tbl8_group_index, },
916 lpm->tbl24[tbl24_index] = new_tbl24_entry;
918 } /* If valid entry but not extended calculate the index into Table8. */
919 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
920 /* Search for free tbl8 group. */
921 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
923 if (tbl8_group_index < 0) {
924 return tbl8_group_index;
927 tbl8_group_start = tbl8_group_index *
928 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
929 tbl8_group_end = tbl8_group_start +
930 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
932 /* Populate new tbl8 with tbl24 value. */
933 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
934 lpm->tbl8[i].valid = VALID;
935 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
936 lpm->tbl8[i].next_hop =
937 lpm->tbl24[tbl24_index].next_hop;
940 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
942 /* Insert new rule into the tbl8 entry. */
943 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
944 lpm->tbl8[i].valid = VALID;
945 lpm->tbl8[i].depth = depth;
946 lpm->tbl8[i].next_hop = next_hop;
950 * Update tbl24 entry to point to new tbl8 entry. Note: The
951 * ext_flag and tbl8_index need to be updated simultaneously,
952 * so assign whole structure in one go.
955 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
956 { .group_idx = (uint8_t)tbl8_group_index, },
962 lpm->tbl24[tbl24_index] = new_tbl24_entry;
965 * If it is valid, extended entry calculate the index into tbl8.
967 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
968 tbl8_group_start = tbl8_group_index *
969 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
970 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
972 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
974 if (!lpm->tbl8[i].valid ||
975 lpm->tbl8[i].depth <= depth) {
976 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
979 .valid_group = lpm->tbl8[i].valid_group,
981 new_tbl8_entry.next_hop = next_hop;
983 * Setting tbl8 entry in one go to avoid race
986 lpm->tbl8[i] = new_tbl8_entry;
996 static inline int32_t
997 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
1000 #define group_idx next_hop
1001 uint32_t tbl24_index;
1002 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
1005 tbl24_index = (ip_masked >> 8);
1006 tbl8_range = depth_to_range(depth);
1008 if (!lpm->tbl24[tbl24_index].valid) {
1009 /* Search for a free tbl8 group. */
1010 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1012 /* Check tbl8 allocation was successful. */
1013 if (tbl8_group_index < 0) {
1014 return tbl8_group_index;
1017 /* Find index into tbl8 and range. */
1018 tbl8_index = (tbl8_group_index *
1019 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1022 /* Set tbl8 entry. */
1023 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1024 lpm->tbl8[i].depth = depth;
1025 lpm->tbl8[i].next_hop = next_hop;
1026 lpm->tbl8[i].valid = VALID;
1030 * Update tbl24 entry to point to new tbl8 entry. Note: The
1031 * ext_flag and tbl8_index need to be updated simultaneously,
1032 * so assign whole structure in one go
1035 struct rte_lpm_tbl_entry new_tbl24_entry = {
1036 .group_idx = tbl8_group_index,
1042 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1044 } /* If valid entry but not extended calculate the index into Table8. */
1045 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1046 /* Search for free tbl8 group. */
1047 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1049 if (tbl8_group_index < 0) {
1050 return tbl8_group_index;
1053 tbl8_group_start = tbl8_group_index *
1054 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1055 tbl8_group_end = tbl8_group_start +
1056 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1058 /* Populate new tbl8 with tbl24 value. */
1059 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1060 lpm->tbl8[i].valid = VALID;
1061 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1062 lpm->tbl8[i].next_hop =
1063 lpm->tbl24[tbl24_index].next_hop;
1066 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1068 /* Insert new rule into the tbl8 entry. */
1069 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1070 lpm->tbl8[i].valid = VALID;
1071 lpm->tbl8[i].depth = depth;
1072 lpm->tbl8[i].next_hop = next_hop;
1076 * Update tbl24 entry to point to new tbl8 entry. Note: The
1077 * ext_flag and tbl8_index need to be updated simultaneously,
1078 * so assign whole structure in one go.
1081 struct rte_lpm_tbl_entry new_tbl24_entry = {
1082 .group_idx = tbl8_group_index,
1088 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1091 * If it is valid, extended entry calculate the index into tbl8.
1093 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1094 tbl8_group_start = tbl8_group_index *
1095 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1096 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1098 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1100 if (!lpm->tbl8[i].valid ||
1101 lpm->tbl8[i].depth <= depth) {
1102 struct rte_lpm_tbl_entry new_tbl8_entry = {
1105 .next_hop = next_hop,
1106 .valid_group = lpm->tbl8[i].valid_group,
1110 * Setting tbl8 entry in one go to avoid race
1113 lpm->tbl8[i] = new_tbl8_entry;
1127 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1130 int32_t rule_index, status = 0;
1133 /* Check user arguments. */
1134 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1137 ip_masked = ip & depth_to_mask(depth);
1139 /* Add the rule to the rule table. */
1140 rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1142 /* If the is no space available for new rule return error. */
1143 if (rule_index < 0) {
1147 if (depth <= MAX_DEPTH_TBL24) {
1148 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1149 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1150 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1153 * If add fails due to exhaustion of tbl8 extensions delete
1154 * rule that was added to rule table.
1157 rule_delete_v20(lpm, rule_index, depth);
1165 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1168 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1171 int32_t rule_index, status = 0;
1174 /* Check user arguments. */
1175 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1178 ip_masked = ip & depth_to_mask(depth);
1180 /* Add the rule to the rule table. */
1181 rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1183 /* If the is no space available for new rule return error. */
1184 if (rule_index < 0) {
1188 if (depth <= MAX_DEPTH_TBL24) {
1189 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1190 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1191 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1194 * If add fails due to exhaustion of tbl8 extensions delete
1195 * rule that was added to rule table.
1198 rule_delete_v1604(lpm, rule_index, depth);
1206 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1207 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1208 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1211 * Look for a rule in the high-level rules table
1214 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1220 /* Check user arguments. */
1221 if ((lpm == NULL) ||
1222 (next_hop == NULL) ||
1223 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1226 /* Look for the rule using rule_find. */
1227 ip_masked = ip & depth_to_mask(depth);
1228 rule_index = rule_find_v20(lpm, ip_masked, depth);
1230 if (rule_index >= 0) {
1231 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1235 /* If rule is not found return 0. */
1238 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1241 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1247 /* Check user arguments. */
1248 if ((lpm == NULL) ||
1249 (next_hop == NULL) ||
1250 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1253 /* Look for the rule using rule_find. */
1254 ip_masked = ip & depth_to_mask(depth);
1255 rule_index = rule_find_v1604(lpm, ip_masked, depth);
1257 if (rule_index >= 0) {
1258 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1262 /* If rule is not found return 0. */
1265 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1266 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1267 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1269 static inline int32_t
1270 find_previous_rule_v20(struct rte_lpm_v20 *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_v20(lpm, ip_masked, prev_depth);
1282 if (rule_index >= 0) {
1283 *sub_rule_depth = prev_depth;
1291 static inline int32_t
1292 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1293 uint8_t *sub_rule_depth)
1299 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1300 ip_masked = ip & depth_to_mask(prev_depth);
1302 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1304 if (rule_index >= 0) {
1305 *sub_rule_depth = prev_depth;
1313 static inline int32_t
1314 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1315 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1317 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1319 /* Calculate the range and index into Table24. */
1320 tbl24_range = depth_to_range(depth);
1321 tbl24_index = (ip_masked >> 8);
1324 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1325 * and a positive number indicates a sub_rule_index.
1327 if (sub_rule_index < 0) {
1329 * If no replacement rule exists then invalidate entries
1330 * associated with this rule.
1332 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1334 if (lpm->tbl24[i].valid_group == 0 &&
1335 lpm->tbl24[i].depth <= depth) {
1336 lpm->tbl24[i].valid = INVALID;
1337 } else if (lpm->tbl24[i].valid_group == 1) {
1339 * If TBL24 entry is extended, then there has
1340 * to be a rule with depth >= 25 in the
1341 * associated TBL8 group.
1344 tbl8_group_index = lpm->tbl24[i].group_idx;
1345 tbl8_index = tbl8_group_index *
1346 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1348 for (j = tbl8_index; j < (tbl8_index +
1349 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1351 if (lpm->tbl8[j].depth <= depth)
1352 lpm->tbl8[j].valid = INVALID;
1358 * If a replacement rule exists then modify entries
1359 * associated with this rule.
1362 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1363 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
1366 .depth = sub_rule_depth,
1369 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1371 .valid_group = VALID,
1372 .depth = sub_rule_depth,
1374 new_tbl8_entry.next_hop =
1375 lpm->rules_tbl[sub_rule_index].next_hop;
1377 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1379 if (lpm->tbl24[i].valid_group == 0 &&
1380 lpm->tbl24[i].depth <= depth) {
1381 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;
1406 static inline int32_t
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);
1418 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1419 * and a positive number indicates a sub_rule_index.
1421 if (sub_rule_index < 0) {
1423 * If no replacement rule exists then invalidate entries
1424 * associated with this rule.
1426 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1428 if (lpm->tbl24[i].valid_group == 0 &&
1429 lpm->tbl24[i].depth <= depth) {
1430 lpm->tbl24[i].valid = INVALID;
1431 } else if (lpm->tbl24[i].valid_group == 1) {
1433 * If TBL24 entry is extended, then there has
1434 * to be a rule with depth >= 25 in the
1435 * associated TBL8 group.
1438 tbl8_group_index = lpm->tbl24[i].group_idx;
1439 tbl8_index = tbl8_group_index *
1440 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1442 for (j = tbl8_index; j < (tbl8_index +
1443 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1445 if (lpm->tbl8[j].depth <= depth)
1446 lpm->tbl8[j].valid = INVALID;
1452 * If a replacement rule exists then modify entries
1453 * associated with this rule.
1456 struct rte_lpm_tbl_entry new_tbl24_entry = {
1457 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1460 .depth = sub_rule_depth,
1463 struct rte_lpm_tbl_entry new_tbl8_entry = {
1465 .valid_group = VALID,
1466 .depth = sub_rule_depth,
1467 .next_hop = lpm->rules_tbl
1468 [sub_rule_index].next_hop,
1471 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1473 if (lpm->tbl24[i].valid_group == 0 &&
1474 lpm->tbl24[i].depth <= depth) {
1475 lpm->tbl24[i] = new_tbl24_entry;
1476 } else if (lpm->tbl24[i].valid_group == 1) {
1478 * If TBL24 entry is extended, then there has
1479 * to be a rule with depth >= 25 in the
1480 * associated TBL8 group.
1483 tbl8_group_index = lpm->tbl24[i].group_idx;
1484 tbl8_index = tbl8_group_index *
1485 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1487 for (j = tbl8_index; j < (tbl8_index +
1488 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1490 if (lpm->tbl8[j].depth <= depth)
1491 lpm->tbl8[j] = new_tbl8_entry;
1501 * Checks if table 8 group can be recycled.
1503 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1504 * Return of -EINVAL means tbl8 is empty and thus can be recycled
1505 * Return of value > -1 means tbl8 is in use but has all the same values and
1506 * thus can be recycled
1508 static inline int32_t
1509 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1510 uint32_t tbl8_group_start)
1512 uint32_t tbl8_group_end, i;
1513 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1516 * Check the first entry of the given tbl8. If it is invalid we know
1517 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1518 * (As they would affect all entries in a tbl8) and thus this table
1519 * can not be recycled.
1521 if (tbl8[tbl8_group_start].valid) {
1523 * If first entry is valid check if the depth is less than 24
1524 * and if so check the rest of the entries to verify that they
1525 * are all of this depth.
1527 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1528 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1531 if (tbl8[i].depth !=
1532 tbl8[tbl8_group_start].depth) {
1537 /* If all entries are the same return the tb8 index */
1538 return tbl8_group_start;
1544 * If the first entry is invalid check if the rest of the entries in
1545 * the tbl8 are invalid.
1547 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1551 /* If no valid entries are found then return -EINVAL. */
1555 static inline int32_t
1556 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1557 uint32_t tbl8_group_start)
1559 uint32_t tbl8_group_end, i;
1560 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1563 * Check the first entry of the given tbl8. If it is invalid we know
1564 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1565 * (As they would affect all entries in a tbl8) and thus this table
1566 * can not be recycled.
1568 if (tbl8[tbl8_group_start].valid) {
1570 * If first entry is valid check if the depth is less than 24
1571 * and if so check the rest of the entries to verify that they
1572 * are all of this depth.
1574 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1575 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1578 if (tbl8[i].depth !=
1579 tbl8[tbl8_group_start].depth) {
1584 /* If all entries are the same return the tb8 index */
1585 return tbl8_group_start;
1591 * If the first entry is invalid check if the rest of the entries in
1592 * the tbl8 are invalid.
1594 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1598 /* If no valid entries are found then return -EINVAL. */
1602 static inline int32_t
1603 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1604 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1606 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1608 int32_t tbl8_recycle_index;
1611 * Calculate the index into tbl24 and range. Note: All depths larger
1612 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1614 tbl24_index = ip_masked >> 8;
1616 /* Calculate the index into tbl8 and range. */
1617 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1618 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1619 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1620 tbl8_range = depth_to_range(depth);
1622 if (sub_rule_index < 0) {
1624 * Loop through the range of entries on tbl8 for which the
1625 * rule_to_delete must be removed or modified.
1627 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1628 if (lpm->tbl8[i].depth <= depth)
1629 lpm->tbl8[i].valid = INVALID;
1632 /* Set new tbl8 entry. */
1633 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1635 .depth = sub_rule_depth,
1636 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1639 new_tbl8_entry.next_hop =
1640 lpm->rules_tbl[sub_rule_index].next_hop;
1642 * Loop through the range of entries on tbl8 for which the
1643 * rule_to_delete must be modified.
1645 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1646 if (lpm->tbl8[i].depth <= depth)
1647 lpm->tbl8[i] = new_tbl8_entry;
1652 * Check if there are any valid entries in this tbl8 group. If all
1653 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1654 * associated tbl24 entry.
1657 tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1659 if (tbl8_recycle_index == -EINVAL) {
1660 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1661 lpm->tbl24[tbl24_index].valid = 0;
1662 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1663 } else if (tbl8_recycle_index > -1) {
1664 /* Update tbl24 entry. */
1665 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1666 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
1669 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1672 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1673 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1674 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1680 static inline int32_t
1681 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1682 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1684 #define group_idx next_hop
1685 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1687 int32_t tbl8_recycle_index;
1690 * Calculate the index into tbl24 and range. Note: All depths larger
1691 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1693 tbl24_index = ip_masked >> 8;
1695 /* Calculate the index into tbl8 and range. */
1696 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1697 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1698 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1699 tbl8_range = depth_to_range(depth);
1701 if (sub_rule_index < 0) {
1703 * Loop through the range of entries on tbl8 for which the
1704 * rule_to_delete must be removed or modified.
1706 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1707 if (lpm->tbl8[i].depth <= depth)
1708 lpm->tbl8[i].valid = INVALID;
1711 /* Set new tbl8 entry. */
1712 struct rte_lpm_tbl_entry new_tbl8_entry = {
1714 .depth = sub_rule_depth,
1715 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1716 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1720 * Loop through the range of entries on tbl8 for which the
1721 * rule_to_delete must be modified.
1723 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1724 if (lpm->tbl8[i].depth <= depth)
1725 lpm->tbl8[i] = new_tbl8_entry;
1730 * Check if there are any valid entries in this tbl8 group. If all
1731 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1732 * associated tbl24 entry.
1735 tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1737 if (tbl8_recycle_index == -EINVAL) {
1738 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1739 lpm->tbl24[tbl24_index].valid = 0;
1740 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1741 } else if (tbl8_recycle_index > -1) {
1742 /* Update tbl24 entry. */
1743 struct rte_lpm_tbl_entry new_tbl24_entry = {
1744 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1747 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1750 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1751 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1752 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1762 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1764 int32_t rule_to_delete_index, sub_rule_index;
1766 uint8_t sub_rule_depth;
1768 * Check input arguments. Note: IP must be a positive integer of 32
1769 * bits in length therefore it need not be checked.
1771 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1775 ip_masked = ip & depth_to_mask(depth);
1778 * Find the index of the input rule, that needs to be deleted, in the
1781 rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1784 * Check if rule_to_delete_index was found. If no rule was found the
1785 * function rule_find returns -EINVAL.
1787 if (rule_to_delete_index < 0)
1790 /* Delete the rule from the rule table. */
1791 rule_delete_v20(lpm, rule_to_delete_index, depth);
1794 * Find rule to replace the rule_to_delete. If there is no rule to
1795 * replace the rule_to_delete we return -1 and invalidate the table
1796 * entries associated with this rule.
1799 sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1802 * If the input depth value is less than 25 use function
1803 * delete_depth_small otherwise use delete_depth_big.
1805 if (depth <= MAX_DEPTH_TBL24) {
1806 return delete_depth_small_v20(lpm, ip_masked, depth,
1807 sub_rule_index, sub_rule_depth);
1808 } else { /* If depth > MAX_DEPTH_TBL24 */
1809 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1813 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1816 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1818 int32_t rule_to_delete_index, sub_rule_index;
1820 uint8_t sub_rule_depth;
1822 * Check input arguments. Note: IP must be a positive integer of 32
1823 * bits in length therefore it need not be checked.
1825 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1829 ip_masked = ip & depth_to_mask(depth);
1832 * Find the index of the input rule, that needs to be deleted, in the
1835 rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1838 * Check if rule_to_delete_index was found. If no rule was found the
1839 * function rule_find returns -EINVAL.
1841 if (rule_to_delete_index < 0)
1844 /* Delete the rule from the rule table. */
1845 rule_delete_v1604(lpm, rule_to_delete_index, depth);
1848 * Find rule to replace the rule_to_delete. If there is no rule to
1849 * replace the rule_to_delete we return -1 and invalidate the table
1850 * entries associated with this rule.
1853 sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1856 * If the input depth value is less than 25 use function
1857 * delete_depth_small otherwise use delete_depth_big.
1859 if (depth <= MAX_DEPTH_TBL24) {
1860 return delete_depth_small_v1604(lpm, ip_masked, depth,
1861 sub_rule_index, sub_rule_depth);
1862 } else { /* If depth > MAX_DEPTH_TBL24 */
1863 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1867 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1868 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1869 uint8_t depth), rte_lpm_delete_v1604);
1872 * Delete all rules from the LPM table.
1875 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1877 /* Zero rule information. */
1878 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1881 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1884 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1886 /* Delete all rules form the rules table. */
1887 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1889 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1892 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1894 /* Zero rule information. */
1895 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1898 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1901 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1902 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1904 /* Delete all rules form the rules table. */
1905 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1907 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1908 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1909 rte_lpm_delete_all_v1604);