update copyright date to 2013
[dpdk.git] / lib / librte_lpm / rte_lpm.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #include <string.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <sys/queue.h>
42
43 #include <rte_log.h>
44 #include <rte_branch_prediction.h>
45 #include <rte_common.h>
46 #include <rte_memory.h>        /* for definition of CACHE_LINE_SIZE */
47 #include <rte_malloc.h>
48 #include <rte_memzone.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_string_fns.h>
54 #include <rte_errno.h>
55 #include <rte_rwlock.h>
56 #include <rte_spinlock.h>
57
58 #include "rte_lpm.h"
59
60 TAILQ_HEAD(rte_lpm_list, rte_lpm);
61  
62 #define MAX_DEPTH_TBL24 24
63
64 enum valid_flag {
65         INVALID = 0,
66         VALID
67 };
68
69 /* Macro to enable/disable run-time checks. */
70 #if defined(RTE_LIBRTE_LPM_DEBUG)
71 #include <rte_debug.h>
72 #define VERIFY_DEPTH(depth) do {                                \
73         if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH))        \
74                 rte_panic("LPM: Invalid depth (%u) at line %d", \
75                                 (unsigned)(depth), __LINE__);   \
76 } while (0)
77 #else
78 #define VERIFY_DEPTH(depth)
79 #endif
80
81 /*
82  * Converts a given depth value to its corresponding mask value.
83  *
84  * depth  (IN)          : range = 1 - 32
85  * mask   (OUT)         : 32bit mask
86  */
87 static uint32_t __attribute__((pure))
88 depth_to_mask(uint8_t depth)
89 {
90         VERIFY_DEPTH(depth);
91
92         /* To calculate a mask start with a 1 on the left hand side and right
93          * shift while populating the left hand side with 1's
94          */
95         return (int)0x80000000 >> (depth - 1);
96 }
97
98 /*
99  * Converts given depth value to its corresponding range value.
100  */
101 static inline uint32_t __attribute__((pure))
102 depth_to_range(uint8_t depth)
103 {
104         VERIFY_DEPTH(depth);
105
106         /*
107          * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
108          */
109         if (depth <= MAX_DEPTH_TBL24)
110                 return 1 << (MAX_DEPTH_TBL24 - depth);
111
112         /* Else if depth is greater than 24 */
113         return (1 << (RTE_LPM_MAX_DEPTH - depth));
114 }
115
116 /*
117  * Find an existing lpm table and return a pointer to it.
118  */
119 struct rte_lpm *
120 rte_lpm_find_existing(const char *name)
121 {
122         struct rte_lpm *l;
123         struct rte_lpm_list *lpm_list;
124
125         /* check that we have an initialised tail queue */
126         if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
127                 rte_errno = E_RTE_NO_TAILQ;
128                 return NULL;
129         }
130
131         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
132         TAILQ_FOREACH(l, lpm_list, next) {
133                 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
134                         break;
135         }
136         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
137
138         if (l == NULL)
139                 rte_errno = ENOENT;
140
141         return l;
142 }
143
144 /*
145  * Allocates memory for LPM object
146  */
147 struct rte_lpm *
148 rte_lpm_create(const char *name, int socket_id, int max_rules,
149                 __rte_unused int flags)
150 {
151         char mem_name[RTE_LPM_NAMESIZE];
152         struct rte_lpm *lpm = NULL;
153         uint32_t mem_size;
154         struct rte_lpm_list *lpm_list;
155
156         /* check that we have an initialised tail queue */
157         if ((lpm_list = 
158              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
159                 rte_errno = E_RTE_NO_TAILQ;
160                 return NULL;    
161         }
162
163         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
164         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
165
166         /* Check user arguments. */
167         if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
168                 rte_errno = EINVAL;
169                 return NULL;
170         }
171
172         rte_snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
173
174         /*
175          * Pad out max_rules so that each depth is given the same number of
176          * rules.
177          */
178         if (max_rules % RTE_LPM_MAX_DEPTH) {
179                 max_rules += RTE_LPM_MAX_DEPTH -
180                                 (max_rules % RTE_LPM_MAX_DEPTH);
181         }
182
183         /* Determine the amount of memory to allocate. */
184         mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
185
186         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
187
188         /* guarantee there's no existing */
189         TAILQ_FOREACH(lpm, lpm_list, next) {
190                 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
191                         break;
192         }
193         if (lpm != NULL)
194                 goto exit;
195
196         /* Allocate memory to store the LPM data structures. */
197         lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
198                         CACHE_LINE_SIZE, socket_id);
199         if (lpm == NULL) {
200                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
201                 goto exit;
202         }
203
204         /* Save user arguments. */
205         lpm->max_rules_per_depth = max_rules / RTE_LPM_MAX_DEPTH;
206         rte_snprintf(lpm->name, sizeof(lpm->name), "%s", name);
207
208         TAILQ_INSERT_TAIL(lpm_list, lpm, next);
209
210 exit:   
211         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
212
213         return lpm;
214 }
215
216 /*
217  * Deallocates memory for given LPM table.
218  */
219 void
220 rte_lpm_free(struct rte_lpm *lpm)
221 {
222         /* Check user arguments. */
223         if (lpm == NULL)
224                 return;
225
226         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_LPM, rte_lpm_list, lpm);
227         rte_free(lpm);
228 }
229
230 /*
231  * Adds a rule to the rule table.
232  *
233  * NOTE: The rule table is split into 32 groups. Each group contains rules that
234  * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
235  * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
236  * to refer to depth 1 because even though the depth range is 1 - 32, depths
237  * are stored in the rule table from 0 - 31.
238  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
239  */
240 static inline int32_t
241 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
242         uint8_t next_hop)
243 {
244         uint32_t rule_gindex, rule_index, last_rule;
245
246         VERIFY_DEPTH(depth);
247
248         /* rule_gindex stands for rule group index. */
249         rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
250         /* Initialise rule_index to point to start of rule group. */
251         rule_index = rule_gindex;
252         /* Last rule = Last used rule in this rule group. */
253         last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
254                 
255         /* Scan through rule group to see if rule already exists. */
256         for (; rule_index < last_rule; rule_index++) {
257
258                 /* If rule already exists update its next_hop and return. */
259                 if (lpm->rules_tbl[rule_index].ip == ip_masked) {
260                         lpm->rules_tbl[rule_index].next_hop = next_hop;
261
262                         return rule_index;
263                 }
264         }
265
266         /*
267          * If rule does not exist check if there is space to add a new rule to
268          * this rule group. If there is no space return error. */
269         if (lpm->used_rules_at_depth[depth - 1] == lpm->max_rules_per_depth) {
270                 return -ENOSPC;
271         }
272
273         /* If there is space for the new rule add it. */
274         lpm->rules_tbl[rule_index].ip = ip_masked;
275         lpm->rules_tbl[rule_index].next_hop = next_hop;
276
277         /* Increment the used rules counter for this rule group. */
278         lpm->used_rules_at_depth[depth - 1]++;
279
280         return rule_index;
281 }
282
283 /*
284  * Delete a rule from the rule table.
285  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
286  */
287 static inline void
288 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
289 {
290         uint32_t rule_gindex, last_rule_index;
291
292         VERIFY_DEPTH(depth);
293
294         rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
295         last_rule_index = rule_gindex +
296                         (lpm->used_rules_at_depth[depth - 1]) - 1;
297         /*
298          * Overwrite redundant rule with last rule in group and decrement rule
299          * counter.
300          */
301         lpm->rules_tbl[rule_index] = lpm->rules_tbl[last_rule_index];
302         lpm->used_rules_at_depth[depth - 1]--;
303 }
304
305
306 /*
307  * Finds a rule in rule table.
308  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
309  */
310 static inline int32_t
311 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
312 {
313         uint32_t rule_gindex, last_rule, rule_index;
314
315         VERIFY_DEPTH(depth);
316
317         rule_gindex = ((depth - 1) * lpm->max_rules_per_depth);
318         last_rule = rule_gindex + lpm->used_rules_at_depth[depth - 1];
319
320         /* Scan used rules at given depth to find rule. */
321         for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
322                 /* If rule is found return the rule index. */
323                 if (lpm->rules_tbl[rule_index].ip == ip_masked)
324                         return (rule_index);
325         }
326
327         /* If rule is not found return -E_RTE_NO_TAILQ. */
328         return -E_RTE_NO_TAILQ;
329 }
330
331 /*
332  * Find, clean and allocate a tbl8.
333  */
334 static inline int32_t
335 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
336 {
337         uint32_t tbl8_gindex; /* tbl8 group index. */
338         struct rte_lpm_tbl8_entry *tbl8_entry;
339
340         /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
341         for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
342                         tbl8_gindex++) {
343                 tbl8_entry = &tbl8[tbl8_gindex *
344                                    RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
345                 /* If a free tbl8 group is found clean it and set as VALID. */
346                 if (!tbl8_entry->valid_group) {
347                         memset(&tbl8_entry[0], 0,
348                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
349                                         sizeof(tbl8_entry[0]));
350
351                         tbl8_entry->valid_group = VALID;
352
353                         /* Return group index for allocated tbl8 group. */
354                         return tbl8_gindex;
355                 }
356         }
357
358         /* If there are no tbl8 groups free then return error. */
359         return -ENOSPC;
360 }
361
362 static inline void
363 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
364 {
365         /* Set tbl8 group invalid*/
366         tbl8[tbl8_group_start].valid_group = INVALID;
367 }
368
369 static inline int32_t
370 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
371                 uint8_t next_hop)
372 {
373         uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
374
375         /* Calculate the index into Table24. */
376         tbl24_index = ip >> 8;
377         tbl24_range = depth_to_range(depth);
378
379         for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
380                 /*
381                  * For invalid OR valid and non-extended tbl 24 entries set
382                  * entry.
383                  */
384                 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
385                                 lpm->tbl24[i].depth <= depth)) {
386
387                         struct rte_lpm_tbl24_entry new_tbl24_entry = {
388                                 { .next_hop = next_hop, },
389                                 .valid = VALID,
390                                 .ext_entry = 0,
391                                 .depth = depth,
392                         };
393
394                         /* Setting tbl24 entry in one go to avoid race
395                          * conditions */
396                         lpm->tbl24[i] = new_tbl24_entry;
397
398                         continue;
399                 }
400
401                 /* If tbl24 entry is valid and extended calculate the index
402                  * into tbl8. */
403                 tbl8_index = lpm->tbl24[tbl24_index].tbl8_gindex * 
404                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
405                 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
406
407                 for (j = tbl8_index; j < tbl8_group_end; j++) {
408                         if (!lpm->tbl8[j].valid ||
409                                         lpm->tbl8[j].depth <= depth) {
410                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
411                                         .valid = VALID,
412                                         .valid_group = VALID,
413                                         .depth = depth,
414                                         .next_hop = next_hop,
415                                 };
416
417                                 /*
418                                  * Setting tbl8 entry in one go to avoid race
419                                  * conditions
420                                  */
421                                 lpm->tbl8[j] = new_tbl8_entry;
422
423                                 continue;
424                         }
425                 }
426         }
427
428         return 0;
429 }
430
431 static inline int32_t
432 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
433                 uint8_t next_hop)
434 {
435         uint32_t tbl24_index;
436         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
437                 tbl8_range, i;
438
439         tbl24_index = (ip_masked >> 8);
440         tbl8_range = depth_to_range(depth);
441
442         if (!lpm->tbl24[tbl24_index].valid) {
443                 /* Search for a free tbl8 group. */
444                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
445
446                 /* Check tbl8 allocation was successful. */
447                 if (tbl8_group_index < 0) {
448                         return tbl8_group_index;
449                 }
450
451                 /* Find index into tbl8 and range. */
452                 tbl8_index = (tbl8_group_index *
453                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
454                                 (ip_masked & 0xFF);
455
456                 /* Set tbl8 entry. */
457                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
458                         lpm->tbl8[i].depth = depth;
459                         lpm->tbl8[i].next_hop = next_hop;
460                         lpm->tbl8[i].valid = VALID;
461                 }
462
463                 /*
464                  * Update tbl24 entry to point to new tbl8 entry. Note: The
465                  * ext_flag and tbl8_index need to be updated simultaneously,
466                  * so assign whole structure in one go
467                  */
468
469                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
470                         { .tbl8_gindex = (uint8_t)tbl8_group_index, },
471                         .valid = VALID,
472                         .ext_entry = 1,
473                         .depth = 0,
474                 };
475
476                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
477
478         }/* If valid entry but not extended calculate the index into Table8. */
479         else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
480                 /* Search for free tbl8 group. */
481                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
482
483                 if (tbl8_group_index < 0) {
484                         return tbl8_group_index;
485                 }
486
487                 tbl8_group_start = tbl8_group_index *
488                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
489                 tbl8_group_end = tbl8_group_start +
490                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
491
492                 /* Populate new tbl8 with tbl24 value. */
493                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
494                         lpm->tbl8[i].valid = VALID;
495                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
496                         lpm->tbl8[i].next_hop =
497                                         lpm->tbl24[tbl24_index].next_hop;
498                 }
499
500                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
501
502                 /* Insert new rule into the tbl8 entry. */
503                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
504                         if (!lpm->tbl8[i].valid ||
505                                         lpm->tbl8[i].depth <= depth) {
506                                 lpm->tbl8[i].valid = VALID;
507                                 lpm->tbl8[i].depth = depth;
508                                 lpm->tbl8[i].next_hop = next_hop;
509
510                                 continue;
511                         }
512                 }
513
514                 /*
515                  * Update tbl24 entry to point to new tbl8 entry. Note: The
516                  * ext_flag and tbl8_index need to be updated simultaneously,
517                  * so assign whole structure in one go.
518                  */
519
520                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
521                                 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
522                                 .valid = VALID,
523                                 .ext_entry = 1,
524                                 .depth = 0,
525                 };
526
527                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
528
529         }
530         else { /*
531                 * If it is valid, extended entry calculate the index into tbl8.
532                 */
533                 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
534                 tbl8_group_start = tbl8_group_index *
535                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
536                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
537
538                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
539
540                         if (!lpm->tbl8[i].valid ||
541                                         lpm->tbl8[i].depth <= depth) {
542                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
543                                         .valid = VALID,
544                                         .depth = depth,
545                                         .next_hop = next_hop,
546                                 };
547
548                                 /*
549                                  * Setting tbl8 entry in one go to avoid race
550                                  * condition
551                                  */
552                                 lpm->tbl8[i] = new_tbl8_entry;
553
554                                 continue;
555                         }
556                 }
557         }
558
559         return 0;
560 }
561
562 /*
563  * Add a route
564  */
565 int
566 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
567                 uint8_t next_hop)
568 {
569         int32_t rule_index, status = 0;
570         uint32_t ip_masked;
571
572         /* Check user arguments. */
573         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
574                 return -EINVAL;
575
576         ip_masked = ip & depth_to_mask(depth);
577
578         /* Add the rule to the rule table. */
579         rule_index = rule_add(lpm, ip_masked, depth, next_hop);
580
581         /* If the is no space available for new rule return error. */
582         if (rule_index < 0) {
583                 return rule_index;
584         }
585
586         if (depth <= MAX_DEPTH_TBL24) {
587                 status = add_depth_small(lpm, ip_masked, depth, next_hop);
588         }
589         else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
590                 status = add_depth_big(lpm, ip_masked, depth, next_hop);
591
592                 /*
593                  * If add fails due to exhaustion of tbl8 extensions delete
594                  * rule that was added to rule table.
595                  */
596                 if (status < 0) {
597                         rule_delete(lpm, rule_index, depth);
598
599                         return status;
600                 }
601         }
602
603         return 0;
604 }
605
606 static inline int32_t
607 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
608 {
609         int32_t rule_index;
610         uint32_t ip_masked;
611         uint8_t prev_depth;
612
613         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
614                 ip_masked = ip & depth_to_mask(prev_depth);
615
616                 rule_index = rule_find(lpm, ip_masked, prev_depth);
617
618                 if (rule_index >= 0)
619                         return rule_index;
620         }
621
622         return -1;
623 }
624
625 static inline int32_t
626 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
627         uint8_t depth, int32_t sub_rule_index)
628 {
629         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
630         uint8_t new_depth;
631
632         /* Calculate the range and index into Table24. */
633         tbl24_range = depth_to_range(depth);
634         tbl24_index = (ip_masked >> 8);
635
636         /*
637          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
638          * and a positive number indicates a sub_rule_index.
639          */
640         if (sub_rule_index < 0) {
641                 /*
642                  * If no replacement rule exists then invalidate entries
643                  * associated with this rule.
644                  */
645                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
646                         
647                         if (lpm->tbl24[i].ext_entry == 0 &&
648                                         lpm->tbl24[i].depth <= depth ) {
649                                 lpm->tbl24[i].valid = INVALID;
650                         }
651                         else {
652                                 /*
653                                  * If TBL24 entry is extended, then there has
654                                  * to be a rule with depth >= 25 in the
655                                  * associated TBL8 group.
656                                  */
657
658                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
659                                 tbl8_index = tbl8_group_index *
660                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
661
662                                 for (j = tbl8_index; j < (tbl8_index +
663                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
664
665                                         if (lpm->tbl8[j].depth <= depth)
666                                                 lpm->tbl8[j].valid = INVALID;
667                                 }
668                         }
669                 }
670         }
671         else {
672                 /*
673                  * If a replacement rule exists then modify entries
674                  * associated with this rule.
675                  */
676
677                 /* Calculate depth of sub_rule. */
678                 new_depth = (uint8_t) (sub_rule_index /
679                                 lpm->max_rules_per_depth);
680
681                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
682                         {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
683                         .valid = VALID,
684                         .ext_entry = 0,
685                         .depth = new_depth,
686                 };
687
688                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
689                         .valid = VALID,
690                         .depth = new_depth,
691                         .next_hop = lpm->rules_tbl
692                         [sub_rule_index].next_hop,
693                 };
694
695                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
696
697                         if (lpm->tbl24[i].ext_entry == 0 &&
698                                         lpm->tbl24[i].depth <= depth ) {
699                                 lpm->tbl24[i] = new_tbl24_entry;
700                         }
701                         else {
702                                 /*
703                                  * If TBL24 entry is extended, then there has
704                                  * to be a rule with depth >= 25 in the
705                                  * associated TBL8 group.
706                                  */
707
708                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
709                                 tbl8_index = tbl8_group_index *
710                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
711                                 
712                                 for (j = tbl8_index; j < (tbl8_index +
713                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
714
715                                         if (lpm->tbl8[j].depth <= depth)
716                                                 lpm->tbl8[j] = new_tbl8_entry;
717                                 }
718                         }
719                 }
720         }
721
722         return 0;
723 }
724
725 /*
726  * Checks if table 8 group can be recycled.
727  *
728  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
729  * Return of -EINVAL means tbl8 is empty and thus can be recycled
730  * Return of value > -1 means tbl8 is in use but has all the same values and
731  * thus can be recycled
732  */
733 static inline int32_t
734 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
735 {
736         uint32_t tbl8_group_end, i;
737         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
738
739         /*
740          * Check the first entry of the given tbl8. If it is invalid we know
741          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
742          *  (As they would affect all entries in a tbl8) and thus this table
743          *  can not be recycled.
744          */
745         if (tbl8[tbl8_group_start].valid) {
746                 /*
747                  * If first entry is valid check if the depth is less than 24
748                  * and if so check the rest of the entries to verify that they
749                  * are all of this depth.
750                  */
751                 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
752                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
753                                         i++) {
754
755                                 if (tbl8[i].depth !=
756                                                 tbl8[tbl8_group_start].depth) {
757
758                                         return -EEXIST;
759                                 }
760                         }
761                         /* If all entries are the same return the tb8 index */
762                         return tbl8_group_start;
763                 }
764
765                 return -EEXIST;
766         }
767         /*
768          * If the first entry is invalid check if the rest of the entries in
769          * the tbl8 are invalid.
770          */
771         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
772                 if (tbl8[i].valid)
773                         return -EEXIST;
774         }
775         /* If no valid entries are found then return -EINVAL. */
776         return -EINVAL;
777 }
778
779 static inline int32_t
780 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
781         uint8_t depth, int32_t sub_rule_index)
782 {
783         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
784                         tbl8_range, i;
785         uint8_t new_depth;
786         int32_t tbl8_recycle_index;
787
788         /*
789          * Calculate the index into tbl24 and range. Note: All depths larger
790          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
791          */
792         tbl24_index = ip_masked >> 8;
793
794         /* Calculate the index into tbl8 and range. */
795         tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
796         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
797         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
798         tbl8_range = depth_to_range(depth);
799
800         if (sub_rule_index < 0) {
801                 /*
802                  * Loop through the range of entries on tbl8 for which the
803                  * rule_to_delete must be removed or modified.
804                  */
805                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
806                         if (lpm->tbl8[i].depth <= depth)
807                                 lpm->tbl8[i].valid = INVALID;
808                 }
809         }
810         else {
811                 new_depth = (uint8_t)(sub_rule_index /
812                                 lpm->max_rules_per_depth);
813
814                 /* Set new tbl8 entry. */
815                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
816                         .valid = VALID,
817                         .depth = new_depth,
818                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
819                 };
820
821                 /*
822                  * Loop through the range of entries on tbl8 for which the
823                  * rule_to_delete must be modified.
824                  */
825                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
826                         if (lpm->tbl8[i].depth <= depth)
827                                 lpm->tbl8[i] = new_tbl8_entry;
828                 }
829         }
830
831         /*
832          * Check if there are any valid entries in this tbl8 group. If all
833          * tbl8 entries are invalid we can free the tbl8 and invalidate the
834          * associated tbl24 entry.
835          */
836
837         tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
838
839         if (tbl8_recycle_index == -EINVAL){
840                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
841                 lpm->tbl24[tbl24_index].valid = 0;
842                 tbl8_free(lpm->tbl8, tbl8_group_start);
843         }
844         else if (tbl8_recycle_index > -1) {
845                 /* Update tbl24 entry. */
846                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
847                         { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
848                         .valid = VALID,
849                         .ext_entry = 0,
850                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
851                 };
852
853                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
854                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
855                 tbl8_free(lpm->tbl8, tbl8_group_start);
856         }
857
858         return 0;
859 }
860
861 /*
862  * Deletes a rule
863  */
864 int
865 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
866 {
867         int32_t rule_to_delete_index, sub_rule_index;
868         uint32_t ip_masked;
869         /*
870          * Check input arguments. Note: IP must be a positive integer of 32
871          * bits in length therefore it need not be checked.
872          */
873         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
874                 return -EINVAL;
875         }
876
877         ip_masked = ip & depth_to_mask(depth);
878
879         /*
880          * Find the index of the input rule, that needs to be deleted, in the
881          * rule table.
882          */
883         rule_to_delete_index = rule_find(lpm, ip_masked, depth);
884
885         /*
886          * Check if rule_to_delete_index was found. If no rule was found the
887          * function rule_find returns -E_RTE_NO_TAILQ.
888          */
889         if (rule_to_delete_index < 0)
890                 return -E_RTE_NO_TAILQ;
891
892         /* Delete the rule from the rule table. */
893         rule_delete(lpm, rule_to_delete_index, depth);
894
895         /*
896          * Find rule to replace the rule_to_delete. If there is no rule to
897          * replace the rule_to_delete we return -1 and invalidate the table
898          * entries associated with this rule.
899          */
900         sub_rule_index = find_previous_rule(lpm, ip, depth);
901
902         /*
903          * If the input depth value is less than 25 use function
904          * delete_depth_small otherwise use delete_depth_big.
905          */
906         if (depth <= MAX_DEPTH_TBL24) {
907                 return delete_depth_small(lpm, ip_masked, depth,
908                                 sub_rule_index);
909         }
910         else { /* If depth > MAX_DEPTH_TBL24 */
911                 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index);
912         }
913 }
914
915 /*
916  * Delete all rules from the LPM table.
917  */
918 void
919 rte_lpm_delete_all(struct rte_lpm *lpm)
920 {
921         /* Zero used rules counter. */
922         memset(lpm->used_rules_at_depth, 0, sizeof(lpm->used_rules_at_depth));
923
924         /* Zero tbl24. */
925         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
926
927         /* Zero tbl8. */
928         memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
929
930         /* Delete all rules form the rules table. */
931         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) *
932                         (lpm->max_rules_per_depth * RTE_LPM_MAX_DEPTH));
933 }
934