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