cc51210c435631727d04dee12a29f550fd9a874e
[dpdk.git] / lib / librte_lpm / rte_lpm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <errno.h>
40 #include <sys/queue.h>
41
42 #include <rte_log.h>
43 #include <rte_branch_prediction.h>
44 #include <rte_common.h>
45 #include <rte_memory.h>        /* for definition of RTE_CACHE_LINE_SIZE */
46 #include <rte_malloc.h>
47 #include <rte_memzone.h>
48 #include <rte_tailq.h>
49 #include <rte_eal.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_string_fns.h>
53 #include <rte_errno.h>
54 #include <rte_rwlock.h>
55 #include <rte_spinlock.h>
56
57 #include "rte_lpm.h"
58
59 TAILQ_HEAD(rte_lpm_list, rte_tailq_entry);
60
61 #define MAX_DEPTH_TBL24 24
62
63 enum valid_flag {
64         INVALID = 0,
65         VALID
66 };
67
68 /* Macro to enable/disable run-time checks. */
69 #if defined(RTE_LIBRTE_LPM_DEBUG)
70 #include <rte_debug.h>
71 #define VERIFY_DEPTH(depth) do {                                \
72         if ((depth == 0) || (depth > RTE_LPM_MAX_DEPTH))        \
73                 rte_panic("LPM: Invalid depth (%u) at line %d", \
74                                 (unsigned)(depth), __LINE__);   \
75 } while (0)
76 #else
77 #define VERIFY_DEPTH(depth)
78 #endif
79
80 /*
81  * Converts a given depth value to its corresponding mask value.
82  *
83  * depth  (IN)          : range = 1 - 32
84  * mask   (OUT)         : 32bit mask
85  */
86 static uint32_t __attribute__((pure))
87 depth_to_mask(uint8_t depth)
88 {
89         VERIFY_DEPTH(depth);
90
91         /* To calculate a mask start with a 1 on the left hand side and right
92          * shift while populating the left hand side with 1's
93          */
94         return (int)0x80000000 >> (depth - 1);
95 }
96
97 /*
98  * Converts given depth value to its corresponding range value.
99  */
100 static inline uint32_t __attribute__((pure))
101 depth_to_range(uint8_t depth)
102 {
103         VERIFY_DEPTH(depth);
104
105         /*
106          * Calculate tbl24 range. (Note: 2^depth = 1 << depth)
107          */
108         if (depth <= MAX_DEPTH_TBL24)
109                 return 1 << (MAX_DEPTH_TBL24 - depth);
110
111         /* Else if depth is greater than 24 */
112         return (1 << (RTE_LPM_MAX_DEPTH - depth));
113 }
114
115 /*
116  * Find an existing lpm table and return a pointer to it.
117  */
118 struct rte_lpm *
119 rte_lpm_find_existing(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         /* check that we have an initialised tail queue */
126         if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM,
127                         rte_lpm_list)) == NULL) {
128                 rte_errno = E_RTE_NO_TAILQ;
129                 return NULL;
130         }
131
132         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
133         TAILQ_FOREACH(te, lpm_list, next) {
134                 l = (struct rte_lpm *) te->data;
135                 if (strncmp(name, l->name, RTE_LPM_NAMESIZE) == 0)
136                         break;
137         }
138         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
139
140         if (te == NULL) {
141                 rte_errno = ENOENT;
142                 return NULL;
143         }
144
145         return l;
146 }
147
148 /*
149  * Allocates memory for LPM object
150  */
151 struct rte_lpm *
152 rte_lpm_create(const char *name, int socket_id, int max_rules,
153                 __rte_unused int flags)
154 {
155         char mem_name[RTE_LPM_NAMESIZE];
156         struct rte_lpm *lpm = NULL;
157         struct rte_tailq_entry *te;
158         uint32_t mem_size;
159         struct rte_lpm_list *lpm_list;
160
161         /* check that we have an initialised tail queue */
162         if ((lpm_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM,
163                         rte_lpm_list)) == NULL) {
164                 rte_errno = E_RTE_NO_TAILQ;
165                 return NULL;
166         }
167
168         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl24_entry) != 2);
169         RTE_BUILD_BUG_ON(sizeof(struct rte_lpm_tbl8_entry) != 2);
170
171         /* Check user arguments. */
172         if ((name == NULL) || (socket_id < -1) || (max_rules == 0)){
173                 rte_errno = EINVAL;
174                 return NULL;
175         }
176
177         snprintf(mem_name, sizeof(mem_name), "LPM_%s", name);
178
179         /* Determine the amount of memory to allocate. */
180         mem_size = sizeof(*lpm) + (sizeof(lpm->rules_tbl[0]) * max_rules);
181
182         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
183
184         /* guarantee there's no existing */
185         TAILQ_FOREACH(te, lpm_list, next) {
186                 lpm = (struct rte_lpm *) te->data;
187                 if (strncmp(name, lpm->name, RTE_LPM_NAMESIZE) == 0)
188                         break;
189         }
190         if (te != NULL)
191                 goto exit;
192
193         /* allocate tailq entry */
194         te = rte_zmalloc("LPM_TAILQ_ENTRY", sizeof(*te), 0);
195         if (te == NULL) {
196                 RTE_LOG(ERR, LPM, "Failed to allocate tailq entry\n");
197                 goto exit;
198         }
199
200         /* Allocate memory to store the LPM data structures. */
201         lpm = (struct rte_lpm *)rte_zmalloc_socket(mem_name, mem_size,
202                         RTE_CACHE_LINE_SIZE, socket_id);
203         if (lpm == NULL) {
204                 RTE_LOG(ERR, LPM, "LPM memory allocation failed\n");
205                 rte_free(te);
206                 goto exit;
207         }
208
209         /* Save user arguments. */
210         lpm->max_rules = max_rules;
211         snprintf(lpm->name, sizeof(lpm->name), "%s", name);
212
213         te->data = (void *) lpm;
214
215         TAILQ_INSERT_TAIL(lpm_list, te, next);
216
217 exit:
218         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
219
220         return lpm;
221 }
222
223 /*
224  * Deallocates memory for given LPM table.
225  */
226 void
227 rte_lpm_free(struct rte_lpm *lpm)
228 {
229         struct rte_lpm_list *lpm_list;
230         struct rte_tailq_entry *te;
231
232         /* Check user arguments. */
233         if (lpm == NULL)
234                 return;
235
236         /* check that we have an initialised tail queue */
237         if ((lpm_list =
238              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_LPM, rte_lpm_list)) == NULL) {
239                 rte_errno = E_RTE_NO_TAILQ;
240                 return;
241         }
242
243         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
244
245         /* find our tailq entry */
246         TAILQ_FOREACH(te, lpm_list, next) {
247                 if (te->data == (void *) lpm)
248                         break;
249         }
250         if (te == NULL) {
251                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
252                 return;
253         }
254
255         TAILQ_REMOVE(lpm_list, te, next);
256
257         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
258
259         rte_free(lpm);
260         rte_free(te);
261 }
262
263 /*
264  * Adds a rule to the rule table.
265  *
266  * NOTE: The rule table is split into 32 groups. Each group contains rules that
267  * apply to a specific prefix depth (i.e. group 1 contains rules that apply to
268  * prefixes with a depth of 1 etc.). In the following code (depth - 1) is used
269  * to refer to depth 1 because even though the depth range is 1 - 32, depths
270  * are stored in the rule table from 0 - 31.
271  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
272  */
273 static inline int32_t
274 rule_add(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
275         uint8_t next_hop)
276 {
277         uint32_t rule_gindex, rule_index, last_rule;
278         int i;
279
280         VERIFY_DEPTH(depth);
281
282         /* Scan through rule group to see if rule already exists. */
283         if (lpm->rule_info[depth - 1].used_rules > 0) {
284
285                 /* rule_gindex stands for rule group index. */
286                 rule_gindex = lpm->rule_info[depth - 1].first_rule;
287                 /* Initialise rule_index to point to start of rule group. */
288                 rule_index = rule_gindex;
289                 /* Last rule = Last used rule in this rule group. */
290                 last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
291
292                 for (; rule_index < last_rule; rule_index++) {
293
294                         /* If rule already exists update its next_hop and return. */
295                         if (lpm->rules_tbl[rule_index].ip == ip_masked) {
296                                 lpm->rules_tbl[rule_index].next_hop = next_hop;
297
298                                 return rule_index;
299                         }
300                 }
301
302                 if (rule_index == lpm->max_rules)
303                         return -ENOSPC;
304         } else {
305                 /* Calculate the position in which the rule will be stored. */
306                 rule_index = 0;
307
308                 for (i = depth - 1; i > 0; i--) {
309                         if (lpm->rule_info[i - 1].used_rules > 0) {
310                                 rule_index = lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules;
311                                 break;
312                         }
313                 }
314                 if (rule_index == lpm->max_rules)
315                         return -ENOSPC;
316
317                 lpm->rule_info[depth - 1].first_rule = rule_index;
318         }
319
320         /* Make room for the new rule in the array. */
321         for (i = RTE_LPM_MAX_DEPTH; i > depth; i--) {
322                 if (lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules == lpm->max_rules)
323                         return -ENOSPC;
324
325                 if (lpm->rule_info[i - 1].used_rules > 0) {
326                         lpm->rules_tbl[lpm->rule_info[i - 1].first_rule + lpm->rule_info[i - 1].used_rules]
327                                         = lpm->rules_tbl[lpm->rule_info[i - 1].first_rule];
328                         lpm->rule_info[i - 1].first_rule++;
329                 }
330         }
331
332         /* Add the new rule. */
333         lpm->rules_tbl[rule_index].ip = ip_masked;
334         lpm->rules_tbl[rule_index].next_hop = next_hop;
335
336         /* Increment the used rules counter for this rule group. */
337         lpm->rule_info[depth - 1].used_rules++;
338
339         return rule_index;
340 }
341
342 /*
343  * Delete a rule from the rule table.
344  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
345  */
346 static inline void
347 rule_delete(struct rte_lpm *lpm, int32_t rule_index, uint8_t depth)
348 {
349         int i;
350
351         VERIFY_DEPTH(depth);
352
353         lpm->rules_tbl[rule_index] = lpm->rules_tbl[lpm->rule_info[depth - 1].first_rule
354                         + lpm->rule_info[depth - 1].used_rules - 1];
355
356         for (i = depth; i < RTE_LPM_MAX_DEPTH; i++) {
357                 if (lpm->rule_info[i].used_rules > 0) {
358                         lpm->rules_tbl[lpm->rule_info[i].first_rule - 1] =
359                                         lpm->rules_tbl[lpm->rule_info[i].first_rule + lpm->rule_info[i].used_rules - 1];
360                         lpm->rule_info[i].first_rule--;
361                 }
362         }
363
364         lpm->rule_info[depth - 1].used_rules--;
365 }
366
367 /*
368  * Finds a rule in rule table.
369  * NOTE: Valid range for depth parameter is 1 .. 32 inclusive.
370  */
371 static inline int32_t
372 rule_find(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth)
373 {
374         uint32_t rule_gindex, last_rule, rule_index;
375
376         VERIFY_DEPTH(depth);
377
378         rule_gindex = lpm->rule_info[depth - 1].first_rule;
379         last_rule = rule_gindex + lpm->rule_info[depth - 1].used_rules;
380
381         /* Scan used rules at given depth to find rule. */
382         for (rule_index = rule_gindex; rule_index < last_rule; rule_index++) {
383                 /* If rule is found return the rule index. */
384                 if (lpm->rules_tbl[rule_index].ip == ip_masked)
385                         return (rule_index);
386         }
387
388         /* If rule is not found return -E_RTE_NO_TAILQ. */
389         return -E_RTE_NO_TAILQ;
390 }
391
392 /*
393  * Find, clean and allocate a tbl8.
394  */
395 static inline int32_t
396 tbl8_alloc(struct rte_lpm_tbl8_entry *tbl8)
397 {
398         uint32_t tbl8_gindex; /* tbl8 group index. */
399         struct rte_lpm_tbl8_entry *tbl8_entry;
400
401         /* Scan through tbl8 to find a free (i.e. INVALID) tbl8 group. */
402         for (tbl8_gindex = 0; tbl8_gindex < RTE_LPM_TBL8_NUM_GROUPS;
403                         tbl8_gindex++) {
404                 tbl8_entry = &tbl8[tbl8_gindex *
405                                    RTE_LPM_TBL8_GROUP_NUM_ENTRIES];
406                 /* If a free tbl8 group is found clean it and set as VALID. */
407                 if (!tbl8_entry->valid_group) {
408                         memset(&tbl8_entry[0], 0,
409                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES *
410                                         sizeof(tbl8_entry[0]));
411
412                         tbl8_entry->valid_group = VALID;
413
414                         /* Return group index for allocated tbl8 group. */
415                         return tbl8_gindex;
416                 }
417         }
418
419         /* If there are no tbl8 groups free then return error. */
420         return -ENOSPC;
421 }
422
423 static inline void
424 tbl8_free(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
425 {
426         /* Set tbl8 group invalid*/
427         tbl8[tbl8_group_start].valid_group = INVALID;
428 }
429
430 static inline int32_t
431 add_depth_small(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
432                 uint8_t next_hop)
433 {
434         uint32_t tbl24_index, tbl24_range, tbl8_index, tbl8_group_end, i, j;
435
436         /* Calculate the index into Table24. */
437         tbl24_index = ip >> 8;
438         tbl24_range = depth_to_range(depth);
439
440         for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
441                 /*
442                  * For invalid OR valid and non-extended tbl 24 entries set
443                  * entry.
444                  */
445                 if (!lpm->tbl24[i].valid || (lpm->tbl24[i].ext_entry == 0 &&
446                                 lpm->tbl24[i].depth <= depth)) {
447
448                         struct rte_lpm_tbl24_entry new_tbl24_entry = {
449                                 { .next_hop = next_hop, },
450                                 .valid = VALID,
451                                 .ext_entry = 0,
452                                 .depth = depth,
453                         };
454
455                         /* Setting tbl24 entry in one go to avoid race
456                          * conditions */
457                         lpm->tbl24[i] = new_tbl24_entry;
458
459                         continue;
460                 }
461
462                 /* If tbl24 entry is valid and extended calculate the index
463                  * into tbl8. */
464                 tbl8_index = lpm->tbl24[i].tbl8_gindex *
465                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
466                 tbl8_group_end = tbl8_index + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
467
468                 for (j = tbl8_index; j < tbl8_group_end; j++) {
469                         if (!lpm->tbl8[j].valid ||
470                                         lpm->tbl8[j].depth <= depth) {
471                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
472                                         .valid = VALID,
473                                         .valid_group = VALID,
474                                         .depth = depth,
475                                         .next_hop = next_hop,
476                                 };
477
478                                 /*
479                                  * Setting tbl8 entry in one go to avoid race
480                                  * conditions
481                                  */
482                                 lpm->tbl8[j] = new_tbl8_entry;
483
484                                 continue;
485                         }
486                 }
487         }
488
489         return 0;
490 }
491
492 static inline int32_t
493 add_depth_big(struct rte_lpm *lpm, uint32_t ip_masked, uint8_t depth,
494                 uint8_t next_hop)
495 {
496         uint32_t tbl24_index;
497         int32_t tbl8_group_index, tbl8_group_start, tbl8_group_end, tbl8_index,
498                 tbl8_range, i;
499
500         tbl24_index = (ip_masked >> 8);
501         tbl8_range = depth_to_range(depth);
502
503         if (!lpm->tbl24[tbl24_index].valid) {
504                 /* Search for a free tbl8 group. */
505                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
506
507                 /* Check tbl8 allocation was successful. */
508                 if (tbl8_group_index < 0) {
509                         return tbl8_group_index;
510                 }
511
512                 /* Find index into tbl8 and range. */
513                 tbl8_index = (tbl8_group_index *
514                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES) +
515                                 (ip_masked & 0xFF);
516
517                 /* Set tbl8 entry. */
518                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
519                         lpm->tbl8[i].depth = depth;
520                         lpm->tbl8[i].next_hop = next_hop;
521                         lpm->tbl8[i].valid = VALID;
522                 }
523
524                 /*
525                  * Update tbl24 entry to point to new tbl8 entry. Note: The
526                  * ext_flag and tbl8_index need to be updated simultaneously,
527                  * so assign whole structure in one go
528                  */
529
530                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
531                         { .tbl8_gindex = (uint8_t)tbl8_group_index, },
532                         .valid = VALID,
533                         .ext_entry = 1,
534                         .depth = 0,
535                 };
536
537                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
538
539         }/* If valid entry but not extended calculate the index into Table8. */
540         else if (lpm->tbl24[tbl24_index].ext_entry == 0) {
541                 /* Search for free tbl8 group. */
542                 tbl8_group_index = tbl8_alloc(lpm->tbl8);
543
544                 if (tbl8_group_index < 0) {
545                         return tbl8_group_index;
546                 }
547
548                 tbl8_group_start = tbl8_group_index *
549                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
550                 tbl8_group_end = tbl8_group_start +
551                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
552
553                 /* Populate new tbl8 with tbl24 value. */
554                 for (i = tbl8_group_start; i < tbl8_group_end; i++) {
555                         lpm->tbl8[i].valid = VALID;
556                         lpm->tbl8[i].depth = lpm->tbl24[tbl24_index].depth;
557                         lpm->tbl8[i].next_hop =
558                                         lpm->tbl24[tbl24_index].next_hop;
559                 }
560
561                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
562
563                 /* Insert new rule into the tbl8 entry. */
564                 for (i = tbl8_index; i < tbl8_index + tbl8_range; i++) {
565                         if (!lpm->tbl8[i].valid ||
566                                         lpm->tbl8[i].depth <= depth) {
567                                 lpm->tbl8[i].valid = VALID;
568                                 lpm->tbl8[i].depth = depth;
569                                 lpm->tbl8[i].next_hop = next_hop;
570
571                                 continue;
572                         }
573                 }
574
575                 /*
576                  * Update tbl24 entry to point to new tbl8 entry. Note: The
577                  * ext_flag and tbl8_index need to be updated simultaneously,
578                  * so assign whole structure in one go.
579                  */
580
581                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
582                                 { .tbl8_gindex = (uint8_t)tbl8_group_index, },
583                                 .valid = VALID,
584                                 .ext_entry = 1,
585                                 .depth = 0,
586                 };
587
588                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
589
590         }
591         else { /*
592                 * If it is valid, extended entry calculate the index into tbl8.
593                 */
594                 tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
595                 tbl8_group_start = tbl8_group_index *
596                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
597                 tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
598
599                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
600
601                         if (!lpm->tbl8[i].valid ||
602                                         lpm->tbl8[i].depth <= depth) {
603                                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
604                                         .valid = VALID,
605                                         .depth = depth,
606                                         .next_hop = next_hop,
607                                         .valid_group = lpm->tbl8[i].valid_group,
608                                 };
609
610                                 /*
611                                  * Setting tbl8 entry in one go to avoid race
612                                  * condition
613                                  */
614                                 lpm->tbl8[i] = new_tbl8_entry;
615
616                                 continue;
617                         }
618                 }
619         }
620
621         return 0;
622 }
623
624 /*
625  * Add a route
626  */
627 int
628 rte_lpm_add(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
629                 uint8_t next_hop)
630 {
631         int32_t rule_index, status = 0;
632         uint32_t ip_masked;
633
634         /* Check user arguments. */
635         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
636                 return -EINVAL;
637
638         ip_masked = ip & depth_to_mask(depth);
639
640         /* Add the rule to the rule table. */
641         rule_index = rule_add(lpm, ip_masked, depth, next_hop);
642
643         /* If the is no space available for new rule return error. */
644         if (rule_index < 0) {
645                 return rule_index;
646         }
647
648         if (depth <= MAX_DEPTH_TBL24) {
649                 status = add_depth_small(lpm, ip_masked, depth, next_hop);
650         }
651         else { /* If depth > RTE_LPM_MAX_DEPTH_TBL24 */
652                 status = add_depth_big(lpm, ip_masked, depth, next_hop);
653
654                 /*
655                  * If add fails due to exhaustion of tbl8 extensions delete
656                  * rule that was added to rule table.
657                  */
658                 if (status < 0) {
659                         rule_delete(lpm, rule_index, depth);
660
661                         return status;
662                 }
663         }
664
665         return 0;
666 }
667
668 /*
669  * Look for a rule in the high-level rules table
670  */
671 int
672 rte_lpm_is_rule_present(struct rte_lpm *lpm, uint32_t ip, uint8_t depth,
673 uint8_t *next_hop)
674 {
675         uint32_t ip_masked;
676         int32_t rule_index;
677
678         /* Check user arguments. */
679         if ((lpm == NULL) ||
680                 (next_hop == NULL) ||
681                 (depth < 1) || (depth > RTE_LPM_MAX_DEPTH))
682                 return -EINVAL;
683
684         /* Look for the rule using rule_find. */
685         ip_masked = ip & depth_to_mask(depth);
686         rule_index = rule_find(lpm, ip_masked, depth);
687
688         if (rule_index >= 0) {
689                 *next_hop = lpm->rules_tbl[rule_index].next_hop;
690                 return 1;
691         }
692
693         /* If rule is not found return 0. */
694         return 0;
695 }
696
697 static inline int32_t
698 find_previous_rule(struct rte_lpm *lpm, uint32_t ip, uint8_t depth, uint8_t *sub_rule_depth)
699 {
700         int32_t rule_index;
701         uint32_t ip_masked;
702         uint8_t prev_depth;
703
704         for (prev_depth = (uint8_t)(depth - 1); prev_depth > 0; prev_depth--) {
705                 ip_masked = ip & depth_to_mask(prev_depth);
706
707                 rule_index = rule_find(lpm, ip_masked, prev_depth);
708
709                 if (rule_index >= 0) {
710                         *sub_rule_depth = prev_depth;
711                         return rule_index;
712                 }
713         }
714
715         return -1;
716 }
717
718 static inline int32_t
719 delete_depth_small(struct rte_lpm *lpm, uint32_t ip_masked,
720         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
721 {
722         uint32_t tbl24_range, tbl24_index, tbl8_group_index, tbl8_index, i, j;
723
724         /* Calculate the range and index into Table24. */
725         tbl24_range = depth_to_range(depth);
726         tbl24_index = (ip_masked >> 8);
727
728         /*
729          * Firstly check the sub_rule_index. A -1 indicates no replacement rule
730          * and a positive number indicates a sub_rule_index.
731          */
732         if (sub_rule_index < 0) {
733                 /*
734                  * If no replacement rule exists then invalidate entries
735                  * associated with this rule.
736                  */
737                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
738
739                         if (lpm->tbl24[i].ext_entry == 0 &&
740                                         lpm->tbl24[i].depth <= depth ) {
741                                 lpm->tbl24[i].valid = INVALID;
742                         }
743                         else {
744                                 /*
745                                  * If TBL24 entry is extended, then there has
746                                  * to be a rule with depth >= 25 in the
747                                  * associated TBL8 group.
748                                  */
749
750                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
751                                 tbl8_index = tbl8_group_index *
752                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
753
754                                 for (j = tbl8_index; j < (tbl8_index +
755                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
756
757                                         if (lpm->tbl8[j].depth <= depth)
758                                                 lpm->tbl8[j].valid = INVALID;
759                                 }
760                         }
761                 }
762         }
763         else {
764                 /*
765                  * If a replacement rule exists then modify entries
766                  * associated with this rule.
767                  */
768
769                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
770                         {.next_hop = lpm->rules_tbl[sub_rule_index].next_hop,},
771                         .valid = VALID,
772                         .ext_entry = 0,
773                         .depth = sub_rule_depth,
774                 };
775
776                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
777                         .valid = VALID,
778                         .depth = sub_rule_depth,
779                         .next_hop = lpm->rules_tbl
780                         [sub_rule_index].next_hop,
781                 };
782
783                 for (i = tbl24_index; i < (tbl24_index + tbl24_range); i++) {
784
785                         if (lpm->tbl24[i].ext_entry == 0 &&
786                                         lpm->tbl24[i].depth <= depth ) {
787                                 lpm->tbl24[i] = new_tbl24_entry;
788                         }
789                         else {
790                                 /*
791                                  * If TBL24 entry is extended, then there has
792                                  * to be a rule with depth >= 25 in the
793                                  * associated TBL8 group.
794                                  */
795
796                                 tbl8_group_index = lpm->tbl24[i].tbl8_gindex;
797                                 tbl8_index = tbl8_group_index *
798                                                 RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
799
800                                 for (j = tbl8_index; j < (tbl8_index +
801                                         RTE_LPM_TBL8_GROUP_NUM_ENTRIES); j++) {
802
803                                         if (lpm->tbl8[j].depth <= depth)
804                                                 lpm->tbl8[j] = new_tbl8_entry;
805                                 }
806                         }
807                 }
808         }
809
810         return 0;
811 }
812
813 /*
814  * Checks if table 8 group can be recycled.
815  *
816  * Return of -EEXIST means tbl8 is in use and thus can not be recycled.
817  * Return of -EINVAL means tbl8 is empty and thus can be recycled
818  * Return of value > -1 means tbl8 is in use but has all the same values and
819  * thus can be recycled
820  */
821 static inline int32_t
822 tbl8_recycle_check(struct rte_lpm_tbl8_entry *tbl8, uint32_t tbl8_group_start)
823 {
824         uint32_t tbl8_group_end, i;
825         tbl8_group_end = tbl8_group_start + RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
826
827         /*
828          * Check the first entry of the given tbl8. If it is invalid we know
829          * this tbl8 does not contain any rule with a depth < RTE_LPM_MAX_DEPTH
830          *  (As they would affect all entries in a tbl8) and thus this table
831          *  can not be recycled.
832          */
833         if (tbl8[tbl8_group_start].valid) {
834                 /*
835                  * If first entry is valid check if the depth is less than 24
836                  * and if so check the rest of the entries to verify that they
837                  * are all of this depth.
838                  */
839                 if (tbl8[tbl8_group_start].depth < MAX_DEPTH_TBL24) {
840                         for (i = (tbl8_group_start + 1); i < tbl8_group_end;
841                                         i++) {
842
843                                 if (tbl8[i].depth !=
844                                                 tbl8[tbl8_group_start].depth) {
845
846                                         return -EEXIST;
847                                 }
848                         }
849                         /* If all entries are the same return the tb8 index */
850                         return tbl8_group_start;
851                 }
852
853                 return -EEXIST;
854         }
855         /*
856          * If the first entry is invalid check if the rest of the entries in
857          * the tbl8 are invalid.
858          */
859         for (i = (tbl8_group_start + 1); i < tbl8_group_end; i++) {
860                 if (tbl8[i].valid)
861                         return -EEXIST;
862         }
863         /* If no valid entries are found then return -EINVAL. */
864         return -EINVAL;
865 }
866
867 static inline int32_t
868 delete_depth_big(struct rte_lpm *lpm, uint32_t ip_masked,
869         uint8_t depth, int32_t sub_rule_index, uint8_t sub_rule_depth)
870 {
871         uint32_t tbl24_index, tbl8_group_index, tbl8_group_start, tbl8_index,
872                         tbl8_range, i;
873         int32_t tbl8_recycle_index;
874
875         /*
876          * Calculate the index into tbl24 and range. Note: All depths larger
877          * than MAX_DEPTH_TBL24 are associated with only one tbl24 entry.
878          */
879         tbl24_index = ip_masked >> 8;
880
881         /* Calculate the index into tbl8 and range. */
882         tbl8_group_index = lpm->tbl24[tbl24_index].tbl8_gindex;
883         tbl8_group_start = tbl8_group_index * RTE_LPM_TBL8_GROUP_NUM_ENTRIES;
884         tbl8_index = tbl8_group_start + (ip_masked & 0xFF);
885         tbl8_range = depth_to_range(depth);
886
887         if (sub_rule_index < 0) {
888                 /*
889                  * Loop through the range of entries on tbl8 for which the
890                  * rule_to_delete must be removed or modified.
891                  */
892                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
893                         if (lpm->tbl8[i].depth <= depth)
894                                 lpm->tbl8[i].valid = INVALID;
895                 }
896         }
897         else {
898                 /* Set new tbl8 entry. */
899                 struct rte_lpm_tbl8_entry new_tbl8_entry = {
900                         .valid = VALID,
901                         .depth = sub_rule_depth,
902                         .valid_group = lpm->tbl8[tbl8_group_start].valid_group,
903                         .next_hop = lpm->rules_tbl[sub_rule_index].next_hop,
904                 };
905
906                 /*
907                  * Loop through the range of entries on tbl8 for which the
908                  * rule_to_delete must be modified.
909                  */
910                 for (i = tbl8_index; i < (tbl8_index + tbl8_range); i++) {
911                         if (lpm->tbl8[i].depth <= depth)
912                                 lpm->tbl8[i] = new_tbl8_entry;
913                 }
914         }
915
916         /*
917          * Check if there are any valid entries in this tbl8 group. If all
918          * tbl8 entries are invalid we can free the tbl8 and invalidate the
919          * associated tbl24 entry.
920          */
921
922         tbl8_recycle_index = tbl8_recycle_check(lpm->tbl8, tbl8_group_start);
923
924         if (tbl8_recycle_index == -EINVAL){
925                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
926                 lpm->tbl24[tbl24_index].valid = 0;
927                 tbl8_free(lpm->tbl8, tbl8_group_start);
928         }
929         else if (tbl8_recycle_index > -1) {
930                 /* Update tbl24 entry. */
931                 struct rte_lpm_tbl24_entry new_tbl24_entry = {
932                         { .next_hop = lpm->tbl8[tbl8_recycle_index].next_hop, },
933                         .valid = VALID,
934                         .ext_entry = 0,
935                         .depth = lpm->tbl8[tbl8_recycle_index].depth,
936                 };
937
938                 /* Set tbl24 before freeing tbl8 to avoid race condition. */
939                 lpm->tbl24[tbl24_index] = new_tbl24_entry;
940                 tbl8_free(lpm->tbl8, tbl8_group_start);
941         }
942
943         return 0;
944 }
945
946 /*
947  * Deletes a rule
948  */
949 int
950 rte_lpm_delete(struct rte_lpm *lpm, uint32_t ip, uint8_t depth)
951 {
952         int32_t rule_to_delete_index, sub_rule_index;
953         uint32_t ip_masked;
954         uint8_t sub_rule_depth;
955         /*
956          * Check input arguments. Note: IP must be a positive integer of 32
957          * bits in length therefore it need not be checked.
958          */
959         if ((lpm == NULL) || (depth < 1) || (depth > RTE_LPM_MAX_DEPTH)) {
960                 return -EINVAL;
961         }
962
963         ip_masked = ip & depth_to_mask(depth);
964
965         /*
966          * Find the index of the input rule, that needs to be deleted, in the
967          * rule table.
968          */
969         rule_to_delete_index = rule_find(lpm, ip_masked, depth);
970
971         /*
972          * Check if rule_to_delete_index was found. If no rule was found the
973          * function rule_find returns -E_RTE_NO_TAILQ.
974          */
975         if (rule_to_delete_index < 0)
976                 return -E_RTE_NO_TAILQ;
977
978         /* Delete the rule from the rule table. */
979         rule_delete(lpm, rule_to_delete_index, depth);
980
981         /*
982          * Find rule to replace the rule_to_delete. If there is no rule to
983          * replace the rule_to_delete we return -1 and invalidate the table
984          * entries associated with this rule.
985          */
986         sub_rule_depth = 0;
987         sub_rule_index = find_previous_rule(lpm, ip, depth, &sub_rule_depth);
988
989         /*
990          * If the input depth value is less than 25 use function
991          * delete_depth_small otherwise use delete_depth_big.
992          */
993         if (depth <= MAX_DEPTH_TBL24) {
994                 return delete_depth_small(lpm, ip_masked, depth,
995                                 sub_rule_index, sub_rule_depth);
996         }
997         else { /* If depth > MAX_DEPTH_TBL24 */
998                 return delete_depth_big(lpm, ip_masked, depth, sub_rule_index, sub_rule_depth);
999         }
1000 }
1001
1002 /*
1003  * Delete all rules from the LPM table.
1004  */
1005 void
1006 rte_lpm_delete_all(struct rte_lpm *lpm)
1007 {
1008         /* Zero rule information. */
1009         memset(lpm->rule_info, 0, sizeof(lpm->rule_info));
1010
1011         /* Zero tbl24. */
1012         memset(lpm->tbl24, 0, sizeof(lpm->tbl24));
1013
1014         /* Zero tbl8. */
1015         memset(lpm->tbl8, 0, sizeof(lpm->tbl8));
1016
1017         /* Delete all rules form the rules table. */
1018         memset(lpm->rules_tbl, 0, sizeof(lpm->rules_tbl[0]) * lpm->max_rules);
1019 }
1020