1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
12 #include <rte_branch_prediction.h>
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_malloc.h>
16 #include <rte_memcpy.h>
18 #include <rte_eal_memconfig.h>
19 #include <rte_per_lcore.h>
20 #include <rte_string_fns.h>
21 #include <rte_errno.h>
22 #include <rte_rwlock.h>
23 #include <rte_spinlock.h>
26 #include <rte_jhash.h>
30 #define RTE_LPM6_TBL24_NUM_ENTRIES (1 << 24)
31 #define RTE_LPM6_TBL8_GROUP_NUM_ENTRIES 256
32 #define RTE_LPM6_TBL8_MAX_NUM_GROUPS (1 << 21)
34 #define RTE_LPM6_VALID_EXT_ENTRY_BITMASK 0xA0000000
35 #define RTE_LPM6_LOOKUP_SUCCESS 0x20000000
36 #define RTE_LPM6_TBL8_BITMASK 0x001FFFFF
38 #define ADD_FIRST_BYTE 3
39 #define LOOKUP_FIRST_BYTE 4
41 #define BYTES2_SIZE 16
43 #define RULE_HASH_TABLE_EXTRA_SPACE 64
44 #define TBL24_IND UINT32_MAX
46 #define lpm6_tbl8_gindex next_hop
48 /** Flags for setting an entry as valid/invalid. */
54 TAILQ_HEAD(rte_lpm6_list, rte_tailq_entry);
56 static struct rte_tailq_elem rte_lpm6_tailq = {
59 EAL_REGISTER_TAILQ(rte_lpm6_tailq)
61 /** Tbl entry structure. It is the same for both tbl24 and tbl8 */
62 struct rte_lpm6_tbl_entry {
63 uint32_t next_hop: 21; /**< Next hop / next table to be checked. */
64 uint32_t depth :8; /**< Rule depth. */
67 uint32_t valid :1; /**< Validation flag. */
68 uint32_t valid_group :1; /**< Group validation flag. */
69 uint32_t ext_entry :1; /**< External entry. */
72 /** Rules tbl entry structure. */
73 struct rte_lpm6_rule {
74 uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
75 uint32_t next_hop; /**< Rule next hop. */
76 uint8_t depth; /**< Rule depth. */
79 /** Rules tbl entry key. */
80 struct rte_lpm6_rule_key {
81 uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
82 uint8_t depth; /**< Rule depth. */
86 struct rte_lpm_tbl8_hdr {
87 uint32_t owner_tbl_ind; /**< owner table: TBL24_IND if owner is tbl24,
88 * otherwise index of tbl8
90 uint32_t owner_entry_ind; /**< index of the owner table entry where
91 * pointer to the tbl8 is stored
93 uint32_t ref_cnt; /**< table reference counter */
96 /** LPM6 structure. */
99 char name[RTE_LPM6_NAMESIZE]; /**< Name of the lpm. */
100 uint32_t max_rules; /**< Max number of rules. */
101 uint32_t used_rules; /**< Used rules so far. */
102 uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
105 struct rte_hash *rules_tbl; /**< LPM rules. */
106 struct rte_lpm6_tbl_entry tbl24[RTE_LPM6_TBL24_NUM_ENTRIES]
107 __rte_cache_aligned; /**< LPM tbl24 table. */
109 uint32_t *tbl8_pool; /**< pool of indexes of free tbl8s */
110 uint32_t tbl8_pool_pos; /**< current position in the tbl8 pool */
112 struct rte_lpm_tbl8_hdr *tbl8_hdrs; /* array of tbl8 headers */
114 struct rte_lpm6_tbl_entry tbl8[0]
115 __rte_cache_aligned; /**< LPM tbl8 table. */
119 * Takes an array of uint8_t (IPv6 address) and masks it using the depth.
120 * It leaves untouched one bit per unit in the depth variable
121 * and set the rest to 0.
124 ip6_mask_addr(uint8_t *ip, uint8_t depth)
126 int16_t part_depth, mask;
131 for (i = 0; i < RTE_LPM6_IPV6_ADDR_SIZE; i++) {
132 if (part_depth < BYTE_SIZE && part_depth >= 0) {
133 mask = (uint16_t)(~(UINT8_MAX >> part_depth));
134 ip[i] = (uint8_t)(ip[i] & mask);
135 } else if (part_depth < 0)
138 part_depth -= BYTE_SIZE;
142 /* copy ipv6 address */
144 ip6_copy_addr(uint8_t *dst, const uint8_t *src)
146 rte_memcpy(dst, src, RTE_LPM6_IPV6_ADDR_SIZE);
150 * LPM6 rule hash function
152 * It's used as a hash function for the rte_hash
155 static inline uint32_t
156 rule_hash(const void *data, __rte_unused uint32_t data_len,
159 return rte_jhash(data, sizeof(struct rte_lpm6_rule_key), init_val);
163 * Init pool of free tbl8 indexes
166 tbl8_pool_init(struct rte_lpm6 *lpm)
170 /* put entire range of indexes to the tbl8 pool */
171 for (i = 0; i < lpm->number_tbl8s; i++)
172 lpm->tbl8_pool[i] = i;
174 lpm->tbl8_pool_pos = 0;
178 * Get an index of a free tbl8 from the pool
180 static inline uint32_t
181 tbl8_get(struct rte_lpm6 *lpm, uint32_t *tbl8_ind)
183 if (lpm->tbl8_pool_pos == lpm->number_tbl8s)
184 /* no more free tbl8 */
188 *tbl8_ind = lpm->tbl8_pool[lpm->tbl8_pool_pos++];
193 * Put an index of a free tbl8 back to the pool
195 static inline uint32_t
196 tbl8_put(struct rte_lpm6 *lpm, uint32_t tbl8_ind)
198 if (lpm->tbl8_pool_pos == 0)
202 lpm->tbl8_pool[--lpm->tbl8_pool_pos] = tbl8_ind;
207 * Returns number of tbl8s available in the pool
209 static inline uint32_t
210 tbl8_available(struct rte_lpm6 *lpm)
212 return lpm->number_tbl8s - lpm->tbl8_pool_pos;
217 * note that ip must be already masked
220 rule_key_init(struct rte_lpm6_rule_key *key, uint8_t *ip, uint8_t depth)
222 ip6_copy_addr(key->ip, ip);
227 * Rebuild the entire LPM tree by reinserting all rules
230 rebuild_lpm(struct rte_lpm6 *lpm)
233 struct rte_lpm6_rule_key *rule_key;
236 while (rte_hash_iterate(lpm->rules_tbl, (void *) &rule_key,
237 (void **) &next_hop, &iter) >= 0)
238 rte_lpm6_add(lpm, rule_key->ip, rule_key->depth,
239 (uint32_t) next_hop);
243 * Allocates memory for LPM object
246 rte_lpm6_create(const char *name, int socket_id,
247 const struct rte_lpm6_config *config)
249 char mem_name[RTE_LPM6_NAMESIZE];
250 struct rte_lpm6 *lpm = NULL;
251 struct rte_tailq_entry *te;
253 struct rte_lpm6_list *lpm_list;
254 struct rte_hash *rules_tbl = NULL;
255 uint32_t *tbl8_pool = NULL;
256 struct rte_lpm_tbl8_hdr *tbl8_hdrs = NULL;
258 lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
260 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_tbl_entry) != sizeof(uint32_t));
262 /* Check user arguments. */
263 if ((name == NULL) || (socket_id < -1) || (config == NULL) ||
264 (config->max_rules == 0) ||
265 config->number_tbl8s > RTE_LPM6_TBL8_MAX_NUM_GROUPS) {
270 /* create rules hash table */
271 snprintf(mem_name, sizeof(mem_name), "LRH_%s", name);
272 struct rte_hash_parameters rule_hash_tbl_params = {
273 .entries = config->max_rules * 1.2 +
274 RULE_HASH_TABLE_EXTRA_SPACE,
275 .key_len = sizeof(struct rte_lpm6_rule_key),
276 .hash_func = rule_hash,
277 .hash_func_init_val = 0,
280 .socket_id = socket_id,
284 rules_tbl = rte_hash_create(&rule_hash_tbl_params);
285 if (rules_tbl == NULL) {
286 RTE_LOG(ERR, LPM, "LPM rules hash table allocation failed: %s (%d)",
287 rte_strerror(rte_errno), rte_errno);
291 /* allocate tbl8 indexes pool */
292 tbl8_pool = rte_malloc(NULL,
293 sizeof(uint32_t) * config->number_tbl8s,
294 RTE_CACHE_LINE_SIZE);
295 if (tbl8_pool == NULL) {
296 RTE_LOG(ERR, LPM, "LPM tbl8 pool allocation failed: %s (%d)",
297 rte_strerror(rte_errno), rte_errno);
302 /* allocate tbl8 headers */
303 tbl8_hdrs = rte_malloc(NULL,
304 sizeof(struct rte_lpm_tbl8_hdr) * config->number_tbl8s,
305 RTE_CACHE_LINE_SIZE);
306 if (tbl8_hdrs == NULL) {
307 RTE_LOG(ERR, LPM, "LPM tbl8 headers allocation failed: %s (%d)",
308 rte_strerror(rte_errno), rte_errno);
313 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
315 /* Determine the amount of memory to allocate. */
316 mem_size = sizeof(*lpm) + (sizeof(lpm->tbl8[0]) *
317 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
319 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
321 /* Guarantee there's no existing */
322 TAILQ_FOREACH(te, lpm_list, next) {
323 lpm = (struct rte_lpm6 *) te->data;
324 if (strncmp(name, lpm->name, RTE_LPM6_NAMESIZE) == 0)
333 /* allocate tailq entry */
334 te = rte_zmalloc("LPM6_TAILQ_ENTRY", sizeof(*te), 0);
336 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry!\n");
341 /* Allocate memory to store the LPM data structures. */
342 lpm = rte_zmalloc_socket(mem_name, (size_t)mem_size,
343 RTE_CACHE_LINE_SIZE, socket_id);
346 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
352 /* Save user arguments. */
353 lpm->max_rules = config->max_rules;
354 lpm->number_tbl8s = config->number_tbl8s;
355 snprintf(lpm->name, sizeof(lpm->name), "%s", name);
356 lpm->rules_tbl = rules_tbl;
357 lpm->tbl8_pool = tbl8_pool;
358 lpm->tbl8_hdrs = tbl8_hdrs;
363 te->data = (void *) lpm;
365 TAILQ_INSERT_TAIL(lpm_list, te, next);
366 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
370 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
375 rte_hash_free(rules_tbl);
381 * Find an existing lpm table and return a pointer to it.
384 rte_lpm6_find_existing(const char *name)
386 struct rte_lpm6 *l = NULL;
387 struct rte_tailq_entry *te;
388 struct rte_lpm6_list *lpm_list;
390 lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
392 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
393 TAILQ_FOREACH(te, lpm_list, next) {
394 l = (struct rte_lpm6 *) te->data;
395 if (strncmp(name, l->name, RTE_LPM6_NAMESIZE) == 0)
398 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
409 * Deallocates memory for given LPM table.
412 rte_lpm6_free(struct rte_lpm6 *lpm)
414 struct rte_lpm6_list *lpm_list;
415 struct rte_tailq_entry *te;
417 /* Check user arguments. */
421 lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
423 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
425 /* find our tailq entry */
426 TAILQ_FOREACH(te, lpm_list, next) {
427 if (te->data == (void *) lpm)
432 TAILQ_REMOVE(lpm_list, te, next);
434 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
436 rte_free(lpm->tbl8_hdrs);
437 rte_free(lpm->tbl8_pool);
438 rte_hash_free(lpm->rules_tbl);
445 rule_find_with_key(struct rte_lpm6 *lpm,
446 const struct rte_lpm6_rule_key *rule_key,
452 /* lookup for a rule */
453 ret = rte_hash_lookup_data(lpm->rules_tbl, (const void *) rule_key,
454 (void **) &hash_val);
456 *next_hop = (uint32_t) hash_val;
465 rule_find(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
468 struct rte_lpm6_rule_key rule_key;
470 /* init a rule key */
471 rule_key_init(&rule_key, ip, depth);
473 return rule_find_with_key(lpm, &rule_key, next_hop);
477 * Checks if a rule already exists in the rules table and updates
478 * the nexthop if so. Otherwise it adds a new rule if enough space is available.
481 * 0 - next hop of existed rule is updated
482 * 1 - new rule successfully added
486 rule_add(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth, uint32_t next_hop)
489 struct rte_lpm6_rule_key rule_key;
492 /* init a rule key */
493 rule_key_init(&rule_key, ip, depth);
495 /* Scan through rule list to see if rule already exists. */
496 rule_exist = rule_find_with_key(lpm, &rule_key, &unused);
499 * If rule does not exist check if there is space to add a new rule to
500 * this rule group. If there is no space return error.
502 if (!rule_exist && lpm->used_rules == lpm->max_rules)
505 /* add the rule or update rules next hop */
506 ret = rte_hash_add_key_data(lpm->rules_tbl, &rule_key,
507 (void *)(uintptr_t) next_hop);
511 /* Increment the used rules counter for this rule group. */
521 * Function that expands a rule across the data structure when a less-generic
522 * one has been added before. It assures that every possible combination of bits
523 * in the IP address returns a match.
526 expand_rule(struct rte_lpm6 *lpm, uint32_t tbl8_gindex, uint8_t old_depth,
527 uint8_t new_depth, uint32_t next_hop, uint8_t valid)
529 uint32_t tbl8_group_end, tbl8_gindex_next, j;
531 tbl8_group_end = tbl8_gindex + RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
533 struct rte_lpm6_tbl_entry new_tbl8_entry = {
535 .valid_group = valid,
537 .next_hop = next_hop,
541 for (j = tbl8_gindex; j < tbl8_group_end; j++) {
542 if (!lpm->tbl8[j].valid || (lpm->tbl8[j].ext_entry == 0
543 && lpm->tbl8[j].depth <= old_depth)) {
545 lpm->tbl8[j] = new_tbl8_entry;
547 } else if (lpm->tbl8[j].ext_entry == 1) {
549 tbl8_gindex_next = lpm->tbl8[j].lpm6_tbl8_gindex
550 * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
551 expand_rule(lpm, tbl8_gindex_next, old_depth, new_depth,
561 init_tbl8_header(struct rte_lpm6 *lpm, uint32_t tbl_ind,
562 uint32_t owner_tbl_ind, uint32_t owner_entry_ind)
564 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
565 tbl_hdr->owner_tbl_ind = owner_tbl_ind;
566 tbl_hdr->owner_entry_ind = owner_entry_ind;
567 tbl_hdr->ref_cnt = 0;
571 * Calculate index to the table based on the number and position
572 * of the bytes being inspected in this step.
575 get_bitshift(const uint8_t *ip, uint8_t first_byte, uint8_t bytes)
577 uint32_t entry_ind, i;
581 for (i = first_byte; i < (uint32_t)(first_byte + bytes); i++) {
582 bitshift = (int8_t)((bytes - i)*BYTE_SIZE);
586 entry_ind = entry_ind | ip[i-1] << bitshift;
593 * Simulate adding a new route to the LPM counting number
594 * of new tables that will be needed
596 * It returns 0 on success, or 1 if
597 * the process needs to be continued by calling the function again.
600 simulate_add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
601 struct rte_lpm6_tbl_entry **next_tbl, const uint8_t *ip,
602 uint8_t bytes, uint8_t first_byte, uint8_t depth,
603 uint32_t *need_tbl_nb)
606 uint8_t bits_covered;
607 uint32_t next_tbl_ind;
610 * Calculate index to the table based on the number and position
611 * of the bytes being inspected in this step.
613 entry_ind = get_bitshift(ip, first_byte, bytes);
615 /* Number of bits covered in this step */
616 bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
618 if (depth <= bits_covered) {
623 if (tbl[entry_ind].valid == 0 || tbl[entry_ind].ext_entry == 0) {
624 /* from this point on a new table is needed on each level
625 * that is not covered yet
627 depth -= bits_covered;
628 uint32_t cnt = depth >> 3; /* depth / BYTE_SIZE */
629 if (depth & 7) /* 0b00000111 */
630 /* if depth % 8 > 0 then one more table is needed
631 * for those last bits
639 next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
640 *next_tbl = &(lpm->tbl8[next_tbl_ind *
641 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
647 * Partially adds a new route to the data structure (tbl24+tbl8s).
648 * It returns 0 on success, a negative number on failure, or 1 if
649 * the process needs to be continued by calling the function again.
652 add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
653 uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
654 uint32_t *next_tbl_ind, uint8_t *ip, uint8_t bytes,
655 uint8_t first_byte, uint8_t depth, uint32_t next_hop,
658 uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
659 uint32_t tbl8_gindex;
660 uint8_t bits_covered;
664 * Calculate index to the table based on the number and position
665 * of the bytes being inspected in this step.
667 entry_ind = get_bitshift(ip, first_byte, bytes);
669 /* Number of bits covered in this step */
670 bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
673 * If depth if smaller than this number (ie this is the last step)
674 * expand the rule across the relevant positions in the table.
676 if (depth <= bits_covered) {
677 tbl_range = 1 << (bits_covered - depth);
679 for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
680 if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
681 tbl[i].depth <= depth)) {
683 struct rte_lpm6_tbl_entry new_tbl_entry = {
684 .next_hop = next_hop,
687 .valid_group = VALID,
691 tbl[i] = new_tbl_entry;
693 } else if (tbl[i].ext_entry == 1) {
696 * If tbl entry is valid and extended calculate the index
697 * into next tbl8 and expand the rule across the data structure.
699 tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
700 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
701 expand_rule(lpm, tbl8_gindex, depth, depth,
706 /* update tbl8 rule reference counter */
707 if (tbl_ind != TBL24_IND && is_new_rule)
708 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
713 * If this is not the last step just fill one position
714 * and calculate the index to the next table.
717 /* If it's invalid a new tbl8 is needed */
718 if (!tbl[entry_ind].valid) {
719 /* get a new table */
720 ret = tbl8_get(lpm, &tbl8_gindex);
724 /* invalidate all new tbl8 entries */
725 tbl8_group_start = tbl8_gindex *
726 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
727 memset(&lpm->tbl8[tbl8_group_start], 0,
728 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
730 /* init the new table's header:
731 * save the reference to the owner table
733 init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
735 /* reference to a new tbl8 */
736 struct rte_lpm6_tbl_entry new_tbl_entry = {
737 .lpm6_tbl8_gindex = tbl8_gindex,
740 .valid_group = VALID,
744 tbl[entry_ind] = new_tbl_entry;
746 /* update the current table's reference counter */
747 if (tbl_ind != TBL24_IND)
748 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
751 * If it's valid but not extended the rule that was stored
752 * here needs to be moved to the next table.
754 else if (tbl[entry_ind].ext_entry == 0) {
755 /* get a new tbl8 index */
756 ret = tbl8_get(lpm, &tbl8_gindex);
760 tbl8_group_start = tbl8_gindex *
761 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
762 tbl8_group_end = tbl8_group_start +
763 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
765 struct rte_lpm6_tbl_entry tbl_entry = {
766 .next_hop = tbl[entry_ind].next_hop,
767 .depth = tbl[entry_ind].depth,
769 .valid_group = VALID,
773 /* Populate new tbl8 with tbl value. */
774 for (i = tbl8_group_start; i < tbl8_group_end; i++)
775 lpm->tbl8[i] = tbl_entry;
777 /* init the new table's header:
778 * save the reference to the owner table
780 init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
783 * Update tbl entry to point to new tbl8 entry. Note: The
784 * ext_flag and tbl8_index need to be updated simultaneously,
785 * so assign whole structure in one go.
787 struct rte_lpm6_tbl_entry new_tbl_entry = {
788 .lpm6_tbl8_gindex = tbl8_gindex,
791 .valid_group = VALID,
795 tbl[entry_ind] = new_tbl_entry;
797 /* update the current table's reference counter */
798 if (tbl_ind != TBL24_IND)
799 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
802 *next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
803 *next_tbl = &(lpm->tbl8[*next_tbl_ind *
804 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
814 rte_lpm6_add_v20(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
817 return rte_lpm6_add_v1705(lpm, ip, depth, next_hop);
819 VERSION_SYMBOL(rte_lpm6_add, _v20, 2.0);
823 * Simulate adding a route to LPM
827 * -ENOSPC not enought tbl8 left
830 simulate_add(struct rte_lpm6 *lpm, const uint8_t *masked_ip, uint8_t depth)
832 struct rte_lpm6_tbl_entry *tbl;
833 struct rte_lpm6_tbl_entry *tbl_next = NULL;
836 /* number of new tables needed for a step */
837 uint32_t need_tbl_nb;
838 /* total number of new tables needed */
839 uint32_t total_need_tbl_nb;
841 /* Inspect the first three bytes through tbl24 on the first step. */
842 ret = simulate_add_step(lpm, lpm->tbl24, &tbl_next, masked_ip,
843 ADD_FIRST_BYTE, 1, depth, &need_tbl_nb);
844 total_need_tbl_nb = need_tbl_nb;
846 * Inspect one by one the rest of the bytes until
847 * the process is completed.
849 for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && ret == 1; i++) {
851 ret = simulate_add_step(lpm, tbl, &tbl_next, masked_ip, 1,
852 (uint8_t)(i+1), depth, &need_tbl_nb);
853 total_need_tbl_nb += need_tbl_nb;
856 if (tbl8_available(lpm) < total_need_tbl_nb)
857 /* not enought tbl8 to add a rule */
864 rte_lpm6_add_v1705(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
867 struct rte_lpm6_tbl_entry *tbl;
868 struct rte_lpm6_tbl_entry *tbl_next = NULL;
869 /* init to avoid compiler warning */
870 uint32_t tbl_next_num = 123456;
872 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
875 /* Check user arguments. */
876 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
879 /* Copy the IP and mask it to avoid modifying user's input data. */
880 ip6_copy_addr(masked_ip, ip);
881 ip6_mask_addr(masked_ip, depth);
883 /* Simulate adding a new route */
884 int ret = simulate_add(lpm, masked_ip, depth);
888 /* Add the rule to the rule table. */
889 int is_new_rule = rule_add(lpm, masked_ip, depth, next_hop);
890 /* If there is no space available for new rule return error. */
894 /* Inspect the first three bytes through tbl24 on the first step. */
896 status = add_step(lpm, tbl, TBL24_IND, &tbl_next, &tbl_next_num,
897 masked_ip, ADD_FIRST_BYTE, 1, depth, next_hop,
902 * Inspect one by one the rest of the bytes until
903 * the process is completed.
905 for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && status == 1; i++) {
907 status = add_step(lpm, tbl, tbl_next_num, &tbl_next,
908 &tbl_next_num, masked_ip, 1, (uint8_t)(i+1),
909 depth, next_hop, is_new_rule);
915 BIND_DEFAULT_SYMBOL(rte_lpm6_add, _v1705, 17.05);
916 MAP_STATIC_SYMBOL(int rte_lpm6_add(struct rte_lpm6 *lpm, uint8_t *ip,
917 uint8_t depth, uint32_t next_hop),
921 * Takes a pointer to a table entry and inspect one level.
922 * The function returns 0 on lookup success, ENOENT if no match was found
923 * or 1 if the process needs to be continued by calling the function again.
926 lookup_step(const struct rte_lpm6 *lpm, const struct rte_lpm6_tbl_entry *tbl,
927 const struct rte_lpm6_tbl_entry **tbl_next, uint8_t *ip,
928 uint8_t first_byte, uint32_t *next_hop)
930 uint32_t tbl8_index, tbl_entry;
932 /* Take the integer value from the pointer. */
933 tbl_entry = *(const uint32_t *)tbl;
935 /* If it is valid and extended we calculate the new pointer to return. */
936 if ((tbl_entry & RTE_LPM6_VALID_EXT_ENTRY_BITMASK) ==
937 RTE_LPM6_VALID_EXT_ENTRY_BITMASK) {
939 tbl8_index = ip[first_byte-1] +
940 ((tbl_entry & RTE_LPM6_TBL8_BITMASK) *
941 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
943 *tbl_next = &lpm->tbl8[tbl8_index];
947 /* If not extended then we can have a match. */
948 *next_hop = ((uint32_t)tbl_entry & RTE_LPM6_TBL8_BITMASK);
949 return (tbl_entry & RTE_LPM6_LOOKUP_SUCCESS) ? 0 : -ENOENT;
957 rte_lpm6_lookup_v20(const struct rte_lpm6 *lpm, uint8_t *ip, uint8_t *next_hop)
959 uint32_t next_hop32 = 0;
962 /* DEBUG: Check user input arguments. */
963 if (next_hop == NULL)
966 status = rte_lpm6_lookup_v1705(lpm, ip, &next_hop32);
968 *next_hop = (uint8_t)next_hop32;
972 VERSION_SYMBOL(rte_lpm6_lookup, _v20, 2.0);
975 rte_lpm6_lookup_v1705(const struct rte_lpm6 *lpm, uint8_t *ip,
978 const struct rte_lpm6_tbl_entry *tbl;
979 const struct rte_lpm6_tbl_entry *tbl_next = NULL;
982 uint32_t tbl24_index;
984 /* DEBUG: Check user input arguments. */
985 if ((lpm == NULL) || (ip == NULL) || (next_hop == NULL))
988 first_byte = LOOKUP_FIRST_BYTE;
989 tbl24_index = (ip[0] << BYTES2_SIZE) | (ip[1] << BYTE_SIZE) | ip[2];
991 /* Calculate pointer to the first entry to be inspected */
992 tbl = &lpm->tbl24[tbl24_index];
995 /* Continue inspecting following levels until success or failure */
996 status = lookup_step(lpm, tbl, &tbl_next, ip, first_byte++, next_hop);
998 } while (status == 1);
1002 BIND_DEFAULT_SYMBOL(rte_lpm6_lookup, _v1705, 17.05);
1003 MAP_STATIC_SYMBOL(int rte_lpm6_lookup(const struct rte_lpm6 *lpm, uint8_t *ip,
1004 uint32_t *next_hop), rte_lpm6_lookup_v1705);
1007 * Looks up a group of IP addresses
1010 rte_lpm6_lookup_bulk_func_v20(const struct rte_lpm6 *lpm,
1011 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1012 int16_t * next_hops, unsigned n)
1015 const struct rte_lpm6_tbl_entry *tbl;
1016 const struct rte_lpm6_tbl_entry *tbl_next = NULL;
1017 uint32_t tbl24_index, next_hop;
1021 /* DEBUG: Check user input arguments. */
1022 if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
1025 for (i = 0; i < n; i++) {
1026 first_byte = LOOKUP_FIRST_BYTE;
1027 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
1028 (ips[i][1] << BYTE_SIZE) | ips[i][2];
1030 /* Calculate pointer to the first entry to be inspected */
1031 tbl = &lpm->tbl24[tbl24_index];
1034 /* Continue inspecting following levels until success or failure */
1035 status = lookup_step(lpm, tbl, &tbl_next, ips[i], first_byte++,
1038 } while (status == 1);
1043 next_hops[i] = (int16_t)next_hop;
1048 VERSION_SYMBOL(rte_lpm6_lookup_bulk_func, _v20, 2.0);
1051 rte_lpm6_lookup_bulk_func_v1705(const struct rte_lpm6 *lpm,
1052 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1053 int32_t *next_hops, unsigned int n)
1056 const struct rte_lpm6_tbl_entry *tbl;
1057 const struct rte_lpm6_tbl_entry *tbl_next = NULL;
1058 uint32_t tbl24_index, next_hop;
1062 /* DEBUG: Check user input arguments. */
1063 if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
1066 for (i = 0; i < n; i++) {
1067 first_byte = LOOKUP_FIRST_BYTE;
1068 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
1069 (ips[i][1] << BYTE_SIZE) | ips[i][2];
1071 /* Calculate pointer to the first entry to be inspected */
1072 tbl = &lpm->tbl24[tbl24_index];
1075 /* Continue inspecting following levels
1076 * until success or failure
1078 status = lookup_step(lpm, tbl, &tbl_next, ips[i],
1079 first_byte++, &next_hop);
1081 } while (status == 1);
1086 next_hops[i] = (int32_t)next_hop;
1091 BIND_DEFAULT_SYMBOL(rte_lpm6_lookup_bulk_func, _v1705, 17.05);
1092 MAP_STATIC_SYMBOL(int rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
1093 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1094 int32_t *next_hops, unsigned int n),
1095 rte_lpm6_lookup_bulk_func_v1705);
1098 * Look for a rule in the high-level rules table
1101 rte_lpm6_is_rule_present_v20(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1104 uint32_t next_hop32 = 0;
1107 /* DEBUG: Check user input arguments. */
1108 if (next_hop == NULL)
1111 status = rte_lpm6_is_rule_present_v1705(lpm, ip, depth, &next_hop32);
1113 *next_hop = (uint8_t)next_hop32;
1118 VERSION_SYMBOL(rte_lpm6_is_rule_present, _v20, 2.0);
1121 rte_lpm6_is_rule_present_v1705(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1124 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1126 /* Check user arguments. */
1127 if ((lpm == NULL) || next_hop == NULL || ip == NULL ||
1128 (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1131 /* Copy the IP and mask it to avoid modifying user's input data. */
1132 ip6_copy_addr(masked_ip, ip);
1133 ip6_mask_addr(masked_ip, depth);
1135 return rule_find(lpm, masked_ip, depth, next_hop);
1137 BIND_DEFAULT_SYMBOL(rte_lpm6_is_rule_present, _v1705, 17.05);
1138 MAP_STATIC_SYMBOL(int rte_lpm6_is_rule_present(struct rte_lpm6 *lpm,
1139 uint8_t *ip, uint8_t depth, uint32_t *next_hop),
1140 rte_lpm6_is_rule_present_v1705);
1143 * Delete a rule from the rule table.
1144 * NOTE: Valid range for depth parameter is 1 .. 128 inclusive.
1150 rule_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1153 struct rte_lpm6_rule_key rule_key;
1156 rule_key_init(&rule_key, ip, depth);
1158 /* delete the rule */
1159 ret = rte_hash_del_key(lpm->rules_tbl, (void *) &rule_key);
1167 * Deletes a group of rules
1169 * Note that the function rebuilds the lpm table,
1170 * rather than doing incremental updates like
1171 * the regular delete function
1174 rte_lpm6_delete_bulk_func(struct rte_lpm6 *lpm,
1175 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE], uint8_t *depths,
1178 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1181 /* Check input arguments. */
1182 if ((lpm == NULL) || (ips == NULL) || (depths == NULL))
1185 for (i = 0; i < n; i++) {
1186 ip6_copy_addr(masked_ip, ips[i]);
1187 ip6_mask_addr(masked_ip, depths[i]);
1188 rule_delete(lpm, masked_ip, depths[i]);
1192 * Set all the table entries to 0 (ie delete every rule
1193 * from the data structure.
1195 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1196 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1197 * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1198 tbl8_pool_init(lpm);
1201 * Add every rule again (except for the ones that were removed from
1210 * Delete all rules from the LPM table.
1213 rte_lpm6_delete_all(struct rte_lpm6 *lpm)
1215 /* Zero used rules counter. */
1216 lpm->used_rules = 0;
1219 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1222 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0]) *
1223 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1225 /* init pool of free tbl8 indexes */
1226 tbl8_pool_init(lpm);
1228 /* Delete all rules form the rules table. */
1229 rte_hash_reset(lpm->rules_tbl);
1233 * Convert a depth to a one byte long mask
1234 * Example: 4 will be converted to 0xF0
1236 static uint8_t __attribute__((pure))
1237 depth_to_mask_1b(uint8_t depth)
1239 /* To calculate a mask start with a 1 on the left hand side and right
1240 * shift while populating the left hand side with 1's
1242 return (signed char)0x80 >> (depth - 1);
1246 * Find a less specific rule
1249 rule_find_less_specific(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1250 struct rte_lpm6_rule *rule)
1255 struct rte_lpm6_rule_key rule_key;
1260 rule_key_init(&rule_key, ip, depth);
1265 /* each iteration zero one more bit of the key */
1266 mask = depth & 7; /* depth % BYTE_SIZE */
1268 mask = depth_to_mask_1b(mask);
1270 rule_key.depth = depth;
1271 rule_key.ip[depth >> 3] &= mask;
1273 ret = rule_find_with_key(lpm, &rule_key, &next_hop);
1275 rule->depth = depth;
1276 ip6_copy_addr(rule->ip, rule_key.ip);
1277 rule->next_hop = next_hop;
1286 * Find range of tbl8 cells occupied by a rule
1289 rule_find_range(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1290 struct rte_lpm6_tbl_entry **from,
1291 struct rte_lpm6_tbl_entry **to,
1292 uint32_t *out_tbl_ind)
1295 uint32_t first_3bytes = (uint32_t)ip[0] << 16 | ip[1] << 8 | ip[2];
1298 /* rule is within the top level */
1300 *from = &lpm->tbl24[ind];
1301 ind += (1 << (24 - depth)) - 1;
1302 *to = &lpm->tbl24[ind];
1303 *out_tbl_ind = TBL24_IND;
1305 /* top level entry */
1306 struct rte_lpm6_tbl_entry *tbl = &lpm->tbl24[first_3bytes];
1307 assert(tbl->ext_entry == 1);
1309 uint32_t tbl_ind = tbl->lpm6_tbl8_gindex;
1310 tbl = &lpm->tbl8[tbl_ind *
1311 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1312 /* current ip byte, the top level is already behind */
1314 /* minus top level */
1317 /* interate through levels (tbl8s)
1318 * until we reach the last one
1322 assert(tbl->ext_entry == 1);
1323 /* go to the next level/tbl8 */
1324 tbl_ind = tbl->lpm6_tbl8_gindex;
1325 tbl = &lpm->tbl8[tbl_ind *
1326 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1331 /* last level/tbl8 */
1332 ind = ip[byte] & depth_to_mask_1b(depth);
1334 ind += (1 << (8 - depth)) - 1;
1336 *out_tbl_ind = tbl_ind;
1341 * Remove a table from the LPM tree
1344 remove_tbl(struct rte_lpm6 *lpm, struct rte_lpm_tbl8_hdr *tbl_hdr,
1345 uint32_t tbl_ind, struct rte_lpm6_rule *lsp_rule)
1347 struct rte_lpm6_tbl_entry *owner_entry;
1349 if (tbl_hdr->owner_tbl_ind == TBL24_IND)
1350 owner_entry = &lpm->tbl24[tbl_hdr->owner_entry_ind];
1352 uint32_t owner_tbl_ind = tbl_hdr->owner_tbl_ind;
1353 owner_entry = &lpm->tbl8[
1354 owner_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES +
1355 tbl_hdr->owner_entry_ind];
1357 struct rte_lpm_tbl8_hdr *owner_tbl_hdr =
1358 &lpm->tbl8_hdrs[owner_tbl_ind];
1359 if (--owner_tbl_hdr->ref_cnt == 0)
1360 remove_tbl(lpm, owner_tbl_hdr, owner_tbl_ind, lsp_rule);
1363 assert(owner_entry->ext_entry == 1);
1365 /* unlink the table */
1366 if (lsp_rule != NULL) {
1367 struct rte_lpm6_tbl_entry new_tbl_entry = {
1368 .next_hop = lsp_rule->next_hop,
1369 .depth = lsp_rule->depth,
1371 .valid_group = VALID,
1375 *owner_entry = new_tbl_entry;
1377 struct rte_lpm6_tbl_entry new_tbl_entry = {
1381 .valid_group = INVALID,
1385 *owner_entry = new_tbl_entry;
1388 /* return the table to the pool */
1389 tbl8_put(lpm, tbl_ind);
1396 rte_lpm6_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1398 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1399 struct rte_lpm6_rule lsp_rule_obj;
1400 struct rte_lpm6_rule *lsp_rule;
1403 struct rte_lpm6_tbl_entry *from, *to;
1405 /* Check input arguments. */
1406 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1409 /* Copy the IP and mask it to avoid modifying user's input data. */
1410 ip6_copy_addr(masked_ip, ip);
1411 ip6_mask_addr(masked_ip, depth);
1413 /* Delete the rule from the rule table. */
1414 ret = rule_delete(lpm, masked_ip, depth);
1418 /* find rule cells */
1419 rule_find_range(lpm, masked_ip, depth, &from, &to, &tbl_ind);
1421 /* find a less specific rule (a rule with smaller depth)
1422 * note: masked_ip will be modified, don't use it anymore
1424 ret = rule_find_less_specific(lpm, masked_ip, depth,
1426 lsp_rule = ret ? &lsp_rule_obj : NULL;
1428 /* decrement the table rule counter,
1429 * note that tbl24 doesn't have a header
1431 if (tbl_ind != TBL24_IND) {
1432 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
1433 if (--tbl_hdr->ref_cnt == 0) {
1434 /* remove the table */
1435 remove_tbl(lpm, tbl_hdr, tbl_ind, lsp_rule);
1440 /* iterate rule cells */
1441 for (; from <= to; from++)
1442 if (from->ext_entry == 1) {
1443 /* reference to a more specific space
1444 * of the prefix/rule. Entries in a more
1445 * specific space that are not used by
1446 * a more specific prefix must be occupied
1449 if (lsp_rule != NULL)
1451 from->lpm6_tbl8_gindex *
1452 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1453 depth, lsp_rule->depth,
1454 lsp_rule->next_hop, VALID);
1456 /* since the prefix has no less specific prefix,
1457 * its more specific space must be invalidated
1460 from->lpm6_tbl8_gindex *
1461 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1462 depth, 0, 0, INVALID);
1463 } else if (from->depth == depth) {
1464 /* entry is not a reference and belongs to the prefix */
1465 if (lsp_rule != NULL) {
1466 struct rte_lpm6_tbl_entry new_tbl_entry = {
1467 .next_hop = lsp_rule->next_hop,
1468 .depth = lsp_rule->depth,
1470 .valid_group = VALID,
1474 *from = new_tbl_entry;
1476 struct rte_lpm6_tbl_entry new_tbl_entry = {
1480 .valid_group = INVALID,
1484 *from = new_tbl_entry;