eal/ppc: undefine AltiVec keyword vector
[dpdk.git] / lib / rib / rte_rib6.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3  * Copyright(c) 2019 Intel Corporation
4  */
5
6 #include <stdbool.h>
7 #include <stdint.h>
8
9 #include <rte_eal_memconfig.h>
10 #include <rte_errno.h>
11 #include <rte_malloc.h>
12 #include <rte_mempool.h>
13 #include <rte_string_fns.h>
14 #include <rte_tailq.h>
15
16 #include <rte_rib6.h>
17
18 #define RTE_RIB_VALID_NODE      1
19 #define RIB6_MAXDEPTH           128
20 /* Maximum length of a RIB6 name. */
21 #define RTE_RIB6_NAMESIZE       64
22
23 TAILQ_HEAD(rte_rib6_list, rte_tailq_entry);
24 static struct rte_tailq_elem rte_rib6_tailq = {
25         .name = "RTE_RIB6",
26 };
27 EAL_REGISTER_TAILQ(rte_rib6_tailq)
28
29 struct rte_rib6_node {
30         struct rte_rib6_node    *left;
31         struct rte_rib6_node    *right;
32         struct rte_rib6_node    *parent;
33         uint64_t                nh;
34         uint8_t                 ip[RTE_RIB6_IPV6_ADDR_SIZE];
35         uint8_t                 depth;
36         uint8_t                 flag;
37         __extension__ uint64_t          ext[0];
38 };
39
40 struct rte_rib6 {
41         char            name[RTE_RIB6_NAMESIZE];
42         struct rte_rib6_node    *tree;
43         struct rte_mempool      *node_pool;
44         uint32_t                cur_nodes;
45         uint32_t                cur_routes;
46         int                     max_nodes;
47 };
48
49 static inline bool
50 is_valid_node(const struct rte_rib6_node *node)
51 {
52         return (node->flag & RTE_RIB_VALID_NODE) == RTE_RIB_VALID_NODE;
53 }
54
55 static inline bool
56 is_right_node(const struct rte_rib6_node *node)
57 {
58         return node->parent->right == node;
59 }
60
61 /*
62  * Check if ip1 is covered by ip2/depth prefix
63  */
64 static inline bool
65 is_covered(const uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE],
66                 const uint8_t ip2[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
67 {
68         int i;
69
70         for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
71                 if ((ip1[i] ^ ip2[i]) & get_msk_part(depth, i))
72                         return false;
73
74         return true;
75 }
76
77 static inline int
78 get_dir(const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
79 {
80         uint8_t index, msk;
81
82         /*
83          * depth & 127 clamps depth to values that will not
84          * read off the end of ip.
85          * depth is the number of bits deep into ip to traverse, and
86          * is incremented in blocks of 8 (1 byte). This means the last
87          * 3 bits are irrelevant to what the index of ip should be.
88          */
89         index = (depth & INT8_MAX) / CHAR_BIT;
90
91         /*
92          * msk is the bitmask used to extract the bit used to decide the
93          * direction of the next step of the binary search.
94          */
95         msk = 1 << (7 - (depth & 7));
96
97         return (ip[index] & msk) != 0;
98 }
99
100 static inline struct rte_rib6_node *
101 get_nxt_node(struct rte_rib6_node *node,
102         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
103 {
104         if (node->depth == RIB6_MAXDEPTH)
105                 return NULL;
106
107         return (get_dir(ip, node->depth)) ? node->right : node->left;
108 }
109
110 static struct rte_rib6_node *
111 node_alloc(struct rte_rib6 *rib)
112 {
113         struct rte_rib6_node *ent;
114         int ret;
115
116         ret = rte_mempool_get(rib->node_pool, (void *)&ent);
117         if (unlikely(ret != 0))
118                 return NULL;
119         ++rib->cur_nodes;
120         return ent;
121 }
122
123 static void
124 node_free(struct rte_rib6 *rib, struct rte_rib6_node *ent)
125 {
126         --rib->cur_nodes;
127         rte_mempool_put(rib->node_pool, ent);
128 }
129
130 struct rte_rib6_node *
131 rte_rib6_lookup(struct rte_rib6 *rib,
132         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
133 {
134         struct rte_rib6_node *cur;
135         struct rte_rib6_node *prev = NULL;
136
137         if (unlikely(rib == NULL)) {
138                 rte_errno = EINVAL;
139                 return NULL;
140         }
141         cur = rib->tree;
142
143         while ((cur != NULL) && is_covered(ip, cur->ip, cur->depth)) {
144                 if (is_valid_node(cur))
145                         prev = cur;
146                 cur = get_nxt_node(cur, ip);
147         }
148         return prev;
149 }
150
151 struct rte_rib6_node *
152 rte_rib6_lookup_parent(struct rte_rib6_node *ent)
153 {
154         struct rte_rib6_node *tmp;
155
156         if (ent == NULL)
157                 return NULL;
158
159         tmp = ent->parent;
160         while ((tmp != NULL) && (!is_valid_node(tmp)))
161                 tmp = tmp->parent;
162
163         return tmp;
164 }
165
166 struct rte_rib6_node *
167 rte_rib6_lookup_exact(struct rte_rib6 *rib,
168         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
169 {
170         struct rte_rib6_node *cur;
171         uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
172         int i;
173
174         if (unlikely(rib == NULL || ip == NULL || depth > RIB6_MAXDEPTH)) {
175                 rte_errno = EINVAL;
176                 return NULL;
177         }
178         cur = rib->tree;
179
180         for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
181                 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
182
183         while (cur != NULL) {
184                 if (rte_rib6_is_equal(cur->ip, tmp_ip) &&
185                                 (cur->depth == depth) &&
186                                 is_valid_node(cur))
187                         return cur;
188
189                 if (!(is_covered(tmp_ip, cur->ip, cur->depth)) ||
190                                 (cur->depth >= depth))
191                         break;
192
193                 cur = get_nxt_node(cur, tmp_ip);
194         }
195
196         return NULL;
197 }
198
199 /*
200  *  Traverses on subtree and retrieves more specific routes
201  *  for a given in args ip/depth prefix
202  *  last = NULL means the first invocation
203  */
204 struct rte_rib6_node *
205 rte_rib6_get_nxt(struct rte_rib6 *rib,
206         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
207         uint8_t depth, struct rte_rib6_node *last, int flag)
208 {
209         struct rte_rib6_node *tmp, *prev = NULL;
210         uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
211         int i;
212
213         if (unlikely(rib == NULL || ip == NULL || depth > RIB6_MAXDEPTH)) {
214                 rte_errno = EINVAL;
215                 return NULL;
216         }
217
218         for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
219                 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
220
221         if (last == NULL) {
222                 tmp = rib->tree;
223                 while ((tmp) && (tmp->depth < depth))
224                         tmp = get_nxt_node(tmp, tmp_ip);
225         } else {
226                 tmp = last;
227                 while ((tmp->parent != NULL) && (is_right_node(tmp) ||
228                                 (tmp->parent->right == NULL))) {
229                         tmp = tmp->parent;
230                         if (is_valid_node(tmp) &&
231                                         (is_covered(tmp->ip, tmp_ip, depth) &&
232                                         (tmp->depth > depth)))
233                                 return tmp;
234                 }
235                 tmp = (tmp->parent != NULL) ? tmp->parent->right : NULL;
236         }
237         while (tmp) {
238                 if (is_valid_node(tmp) &&
239                                 (is_covered(tmp->ip, tmp_ip, depth) &&
240                                 (tmp->depth > depth))) {
241                         prev = tmp;
242                         if (flag == RTE_RIB6_GET_NXT_COVER)
243                                 return prev;
244                 }
245                 tmp = (tmp->left != NULL) ? tmp->left : tmp->right;
246         }
247         return prev;
248 }
249
250 void
251 rte_rib6_remove(struct rte_rib6 *rib,
252         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
253 {
254         struct rte_rib6_node *cur, *prev, *child;
255
256         cur = rte_rib6_lookup_exact(rib, ip, depth);
257         if (cur == NULL)
258                 return;
259
260         --rib->cur_routes;
261         cur->flag &= ~RTE_RIB_VALID_NODE;
262         while (!is_valid_node(cur)) {
263                 if ((cur->left != NULL) && (cur->right != NULL))
264                         return;
265                 child = (cur->left == NULL) ? cur->right : cur->left;
266                 if (child != NULL)
267                         child->parent = cur->parent;
268                 if (cur->parent == NULL) {
269                         rib->tree = child;
270                         node_free(rib, cur);
271                         return;
272                 }
273                 if (cur->parent->left == cur)
274                         cur->parent->left = child;
275                 else
276                         cur->parent->right = child;
277                 prev = cur;
278                 cur = cur->parent;
279                 node_free(rib, prev);
280         }
281 }
282
283 struct rte_rib6_node *
284 rte_rib6_insert(struct rte_rib6 *rib,
285         const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
286 {
287         struct rte_rib6_node **tmp;
288         struct rte_rib6_node *prev = NULL;
289         struct rte_rib6_node *new_node = NULL;
290         struct rte_rib6_node *common_node = NULL;
291         uint8_t common_prefix[RTE_RIB6_IPV6_ADDR_SIZE];
292         uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
293         int i, d;
294         uint8_t common_depth, ip_xor;
295
296         if (unlikely((rib == NULL || ip == NULL || depth > RIB6_MAXDEPTH))) {
297                 rte_errno = EINVAL;
298                 return NULL;
299         }
300
301         tmp = &rib->tree;
302
303         for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
304                 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
305
306         new_node = rte_rib6_lookup_exact(rib, tmp_ip, depth);
307         if (new_node != NULL) {
308                 rte_errno = EEXIST;
309                 return NULL;
310         }
311
312         new_node = node_alloc(rib);
313         if (new_node == NULL) {
314                 rte_errno = ENOMEM;
315                 return NULL;
316         }
317         new_node->left = NULL;
318         new_node->right = NULL;
319         new_node->parent = NULL;
320         rte_rib6_copy_addr(new_node->ip, tmp_ip);
321         new_node->depth = depth;
322         new_node->flag = RTE_RIB_VALID_NODE;
323
324         /* traverse down the tree to find matching node or closest matching */
325         while (1) {
326                 /* insert as the last node in the branch */
327                 if (*tmp == NULL) {
328                         *tmp = new_node;
329                         new_node->parent = prev;
330                         ++rib->cur_routes;
331                         return *tmp;
332                 }
333                 /*
334                  * Intermediate node found.
335                  * Previous rte_rib6_lookup_exact() returned NULL
336                  * but node with proper search criteria is found.
337                  * Validate intermediate node and return.
338                  */
339                 if (rte_rib6_is_equal(tmp_ip, (*tmp)->ip) &&
340                                 (depth == (*tmp)->depth)) {
341                         node_free(rib, new_node);
342                         (*tmp)->flag |= RTE_RIB_VALID_NODE;
343                         ++rib->cur_routes;
344                         return *tmp;
345                 }
346
347                 if (!is_covered(tmp_ip, (*tmp)->ip, (*tmp)->depth) ||
348                                 ((*tmp)->depth >= depth)) {
349                         break;
350                 }
351                 prev = *tmp;
352
353                 tmp = (get_dir(tmp_ip, (*tmp)->depth)) ? &(*tmp)->right :
354                                 &(*tmp)->left;
355         }
356
357         /* closest node found, new_node should be inserted in the middle */
358         common_depth = RTE_MIN(depth, (*tmp)->depth);
359         for (i = 0, d = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++) {
360                 ip_xor = tmp_ip[i] ^ (*tmp)->ip[i];
361                 if (ip_xor == 0)
362                         d += 8;
363                 else {
364                         d += __builtin_clz(ip_xor << 24);
365                         break;
366                 }
367         }
368
369         common_depth = RTE_MIN(d, common_depth);
370
371         for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
372                 common_prefix[i] = tmp_ip[i] & get_msk_part(common_depth, i);
373
374         if (rte_rib6_is_equal(common_prefix, tmp_ip) &&
375                         (common_depth == depth)) {
376                 /* insert as a parent */
377                 if (get_dir((*tmp)->ip, depth))
378                         new_node->right = *tmp;
379                 else
380                         new_node->left = *tmp;
381                 new_node->parent = (*tmp)->parent;
382                 (*tmp)->parent = new_node;
383                 *tmp = new_node;
384         } else {
385                 /* create intermediate node */
386                 common_node = node_alloc(rib);
387                 if (common_node == NULL) {
388                         node_free(rib, new_node);
389                         rte_errno = ENOMEM;
390                         return NULL;
391                 }
392                 rte_rib6_copy_addr(common_node->ip, common_prefix);
393                 common_node->depth = common_depth;
394                 common_node->flag = 0;
395                 common_node->parent = (*tmp)->parent;
396                 new_node->parent = common_node;
397                 (*tmp)->parent = common_node;
398                 if (get_dir((*tmp)->ip, common_depth) == 1) {
399                         common_node->left = new_node;
400                         common_node->right = *tmp;
401                 } else {
402                         common_node->left = *tmp;
403                         common_node->right = new_node;
404                 }
405                 *tmp = common_node;
406         }
407         ++rib->cur_routes;
408         return new_node;
409 }
410
411 int
412 rte_rib6_get_ip(const struct rte_rib6_node *node,
413                 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
414 {
415         if (unlikely(node == NULL || ip == NULL)) {
416                 rte_errno = EINVAL;
417                 return -1;
418         }
419         rte_rib6_copy_addr(ip, node->ip);
420         return 0;
421 }
422
423 int
424 rte_rib6_get_depth(const struct rte_rib6_node *node, uint8_t *depth)
425 {
426         if (unlikely(node == NULL || depth == NULL)) {
427                 rte_errno = EINVAL;
428                 return -1;
429         }
430         *depth = node->depth;
431         return 0;
432 }
433
434 void *
435 rte_rib6_get_ext(struct rte_rib6_node *node)
436 {
437         return (node == NULL) ? NULL : &node->ext[0];
438 }
439
440 int
441 rte_rib6_get_nh(const struct rte_rib6_node *node, uint64_t *nh)
442 {
443         if (unlikely(node == NULL || nh == NULL)) {
444                 rte_errno = EINVAL;
445                 return -1;
446         }
447         *nh = node->nh;
448         return 0;
449 }
450
451 int
452 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh)
453 {
454         if (unlikely(node == NULL)) {
455                 rte_errno = EINVAL;
456                 return -1;
457         }
458         node->nh = nh;
459         return 0;
460 }
461
462 struct rte_rib6 *
463 rte_rib6_create(const char *name, int socket_id,
464                 const struct rte_rib6_conf *conf)
465 {
466         char mem_name[RTE_RIB6_NAMESIZE];
467         struct rte_rib6 *rib = NULL;
468         struct rte_tailq_entry *te;
469         struct rte_rib6_list *rib6_list;
470         struct rte_mempool *node_pool;
471
472         /* Check user arguments. */
473         if (unlikely(name == NULL || conf == NULL || conf->max_nodes <= 0)) {
474                 rte_errno = EINVAL;
475                 return NULL;
476         }
477
478         snprintf(mem_name, sizeof(mem_name), "MP_%s", name);
479         node_pool = rte_mempool_create(mem_name, conf->max_nodes,
480                 sizeof(struct rte_rib6_node) + conf->ext_sz, 0, 0,
481                 NULL, NULL, NULL, NULL, socket_id, 0);
482
483         if (node_pool == NULL) {
484                 RTE_LOG(ERR, LPM,
485                         "Can not allocate mempool for RIB6 %s\n", name);
486                 return NULL;
487         }
488
489         snprintf(mem_name, sizeof(mem_name), "RIB6_%s", name);
490         rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
491
492         rte_mcfg_tailq_write_lock();
493
494         /* guarantee there's no existing */
495         TAILQ_FOREACH(te, rib6_list, next) {
496                 rib = (struct rte_rib6 *)te->data;
497                 if (strncmp(name, rib->name, RTE_RIB6_NAMESIZE) == 0)
498                         break;
499         }
500         rib = NULL;
501         if (te != NULL) {
502                 rte_errno = EEXIST;
503                 goto exit;
504         }
505
506         /* allocate tailq entry */
507         te = rte_zmalloc("RIB6_TAILQ_ENTRY", sizeof(*te), 0);
508         if (unlikely(te == NULL)) {
509                 RTE_LOG(ERR, LPM,
510                         "Can not allocate tailq entry for RIB6 %s\n", name);
511                 rte_errno = ENOMEM;
512                 goto exit;
513         }
514
515         /* Allocate memory to store the RIB6 data structures. */
516         rib = rte_zmalloc_socket(mem_name,
517                 sizeof(struct rte_rib6), RTE_CACHE_LINE_SIZE, socket_id);
518         if (unlikely(rib == NULL)) {
519                 RTE_LOG(ERR, LPM, "RIB6 %s memory allocation failed\n", name);
520                 rte_errno = ENOMEM;
521                 goto free_te;
522         }
523
524         rte_strlcpy(rib->name, name, sizeof(rib->name));
525         rib->tree = NULL;
526         rib->max_nodes = conf->max_nodes;
527         rib->node_pool = node_pool;
528
529         te->data = (void *)rib;
530         TAILQ_INSERT_TAIL(rib6_list, te, next);
531
532         rte_mcfg_tailq_write_unlock();
533
534         return rib;
535
536 free_te:
537         rte_free(te);
538 exit:
539         rte_mcfg_tailq_write_unlock();
540         rte_mempool_free(node_pool);
541
542         return NULL;
543 }
544
545 struct rte_rib6 *
546 rte_rib6_find_existing(const char *name)
547 {
548         struct rte_rib6 *rib = NULL;
549         struct rte_tailq_entry *te;
550         struct rte_rib6_list *rib6_list;
551
552         if (unlikely(name == NULL)) {
553                 rte_errno = EINVAL;
554                 return NULL;
555         }
556
557         rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
558
559         rte_mcfg_tailq_read_lock();
560         TAILQ_FOREACH(te, rib6_list, next) {
561                 rib = (struct rte_rib6 *) te->data;
562                 if (strncmp(name, rib->name, RTE_RIB6_NAMESIZE) == 0)
563                         break;
564         }
565         rte_mcfg_tailq_read_unlock();
566
567         if (te == NULL) {
568                 rte_errno = ENOENT;
569                 return NULL;
570         }
571
572         return rib;
573 }
574
575 void
576 rte_rib6_free(struct rte_rib6 *rib)
577 {
578         struct rte_tailq_entry *te;
579         struct rte_rib6_list *rib6_list;
580         struct rte_rib6_node *tmp = NULL;
581
582         if (unlikely(rib == NULL)) {
583                 rte_errno = EINVAL;
584                 return;
585         }
586
587         rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
588
589         rte_mcfg_tailq_write_lock();
590
591         /* find our tailq entry */
592         TAILQ_FOREACH(te, rib6_list, next) {
593                 if (te->data == (void *)rib)
594                         break;
595         }
596         if (te != NULL)
597                 TAILQ_REMOVE(rib6_list, te, next);
598
599         rte_mcfg_tailq_write_unlock();
600
601         while ((tmp = rte_rib6_get_nxt(rib, 0, 0, tmp,
602                         RTE_RIB6_GET_NXT_ALL)) != NULL)
603                 rte_rib6_remove(rib, tmp->ip, tmp->depth);
604
605         rte_mempool_free(rib->node_pool);
606
607         rte_free(rib);
608         rte_free(te);
609 }