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