1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <sys/rwlock.h>
10 #include <sys/malloc.h>
12 #include <sys/socket.h>
13 #include <sys/kernel.h>
15 //#include <netinet6/rte_tailq.h>
16 int errno = 0, rte_errno = 0;
21 #define RTE_LPM6_TBL24_NUM_ENTRIES (1 << 24)
22 #define RTE_LPM6_TBL8_GROUP_NUM_ENTRIES 256
23 #define RTE_LPM6_TBL8_MAX_NUM_GROUPS (1 << 21)
25 #define RTE_LPM6_VALID_EXT_ENTRY_BITMASK 0xA0000000
26 #define RTE_LPM6_LOOKUP_SUCCESS 0x20000000
27 #define RTE_LPM6_TBL8_BITMASK 0x001FFFFF
29 #define ADD_FIRST_BYTE 3
30 #define LOOKUP_FIRST_BYTE 4
32 #define BYTES2_SIZE 16
34 #define RULE_HASH_TABLE_EXTRA_SPACE 64
35 #define TBL24_IND UINT32_MAX
37 #define lpm6_tbl8_gindex next_hop
39 /** Flags for setting an entry as valid/invalid. */
46 TAILQ_HEAD(rte_lpm6_list, rte_tailq_entry);
48 static struct rte_tailq_elem rte_lpm6_tailq = {
51 EAL_REGISTER_TAILQ(rte_lpm6_tailq)
54 /** Tbl entry structure. It is the same for both tbl24 and tbl8 */
55 struct rte_lpm6_tbl_entry {
56 uint32_t next_hop: 21; /**< Next hop / next table to be checked. */
57 uint32_t depth :8; /**< Rule depth. */
60 uint32_t valid :1; /**< Validation flag. */
61 uint32_t valid_group :1; /**< Group validation flag. */
62 uint32_t ext_entry :1; /**< External entry. */
65 /** Rules tbl entry structure. */
66 struct rte_lpm6_rule {
67 uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
68 uint32_t next_hop; /**< Rule next hop. */
69 uint8_t depth; /**< Rule depth. */
72 /** Rules tbl entry key. */
73 struct rte_lpm6_rule_key {
74 uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
75 uint8_t depth; /**< Rule depth. */
79 struct rte_lpm_tbl8_hdr {
80 uint32_t owner_tbl_ind; /**< owner table: TBL24_IND if owner is tbl24,
81 * otherwise index of tbl8
83 uint32_t owner_entry_ind; /**< index of the owner table entry where
84 * pointer to the tbl8 is stored
86 uint32_t ref_cnt; /**< table reference counter */
89 /** LPM6 structure. */
91 struct rte_lpm6_external ext; /* Storage used by the algo wrapper */
93 char name[RTE_LPM6_NAMESIZE]; /**< Name of the lpm. */
94 uint32_t max_rules; /**< Max number of rules. */
95 uint32_t used_rules; /**< Used rules so far. */
96 uint32_t number_tbl8s; /**< Number of tbl8s to allocate. */
99 //struct rte_hash *rules_tbl; /**< LPM rules. */
100 struct rte_lpm6_tbl_entry tbl24[RTE_LPM6_TBL24_NUM_ENTRIES]
101 __rte_cache_aligned; /**< LPM tbl24 table. */
103 uint32_t *tbl8_pool; /**< pool of indexes of free tbl8s */
104 uint32_t tbl8_pool_pos; /**< current position in the tbl8 pool */
106 struct rte_lpm_tbl8_hdr *tbl8_hdrs; /* array of tbl8 headers */
108 struct rte_lpm6_tbl_entry tbl8[0]
109 __rte_cache_aligned; /**< LPM tbl8 table. */
113 * Takes an array of uint8_t (IPv6 address) and masks it using the depth.
114 * It leaves untouched one bit per unit in the depth variable
115 * and set the rest to 0.
118 ip6_mask_addr(uint8_t *ip, uint8_t depth)
120 int16_t part_depth, mask;
125 for (i = 0; i < RTE_LPM6_IPV6_ADDR_SIZE; i++) {
126 if (part_depth < BYTE_SIZE && part_depth >= 0) {
127 mask = (uint16_t)(~(UINT8_MAX >> part_depth));
128 ip[i] = (uint8_t)(ip[i] & mask);
129 } else if (part_depth < 0)
132 part_depth -= BYTE_SIZE;
136 /* copy ipv6 address */
138 ip6_copy_addr(uint8_t *dst, const uint8_t *src)
140 rte_memcpy(dst, src, RTE_LPM6_IPV6_ADDR_SIZE);
145 * LPM6 rule hash function
147 * It's used as a hash function for the rte_hash
150 static inline uint32_t
151 rule_hash(const void *data, __rte_unused uint32_t data_len,
154 return rte_jhash(data, sizeof(struct rte_lpm6_rule_key), init_val);
159 * Init pool of free tbl8 indexes
162 tbl8_pool_init(struct rte_lpm6 *lpm)
166 /* put entire range of indexes to the tbl8 pool */
167 for (i = 0; i < lpm->number_tbl8s; i++)
168 lpm->tbl8_pool[i] = i;
170 lpm->tbl8_pool_pos = 0;
174 * Get an index of a free tbl8 from the pool
176 static inline uint32_t
177 tbl8_get(struct rte_lpm6 *lpm, uint32_t *tbl8_ind)
179 if (lpm->tbl8_pool_pos == lpm->number_tbl8s)
180 /* no more free tbl8 */
184 *tbl8_ind = lpm->tbl8_pool[lpm->tbl8_pool_pos++];
189 * Put an index of a free tbl8 back to the pool
191 static inline uint32_t
192 tbl8_put(struct rte_lpm6 *lpm, uint32_t tbl8_ind)
194 if (lpm->tbl8_pool_pos == 0)
198 lpm->tbl8_pool[--lpm->tbl8_pool_pos] = tbl8_ind;
203 * Returns number of tbl8s available in the pool
205 static inline uint32_t
206 tbl8_available(struct rte_lpm6 *lpm)
208 return lpm->number_tbl8s - lpm->tbl8_pool_pos;
214 * note that ip must be already masked
217 rule_key_init(struct rte_lpm6_rule_key *key, uint8_t *ip, uint8_t depth)
219 ip6_copy_addr(key->ip, ip);
224 * Rebuild the entire LPM tree by reinserting all rules
227 rebuild_lpm(struct rte_lpm6 *lpm)
230 struct rte_lpm6_rule_key *rule_key;
233 while (rte_hash_iterate(lpm->rules_tbl, (void *) &rule_key,
234 (void **) &next_hop, &iter) >= 0)
235 rte_lpm6_add(lpm, rule_key->ip, rule_key->depth,
236 (uint32_t) next_hop);
241 * Allocates memory for LPM object
244 rte_lpm6_create(const char *name, int socket_id,
245 const struct rte_lpm6_config *config)
247 char mem_name[RTE_LPM6_NAMESIZE];
248 struct rte_lpm6 *lpm = NULL;
249 //struct rte_tailq_entry *te;
251 //struct rte_lpm6_list *lpm_list;
252 //struct rte_hash *rules_tbl = NULL;
253 uint32_t *tbl8_pool = NULL;
254 struct rte_lpm_tbl8_hdr *tbl8_hdrs = NULL;
256 //lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
258 RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_tbl_entry) != sizeof(uint32_t));
260 /* Check user arguments. */
261 if ((name == NULL) || (socket_id < -1) || (config == NULL) ||
262 config->number_tbl8s > RTE_LPM6_TBL8_MAX_NUM_GROUPS) {
268 /* create rules hash table */
269 snprintf(mem_name, sizeof(mem_name), "LRH_%s", name);
270 struct rte_hash_parameters rule_hash_tbl_params = {
271 .entries = config->max_rules * 1.2 +
272 RULE_HASH_TABLE_EXTRA_SPACE,
273 .key_len = sizeof(struct rte_lpm6_rule_key),
274 .hash_func = rule_hash,
275 .hash_func_init_val = 0,
278 .socket_id = socket_id,
282 rules_tbl = rte_hash_create(&rule_hash_tbl_params);
283 if (rules_tbl == NULL) {
284 RTE_LOG(ERR, LPM, "LPM rules hash table allocation failed: %s (%d)",
285 rte_strerror(rte_errno), rte_errno);
290 /* allocate tbl8 indexes pool */
291 tbl8_pool = rte_malloc(NULL,
292 sizeof(uint32_t) * config->number_tbl8s,
293 RTE_CACHE_LINE_SIZE);
294 if (tbl8_pool == NULL) {
295 RTE_LOG(ERR, LPM, "LPM tbl8 pool allocation failed: %s (%d)",
296 rte_strerror(rte_errno), rte_errno);
301 /* allocate tbl8 headers */
302 tbl8_hdrs = rte_malloc(NULL,
303 sizeof(struct rte_lpm_tbl8_hdr) * config->number_tbl8s,
304 RTE_CACHE_LINE_SIZE);
305 if (tbl8_hdrs == NULL) {
306 RTE_LOG(ERR, LPM, "LPM tbl8 headers allocation failed: %s (%d)",
307 rte_strerror(rte_errno), rte_errno);
312 snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
314 /* Determine the amount of memory to allocate. */
315 mem_size = sizeof(*lpm) + (sizeof(lpm->tbl8[0]) *
316 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
319 rte_mcfg_tailq_write_lock();
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");
342 /* Allocate memory to store the LPM data structures. */
343 lpm = rte_zmalloc_socket(mem_name, (size_t)mem_size,
344 RTE_CACHE_LINE_SIZE, socket_id);
347 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
353 /* Save user arguments. */
354 //lpm->max_rules = config->max_rules;
355 lpm->number_tbl8s = config->number_tbl8s;
356 strlcpy(lpm->name, name, sizeof(lpm->name));
357 //lpm->rules_tbl = rules_tbl;
358 lpm->tbl8_pool = tbl8_pool;
359 lpm->tbl8_hdrs = tbl8_hdrs;
364 //te->data = (void *) lpm;
366 //TAILQ_INSERT_TAIL(lpm_list, te, next);
367 rte_mcfg_tailq_write_unlock();
371 rte_mcfg_tailq_write_unlock();
376 //rte_hash_free(rules_tbl);
383 * Find an existing lpm table and return a pointer to it.
386 rte_lpm6_find_existing(const char *name)
388 struct rte_lpm6 *l = NULL;
389 struct rte_tailq_entry *te;
390 struct rte_lpm6_list *lpm_list;
392 lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
394 rte_mcfg_tailq_read_lock();
395 TAILQ_FOREACH(te, lpm_list, next) {
396 l = (struct rte_lpm6 *) te->data;
397 if (strncmp(name, l->name, RTE_LPM6_NAMESIZE) == 0)
400 rte_mcfg_tailq_read_unlock();
412 * Deallocates memory for given LPM table.
415 rte_lpm6_free(struct rte_lpm6 *lpm)
418 struct rte_lpm6_list *lpm_list;
419 struct rte_tailq_entry *te;
421 /* Check user arguments. */
425 lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
427 rte_mcfg_tailq_write_lock();
429 /* find our tailq entry */
430 TAILQ_FOREACH(te, lpm_list, next) {
431 if (te->data == (void *) lpm)
436 TAILQ_REMOVE(lpm_list, te, next);
438 rte_mcfg_tailq_write_unlock();
441 rte_free(lpm->tbl8_hdrs);
442 rte_free(lpm->tbl8_pool);
443 //rte_hash_free(lpm->rules_tbl);
451 rule_find_with_key(struct rte_lpm6 *lpm,
452 const struct rte_lpm6_rule_key *rule_key,
458 /* lookup for a rule */
459 ret = rte_hash_lookup_data(lpm->rules_tbl, (const void *) rule_key,
460 (void **) &hash_val);
462 *next_hop = (uint32_t) hash_val;
471 rule_find(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
474 struct rte_lpm6_rule_key rule_key;
476 /* init a rule key */
477 rule_key_init(&rule_key, ip, depth);
479 return rule_find_with_key(lpm, &rule_key, next_hop);
483 * Checks if a rule already exists in the rules table and updates
484 * the nexthop if so. Otherwise it adds a new rule if enough space is available.
487 * 0 - next hop of existed rule is updated
488 * 1 - new rule successfully added
492 rule_add(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth, uint32_t next_hop)
495 struct rte_lpm6_rule_key rule_key;
498 /* init a rule key */
499 rule_key_init(&rule_key, ip, depth);
501 /* Scan through rule list to see if rule already exists. */
502 rule_exist = rule_find_with_key(lpm, &rule_key, &unused);
505 * If rule does not exist check if there is space to add a new rule to
506 * this rule group. If there is no space return error.
508 if (!rule_exist && lpm->used_rules == lpm->max_rules)
511 /* add the rule or update rules next hop */
512 ret = rte_hash_add_key_data(lpm->rules_tbl, &rule_key,
513 (void *)(uintptr_t) next_hop);
517 /* Increment the used rules counter for this rule group. */
528 * Function that expands a rule across the data structure when a less-generic
529 * one has been added before. It assures that every possible combination of bits
530 * in the IP address returns a match.
533 expand_rule(struct rte_lpm6 *lpm, uint32_t tbl8_gindex, uint8_t old_depth,
534 uint8_t new_depth, uint32_t next_hop, uint8_t valid)
536 uint32_t tbl8_group_end, tbl8_gindex_next, j;
538 tbl8_group_end = tbl8_gindex + RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
540 struct rte_lpm6_tbl_entry new_tbl8_entry = {
542 .valid_group = valid,
544 .next_hop = next_hop,
548 for (j = tbl8_gindex; j < tbl8_group_end; j++) {
549 if (!lpm->tbl8[j].valid || (lpm->tbl8[j].ext_entry == 0
550 && lpm->tbl8[j].depth <= old_depth)) {
552 lpm->tbl8[j] = new_tbl8_entry;
554 } else if (lpm->tbl8[j].ext_entry == 1) {
556 tbl8_gindex_next = lpm->tbl8[j].lpm6_tbl8_gindex
557 * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
558 expand_rule(lpm, tbl8_gindex_next, old_depth, new_depth,
568 init_tbl8_header(struct rte_lpm6 *lpm, uint32_t tbl_ind,
569 uint32_t owner_tbl_ind, uint32_t owner_entry_ind)
571 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
572 tbl_hdr->owner_tbl_ind = owner_tbl_ind;
573 tbl_hdr->owner_entry_ind = owner_entry_ind;
574 tbl_hdr->ref_cnt = 0;
578 * Calculate index to the table based on the number and position
579 * of the bytes being inspected in this step.
582 get_bitshift(const uint8_t *ip, uint8_t first_byte, uint8_t bytes)
584 uint32_t entry_ind, i;
588 for (i = first_byte; i < (uint32_t)(first_byte + bytes); i++) {
589 bitshift = (int8_t)((bytes - i)*BYTE_SIZE);
593 entry_ind = entry_ind | ip[i-1] << bitshift;
600 * Simulate adding a new route to the LPM counting number
601 * of new tables that will be needed
603 * It returns 0 on success, or 1 if
604 * the process needs to be continued by calling the function again.
607 simulate_add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
608 struct rte_lpm6_tbl_entry **next_tbl, const uint8_t *ip,
609 uint8_t bytes, uint8_t first_byte, uint8_t depth,
610 uint32_t *need_tbl_nb)
613 uint8_t bits_covered;
614 uint32_t next_tbl_ind;
617 * Calculate index to the table based on the number and position
618 * of the bytes being inspected in this step.
620 entry_ind = get_bitshift(ip, first_byte, bytes);
622 /* Number of bits covered in this step */
623 bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
625 if (depth <= bits_covered) {
630 if (tbl[entry_ind].valid == 0 || tbl[entry_ind].ext_entry == 0) {
631 /* from this point on a new table is needed on each level
632 * that is not covered yet
634 depth -= bits_covered;
635 uint32_t cnt = depth >> 3; /* depth / BYTE_SIZE */
636 if (depth & 7) /* 0b00000111 */
637 /* if depth % 8 > 0 then one more table is needed
638 * for those last bits
646 next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
647 *next_tbl = &(lpm->tbl8[next_tbl_ind *
648 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
654 * Partially adds a new route to the data structure (tbl24+tbl8s).
655 * It returns 0 on success, a negative number on failure, or 1 if
656 * the process needs to be continued by calling the function again.
659 add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
660 uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
661 uint32_t *next_tbl_ind, uint8_t *ip, uint8_t bytes,
662 uint8_t first_byte, uint8_t depth, uint32_t next_hop,
665 uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
666 uint32_t tbl8_gindex;
667 uint8_t bits_covered;
671 * Calculate index to the table based on the number and position
672 * of the bytes being inspected in this step.
674 entry_ind = get_bitshift(ip, first_byte, bytes);
676 /* Number of bits covered in this step */
677 bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
680 * If depth if smaller than this number (ie this is the last step)
681 * expand the rule across the relevant positions in the table.
683 if (depth <= bits_covered) {
684 tbl_range = 1 << (bits_covered - depth);
686 for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
687 if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
688 tbl[i].depth <= depth)) {
690 struct rte_lpm6_tbl_entry new_tbl_entry = {
691 .next_hop = next_hop,
694 .valid_group = VALID,
698 tbl[i] = new_tbl_entry;
700 } else if (tbl[i].ext_entry == 1) {
703 * If tbl entry is valid and extended calculate the index
704 * into next tbl8 and expand the rule across the data structure.
706 tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
707 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
708 expand_rule(lpm, tbl8_gindex, depth, depth,
713 /* update tbl8 rule reference counter */
714 if (tbl_ind != TBL24_IND && is_new_rule)
715 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
720 * If this is not the last step just fill one position
721 * and calculate the index to the next table.
724 /* If it's invalid a new tbl8 is needed */
725 if (!tbl[entry_ind].valid) {
726 /* get a new table */
727 ret = tbl8_get(lpm, &tbl8_gindex);
731 /* invalidate all new tbl8 entries */
732 tbl8_group_start = tbl8_gindex *
733 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
734 memset(&lpm->tbl8[tbl8_group_start], 0,
735 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES *
736 sizeof(struct rte_lpm6_tbl_entry));
738 /* init the new table's header:
739 * save the reference to the owner table
741 init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
743 /* reference to a new tbl8 */
744 struct rte_lpm6_tbl_entry new_tbl_entry = {
745 .lpm6_tbl8_gindex = tbl8_gindex,
748 .valid_group = VALID,
752 tbl[entry_ind] = new_tbl_entry;
754 /* update the current table's reference counter */
755 if (tbl_ind != TBL24_IND)
756 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
759 * If it's valid but not extended the rule that was stored
760 * here needs to be moved to the next table.
762 else if (tbl[entry_ind].ext_entry == 0) {
763 /* get a new tbl8 index */
764 ret = tbl8_get(lpm, &tbl8_gindex);
768 tbl8_group_start = tbl8_gindex *
769 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
770 tbl8_group_end = tbl8_group_start +
771 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
773 struct rte_lpm6_tbl_entry tbl_entry = {
774 .next_hop = tbl[entry_ind].next_hop,
775 .depth = tbl[entry_ind].depth,
777 .valid_group = VALID,
781 /* Populate new tbl8 with tbl value. */
782 for (i = tbl8_group_start; i < tbl8_group_end; i++)
783 lpm->tbl8[i] = tbl_entry;
785 /* init the new table's header:
786 * save the reference to the owner table
788 init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
791 * Update tbl entry to point to new tbl8 entry. Note: The
792 * ext_flag and tbl8_index need to be updated simultaneously,
793 * so assign whole structure in one go.
795 struct rte_lpm6_tbl_entry new_tbl_entry = {
796 .lpm6_tbl8_gindex = tbl8_gindex,
799 .valid_group = VALID,
803 tbl[entry_ind] = new_tbl_entry;
805 /* update the current table's reference counter */
806 if (tbl_ind != TBL24_IND)
807 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
810 *next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
811 *next_tbl = &(lpm->tbl8[*next_tbl_ind *
812 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
819 * Simulate adding a route to LPM
823 * -ENOSPC not enough tbl8 left
826 simulate_add(struct rte_lpm6 *lpm, const uint8_t *masked_ip, uint8_t depth)
828 struct rte_lpm6_tbl_entry *tbl;
829 struct rte_lpm6_tbl_entry *tbl_next = NULL;
832 /* number of new tables needed for a step */
833 uint32_t need_tbl_nb;
834 /* total number of new tables needed */
835 uint32_t total_need_tbl_nb;
837 /* Inspect the first three bytes through tbl24 on the first step. */
838 ret = simulate_add_step(lpm, lpm->tbl24, &tbl_next, masked_ip,
839 ADD_FIRST_BYTE, 1, depth, &need_tbl_nb);
840 total_need_tbl_nb = need_tbl_nb;
842 * Inspect one by one the rest of the bytes until
843 * the process is completed.
845 for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && ret == 1; i++) {
847 ret = simulate_add_step(lpm, tbl, &tbl_next, masked_ip, 1,
848 (uint8_t)(i + 1), depth, &need_tbl_nb);
849 total_need_tbl_nb += need_tbl_nb;
852 if (tbl8_available(lpm) < total_need_tbl_nb)
853 /* not enough tbl8 to add a rule */
863 rte_lpm6_add(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
864 uint32_t next_hop, int is_new_rule)
866 struct rte_lpm6_tbl_entry *tbl;
867 struct rte_lpm6_tbl_entry *tbl_next = NULL;
868 /* init to avoid compiler warning */
869 uint32_t tbl_next_num = 123456;
871 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
874 /* Check user arguments. */
875 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
878 /* Copy the IP and mask it to avoid modifying user's input data. */
879 ip6_copy_addr(masked_ip, ip);
880 ip6_mask_addr(masked_ip, depth);
882 /* Simulate adding a new route */
883 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. */
895 /* Inspect the first three bytes through tbl24 on the first step. */
897 status = add_step(lpm, tbl, TBL24_IND, &tbl_next, &tbl_next_num,
898 masked_ip, ADD_FIRST_BYTE, 1, depth, next_hop,
903 * Inspect one by one the rest of the bytes until
904 * the process is completed.
906 for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && status == 1; i++) {
908 status = add_step(lpm, tbl, tbl_next_num, &tbl_next,
909 &tbl_next_num, masked_ip, 1, (uint8_t)(i + 1),
910 depth, next_hop, is_new_rule);
918 * Takes a pointer to a table entry and inspect one level.
919 * The function returns 0 on lookup success, ENOENT if no match was found
920 * or 1 if the process needs to be continued by calling the function again.
923 lookup_step(const struct rte_lpm6 *lpm, const struct rte_lpm6_tbl_entry *tbl,
924 const struct rte_lpm6_tbl_entry **tbl_next, const uint8_t *ip,
925 uint8_t first_byte, uint32_t *next_hop)
927 uint32_t tbl8_index, tbl_entry;
929 /* Take the integer value from the pointer. */
930 tbl_entry = *(const uint32_t *)tbl;
932 /* If it is valid and extended we calculate the new pointer to return. */
933 if ((tbl_entry & RTE_LPM6_VALID_EXT_ENTRY_BITMASK) ==
934 RTE_LPM6_VALID_EXT_ENTRY_BITMASK) {
936 tbl8_index = ip[first_byte-1] +
937 ((tbl_entry & RTE_LPM6_TBL8_BITMASK) *
938 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
940 *tbl_next = &lpm->tbl8[tbl8_index];
944 /* If not extended then we can have a match. */
945 *next_hop = ((uint32_t)tbl_entry & RTE_LPM6_TBL8_BITMASK);
946 return (tbl_entry & RTE_LPM6_LOOKUP_SUCCESS) ? 0 : -ENOENT;
954 rte_lpm6_lookup(const struct rte_lpm6 *lpm, const uint8_t *ip,
957 const struct rte_lpm6_tbl_entry *tbl;
958 const struct rte_lpm6_tbl_entry *tbl_next = NULL;
961 uint32_t tbl24_index;
963 /* DEBUG: Check user input arguments. */
964 if ((lpm == NULL) || (ip == NULL) || (next_hop == NULL))
967 first_byte = LOOKUP_FIRST_BYTE;
968 tbl24_index = (ip[0] << BYTES2_SIZE) | (ip[1] << BYTE_SIZE) | ip[2];
970 /* Calculate pointer to the first entry to be inspected */
971 tbl = &lpm->tbl24[tbl24_index];
974 /* Continue inspecting following levels until success or failure */
975 status = lookup_step(lpm, tbl, &tbl_next, ip, first_byte++, next_hop);
977 } while (status == 1);
983 * Looks up a group of IP addresses
986 rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
987 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
988 int32_t *next_hops, unsigned int n)
991 const struct rte_lpm6_tbl_entry *tbl;
992 const struct rte_lpm6_tbl_entry *tbl_next = NULL;
993 uint32_t tbl24_index, next_hop;
997 /* DEBUG: Check user input arguments. */
998 if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
1001 for (i = 0; i < n; i++) {
1002 first_byte = LOOKUP_FIRST_BYTE;
1003 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
1004 (ips[i][1] << BYTE_SIZE) | ips[i][2];
1006 /* Calculate pointer to the first entry to be inspected */
1007 tbl = &lpm->tbl24[tbl24_index];
1010 /* Continue inspecting following levels
1011 * until success or failure
1013 status = lookup_step(lpm, tbl, &tbl_next, ips[i],
1014 first_byte++, &next_hop);
1016 } while (status == 1);
1021 next_hops[i] = (int32_t)next_hop;
1027 struct rte_lpm6_rule *
1028 fill_rule6(char *buffer, const uint8_t *ip, uint8_t depth, uint32_t next_hop)
1030 struct rte_lpm6_rule *rule = (struct rte_lpm6_rule *)buffer;
1032 ip6_copy_addr((uint8_t *)&rule->ip, ip);
1033 rule->depth = depth;
1034 rule->next_hop = next_hop;
1041 * Look for a rule in the high-level rules table
1044 rte_lpm6_is_rule_present(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1047 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1049 /* Check user arguments. */
1050 if ((lpm == NULL) || next_hop == NULL || ip == NULL ||
1051 (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1054 /* Copy the IP and mask it to avoid modifying user's input data. */
1055 ip6_copy_addr(masked_ip, ip);
1056 ip6_mask_addr(masked_ip, depth);
1058 return rule_find(lpm, masked_ip, depth, next_hop);
1062 * Delete a rule from the rule table.
1063 * NOTE: Valid range for depth parameter is 1 .. 128 inclusive.
1069 rule_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1072 struct rte_lpm6_rule_key rule_key;
1075 rule_key_init(&rule_key, ip, depth);
1077 /* delete the rule */
1078 ret = rte_hash_del_key(lpm->rules_tbl, (void *) &rule_key);
1086 * Deletes a group of rules
1088 * Note that the function rebuilds the lpm table,
1089 * rather than doing incremental updates like
1090 * the regular delete function
1093 rte_lpm6_delete_bulk_func(struct rte_lpm6 *lpm,
1094 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE], uint8_t *depths,
1097 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1100 /* Check input arguments. */
1101 if ((lpm == NULL) || (ips == NULL) || (depths == NULL))
1104 for (i = 0; i < n; i++) {
1105 ip6_copy_addr(masked_ip, ips[i]);
1106 ip6_mask_addr(masked_ip, depths[i]);
1107 rule_delete(lpm, masked_ip, depths[i]);
1111 * Set all the table entries to 0 (ie delete every rule
1112 * from the data structure.
1114 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1115 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1116 * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1117 tbl8_pool_init(lpm);
1120 * Add every rule again (except for the ones that were removed from
1129 * Delete all rules from the LPM table.
1132 rte_lpm6_delete_all(struct rte_lpm6 *lpm)
1134 /* Zero used rules counter. */
1135 lpm->used_rules = 0;
1138 memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1141 memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0]) *
1142 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1144 /* init pool of free tbl8 indexes */
1145 tbl8_pool_init(lpm);
1147 /* Delete all rules form the rules table. */
1148 rte_hash_reset(lpm->rules_tbl);
1153 * Convert a depth to a one byte long mask
1154 * Example: 4 will be converted to 0xF0
1156 static uint8_t __attribute__((pure))
1157 depth_to_mask_1b(uint8_t depth)
1159 /* To calculate a mask start with a 1 on the left hand side and right
1160 * shift while populating the left hand side with 1's
1162 return (signed char)0x80 >> (depth - 1);
1167 * Find a less specific rule
1170 rule_find_less_specific(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1171 struct rte_lpm6_rule *rule)
1176 struct rte_lpm6_rule_key rule_key;
1181 rule_key_init(&rule_key, ip, depth);
1186 /* each iteration zero one more bit of the key */
1187 mask = depth & 7; /* depth % BYTE_SIZE */
1189 mask = depth_to_mask_1b(mask);
1191 rule_key.depth = depth;
1192 rule_key.ip[depth >> 3] &= mask;
1194 ret = rule_find_with_key(lpm, &rule_key, &next_hop);
1196 rule->depth = depth;
1197 ip6_copy_addr(rule->ip, rule_key.ip);
1198 rule->next_hop = next_hop;
1208 * Find range of tbl8 cells occupied by a rule
1211 rule_find_range(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1212 struct rte_lpm6_tbl_entry **from,
1213 struct rte_lpm6_tbl_entry **to,
1214 uint32_t *out_tbl_ind)
1217 uint32_t first_3bytes = (uint32_t)ip[0] << 16 | ip[1] << 8 | ip[2];
1220 /* rule is within the top level */
1222 *from = &lpm->tbl24[ind];
1223 ind += (1 << (24 - depth)) - 1;
1224 *to = &lpm->tbl24[ind];
1225 *out_tbl_ind = TBL24_IND;
1227 /* top level entry */
1228 struct rte_lpm6_tbl_entry *tbl = &lpm->tbl24[first_3bytes];
1229 assert(tbl->ext_entry == 1);
1231 uint32_t tbl_ind = tbl->lpm6_tbl8_gindex;
1232 tbl = &lpm->tbl8[tbl_ind *
1233 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1234 /* current ip byte, the top level is already behind */
1236 /* minus top level */
1239 /* iterate through levels (tbl8s)
1240 * until we reach the last one
1244 assert(tbl->ext_entry == 1);
1245 /* go to the next level/tbl8 */
1246 tbl_ind = tbl->lpm6_tbl8_gindex;
1247 tbl = &lpm->tbl8[tbl_ind *
1248 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1253 /* last level/tbl8 */
1254 ind = ip[byte] & depth_to_mask_1b(depth);
1256 ind += (1 << (8 - depth)) - 1;
1258 *out_tbl_ind = tbl_ind;
1263 * Remove a table from the LPM tree
1266 remove_tbl(struct rte_lpm6 *lpm, struct rte_lpm_tbl8_hdr *tbl_hdr,
1267 uint32_t tbl_ind, struct rte_lpm6_rule *lsp_rule)
1269 struct rte_lpm6_tbl_entry *owner_entry;
1271 if (tbl_hdr->owner_tbl_ind == TBL24_IND)
1272 owner_entry = &lpm->tbl24[tbl_hdr->owner_entry_ind];
1274 uint32_t owner_tbl_ind = tbl_hdr->owner_tbl_ind;
1275 owner_entry = &lpm->tbl8[
1276 owner_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES +
1277 tbl_hdr->owner_entry_ind];
1279 struct rte_lpm_tbl8_hdr *owner_tbl_hdr =
1280 &lpm->tbl8_hdrs[owner_tbl_ind];
1281 if (--owner_tbl_hdr->ref_cnt == 0)
1282 remove_tbl(lpm, owner_tbl_hdr, owner_tbl_ind, lsp_rule);
1285 assert(owner_entry->ext_entry == 1);
1287 /* unlink the table */
1288 if (lsp_rule != NULL) {
1289 struct rte_lpm6_tbl_entry new_tbl_entry = {
1290 .next_hop = lsp_rule->next_hop,
1291 .depth = lsp_rule->depth,
1293 .valid_group = VALID,
1297 *owner_entry = new_tbl_entry;
1299 struct rte_lpm6_tbl_entry new_tbl_entry = {
1303 .valid_group = INVALID,
1307 *owner_entry = new_tbl_entry;
1310 /* return the table to the pool */
1311 tbl8_put(lpm, tbl_ind);
1318 rte_lpm6_delete(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1319 struct rte_lpm6_rule *lsp_rule)
1321 uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1322 //struct rte_lpm6_rule lsp_rule_obj;
1323 //struct rte_lpm6_rule *lsp_rule;
1326 struct rte_lpm6_tbl_entry *from, *to;
1328 /* Check input arguments. */
1329 if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1332 /* Copy the IP and mask it to avoid modifying user's input data. */
1333 ip6_copy_addr(masked_ip, ip);
1334 ip6_mask_addr(masked_ip, depth);
1337 /* Delete the rule from the rule table. */
1338 ret = rule_delete(lpm, masked_ip, depth);
1343 /* find rule cells */
1344 rule_find_range(lpm, masked_ip, depth, &from, &to, &tbl_ind);
1347 /* find a less specific rule (a rule with smaller depth)
1348 * note: masked_ip will be modified, don't use it anymore
1350 ret = rule_find_less_specific(lpm, masked_ip, depth,
1352 lsp_rule = ret ? &lsp_rule_obj : NULL;
1354 /* decrement the table rule counter,
1355 * note that tbl24 doesn't have a header
1357 if (tbl_ind != TBL24_IND) {
1358 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
1359 if (--tbl_hdr->ref_cnt == 0) {
1360 /* remove the table */
1361 remove_tbl(lpm, tbl_hdr, tbl_ind, lsp_rule);
1366 /* iterate rule cells */
1367 for (; from <= to; from++)
1368 if (from->ext_entry == 1) {
1369 /* reference to a more specific space
1370 * of the prefix/rule. Entries in a more
1371 * specific space that are not used by
1372 * a more specific prefix must be occupied
1375 if (lsp_rule != NULL)
1377 from->lpm6_tbl8_gindex *
1378 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1379 depth, lsp_rule->depth,
1380 lsp_rule->next_hop, VALID);
1382 /* since the prefix has no less specific prefix,
1383 * its more specific space must be invalidated
1386 from->lpm6_tbl8_gindex *
1387 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1388 depth, 0, 0, INVALID);
1389 } else if (from->depth == depth) {
1390 /* entry is not a reference and belongs to the prefix */
1391 if (lsp_rule != NULL) {
1392 struct rte_lpm6_tbl_entry new_tbl_entry = {
1393 .next_hop = lsp_rule->next_hop,
1394 .depth = lsp_rule->depth,
1396 .valid_group = VALID,
1400 *from = new_tbl_entry;
1402 struct rte_lpm6_tbl_entry new_tbl_entry = {
1406 .valid_group = INVALID,
1410 *from = new_tbl_entry;