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