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