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>
47 #include <rte_eal_memconfig.h>
48 #include <rte_per_lcore.h>
49 #include <rte_string_fns.h>
50 #include <rte_errno.h>
51 #include <rte_rwlock.h>
52 #include <rte_spinlock.h>
56 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
58 static struct rte_tailq_elem rte_lpm_tailq = {
61 EAL_REGISTER_TAILQ(rte_lpm_tailq)
63 #define MAX_DEPTH_TBL24 24
70 /* Macro to enable/disable run-time checks. */
71 #if defined(RTE_LIBRTE_LPM_DEBUG)
72 #include <rte_debug.h>
73 #define VERIFY_DEPTH(depth) do { \
74 if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH)) \
75 rte_panic("LPM: Invalid depth (%u) at line %d", \
76 (unsigned)(depth), __LINE__); \
79 #define VERIFY_DEPTH(depth)
83 * Converts a given depth value to its corresponding mask value.
85 * depth (IN) : range = 1 - 32
86 * mask (OUT) : 32bit mask
88 static uint32_t __attribute__((pure))
89 depth_to_mask(uint8_t depth)
93 /* To calculate a mask start with a 1 on the left hand side and right
94 * shift while populating the left hand side with 1's
96 return (int)0x80000000 >> (depth - 1);
100 * Converts given depth value to its corresponding range value.
102 static inline uint32_t __attribute__((pure))
103 depth_to_range(uint8_t depth)
108 * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
110 if (depth <= MAX_DEPTH_TBL24)
111 return 1 << (MAX_DEPTH_TBL24 - depth);
113 /* Else if depth is greater than 24 */
114 return 1 << (RTE_LPM_MAX_DEPTH - depth);
118 * Find an existing lpm table and return a pointer to it.
121 rte_lpm_find_existing_v20(const char *name)
123 struct rte_lpm_v20 *l = NULL;
124 struct rte_tailq_entry *te;
125 struct rte_lpm_list *lpm_list;
127 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
129 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
130 TAILQ_FOREACH(te, lpm_list, next) {
131 l = (struct rte_lpm_v20 *) te->data;
132 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
135 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
144 VERSION_SYMBOL(rte_lpm_find_existing, _v20, 2.0);
147 rte_lpm_find_existing_v1604(const char *name)
149 struct rte_lpm *l = NULL;
150 struct rte_tailq_entry *te;
151 struct rte_lpm_list *lpm_list;
153 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
155 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
156 TAILQ_FOREACH(te, lpm_list, next) {
157 l = (struct rte_lpm *) te->data;
158 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
161 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
170 BIND_DEFAULT_SYMBOL(rte_lpm_find_existing, _v1604, 16.04);
171 MAP_STATIC_SYMBOL(struct rte_lpm *rte_lpm_find_existing(const char *name),
172 rte_lpm_find_existing_v1604);
175 * Allocates memory for LPM object
178 rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
179 __rte_unused int flags)
181 char mem_name[RTE_LPM_NAMESIZE];
182 struct rte_lpm_v20 *lpm = NULL;
183 struct rte_tailq_entry *te;
185 struct rte_lpm_list *lpm_list;
187 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
189 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry_v20) != 2);
191 /* Check user arguments. */
192 if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
197 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
199 /* Determine the amount of memory to allocate. */
200 mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
202 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
204 /* guarantee there's no existing */
205 TAILQ_FOREACH(te, lpm_list, next) {
206 lpm = (struct rte_lpm_v20 *) te->data;
207 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
216 /* allocate tailq entry */
217 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
219 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");
234 /* Save user arguments. */
235 lpm->max_rules = max_rules;
236 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
238 te->data = (void *) lpm;
240 TAILQ_INSERT_TAIL(lpm_list, te, next);
243 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
247 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
250 rte_lpm_create_v1604(const char *name, int socket_id,
251 const struct rte_lpm_config *config)
253 char mem_name[RTE_LPM_NAMESIZE];
254 struct rte_lpm *lpm = NULL;
255 struct rte_tailq_entry *te;
256 uint32_t mem_size, rules_size, tbl8s_size;
257 struct rte_lpm_list *lpm_list;
259 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
261 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
263 /* Check user arguments. */
264 if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
265 || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
270 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
272 /* Determine the amount of memory to allocate. */
273 mem_size = sizeof(*lpm);
274 rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
275 tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
276 RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
278 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
280 /* guarantee there's no existing */
281 TAILQ_FOREACH(te, lpm_list, next) {
282 lpm = (struct rte_lpm *) te->data;
283 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
292 /* allocate tailq entry */
293 te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
295 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
300 /* Allocate memory to store the LPM data structures. */
301 lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
302 RTE_CACHE_LINE_SIZE, socket_id);
304 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
310 lpm->rules_tbl = (struct rte_lpm_rule *)rte_zmalloc_socket(NULL,
311 (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
313 if (lpm->rules_tbl == NULL) {
314 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
322 lpm->tbl8 = (struct rte_lpm_tbl_entry *)rte_zmalloc_socket(NULL,
323 (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
325 if (lpm->tbl8 == NULL) {
326 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
327 rte_free(lpm->rules_tbl);
335 /* Save user arguments. */
336 lpm->max_rules = config->max_rules;
337 lpm->number_tbl8s = config->number_tbl8s;
338 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
340 te->data = (void *) lpm;
342 TAILQ_INSERT_TAIL(lpm_list, te, next);
345 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
349 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
351 struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
352 const struct rte_lpm_config *config), rte_lpm_create_v1604);
355 * Deallocates memory for given LPM table.
358 rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
360 struct rte_lpm_list *lpm_list;
361 struct rte_tailq_entry *te;
363 /* Check user arguments. */
367 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
369 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
371 /* find our tailq entry */
372 TAILQ_FOREACH(te, lpm_list, next) {
373 if (te->data == (void *) lpm)
377 TAILQ_REMOVE(lpm_list, te, next);
379 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
384 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
387 rte_lpm_free_v1604(struct rte_lpm *lpm)
389 struct rte_lpm_list *lpm_list;
390 struct rte_tailq_entry *te;
392 /* Check user arguments. */
396 lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
398 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
400 /* find our tailq entry */
401 TAILQ_FOREACH(te, lpm_list, next) {
402 if (te->data == (void *) lpm)
406 TAILQ_REMOVE(lpm_list, te, next);
408 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
411 rte_free(lpm->rules_tbl);
415 BIND_DEFAULT_SYMBOL(rte_lpm_free, _v1604, 16.04);
416 MAP_STATIC_SYMBOL(void rte_lpm_free(struct rte_lpm *lpm),
420 * Adds a rule to the rule table.
422 * NOTE: The rule table is split into 32 groups. Each group contains rules that
423 * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
424 * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
425 * to refer to depth 1 because even though the depth range is 1 - 32, depths
426 * are stored in the rule table from 0 - 31.
427 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
429 static inline int32_t
430 rule_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
433 uint32_t rule_gindex, rule_index, last_rule;
438 /* Scan through rule group to see if rule already exists. */
439 if (lpm->rule_info[depth - 1].used_rules > 0) {
441 /* rule_gindex stands for rule group index. */
442 rule_gindex = lpm->rule_info[depth - 1].first_rule;
443 /* Initialise rule_index to point to start of rule group. */
444 rule_index = rule_gindex;
445 /* Last rule = Last used rule in this rule group. */
446 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
448 for (; rule_index < last_rule; rule_index++) {
450 /* If rule already exists update its next_hop and return. */
451 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
452 lpm->rules_tbl[rule_index].next_hop = next_hop;
458 if (rule_index == lpm->max_rules)
461 /* Calculate the position in which the rule will be stored. */
464 for (i = depth - 1; i > 0; i--) {
465 if (lpm->rule_info[i - 1].used_rules > 0) {
466 rule_index = lpm->rule_info[i - 1].first_rule
467 + lpm->rule_info[i - 1].used_rules;
471 if (rule_index == lpm->max_rules)
474 lpm->rule_info[depth - 1].first_rule = rule_index;
477 /* Make room for the new rule in the array. */
478 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
479 if (lpm->rule_info[i - 1].first_rule
480 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
483 if (lpm->rule_info[i - 1].used_rules > 0) {
484 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
485 + lpm->rule_info[i - 1].used_rules]
486 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
487 lpm->rule_info[i - 1].first_rule++;
491 /* Add the new rule. */
492 lpm->rules_tbl[rule_index].ip = ip_masked;
493 lpm->rules_tbl[rule_index].next_hop = next_hop;
495 /* Increment the used rules counter for this rule group. */
496 lpm->rule_info[depth - 1].used_rules++;
501 static inline int32_t
502 rule_add_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
505 uint32_t rule_gindex, rule_index, last_rule;
510 /* Scan through rule group to see if rule already exists. */
511 if (lpm->rule_info[depth - 1].used_rules > 0) {
513 /* rule_gindex stands for rule group index. */
514 rule_gindex = lpm->rule_info[depth - 1].first_rule;
515 /* Initialise rule_index to point to start of rule group. */
516 rule_index = rule_gindex;
517 /* Last rule = Last used rule in this rule group. */
518 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
520 for (; rule_index < last_rule; rule_index++) {
522 /* If rule already exists update its next_hop and return. */
523 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
524 lpm->rules_tbl[rule_index].next_hop = next_hop;
530 if (rule_index == lpm->max_rules)
533 /* Calculate the position in which the rule will be stored. */
536 for (i = depth - 1; i > 0; i--) {
537 if (lpm->rule_info[i - 1].used_rules > 0) {
538 rule_index = lpm->rule_info[i - 1].first_rule
539 + lpm->rule_info[i - 1].used_rules;
543 if (rule_index == lpm->max_rules)
546 lpm->rule_info[depth - 1].first_rule = rule_index;
549 /* Make room for the new rule in the array. */
550 for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
551 if (lpm->rule_info[i - 1].first_rule
552 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
555 if (lpm->rule_info[i - 1].used_rules > 0) {
556 lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
557 + lpm->rule_info[i - 1].used_rules]
558 = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
559 lpm->rule_info[i - 1].first_rule++;
563 /* Add the new rule. */
564 lpm->rules_tbl[rule_index].ip = ip_masked;
565 lpm->rules_tbl[rule_index].next_hop = next_hop;
567 /* Increment the used rules counter for this rule group. */
568 lpm->rule_info[depth - 1].used_rules++;
574 * Delete a rule from the rule table.
575 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
578 rule_delete_v20(struct rte_lpm_v20 *lpm, int32_t rule_index, uint8_t depth)
584 lpm->rules_tbl[rule_index] =
585 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
586 + lpm->rule_info[depth - 1].used_rules - 1];
588 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
589 if (lpm->rule_info[i].used_rules > 0) {
590 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
591 lpm->rules_tbl[lpm->rule_info[i].first_rule
592 + lpm->rule_info[i].used_rules - 1];
593 lpm->rule_info[i].first_rule--;
597 lpm->rule_info[depth - 1].used_rules--;
601 rule_delete_v1604(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
607 lpm->rules_tbl[rule_index] =
608 lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
609 + lpm->rule_info[depth - 1].used_rules - 1];
611 for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
612 if (lpm->rule_info[i].used_rules > 0) {
613 lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
614 lpm->rules_tbl[lpm->rule_info[i].first_rule
615 + lpm->rule_info[i].used_rules - 1];
616 lpm->rule_info[i].first_rule--;
620 lpm->rule_info[depth - 1].used_rules--;
624 * Finds a rule in rule table.
625 * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
627 static inline int32_t
628 rule_find_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth)
630 uint32_t rule_gindex, last_rule, rule_index;
634 rule_gindex = lpm->rule_info[depth - 1].first_rule;
635 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
637 /* Scan used rules at given depth to find rule. */
638 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
639 /* If rule is found return the rule index. */
640 if (lpm->rules_tbl[rule_index].ip == ip_masked)
644 /* If rule is not found return -EINVAL. */
648 static inline int32_t
649 rule_find_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
651 uint32_t rule_gindex, last_rule, rule_index;
655 rule_gindex = lpm->rule_info[depth - 1].first_rule;
656 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
658 /* Scan used rules at given depth to find rule. */
659 for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
660 /* If rule is found return the rule index. */
661 if (lpm->rules_tbl[rule_index].ip == ip_masked)
665 /* If rule is not found return -EINVAL. */
670 * Find, clean and allocate a tbl8.
672 static inline int32_t
673 tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
675 uint32_t group_idx; /* tbl8 group index. */
676 struct rte_lpm_tbl_entry_v20 *tbl8_entry;
678 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
679 for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
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. */
699 static inline int32_t
700 tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
702 uint32_t group_idx; /* tbl8 group index. */
703 struct rte_lpm_tbl_entry *tbl8_entry;
705 /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
706 for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
707 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
708 /* If a free tbl8 group is found clean it and set as VALID. */
709 if (!tbl8_entry->valid_group) {
710 memset(&tbl8_entry[0], 0,
711 RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
712 sizeof(tbl8_entry[0]));
714 tbl8_entry->valid_group = VALID;
716 /* Return group index for allocated tbl8 group. */
721 /* If there are no tbl8 groups free then return error. */
726 tbl8_free_v20(struct rte_lpm_tbl_entry_v20 *tbl8, uint32_t tbl8_group_start)
728 /* Set tbl8 group invalid*/
729 tbl8[tbl8_group_start].valid_group = INVALID;
733 tbl8_free_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
735 /* Set tbl8 group invalid*/
736 tbl8[tbl8_group_start].valid_group = INVALID;
739 static inline int32_t
740 add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
743 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
745 /* Calculate the index into Table24. */
746 tbl24_index = ip >> 8;
747 tbl24_range = depth_to_range(depth);
749 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
751 * For invalid OR valid and non-extended tbl 24 entries set
754 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
755 lpm->tbl24[i].depth <= depth)) {
757 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
762 new_tbl24_entry.next_hop = next_hop;
764 /* Setting tbl24 entry in one go to avoid race
767 lpm->tbl24[i] = new_tbl24_entry;
772 if (lpm->tbl24[i].valid_group == 1) {
773 /* If tbl24 entry is valid and extended calculate the
776 tbl8_index = lpm->tbl24[i].group_idx *
777 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
778 tbl8_group_end = tbl8_index +
779 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
781 for (j = tbl8_index; j < tbl8_group_end; j++) {
782 if (!lpm->tbl8[j].valid ||
783 lpm->tbl8[j].depth <= depth) {
784 struct rte_lpm_tbl_entry_v20
787 .valid_group = VALID,
790 new_tbl8_entry.next_hop = next_hop;
793 * Setting tbl8 entry in one go to avoid
796 lpm->tbl8[j] = new_tbl8_entry;
807 static inline int32_t
808 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
811 #define group_idx next_hop
812 uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
814 /* Calculate the index into Table24. */
815 tbl24_index = ip >> 8;
816 tbl24_range = depth_to_range(depth);
818 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
820 * For invalid OR valid and non-extended tbl 24 entries set
823 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
824 lpm->tbl24[i].depth <= depth)) {
826 struct rte_lpm_tbl_entry new_tbl24_entry = {
827 .next_hop = next_hop,
833 /* Setting tbl24 entry in one go to avoid race
836 lpm->tbl24[i] = new_tbl24_entry;
841 if (lpm->tbl24[i].valid_group == 1) {
842 /* If tbl24 entry is valid and extended calculate the
845 tbl8_index = lpm->tbl24[i].group_idx *
846 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
847 tbl8_group_end = tbl8_index +
848 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
850 for (j = tbl8_index; j < tbl8_group_end; j++) {
851 if (!lpm->tbl8[j].valid ||
852 lpm->tbl8[j].depth <= depth) {
853 struct rte_lpm_tbl_entry
856 .valid_group = VALID,
858 .next_hop = next_hop,
862 * Setting tbl8 entry in one go to avoid
865 lpm->tbl8[j] = new_tbl8_entry;
876 static inline int32_t
877 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
880 uint32_t tbl24_index;
881 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
884 tbl24_index = (ip_masked >> 8);
885 tbl8_range = depth_to_range(depth);
887 if (!lpm->tbl24[tbl24_index].valid) {
888 /* Search for a free tbl8 group. */
889 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
891 /* Check tbl8 allocation was successful. */
892 if (tbl8_group_index < 0) {
893 return tbl8_group_index;
896 /* Find index into tbl8 and range. */
897 tbl8_index = (tbl8_group_index *
898 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
901 /* Set tbl8 entry. */
902 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
903 lpm->tbl8[i].depth = depth;
904 lpm->tbl8[i].next_hop = next_hop;
905 lpm->tbl8[i].valid = VALID;
909 * Update tbl24 entry to point to new tbl8 entry. Note: The
910 * ext_flag and tbl8_index need to be updated simultaneously,
911 * so assign whole structure in one go
914 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
915 { .group_idx = (uint8_t)tbl8_group_index, },
921 lpm->tbl24[tbl24_index] = new_tbl24_entry;
923 } /* If valid entry but not extended calculate the index into Table8. */
924 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
925 /* Search for free tbl8 group. */
926 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
928 if (tbl8_group_index < 0) {
929 return tbl8_group_index;
932 tbl8_group_start = tbl8_group_index *
933 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
934 tbl8_group_end = tbl8_group_start +
935 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
937 /* Populate new tbl8 with tbl24 value. */
938 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
939 lpm->tbl8[i].valid = VALID;
940 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
941 lpm->tbl8[i].next_hop =
942 lpm->tbl24[tbl24_index].next_hop;
945 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
947 /* Insert new rule into the tbl8 entry. */
948 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
949 lpm->tbl8[i].valid = VALID;
950 lpm->tbl8[i].depth = depth;
951 lpm->tbl8[i].next_hop = next_hop;
955 * Update tbl24 entry to point to new tbl8 entry. Note: The
956 * ext_flag and tbl8_index need to be updated simultaneously,
957 * so assign whole structure in one go.
960 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
961 { .group_idx = (uint8_t)tbl8_group_index, },
967 lpm->tbl24[tbl24_index] = new_tbl24_entry;
970 * If it is valid, extended entry calculate the index into tbl8.
972 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
973 tbl8_group_start = tbl8_group_index *
974 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
975 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
977 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
979 if (!lpm->tbl8[i].valid ||
980 lpm->tbl8[i].depth <= depth) {
981 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
984 .valid_group = lpm->tbl8[i].valid_group,
986 new_tbl8_entry.next_hop = next_hop;
988 * Setting tbl8 entry in one go to avoid race
991 lpm->tbl8[i] = new_tbl8_entry;
1001 static inline int32_t
1002 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
1005 #define group_idx next_hop
1006 uint32_t tbl24_index;
1007 int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
1010 tbl24_index = (ip_masked >> 8);
1011 tbl8_range = depth_to_range(depth);
1013 if (!lpm->tbl24[tbl24_index].valid) {
1014 /* Search for a free tbl8 group. */
1015 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1017 /* Check tbl8 allocation was successful. */
1018 if (tbl8_group_index < 0) {
1019 return tbl8_group_index;
1022 /* Find index into tbl8 and range. */
1023 tbl8_index = (tbl8_group_index *
1024 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1027 /* Set tbl8 entry. */
1028 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1029 lpm->tbl8[i].depth = depth;
1030 lpm->tbl8[i].next_hop = next_hop;
1031 lpm->tbl8[i].valid = VALID;
1035 * Update tbl24 entry to point to new tbl8 entry. Note: The
1036 * ext_flag and tbl8_index need to be updated simultaneously,
1037 * so assign whole structure in one go
1040 struct rte_lpm_tbl_entry new_tbl24_entry = {
1041 .group_idx = tbl8_group_index,
1047 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1049 } /* If valid entry but not extended calculate the index into Table8. */
1050 else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1051 /* Search for free tbl8 group. */
1052 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1054 if (tbl8_group_index < 0) {
1055 return tbl8_group_index;
1058 tbl8_group_start = tbl8_group_index *
1059 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1060 tbl8_group_end = tbl8_group_start +
1061 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1063 /* Populate new tbl8 with tbl24 value. */
1064 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1065 lpm->tbl8[i].valid = VALID;
1066 lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1067 lpm->tbl8[i].next_hop =
1068 lpm->tbl24[tbl24_index].next_hop;
1071 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1073 /* Insert new rule into the tbl8 entry. */
1074 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1075 lpm->tbl8[i].valid = VALID;
1076 lpm->tbl8[i].depth = depth;
1077 lpm->tbl8[i].next_hop = next_hop;
1081 * Update tbl24 entry to point to new tbl8 entry. Note: The
1082 * ext_flag and tbl8_index need to be updated simultaneously,
1083 * so assign whole structure in one go.
1086 struct rte_lpm_tbl_entry new_tbl24_entry = {
1087 .group_idx = tbl8_group_index,
1093 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1096 * If it is valid, extended entry calculate the index into tbl8.
1098 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1099 tbl8_group_start = tbl8_group_index *
1100 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1101 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1103 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1105 if (!lpm->tbl8[i].valid ||
1106 lpm->tbl8[i].depth <= depth) {
1107 struct rte_lpm_tbl_entry new_tbl8_entry = {
1110 .next_hop = next_hop,
1111 .valid_group = lpm->tbl8[i].valid_group,
1115 * Setting tbl8 entry in one go to avoid race
1118 lpm->tbl8[i] = new_tbl8_entry;
1132 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1135 int32_t rule_index, status = 0;
1138 /* Check user arguments. */
1139 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1142 ip_masked = ip & depth_to_mask(depth);
1144 /* Add the rule to the rule table. */
1145 rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1147 /* If the is no space available for new rule return error. */
1148 if (rule_index < 0) {
1152 if (depth <= MAX_DEPTH_TBL24) {
1153 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1154 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1155 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1158 * If add fails due to exhaustion of tbl8 extensions delete
1159 * rule that was added to rule table.
1162 rule_delete_v20(lpm, rule_index, depth);
1170 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1173 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1176 int32_t rule_index, status = 0;
1179 /* Check user arguments. */
1180 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1183 ip_masked = ip & depth_to_mask(depth);
1185 /* Add the rule to the rule table. */
1186 rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1188 /* If the is no space available for new rule return error. */
1189 if (rule_index < 0) {
1193 if (depth <= MAX_DEPTH_TBL24) {
1194 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1195 } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1196 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1199 * If add fails due to exhaustion of tbl8 extensions delete
1200 * rule that was added to rule table.
1203 rule_delete_v1604(lpm, rule_index, depth);
1211 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1212 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1213 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1216 * Look for a rule in the high-level rules table
1219 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1225 /* Check user arguments. */
1226 if ((lpm == NULL) ||
1227 (next_hop == NULL) ||
1228 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1231 /* Look for the rule using rule_find. */
1232 ip_masked = ip & depth_to_mask(depth);
1233 rule_index = rule_find_v20(lpm, ip_masked, depth);
1235 if (rule_index >= 0) {
1236 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1240 /* If rule is not found return 0. */
1243 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1246 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1252 /* Check user arguments. */
1253 if ((lpm == NULL) ||
1254 (next_hop == NULL) ||
1255 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1258 /* Look for the rule using rule_find. */
1259 ip_masked = ip & depth_to_mask(depth);
1260 rule_index = rule_find_v1604(lpm, ip_masked, depth);
1262 if (rule_index >= 0) {
1263 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1267 /* If rule is not found return 0. */
1270 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1271 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1272 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1274 static inline int32_t
1275 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1276 uint8_t *sub_rule_depth)
1282 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1283 ip_masked = ip & depth_to_mask(prev_depth);
1285 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1287 if (rule_index >= 0) {
1288 *sub_rule_depth = prev_depth;
1296 static inline int32_t
1297 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1298 uint8_t *sub_rule_depth)
1304 for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1305 ip_masked = ip & depth_to_mask(prev_depth);
1307 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1309 if (rule_index >= 0) {
1310 *sub_rule_depth = prev_depth;
1318 static inline int32_t
1319 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1320 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1322 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1324 /* Calculate the range and index into Table24. */
1325 tbl24_range = depth_to_range(depth);
1326 tbl24_index = (ip_masked >> 8);
1329 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1330 * and a positive number indicates a sub_rule_index.
1332 if (sub_rule_index < 0) {
1334 * If no replacement rule exists then invalidate entries
1335 * associated with this rule.
1337 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1339 if (lpm->tbl24[i].valid_group == 0 &&
1340 lpm->tbl24[i].depth <= depth) {
1341 lpm->tbl24[i].valid = INVALID;
1342 } else if (lpm->tbl24[i].valid_group == 1) {
1344 * If TBL24 entry is extended, then there has
1345 * to be a rule with depth >= 25 in the
1346 * associated TBL8 group.
1349 tbl8_group_index = lpm->tbl24[i].group_idx;
1350 tbl8_index = tbl8_group_index *
1351 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1353 for (j = tbl8_index; j < (tbl8_index +
1354 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1356 if (lpm->tbl8[j].depth <= depth)
1357 lpm->tbl8[j].valid = INVALID;
1363 * If a replacement rule exists then modify entries
1364 * associated with this rule.
1367 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1368 {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
1371 .depth = sub_rule_depth,
1374 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1376 .valid_group = VALID,
1377 .depth = sub_rule_depth,
1379 new_tbl8_entry.next_hop =
1380 lpm->rules_tbl[sub_rule_index].next_hop;
1382 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1384 if (lpm->tbl24[i].valid_group == 0 &&
1385 lpm->tbl24[i].depth <= depth) {
1386 lpm->tbl24[i] = new_tbl24_entry;
1387 } else if (lpm->tbl24[i].valid_group == 1) {
1389 * If TBL24 entry is extended, then there has
1390 * to be a rule with depth >= 25 in the
1391 * associated TBL8 group.
1394 tbl8_group_index = lpm->tbl24[i].group_idx;
1395 tbl8_index = tbl8_group_index *
1396 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1398 for (j = tbl8_index; j < (tbl8_index +
1399 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1401 if (lpm->tbl8[j].depth <= depth)
1402 lpm->tbl8[j] = new_tbl8_entry;
1411 static inline int32_t
1412 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1413 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1415 #define group_idx next_hop
1416 uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1418 /* Calculate the range and index into Table24. */
1419 tbl24_range = depth_to_range(depth);
1420 tbl24_index = (ip_masked >> 8);
1423 * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1424 * and a positive number indicates a sub_rule_index.
1426 if (sub_rule_index < 0) {
1428 * If no replacement rule exists then invalidate entries
1429 * associated with this rule.
1431 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1433 if (lpm->tbl24[i].valid_group == 0 &&
1434 lpm->tbl24[i].depth <= depth) {
1435 lpm->tbl24[i].valid = INVALID;
1436 } else if (lpm->tbl24[i].valid_group == 1) {
1438 * If TBL24 entry is extended, then there has
1439 * to be a rule with depth >= 25 in the
1440 * associated TBL8 group.
1443 tbl8_group_index = lpm->tbl24[i].group_idx;
1444 tbl8_index = tbl8_group_index *
1445 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1447 for (j = tbl8_index; j < (tbl8_index +
1448 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1450 if (lpm->tbl8[j].depth <= depth)
1451 lpm->tbl8[j].valid = INVALID;
1457 * If a replacement rule exists then modify entries
1458 * associated with this rule.
1461 struct rte_lpm_tbl_entry new_tbl24_entry = {
1462 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1465 .depth = sub_rule_depth,
1468 struct rte_lpm_tbl_entry new_tbl8_entry = {
1470 .valid_group = VALID,
1471 .depth = sub_rule_depth,
1472 .next_hop = lpm->rules_tbl
1473 [sub_rule_index].next_hop,
1476 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1478 if (lpm->tbl24[i].valid_group == 0 &&
1479 lpm->tbl24[i].depth <= depth) {
1480 lpm->tbl24[i] = new_tbl24_entry;
1481 } else if (lpm->tbl24[i].valid_group == 1) {
1483 * If TBL24 entry is extended, then there has
1484 * to be a rule with depth >= 25 in the
1485 * associated TBL8 group.
1488 tbl8_group_index = lpm->tbl24[i].group_idx;
1489 tbl8_index = tbl8_group_index *
1490 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1492 for (j = tbl8_index; j < (tbl8_index +
1493 RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1495 if (lpm->tbl8[j].depth <= depth)
1496 lpm->tbl8[j] = new_tbl8_entry;
1506 * Checks if table 8 group can be recycled.
1508 * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1509 * Return of -EINVAL means tbl8 is empty and thus can be recycled
1510 * Return of value > -1 means tbl8 is in use but has all the same values and
1511 * thus can be recycled
1513 static inline int32_t
1514 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1515 uint32_t tbl8_group_start)
1517 uint32_t tbl8_group_end, i;
1518 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1521 * Check the first entry of the given tbl8. If it is invalid we know
1522 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1523 * (As they would affect all entries in a tbl8) and thus this table
1524 * can not be recycled.
1526 if (tbl8[tbl8_group_start].valid) {
1528 * If first entry is valid check if the depth is less than 24
1529 * and if so check the rest of the entries to verify that they
1530 * are all of this depth.
1532 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1533 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1536 if (tbl8[i].depth !=
1537 tbl8[tbl8_group_start].depth) {
1542 /* If all entries are the same return the tb8 index */
1543 return tbl8_group_start;
1549 * If the first entry is invalid check if the rest of the entries in
1550 * the tbl8 are invalid.
1552 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1556 /* If no valid entries are found then return -EINVAL. */
1560 static inline int32_t
1561 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1562 uint32_t tbl8_group_start)
1564 uint32_t tbl8_group_end, i;
1565 tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1568 * Check the first entry of the given tbl8. If it is invalid we know
1569 * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1570 * (As they would affect all entries in a tbl8) and thus this table
1571 * can not be recycled.
1573 if (tbl8[tbl8_group_start].valid) {
1575 * If first entry is valid check if the depth is less than 24
1576 * and if so check the rest of the entries to verify that they
1577 * are all of this depth.
1579 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1580 for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1583 if (tbl8[i].depth !=
1584 tbl8[tbl8_group_start].depth) {
1589 /* If all entries are the same return the tb8 index */
1590 return tbl8_group_start;
1596 * If the first entry is invalid check if the rest of the entries in
1597 * the tbl8 are invalid.
1599 for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1603 /* If no valid entries are found then return -EINVAL. */
1607 static inline int32_t
1608 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1609 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1611 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1613 int32_t tbl8_recycle_index;
1616 * Calculate the index into tbl24 and range. Note: All depths larger
1617 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1619 tbl24_index = ip_masked >> 8;
1621 /* Calculate the index into tbl8 and range. */
1622 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1623 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1624 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1625 tbl8_range = depth_to_range(depth);
1627 if (sub_rule_index < 0) {
1629 * Loop through the range of entries on tbl8 for which the
1630 * rule_to_delete must be removed or modified.
1632 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1633 if (lpm->tbl8[i].depth <= depth)
1634 lpm->tbl8[i].valid = INVALID;
1637 /* Set new tbl8 entry. */
1638 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1640 .depth = sub_rule_depth,
1641 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1644 new_tbl8_entry.next_hop =
1645 lpm->rules_tbl[sub_rule_index].next_hop;
1647 * Loop through the range of entries on tbl8 for which the
1648 * rule_to_delete must be modified.
1650 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1651 if (lpm->tbl8[i].depth <= depth)
1652 lpm->tbl8[i] = new_tbl8_entry;
1657 * Check if there are any valid entries in this tbl8 group. If all
1658 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1659 * associated tbl24 entry.
1662 tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1664 if (tbl8_recycle_index == -EINVAL) {
1665 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1666 lpm->tbl24[tbl24_index].valid = 0;
1667 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1668 } else if (tbl8_recycle_index > -1) {
1669 /* Update tbl24 entry. */
1670 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1671 { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
1674 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1677 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1678 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1679 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1685 static inline int32_t
1686 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1687 uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1689 #define group_idx next_hop
1690 uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1692 int32_t tbl8_recycle_index;
1695 * Calculate the index into tbl24 and range. Note: All depths larger
1696 * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1698 tbl24_index = ip_masked >> 8;
1700 /* Calculate the index into tbl8 and range. */
1701 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1702 tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1703 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1704 tbl8_range = depth_to_range(depth);
1706 if (sub_rule_index < 0) {
1708 * Loop through the range of entries on tbl8 for which the
1709 * rule_to_delete must be removed or modified.
1711 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1712 if (lpm->tbl8[i].depth <= depth)
1713 lpm->tbl8[i].valid = INVALID;
1716 /* Set new tbl8 entry. */
1717 struct rte_lpm_tbl_entry new_tbl8_entry = {
1719 .depth = sub_rule_depth,
1720 .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1721 .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1725 * Loop through the range of entries on tbl8 for which the
1726 * rule_to_delete must be modified.
1728 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1729 if (lpm->tbl8[i].depth <= depth)
1730 lpm->tbl8[i] = new_tbl8_entry;
1735 * Check if there are any valid entries in this tbl8 group. If all
1736 * tbl8 entries are invalid we can free the tbl8 and invalidate the
1737 * associated tbl24 entry.
1740 tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1742 if (tbl8_recycle_index == -EINVAL) {
1743 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1744 lpm->tbl24[tbl24_index].valid = 0;
1745 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1746 } else if (tbl8_recycle_index > -1) {
1747 /* Update tbl24 entry. */
1748 struct rte_lpm_tbl_entry new_tbl24_entry = {
1749 .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1752 .depth = lpm->tbl8[tbl8_recycle_index].depth,
1755 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1756 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1757 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1767 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1769 int32_t rule_to_delete_index, sub_rule_index;
1771 uint8_t sub_rule_depth;
1773 * Check input arguments. Note: IP must be a positive integer of 32
1774 * bits in length therefore it need not be checked.
1776 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1780 ip_masked = ip & depth_to_mask(depth);
1783 * Find the index of the input rule, that needs to be deleted, in the
1786 rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1789 * Check if rule_to_delete_index was found. If no rule was found the
1790 * function rule_find returns -EINVAL.
1792 if (rule_to_delete_index < 0)
1795 /* Delete the rule from the rule table. */
1796 rule_delete_v20(lpm, rule_to_delete_index, depth);
1799 * Find rule to replace the rule_to_delete. If there is no rule to
1800 * replace the rule_to_delete we return -1 and invalidate the table
1801 * entries associated with this rule.
1804 sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1807 * If the input depth value is less than 25 use function
1808 * delete_depth_small otherwise use delete_depth_big.
1810 if (depth <= MAX_DEPTH_TBL24) {
1811 return delete_depth_small_v20(lpm, ip_masked, depth,
1812 sub_rule_index, sub_rule_depth);
1813 } else { /* If depth > MAX_DEPTH_TBL24 */
1814 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1818 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1821 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1823 int32_t rule_to_delete_index, sub_rule_index;
1825 uint8_t sub_rule_depth;
1827 * Check input arguments. Note: IP must be a positive integer of 32
1828 * bits in length therefore it need not be checked.
1830 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1834 ip_masked = ip & depth_to_mask(depth);
1837 * Find the index of the input rule, that needs to be deleted, in the
1840 rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1843 * Check if rule_to_delete_index was found. If no rule was found the
1844 * function rule_find returns -EINVAL.
1846 if (rule_to_delete_index < 0)
1849 /* Delete the rule from the rule table. */
1850 rule_delete_v1604(lpm, rule_to_delete_index, depth);
1853 * Find rule to replace the rule_to_delete. If there is no rule to
1854 * replace the rule_to_delete we return -1 and invalidate the table
1855 * entries associated with this rule.
1858 sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1861 * If the input depth value is less than 25 use function
1862 * delete_depth_small otherwise use delete_depth_big.
1864 if (depth <= MAX_DEPTH_TBL24) {
1865 return delete_depth_small_v1604(lpm, ip_masked, depth,
1866 sub_rule_index, sub_rule_depth);
1867 } else { /* If depth > MAX_DEPTH_TBL24 */
1868 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1872 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1873 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1874 uint8_t depth), rte_lpm_delete_v1604);
1877 * Delete all rules from the LPM table.
1880 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1882 /* Zero rule information. */
1883 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1886 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1889 memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1891 /* Delete all rules form the rules table. */
1892 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1894 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1897 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1899 /* Zero rule information. */
1900 memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1903 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1906 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1907 * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1909 /* Delete all rules form the rules table. */
1910 memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1912 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1913 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1914 rte_lpm_delete_all_v1604);