lib: annotate versioned functions
[dpdk.git] / lib / librte_lpm / rte_lpm6.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <string.h>
5 #include <stdint.h>
6 #include <errno.h>
7 #include <stdarg.h>
8 #include <stdio.h>
9 #include <sys/queue.h>
10
11 #include <rte_log.h>
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>
17 #include <rte_eal.h>
18 #include <rte_eal_memconfig.h>
19 #include <rte_per_lcore.h>
20 #include <rte_string_fns.h>
21 #include <rte_errno.h>
22 #include <rte_rwlock.h>
23 #include <rte_spinlock.h>
24 #include <rte_hash.h>
25 #include <assert.h>
26 #include <rte_jhash.h>
27 #include <rte_tailq.h>
28 #include <rte_function_versioning.h>
29
30 #include "rte_lpm6.h"
31
32 #define RTE_LPM6_TBL24_NUM_ENTRIES        (1 << 24)
33 #define RTE_LPM6_TBL8_GROUP_NUM_ENTRIES         256
34 #define RTE_LPM6_TBL8_MAX_NUM_GROUPS      (1 << 21)
35
36 #define RTE_LPM6_VALID_EXT_ENTRY_BITMASK 0xA0000000
37 #define RTE_LPM6_LOOKUP_SUCCESS          0x20000000
38 #define RTE_LPM6_TBL8_BITMASK            0x001FFFFF
39
40 #define ADD_FIRST_BYTE                            3
41 #define LOOKUP_FIRST_BYTE                         4
42 #define BYTE_SIZE                                 8
43 #define BYTES2_SIZE                              16
44
45 #define RULE_HASH_TABLE_EXTRA_SPACE              64
46 #define TBL24_IND                        UINT32_MAX
47
48 #define lpm6_tbl8_gindex next_hop
49
50 /** Flags for setting an entry as valid/invalid. */
51 enum valid_flag {
52         INVALID = 0,
53         VALID
54 };
55
56 TAILQ_HEAD(rte_lpm6_list, rte_tailq_entry);
57
58 static struct rte_tailq_elem rte_lpm6_tailq = {
59         .name = "RTE_LPM6",
60 };
61 EAL_REGISTER_TAILQ(rte_lpm6_tailq)
62
63 /** Tbl entry structure. It is the same for both tbl24 and tbl8 */
64 struct rte_lpm6_tbl_entry {
65         uint32_t next_hop:      21;  /**< Next hop / next table to be checked. */
66         uint32_t depth  :8;      /**< Rule depth. */
67
68         /* Flags. */
69         uint32_t valid     :1;   /**< Validation flag. */
70         uint32_t valid_group :1; /**< Group validation flag. */
71         uint32_t ext_entry :1;   /**< External entry. */
72 };
73
74 /** Rules tbl entry structure. */
75 struct rte_lpm6_rule {
76         uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
77         uint32_t next_hop; /**< Rule next hop. */
78         uint8_t depth; /**< Rule depth. */
79 };
80
81 /** Rules tbl entry key. */
82 struct rte_lpm6_rule_key {
83         uint8_t ip[RTE_LPM6_IPV6_ADDR_SIZE]; /**< Rule IP address. */
84         uint8_t depth; /**< Rule depth. */
85 };
86
87 /* Header of tbl8 */
88 struct rte_lpm_tbl8_hdr {
89         uint32_t owner_tbl_ind; /**< owner table: TBL24_IND if owner is tbl24,
90                                   *  otherwise index of tbl8
91                                   */
92         uint32_t owner_entry_ind; /**< index of the owner table entry where
93                                     *  pointer to the tbl8 is stored
94                                     */
95         uint32_t ref_cnt; /**< table reference counter */
96 };
97
98 /** LPM6 structure. */
99 struct rte_lpm6 {
100         /* LPM metadata. */
101         char name[RTE_LPM6_NAMESIZE];    /**< Name of the lpm. */
102         uint32_t max_rules;              /**< Max number of rules. */
103         uint32_t used_rules;             /**< Used rules so far. */
104         uint32_t number_tbl8s;           /**< Number of tbl8s to allocate. */
105
106         /* LPM Tables. */
107         struct rte_hash *rules_tbl; /**< LPM rules. */
108         struct rte_lpm6_tbl_entry tbl24[RTE_LPM6_TBL24_NUM_ENTRIES]
109                         __rte_cache_aligned; /**< LPM tbl24 table. */
110
111         uint32_t *tbl8_pool; /**< pool of indexes of free tbl8s */
112         uint32_t tbl8_pool_pos; /**< current position in the tbl8 pool */
113
114         struct rte_lpm_tbl8_hdr *tbl8_hdrs; /* array of tbl8 headers */
115
116         struct rte_lpm6_tbl_entry tbl8[0]
117                         __rte_cache_aligned; /**< LPM tbl8 table. */
118 };
119
120 /*
121  * Takes an array of uint8_t (IPv6 address) and masks it using the depth.
122  * It leaves untouched one bit per unit in the depth variable
123  * and set the rest to 0.
124  */
125 static inline void
126 ip6_mask_addr(uint8_t *ip, uint8_t depth)
127 {
128         int16_t part_depth, mask;
129         int i;
130
131         part_depth = depth;
132
133         for (i = 0; i < RTE_LPM6_IPV6_ADDR_SIZE; i++) {
134                 if (part_depth < BYTE_SIZE && part_depth >= 0) {
135                         mask = (uint16_t)(~(UINT8_MAX >> part_depth));
136                         ip[i] = (uint8_t)(ip[i] & mask);
137                 } else if (part_depth < 0)
138                         ip[i] = 0;
139
140                 part_depth -= BYTE_SIZE;
141         }
142 }
143
144 /* copy ipv6 address */
145 static inline void
146 ip6_copy_addr(uint8_t *dst, const uint8_t *src)
147 {
148         rte_memcpy(dst, src, RTE_LPM6_IPV6_ADDR_SIZE);
149 }
150
151 /*
152  * LPM6 rule hash function
153  *
154  * It's used as a hash function for the rte_hash
155  *      containing rules
156  */
157 static inline uint32_t
158 rule_hash(const void *data, __rte_unused uint32_t data_len,
159                   uint32_t init_val)
160 {
161         return rte_jhash(data, sizeof(struct rte_lpm6_rule_key), init_val);
162 }
163
164 /*
165  * Init pool of free tbl8 indexes
166  */
167 static void
168 tbl8_pool_init(struct rte_lpm6 *lpm)
169 {
170         uint32_t i;
171
172         /* put entire range of indexes to the tbl8 pool */
173         for (i = 0; i < lpm->number_tbl8s; i++)
174                 lpm->tbl8_pool[i] = i;
175
176         lpm->tbl8_pool_pos = 0;
177 }
178
179 /*
180  * Get an index of a free tbl8 from the pool
181  */
182 static inline uint32_t
183 tbl8_get(struct rte_lpm6 *lpm, uint32_t *tbl8_ind)
184 {
185         if (lpm->tbl8_pool_pos == lpm->number_tbl8s)
186                 /* no more free tbl8 */
187                 return -ENOSPC;
188
189         /* next index */
190         *tbl8_ind = lpm->tbl8_pool[lpm->tbl8_pool_pos++];
191         return 0;
192 }
193
194 /*
195  * Put an index of a free tbl8 back to the pool
196  */
197 static inline uint32_t
198 tbl8_put(struct rte_lpm6 *lpm, uint32_t tbl8_ind)
199 {
200         if (lpm->tbl8_pool_pos == 0)
201                 /* pool is full */
202                 return -ENOSPC;
203
204         lpm->tbl8_pool[--lpm->tbl8_pool_pos] = tbl8_ind;
205         return 0;
206 }
207
208 /*
209  * Returns number of tbl8s available in the pool
210  */
211 static inline uint32_t
212 tbl8_available(struct rte_lpm6 *lpm)
213 {
214         return lpm->number_tbl8s - lpm->tbl8_pool_pos;
215 }
216
217 /*
218  * Init a rule key.
219  *        note that ip must be already masked
220  */
221 static inline void
222 rule_key_init(struct rte_lpm6_rule_key *key, uint8_t *ip, uint8_t depth)
223 {
224         ip6_copy_addr(key->ip, ip);
225         key->depth = depth;
226 }
227
228 /*
229  * Rebuild the entire LPM tree by reinserting all rules
230  */
231 static void
232 rebuild_lpm(struct rte_lpm6 *lpm)
233 {
234         uint64_t next_hop;
235         struct rte_lpm6_rule_key *rule_key;
236         uint32_t iter = 0;
237
238         while (rte_hash_iterate(lpm->rules_tbl, (void *) &rule_key,
239                         (void **) &next_hop, &iter) >= 0)
240                 rte_lpm6_add(lpm, rule_key->ip, rule_key->depth,
241                         (uint32_t) next_hop);
242 }
243
244 /*
245  * Allocates memory for LPM object
246  */
247 struct rte_lpm6 *
248 rte_lpm6_create(const char *name, int socket_id,
249                 const struct rte_lpm6_config *config)
250 {
251         char mem_name[RTE_LPM6_NAMESIZE];
252         struct rte_lpm6 *lpm = NULL;
253         struct rte_tailq_entry *te;
254         uint64_t mem_size;
255         struct rte_lpm6_list *lpm_list;
256         struct rte_hash *rules_tbl = NULL;
257         uint32_t *tbl8_pool = NULL;
258         struct rte_lpm_tbl8_hdr *tbl8_hdrs = NULL;
259
260         lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
261
262         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm6_tbl_entry) != sizeof(uint32_t));
263
264         /* Check user arguments. */
265         if ((name == NULL) || (socket_id < -1) || (config == NULL) ||
266                         (config->max_rules == 0) ||
267                         config->number_tbl8s > RTE_LPM6_TBL8_MAX_NUM_GROUPS) {
268                 rte_errno = EINVAL;
269                 return NULL;
270         }
271
272         /* create rules hash table */
273         snprintf(mem_name, sizeof(mem_name), "LRH_%s", name);
274         struct rte_hash_parameters rule_hash_tbl_params = {
275                 .entries = config->max_rules * 1.2 +
276                         RULE_HASH_TABLE_EXTRA_SPACE,
277                 .key_len = sizeof(struct rte_lpm6_rule_key),
278                 .hash_func = rule_hash,
279                 .hash_func_init_val = 0,
280                 .name = mem_name,
281                 .reserved = 0,
282                 .socket_id = socket_id,
283                 .extra_flag = 0
284         };
285
286         rules_tbl = rte_hash_create(&rule_hash_tbl_params);
287         if (rules_tbl == NULL) {
288                 RTE_LOG(ERR, LPM, "LPM rules hash table allocation failed: %s (%d)",
289                                   rte_strerror(rte_errno), rte_errno);
290                 goto fail_wo_unlock;
291         }
292
293         /* allocate tbl8 indexes pool */
294         tbl8_pool = rte_malloc(NULL,
295                         sizeof(uint32_t) * config->number_tbl8s,
296                         RTE_CACHE_LINE_SIZE);
297         if (tbl8_pool == NULL) {
298                 RTE_LOG(ERR, LPM, "LPM tbl8 pool allocation failed: %s (%d)",
299                                   rte_strerror(rte_errno), rte_errno);
300                 rte_errno = ENOMEM;
301                 goto fail_wo_unlock;
302         }
303
304         /* allocate tbl8 headers */
305         tbl8_hdrs = rte_malloc(NULL,
306                         sizeof(struct rte_lpm_tbl8_hdr) * config->number_tbl8s,
307                         RTE_CACHE_LINE_SIZE);
308         if (tbl8_hdrs == NULL) {
309                 RTE_LOG(ERR, LPM, "LPM tbl8 headers allocation failed: %s (%d)",
310                                   rte_strerror(rte_errno), rte_errno);
311                 rte_errno = ENOMEM;
312                 goto fail_wo_unlock;
313         }
314
315         snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
316
317         /* Determine the amount of memory to allocate. */
318         mem_size = sizeof(*lpm) + (sizeof(lpm->tbl8[0]) *
319                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
320
321         rte_mcfg_tailq_write_lock();
322
323         /* Guarantee there's no existing */
324         TAILQ_FOREACH(te, lpm_list, next) {
325                 lpm = (struct rte_lpm6 *) te->data;
326                 if (strncmp(name, lpm->name, RTE_LPM6_NAMESIZE) == 0)
327                         break;
328         }
329         lpm = NULL;
330         if (te != NULL) {
331                 rte_errno = EEXIST;
332                 goto fail;
333         }
334
335         /* allocate tailq entry */
336         te = rte_zmalloc("LPM6_TAILQ_ENTRY", sizeof(*te), 0);
337         if (te == NULL) {
338                 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry!\n");
339                 rte_errno = ENOMEM;
340                 goto fail;
341         }
342
343         /* Allocate memory to store the LPM data structures. */
344         lpm = rte_zmalloc_socket(mem_name, (size_t)mem_size,
345                         RTE_CACHE_LINE_SIZE, socket_id);
346
347         if (lpm == NULL) {
348                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
349                 rte_free(te);
350                 rte_errno = ENOMEM;
351                 goto fail;
352         }
353
354         /* Save user arguments. */
355         lpm->max_rules = config->max_rules;
356         lpm->number_tbl8s = config->number_tbl8s;
357         strlcpy(lpm->name, name, sizeof(lpm->name));
358         lpm->rules_tbl = rules_tbl;
359         lpm->tbl8_pool = tbl8_pool;
360         lpm->tbl8_hdrs = tbl8_hdrs;
361
362         /* init the stack */
363         tbl8_pool_init(lpm);
364
365         te->data = (void *) lpm;
366
367         TAILQ_INSERT_TAIL(lpm_list, te, next);
368         rte_mcfg_tailq_write_unlock();
369         return lpm;
370
371 fail:
372         rte_mcfg_tailq_write_unlock();
373
374 fail_wo_unlock:
375         rte_free(tbl8_hdrs);
376         rte_free(tbl8_pool);
377         rte_hash_free(rules_tbl);
378
379         return NULL;
380 }
381
382 /*
383  * Find an existing lpm table and return a pointer to it.
384  */
385 struct rte_lpm6 *
386 rte_lpm6_find_existing(const char *name)
387 {
388         struct rte_lpm6 *l = NULL;
389         struct rte_tailq_entry *te;
390         struct rte_lpm6_list *lpm_list;
391
392         lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
393
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)
398                         break;
399         }
400         rte_mcfg_tailq_read_unlock();
401
402         if (te == NULL) {
403                 rte_errno = ENOENT;
404                 return NULL;
405         }
406
407         return l;
408 }
409
410 /*
411  * Deallocates memory for given LPM table.
412  */
413 void
414 rte_lpm6_free(struct rte_lpm6 *lpm)
415 {
416         struct rte_lpm6_list *lpm_list;
417         struct rte_tailq_entry *te;
418
419         /* Check user arguments. */
420         if (lpm == NULL)
421                 return;
422
423         lpm_list = RTE_TAILQ_CAST(rte_lpm6_tailq.head, rte_lpm6_list);
424
425         rte_mcfg_tailq_write_lock();
426
427         /* find our tailq entry */
428         TAILQ_FOREACH(te, lpm_list, next) {
429                 if (te->data == (void *) lpm)
430                         break;
431         }
432
433         if (te != NULL)
434                 TAILQ_REMOVE(lpm_list, te, next);
435
436         rte_mcfg_tailq_write_unlock();
437
438         rte_free(lpm->tbl8_hdrs);
439         rte_free(lpm->tbl8_pool);
440         rte_hash_free(lpm->rules_tbl);
441         rte_free(lpm);
442         rte_free(te);
443 }
444
445 /* Find a rule */
446 static inline int
447 rule_find_with_key(struct rte_lpm6 *lpm,
448                   const struct rte_lpm6_rule_key *rule_key,
449                   uint32_t *next_hop)
450 {
451         uint64_t hash_val;
452         int ret;
453
454         /* lookup for a rule */
455         ret = rte_hash_lookup_data(lpm->rules_tbl, (const void *) rule_key,
456                 (void **) &hash_val);
457         if (ret >= 0) {
458                 *next_hop = (uint32_t) hash_val;
459                 return 1;
460         }
461
462         return 0;
463 }
464
465 /* Find a rule */
466 static int
467 rule_find(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
468                   uint32_t *next_hop)
469 {
470         struct rte_lpm6_rule_key rule_key;
471
472         /* init a rule key */
473         rule_key_init(&rule_key, ip, depth);
474
475         return rule_find_with_key(lpm, &rule_key, next_hop);
476 }
477
478 /*
479  * Checks if a rule already exists in the rules table and updates
480  * the nexthop if so. Otherwise it adds a new rule if enough space is available.
481  *
482  * Returns:
483  *    0 - next hop of existed rule is updated
484  *    1 - new rule successfully added
485  *   <0 - error
486  */
487 static inline int
488 rule_add(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth, uint32_t next_hop)
489 {
490         int ret, rule_exist;
491         struct rte_lpm6_rule_key rule_key;
492         uint32_t unused;
493
494         /* init a rule key */
495         rule_key_init(&rule_key, ip, depth);
496
497         /* Scan through rule list to see if rule already exists. */
498         rule_exist = rule_find_with_key(lpm, &rule_key, &unused);
499
500         /*
501          * If rule does not exist check if there is space to add a new rule to
502          * this rule group. If there is no space return error.
503          */
504         if (!rule_exist && lpm->used_rules == lpm->max_rules)
505                 return -ENOSPC;
506
507         /* add the rule or update rules next hop */
508         ret = rte_hash_add_key_data(lpm->rules_tbl, &rule_key,
509                 (void *)(uintptr_t) next_hop);
510         if (ret < 0)
511                 return ret;
512
513         /* Increment the used rules counter for this rule group. */
514         if (!rule_exist) {
515                 lpm->used_rules++;
516                 return 1;
517         }
518
519         return 0;
520 }
521
522 /*
523  * Function that expands a rule across the data structure when a less-generic
524  * one has been added before. It assures that every possible combination of bits
525  * in the IP address returns a match.
526  */
527 static void
528 expand_rule(struct rte_lpm6 *lpm, uint32_t tbl8_gindex, uint8_t old_depth,
529                 uint8_t new_depth, uint32_t next_hop, uint8_t valid)
530 {
531         uint32_t tbl8_group_end, tbl8_gindex_next, j;
532
533         tbl8_group_end = tbl8_gindex + RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
534
535         struct rte_lpm6_tbl_entry new_tbl8_entry = {
536                 .valid = valid,
537                 .valid_group = valid,
538                 .depth = new_depth,
539                 .next_hop = next_hop,
540                 .ext_entry = 0,
541         };
542
543         for (j = tbl8_gindex; j < tbl8_group_end; j++) {
544                 if (!lpm->tbl8[j].valid || (lpm->tbl8[j].ext_entry == 0
545                                 && lpm->tbl8[j].depth <= old_depth)) {
546
547                         lpm->tbl8[j] = new_tbl8_entry;
548
549                 } else if (lpm->tbl8[j].ext_entry == 1) {
550
551                         tbl8_gindex_next = lpm->tbl8[j].lpm6_tbl8_gindex
552                                         * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
553                         expand_rule(lpm, tbl8_gindex_next, old_depth, new_depth,
554                                         next_hop, valid);
555                 }
556         }
557 }
558
559 /*
560  * Init a tbl8 header
561  */
562 static inline void
563 init_tbl8_header(struct rte_lpm6 *lpm, uint32_t tbl_ind,
564                 uint32_t owner_tbl_ind, uint32_t owner_entry_ind)
565 {
566         struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
567         tbl_hdr->owner_tbl_ind = owner_tbl_ind;
568         tbl_hdr->owner_entry_ind = owner_entry_ind;
569         tbl_hdr->ref_cnt = 0;
570 }
571
572 /*
573  * Calculate index to the table based on the number and position
574  * of the bytes being inspected in this step.
575  */
576 static uint32_t
577 get_bitshift(const uint8_t *ip, uint8_t first_byte, uint8_t bytes)
578 {
579         uint32_t entry_ind, i;
580         int8_t bitshift;
581
582         entry_ind = 0;
583         for (i = first_byte; i < (uint32_t)(first_byte + bytes); i++) {
584                 bitshift = (int8_t)((bytes - i)*BYTE_SIZE);
585
586                 if (bitshift < 0)
587                         bitshift = 0;
588                 entry_ind = entry_ind | ip[i-1] << bitshift;
589         }
590
591         return entry_ind;
592 }
593
594 /*
595  * Simulate adding a new route to the LPM counting number
596  * of new tables that will be needed
597  *
598  * It returns 0 on success, or 1 if
599  * the process needs to be continued by calling the function again.
600  */
601 static inline int
602 simulate_add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
603                 struct rte_lpm6_tbl_entry **next_tbl, const uint8_t *ip,
604                 uint8_t bytes, uint8_t first_byte, uint8_t depth,
605                 uint32_t *need_tbl_nb)
606 {
607         uint32_t entry_ind;
608         uint8_t bits_covered;
609         uint32_t next_tbl_ind;
610
611         /*
612          * Calculate index to the table based on the number and position
613          * of the bytes being inspected in this step.
614          */
615         entry_ind = get_bitshift(ip, first_byte, bytes);
616
617         /* Number of bits covered in this step */
618         bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
619
620         if (depth <= bits_covered) {
621                 *need_tbl_nb = 0;
622                 return 0;
623         }
624
625         if (tbl[entry_ind].valid == 0 || tbl[entry_ind].ext_entry == 0) {
626                 /* from this point on a new table is needed on each level
627                  * that is not covered yet
628                  */
629                 depth -= bits_covered;
630                 uint32_t cnt = depth >> 3; /* depth / BYTE_SIZE */
631                 if (depth & 7) /* 0b00000111 */
632                         /* if depth % 8 > 0 then one more table is needed
633                          * for those last bits
634                          */
635                         cnt++;
636
637                 *need_tbl_nb = cnt;
638                 return 0;
639         }
640
641         next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
642         *next_tbl = &(lpm->tbl8[next_tbl_ind *
643                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
644         *need_tbl_nb = 0;
645         return 1;
646 }
647
648 /*
649  * Partially adds a new route to the data structure (tbl24+tbl8s).
650  * It returns 0 on success, a negative number on failure, or 1 if
651  * the process needs to be continued by calling the function again.
652  */
653 static inline int
654 add_step(struct rte_lpm6 *lpm, struct rte_lpm6_tbl_entry *tbl,
655                 uint32_t tbl_ind, struct rte_lpm6_tbl_entry **next_tbl,
656                 uint32_t *next_tbl_ind, uint8_t *ip, uint8_t bytes,
657                 uint8_t first_byte, uint8_t depth, uint32_t next_hop,
658                 uint8_t is_new_rule)
659 {
660         uint32_t entry_ind, tbl_range, tbl8_group_start, tbl8_group_end, i;
661         uint32_t tbl8_gindex;
662         uint8_t bits_covered;
663         int ret;
664
665         /*
666          * Calculate index to the table based on the number and position
667          * of the bytes being inspected in this step.
668          */
669         entry_ind = get_bitshift(ip, first_byte, bytes);
670
671         /* Number of bits covered in this step */
672         bits_covered = (uint8_t)((bytes+first_byte-1)*BYTE_SIZE);
673
674         /*
675          * If depth if smaller than this number (ie this is the last step)
676          * expand the rule across the relevant positions in the table.
677          */
678         if (depth <= bits_covered) {
679                 tbl_range = 1 << (bits_covered - depth);
680
681                 for (i = entry_ind; i < (entry_ind + tbl_range); i++) {
682                         if (!tbl[i].valid || (tbl[i].ext_entry == 0 &&
683                                         tbl[i].depth <= depth)) {
684
685                                 struct rte_lpm6_tbl_entry new_tbl_entry = {
686                                         .next_hop = next_hop,
687                                         .depth = depth,
688                                         .valid = VALID,
689                                         .valid_group = VALID,
690                                         .ext_entry = 0,
691                                 };
692
693                                 tbl[i] = new_tbl_entry;
694
695                         } else if (tbl[i].ext_entry == 1) {
696
697                                 /*
698                                  * If tbl entry is valid and extended calculate the index
699                                  * into next tbl8 and expand the rule across the data structure.
700                                  */
701                                 tbl8_gindex = tbl[i].lpm6_tbl8_gindex *
702                                                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
703                                 expand_rule(lpm, tbl8_gindex, depth, depth,
704                                                 next_hop, VALID);
705                         }
706                 }
707
708                 /* update tbl8 rule reference counter */
709                 if (tbl_ind != TBL24_IND && is_new_rule)
710                         lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
711
712                 return 0;
713         }
714         /*
715          * If this is not the last step just fill one position
716          * and calculate the index to the next table.
717          */
718         else {
719                 /* If it's invalid a new tbl8 is needed */
720                 if (!tbl[entry_ind].valid) {
721                         /* get a new table */
722                         ret = tbl8_get(lpm, &tbl8_gindex);
723                         if (ret != 0)
724                                 return -ENOSPC;
725
726                         /* invalidate all new tbl8 entries */
727                         tbl8_group_start = tbl8_gindex *
728                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
729                         memset(&lpm->tbl8[tbl8_group_start], 0,
730                                           RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
731
732                         /* init the new table's header:
733                          *   save the reference to the owner table
734                          */
735                         init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
736
737                         /* reference to a new tbl8 */
738                         struct rte_lpm6_tbl_entry new_tbl_entry = {
739                                 .lpm6_tbl8_gindex = tbl8_gindex,
740                                 .depth = 0,
741                                 .valid = VALID,
742                                 .valid_group = VALID,
743                                 .ext_entry = 1,
744                         };
745
746                         tbl[entry_ind] = new_tbl_entry;
747
748                         /* update the current table's reference counter */
749                         if (tbl_ind != TBL24_IND)
750                                 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
751                 }
752                 /*
753                  * If it's valid but not extended the rule that was stored
754                  * here needs to be moved to the next table.
755                  */
756                 else if (tbl[entry_ind].ext_entry == 0) {
757                         /* get a new tbl8 index */
758                         ret = tbl8_get(lpm, &tbl8_gindex);
759                         if (ret != 0)
760                                 return -ENOSPC;
761
762                         tbl8_group_start = tbl8_gindex *
763                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
764                         tbl8_group_end = tbl8_group_start +
765                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES;
766
767                         struct rte_lpm6_tbl_entry tbl_entry = {
768                                 .next_hop = tbl[entry_ind].next_hop,
769                                 .depth = tbl[entry_ind].depth,
770                                 .valid = VALID,
771                                 .valid_group = VALID,
772                                 .ext_entry = 0
773                         };
774
775                         /* Populate new tbl8 with tbl value. */
776                         for (i = tbl8_group_start; i < tbl8_group_end; i++)
777                                 lpm->tbl8[i] = tbl_entry;
778
779                         /* init the new table's header:
780                          *   save the reference to the owner table
781                          */
782                         init_tbl8_header(lpm, tbl8_gindex, tbl_ind, entry_ind);
783
784                         /*
785                          * Update tbl entry to point to new tbl8 entry. Note: The
786                          * ext_flag and tbl8_index need to be updated simultaneously,
787                          * so assign whole structure in one go.
788                          */
789                         struct rte_lpm6_tbl_entry new_tbl_entry = {
790                                 .lpm6_tbl8_gindex = tbl8_gindex,
791                                 .depth = 0,
792                                 .valid = VALID,
793                                 .valid_group = VALID,
794                                 .ext_entry = 1,
795                         };
796
797                         tbl[entry_ind] = new_tbl_entry;
798
799                         /* update the current table's reference counter */
800                         if (tbl_ind != TBL24_IND)
801                                 lpm->tbl8_hdrs[tbl_ind].ref_cnt++;
802                 }
803
804                 *next_tbl_ind = tbl[entry_ind].lpm6_tbl8_gindex;
805                 *next_tbl = &(lpm->tbl8[*next_tbl_ind *
806                                   RTE_LPM6_TBL8_GROUP_NUM_ENTRIES]);
807         }
808
809         return 1;
810 }
811
812 /*
813  * Add a route
814  */
815 int __vsym
816 rte_lpm6_add_v20(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
817                 uint8_t next_hop)
818 {
819         return rte_lpm6_add_v1705(lpm, ip, depth, next_hop);
820 }
821 VERSION_SYMBOL(rte_lpm6_add, _v20, 2.0);
822
823
824 /*
825  * Simulate adding a route to LPM
826  *
827  *      Returns:
828  *    0 on success
829  *    -ENOSPC not enought tbl8 left
830  */
831 static int
832 simulate_add(struct rte_lpm6 *lpm, const uint8_t *masked_ip, uint8_t depth)
833 {
834         struct rte_lpm6_tbl_entry *tbl;
835         struct rte_lpm6_tbl_entry *tbl_next = NULL;
836         int ret, i;
837
838         /* number of new tables needed for a step */
839         uint32_t need_tbl_nb;
840         /* total number of new tables needed */
841         uint32_t total_need_tbl_nb;
842
843         /* Inspect the first three bytes through tbl24 on the first step. */
844         ret = simulate_add_step(lpm, lpm->tbl24, &tbl_next, masked_ip,
845                         ADD_FIRST_BYTE, 1, depth, &need_tbl_nb);
846         total_need_tbl_nb = need_tbl_nb;
847         /*
848          * Inspect one by one the rest of the bytes until
849          * the process is completed.
850          */
851         for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && ret == 1; i++) {
852                 tbl = tbl_next;
853                 ret = simulate_add_step(lpm, tbl, &tbl_next, masked_ip, 1,
854                                 (uint8_t)(i+1), depth, &need_tbl_nb);
855                 total_need_tbl_nb += need_tbl_nb;
856         }
857
858         if (tbl8_available(lpm) < total_need_tbl_nb)
859                 /* not enought tbl8 to add a rule */
860                 return -ENOSPC;
861
862         return 0;
863 }
864
865 int __vsym
866 rte_lpm6_add_v1705(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
867                 uint32_t next_hop)
868 {
869         struct rte_lpm6_tbl_entry *tbl;
870         struct rte_lpm6_tbl_entry *tbl_next = NULL;
871         /* init to avoid compiler warning */
872         uint32_t tbl_next_num = 123456;
873         int status;
874         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
875         int i;
876
877         /* Check user arguments. */
878         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
879                 return -EINVAL;
880
881         /* Copy the IP and mask it to avoid modifying user's input data. */
882         ip6_copy_addr(masked_ip, ip);
883         ip6_mask_addr(masked_ip, depth);
884
885         /* Simulate adding a new route */
886         int ret = simulate_add(lpm, masked_ip, depth);
887         if (ret < 0)
888                 return ret;
889
890         /* Add the rule to the rule table. */
891         int is_new_rule = rule_add(lpm, masked_ip, depth, next_hop);
892         /* If there is no space available for new rule return error. */
893         if (is_new_rule < 0)
894                 return is_new_rule;
895
896         /* Inspect the first three bytes through tbl24 on the first step. */
897         tbl = lpm->tbl24;
898         status = add_step(lpm, tbl, TBL24_IND, &tbl_next, &tbl_next_num,
899                         masked_ip, ADD_FIRST_BYTE, 1, depth, next_hop,
900                         is_new_rule);
901         assert(status >= 0);
902
903         /*
904          * Inspect one by one the rest of the bytes until
905          * the process is completed.
906          */
907         for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && status == 1; i++) {
908                 tbl = tbl_next;
909                 status = add_step(lpm, tbl, tbl_next_num, &tbl_next,
910                                 &tbl_next_num, masked_ip, 1, (uint8_t)(i+1),
911                                 depth, next_hop, is_new_rule);
912                 assert(status >= 0);
913         }
914
915         return status;
916 }
917 BIND_DEFAULT_SYMBOL(rte_lpm6_add, _v1705, 17.05);
918 MAP_STATIC_SYMBOL(int rte_lpm6_add(struct rte_lpm6 *lpm, uint8_t *ip,
919                                 uint8_t depth, uint32_t next_hop),
920                 rte_lpm6_add_v1705);
921
922 /*
923  * Takes a pointer to a table entry and inspect one level.
924  * The function returns 0 on lookup success, ENOENT if no match was found
925  * or 1 if the process needs to be continued by calling the function again.
926  */
927 static inline int
928 lookup_step(const struct rte_lpm6 *lpm, const struct rte_lpm6_tbl_entry *tbl,
929                 const struct rte_lpm6_tbl_entry **tbl_next, uint8_t *ip,
930                 uint8_t first_byte, uint32_t *next_hop)
931 {
932         uint32_t tbl8_index, tbl_entry;
933
934         /* Take the integer value from the pointer. */
935         tbl_entry = *(const uint32_t *)tbl;
936
937         /* If it is valid and extended we calculate the new pointer to return. */
938         if ((tbl_entry & RTE_LPM6_VALID_EXT_ENTRY_BITMASK) ==
939                         RTE_LPM6_VALID_EXT_ENTRY_BITMASK) {
940
941                 tbl8_index = ip[first_byte-1] +
942                                 ((tbl_entry & RTE_LPM6_TBL8_BITMASK) *
943                                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
944
945                 *tbl_next = &lpm->tbl8[tbl8_index];
946
947                 return 1;
948         } else {
949                 /* If not extended then we can have a match. */
950                 *next_hop = ((uint32_t)tbl_entry & RTE_LPM6_TBL8_BITMASK);
951                 return (tbl_entry & RTE_LPM6_LOOKUP_SUCCESS) ? 0 : -ENOENT;
952         }
953 }
954
955 /*
956  * Looks up an IP
957  */
958 int __vsym
959 rte_lpm6_lookup_v20(const struct rte_lpm6 *lpm, uint8_t *ip, uint8_t *next_hop)
960 {
961         uint32_t next_hop32 = 0;
962         int32_t status;
963
964         /* DEBUG: Check user input arguments. */
965         if (next_hop == NULL)
966                 return -EINVAL;
967
968         status = rte_lpm6_lookup_v1705(lpm, ip, &next_hop32);
969         if (status == 0)
970                 *next_hop = (uint8_t)next_hop32;
971
972         return status;
973 }
974 VERSION_SYMBOL(rte_lpm6_lookup, _v20, 2.0);
975
976 int __vsym
977 rte_lpm6_lookup_v1705(const struct rte_lpm6 *lpm, uint8_t *ip,
978                 uint32_t *next_hop)
979 {
980         const struct rte_lpm6_tbl_entry *tbl;
981         const struct rte_lpm6_tbl_entry *tbl_next = NULL;
982         int status;
983         uint8_t first_byte;
984         uint32_t tbl24_index;
985
986         /* DEBUG: Check user input arguments. */
987         if ((lpm == NULL) || (ip == NULL) || (next_hop == NULL))
988                 return -EINVAL;
989
990         first_byte = LOOKUP_FIRST_BYTE;
991         tbl24_index = (ip[0] << BYTES2_SIZE) | (ip[1] << BYTE_SIZE) | ip[2];
992
993         /* Calculate pointer to the first entry to be inspected */
994         tbl = &lpm->tbl24[tbl24_index];
995
996         do {
997                 /* Continue inspecting following levels until success or failure */
998                 status = lookup_step(lpm, tbl, &tbl_next, ip, first_byte++, next_hop);
999                 tbl = tbl_next;
1000         } while (status == 1);
1001
1002         return status;
1003 }
1004 BIND_DEFAULT_SYMBOL(rte_lpm6_lookup, _v1705, 17.05);
1005 MAP_STATIC_SYMBOL(int rte_lpm6_lookup(const struct rte_lpm6 *lpm, uint8_t *ip,
1006                                 uint32_t *next_hop), rte_lpm6_lookup_v1705);
1007
1008 /*
1009  * Looks up a group of IP addresses
1010  */
1011 int __vsym
1012 rte_lpm6_lookup_bulk_func_v20(const struct rte_lpm6 *lpm,
1013                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1014                 int16_t * next_hops, unsigned n)
1015 {
1016         unsigned i;
1017         const struct rte_lpm6_tbl_entry *tbl;
1018         const struct rte_lpm6_tbl_entry *tbl_next = NULL;
1019         uint32_t tbl24_index, next_hop;
1020         uint8_t first_byte;
1021         int status;
1022
1023         /* DEBUG: Check user input arguments. */
1024         if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
1025                 return -EINVAL;
1026
1027         for (i = 0; i < n; i++) {
1028                 first_byte = LOOKUP_FIRST_BYTE;
1029                 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
1030                                 (ips[i][1] << BYTE_SIZE) | ips[i][2];
1031
1032                 /* Calculate pointer to the first entry to be inspected */
1033                 tbl = &lpm->tbl24[tbl24_index];
1034
1035                 do {
1036                         /* Continue inspecting following levels until success or failure */
1037                         status = lookup_step(lpm, tbl, &tbl_next, ips[i], first_byte++,
1038                                         &next_hop);
1039                         tbl = tbl_next;
1040                 } while (status == 1);
1041
1042                 if (status < 0)
1043                         next_hops[i] = -1;
1044                 else
1045                         next_hops[i] = (int16_t)next_hop;
1046         }
1047
1048         return 0;
1049 }
1050 VERSION_SYMBOL(rte_lpm6_lookup_bulk_func, _v20, 2.0);
1051
1052 int __vsym
1053 rte_lpm6_lookup_bulk_func_v1705(const struct rte_lpm6 *lpm,
1054                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1055                 int32_t *next_hops, unsigned int n)
1056 {
1057         unsigned int i;
1058         const struct rte_lpm6_tbl_entry *tbl;
1059         const struct rte_lpm6_tbl_entry *tbl_next = NULL;
1060         uint32_t tbl24_index, next_hop;
1061         uint8_t first_byte;
1062         int status;
1063
1064         /* DEBUG: Check user input arguments. */
1065         if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
1066                 return -EINVAL;
1067
1068         for (i = 0; i < n; i++) {
1069                 first_byte = LOOKUP_FIRST_BYTE;
1070                 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
1071                                 (ips[i][1] << BYTE_SIZE) | ips[i][2];
1072
1073                 /* Calculate pointer to the first entry to be inspected */
1074                 tbl = &lpm->tbl24[tbl24_index];
1075
1076                 do {
1077                         /* Continue inspecting following levels
1078                          * until success or failure
1079                          */
1080                         status = lookup_step(lpm, tbl, &tbl_next, ips[i],
1081                                         first_byte++, &next_hop);
1082                         tbl = tbl_next;
1083                 } while (status == 1);
1084
1085                 if (status < 0)
1086                         next_hops[i] = -1;
1087                 else
1088                         next_hops[i] = (int32_t)next_hop;
1089         }
1090
1091         return 0;
1092 }
1093 BIND_DEFAULT_SYMBOL(rte_lpm6_lookup_bulk_func, _v1705, 17.05);
1094 MAP_STATIC_SYMBOL(int rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
1095                                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
1096                                 int32_t *next_hops, unsigned int n),
1097                 rte_lpm6_lookup_bulk_func_v1705);
1098
1099 /*
1100  * Look for a rule in the high-level rules table
1101  */
1102 int __vsym
1103 rte_lpm6_is_rule_present_v20(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1104                 uint8_t *next_hop)
1105 {
1106         uint32_t next_hop32 = 0;
1107         int32_t status;
1108
1109         /* DEBUG: Check user input arguments. */
1110         if (next_hop == NULL)
1111                 return -EINVAL;
1112
1113         status = rte_lpm6_is_rule_present_v1705(lpm, ip, depth, &next_hop32);
1114         if (status > 0)
1115                 *next_hop = (uint8_t)next_hop32;
1116
1117         return status;
1118
1119 }
1120 VERSION_SYMBOL(rte_lpm6_is_rule_present, _v20, 2.0);
1121
1122 int __vsym
1123 rte_lpm6_is_rule_present_v1705(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1124                 uint32_t *next_hop)
1125 {
1126         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1127
1128         /* Check user arguments. */
1129         if ((lpm == NULL) || next_hop == NULL || ip == NULL ||
1130                         (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1131                 return -EINVAL;
1132
1133         /* Copy the IP and mask it to avoid modifying user's input data. */
1134         ip6_copy_addr(masked_ip, ip);
1135         ip6_mask_addr(masked_ip, depth);
1136
1137         return rule_find(lpm, masked_ip, depth, next_hop);
1138 }
1139 BIND_DEFAULT_SYMBOL(rte_lpm6_is_rule_present, _v1705, 17.05);
1140 MAP_STATIC_SYMBOL(int rte_lpm6_is_rule_present(struct rte_lpm6 *lpm,
1141                                 uint8_t *ip, uint8_t depth, uint32_t *next_hop),
1142                 rte_lpm6_is_rule_present_v1705);
1143
1144 /*
1145  * Delete a rule from the rule table.
1146  * NOTE: Valid range for depth parameter is 1 .. 128 inclusive.
1147  * return
1148  *        0 on success
1149  *   <0 on failure
1150  */
1151 static inline int
1152 rule_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1153 {
1154         int ret;
1155         struct rte_lpm6_rule_key rule_key;
1156
1157         /* init rule key */
1158         rule_key_init(&rule_key, ip, depth);
1159
1160         /* delete the rule */
1161         ret = rte_hash_del_key(lpm->rules_tbl, (void *) &rule_key);
1162         if (ret >= 0)
1163                 lpm->used_rules--;
1164
1165         return ret;
1166 }
1167
1168 /*
1169  * Deletes a group of rules
1170  *
1171  * Note that the function rebuilds the lpm table,
1172  * rather than doing incremental updates like
1173  * the regular delete function
1174  */
1175 int
1176 rte_lpm6_delete_bulk_func(struct rte_lpm6 *lpm,
1177                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE], uint8_t *depths,
1178                 unsigned n)
1179 {
1180         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1181         unsigned i;
1182
1183         /* Check input arguments. */
1184         if ((lpm == NULL) || (ips == NULL) || (depths == NULL))
1185                 return -EINVAL;
1186
1187         for (i = 0; i < n; i++) {
1188                 ip6_copy_addr(masked_ip, ips[i]);
1189                 ip6_mask_addr(masked_ip, depths[i]);
1190                 rule_delete(lpm, masked_ip, depths[i]);
1191         }
1192
1193         /*
1194          * Set all the table entries to 0 (ie delete every rule
1195          * from the data structure.
1196          */
1197         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1198         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1199                         * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1200         tbl8_pool_init(lpm);
1201
1202         /*
1203          * Add every rule again (except for the ones that were removed from
1204          * the rules table).
1205          */
1206         rebuild_lpm(lpm);
1207
1208         return 0;
1209 }
1210
1211 /*
1212  * Delete all rules from the LPM table.
1213  */
1214 void
1215 rte_lpm6_delete_all(struct rte_lpm6 *lpm)
1216 {
1217         /* Zero used rules counter. */
1218         lpm->used_rules = 0;
1219
1220         /* Zero tbl24. */
1221         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1222
1223         /* Zero tbl8. */
1224         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0]) *
1225                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1226
1227         /* init pool of free tbl8 indexes */
1228         tbl8_pool_init(lpm);
1229
1230         /* Delete all rules form the rules table. */
1231         rte_hash_reset(lpm->rules_tbl);
1232 }
1233
1234 /*
1235  * Convert a depth to a one byte long mask
1236  *   Example: 4 will be converted to 0xF0
1237  */
1238 static uint8_t __attribute__((pure))
1239 depth_to_mask_1b(uint8_t depth)
1240 {
1241         /* To calculate a mask start with a 1 on the left hand side and right
1242          * shift while populating the left hand side with 1's
1243          */
1244         return (signed char)0x80 >> (depth - 1);
1245 }
1246
1247 /*
1248  * Find a less specific rule
1249  */
1250 static int
1251 rule_find_less_specific(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1252         struct rte_lpm6_rule *rule)
1253 {
1254         int ret;
1255         uint32_t next_hop;
1256         uint8_t mask;
1257         struct rte_lpm6_rule_key rule_key;
1258
1259         if (depth == 1)
1260                 return 0;
1261
1262         rule_key_init(&rule_key, ip, depth);
1263
1264         while (depth > 1) {
1265                 depth--;
1266
1267                 /* each iteration zero one more bit of the key */
1268                 mask = depth & 7; /* depth % BYTE_SIZE */
1269                 if (mask > 0)
1270                         mask = depth_to_mask_1b(mask);
1271
1272                 rule_key.depth = depth;
1273                 rule_key.ip[depth >> 3] &= mask;
1274
1275                 ret = rule_find_with_key(lpm, &rule_key, &next_hop);
1276                 if (ret) {
1277                         rule->depth = depth;
1278                         ip6_copy_addr(rule->ip, rule_key.ip);
1279                         rule->next_hop = next_hop;
1280                         return 1;
1281                 }
1282         }
1283
1284         return 0;
1285 }
1286
1287 /*
1288  * Find range of tbl8 cells occupied by a rule
1289  */
1290 static void
1291 rule_find_range(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1292                   struct rte_lpm6_tbl_entry **from,
1293                   struct rte_lpm6_tbl_entry **to,
1294                   uint32_t *out_tbl_ind)
1295 {
1296         uint32_t ind;
1297         uint32_t first_3bytes = (uint32_t)ip[0] << 16 | ip[1] << 8 | ip[2];
1298
1299         if (depth <= 24) {
1300                 /* rule is within the top level */
1301                 ind = first_3bytes;
1302                 *from = &lpm->tbl24[ind];
1303                 ind += (1 << (24 - depth)) - 1;
1304                 *to = &lpm->tbl24[ind];
1305                 *out_tbl_ind = TBL24_IND;
1306         } else {
1307                 /* top level entry */
1308                 struct rte_lpm6_tbl_entry *tbl = &lpm->tbl24[first_3bytes];
1309                 assert(tbl->ext_entry == 1);
1310                 /* first tbl8 */
1311                 uint32_t tbl_ind = tbl->lpm6_tbl8_gindex;
1312                 tbl = &lpm->tbl8[tbl_ind *
1313                                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1314                 /* current ip byte, the top level is already behind */
1315                 uint8_t byte = 3;
1316                 /* minus top level */
1317                 depth -= 24;
1318
1319                 /* interate through levels (tbl8s)
1320                  * until we reach the last one
1321                  */
1322                 while (depth > 8) {
1323                         tbl += ip[byte];
1324                         assert(tbl->ext_entry == 1);
1325                         /* go to the next level/tbl8 */
1326                         tbl_ind = tbl->lpm6_tbl8_gindex;
1327                         tbl = &lpm->tbl8[tbl_ind *
1328                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1329                         byte += 1;
1330                         depth -= 8;
1331                 }
1332
1333                 /* last level/tbl8 */
1334                 ind = ip[byte] & depth_to_mask_1b(depth);
1335                 *from = &tbl[ind];
1336                 ind += (1 << (8 - depth)) - 1;
1337                 *to = &tbl[ind];
1338                 *out_tbl_ind = tbl_ind;
1339         }
1340 }
1341
1342 /*
1343  * Remove a table from the LPM tree
1344  */
1345 static void
1346 remove_tbl(struct rte_lpm6 *lpm, struct rte_lpm_tbl8_hdr *tbl_hdr,
1347                   uint32_t tbl_ind, struct rte_lpm6_rule *lsp_rule)
1348 {
1349         struct rte_lpm6_tbl_entry *owner_entry;
1350
1351         if (tbl_hdr->owner_tbl_ind == TBL24_IND)
1352                 owner_entry = &lpm->tbl24[tbl_hdr->owner_entry_ind];
1353         else {
1354                 uint32_t owner_tbl_ind = tbl_hdr->owner_tbl_ind;
1355                 owner_entry = &lpm->tbl8[
1356                         owner_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES +
1357                         tbl_hdr->owner_entry_ind];
1358
1359                 struct rte_lpm_tbl8_hdr *owner_tbl_hdr =
1360                         &lpm->tbl8_hdrs[owner_tbl_ind];
1361                 if (--owner_tbl_hdr->ref_cnt == 0)
1362                         remove_tbl(lpm, owner_tbl_hdr, owner_tbl_ind, lsp_rule);
1363         }
1364
1365         assert(owner_entry->ext_entry == 1);
1366
1367         /* unlink the table */
1368         if (lsp_rule != NULL) {
1369                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1370                         .next_hop = lsp_rule->next_hop,
1371                         .depth = lsp_rule->depth,
1372                         .valid = VALID,
1373                         .valid_group = VALID,
1374                         .ext_entry = 0
1375                 };
1376
1377                 *owner_entry = new_tbl_entry;
1378         } else {
1379                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1380                         .next_hop = 0,
1381                         .depth = 0,
1382                         .valid = INVALID,
1383                         .valid_group = INVALID,
1384                         .ext_entry = 0
1385                 };
1386
1387                 *owner_entry = new_tbl_entry;
1388         }
1389
1390         /* return the table to the pool */
1391         tbl8_put(lpm, tbl_ind);
1392 }
1393
1394 /*
1395  * Deletes a rule
1396  */
1397 int
1398 rte_lpm6_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1399 {
1400         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1401         struct rte_lpm6_rule lsp_rule_obj;
1402         struct rte_lpm6_rule *lsp_rule;
1403         int ret;
1404         uint32_t tbl_ind;
1405         struct rte_lpm6_tbl_entry *from, *to;
1406
1407         /* Check input arguments. */
1408         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1409                 return -EINVAL;
1410
1411         /* Copy the IP and mask it to avoid modifying user's input data. */
1412         ip6_copy_addr(masked_ip, ip);
1413         ip6_mask_addr(masked_ip, depth);
1414
1415         /* Delete the rule from the rule table. */
1416         ret = rule_delete(lpm, masked_ip, depth);
1417         if (ret < 0)
1418                 return -ENOENT;
1419
1420         /* find rule cells */
1421         rule_find_range(lpm, masked_ip, depth, &from, &to, &tbl_ind);
1422
1423         /* find a less specific rule (a rule with smaller depth)
1424          * note: masked_ip will be modified, don't use it anymore
1425          */
1426         ret = rule_find_less_specific(lpm, masked_ip, depth,
1427                         &lsp_rule_obj);
1428         lsp_rule = ret ? &lsp_rule_obj : NULL;
1429
1430         /* decrement the table rule counter,
1431          * note that tbl24 doesn't have a header
1432          */
1433         if (tbl_ind != TBL24_IND) {
1434                 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
1435                 if (--tbl_hdr->ref_cnt == 0) {
1436                         /* remove the table */
1437                         remove_tbl(lpm, tbl_hdr, tbl_ind, lsp_rule);
1438                         return 0;
1439                 }
1440         }
1441
1442         /* iterate rule cells */
1443         for (; from <= to; from++)
1444                 if (from->ext_entry == 1) {
1445                         /* reference to a more specific space
1446                          * of the prefix/rule. Entries in a more
1447                          * specific space that are not used by
1448                          * a more specific prefix must be occupied
1449                          * by the prefix
1450                          */
1451                         if (lsp_rule != NULL)
1452                                 expand_rule(lpm,
1453                                         from->lpm6_tbl8_gindex *
1454                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1455                                         depth, lsp_rule->depth,
1456                                         lsp_rule->next_hop, VALID);
1457                         else
1458                                 /* since the prefix has no less specific prefix,
1459                                  * its more specific space must be invalidated
1460                                  */
1461                                 expand_rule(lpm,
1462                                         from->lpm6_tbl8_gindex *
1463                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1464                                         depth, 0, 0, INVALID);
1465                 } else if (from->depth == depth) {
1466                         /* entry is not a reference and belongs to the prefix */
1467                         if (lsp_rule != NULL) {
1468                                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1469                                         .next_hop = lsp_rule->next_hop,
1470                                         .depth = lsp_rule->depth,
1471                                         .valid = VALID,
1472                                         .valid_group = VALID,
1473                                         .ext_entry = 0
1474                                 };
1475
1476                                 *from = new_tbl_entry;
1477                         } else {
1478                                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1479                                         .next_hop = 0,
1480                                         .depth = 0,
1481                                         .valid = INVALID,
1482                                         .valid_group = INVALID,
1483                                         .ext_entry = 0
1484                                 };
1485
1486                                 *from = new_tbl_entry;
1487                         }
1488                 }
1489
1490         return 0;
1491 }