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