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