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