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