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