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