126fc5a82dbd41c8419d3d76103f65bc3993a5e1
[dpdk.git] / lib / librte_lpm / rte_lpm.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  * Copyright(c) 2020 Arm Limited
4  */
5
6 #include <string.h>
7 #include <stdint.h>
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12
13 #include <rte_log.h>
14 #include <rte_branch_prediction.h>
15 #include <rte_common.h>
16 #include <rte_memory.h>        /* for definition of RTE_CACHE_LINE_SIZE */
17 #include <rte_malloc.h>
18 #include <rte_eal.h>
19 #include <rte_eal_memconfig.h>
20 #include <rte_per_lcore.h>
21 #include <rte_string_fns.h>
22 #include <rte_errno.h>
23 #include <rte_rwlock.h>
24 #include <rte_spinlock.h>
25 #include <rte_tailq.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 /** @internal LPM structure. */
44 struct __rte_lpm {
45         /* LPM metadata. */
46         struct rte_lpm lpm;
47
48         /* RCU config. */
49         struct rte_rcu_qsbr *v;         /* RCU QSBR variable. */
50         enum rte_lpm_qsbr_mode rcu_mode;/* Blocking, defer queue. */
51         struct rte_rcu_qsbr_dq *dq;     /* RCU QSBR defer queue. */
52 };
53
54 /* Macro to enable/disable run-time checks. */
55 #if defined(RTE_LIBRTE_LPM_DEBUG)
56 #include <rte_debug.h>
57 #define VERIFY_DEPTH(depth) do {                                \
58         if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH))        \
59                 rte_panic("LPM: Invalid depth (%u) at line %d", \
60                                 (unsigned)(depth), __LINE__);   \
61 } while (0)
62 #else
63 #define VERIFY_DEPTH(depth)
64 #endif
65
66 /*
67  * Converts a given depth value to its corresponding mask value.
68  *
69  * depth  (IN)          : range = 1 - 32
70  * mask   (OUT)         : 32bit mask
71  */
72 static uint32_t __attribute__((pure))
73 depth_to_mask(uint8_t depth)
74 {
75         VERIFY_DEPTH(depth);
76
77         /* To calculate a mask start with a 1 on the left hand side and right
78          * shift while populating the left hand side with 1's
79          */
80         return (int)0x80000000 >> (depth - 1);
81 }
82
83 /*
84  * Converts given depth value to its corresponding range value.
85  */
86 static uint32_t __attribute__((pure))
87 depth_to_range(uint8_t depth)
88 {
89         VERIFY_DEPTH(depth);
90
91         /*
92          * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
93          */
94         if (depth <= MAX_DEPTH_TBL24)
95                 return 1 << (MAX_DEPTH_TBL24 - depth);
96
97         /* Else if depth is greater than 24 */
98         return 1 << (RTE_LPM_MAX_DEPTH - depth);
99 }
100
101 /*
102  * Find an existing lpm table and return a pointer to it.
103  */
104 struct rte_lpm *
105 rte_lpm_find_existing(const char *name)
106 {
107         struct rte_lpm *l = NULL;
108         struct rte_tailq_entry *te;
109         struct rte_lpm_list *lpm_list;
110
111         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
112
113         rte_mcfg_tailq_read_lock();
114         TAILQ_FOREACH(te, lpm_list, next) {
115                 l = te->data;
116                 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
117                         break;
118         }
119         rte_mcfg_tailq_read_unlock();
120
121         if (te == NULL) {
122                 rte_errno = ENOENT;
123                 return NULL;
124         }
125
126         return l;
127 }
128
129 /*
130  * Allocates memory for LPM object
131  */
132 struct rte_lpm *
133 rte_lpm_create(const char *name, int socket_id,
134                 const struct rte_lpm_config *config)
135 {
136         char mem_name[RTE_LPM_NAMESIZE];
137         struct __rte_lpm *internal_lpm;
138         struct rte_lpm *lpm = NULL;
139         struct rte_tailq_entry *te;
140         uint32_t mem_size, rules_size, tbl8s_size;
141         struct rte_lpm_list *lpm_list;
142
143         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
144
145         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl_entry) != 4);
146
147         /* Check user arguments. */
148         if ((name == NULL) || (socket_id < -1) || (config->max_rules == 0)
149                         || config->number_tbl8s > RTE_LPM_MAX_TBL8_NUM_GROUPS) {
150                 rte_errno = EINVAL;
151                 return NULL;
152         }
153
154         snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
155
156         rte_mcfg_tailq_write_lock();
157
158         /* guarantee there's no existing */
159         TAILQ_FOREACH(te, lpm_list, next) {
160                 lpm = te->data;
161                 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
162                         break;
163         }
164
165         if (te != NULL) {
166                 lpm = NULL;
167                 rte_errno = EEXIST;
168                 goto exit;
169         }
170
171         /* Determine the amount of memory to allocate. */
172         mem_size = sizeof(*internal_lpm);
173         rules_size = sizeof(struct rte_lpm_rule) * config->max_rules;
174         tbl8s_size = sizeof(struct rte_lpm_tbl_entry) *
175                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES * config->number_tbl8s;
176
177         /* allocate tailq entry */
178         te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
179         if (te == NULL) {
180                 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
181                 rte_errno = ENOMEM;
182                 goto exit;
183         }
184
185         /* Allocate memory to store the LPM data structures. */
186         internal_lpm = rte_zmalloc_socket(mem_name, mem_size,
187                         RTE_CACHE_LINE_SIZE, socket_id);
188         if (internal_lpm == NULL) {
189                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
190                 rte_free(te);
191                 rte_errno = ENOMEM;
192                 goto exit;
193         }
194
195         lpm = &internal_lpm->lpm;
196         lpm->rules_tbl = rte_zmalloc_socket(NULL,
197                         (size_t)rules_size, RTE_CACHE_LINE_SIZE, socket_id);
198
199         if (lpm->rules_tbl == NULL) {
200                 RTE_LOG(ERR, LPM, "LPM rules_tbl memory allocation failed\n");
201                 rte_free(internal_lpm);
202                 internal_lpm = NULL;
203                 lpm = NULL;
204                 rte_free(te);
205                 rte_errno = ENOMEM;
206                 goto exit;
207         }
208
209         lpm->tbl8 = rte_zmalloc_socket(NULL,
210                         (size_t)tbl8s_size, RTE_CACHE_LINE_SIZE, socket_id);
211
212         if (lpm->tbl8 == NULL) {
213                 RTE_LOG(ERR, LPM, "LPM tbl8 memory allocation failed\n");
214                 rte_free(lpm->rules_tbl);
215                 rte_free(internal_lpm);
216                 internal_lpm = NULL;
217                 lpm = NULL;
218                 rte_free(te);
219                 rte_errno = ENOMEM;
220                 goto exit;
221         }
222
223         /* Save user arguments. */
224         lpm->max_rules = config->max_rules;
225         lpm->number_tbl8s = config->number_tbl8s;
226         strlcpy(lpm->name, name, sizeof(lpm->name));
227
228         te->data = lpm;
229
230         TAILQ_INSERT_TAIL(lpm_list, te, next);
231
232 exit:
233         rte_mcfg_tailq_write_unlock();
234
235         return lpm;
236 }
237
238 /*
239  * Deallocates memory for given LPM table.
240  */
241 void
242 rte_lpm_free(struct rte_lpm *lpm)
243 {
244         struct __rte_lpm *internal_lpm;
245         struct rte_lpm_list *lpm_list;
246         struct rte_tailq_entry *te;
247
248         /* Check user arguments. */
249         if (lpm == NULL)
250                 return;
251
252         lpm_list = RTE_TAILQ_CAST(rte_lpm_tailq.head, rte_lpm_list);
253
254         rte_mcfg_tailq_write_lock();
255
256         /* find our tailq entry */
257         TAILQ_FOREACH(te, lpm_list, next) {
258                 if (te->data == (void *) lpm)
259                         break;
260         }
261         if (te != NULL)
262                 TAILQ_REMOVE(lpm_list, te, next);
263
264         rte_mcfg_tailq_write_unlock();
265
266         internal_lpm = container_of(lpm, struct __rte_lpm, lpm);
267         if (internal_lpm->dq != NULL)
268                 rte_rcu_qsbr_dq_delete(internal_lpm->dq);
269         rte_free(lpm->tbl8);
270         rte_free(lpm->rules_tbl);
271         rte_free(lpm);
272         rte_free(te);
273 }
274
275 static void
276 __lpm_rcu_qsbr_free_resource(void *p, void *data, unsigned int n)
277 {
278         struct rte_lpm_tbl_entry *tbl8 = ((struct rte_lpm *)p)->tbl8;
279         struct rte_lpm_tbl_entry zero_tbl8_entry = {0};
280         uint32_t tbl8_group_index = *(uint32_t *)data;
281
282         RTE_SET_USED(n);
283         /* Set tbl8 group invalid */
284         __atomic_store(&tbl8[tbl8_group_index], &zero_tbl8_entry,
285                 __ATOMIC_RELAXED);
286 }
287
288 /* Associate QSBR variable with an LPM object.
289  */
290 int
291 rte_lpm_rcu_qsbr_add(struct rte_lpm *lpm, struct rte_lpm_rcu_config *cfg,
292         struct rte_rcu_qsbr_dq **dq)
293 {
294         struct rte_rcu_qsbr_dq_parameters params = {0};
295         char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
296         struct __rte_lpm *internal_lpm;
297
298         if (lpm == NULL || cfg == NULL) {
299                 rte_errno = EINVAL;
300                 return 1;
301         }
302
303         internal_lpm = container_of(lpm, struct __rte_lpm, lpm);
304         if (internal_lpm->v != NULL) {
305                 rte_errno = EEXIST;
306                 return 1;
307         }
308
309         if (cfg->mode == RTE_LPM_QSBR_MODE_SYNC) {
310                 /* No other things to do. */
311         } else if (cfg->mode == RTE_LPM_QSBR_MODE_DQ) {
312                 /* Init QSBR defer queue. */
313                 snprintf(rcu_dq_name, sizeof(rcu_dq_name),
314                                 "LPM_RCU_%s", lpm->name);
315                 params.name = rcu_dq_name;
316                 params.size = cfg->dq_size;
317                 if (params.size == 0)
318                         params.size = lpm->number_tbl8s;
319                 params.trigger_reclaim_limit = cfg->reclaim_thd;
320                 params.max_reclaim_size = cfg->reclaim_max;
321                 if (params.max_reclaim_size == 0)
322                         params.max_reclaim_size = RTE_LPM_RCU_DQ_RECLAIM_MAX;
323                 params.esize = sizeof(uint32_t);        /* tbl8 group index */
324                 params.free_fn = __lpm_rcu_qsbr_free_resource;
325                 params.p = lpm;
326                 params.v = cfg->v;
327                 internal_lpm->dq = rte_rcu_qsbr_dq_create(&params);
328                 if (internal_lpm->dq == NULL) {
329                         RTE_LOG(ERR, LPM, "LPM defer queue creation failed\n");
330                         return 1;
331                 }
332                 if (dq != NULL)
333                         *dq = internal_lpm->dq;
334         } else {
335                 rte_errno = EINVAL;
336                 return 1;
337         }
338         internal_lpm->rcu_mode = cfg->mode;
339         internal_lpm->v = cfg->v;
340
341         return 0;
342 }
343
344 /*
345  * Adds a rule to the rule table.
346  *
347  * NOTE: The rule table is split into 32 groups. Each group contains rules that
348  * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
349  * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
350  * to refer to depth 1 because even though the depth range is 1 - 32, depths
351  * are stored in the rule table from 0 - 31.
352  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
353  */
354 static int32_t
355 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
356         uint32_t next_hop)
357 {
358         uint32_t rule_gindex, rule_index, last_rule;
359         int i;
360
361         VERIFY_DEPTH(depth);
362
363         /* Scan through rule group to see if rule already exists. */
364         if (lpm->rule_info[depth - 1].used_rules > 0) {
365
366                 /* rule_gindex stands for rule group index. */
367                 rule_gindex = lpm->rule_info[depth - 1].first_rule;
368                 /* Initialise rule_index to point to start of rule group. */
369                 rule_index = rule_gindex;
370                 /* Last rule = Last used rule in this rule group. */
371                 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
372
373                 for (; rule_index < last_rule; rule_index++) {
374
375                         /* If rule already exists update next hop and return. */
376                         if (lpm->rules_tbl[rule_index].ip == ip_masked) {
377
378                                 if (lpm->rules_tbl[rule_index].next_hop
379                                                 == next_hop)
380                                         return -EEXIST;
381                                 lpm->rules_tbl[rule_index].next_hop = next_hop;
382
383                                 return rule_index;
384                         }
385                 }
386
387                 if (rule_index == lpm->max_rules)
388                         return -ENOSPC;
389         } else {
390                 /* Calculate the position in which the rule will be stored. */
391                 rule_index = 0;
392
393                 for (i = depth - 1; i > 0; i--) {
394                         if (lpm->rule_info[i - 1].used_rules > 0) {
395                                 rule_index = lpm->rule_info[i - 1].first_rule
396                                                 + lpm->rule_info[i - 1].used_rules;
397                                 break;
398                         }
399                 }
400                 if (rule_index == lpm->max_rules)
401                         return -ENOSPC;
402
403                 lpm->rule_info[depth - 1].first_rule = rule_index;
404         }
405
406         /* Make room for the new rule in the array. */
407         for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
408                 if (lpm->rule_info[i - 1].first_rule
409                                 + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
410                         return -ENOSPC;
411
412                 if (lpm->rule_info[i - 1].used_rules > 0) {
413                         lpm->rules_tbl[lpm->rule_info[i - 1].first_rule
414                                 + lpm->rule_info[i - 1].used_rules]
415                                         = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
416                         lpm->rule_info[i - 1].first_rule++;
417                 }
418         }
419
420         /* Add the new rule. */
421         lpm->rules_tbl[rule_index].ip = ip_masked;
422         lpm->rules_tbl[rule_index].next_hop = next_hop;
423
424         /* Increment the used rules counter for this rule group. */
425         lpm->rule_info[depth - 1].used_rules++;
426
427         return rule_index;
428 }
429
430 /*
431  * Delete a rule from the rule table.
432  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
433  */
434 static void
435 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
436 {
437         int i;
438
439         VERIFY_DEPTH(depth);
440
441         lpm->rules_tbl[rule_index] =
442                         lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
443                         + lpm->rule_info[depth - 1].used_rules - 1];
444
445         for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
446                 if (lpm->rule_info[i].used_rules > 0) {
447                         lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
448                                         lpm->rules_tbl[lpm->rule_info[i].first_rule
449                                                 + lpm->rule_info[i].used_rules - 1];
450                         lpm->rule_info[i].first_rule--;
451                 }
452         }
453
454         lpm->rule_info[depth - 1].used_rules--;
455 }
456
457 /*
458  * Finds a rule in rule table.
459  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
460  */
461 static int32_t
462 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
463 {
464         uint32_t rule_gindex, last_rule, rule_index;
465
466         VERIFY_DEPTH(depth);
467
468         rule_gindex = lpm->rule_info[depth - 1].first_rule;
469         last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
470
471         /* Scan used rules at given depth to find rule. */
472         for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
473                 /* If rule is found return the rule index. */
474                 if (lpm->rules_tbl[rule_index].ip == ip_masked)
475                         return rule_index;
476         }
477
478         /* If rule is not found return -EINVAL. */
479         return -EINVAL;
480 }
481
482 /*
483  * Find, clean and allocate a tbl8.
484  */
485 static int32_t
486 _tbl8_alloc(struct rte_lpm *lpm)
487 {
488         uint32_t group_idx; /* tbl8 group index. */
489         struct rte_lpm_tbl_entry *tbl8_entry;
490
491         /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
492         for (group_idx = 0; group_idx < lpm->number_tbl8s; group_idx++) {
493                 tbl8_entry = &lpm->tbl8[group_idx *
494                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
495                 /* If a free tbl8 group is found clean it and set as VALID. */
496                 if (!tbl8_entry->valid_group) {
497                         struct rte_lpm_tbl_entry new_tbl8_entry = {
498                                 .next_hop = 0,
499                                 .valid = INVALID,
500                                 .depth = 0,
501                                 .valid_group = VALID,
502                         };
503
504                         memset(&tbl8_entry[0], 0,
505                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
506                                         sizeof(tbl8_entry[0]));
507
508                         __atomic_store(tbl8_entry, &new_tbl8_entry,
509                                         __ATOMIC_RELAXED);
510
511                         /* Return group index for allocated tbl8 group. */
512                         return group_idx;
513                 }
514         }
515
516         /* If there are no tbl8 groups free then return error. */
517         return -ENOSPC;
518 }
519
520 static int32_t
521 tbl8_alloc(struct rte_lpm *lpm)
522 {
523         int32_t group_idx; /* tbl8 group index. */
524         struct __rte_lpm *internal_lpm;
525
526         internal_lpm = container_of(lpm, struct __rte_lpm, lpm);
527         group_idx = _tbl8_alloc(lpm);
528         if (group_idx == -ENOSPC && internal_lpm->dq != NULL) {
529                 /* If there are no tbl8 groups try to reclaim one. */
530                 if (rte_rcu_qsbr_dq_reclaim(internal_lpm->dq, 1,
531                                 NULL, NULL, NULL) == 0)
532                         group_idx = _tbl8_alloc(lpm);
533         }
534
535         return group_idx;
536 }
537
538 static void
539 tbl8_free(struct rte_lpm *lpm, uint32_t tbl8_group_start)
540 {
541         struct rte_lpm_tbl_entry zero_tbl8_entry = {0};
542         struct __rte_lpm *internal_lpm;
543
544         internal_lpm = container_of(lpm, struct __rte_lpm, lpm);
545         if (internal_lpm->v == NULL) {
546                 /* Set tbl8 group invalid*/
547                 __atomic_store(&lpm->tbl8[tbl8_group_start], &zero_tbl8_entry,
548                                 __ATOMIC_RELAXED);
549         } else if (internal_lpm->rcu_mode == RTE_LPM_QSBR_MODE_SYNC) {
550                 /* Wait for quiescent state change. */
551                 rte_rcu_qsbr_synchronize(internal_lpm->v,
552                         RTE_QSBR_THRID_INVALID);
553                 /* Set tbl8 group invalid*/
554                 __atomic_store(&lpm->tbl8[tbl8_group_start], &zero_tbl8_entry,
555                                 __ATOMIC_RELAXED);
556         } else if (internal_lpm->rcu_mode == RTE_LPM_QSBR_MODE_DQ) {
557                 /* Push into QSBR defer queue. */
558                 rte_rcu_qsbr_dq_enqueue(internal_lpm->dq,
559                                 (void *)&tbl8_group_start);
560         }
561 }
562
563 static __rte_noinline int32_t
564 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
565                 uint32_t next_hop)
566 {
567 #define group_idx next_hop
568         uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
569
570         /* Calculate the index into Table24. */
571         tbl24_index = ip >> 8;
572         tbl24_range = depth_to_range(depth);
573
574         for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
575                 /*
576                  * For invalid OR valid and non-extended tbl 24 entries set
577                  * entry.
578                  */
579                 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].valid_group == 0 &&
580                                 lpm->tbl24[i].depth <= depth)) {
581
582                         struct rte_lpm_tbl_entry new_tbl24_entry = {
583                                 .next_hop = next_hop,
584                                 .valid = VALID,
585                                 .valid_group = 0,
586                                 .depth = depth,
587                         };
588
589                         /* Setting tbl24 entry in one go to avoid race
590                          * conditions
591                          */
592                         __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
593                                         __ATOMIC_RELEASE);
594
595                         continue;
596                 }
597
598                 if (lpm->tbl24[i].valid_group == 1) {
599                         /* If tbl24 entry is valid and extended calculate the
600                          *  index into tbl8.
601                          */
602                         tbl8_index = lpm->tbl24[i].group_idx *
603                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
604                         tbl8_group_end = tbl8_index +
605                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
606
607                         for (j = tbl8_index; j < tbl8_group_end; j++) {
608                                 if (!lpm->tbl8[j].valid ||
609                                                 lpm->tbl8[j].depth <= depth) {
610                                         struct rte_lpm_tbl_entry
611                                                 new_tbl8_entry = {
612                                                 .valid = VALID,
613                                                 .valid_group = VALID,
614                                                 .depth = depth,
615                                                 .next_hop = next_hop,
616                                         };
617
618                                         /*
619                                          * Setting tbl8 entry in one go to avoid
620                                          * race conditions
621                                          */
622                                         __atomic_store(&lpm->tbl8[j],
623                                                 &new_tbl8_entry,
624                                                 __ATOMIC_RELAXED);
625
626                                         continue;
627                                 }
628                         }
629                 }
630         }
631 #undef group_idx
632         return 0;
633 }
634
635 static __rte_noinline int32_t
636 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
637                 uint32_t next_hop)
638 {
639 #define group_idx next_hop
640         uint32_t tbl24_index;
641         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
642                 tbl8_range, i;
643
644         tbl24_index = (ip_masked >> 8);
645         tbl8_range = depth_to_range(depth);
646
647         if (!lpm->tbl24[tbl24_index].valid) {
648                 /* Search for a free tbl8 group. */
649                 tbl8_group_index = tbl8_alloc(lpm);
650
651                 /* Check tbl8 allocation was successful. */
652                 if (tbl8_group_index < 0) {
653                         return tbl8_group_index;
654                 }
655
656                 /* Find index into tbl8 and range. */
657                 tbl8_index = (tbl8_group_index *
658                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
659                                 (ip_masked & 0xFF);
660
661                 /* Set tbl8 entry. */
662                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
663                         struct rte_lpm_tbl_entry new_tbl8_entry = {
664                                 .valid = VALID,
665                                 .depth = depth,
666                                 .valid_group = lpm->tbl8[i].valid_group,
667                                 .next_hop = next_hop,
668                         };
669                         __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
670                                         __ATOMIC_RELAXED);
671                 }
672
673                 /*
674                  * Update tbl24 entry to point to new tbl8 entry. Note: The
675                  * ext_flag and tbl8_index need to be updated simultaneously,
676                  * so assign whole structure in one go
677                  */
678
679                 struct rte_lpm_tbl_entry new_tbl24_entry = {
680                         .group_idx = tbl8_group_index,
681                         .valid = VALID,
682                         .valid_group = 1,
683                         .depth = 0,
684                 };
685
686                 /* The tbl24 entry must be written only after the
687                  * tbl8 entries are written.
688                  */
689                 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
690                                 __ATOMIC_RELEASE);
691
692         } /* If valid entry but not extended calculate the index into Table8. */
693         else if (lpm->tbl24[tbl24_index].valid_group == 0) {
694                 /* Search for free tbl8 group. */
695                 tbl8_group_index = tbl8_alloc(lpm);
696
697                 if (tbl8_group_index < 0) {
698                         return tbl8_group_index;
699                 }
700
701                 tbl8_group_start = tbl8_group_index *
702                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
703                 tbl8_group_end = tbl8_group_start +
704                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
705
706                 /* Populate new tbl8 with tbl24 value. */
707                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
708                         struct rte_lpm_tbl_entry new_tbl8_entry = {
709                                 .valid = VALID,
710                                 .depth = lpm->tbl24[tbl24_index].depth,
711                                 .valid_group = lpm->tbl8[i].valid_group,
712                                 .next_hop = lpm->tbl24[tbl24_index].next_hop,
713                         };
714                         __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
715                                         __ATOMIC_RELAXED);
716                 }
717
718                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
719
720                 /* Insert new rule into the tbl8 entry. */
721                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
722                         struct rte_lpm_tbl_entry new_tbl8_entry = {
723                                 .valid = VALID,
724                                 .depth = depth,
725                                 .valid_group = lpm->tbl8[i].valid_group,
726                                 .next_hop = next_hop,
727                         };
728                         __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
729                                         __ATOMIC_RELAXED);
730                 }
731
732                 /*
733                  * Update tbl24 entry to point to new tbl8 entry. Note: The
734                  * ext_flag and tbl8_index need to be updated simultaneously,
735                  * so assign whole structure in one go.
736                  */
737
738                 struct rte_lpm_tbl_entry new_tbl24_entry = {
739                                 .group_idx = tbl8_group_index,
740                                 .valid = VALID,
741                                 .valid_group = 1,
742                                 .depth = 0,
743                 };
744
745                 /* The tbl24 entry must be written only after the
746                  * tbl8 entries are written.
747                  */
748                 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
749                                 __ATOMIC_RELEASE);
750
751         } else { /*
752                 * If it is valid, extended entry calculate the index into tbl8.
753                 */
754                 tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
755                 tbl8_group_start = tbl8_group_index *
756                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
757                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
758
759                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
760
761                         if (!lpm->tbl8[i].valid ||
762                                         lpm->tbl8[i].depth <= depth) {
763                                 struct rte_lpm_tbl_entry new_tbl8_entry = {
764                                         .valid = VALID,
765                                         .depth = depth,
766                                         .next_hop = next_hop,
767                                         .valid_group = lpm->tbl8[i].valid_group,
768                                 };
769
770                                 /*
771                                  * Setting tbl8 entry in one go to avoid race
772                                  * condition
773                                  */
774                                 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
775                                                 __ATOMIC_RELAXED);
776
777                                 continue;
778                         }
779                 }
780         }
781 #undef group_idx
782         return 0;
783 }
784
785 /*
786  * Add a route
787  */
788 int
789 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
790                 uint32_t next_hop)
791 {
792         int32_t rule_index, status = 0;
793         uint32_t ip_masked;
794
795         /* Check user arguments. */
796         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
797                 return -EINVAL;
798
799         ip_masked = ip & depth_to_mask(depth);
800
801         /* Add the rule to the rule table. */
802         rule_index = rule_add(lpm, ip_masked, depth, next_hop);
803
804         /* Skip table entries update if The rule is the same as
805          * the rule in the rules table.
806          */
807         if (rule_index == -EEXIST)
808                 return 0;
809
810         /* If the is no space available for new rule return error. */
811         if (rule_index < 0) {
812                 return rule_index;
813         }
814
815         if (depth <= MAX_DEPTH_TBL24) {
816                 status = add_depth_small(lpm, ip_masked, depth, next_hop);
817         } else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
818                 status = add_depth_big(lpm, ip_masked, depth, next_hop);
819
820                 /*
821                  * If add fails due to exhaustion of tbl8 extensions delete
822                  * rule that was added to rule table.
823                  */
824                 if (status < 0) {
825                         rule_delete(lpm, rule_index, depth);
826
827                         return status;
828                 }
829         }
830
831         return 0;
832 }
833
834 /*
835  * Look for a rule in the high-level rules table
836  */
837 int
838 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
839 uint32_t *next_hop)
840 {
841         uint32_t ip_masked;
842         int32_t rule_index;
843
844         /* Check user arguments. */
845         if ((lpm == NULL) ||
846                 (next_hop == NULL) ||
847                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
848                 return -EINVAL;
849
850         /* Look for the rule using rule_find. */
851         ip_masked = ip & depth_to_mask(depth);
852         rule_index = rule_find(lpm, ip_masked, depth);
853
854         if (rule_index >= 0) {
855                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
856                 return 1;
857         }
858
859         /* If rule is not found return 0. */
860         return 0;
861 }
862
863 static int32_t
864 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
865                 uint8_t *sub_rule_depth)
866 {
867         int32_t rule_index;
868         uint32_t ip_masked;
869         uint8_t prev_depth;
870
871         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
872                 ip_masked = ip & depth_to_mask(prev_depth);
873
874                 rule_index = rule_find(lpm, ip_masked, prev_depth);
875
876                 if (rule_index >= 0) {
877                         *sub_rule_depth = prev_depth;
878                         return rule_index;
879                 }
880         }
881
882         return -1;
883 }
884
885 static int32_t
886 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
887         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
888 {
889 #define group_idx next_hop
890         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
891
892         /* Calculate the range and index into Table24. */
893         tbl24_range = depth_to_range(depth);
894         tbl24_index = (ip_masked >> 8);
895         struct rte_lpm_tbl_entry zero_tbl24_entry = {0};
896
897         /*
898          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
899          * and a positive number indicates a sub_rule_index.
900          */
901         if (sub_rule_index < 0) {
902                 /*
903                  * If no replacement rule exists then invalidate entries
904                  * associated with this rule.
905                  */
906                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
907
908                         if (lpm->tbl24[i].valid_group == 0 &&
909                                         lpm->tbl24[i].depth <= depth) {
910                                 __atomic_store(&lpm->tbl24[i],
911                                         &zero_tbl24_entry, __ATOMIC_RELEASE);
912                         } else if (lpm->tbl24[i].valid_group == 1) {
913                                 /*
914                                  * If TBL24 entry is extended, then there has
915                                  * to be a rule with depth >= 25 in the
916                                  * associated TBL8 group.
917                                  */
918
919                                 tbl8_group_index = lpm->tbl24[i].group_idx;
920                                 tbl8_index = tbl8_group_index *
921                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
922
923                                 for (j = tbl8_index; j < (tbl8_index +
924                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
925
926                                         if (lpm->tbl8[j].depth <= depth)
927                                                 lpm->tbl8[j].valid = INVALID;
928                                 }
929                         }
930                 }
931         } else {
932                 /*
933                  * If a replacement rule exists then modify entries
934                  * associated with this rule.
935                  */
936
937                 struct rte_lpm_tbl_entry new_tbl24_entry = {
938                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
939                         .valid = VALID,
940                         .valid_group = 0,
941                         .depth = sub_rule_depth,
942                 };
943
944                 struct rte_lpm_tbl_entry new_tbl8_entry = {
945                         .valid = VALID,
946                         .valid_group = VALID,
947                         .depth = sub_rule_depth,
948                         .next_hop = lpm->rules_tbl
949                         [sub_rule_index].next_hop,
950                 };
951
952                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
953
954                         if (lpm->tbl24[i].valid_group == 0 &&
955                                         lpm->tbl24[i].depth <= depth) {
956                                 __atomic_store(&lpm->tbl24[i], &new_tbl24_entry,
957                                                 __ATOMIC_RELEASE);
958                         } else  if (lpm->tbl24[i].valid_group == 1) {
959                                 /*
960                                  * If TBL24 entry is extended, then there has
961                                  * to be a rule with depth >= 25 in the
962                                  * associated TBL8 group.
963                                  */
964
965                                 tbl8_group_index = lpm->tbl24[i].group_idx;
966                                 tbl8_index = tbl8_group_index *
967                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
968
969                                 for (j = tbl8_index; j < (tbl8_index +
970                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
971
972                                         if (lpm->tbl8[j].depth <= depth)
973                                                 __atomic_store(&lpm->tbl8[j],
974                                                         &new_tbl8_entry,
975                                                         __ATOMIC_RELAXED);
976                                 }
977                         }
978                 }
979         }
980 #undef group_idx
981         return 0;
982 }
983
984 /*
985  * Checks if table 8 group can be recycled.
986  *
987  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
988  * Return of -EINVAL means tbl8 is empty and thus can be recycled
989  * Return of value > -1 means tbl8 is in use but has all the same values and
990  * thus can be recycled
991  */
992 static int32_t
993 tbl8_recycle_check(struct rte_lpm_tbl_entry *tbl8,
994                 uint32_t tbl8_group_start)
995 {
996         uint32_t tbl8_group_end, i;
997         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
998
999         /*
1000          * Check the first entry of the given tbl8. If it is invalid we know
1001          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
1002          *  (As they would affect all entries in a tbl8) and thus this table
1003          *  can not be recycled.
1004          */
1005         if (tbl8[tbl8_group_start].valid) {
1006                 /*
1007                  * If first entry is valid check if the depth is less than 24
1008                  * and if so check the rest of the entries to verify that they
1009                  * are all of this depth.
1010                  */
1011                 if (tbl8[tbl8_group_start].depth <= MAX_DEPTH_TBL24) {
1012                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
1013                                         i++) {
1014
1015                                 if (tbl8[i].depth !=
1016                                                 tbl8[tbl8_group_start].depth) {
1017
1018                                         return -EEXIST;
1019                                 }
1020                         }
1021                         /* If all entries are the same return the tb8 index */
1022                         return tbl8_group_start;
1023                 }
1024
1025                 return -EEXIST;
1026         }
1027         /*
1028          * If the first entry is invalid check if the rest of the entries in
1029          * the tbl8 are invalid.
1030          */
1031         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
1032                 if (tbl8[i].valid)
1033                         return -EEXIST;
1034         }
1035         /* If no valid entries are found then return -EINVAL. */
1036         return -EINVAL;
1037 }
1038
1039 static int32_t
1040 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
1041         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
1042 {
1043 #define group_idx next_hop
1044         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
1045                         tbl8_range, i;
1046         int32_t tbl8_recycle_index;
1047
1048         /*
1049          * Calculate the index into tbl24 and range. Note: All depths larger
1050          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
1051          */
1052         tbl24_index = ip_masked >> 8;
1053
1054         /* Calculate the index into tbl8 and range. */
1055         tbl8_group_index = lpm->tbl24[tbl24_index].group_idx;
1056         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
1057         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
1058         tbl8_range = depth_to_range(depth);
1059
1060         if (sub_rule_index < 0) {
1061                 /*
1062                  * Loop through the range of entries on tbl8 for which the
1063                  * rule_to_delete must be removed or modified.
1064                  */
1065                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1066                         if (lpm->tbl8[i].depth <= depth)
1067                                 lpm->tbl8[i].valid = INVALID;
1068                 }
1069         } else {
1070                 /* Set new tbl8 entry. */
1071                 struct rte_lpm_tbl_entry new_tbl8_entry = {
1072                         .valid = VALID,
1073                         .depth = sub_rule_depth,
1074                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
1075                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
1076                 };
1077
1078                 /*
1079                  * Loop through the range of entries on tbl8 for which the
1080                  * rule_to_delete must be modified.
1081                  */
1082                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
1083                         if (lpm->tbl8[i].depth <= depth)
1084                                 __atomic_store(&lpm->tbl8[i], &new_tbl8_entry,
1085                                                 __ATOMIC_RELAXED);
1086                 }
1087         }
1088
1089         /*
1090          * Check if there are any valid entries in this tbl8 group. If all
1091          * tbl8 entries are invalid we can free the tbl8 and invalidate the
1092          * associated tbl24 entry.
1093          */
1094
1095         tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
1096
1097         if (tbl8_recycle_index == -EINVAL) {
1098                 /* Set tbl24 before freeing tbl8 to avoid race condition.
1099                  * Prevent the free of the tbl8 group from hoisting.
1100                  */
1101                 lpm->tbl24[tbl24_index].valid = 0;
1102                 __atomic_thread_fence(__ATOMIC_RELEASE);
1103                 tbl8_free(lpm, tbl8_group_start);
1104         } else if (tbl8_recycle_index > -1) {
1105                 /* Update tbl24 entry. */
1106                 struct rte_lpm_tbl_entry new_tbl24_entry = {
1107                         .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop,
1108                         .valid = VALID,
1109                         .valid_group = 0,
1110                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
1111                 };
1112
1113                 /* Set tbl24 before freeing tbl8 to avoid race condition.
1114                  * Prevent the free of the tbl8 group from hoisting.
1115                  */
1116                 __atomic_store(&lpm->tbl24[tbl24_index], &new_tbl24_entry,
1117                                 __ATOMIC_RELAXED);
1118                 __atomic_thread_fence(__ATOMIC_RELEASE);
1119                 tbl8_free(lpm, tbl8_group_start);
1120         }
1121 #undef group_idx
1122         return 0;
1123 }
1124
1125 /*
1126  * Deletes a rule
1127  */
1128 int
1129 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
1130 {
1131         int32_t rule_to_delete_index, sub_rule_index;
1132         uint32_t ip_masked;
1133         uint8_t sub_rule_depth;
1134         /*
1135          * Check input arguments. Note: IP must be a positive integer of 32
1136          * bits in length therefore it need not be checked.
1137          */
1138         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
1139                 return -EINVAL;
1140         }
1141
1142         ip_masked = ip & depth_to_mask(depth);
1143
1144         /*
1145          * Find the index of the input rule, that needs to be deleted, in the
1146          * rule table.
1147          */
1148         rule_to_delete_index = rule_find(lpm, ip_masked, depth);
1149
1150         /*
1151          * Check if rule_to_delete_index was found. If no rule was found the
1152          * function rule_find returns -EINVAL.
1153          */
1154         if (rule_to_delete_index < 0)
1155                 return -EINVAL;
1156
1157         /* Delete the rule from the rule table. */
1158         rule_delete(lpm, rule_to_delete_index, depth);
1159
1160         /*
1161          * Find rule to replace the rule_to_delete. If there is no rule to
1162          * replace the rule_to_delete we return -1 and invalidate the table
1163          * entries associated with this rule.
1164          */
1165         sub_rule_depth = 0;
1166         sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
1167
1168         /*
1169          * If the input depth value is less than 25 use function
1170          * delete_depth_small otherwise use delete_depth_big.
1171          */
1172         if (depth <= MAX_DEPTH_TBL24) {
1173                 return delete_depth_small(lpm, ip_masked, depth,
1174                                 sub_rule_index, sub_rule_depth);
1175         } else { /* If depth > MAX_DEPTH_TBL24 */
1176                 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index,
1177                                 sub_rule_depth);
1178         }
1179 }
1180
1181 /*
1182  * Delete all rules from the LPM table.
1183  */
1184 void
1185 rte_lpm_delete_all(struct rte_lpm *lpm)
1186 {
1187         /* Zero rule information. */
1188         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1189
1190         /* Zero tbl24. */
1191         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1192
1193         /* Zero tbl8. */
1194         memset(lpm->tbl8, 0, sizeof(lpm->tbl8[0])
1195                         * RTE_LPM_TBL8_GROUP_NUM_ENTRIES * lpm->number_tbl8s);
1196
1197         /* Delete all rules form the rules table. */
1198         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1199 }