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