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