Imported Upstream version 16.04
[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 <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->rules_tbl);
377         rte_free(lpm);
378         rte_free(te);
379 }
380 VERSION_SYMBOL(rte_lpm_free, _v20, 2.0);
381
382 void
383 rte_lpm_free_v1604(struct rte_lpm *lpm)
384 {
385         struct rte_lpm_list *lpm_list;
386         struct rte_tailq_entry *te;
387
388         /* Check user arguments. */
389         if (lpm == NULL)
390                 return;
391
392         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
393
394         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
395
396         /* find our tailq entry */
397         TAILQ_FOREACH(te, lpm_list, next) {
398                 if (te->data == (void *) lpm)
399                         break;
400         }
401         if (te != NULL)
402                 TAILQ_REMOVE(lpm_list, te, next);
403
404         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
405
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                         if (!lpm->tbl8[i].valid ||
945                                         lpm->tbl8[i].depth <= depth) {
946                                 lpm->tbl8[i].valid = VALID;
947                                 lpm->tbl8[i].depth = depth;
948                                 lpm->tbl8[i].next_hop = next_hop;
949
950                                 continue;
951                         }
952                 }
953
954                 /*
955                  * Update tbl24 entry to point to new tbl8 entry. Note: The
956                  * ext_flag and tbl8_index need to be updated simultaneously,
957                  * so assign whole structure in one go.
958                  */
959
960                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
961                                 { .group_idx = (uint8_t)tbl8_group_index, },
962                                 .valid = VALID,
963                                 .valid_group = 1,
964                                 .depth = 0,
965                 };
966
967                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
968
969         } else { /*
970                 * If it is valid, extended entry calculate the index into tbl8.
971                 */
972                 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
973                 tbl8_group_start = tbl8_group_index *
974                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
975                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
976
977                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
978
979                         if (!lpm->tbl8[i].valid ||
980                                         lpm->tbl8[i].depth <= depth) {
981                                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
982                                         .valid = VALID,
983                                         .depth = depth,
984                                         .valid_group = lpm->tbl8[i].valid_group,
985                                 };
986                                 new_tbl8_entry.next_hop = next_hop;
987                                 /*
988                                  * Setting tbl8 entry in one go to avoid race
989                                  * condition
990                                  */
991                                 lpm->tbl8[i] = new_tbl8_entry;
992
993                                 continue;
994                         }
995                 }
996         }
997
998         return 0;
999 }
1000
1001 static inline int32_t
1002 add_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
1003                 uint32_t next_hop)
1004 {
1005 #define group_idx next_hop
1006         uint32_t tbl24_index;
1007         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
1008                 tbl8_range, i;
1009
1010         tbl24_index = (ip_masked >> 8);
1011         tbl8_range = depth_to_range(depth);
1012
1013         if (!lpm->tbl24[tbl24_index].valid) {
1014                 /* Search for a free tbl8 group. */
1015                 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1016
1017                 /* Check tbl8 allocation was successful. */
1018                 if (tbl8_group_index < 0) {
1019                         return tbl8_group_index;
1020                 }
1021
1022                 /* Find index into tbl8 and range. */
1023                 tbl8_index = (tbl8_group_index *
1024                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
1025                                 (ip_masked & 0xFF);
1026
1027                 /* Set tbl8 entry. */
1028                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1029                         lpm->tbl8[i].depth = depth;
1030                         lpm->tbl8[i].next_hop = next_hop;
1031                         lpm->tbl8[i].valid = VALID;
1032                 }
1033
1034                 /*
1035                  * Update tbl24 entry to point to new tbl8 entry. Note: The
1036                  * ext_flag and tbl8_index need to be updated simultaneously,
1037                  * so assign whole structure in one go
1038                  */
1039
1040                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1041                         .group_idx = (uint8_t)tbl8_group_index,
1042                         .valid = VALID,
1043                         .valid_group = 1,
1044                         .depth = 0,
1045                 };
1046
1047                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1048
1049         } /* If valid entry but not extended calculate the index into Table8. */
1050         else if (lpm->tbl24[tbl24_index].valid_group == 0) {
1051                 /* Search for free tbl8 group. */
1052                 tbl8_group_index = tbl8_alloc_v1604(lpm->tbl8, lpm->number_tbl8s);
1053
1054                 if (tbl8_group_index < 0) {
1055                         return tbl8_group_index;
1056                 }
1057
1058                 tbl8_group_start = tbl8_group_index *
1059                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1060                 tbl8_group_end = tbl8_group_start +
1061                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1062
1063                 /* Populate new tbl8 with tbl24 value. */
1064                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
1065                         lpm->tbl8[i].valid = VALID;
1066                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
1067                         lpm->tbl8[i].next_hop =
1068                                         lpm->tbl24[tbl24_index].next_hop;
1069                 }
1070
1071                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1072
1073                 /* Insert new rule into the tbl8 entry. */
1074                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
1075                         if (!lpm->tbl8[i].valid ||
1076                                         lpm->tbl8[i].depth <= depth) {
1077                                 lpm->tbl8[i].valid = VALID;
1078                                 lpm->tbl8[i].depth = depth;
1079                                 lpm->tbl8[i].next_hop = next_hop;
1080
1081                                 continue;
1082                         }
1083                 }
1084
1085                 /*
1086                  * Update tbl24 entry to point to new tbl8 entry. Note: The
1087                  * ext_flag and tbl8_index need to be updated simultaneously,
1088                  * so assign whole structure in one go.
1089                  */
1090
1091                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1092                                 .group_idx = (uint8_t)tbl8_group_index,
1093                                 .valid = VALID,
1094                                 .valid_group = 1,
1095                                 .depth = 0,
1096                 };
1097
1098                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1099
1100         } else { /*
1101                 * If it is valid, extended entry calculate the index into tbl8.
1102                 */
1103                 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1104                 tbl8_group_start = tbl8_group_index *
1105                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1106                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1107
1108                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1109
1110                         if (!lpm->tbl8[i].valid ||
1111                                         lpm->tbl8[i].depth <= depth) {
1112                                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1113                                         .valid = VALID,
1114                                         .depth = depth,
1115                                         .next_hop = next_hop,
1116                                         .valid_group = lpm->tbl8[i].valid_group,
1117                                 };
1118
1119                                 /*
1120                                  * Setting tbl8 entry in one go to avoid race
1121                                  * condition
1122                                  */
1123                                 lpm->tbl8[i] = new_tbl8_entry;
1124
1125                                 continue;
1126                         }
1127                 }
1128         }
1129 #undef group_idx
1130         return 0;
1131 }
1132
1133 /*
1134  * Add a route
1135  */
1136 int
1137 rte_lpm_add_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1138                 uint8_t next_hop)
1139 {
1140         int32_t rule_index, status = 0;
1141         uint32_t ip_masked;
1142
1143         /* Check user arguments. */
1144         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1145                 return -EINVAL;
1146
1147         ip_masked = ip & depth_to_mask(depth);
1148
1149         /* Add the rule to the rule table. */
1150         rule_index = rule_add_v20(lpm, ip_masked, depth, next_hop);
1151
1152         /* If the is no space available for new rule return error. */
1153         if (rule_index < 0) {
1154                 return rule_index;
1155         }
1156
1157         if (depth <= MAX_DEPTH_TBL24) {
1158                 status = add_depth_small_v20(lpm, ip_masked, depth, next_hop);
1159         } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1160                 status = add_depth_big_v20(lpm, ip_masked, depth, next_hop);
1161
1162                 /*
1163                  * If add fails due to exhaustion of tbl8 extensions delete
1164                  * rule that was added to rule table.
1165                  */
1166                 if (status < 0) {
1167                         rule_delete_v20(lpm, rule_index, depth);
1168
1169                         return status;
1170                 }
1171         }
1172
1173         return 0;
1174 }
1175 VERSION_SYMBOL(rte_lpm_add, _v20, 2.0);
1176
1177 int
1178 rte_lpm_add_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1179                 uint32_t next_hop)
1180 {
1181         int32_t rule_index, status = 0;
1182         uint32_t ip_masked;
1183
1184         /* Check user arguments. */
1185         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1186                 return -EINVAL;
1187
1188         ip_masked = ip & depth_to_mask(depth);
1189
1190         /* Add the rule to the rule table. */
1191         rule_index = rule_add_v1604(lpm, ip_masked, depth, next_hop);
1192
1193         /* If the is no space available for new rule return error. */
1194         if (rule_index < 0) {
1195                 return rule_index;
1196         }
1197
1198         if (depth <= MAX_DEPTH_TBL24) {
1199                 status = add_depth_small_v1604(lpm, ip_masked, depth, next_hop);
1200         } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
1201                 status = add_depth_big_v1604(lpm, ip_masked, depth, next_hop);
1202
1203                 /*
1204                  * If add fails due to exhaustion of tbl8 extensions delete
1205                  * rule that was added to rule table.
1206                  */
1207                 if (status < 0) {
1208                         rule_delete_v1604(lpm, rule_index, depth);
1209
1210                         return status;
1211                 }
1212         }
1213
1214         return 0;
1215 }
1216 BIND_DEFAULT_SYMBOL(rte_lpm_add, _v1604, 16.04);
1217 MAP_STATIC_SYMBOL(int rte_lpm_add(struct rte_lpm *lpm, uint32_t ip,
1218                 uint8_t depth, uint32_t next_hop), rte_lpm_add_v1604);
1219
1220 /*
1221  * Look for a rule in the high-level rules table
1222  */
1223 int
1224 rte_lpm_is_rule_present_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1225 uint8_t *next_hop)
1226 {
1227         uint32_t ip_masked;
1228         int32_t rule_index;
1229
1230         /* Check user arguments. */
1231         if ((lpm == NULL) ||
1232                 (next_hop == NULL) ||
1233                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1234                 return -EINVAL;
1235
1236         /* Look for the rule using rule_find. */
1237         ip_masked = ip & depth_to_mask(depth);
1238         rule_index = rule_find_v20(lpm, ip_masked, depth);
1239
1240         if (rule_index >= 0) {
1241                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1242                 return 1;
1243         }
1244
1245         /* If rule is not found return 0. */
1246         return 0;
1247 }
1248 VERSION_SYMBOL(rte_lpm_is_rule_present, _v20, 2.0);
1249
1250 int
1251 rte_lpm_is_rule_present_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1252 uint32_t *next_hop)
1253 {
1254         uint32_t ip_masked;
1255         int32_t rule_index;
1256
1257         /* Check user arguments. */
1258         if ((lpm == NULL) ||
1259                 (next_hop == NULL) ||
1260                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
1261                 return -EINVAL;
1262
1263         /* Look for the rule using rule_find. */
1264         ip_masked = ip & depth_to_mask(depth);
1265         rule_index = rule_find_v1604(lpm, ip_masked, depth);
1266
1267         if (rule_index >= 0) {
1268                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
1269                 return 1;
1270         }
1271
1272         /* If rule is not found return 0. */
1273         return 0;
1274 }
1275 BIND_DEFAULT_SYMBOL(rte_lpm_is_rule_present, _v1604, 16.04);
1276 MAP_STATIC_SYMBOL(int rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip,
1277                 uint8_t depth, uint32_t *next_hop), rte_lpm_is_rule_present_v1604);
1278
1279 static inline int32_t
1280 find_previous_rule_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth,
1281                 uint8_t *sub_rule_depth)
1282 {
1283         int32_t rule_index;
1284         uint32_t ip_masked;
1285         uint8_t prev_depth;
1286
1287         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1288                 ip_masked = ip & depth_to_mask(prev_depth);
1289
1290                 rule_index = rule_find_v20(lpm, ip_masked, prev_depth);
1291
1292                 if (rule_index >= 0) {
1293                         *sub_rule_depth = prev_depth;
1294                         return rule_index;
1295                 }
1296         }
1297
1298         return -1;
1299 }
1300
1301 static inline int32_t
1302 find_previous_rule_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
1303                 uint8_t *sub_rule_depth)
1304 {
1305         int32_t rule_index;
1306         uint32_t ip_masked;
1307         uint8_t prev_depth;
1308
1309         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
1310                 ip_masked = ip & depth_to_mask(prev_depth);
1311
1312                 rule_index = rule_find_v1604(lpm, ip_masked, prev_depth);
1313
1314                 if (rule_index >= 0) {
1315                         *sub_rule_depth = prev_depth;
1316                         return rule_index;
1317                 }
1318         }
1319
1320         return -1;
1321 }
1322
1323 static inline int32_t
1324 delete_depth_small_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1325         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1326 {
1327         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1328
1329         /* Calculate the range and index into Table24. */
1330         tbl24_range = depth_to_range(depth);
1331         tbl24_index = (ip_masked >> 8);
1332
1333         /*
1334          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1335          * and a positive number indicates a sub_rule_index.
1336          */
1337         if (sub_rule_index < 0) {
1338                 /*
1339                  * If no replacement rule exists then invalidate entries
1340                  * associated with this rule.
1341                  */
1342                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1343
1344                         if (lpm->tbl24[i].valid_group == 0 &&
1345                                         lpm->tbl24[i].depth <= depth) {
1346                                 lpm->tbl24[i].valid = INVALID;
1347                         } else if (lpm->tbl24[i].valid_group == 1) {
1348                                 /*
1349                                  * If TBL24 entry is extended, then there has
1350                                  * to be a rule with depth >= 25 in the
1351                                  * associated TBL8 group.
1352                                  */
1353
1354                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1355                                 tbl8_index = tbl8_group_index *
1356                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1357
1358                                 for (j = tbl8_index; j < (tbl8_index +
1359                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1360
1361                                         if (lpm->tbl8[j].depth <= depth)
1362                                                 lpm->tbl8[j].valid = INVALID;
1363                                 }
1364                         }
1365                 }
1366         } else {
1367                 /*
1368                  * If a replacement rule exists then modify entries
1369                  * associated with this rule.
1370                  */
1371
1372                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1373                         {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
1374                         .valid = VALID,
1375                         .valid_group = 0,
1376                         .depth = sub_rule_depth,
1377                 };
1378
1379                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1380                         .valid = VALID,
1381                         .valid_group = VALID,
1382                         .depth = sub_rule_depth,
1383                 };
1384                 new_tbl8_entry.next_hop =
1385                                 lpm->rules_tbl[sub_rule_index].next_hop;
1386
1387                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1388
1389                         if (lpm->tbl24[i].valid_group == 0 &&
1390                                         lpm->tbl24[i].depth <= depth) {
1391                                 lpm->tbl24[i] = new_tbl24_entry;
1392                         } else  if (lpm->tbl24[i].valid_group == 1) {
1393                                 /*
1394                                  * If TBL24 entry is extended, then there has
1395                                  * to be a rule with depth >= 25 in the
1396                                  * associated TBL8 group.
1397                                  */
1398
1399                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1400                                 tbl8_index = tbl8_group_index *
1401                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1402
1403                                 for (j = tbl8_index; j < (tbl8_index +
1404                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1405
1406                                         if (lpm->tbl8[j].depth <= depth)
1407                                                 lpm->tbl8[j] = new_tbl8_entry;
1408                                 }
1409                         }
1410                 }
1411         }
1412
1413         return 0;
1414 }
1415
1416 static inline int32_t
1417 delete_depth_small_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1418         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1419 {
1420 #define group_idx next_hop
1421         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
1422
1423         /* Calculate the range and index into Table24. */
1424         tbl24_range = depth_to_range(depth);
1425         tbl24_index = (ip_masked >> 8);
1426
1427         /*
1428          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
1429          * and a positive number indicates a sub_rule_index.
1430          */
1431         if (sub_rule_index < 0) {
1432                 /*
1433                  * If no replacement rule exists then invalidate entries
1434                  * associated with this rule.
1435                  */
1436                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1437
1438                         if (lpm->tbl24[i].valid_group == 0 &&
1439                                         lpm->tbl24[i].depth <= depth) {
1440                                 lpm->tbl24[i].valid = INVALID;
1441                         } else if (lpm->tbl24[i].valid_group == 1) {
1442                                 /*
1443                                  * If TBL24 entry is extended, then there has
1444                                  * to be a rule with depth >= 25 in the
1445                                  * associated TBL8 group.
1446                                  */
1447
1448                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1449                                 tbl8_index = tbl8_group_index *
1450                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1451
1452                                 for (j = tbl8_index; j < (tbl8_index +
1453                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1454
1455                                         if (lpm->tbl8[j].depth <= depth)
1456                                                 lpm->tbl8[j].valid = INVALID;
1457                                 }
1458                         }
1459                 }
1460         } else {
1461                 /*
1462                  * If a replacement rule exists then modify entries
1463                  * associated with this rule.
1464                  */
1465
1466                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1467                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1468                         .valid = VALID,
1469                         .valid_group = 0,
1470                         .depth = sub_rule_depth,
1471                 };
1472
1473                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1474                         .valid = VALID,
1475                         .valid_group = VALID,
1476                         .depth = sub_rule_depth,
1477                         .next_hop = lpm->rules_tbl
1478                         [sub_rule_index].next_hop,
1479                 };
1480
1481                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
1482
1483                         if (lpm->tbl24[i].valid_group == 0 &&
1484                                         lpm->tbl24[i].depth <= depth) {
1485                                 lpm->tbl24[i] = new_tbl24_entry;
1486                         } else  if (lpm->tbl24[i].valid_group == 1) {
1487                                 /*
1488                                  * If TBL24 entry is extended, then there has
1489                                  * to be a rule with depth >= 25 in the
1490                                  * associated TBL8 group.
1491                                  */
1492
1493                                 tbl8_group_index = lpm->tbl24[i].group_idx;
1494                                 tbl8_index = tbl8_group_index *
1495                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1496
1497                                 for (j = tbl8_index; j < (tbl8_index +
1498                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
1499
1500                                         if (lpm->tbl8[j].depth <= depth)
1501                                                 lpm->tbl8[j] = new_tbl8_entry;
1502                                 }
1503                         }
1504                 }
1505         }
1506 #undef group_idx
1507         return 0;
1508 }
1509
1510 /*
1511  * Checks if table 8 group can be recycled.
1512  *
1513  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
1514  * Return of -EINVAL means tbl8 is empty and thus can be recycled
1515  * Return of value > -1 means tbl8 is in use but has all the same values and
1516  * thus can be recycled
1517  */
1518 static inline int32_t
1519 tbl8_recycle_check_v20(struct rte_lpm_tbl_entry_v20 *tbl8,
1520                 uint32_t tbl8_group_start)
1521 {
1522         uint32_t tbl8_group_end, i;
1523         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1524
1525         /*
1526          * Check the first entry of the given tbl8. If it is invalid we know
1527          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1528          *  (As they would affect all entries in a tbl8) and thus this table
1529          *  can not be recycled.
1530          */
1531         if (tbl8[tbl8_group_start].valid) {
1532                 /*
1533                  * If first entry is valid check if the depth is less than 24
1534                  * and if so check the rest of the entries to verify that they
1535                  * are all of this depth.
1536                  */
1537                 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
1538                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1539                                         i++) {
1540
1541                                 if (tbl8[i].depth !=
1542                                                 tbl8[tbl8_group_start].depth) {
1543
1544                                         return -EEXIST;
1545                                 }
1546                         }
1547                         /* If all entries are the same return the tb8 index */
1548                         return tbl8_group_start;
1549                 }
1550
1551                 return -EEXIST;
1552         }
1553         /*
1554          * If the first entry is invalid check if the rest of the entries in
1555          * the tbl8 are invalid.
1556          */
1557         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1558                 if (tbl8[i].valid)
1559                         return -EEXIST;
1560         }
1561         /* If no valid entries are found then return -EINVAL. */
1562         return -EINVAL;
1563 }
1564
1565 static inline int32_t
1566 tbl8_recycle_check_v1604(struct rte_lpm_tbl_entry *tbl8,
1567                 uint32_t tbl8_group_start)
1568 {
1569         uint32_t tbl8_group_end, i;
1570         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1571
1572         /*
1573          * Check the first entry of the given tbl8. If it is invalid we know
1574          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1575          *  (As they would affect all entries in a tbl8) and thus this table
1576          *  can not be recycled.
1577          */
1578         if (tbl8[tbl8_group_start].valid) {
1579                 /*
1580                  * If first entry is valid check if the depth is less than 24
1581                  * and if so check the rest of the entries to verify that they
1582                  * are all of this depth.
1583                  */
1584                 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
1585                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1586                                         i++) {
1587
1588                                 if (tbl8[i].depth !=
1589                                                 tbl8[tbl8_group_start].depth) {
1590
1591                                         return -EEXIST;
1592                                 }
1593                         }
1594                         /* If all entries are the same return the tb8 index */
1595                         return tbl8_group_start;
1596                 }
1597
1598                 return -EEXIST;
1599         }
1600         /*
1601          * If the first entry is invalid check if the rest of the entries in
1602          * the tbl8 are invalid.
1603          */
1604         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1605                 if (tbl8[i].valid)
1606                         return -EEXIST;
1607         }
1608         /* If no valid entries are found then return -EINVAL. */
1609         return -EINVAL;
1610 }
1611
1612 static inline int32_t
1613 delete_depth_big_v20(struct rte_lpm_v20 *lpm, uint32_t ip_masked,
1614         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1615 {
1616         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1617                         tbl8_range, i;
1618         int32_t tbl8_recycle_index;
1619
1620         /*
1621          * Calculate the index into tbl24 and range. Note: All depths larger
1622          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1623          */
1624         tbl24_index = ip_masked >> 8;
1625
1626         /* Calculate the index into tbl8 and range. */
1627         tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1628         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1629         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1630         tbl8_range = depth_to_range(depth);
1631
1632         if (sub_rule_index < 0) {
1633                 /*
1634                  * Loop through the range of entries on tbl8 for which the
1635                  * rule_to_delete must be removed or modified.
1636                  */
1637                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1638                         if (lpm->tbl8[i].depth <= depth)
1639                                 lpm->tbl8[i].valid = INVALID;
1640                 }
1641         } else {
1642                 /* Set new tbl8 entry. */
1643                 struct rte_lpm_tbl_entry_v20 new_tbl8_entry = {
1644                         .valid = VALID,
1645                         .depth = sub_rule_depth,
1646                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1647                 };
1648
1649                 new_tbl8_entry.next_hop =
1650                                 lpm->rules_tbl[sub_rule_index].next_hop;
1651                 /*
1652                  * Loop through the range of entries on tbl8 for which the
1653                  * rule_to_delete must be modified.
1654                  */
1655                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1656                         if (lpm->tbl8[i].depth <= depth)
1657                                 lpm->tbl8[i] = new_tbl8_entry;
1658                 }
1659         }
1660
1661         /*
1662          * Check if there are any valid entries in this tbl8 group. If all
1663          * tbl8 entries are invalid we can free the tbl8 and invalidate the
1664          * associated tbl24 entry.
1665          */
1666
1667         tbl8_recycle_index = tbl8_recycle_check_v20(lpm->tbl8, tbl8_group_start);
1668
1669         if (tbl8_recycle_index == -EINVAL) {
1670                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1671                 lpm->tbl24[tbl24_index].valid = 0;
1672                 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1673         } else if (tbl8_recycle_index > -1) {
1674                 /* Update tbl24 entry. */
1675                 struct rte_lpm_tbl_entry_v20 new_tbl24_entry = {
1676                         { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
1677                         .valid = VALID,
1678                         .valid_group = 0,
1679                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
1680                 };
1681
1682                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1683                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1684                 tbl8_free_v20(lpm->tbl8, tbl8_group_start);
1685         }
1686
1687         return 0;
1688 }
1689
1690 static inline int32_t
1691 delete_depth_big_v1604(struct rte_lpm *lpm, uint32_t ip_masked,
1692         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1693 {
1694 #define group_idx next_hop
1695         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1696                         tbl8_range, i;
1697         int32_t tbl8_recycle_index;
1698
1699         /*
1700          * Calculate the index into tbl24 and range. Note: All depths larger
1701          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1702          */
1703         tbl24_index = ip_masked >> 8;
1704
1705         /* Calculate the index into tbl8 and range. */
1706         tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1707         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1708         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1709         tbl8_range = depth_to_range(depth);
1710
1711         if (sub_rule_index < 0) {
1712                 /*
1713                  * Loop through the range of entries on tbl8 for which the
1714                  * rule_to_delete must be removed or modified.
1715                  */
1716                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1717                         if (lpm->tbl8[i].depth <= depth)
1718                                 lpm->tbl8[i].valid = INVALID;
1719                 }
1720         } else {
1721                 /* Set new tbl8 entry. */
1722                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1723                         .valid = VALID,
1724                         .depth = sub_rule_depth,
1725                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1726                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1727                 };
1728
1729                 /*
1730                  * Loop through the range of entries on tbl8 for which the
1731                  * rule_to_delete must be modified.
1732                  */
1733                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1734                         if (lpm->tbl8[i].depth <= depth)
1735                                 lpm->tbl8[i] = new_tbl8_entry;
1736                 }
1737         }
1738
1739         /*
1740          * Check if there are any valid entries in this tbl8 group. If all
1741          * tbl8 entries are invalid we can free the tbl8 and invalidate the
1742          * associated tbl24 entry.
1743          */
1744
1745         tbl8_recycle_index = tbl8_recycle_check_v1604(lpm->tbl8, tbl8_group_start);
1746
1747         if (tbl8_recycle_index == -EINVAL) {
1748                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1749                 lpm->tbl24[tbl24_index].valid = 0;
1750                 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1751         } else if (tbl8_recycle_index > -1) {
1752                 /* Update tbl24 entry. */
1753                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1754                         .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1755                         .valid = VALID,
1756                         .valid_group = 0,
1757                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
1758                 };
1759
1760                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
1761                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
1762                 tbl8_free_v1604(lpm->tbl8, tbl8_group_start);
1763         }
1764 #undef group_idx
1765         return 0;
1766 }
1767
1768 /*
1769  * Deletes a rule
1770  */
1771 int
1772 rte_lpm_delete_v20(struct rte_lpm_v20 *lpm, uint32_t ip, uint8_t depth)
1773 {
1774         int32_t rule_to_delete_index, sub_rule_index;
1775         uint32_t ip_masked;
1776         uint8_t sub_rule_depth;
1777         /*
1778          * Check input arguments. Note: IP must be a positive integer of 32
1779          * bits in length therefore it need not be checked.
1780          */
1781         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1782                 return -EINVAL;
1783         }
1784
1785         ip_masked = ip & depth_to_mask(depth);
1786
1787         /*
1788          * Find the index of the input rule, that needs to be deleted, in the
1789          * rule table.
1790          */
1791         rule_to_delete_index = rule_find_v20(lpm, ip_masked, depth);
1792
1793         /*
1794          * Check if rule_to_delete_index was found. If no rule was found the
1795          * function rule_find returns -EINVAL.
1796          */
1797         if (rule_to_delete_index < 0)
1798                 return -EINVAL;
1799
1800         /* Delete the rule from the rule table. */
1801         rule_delete_v20(lpm, rule_to_delete_index, depth);
1802
1803         /*
1804          * Find rule to replace the rule_to_delete. If there is no rule to
1805          * replace the rule_to_delete we return -1 and invalidate the table
1806          * entries associated with this rule.
1807          */
1808         sub_rule_depth = 0;
1809         sub_rule_index = find_previous_rule_v20(lpm, ip, depth, &sub_rule_depth);
1810
1811         /*
1812          * If the input depth value is less than 25 use function
1813          * delete_depth_small otherwise use delete_depth_big.
1814          */
1815         if (depth <= MAX_DEPTH_TBL24) {
1816                 return delete_depth_small_v20(lpm, ip_masked, depth,
1817                                 sub_rule_index, sub_rule_depth);
1818         } else { /* If depth > MAX_DEPTH_TBL24 */
1819                 return delete_depth_big_v20(lpm, ip_masked, depth, sub_rule_index,
1820                                 sub_rule_depth);
1821         }
1822 }
1823 VERSION_SYMBOL(rte_lpm_delete, _v20, 2.0);
1824
1825 int
1826 rte_lpm_delete_v1604(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1827 {
1828         int32_t rule_to_delete_index, sub_rule_index;
1829         uint32_t ip_masked;
1830         uint8_t sub_rule_depth;
1831         /*
1832          * Check input arguments. Note: IP must be a positive integer of 32
1833          * bits in length therefore it need not be checked.
1834          */
1835         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1836                 return -EINVAL;
1837         }
1838
1839         ip_masked = ip & depth_to_mask(depth);
1840
1841         /*
1842          * Find the index of the input rule, that needs to be deleted, in the
1843          * rule table.
1844          */
1845         rule_to_delete_index = rule_find_v1604(lpm, ip_masked, depth);
1846
1847         /*
1848          * Check if rule_to_delete_index was found. If no rule was found the
1849          * function rule_find returns -EINVAL.
1850          */
1851         if (rule_to_delete_index < 0)
1852                 return -EINVAL;
1853
1854         /* Delete the rule from the rule table. */
1855         rule_delete_v1604(lpm, rule_to_delete_index, depth);
1856
1857         /*
1858          * Find rule to replace the rule_to_delete. If there is no rule to
1859          * replace the rule_to_delete we return -1 and invalidate the table
1860          * entries associated with this rule.
1861          */
1862         sub_rule_depth = 0;
1863         sub_rule_index = find_previous_rule_v1604(lpm, ip, depth, &sub_rule_depth);
1864
1865         /*
1866          * If the input depth value is less than 25 use function
1867          * delete_depth_small otherwise use delete_depth_big.
1868          */
1869         if (depth <= MAX_DEPTH_TBL24) {
1870                 return delete_depth_small_v1604(lpm, ip_masked, depth,
1871                                 sub_rule_index, sub_rule_depth);
1872         } else { /* If depth > MAX_DEPTH_TBL24 */
1873                 return delete_depth_big_v1604(lpm, ip_masked, depth, sub_rule_index,
1874                                 sub_rule_depth);
1875         }
1876 }
1877 BIND_DEFAULT_SYMBOL(rte_lpm_delete, _v1604, 16.04);
1878 MAP_STATIC_SYMBOL(int rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip,
1879                 uint8_t depth), rte_lpm_delete_v1604);
1880
1881 /*
1882  * Delete all rules from the LPM table.
1883  */
1884 void
1885 rte_lpm_delete_all_v20(struct rte_lpm_v20 *lpm)
1886 {
1887         /* Zero rule information. */
1888         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1889
1890         /* Zero tbl24. */
1891         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1892
1893         /* Zero tbl8. */
1894         memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1895
1896         /* Delete all rules form the rules table. */
1897         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1898 }
1899 VERSION_SYMBOL(rte_lpm_delete_all, _v20, 2.0);
1900
1901 void
1902 rte_lpm_delete_all_v1604(struct rte_lpm *lpm)
1903 {
1904         /* Zero rule information. */
1905         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1906
1907         /* Zero tbl24. */
1908         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1909
1910         /* Zero tbl8. */
1911         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1912                         * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1913
1914         /* Delete all rules form the rules table. */
1915         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1916 }
1917 BIND_DEFAULT_SYMBOL(rte_lpm_delete_all, _v1604, 16.04);
1918 MAP_STATIC_SYMBOL(void rte_lpm_delete_all(struct rte_lpm *lpm),
1919                 rte_lpm_delete_all_v1604);