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