lpm: remove redundant check when adding rule
[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_v20 *
123 rte_lpm_find_existing_v20(const char *name)
124 {
125         struct rte_lpm_v20 *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_v20 *) 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 VERSION_SYMBOL(rte_lpm_find_existing, _v20, 2.0);
147
148 struct rte_lpm *
149 rte_lpm_find_existing_v1604(const char *name)
150 {
151         struct rte_lpm *l = NULL;
152         struct rte_tailq_entry *te;
153         struct rte_lpm_list *lpm_list;
154
155         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
156
157         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
158         TAILQ_FOREACH(te, lpm_list, next) {
159                 l = (struct rte_lpm *) te->data;
160                 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
161                         break;
162         }
163         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
164
165         if (te == NULL) {
166                 rte_errno = ENOENT;
167                 return NULL;
168         }
169
170         return l;
171 }
172 BIND_DEFAULT_SYMBOL(rte_lpm_find_existing, _v1604, 16.04);
173 MAP_STATIC_SYMBOL(struct rte_lpm *rte_lpm_find_existing(const char *name),
174                 rte_lpm_find_existing_v1604);
175
176 /*
177  * Allocates memory for LPM object
178  */
179 struct rte_lpm_v20 *
180 rte_lpm_create_v20(const char *name, int socket_id, int max_rules,
181                 __rte_unused int flags)
182 {
183         char mem_name[RTE_LPM_NAMESIZE];
184         struct rte_lpm_v20 *lpm = NULL;
185         struct rte_tailq_entry *te;
186         uint32_t mem_size;
187         struct rte_lpm_list *lpm_list;
188
189         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
190
191         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry_v20) != 2);
192
193         /* Check user arguments. */
194         if ((name == NULL) || (socket_id < -1) || (max_rules == 0)) {
195                 rte_errno = EINVAL;
196                 return NULL;
197         }
198
199         snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
200
201         /* Determine the amount of memory to allocate. */
202         mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
203
204         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
205
206         /* guarantee there's no existing */
207         TAILQ_FOREACH(te, lpm_list, next) {
208                 lpm = (struct rte_lpm_v20 *) te->data;
209                 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
210                         break;
211         }
212         lpm = NULL;
213         if (te != NULL) {
214                 rte_errno = EEXIST;
215                 goto exit;
216         }
217
218         /* allocate tailq entry */
219         te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
220         if (te == NULL) {
221                 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
222                 goto exit;
223         }
224
225         /* Allocate memory to store the LPM data structures. */
226         lpm = (struct rte_lpm_v20 *)rte_zmalloc_socket(mem_name, mem_size,
227                         RTE_CACHE_LINE_SIZE, socket_id);
228         if (lpm == NULL) {
229                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
230                 rte_free(te);
231                 goto exit;
232         }
233
234         /* Save user arguments. */
235         lpm->max_rules = max_rules;
236         snprintf(lpm->name, sizeof(lpm->name), "%s", name);
237
238         te->data = (void *) lpm;
239
240         TAILQ_INSERT_TAIL(lpm_list, te, next);
241
242 exit:
243         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
244
245         return lpm;
246 }
247 VERSION_SYMBOL(rte_lpm_create, _v20, 2.0);
248
249 struct rte_lpm *
250 rte_lpm_create_v1604(const char *name, int socket_id,
251                 const struct rte_lpm_config *config)
252 {
253         char mem_name[RTE_LPM_NAMESIZE];
254         struct rte_lpm *lpm = NULL;
255         struct rte_tailq_entry *te;
256         uint32_t mem_size, rules_size, tbl8s_size;
257         struct rte_lpm_list *lpm_list;
258
259         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
260
261         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
262
263         /* Check user arguments. */
264         if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
265                         || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
266                 rte_errno = EINVAL;
267                 return NULL;
268         }
269
270         snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
271
272         /* Determine the amount of memory to allocate. */
273         mem_size = sizeof(*lpm);
274         rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
275         tbl8s_size = (sizeof(struct rte_lpm_tbl_entry) *
276                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s);
277
278         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
279
280         /* guarantee there's no existing */
281         TAILQ_FOREACH(te, lpm_list, next) {
282                 lpm = (struct rte_lpm *) te->data;
283                 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
284                         break;
285         }
286         lpm = NULL;
287         if (te != NULL) {
288                 rte_errno = EEXIST;
289                 goto exit;
290         }
291
292         /* allocate tailq entry */
293         te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
294         if (te == NULL) {
295                 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
296                 goto exit;
297         }
298
299         /* Allocate memory to store the LPM data structures. */
300         lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
301                         RTE_CACHE_LINE_SIZE, socket_id);
302         if (lpm == NULL) {
303                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
304                 rte_free(te);
305                 goto exit;
306         }
307
308         lpm->rules_tbl = (struct rte_lpm_rule *)rte_zmalloc_socket(NULL,
309                         (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
310
311         if (lpm->rules_tbl == NULL) {
312                 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
313                 rte_free(lpm);
314                 lpm = NULL;
315                 rte_free(te);
316                 goto exit;
317         }
318
319         lpm->tbl8 = (struct rte_lpm_tbl_entry *)rte_zmalloc_socket(NULL,
320                         (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
321
322         if (lpm->tbl8 == NULL) {
323                 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
324                 rte_free(lpm);
325                 lpm = NULL;
326                 rte_free(te);
327                 goto exit;
328         }
329
330         /* Save user arguments. */
331         lpm->max_rules = config->max_rules;
332         lpm->number_tbl8s = config->number_tbl8s;
333         snprintf(lpm->name, sizeof(lpm->name), "%s", name);
334
335         te->data = (void *) lpm;
336
337         TAILQ_INSERT_TAIL(lpm_list, te, next);
338
339 exit:
340         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
341
342         return lpm;
343 }
344 BIND_DEFAULT_SYMBOL(rte_lpm_create, _v1604, 16.04);
345 MAP_STATIC_SYMBOL(
346         struct rte_lpm *rte_lpm_create(const char *name, int socket_id,
347                         const struct rte_lpm_config *config), rte_lpm_create_v1604);
348
349 /*
350  * Deallocates memory for given LPM table.
351  */
352 void
353 rte_lpm_free_v20(struct rte_lpm_v20 *lpm)
354 {
355         struct rte_lpm_list *lpm_list;
356         struct rte_tailq_entry *te;
357
358         /* Check user arguments. */
359         if (lpm == NULL)
360                 return;
361
362         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
363
364         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
365
366         /* find our tailq entry */
367         TAILQ_FOREACH(te, lpm_list, next) {
368                 if (te->data == (void *) lpm)
369                         break;
370         }
371         if (te != NULL)
372                 TAILQ_REMOVE(lpm_list, te, next);
373
374         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
375
376         rte_free(lpm);
377         rte_free(te);
378 }
379 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
380
381 void
382 rte_lpm_free_v1604(struct rte_lpm *lpm)
383 {
384         struct rte_lpm_list *lpm_list;
385         struct rte_tailq_entry *te;
386
387         /* Check user arguments. */
388         if (lpm == NULL)
389                 return;
390
391         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
392
393         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
394
395         /* find our tailq entry */
396         TAILQ_FOREACH(te, lpm_list, next) {
397                 if (te->data == (void *) lpm)
398                         break;
399         }
400         if (te != NULL)
401                 TAILQ_REMOVE(lpm_list, te, next);
402
403         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
404
405         rte_free(lpm->rules_tbl);
406         rte_free(lpm);
407         rte_free(te);
408 }
409 BIND_DEFAULT_SYMBOL(rte_lpm_free, _v1604, 16.04);
410 MAP_STATIC_SYMBOL(void rte_lpm_free(struct rte_lpm *lpm),
411                 rte_lpm_free_v1604);
412
413 /*
414  * Adds a rule to the rule table.
415  *
416  * NOTE: The rule table is split into 32 groups. Each group contains rules that
417  * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
418  * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
419  * to refer to depth 1 because even though the depth range is 1 - 32, depths
420  * are stored in the rule table from 0 - 31.
421  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
422  */
423 static inline int32_t
424 rule_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
425         uint8_t next_hop)
426 {
427         uint32_t rule_gindex, rule_index, last_rule;
428         int i;
429
430         VERIFY_DEPTH(depth);
431
432         /* Scan through rule group to see if rule already exists. */
433         if (lpm->rule_info[depth - 1].used_rules > 0) {
434
435                 /* rule_gindex stands for rule group index. */
436                 rule_gindex = lpm->rule_info[depth - 1].first_rule;
437                 /* Initialise rule_index to point to start of rule group. */
438                 rule_index = rule_gindex;
439                 /* Last rule = Last used rule in this rule group. */
440                 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
441
442                 for (; rule_index < last_rule; rule_index++) {
443
444                         /* If rule already exists update its next_hop and return. */
445                         if (lpm->rules_tbl[rule_index].ip == ip_masked) {
446                                 lpm->rules_tbl[rule_index].next_hop = next_hop;
447
448                                 return rule_index;
449                         }
450                 }
451
452                 if (rule_index == lpm->max_rules)
453                         return -ENOSPC;
454         } else {
455                 /* Calculate the position in which the rule will be stored. */
456                 rule_index = 0;
457
458                 for (i = depth - 1; i > 0; i--) {
459                         if (lpm->rule_info[i - 1].used_rules > 0) {
460                                 rule_index = lpm->rule_info[i - 1].first_rule
461                                                 + lpm->rule_info[i - 1].used_rules;
462                                 break;
463                         }
464                 }
465                 if (rule_index == lpm->max_rules)
466                         return -ENOSPC;
467
468                 lpm->rule_info[depth - 1].first_rule = rule_index;
469         }
470
471         /* Make room for the new rule in the array. */
472         for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
473                 if (lpm->rule_info[i - 1].first_rule
474                                 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
475                         return -ENOSPC;
476
477                 if (lpm->rule_info[i - 1].used_rules > 0) {
478                         lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
479                                 + lpm->rule_info[i - 1].used_rules]
480                                         = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
481                         lpm->rule_info[i - 1].first_rule++;
482                 }
483         }
484
485         /* Add the new rule. */
486         lpm->rules_tbl[rule_index].ip = ip_masked;
487         lpm->rules_tbl[rule_index].next_hop = next_hop;
488
489         /* Increment the used rules counter for this rule group. */
490         lpm->rule_info[depth - 1].used_rules++;
491
492         return rule_index;
493 }
494
495 static inline int32_t
496 rule_add_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
497         uint32_t next_hop)
498 {
499         uint32_t rule_gindex, rule_index, last_rule;
500         int i;
501
502         VERIFY_DEPTH(depth);
503
504         /* Scan through rule group to see if rule already exists. */
505         if (lpm->rule_info[depth - 1].used_rules > 0) {
506
507                 /* rule_gindex stands for rule group index. */
508                 rule_gindex = lpm->rule_info[depth - 1].first_rule;
509                 /* Initialise rule_index to point to start of rule group. */
510                 rule_index = rule_gindex;
511                 /* Last rule = Last used rule in this rule group. */
512                 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
513
514                 for (; rule_index < last_rule; rule_index++) {
515
516                         /* If rule already exists update its next_hop and return. */
517                         if (lpm->rules_tbl[rule_index].ip == ip_masked) {
518                                 lpm->rules_tbl[rule_index].next_hop = next_hop;
519
520                                 return rule_index;
521                         }
522                 }
523
524                 if (rule_index == lpm->max_rules)
525                         return -ENOSPC;
526         } else {
527                 /* Calculate the position in which the rule will be stored. */
528                 rule_index = 0;
529
530                 for (i = depth - 1; i > 0; i--) {
531                         if (lpm->rule_info[i - 1].used_rules > 0) {
532                                 rule_index = lpm->rule_info[i - 1].first_rule
533                                                 + lpm->rule_info[i - 1].used_rules;
534                                 break;
535                         }
536                 }
537                 if (rule_index == lpm->max_rules)
538                         return -ENOSPC;
539
540                 lpm->rule_info[depth - 1].first_rule = rule_index;
541         }
542
543         /* Make room for the new rule in the array. */
544         for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
545                 if (lpm->rule_info[i - 1].first_rule
546                                 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
547                         return -ENOSPC;
548
549                 if (lpm->rule_info[i - 1].used_rules > 0) {
550                         lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
551                                 + lpm->rule_info[i - 1].used_rules]
552                                         = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
553                         lpm->rule_info[i - 1].first_rule++;
554                 }
555         }
556
557         /* Add the new rule. */
558         lpm->rules_tbl[rule_index].ip = ip_masked;
559         lpm->rules_tbl[rule_index].next_hop = next_hop;
560
561         /* Increment the used rules counter for this rule group. */
562         lpm->rule_info[depth - 1].used_rules++;
563
564         return rule_index;
565 }
566
567 /*
568  * Delete a rule from the rule table.
569  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
570  */
571 static inline void
572 rule_delete_v20(struct rte_lpm_v20 *lpm, int32_t rule_index, uint8_t depth)
573 {
574         int i;
575
576         VERIFY_DEPTH(depth);
577
578         lpm->rules_tbl[rule_index] =
579                         lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
580                                 + lpm->rule_info[depth - 1].used_rules - 1];
581
582         for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
583                 if (lpm->rule_info[i].used_rules > 0) {
584                         lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
585                                 lpm->rules_tbl[lpm->rule_info[i].first_rule
586                                         + lpm->rule_info[i].used_rules - 1];
587                         lpm->rule_info[i].first_rule--;
588                 }
589         }
590
591         lpm->rule_info[depth - 1].used_rules--;
592 }
593
594 static inline void
595 rule_delete_v1604(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
596 {
597         int i;
598
599         VERIFY_DEPTH(depth);
600
601         lpm->rules_tbl[rule_index] =
602                         lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
603                         + lpm->rule_info[depth - 1].used_rules - 1];
604
605         for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
606                 if (lpm->rule_info[i].used_rules > 0) {
607                         lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
608                                         lpm->rules_tbl[lpm->rule_info[i].first_rule
609                                                 + lpm->rule_info[i].used_rules - 1];
610                         lpm->rule_info[i].first_rule--;
611                 }
612         }
613
614         lpm->rule_info[depth - 1].used_rules--;
615 }
616
617 /*
618  * Finds a rule in rule table.
619  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
620  */
621 static inline int32_t
622 rule_find_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth)
623 {
624         uint32_t rule_gindex, last_rule, rule_index;
625
626         VERIFY_DEPTH(depth);
627
628         rule_gindex = lpm->rule_info[depth - 1].first_rule;
629         last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
630
631         /* Scan used rules at given depth to find rule. */
632         for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
633                 /* If rule is found return the rule index. */
634                 if (lpm->rules_tbl[rule_index].ip == ip_masked)
635                         return rule_index;
636         }
637
638         /* If rule is not found return -EINVAL. */
639         return -EINVAL;
640 }
641
642 static inline int32_t
643 rule_find_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
644 {
645         uint32_t rule_gindex, last_rule, rule_index;
646
647         VERIFY_DEPTH(depth);
648
649         rule_gindex = lpm->rule_info[depth - 1].first_rule;
650         last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
651
652         /* Scan used rules at given depth to find rule. */
653         for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
654                 /* If rule is found return the rule index. */
655                 if (lpm->rules_tbl[rule_index].ip == ip_masked)
656                         return rule_index;
657         }
658
659         /* If rule is not found return -EINVAL. */
660         return -EINVAL;
661 }
662
663 /*
664  * Find, clean and allocate a tbl8.
665  */
666 static inline int32_t
667 tbl8_alloc_v20(struct rte_lpm_tbl_entry_v20 *tbl8)
668 {
669         uint32_t group_idx; /* tbl8 group index. */
670         struct rte_lpm_tbl_entry_v20 *tbl8_entry;
671
672         /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
673         for (group_idx = 0; group_idx < RTE_LPM_TBL8_NUM_GROUPS;
674                         group_idx++) {
675                 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
676                 /* If a free tbl8 group is found clean it and set as VALID. */
677                 if (!tbl8_entry->valid_group) {
678                         memset(&tbl8_entry[0], 0,
679                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
680                                         sizeof(tbl8_entry[0]));
681
682                         tbl8_entry->valid_group = VALID;
683
684                         /* Return group index for allocated tbl8 group. */
685                         return group_idx;
686                 }
687         }
688
689         /* If there are no tbl8 groups free then return error. */
690         return -ENOSPC;
691 }
692
693 static inline int32_t
694 tbl8_alloc_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t number_tbl8s)
695 {
696         uint32_t group_idx; /* tbl8 group index. */
697         struct rte_lpm_tbl_entry *tbl8_entry;
698
699         /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
700         for (group_idx = 0; group_idx < number_tbl8s; group_idx++) {
701                 tbl8_entry = &tbl8[group_idx * RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
702                 /* If a free tbl8 group is found clean it and set as VALID. */
703                 if (!tbl8_entry->valid_group) {
704                         memset(&tbl8_entry[0], 0,
705                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
706                                         sizeof(tbl8_entry[0]));
707
708                         tbl8_entry->valid_group = VALID;
709
710                         /* Return group index for allocated tbl8 group. */
711                         return group_idx;
712                 }
713         }
714
715         /* If there are no tbl8 groups free then return error. */
716         return -ENOSPC;
717 }
718
719 static inline void
720 tbl8_free_v20(struct rte_lpm_tbl_entry_v20 *tbl8, uint32_t tbl8_group_start)
721 {
722         /* Set tbl8 group invalid*/
723         tbl8[tbl8_group_start].valid_group = INVALID;
724 }
725
726 static inline void
727 tbl8_free_v1604(struct rte_lpm_tbl_entry *tbl8, uint32_t tbl8_group_start)
728 {
729         /* Set tbl8 group invalid*/
730         tbl8[tbl8_group_start].valid_group = INVALID;
731 }
732
733 static inline int32_t
734 add_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
735                 uint8_t next_hop)
736 {
737         uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
738
739         /* Calculate the index into Table24. */
740         tbl24_index = ip >> 8;
741         tbl24_range = depth_to_range(depth);
742
743         for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
744                 /*
745                  * For invalid OR valid and non-extended tbl 24 entries set
746                  * entry.
747                  */
748                 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
749                                 lpm->tbl24[i].depth <= depth)) {
750
751                         struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
752                                 .valid = VALID,
753                                 .valid_group = 0,
754                                 .depth = depth,
755                         };
756                         new_tbl24_entry.next_hop = next_hop;
757
758                         /* Setting tbl24 entry in one go to avoid race
759                          * conditions
760                          */
761                         lpm->tbl24[i] = new_tbl24_entry;
762
763                         continue;
764                 }
765
766                 if (lpm->tbl24[i].valid_group == 1) {
767                         /* If tbl24 entry is valid and extended calculate the
768                          *  index into tbl8.
769                          */
770                         tbl8_index = lpm->tbl24[i].group_idx *
771                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
772                         tbl8_group_end = tbl8_index +
773                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
774
775                         for (j = tbl8_index; j < tbl8_group_end; j++) {
776                                 if (!lpm->tbl8[j].valid ||
777                                                 lpm->tbl8[j].depth <= depth) {
778                                         struct rte_lpm_tbl_entry_v20
779                                                 new_tbl8_entry = {
780                                                 .valid = VALID,
781                                                 .valid_group = VALID,
782                                                 .depth = depth,
783                                         };
784                                         new_tbl8_entry.next_hop = next_hop;
785
786                                         /*
787                                          * Setting tbl8 entry in one go to avoid
788                                          * race conditions
789                                          */
790                                         lpm->tbl8[j] = new_tbl8_entry;
791
792                                         continue;
793                                 }
794                         }
795                 }
796         }
797
798         return 0;
799 }
800
801 static inline int32_t
802 add_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
803                 uint32_t next_hop)
804 {
805 #define group_idx next_hop
806         uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
807
808         /* Calculate the index into Table24. */
809         tbl24_index = ip >> 8;
810         tbl24_range = depth_to_range(depth);
811
812         for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
813                 /*
814                  * For invalid OR valid and non-extended tbl 24 entries set
815                  * entry.
816                  */
817                 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
818                                 lpm->tbl24[i].depth <= depth)) {
819
820                         struct rte_lpm_tbl_entry new_tbl24_entry = {
821                                 .next_hop = next_hop,
822                                 .valid = VALID,
823                                 .valid_group = 0,
824                                 .depth = depth,
825                         };
826
827                         /* Setting tbl24 entry in one go to avoid race
828                          * conditions
829                          */
830                         lpm->tbl24[i] = new_tbl24_entry;
831
832                         continue;
833                 }
834
835                 if (lpm->tbl24[i].valid_group == 1) {
836                         /* If tbl24 entry is valid and extended calculate the
837                          *  index into tbl8.
838                          */
839                         tbl8_index = lpm->tbl24[i].group_idx *
840                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
841                         tbl8_group_end = tbl8_index +
842                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
843
844                         for (j = tbl8_index; j < tbl8_group_end; j++) {
845                                 if (!lpm->tbl8[j].valid ||
846                                                 lpm->tbl8[j].depth <= depth) {
847                                         struct rte_lpm_tbl_entry
848                                                 new_tbl8_entry = {
849                                                 .valid = VALID,
850                                                 .valid_group = VALID,
851                                                 .depth = depth,
852                                                 .next_hop = next_hop,
853                                         };
854
855                                         /*
856                                          * Setting tbl8 entry in one go to avoid
857                                          * race conditions
858                                          */
859                                         lpm->tbl8[j] = new_tbl8_entry;
860
861                                         continue;
862                                 }
863                         }
864                 }
865         }
866 #undef group_idx
867         return 0;
868 }
869
870 static inline int32_t
871 add_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked, uint8_t depth,
872                 uint8_t next_hop)
873 {
874         uint32_t tbl24_index;
875         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
876                 tbl8_range, i;
877
878         tbl24_index = (ip_masked >> 8);
879         tbl8_range = depth_to_range(depth);
880
881         if (!lpm->tbl24[tbl24_index].valid) {
882                 /* Search for a free tbl8 group. */
883                 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
884
885                 /* Check tbl8 allocation was successful. */
886                 if (tbl8_group_index < 0) {
887                         return tbl8_group_index;
888                 }
889
890                 /* Find index into tbl8 and range. */
891                 tbl8_index = (tbl8_group_index *
892                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
893                                 (ip_masked & 0xFF);
894
895                 /* Set tbl8 entry. */
896                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
897                         lpm->tbl8[i].depth = depth;
898                         lpm->tbl8[i].next_hop = next_hop;
899                         lpm->tbl8[i].valid = VALID;
900                 }
901
902                 /*
903                  * Update tbl24 entry to point to new tbl8 entry. Note: The
904                  * ext_flag and tbl8_index need to be updated simultaneously,
905                  * so assign whole structure in one go
906                  */
907
908                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
909                         { .group_idx = (uint8_t)tbl8_group_index, },
910                         .valid = VALID,
911                         .valid_group = 1,
912                         .depth = 0,
913                 };
914
915                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
916
917         } /* If valid entry but not extended calculate the index into Table8. */
918         else if (lpm->tbl24[tbl24_index].valid_group == 0) {
919                 /* Search for free tbl8 group. */
920                 tbl8_group_index = tbl8_alloc_v20(lpm->tbl8);
921
922                 if (tbl8_group_index < 0) {
923                         return tbl8_group_index;
924                 }
925
926                 tbl8_group_start = tbl8_group_index *
927                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
928                 tbl8_group_end = tbl8_group_start +
929                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
930
931                 /* Populate new tbl8 with tbl24 value. */
932                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
933                         lpm->tbl8[i].valid = VALID;
934                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
935                         lpm->tbl8[i].next_hop =
936                                         lpm->tbl24[tbl24_index].next_hop;
937                 }
938
939                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
940
941                 /* Insert new rule into the tbl8 entry. */
942                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
943                         lpm->tbl8[i].valid = VALID;
944                         lpm->tbl8[i].depth = depth;
945                         lpm->tbl8[i].next_hop = next_hop;
946                 }
947
948                 /*
949                  * Update tbl24 entry to point to new tbl8 entry. Note: The
950                  * ext_flag and tbl8_index need to be updated simultaneously,
951                  * so assign whole structure in one go.
952                  */
953
954                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
955                                 { .group_idx = (uint8_t)tbl8_group_index, },
956                                 .valid = VALID,
957                                 .valid_group = 1,
958                                 .depth = 0,
959                 };
960
961                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
962
963         } else { /*
964                 * If it is valid, extended entry calculate the index into tbl8.
965                 */
966                 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
967                 tbl8_group_start = tbl8_group_index *
968                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
969                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
970
971                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
972
973                         if (!lpm->tbl8[i].valid ||
974                                         lpm->tbl8[i].depth <= depth) {
975                                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
976                                         .valid = VALID,
977                                         .depth = depth,
978                                         .valid_group = lpm->tbl8[i].valid_group,
979                                 };
980                                 new_tbl8_entry.next_hop = next_hop;
981                                 /*
982                                  * Setting tbl8 entry in one go to avoid race
983                                  * condition
984                                  */
985                                 lpm->tbl8[i] = new_tbl8_entry;
986
987                                 continue;
988                         }
989                 }
990         }
991
992         return 0;
993 }
994
995 static inline int32_t
996 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
997                 uint32_t next_hop)
998 {
999 #define group_idx next_hop
1000         uint32_t tbl24_index;
1001         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
1002                 tbl8_range, i;
1003
1004         tbl24_index = (ip_masked >> 8);
1005         tbl8_range = depth_to_range(depth);
1006
1007         if (!lpm->tbl24[tbl24_index].valid) {
1008                 /* Search for a free tbl8 group. */
1009                 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1010
1011                 /* Check tbl8 allocation was successful. */
1012                 if (tbl8_group_index < 0) {
1013                         return tbl8_group_index;
1014                 }
1015
1016                 /* Find index into tbl8 and range. */
1017                 tbl8_index = (tbl8_group_index *
1018                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1019                                 (ip_masked & 0xFF);
1020
1021                 /* Set tbl8 entry. */
1022                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1023                         lpm->tbl8[i].depth = depth;
1024                         lpm->tbl8[i].next_hop = next_hop;
1025                         lpm->tbl8[i].valid = VALID;
1026                 }
1027
1028                 /*
1029                  * Update tbl24 entry to point to new tbl8 entry. Note: The
1030                  * ext_flag and tbl8_index need to be updated simultaneously,
1031                  * so assign whole structure in one go
1032                  */
1033
1034                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1035                         .group_idx = (uint8_t)tbl8_group_index,
1036                         .valid = VALID,
1037                         .valid_group = 1,
1038                         .depth = 0,
1039                 };
1040
1041                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1042
1043         } /* If valid entry but not extended calculate the index into Table8. */
1044         else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1045                 /* Search for free tbl8 group. */
1046                 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1047
1048                 if (tbl8_group_index < 0) {
1049                         return tbl8_group_index;
1050                 }
1051
1052                 tbl8_group_start = tbl8_group_index *
1053                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1054                 tbl8_group_end = tbl8_group_start +
1055                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1056
1057                 /* Populate new tbl8 with tbl24 value. */
1058                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1059                         lpm->tbl8[i].valid = VALID;
1060                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1061                         lpm->tbl8[i].next_hop =
1062                                         lpm->tbl24[tbl24_index].next_hop;
1063                 }
1064
1065                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1066
1067                 /* Insert new rule into the tbl8 entry. */
1068                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1069                         lpm->tbl8[i].valid = VALID;
1070                         lpm->tbl8[i].depth = depth;
1071                         lpm->tbl8[i].next_hop = next_hop;
1072                 }
1073
1074                 /*
1075                  * Update tbl24 entry to point to new tbl8 entry. Note: The
1076                  * ext_flag and tbl8_index need to be updated simultaneously,
1077                  * so assign whole structure in one go.
1078                  */
1079
1080                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1081                                 .group_idx = (uint8_t)tbl8_group_index,
1082                                 .valid = VALID,
1083                                 .valid_group = 1,
1084                                 .depth = 0,
1085                 };
1086
1087                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1088
1089         } else { /*
1090                 * If it is valid, extended entry calculate the index into tbl8.
1091                 */
1092                 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1093                 tbl8_group_start = tbl8_group_index *
1094                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1095                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1096
1097                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1098
1099                         if (!lpm->tbl8[i].valid ||
1100                                         lpm->tbl8[i].depth <= depth) {
1101                                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1102                                         .valid = VALID,
1103                                         .depth = depth,
1104                                         .next_hop = next_hop,
1105                                         .valid_group = lpm->tbl8[i].valid_group,
1106                                 };
1107
1108                                 /*
1109                                  * Setting tbl8 entry in one go to avoid race
1110                                  * condition
1111                                  */
1112                                 lpm->tbl8[i] = new_tbl8_entry;
1113
1114                                 continue;
1115                         }
1116                 }
1117         }
1118 #undef group_idx
1119         return 0;
1120 }
1121
1122 /*
1123  * Add a route
1124  */
1125 int
1126 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1127                 uint8_t next_hop)
1128 {
1129         int32_t rule_index, status = 0;
1130         uint32_t ip_masked;
1131
1132         /* Check user arguments. */
1133         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1134                 return -EINVAL;
1135
1136         ip_masked = ip & depth_to_mask(depth);
1137
1138         /* Add the rule to the rule table. */
1139         rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1140
1141         /* If the is no space available for new rule return error. */
1142         if (rule_index < 0) {
1143                 return rule_index;
1144         }
1145
1146         if (depth <= MAX_DEPTH_TBL24) {
1147                 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1148         } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1149                 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1150
1151                 /*
1152                  * If add fails due to exhaustion of tbl8 extensions delete
1153                  * rule that was added to rule table.
1154                  */
1155                 if (status < 0) {
1156                         rule_delete_v20(lpm, rule_index, depth);
1157
1158                         return status;
1159                 }
1160         }
1161
1162         return 0;
1163 }
1164 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1165
1166 int
1167 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1168                 uint32_t next_hop)
1169 {
1170         int32_t rule_index, status = 0;
1171         uint32_t ip_masked;
1172
1173         /* Check user arguments. */
1174         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1175                 return -EINVAL;
1176
1177         ip_masked = ip & depth_to_mask(depth);
1178
1179         /* Add the rule to the rule table. */
1180         rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1181
1182         /* If the is no space available for new rule return error. */
1183         if (rule_index < 0) {
1184                 return rule_index;
1185         }
1186
1187         if (depth <= MAX_DEPTH_TBL24) {
1188                 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1189         } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1190                 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1191
1192                 /*
1193                  * If add fails due to exhaustion of tbl8 extensions delete
1194                  * rule that was added to rule table.
1195                  */
1196                 if (status < 0) {
1197                         rule_delete_v1604(lpm, rule_index, depth);
1198
1199                         return status;
1200                 }
1201         }
1202
1203         return 0;
1204 }
1205 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1206 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1207                 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1208
1209 /*
1210  * Look for a rule in the high-level rules table
1211  */
1212 int
1213 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1214 uint8_t *next_hop)
1215 {
1216         uint32_t ip_masked;
1217         int32_t rule_index;
1218
1219         /* Check user arguments. */
1220         if ((lpm == NULL) ||
1221                 (next_hop == NULL) ||
1222                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1223                 return -EINVAL;
1224
1225         /* Look for the rule using rule_find. */
1226         ip_masked = ip & depth_to_mask(depth);
1227         rule_index = rule_find_v20(lpm, ip_masked, depth);
1228
1229         if (rule_index >= 0) {
1230                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1231                 return 1;
1232         }
1233
1234         /* If rule is not found return 0. */
1235         return 0;
1236 }
1237 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1238
1239 int
1240 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1241 uint32_t *next_hop)
1242 {
1243         uint32_t ip_masked;
1244         int32_t rule_index;
1245
1246         /* Check user arguments. */
1247         if ((lpm == NULL) ||
1248                 (next_hop == NULL) ||
1249                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1250                 return -EINVAL;
1251
1252         /* Look for the rule using rule_find. */
1253         ip_masked = ip & depth_to_mask(depth);
1254         rule_index = rule_find_v1604(lpm, ip_masked, depth);
1255
1256         if (rule_index >= 0) {
1257                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1258                 return 1;
1259         }
1260
1261         /* If rule is not found return 0. */
1262         return 0;
1263 }
1264 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1265 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1266                 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1267
1268 static inline int32_t
1269 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1270                 uint8_t *sub_rule_depth)
1271 {
1272         int32_t rule_index;
1273         uint32_t ip_masked;
1274         uint8_t prev_depth;
1275
1276         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1277                 ip_masked = ip & depth_to_mask(prev_depth);
1278
1279                 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1280
1281                 if (rule_index >= 0) {
1282                         *sub_rule_depth = prev_depth;
1283                         return rule_index;
1284                 }
1285         }
1286
1287         return -1;
1288 }
1289
1290 static inline int32_t
1291 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1292                 uint8_t *sub_rule_depth)
1293 {
1294         int32_t rule_index;
1295         uint32_t ip_masked;
1296         uint8_t prev_depth;
1297
1298         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1299                 ip_masked = ip & depth_to_mask(prev_depth);
1300
1301                 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1302
1303                 if (rule_index >= 0) {
1304                         *sub_rule_depth = prev_depth;
1305                         return rule_index;
1306                 }
1307         }
1308
1309         return -1;
1310 }
1311
1312 static inline int32_t
1313 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1314         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1315 {
1316         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1317
1318         /* Calculate the range and index into Table24. */
1319         tbl24_range = depth_to_range(depth);
1320         tbl24_index = (ip_masked >> 8);
1321
1322         /*
1323          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1324          * and a positive number indicates a sub_rule_index.
1325          */
1326         if (sub_rule_index < 0) {
1327                 /*
1328                  * If no replacement rule exists then invalidate entries
1329                  * associated with this rule.
1330                  */
1331                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1332
1333                         if (lpm->tbl24[i].valid_group == 0 &&
1334                                         lpm->tbl24[i].depth <= depth) {
1335                                 lpm->tbl24[i].valid = INVALID;
1336                         } else if (lpm->tbl24[i].valid_group == 1) {
1337                                 /*
1338                                  * If TBL24 entry is extended, then there has
1339                                  * to be a rule with depth >= 25 in the
1340                                  * associated TBL8 group.
1341                                  */
1342
1343                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1344                                 tbl8_index = tbl8_group_index *
1345                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1346
1347                                 for (j = tbl8_index; j < (tbl8_index +
1348                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1349
1350                                         if (lpm->tbl8[j].depth <= depth)
1351                                                 lpm->tbl8[j].valid = INVALID;
1352                                 }
1353                         }
1354                 }
1355         } else {
1356                 /*
1357                  * If a replacement rule exists then modify entries
1358                  * associated with this rule.
1359                  */
1360
1361                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1362                         {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
1363                         .valid = VALID,
1364                         .valid_group = 0,
1365                         .depth = sub_rule_depth,
1366                 };
1367
1368                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1369                         .valid = VALID,
1370                         .valid_group = VALID,
1371                         .depth = sub_rule_depth,
1372                 };
1373                 new_tbl8_entry.next_hop =
1374                                 lpm->rules_tbl[sub_rule_index].next_hop;
1375
1376                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1377
1378                         if (lpm->tbl24[i].valid_group == 0 &&
1379                                         lpm->tbl24[i].depth <= depth) {
1380                                 lpm->tbl24[i] = new_tbl24_entry;
1381                         } else  if (lpm->tbl24[i].valid_group == 1) {
1382                                 /*
1383                                  * If TBL24 entry is extended, then there has
1384                                  * to be a rule with depth >= 25 in the
1385                                  * associated TBL8 group.
1386                                  */
1387
1388                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1389                                 tbl8_index = tbl8_group_index *
1390                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1391
1392                                 for (j = tbl8_index; j < (tbl8_index +
1393                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1394
1395                                         if (lpm->tbl8[j].depth <= depth)
1396                                                 lpm->tbl8[j] = new_tbl8_entry;
1397                                 }
1398                         }
1399                 }
1400         }
1401
1402         return 0;
1403 }
1404
1405 static inline int32_t
1406 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1407         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1408 {
1409 #define group_idx next_hop
1410         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1411
1412         /* Calculate the range and index into Table24. */
1413         tbl24_range = depth_to_range(depth);
1414         tbl24_index = (ip_masked >> 8);
1415
1416         /*
1417          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1418          * and a positive number indicates a sub_rule_index.
1419          */
1420         if (sub_rule_index < 0) {
1421                 /*
1422                  * If no replacement rule exists then invalidate entries
1423                  * associated with this rule.
1424                  */
1425                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1426
1427                         if (lpm->tbl24[i].valid_group == 0 &&
1428                                         lpm->tbl24[i].depth <= depth) {
1429                                 lpm->tbl24[i].valid = INVALID;
1430                         } else if (lpm->tbl24[i].valid_group == 1) {
1431                                 /*
1432                                  * If TBL24 entry is extended, then there has
1433                                  * to be a rule with depth >= 25 in the
1434                                  * associated TBL8 group.
1435                                  */
1436
1437                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1438                                 tbl8_index = tbl8_group_index *
1439                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1440
1441                                 for (j = tbl8_index; j < (tbl8_index +
1442                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1443
1444                                         if (lpm->tbl8[j].depth <= depth)
1445                                                 lpm->tbl8[j].valid = INVALID;
1446                                 }
1447                         }
1448                 }
1449         } else {
1450                 /*
1451                  * If a replacement rule exists then modify entries
1452                  * associated with this rule.
1453                  */
1454
1455                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1456                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1457                         .valid = VALID,
1458                         .valid_group = 0,
1459                         .depth = sub_rule_depth,
1460                 };
1461
1462                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1463                         .valid = VALID,
1464                         .valid_group = VALID,
1465                         .depth = sub_rule_depth,
1466                         .next_hop = lpm->rules_tbl
1467                         [sub_rule_index].next_hop,
1468                 };
1469
1470                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1471
1472                         if (lpm->tbl24[i].valid_group == 0 &&
1473                                         lpm->tbl24[i].depth <= depth) {
1474                                 lpm->tbl24[i] = new_tbl24_entry;
1475                         } else  if (lpm->tbl24[i].valid_group == 1) {
1476                                 /*
1477                                  * If TBL24 entry is extended, then there has
1478                                  * to be a rule with depth >= 25 in the
1479                                  * associated TBL8 group.
1480                                  */
1481
1482                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1483                                 tbl8_index = tbl8_group_index *
1484                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1485
1486                                 for (j = tbl8_index; j < (tbl8_index +
1487                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1488
1489                                         if (lpm->tbl8[j].depth <= depth)
1490                                                 lpm->tbl8[j] = new_tbl8_entry;
1491                                 }
1492                         }
1493                 }
1494         }
1495 #undef group_idx
1496         return 0;
1497 }
1498
1499 /*
1500  * Checks if table 8 group can be recycled.
1501  *
1502  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1503  * Return of -EINVAL means tbl8 is empty and thus can be recycled
1504  * Return of value > -1 means tbl8 is in use but has all the same values and
1505  * thus can be recycled
1506  */
1507 static inline int32_t
1508 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1509                 uint32_t tbl8_group_start)
1510 {
1511         uint32_t tbl8_group_end, i;
1512         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1513
1514         /*
1515          * Check the first entry of the given tbl8. If it is invalid we know
1516          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1517          *  (As they would affect all entries in a tbl8) and thus this table
1518          *  can not be recycled.
1519          */
1520         if (tbl8[tbl8_group_start].valid) {
1521                 /*
1522                  * If first entry is valid check if the depth is less than 24
1523                  * and if so check the rest of the entries to verify that they
1524                  * are all of this depth.
1525                  */
1526                 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1527                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1528                                         i++) {
1529
1530                                 if (tbl8[i].depth !=
1531                                                 tbl8[tbl8_group_start].depth) {
1532
1533                                         return -EEXIST;
1534                                 }
1535                         }
1536                         /* If all entries are the same return the tb8 index */
1537                         return tbl8_group_start;
1538                 }
1539
1540                 return -EEXIST;
1541         }
1542         /*
1543          * If the first entry is invalid check if the rest of the entries in
1544          * the tbl8 are invalid.
1545          */
1546         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1547                 if (tbl8[i].valid)
1548                         return -EEXIST;
1549         }
1550         /* If no valid entries are found then return -EINVAL. */
1551         return -EINVAL;
1552 }
1553
1554 static inline int32_t
1555 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1556                 uint32_t tbl8_group_start)
1557 {
1558         uint32_t tbl8_group_end, i;
1559         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1560
1561         /*
1562          * Check the first entry of the given tbl8. If it is invalid we know
1563          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1564          *  (As they would affect all entries in a tbl8) and thus this table
1565          *  can not be recycled.
1566          */
1567         if (tbl8[tbl8_group_start].valid) {
1568                 /*
1569                  * If first entry is valid check if the depth is less than 24
1570                  * and if so check the rest of the entries to verify that they
1571                  * are all of this depth.
1572                  */
1573                 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1574                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1575                                         i++) {
1576
1577                                 if (tbl8[i].depth !=
1578                                                 tbl8[tbl8_group_start].depth) {
1579
1580                                         return -EEXIST;
1581                                 }
1582                         }
1583                         /* If all entries are the same return the tb8 index */
1584                         return tbl8_group_start;
1585                 }
1586
1587                 return -EEXIST;
1588         }
1589         /*
1590          * If the first entry is invalid check if the rest of the entries in
1591          * the tbl8 are invalid.
1592          */
1593         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1594                 if (tbl8[i].valid)
1595                         return -EEXIST;
1596         }
1597         /* If no valid entries are found then return -EINVAL. */
1598         return -EINVAL;
1599 }
1600
1601 static inline int32_t
1602 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1603         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1604 {
1605         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1606                         tbl8_range, i;
1607         int32_t tbl8_recycle_index;
1608
1609         /*
1610          * Calculate the index into tbl24 and range. Note: All depths larger
1611          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1612          */
1613         tbl24_index = ip_masked >> 8;
1614
1615         /* Calculate the index into tbl8 and range. */
1616         tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1617         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1618         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1619         tbl8_range = depth_to_range(depth);
1620
1621         if (sub_rule_index < 0) {
1622                 /*
1623                  * Loop through the range of entries on tbl8 for which the
1624                  * rule_to_delete must be removed or modified.
1625                  */
1626                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1627                         if (lpm->tbl8[i].depth <= depth)
1628                                 lpm->tbl8[i].valid = INVALID;
1629                 }
1630         } else {
1631                 /* Set new tbl8 entry. */
1632                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1633                         .valid = VALID,
1634                         .depth = sub_rule_depth,
1635                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1636                 };
1637
1638                 new_tbl8_entry.next_hop =
1639                                 lpm->rules_tbl[sub_rule_index].next_hop;
1640                 /*
1641                  * Loop through the range of entries on tbl8 for which the
1642                  * rule_to_delete must be modified.
1643                  */
1644                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1645                         if (lpm->tbl8[i].depth <= depth)
1646                                 lpm->tbl8[i] = new_tbl8_entry;
1647                 }
1648         }
1649
1650         /*
1651          * Check if there are any valid entries in this tbl8 group. If all
1652          * tbl8 entries are invalid we can free the tbl8 and invalidate the
1653          * associated tbl24 entry.
1654          */
1655
1656         tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1657
1658         if (tbl8_recycle_index == -EINVAL) {
1659                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1660                 lpm->tbl24[tbl24_index].valid = 0;
1661                 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1662         } else if (tbl8_recycle_index > -1) {
1663                 /* Update tbl24 entry. */
1664                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1665                         { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
1666                         .valid = VALID,
1667                         .valid_group = 0,
1668                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
1669                 };
1670
1671                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1672                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1673                 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1674         }
1675
1676         return 0;
1677 }
1678
1679 static inline int32_t
1680 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1681         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1682 {
1683 #define group_idx next_hop
1684         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1685                         tbl8_range, i;
1686         int32_t tbl8_recycle_index;
1687
1688         /*
1689          * Calculate the index into tbl24 and range. Note: All depths larger
1690          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1691          */
1692         tbl24_index = ip_masked >> 8;
1693
1694         /* Calculate the index into tbl8 and range. */
1695         tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1696         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1697         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1698         tbl8_range = depth_to_range(depth);
1699
1700         if (sub_rule_index < 0) {
1701                 /*
1702                  * Loop through the range of entries on tbl8 for which the
1703                  * rule_to_delete must be removed or modified.
1704                  */
1705                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1706                         if (lpm->tbl8[i].depth <= depth)
1707                                 lpm->tbl8[i].valid = INVALID;
1708                 }
1709         } else {
1710                 /* Set new tbl8 entry. */
1711                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1712                         .valid = VALID,
1713                         .depth = sub_rule_depth,
1714                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1715                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1716                 };
1717
1718                 /*
1719                  * Loop through the range of entries on tbl8 for which the
1720                  * rule_to_delete must be modified.
1721                  */
1722                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1723                         if (lpm->tbl8[i].depth <= depth)
1724                                 lpm->tbl8[i] = new_tbl8_entry;
1725                 }
1726         }
1727
1728         /*
1729          * Check if there are any valid entries in this tbl8 group. If all
1730          * tbl8 entries are invalid we can free the tbl8 and invalidate the
1731          * associated tbl24 entry.
1732          */
1733
1734         tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1735
1736         if (tbl8_recycle_index == -EINVAL) {
1737                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1738                 lpm->tbl24[tbl24_index].valid = 0;
1739                 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1740         } else if (tbl8_recycle_index > -1) {
1741                 /* Update tbl24 entry. */
1742                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1743                         .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1744                         .valid = VALID,
1745                         .valid_group = 0,
1746                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
1747                 };
1748
1749                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1750                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1751                 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1752         }
1753 #undef group_idx
1754         return 0;
1755 }
1756
1757 /*
1758  * Deletes a rule
1759  */
1760 int
1761 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1762 {
1763         int32_t rule_to_delete_index, sub_rule_index;
1764         uint32_t ip_masked;
1765         uint8_t sub_rule_depth;
1766         /*
1767          * Check input arguments. Note: IP must be a positive integer of 32
1768          * bits in length therefore it need not be checked.
1769          */
1770         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1771                 return -EINVAL;
1772         }
1773
1774         ip_masked = ip & depth_to_mask(depth);
1775
1776         /*
1777          * Find the index of the input rule, that needs to be deleted, in the
1778          * rule table.
1779          */
1780         rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1781
1782         /*
1783          * Check if rule_to_delete_index was found. If no rule was found the
1784          * function rule_find returns -EINVAL.
1785          */
1786         if (rule_to_delete_index < 0)
1787                 return -EINVAL;
1788
1789         /* Delete the rule from the rule table. */
1790         rule_delete_v20(lpm, rule_to_delete_index, depth);
1791
1792         /*
1793          * Find rule to replace the rule_to_delete. If there is no rule to
1794          * replace the rule_to_delete we return -1 and invalidate the table
1795          * entries associated with this rule.
1796          */
1797         sub_rule_depth = 0;
1798         sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1799
1800         /*
1801          * If the input depth value is less than 25 use function
1802          * delete_depth_small otherwise use delete_depth_big.
1803          */
1804         if (depth <= MAX_DEPTH_TBL24) {
1805                 return delete_depth_small_v20(lpm, ip_masked, depth,
1806                                 sub_rule_index, sub_rule_depth);
1807         } else { /* If depth > MAX_DEPTH_TBL24 */
1808                 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1809                                 sub_rule_depth);
1810         }
1811 }
1812 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1813
1814 int
1815 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1816 {
1817         int32_t rule_to_delete_index, sub_rule_index;
1818         uint32_t ip_masked;
1819         uint8_t sub_rule_depth;
1820         /*
1821          * Check input arguments. Note: IP must be a positive integer of 32
1822          * bits in length therefore it need not be checked.
1823          */
1824         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1825                 return -EINVAL;
1826         }
1827
1828         ip_masked = ip & depth_to_mask(depth);
1829
1830         /*
1831          * Find the index of the input rule, that needs to be deleted, in the
1832          * rule table.
1833          */
1834         rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1835
1836         /*
1837          * Check if rule_to_delete_index was found. If no rule was found the
1838          * function rule_find returns -EINVAL.
1839          */
1840         if (rule_to_delete_index < 0)
1841                 return -EINVAL;
1842
1843         /* Delete the rule from the rule table. */
1844         rule_delete_v1604(lpm, rule_to_delete_index, depth);
1845
1846         /*
1847          * Find rule to replace the rule_to_delete. If there is no rule to
1848          * replace the rule_to_delete we return -1 and invalidate the table
1849          * entries associated with this rule.
1850          */
1851         sub_rule_depth = 0;
1852         sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1853
1854         /*
1855          * If the input depth value is less than 25 use function
1856          * delete_depth_small otherwise use delete_depth_big.
1857          */
1858         if (depth <= MAX_DEPTH_TBL24) {
1859                 return delete_depth_small_v1604(lpm, ip_masked, depth,
1860                                 sub_rule_index, sub_rule_depth);
1861         } else { /* If depth > MAX_DEPTH_TBL24 */
1862                 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1863                                 sub_rule_depth);
1864         }
1865 }
1866 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1867 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1868                 uint8_t depth), rte_lpm_delete_v1604);
1869
1870 /*
1871  * Delete all rules from the LPM table.
1872  */
1873 void
1874 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1875 {
1876         /* Zero rule information. */
1877         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1878
1879         /* Zero tbl24. */
1880         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1881
1882         /* Zero tbl8. */
1883         memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1884
1885         /* Delete all rules form the rules table. */
1886         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1887 }
1888 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1889
1890 void
1891 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1892 {
1893         /* Zero rule information. */
1894         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1895
1896         /* Zero tbl24. */
1897         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1898
1899         /* Zero tbl8. */
1900         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1901                         * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1902
1903         /* Delete all rules form the rules table. */
1904         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1905 }
1906 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1907 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1908                 rte_lpm_delete_all_v1604);