tailq: move to dynamic tailq
[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                         lpm->tbl24[i] = new_tbl24_entry;
447
448                         continue;
449                 }
450
451                 /* If tbl24 entry is valid and extended calculate the index
452                  * into tbl8. */
453                 tbl8_index = lpm->tbl24[i].tbl8_gindex *
454                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
455                 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
456
457                 for (j = tbl8_index; j < tbl8_group_end; j++) {
458                         if (!lpm->tbl8[j].valid ||
459                                         lpm->tbl8[j].depth <= depth) {
460                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
461                                         .valid = VALID,
462                                         .valid_group = VALID,
463                                         .depth = depth,
464                                         .next_hop = next_hop,
465                                 };
466
467                                 /*
468                                  * Setting tbl8 entry in one go to avoid race
469                                  * conditions
470                                  */
471                                 lpm->tbl8[j] = new_tbl8_entry;
472
473                                 continue;
474                         }
475                 }
476         }
477
478         return 0;
479 }
480
481 static inline int32_t
482 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
483                 uint8_t next_hop)
484 {
485         uint32_t tbl24_index;
486         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
487                 tbl8_range, i;
488
489         tbl24_index = (ip_masked >> 8);
490         tbl8_range = depth_to_range(depth);
491
492         if (!lpm->tbl24[tbl24_index].valid) {
493                 /* Search for a free tbl8 group. */
494                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
495
496                 /* Check tbl8 allocation was successful. */
497                 if (tbl8_group_index < 0) {
498                         return tbl8_group_index;
499                 }
500
501                 /* Find index into tbl8 and range. */
502                 tbl8_index = (tbl8_group_index *
503                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
504                                 (ip_masked & 0xFF);
505
506                 /* Set tbl8 entry. */
507                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
508                         lpm->tbl8[i].depth = depth;
509                         lpm->tbl8[i].next_hop = next_hop;
510                         lpm->tbl8[i].valid = VALID;
511                 }
512
513                 /*
514                  * Update tbl24 entry to point to new tbl8 entry. Note: The
515                  * ext_flag and tbl8_index need to be updated simultaneously,
516                  * so assign whole structure in one go
517                  */
518
519                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
520                         { .tbl8_gindex = (uint8_t)tbl8_group_index, },
521                         .valid = VALID,
522                         .ext_entry = 1,
523                         .depth = 0,
524                 };
525
526                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
527
528         }/* If valid entry but not extended calculate the index into Table8. */
529         else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
530                 /* Search for free tbl8 group. */
531                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
532
533                 if (tbl8_group_index < 0) {
534                         return tbl8_group_index;
535                 }
536
537                 tbl8_group_start = tbl8_group_index *
538                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
539                 tbl8_group_end = tbl8_group_start +
540                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
541
542                 /* Populate new tbl8 with tbl24 value. */
543                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
544                         lpm->tbl8[i].valid = VALID;
545                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
546                         lpm->tbl8[i].next_hop =
547                                         lpm->tbl24[tbl24_index].next_hop;
548                 }
549
550                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
551
552                 /* Insert new rule into the tbl8 entry. */
553                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
554                         if (!lpm->tbl8[i].valid ||
555                                         lpm->tbl8[i].depth <= depth) {
556                                 lpm->tbl8[i].valid = VALID;
557                                 lpm->tbl8[i].depth = depth;
558                                 lpm->tbl8[i].next_hop = next_hop;
559
560                                 continue;
561                         }
562                 }
563
564                 /*
565                  * Update tbl24 entry to point to new tbl8 entry. Note: The
566                  * ext_flag and tbl8_index need to be updated simultaneously,
567                  * so assign whole structure in one go.
568                  */
569
570                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
571                                 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
572                                 .valid = VALID,
573                                 .ext_entry = 1,
574                                 .depth = 0,
575                 };
576
577                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
578
579         }
580         else { /*
581                 * If it is valid, extended entry calculate the index into tbl8.
582                 */
583                 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
584                 tbl8_group_start = tbl8_group_index *
585                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
586                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
587
588                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
589
590                         if (!lpm->tbl8[i].valid ||
591                                         lpm->tbl8[i].depth <= depth) {
592                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
593                                         .valid = VALID,
594                                         .depth = depth,
595                                         .next_hop = next_hop,
596                                         .valid_group = lpm->tbl8[i].valid_group,
597                                 };
598
599                                 /*
600                                  * Setting tbl8 entry in one go to avoid race
601                                  * condition
602                                  */
603                                 lpm->tbl8[i] = new_tbl8_entry;
604
605                                 continue;
606                         }
607                 }
608         }
609
610         return 0;
611 }
612
613 /*
614  * Add a route
615  */
616 int
617 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
618                 uint8_t next_hop)
619 {
620         int32_t rule_index, status = 0;
621         uint32_t ip_masked;
622
623         /* Check user arguments. */
624         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
625                 return -EINVAL;
626
627         ip_masked = ip & depth_to_mask(depth);
628
629         /* Add the rule to the rule table. */
630         rule_index = rule_add(lpm, ip_masked, depth, next_hop);
631
632         /* If the is no space available for new rule return error. */
633         if (rule_index < 0) {
634                 return rule_index;
635         }
636
637         if (depth <= MAX_DEPTH_TBL24) {
638                 status = add_depth_small(lpm, ip_masked, depth, next_hop);
639         }
640         else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
641                 status = add_depth_big(lpm, ip_masked, depth, next_hop);
642
643                 /*
644                  * If add fails due to exhaustion of tbl8 extensions delete
645                  * rule that was added to rule table.
646                  */
647                 if (status < 0) {
648                         rule_delete(lpm, rule_index, depth);
649
650                         return status;
651                 }
652         }
653
654         return 0;
655 }
656
657 /*
658  * Look for a rule in the high-level rules table
659  */
660 int
661 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
662 uint8_t *next_hop)
663 {
664         uint32_t ip_masked;
665         int32_t rule_index;
666
667         /* Check user arguments. */
668         if ((lpm == NULL) ||
669                 (next_hop == NULL) ||
670                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
671                 return -EINVAL;
672
673         /* Look for the rule using rule_find. */
674         ip_masked = ip & depth_to_mask(depth);
675         rule_index = rule_find(lpm, ip_masked, depth);
676
677         if (rule_index >= 0) {
678                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
679                 return 1;
680         }
681
682         /* If rule is not found return 0. */
683         return 0;
684 }
685
686 static inline int32_t
687 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
688 {
689         int32_t rule_index;
690         uint32_t ip_masked;
691         uint8_t prev_depth;
692
693         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
694                 ip_masked = ip & depth_to_mask(prev_depth);
695
696                 rule_index = rule_find(lpm, ip_masked, prev_depth);
697
698                 if (rule_index >= 0) {
699                         *sub_rule_depth = prev_depth;
700                         return rule_index;
701                 }
702         }
703
704         return -1;
705 }
706
707 static inline int32_t
708 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
709         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
710 {
711         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
712
713         /* Calculate the range and index into Table24. */
714         tbl24_range = depth_to_range(depth);
715         tbl24_index = (ip_masked >> 8);
716
717         /*
718          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
719          * and a positive number indicates a sub_rule_index.
720          */
721         if (sub_rule_index < 0) {
722                 /*
723                  * If no replacement rule exists then invalidate entries
724                  * associated with this rule.
725                  */
726                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
727
728                         if (lpm->tbl24[i].ext_entry == 0 &&
729                                         lpm->tbl24[i].depth <= depth ) {
730                                 lpm->tbl24[i].valid = INVALID;
731                         }
732                         else {
733                                 /*
734                                  * If TBL24 entry is extended, then there has
735                                  * to be a rule with depth >= 25 in the
736                                  * associated TBL8 group.
737                                  */
738
739                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
740                                 tbl8_index = tbl8_group_index *
741                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
742
743                                 for (j = tbl8_index; j < (tbl8_index +
744                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
745
746                                         if (lpm->tbl8[j].depth <= depth)
747                                                 lpm->tbl8[j].valid = INVALID;
748                                 }
749                         }
750                 }
751         }
752         else {
753                 /*
754                  * If a replacement rule exists then modify entries
755                  * associated with this rule.
756                  */
757
758                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
759                         {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
760                         .valid = VALID,
761                         .ext_entry = 0,
762                         .depth = sub_rule_depth,
763                 };
764
765                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
766                         .valid = VALID,
767                         .depth = sub_rule_depth,
768                         .next_hop = lpm->rules_tbl
769                         [sub_rule_index].next_hop,
770                 };
771
772                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
773
774                         if (lpm->tbl24[i].ext_entry == 0 &&
775                                         lpm->tbl24[i].depth <= depth ) {
776                                 lpm->tbl24[i] = new_tbl24_entry;
777                         }
778                         else {
779                                 /*
780                                  * If TBL24 entry is extended, then there has
781                                  * to be a rule with depth >= 25 in the
782                                  * associated TBL8 group.
783                                  */
784
785                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
786                                 tbl8_index = tbl8_group_index *
787                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
788
789                                 for (j = tbl8_index; j < (tbl8_index +
790                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
791
792                                         if (lpm->tbl8[j].depth <= depth)
793                                                 lpm->tbl8[j] = new_tbl8_entry;
794                                 }
795                         }
796                 }
797         }
798
799         return 0;
800 }
801
802 /*
803  * Checks if table 8 group can be recycled.
804  *
805  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
806  * Return of -EINVAL means tbl8 is empty and thus can be recycled
807  * Return of value > -1 means tbl8 is in use but has all the same values and
808  * thus can be recycled
809  */
810 static inline int32_t
811 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
812 {
813         uint32_t tbl8_group_end, i;
814         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
815
816         /*
817          * Check the first entry of the given tbl8. If it is invalid we know
818          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
819          *  (As they would affect all entries in a tbl8) and thus this table
820          *  can not be recycled.
821          */
822         if (tbl8[tbl8_group_start].valid) {
823                 /*
824                  * If first entry is valid check if the depth is less than 24
825                  * and if so check the rest of the entries to verify that they
826                  * are all of this depth.
827                  */
828                 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
829                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
830                                         i++) {
831
832                                 if (tbl8[i].depth !=
833                                                 tbl8[tbl8_group_start].depth) {
834
835                                         return -EEXIST;
836                                 }
837                         }
838                         /* If all entries are the same return the tb8 index */
839                         return tbl8_group_start;
840                 }
841
842                 return -EEXIST;
843         }
844         /*
845          * If the first entry is invalid check if the rest of the entries in
846          * the tbl8 are invalid.
847          */
848         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
849                 if (tbl8[i].valid)
850                         return -EEXIST;
851         }
852         /* If no valid entries are found then return -EINVAL. */
853         return -EINVAL;
854 }
855
856 static inline int32_t
857 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
858         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
859 {
860         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
861                         tbl8_range, i;
862         int32_t tbl8_recycle_index;
863
864         /*
865          * Calculate the index into tbl24 and range. Note: All depths larger
866          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
867          */
868         tbl24_index = ip_masked >> 8;
869
870         /* Calculate the index into tbl8 and range. */
871         tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
872         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
873         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
874         tbl8_range = depth_to_range(depth);
875
876         if (sub_rule_index < 0) {
877                 /*
878                  * Loop through the range of entries on tbl8 for which the
879                  * rule_to_delete must be removed or modified.
880                  */
881                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
882                         if (lpm->tbl8[i].depth <= depth)
883                                 lpm->tbl8[i].valid = INVALID;
884                 }
885         }
886         else {
887                 /* Set new tbl8 entry. */
888                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
889                         .valid = VALID,
890                         .depth = sub_rule_depth,
891                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
892                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
893                 };
894
895                 /*
896                  * Loop through the range of entries on tbl8 for which the
897                  * rule_to_delete must be modified.
898                  */
899                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
900                         if (lpm->tbl8[i].depth <= depth)
901                                 lpm->tbl8[i] = new_tbl8_entry;
902                 }
903         }
904
905         /*
906          * Check if there are any valid entries in this tbl8 group. If all
907          * tbl8 entries are invalid we can free the tbl8 and invalidate the
908          * associated tbl24 entry.
909          */
910
911         tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
912
913         if (tbl8_recycle_index == -EINVAL){
914                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
915                 lpm->tbl24[tbl24_index].valid = 0;
916                 tbl8_free(lpm->tbl8, tbl8_group_start);
917         }
918         else if (tbl8_recycle_index > -1) {
919                 /* Update tbl24 entry. */
920                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
921                         { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
922                         .valid = VALID,
923                         .ext_entry = 0,
924                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
925                 };
926
927                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
928                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
929                 tbl8_free(lpm->tbl8, tbl8_group_start);
930         }
931
932         return 0;
933 }
934
935 /*
936  * Deletes a rule
937  */
938 int
939 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
940 {
941         int32_t rule_to_delete_index, sub_rule_index;
942         uint32_t ip_masked;
943         uint8_t sub_rule_depth;
944         /*
945          * Check input arguments. Note: IP must be a positive integer of 32
946          * bits in length therefore it need not be checked.
947          */
948         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
949                 return -EINVAL;
950         }
951
952         ip_masked = ip & depth_to_mask(depth);
953
954         /*
955          * Find the index of the input rule, that needs to be deleted, in the
956          * rule table.
957          */
958         rule_to_delete_index = rule_find(lpm, ip_masked, depth);
959
960         /*
961          * Check if rule_to_delete_index was found. If no rule was found the
962          * function rule_find returns -EINVAL.
963          */
964         if (rule_to_delete_index < 0)
965                 return -EINVAL;
966
967         /* Delete the rule from the rule table. */
968         rule_delete(lpm, rule_to_delete_index, depth);
969
970         /*
971          * Find rule to replace the rule_to_delete. If there is no rule to
972          * replace the rule_to_delete we return -1 and invalidate the table
973          * entries associated with this rule.
974          */
975         sub_rule_depth = 0;
976         sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
977
978         /*
979          * If the input depth value is less than 25 use function
980          * delete_depth_small otherwise use delete_depth_big.
981          */
982         if (depth <= MAX_DEPTH_TBL24) {
983                 return delete_depth_small(lpm, ip_masked, depth,
984                                 sub_rule_index, sub_rule_depth);
985         }
986         else { /* If depth > MAX_DEPTH_TBL24 */
987                 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
988         }
989 }
990
991 /*
992  * Delete all rules from the LPM table.
993  */
994 void
995 rte_lpm_delete_all(struct rte_lpm *lpm)
996 {
997         /* Zero rule information. */
998         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
999
1000         /* Zero tbl24. */
1001         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1002
1003         /* Zero tbl8. */
1004         memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1005
1006         /* Delete all rules form the rules table. */
1007         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1008 }
1009