c46e557e2389ed9ac3edced35c120b0f39798f85
[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  * Simulate adding a route to LPM
814  *
815  *      Returns:
816  *    0 on success
817  *    -ENOSPC not enought tbl8 left
818  */
819 static int
820 simulate_add(struct rte_lpm6 *lpm, const uint8_t *masked_ip, uint8_t depth)
821 {
822         struct rte_lpm6_tbl_entry *tbl;
823         struct rte_lpm6_tbl_entry *tbl_next = NULL;
824         int ret, i;
825
826         /* number of new tables needed for a step */
827         uint32_t need_tbl_nb;
828         /* total number of new tables needed */
829         uint32_t total_need_tbl_nb;
830
831         /* Inspect the first three bytes through tbl24 on the first step. */
832         ret = simulate_add_step(lpm, lpm->tbl24, &tbl_next, masked_ip,
833                 ADD_FIRST_BYTE, 1, depth, &need_tbl_nb);
834         total_need_tbl_nb = need_tbl_nb;
835         /*
836          * Inspect one by one the rest of the bytes until
837          * the process is completed.
838          */
839         for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && ret == 1; i++) {
840                 tbl = tbl_next;
841                 ret = simulate_add_step(lpm, tbl, &tbl_next, masked_ip, 1,
842                         (uint8_t)(i + 1), depth, &need_tbl_nb);
843                 total_need_tbl_nb += need_tbl_nb;
844         }
845
846         if (tbl8_available(lpm) < total_need_tbl_nb)
847                 /* not enought tbl8 to add a rule */
848                 return -ENOSPC;
849
850         return 0;
851 }
852
853 /*
854  * Add a route
855  */
856 int
857 rte_lpm6_add(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
858         uint32_t next_hop)
859 {
860         struct rte_lpm6_tbl_entry *tbl;
861         struct rte_lpm6_tbl_entry *tbl_next = NULL;
862         /* init to avoid compiler warning */
863         uint32_t tbl_next_num = 123456;
864         int status;
865         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
866         int i;
867
868         /* Check user arguments. */
869         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
870                 return -EINVAL;
871
872         /* Copy the IP and mask it to avoid modifying user's input data. */
873         ip6_copy_addr(masked_ip, ip);
874         ip6_mask_addr(masked_ip, depth);
875
876         /* Simulate adding a new route */
877         int ret = simulate_add(lpm, masked_ip, depth);
878         if (ret < 0)
879                 return ret;
880
881         /* Add the rule to the rule table. */
882         int is_new_rule = rule_add(lpm, masked_ip, depth, next_hop);
883         /* If there is no space available for new rule return error. */
884         if (is_new_rule < 0)
885                 return is_new_rule;
886
887         /* Inspect the first three bytes through tbl24 on the first step. */
888         tbl = lpm->tbl24;
889         status = add_step(lpm, tbl, TBL24_IND, &tbl_next, &tbl_next_num,
890                 masked_ip, ADD_FIRST_BYTE, 1, depth, next_hop,
891                 is_new_rule);
892         assert(status >= 0);
893
894         /*
895          * Inspect one by one the rest of the bytes until
896          * the process is completed.
897          */
898         for (i = ADD_FIRST_BYTE; i < RTE_LPM6_IPV6_ADDR_SIZE && status == 1; i++) {
899                 tbl = tbl_next;
900                 status = add_step(lpm, tbl, tbl_next_num, &tbl_next,
901                         &tbl_next_num, masked_ip, 1, (uint8_t)(i + 1),
902                         depth, next_hop, is_new_rule);
903                 assert(status >= 0);
904         }
905
906         return status;
907 }
908
909 /*
910  * Takes a pointer to a table entry and inspect one level.
911  * The function returns 0 on lookup success, ENOENT if no match was found
912  * or 1 if the process needs to be continued by calling the function again.
913  */
914 static inline int
915 lookup_step(const struct rte_lpm6 *lpm, const struct rte_lpm6_tbl_entry *tbl,
916                 const struct rte_lpm6_tbl_entry **tbl_next, uint8_t *ip,
917                 uint8_t first_byte, uint32_t *next_hop)
918 {
919         uint32_t tbl8_index, tbl_entry;
920
921         /* Take the integer value from the pointer. */
922         tbl_entry = *(const uint32_t *)tbl;
923
924         /* If it is valid and extended we calculate the new pointer to return. */
925         if ((tbl_entry & RTE_LPM6_VALID_EXT_ENTRY_BITMASK) ==
926                         RTE_LPM6_VALID_EXT_ENTRY_BITMASK) {
927
928                 tbl8_index = ip[first_byte-1] +
929                                 ((tbl_entry & RTE_LPM6_TBL8_BITMASK) *
930                                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES);
931
932                 *tbl_next = &lpm->tbl8[tbl8_index];
933
934                 return 1;
935         } else {
936                 /* If not extended then we can have a match. */
937                 *next_hop = ((uint32_t)tbl_entry & RTE_LPM6_TBL8_BITMASK);
938                 return (tbl_entry & RTE_LPM6_LOOKUP_SUCCESS) ? 0 : -ENOENT;
939         }
940 }
941
942 /*
943  * Looks up an IP
944  */
945 int
946 rte_lpm6_lookup(const struct rte_lpm6 *lpm, uint8_t *ip,
947                 uint32_t *next_hop)
948 {
949         const struct rte_lpm6_tbl_entry *tbl;
950         const struct rte_lpm6_tbl_entry *tbl_next = NULL;
951         int status;
952         uint8_t first_byte;
953         uint32_t tbl24_index;
954
955         /* DEBUG: Check user input arguments. */
956         if ((lpm == NULL) || (ip == NULL) || (next_hop == NULL))
957                 return -EINVAL;
958
959         first_byte = LOOKUP_FIRST_BYTE;
960         tbl24_index = (ip[0] << BYTES2_SIZE) | (ip[1] << BYTE_SIZE) | ip[2];
961
962         /* Calculate pointer to the first entry to be inspected */
963         tbl = &lpm->tbl24[tbl24_index];
964
965         do {
966                 /* Continue inspecting following levels until success or failure */
967                 status = lookup_step(lpm, tbl, &tbl_next, ip, first_byte++, next_hop);
968                 tbl = tbl_next;
969         } while (status == 1);
970
971         return status;
972 }
973
974 /*
975  * Looks up a group of IP addresses
976  */
977 int
978 rte_lpm6_lookup_bulk_func(const struct rte_lpm6 *lpm,
979                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE],
980                 int32_t *next_hops, unsigned int n)
981 {
982         unsigned int i;
983         const struct rte_lpm6_tbl_entry *tbl;
984         const struct rte_lpm6_tbl_entry *tbl_next = NULL;
985         uint32_t tbl24_index, next_hop;
986         uint8_t first_byte;
987         int status;
988
989         /* DEBUG: Check user input arguments. */
990         if ((lpm == NULL) || (ips == NULL) || (next_hops == NULL))
991                 return -EINVAL;
992
993         for (i = 0; i < n; i++) {
994                 first_byte = LOOKUP_FIRST_BYTE;
995                 tbl24_index = (ips[i][0] << BYTES2_SIZE) |
996                                 (ips[i][1] << BYTE_SIZE) | ips[i][2];
997
998                 /* Calculate pointer to the first entry to be inspected */
999                 tbl = &lpm->tbl24[tbl24_index];
1000
1001                 do {
1002                         /* Continue inspecting following levels
1003                          * until success or failure
1004                          */
1005                         status = lookup_step(lpm, tbl, &tbl_next, ips[i],
1006                                         first_byte++, &next_hop);
1007                         tbl = tbl_next;
1008                 } while (status == 1);
1009
1010                 if (status < 0)
1011                         next_hops[i] = -1;
1012                 else
1013                         next_hops[i] = (int32_t)next_hop;
1014         }
1015
1016         return 0;
1017 }
1018
1019 /*
1020  * Look for a rule in the high-level rules table
1021  */
1022 int
1023 rte_lpm6_is_rule_present(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1024                 uint32_t *next_hop)
1025 {
1026         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1027
1028         /* Check user arguments. */
1029         if ((lpm == NULL) || next_hop == NULL || ip == NULL ||
1030                         (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1031                 return -EINVAL;
1032
1033         /* Copy the IP and mask it to avoid modifying user's input data. */
1034         ip6_copy_addr(masked_ip, ip);
1035         ip6_mask_addr(masked_ip, depth);
1036
1037         return rule_find(lpm, masked_ip, depth, next_hop);
1038 }
1039
1040 /*
1041  * Delete a rule from the rule table.
1042  * NOTE: Valid range for depth parameter is 1 .. 128 inclusive.
1043  * return
1044  *        0 on success
1045  *   <0 on failure
1046  */
1047 static inline int
1048 rule_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1049 {
1050         int ret;
1051         struct rte_lpm6_rule_key rule_key;
1052
1053         /* init rule key */
1054         rule_key_init(&rule_key, ip, depth);
1055
1056         /* delete the rule */
1057         ret = rte_hash_del_key(lpm->rules_tbl, (void *) &rule_key);
1058         if (ret >= 0)
1059                 lpm->used_rules--;
1060
1061         return ret;
1062 }
1063
1064 /*
1065  * Deletes a group of rules
1066  *
1067  * Note that the function rebuilds the lpm table,
1068  * rather than doing incremental updates like
1069  * the regular delete function
1070  */
1071 int
1072 rte_lpm6_delete_bulk_func(struct rte_lpm6 *lpm,
1073                 uint8_t ips[][RTE_LPM6_IPV6_ADDR_SIZE], uint8_t *depths,
1074                 unsigned n)
1075 {
1076         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1077         unsigned i;
1078
1079         /* Check input arguments. */
1080         if ((lpm == NULL) || (ips == NULL) || (depths == NULL))
1081                 return -EINVAL;
1082
1083         for (i = 0; i < n; i++) {
1084                 ip6_copy_addr(masked_ip, ips[i]);
1085                 ip6_mask_addr(masked_ip, depths[i]);
1086                 rule_delete(lpm, masked_ip, depths[i]);
1087         }
1088
1089         /*
1090          * Set all the table entries to 0 (ie delete every rule
1091          * from the data structure.
1092          */
1093         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1094         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1095                         * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1096         tbl8_pool_init(lpm);
1097
1098         /*
1099          * Add every rule again (except for the ones that were removed from
1100          * the rules table).
1101          */
1102         rebuild_lpm(lpm);
1103
1104         return 0;
1105 }
1106
1107 /*
1108  * Delete all rules from the LPM table.
1109  */
1110 void
1111 rte_lpm6_delete_all(struct rte_lpm6 *lpm)
1112 {
1113         /* Zero used rules counter. */
1114         lpm->used_rules = 0;
1115
1116         /* Zero tbl24. */
1117         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1118
1119         /* Zero tbl8. */
1120         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0]) *
1121                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1122
1123         /* init pool of free tbl8 indexes */
1124         tbl8_pool_init(lpm);
1125
1126         /* Delete all rules form the rules table. */
1127         rte_hash_reset(lpm->rules_tbl);
1128 }
1129
1130 /*
1131  * Convert a depth to a one byte long mask
1132  *   Example: 4 will be converted to 0xF0
1133  */
1134 static uint8_t __attribute__((pure))
1135 depth_to_mask_1b(uint8_t depth)
1136 {
1137         /* To calculate a mask start with a 1 on the left hand side and right
1138          * shift while populating the left hand side with 1's
1139          */
1140         return (signed char)0x80 >> (depth - 1);
1141 }
1142
1143 /*
1144  * Find a less specific rule
1145  */
1146 static int
1147 rule_find_less_specific(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth,
1148         struct rte_lpm6_rule *rule)
1149 {
1150         int ret;
1151         uint32_t next_hop;
1152         uint8_t mask;
1153         struct rte_lpm6_rule_key rule_key;
1154
1155         if (depth == 1)
1156                 return 0;
1157
1158         rule_key_init(&rule_key, ip, depth);
1159
1160         while (depth > 1) {
1161                 depth--;
1162
1163                 /* each iteration zero one more bit of the key */
1164                 mask = depth & 7; /* depth % BYTE_SIZE */
1165                 if (mask > 0)
1166                         mask = depth_to_mask_1b(mask);
1167
1168                 rule_key.depth = depth;
1169                 rule_key.ip[depth >> 3] &= mask;
1170
1171                 ret = rule_find_with_key(lpm, &rule_key, &next_hop);
1172                 if (ret) {
1173                         rule->depth = depth;
1174                         ip6_copy_addr(rule->ip, rule_key.ip);
1175                         rule->next_hop = next_hop;
1176                         return 1;
1177                 }
1178         }
1179
1180         return 0;
1181 }
1182
1183 /*
1184  * Find range of tbl8 cells occupied by a rule
1185  */
1186 static void
1187 rule_find_range(struct rte_lpm6 *lpm, const uint8_t *ip, uint8_t depth,
1188                   struct rte_lpm6_tbl_entry **from,
1189                   struct rte_lpm6_tbl_entry **to,
1190                   uint32_t *out_tbl_ind)
1191 {
1192         uint32_t ind;
1193         uint32_t first_3bytes = (uint32_t)ip[0] << 16 | ip[1] << 8 | ip[2];
1194
1195         if (depth <= 24) {
1196                 /* rule is within the top level */
1197                 ind = first_3bytes;
1198                 *from = &lpm->tbl24[ind];
1199                 ind += (1 << (24 - depth)) - 1;
1200                 *to = &lpm->tbl24[ind];
1201                 *out_tbl_ind = TBL24_IND;
1202         } else {
1203                 /* top level entry */
1204                 struct rte_lpm6_tbl_entry *tbl = &lpm->tbl24[first_3bytes];
1205                 assert(tbl->ext_entry == 1);
1206                 /* first tbl8 */
1207                 uint32_t tbl_ind = tbl->lpm6_tbl8_gindex;
1208                 tbl = &lpm->tbl8[tbl_ind *
1209                                 RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1210                 /* current ip byte, the top level is already behind */
1211                 uint8_t byte = 3;
1212                 /* minus top level */
1213                 depth -= 24;
1214
1215                 /* interate through levels (tbl8s)
1216                  * until we reach the last one
1217                  */
1218                 while (depth > 8) {
1219                         tbl += ip[byte];
1220                         assert(tbl->ext_entry == 1);
1221                         /* go to the next level/tbl8 */
1222                         tbl_ind = tbl->lpm6_tbl8_gindex;
1223                         tbl = &lpm->tbl8[tbl_ind *
1224                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES];
1225                         byte += 1;
1226                         depth -= 8;
1227                 }
1228
1229                 /* last level/tbl8 */
1230                 ind = ip[byte] & depth_to_mask_1b(depth);
1231                 *from = &tbl[ind];
1232                 ind += (1 << (8 - depth)) - 1;
1233                 *to = &tbl[ind];
1234                 *out_tbl_ind = tbl_ind;
1235         }
1236 }
1237
1238 /*
1239  * Remove a table from the LPM tree
1240  */
1241 static void
1242 remove_tbl(struct rte_lpm6 *lpm, struct rte_lpm_tbl8_hdr *tbl_hdr,
1243                   uint32_t tbl_ind, struct rte_lpm6_rule *lsp_rule)
1244 {
1245         struct rte_lpm6_tbl_entry *owner_entry;
1246
1247         if (tbl_hdr->owner_tbl_ind == TBL24_IND)
1248                 owner_entry = &lpm->tbl24[tbl_hdr->owner_entry_ind];
1249         else {
1250                 uint32_t owner_tbl_ind = tbl_hdr->owner_tbl_ind;
1251                 owner_entry = &lpm->tbl8[
1252                         owner_tbl_ind * RTE_LPM6_TBL8_GROUP_NUM_ENTRIES +
1253                         tbl_hdr->owner_entry_ind];
1254
1255                 struct rte_lpm_tbl8_hdr *owner_tbl_hdr =
1256                         &lpm->tbl8_hdrs[owner_tbl_ind];
1257                 if (--owner_tbl_hdr->ref_cnt == 0)
1258                         remove_tbl(lpm, owner_tbl_hdr, owner_tbl_ind, lsp_rule);
1259         }
1260
1261         assert(owner_entry->ext_entry == 1);
1262
1263         /* unlink the table */
1264         if (lsp_rule != NULL) {
1265                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1266                         .next_hop = lsp_rule->next_hop,
1267                         .depth = lsp_rule->depth,
1268                         .valid = VALID,
1269                         .valid_group = VALID,
1270                         .ext_entry = 0
1271                 };
1272
1273                 *owner_entry = new_tbl_entry;
1274         } else {
1275                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1276                         .next_hop = 0,
1277                         .depth = 0,
1278                         .valid = INVALID,
1279                         .valid_group = INVALID,
1280                         .ext_entry = 0
1281                 };
1282
1283                 *owner_entry = new_tbl_entry;
1284         }
1285
1286         /* return the table to the pool */
1287         tbl8_put(lpm, tbl_ind);
1288 }
1289
1290 /*
1291  * Deletes a rule
1292  */
1293 int
1294 rte_lpm6_delete(struct rte_lpm6 *lpm, uint8_t *ip, uint8_t depth)
1295 {
1296         uint8_t masked_ip[RTE_LPM6_IPV6_ADDR_SIZE];
1297         struct rte_lpm6_rule lsp_rule_obj;
1298         struct rte_lpm6_rule *lsp_rule;
1299         int ret;
1300         uint32_t tbl_ind;
1301         struct rte_lpm6_tbl_entry *from, *to;
1302
1303         /* Check input arguments. */
1304         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM6_MAX_DEPTH))
1305                 return -EINVAL;
1306
1307         /* Copy the IP and mask it to avoid modifying user's input data. */
1308         ip6_copy_addr(masked_ip, ip);
1309         ip6_mask_addr(masked_ip, depth);
1310
1311         /* Delete the rule from the rule table. */
1312         ret = rule_delete(lpm, masked_ip, depth);
1313         if (ret < 0)
1314                 return -ENOENT;
1315
1316         /* find rule cells */
1317         rule_find_range(lpm, masked_ip, depth, &from, &to, &tbl_ind);
1318
1319         /* find a less specific rule (a rule with smaller depth)
1320          * note: masked_ip will be modified, don't use it anymore
1321          */
1322         ret = rule_find_less_specific(lpm, masked_ip, depth,
1323                         &lsp_rule_obj);
1324         lsp_rule = ret ? &lsp_rule_obj : NULL;
1325
1326         /* decrement the table rule counter,
1327          * note that tbl24 doesn't have a header
1328          */
1329         if (tbl_ind != TBL24_IND) {
1330                 struct rte_lpm_tbl8_hdr *tbl_hdr = &lpm->tbl8_hdrs[tbl_ind];
1331                 if (--tbl_hdr->ref_cnt == 0) {
1332                         /* remove the table */
1333                         remove_tbl(lpm, tbl_hdr, tbl_ind, lsp_rule);
1334                         return 0;
1335                 }
1336         }
1337
1338         /* iterate rule cells */
1339         for (; from <= to; from++)
1340                 if (from->ext_entry == 1) {
1341                         /* reference to a more specific space
1342                          * of the prefix/rule. Entries in a more
1343                          * specific space that are not used by
1344                          * a more specific prefix must be occupied
1345                          * by the prefix
1346                          */
1347                         if (lsp_rule != NULL)
1348                                 expand_rule(lpm,
1349                                         from->lpm6_tbl8_gindex *
1350                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1351                                         depth, lsp_rule->depth,
1352                                         lsp_rule->next_hop, VALID);
1353                         else
1354                                 /* since the prefix has no less specific prefix,
1355                                  * its more specific space must be invalidated
1356                                  */
1357                                 expand_rule(lpm,
1358                                         from->lpm6_tbl8_gindex *
1359                                         RTE_LPM6_TBL8_GROUP_NUM_ENTRIES,
1360                                         depth, 0, 0, INVALID);
1361                 } else if (from->depth == depth) {
1362                         /* entry is not a reference and belongs to the prefix */
1363                         if (lsp_rule != NULL) {
1364                                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1365                                         .next_hop = lsp_rule->next_hop,
1366                                         .depth = lsp_rule->depth,
1367                                         .valid = VALID,
1368                                         .valid_group = VALID,
1369                                         .ext_entry = 0
1370                                 };
1371
1372                                 *from = new_tbl_entry;
1373                         } else {
1374                                 struct rte_lpm6_tbl_entry new_tbl_entry = {
1375                                         .next_hop = 0,
1376                                         .depth = 0,
1377                                         .valid = INVALID,
1378                                         .valid_group = INVALID,
1379                                         .ext_entry = 0
1380                                 };
1381
1382                                 *from = new_tbl_entry;
1383                         }
1384                 }
1385
1386         return 0;
1387 }