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