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