1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
3 * Copyright(c) 2019 Intel Corporation
10 #include <rte_eal_memconfig.h>
11 #include <rte_errno.h>
12 #include <rte_malloc.h>
13 #include <rte_mempool.h>
14 #include <rte_rwlock.h>
15 #include <rte_string_fns.h>
16 #include <rte_tailq.h>
20 #define RTE_RIB_VALID_NODE 1
21 #define RIB6_MAXDEPTH 128
22 /* Maximum length of a RIB6 name. */
23 #define RTE_RIB6_NAMESIZE 64
25 TAILQ_HEAD(rte_rib6_list, rte_tailq_entry);
26 static struct rte_tailq_elem rte_rib6_tailq = {
29 EAL_REGISTER_TAILQ(rte_rib6_tailq)
31 struct rte_rib6_node {
32 struct rte_rib6_node *left;
33 struct rte_rib6_node *right;
34 struct rte_rib6_node *parent;
36 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE];
39 __extension__ uint64_t ext[0];
43 char name[RTE_RIB6_NAMESIZE];
44 struct rte_rib6_node *tree;
45 struct rte_mempool *node_pool;
52 is_valid_node(struct rte_rib6_node *node)
54 return (node->flag & RTE_RIB_VALID_NODE) == RTE_RIB_VALID_NODE;
58 is_right_node(struct rte_rib6_node *node)
60 return node->parent->right == node;
64 * Check if ip1 is covered by ip2/depth prefix
67 is_covered(const uint8_t ip1[RTE_RIB6_IPV6_ADDR_SIZE],
68 const uint8_t ip2[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
72 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
73 if ((ip1[i] ^ ip2[i]) & get_msk_part(depth, i))
80 get_dir(const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
85 for (p_depth = depth; p_depth >= 8; p_depth -= 8)
88 msk = 1 << (7 - p_depth);
89 return (ip[i] & msk) != 0;
92 static inline struct rte_rib6_node *
93 get_nxt_node(struct rte_rib6_node *node,
94 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
96 return (get_dir(ip, node->depth)) ? node->right : node->left;
99 static struct rte_rib6_node *
100 node_alloc(struct rte_rib6 *rib)
102 struct rte_rib6_node *ent;
105 ret = rte_mempool_get(rib->node_pool, (void *)&ent);
106 if (unlikely(ret != 0))
113 node_free(struct rte_rib6 *rib, struct rte_rib6_node *ent)
116 rte_mempool_put(rib->node_pool, ent);
119 struct rte_rib6_node *
120 rte_rib6_lookup(struct rte_rib6 *rib,
121 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
123 struct rte_rib6_node *cur;
124 struct rte_rib6_node *prev = NULL;
126 if (unlikely(rib == NULL)) {
132 while ((cur != NULL) && is_covered(ip, cur->ip, cur->depth)) {
133 if (is_valid_node(cur))
135 cur = get_nxt_node(cur, ip);
140 struct rte_rib6_node *
141 rte_rib6_lookup_parent(struct rte_rib6_node *ent)
143 struct rte_rib6_node *tmp;
149 while ((tmp != NULL) && (!is_valid_node(tmp)))
155 struct rte_rib6_node *
156 rte_rib6_lookup_exact(struct rte_rib6 *rib,
157 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
159 struct rte_rib6_node *cur;
160 uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
163 if ((rib == NULL) || (ip == NULL) || (depth > RIB6_MAXDEPTH)) {
169 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
170 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
172 while (cur != NULL) {
173 if (rte_rib6_is_equal(cur->ip, tmp_ip) &&
174 (cur->depth == depth) &&
178 if (!(is_covered(tmp_ip, cur->ip, cur->depth)) ||
179 (cur->depth >= depth))
182 cur = get_nxt_node(cur, tmp_ip);
189 * Traverses on subtree and retreeves more specific routes
190 * for a given in args ip/depth prefix
191 * last = NULL means the first invocation
193 struct rte_rib6_node *
194 rte_rib6_get_nxt(struct rte_rib6 *rib,
195 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE],
196 uint8_t depth, struct rte_rib6_node *last, int flag)
198 struct rte_rib6_node *tmp, *prev = NULL;
199 uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
202 if ((rib == NULL) || (ip == NULL) || (depth > RIB6_MAXDEPTH)) {
207 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
208 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
212 while ((tmp) && (tmp->depth < depth))
213 tmp = get_nxt_node(tmp, tmp_ip);
216 while ((tmp->parent != NULL) && (is_right_node(tmp) ||
217 (tmp->parent->right == NULL))) {
219 if (is_valid_node(tmp) &&
220 (is_covered(tmp->ip, tmp_ip, depth) &&
221 (tmp->depth > depth)))
224 tmp = (tmp->parent != NULL) ? tmp->parent->right : NULL;
227 if (is_valid_node(tmp) &&
228 (is_covered(tmp->ip, tmp_ip, depth) &&
229 (tmp->depth > depth))) {
231 if (flag == RTE_RIB6_GET_NXT_COVER)
234 tmp = (tmp->left != NULL) ? tmp->left : tmp->right;
240 rte_rib6_remove(struct rte_rib6 *rib,
241 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
243 struct rte_rib6_node *cur, *prev, *child;
245 cur = rte_rib6_lookup_exact(rib, ip, depth);
250 cur->flag &= ~RTE_RIB_VALID_NODE;
251 while (!is_valid_node(cur)) {
252 if ((cur->left != NULL) && (cur->right != NULL))
254 child = (cur->left == NULL) ? cur->right : cur->left;
256 child->parent = cur->parent;
257 if (cur->parent == NULL) {
262 if (cur->parent->left == cur)
263 cur->parent->left = child;
265 cur->parent->right = child;
268 node_free(rib, prev);
272 struct rte_rib6_node *
273 rte_rib6_insert(struct rte_rib6 *rib,
274 const uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE], uint8_t depth)
276 struct rte_rib6_node **tmp;
277 struct rte_rib6_node *prev = NULL;
278 struct rte_rib6_node *new_node = NULL;
279 struct rte_rib6_node *common_node = NULL;
280 uint8_t common_prefix[RTE_RIB6_IPV6_ADDR_SIZE];
281 uint8_t tmp_ip[RTE_RIB6_IPV6_ADDR_SIZE];
283 uint8_t common_depth, ip_xor;
285 if (unlikely((rib == NULL) || (ip == NULL) ||
286 (depth > RIB6_MAXDEPTH))) {
293 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
294 tmp_ip[i] = ip[i] & get_msk_part(depth, i);
296 new_node = rte_rib6_lookup_exact(rib, tmp_ip, depth);
297 if (new_node != NULL) {
302 new_node = node_alloc(rib);
303 if (new_node == NULL) {
307 new_node->left = NULL;
308 new_node->right = NULL;
309 new_node->parent = NULL;
310 rte_rib6_copy_addr(new_node->ip, tmp_ip);
311 new_node->depth = depth;
312 new_node->flag = RTE_RIB_VALID_NODE;
314 /* traverse down the tree to find matching node or closest matching */
316 /* insert as the last node in the branch */
319 new_node->parent = prev;
324 * Intermediate node found.
325 * Previous rte_rib6_lookup_exact() returned NULL
326 * but node with proper search criteria is found.
327 * Validate intermediate node and return.
329 if (rte_rib6_is_equal(tmp_ip, (*tmp)->ip) &&
330 (depth == (*tmp)->depth)) {
331 node_free(rib, new_node);
332 (*tmp)->flag |= RTE_RIB_VALID_NODE;
337 if (!is_covered(tmp_ip, (*tmp)->ip, (*tmp)->depth) ||
338 ((*tmp)->depth >= depth)) {
343 tmp = (get_dir(tmp_ip, (*tmp)->depth)) ? &(*tmp)->right :
347 /* closest node found, new_node should be inserted in the middle */
348 common_depth = RTE_MIN(depth, (*tmp)->depth);
349 for (i = 0, d = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++) {
350 ip_xor = tmp_ip[i] ^ (*tmp)->ip[i];
354 d += __builtin_clz(ip_xor << 24);
359 common_depth = RTE_MIN(d, common_depth);
361 for (i = 0; i < RTE_RIB6_IPV6_ADDR_SIZE; i++)
362 common_prefix[i] = tmp_ip[i] & get_msk_part(common_depth, i);
364 if (rte_rib6_is_equal(common_prefix, tmp_ip) &&
365 (common_depth == depth)) {
366 /* insert as a parent */
367 if (get_dir((*tmp)->ip, depth))
368 new_node->right = *tmp;
370 new_node->left = *tmp;
371 new_node->parent = (*tmp)->parent;
372 (*tmp)->parent = new_node;
375 /* create intermediate node */
376 common_node = node_alloc(rib);
377 if (common_node == NULL) {
378 node_free(rib, new_node);
382 rte_rib6_copy_addr(common_node->ip, common_prefix);
383 common_node->depth = common_depth;
384 common_node->flag = 0;
385 common_node->parent = (*tmp)->parent;
386 new_node->parent = common_node;
387 (*tmp)->parent = common_node;
388 if (get_dir((*tmp)->ip, common_depth) == 1) {
389 common_node->left = new_node;
390 common_node->right = *tmp;
392 common_node->left = *tmp;
393 common_node->right = new_node;
402 rte_rib6_get_ip(const struct rte_rib6_node *node,
403 uint8_t ip[RTE_RIB6_IPV6_ADDR_SIZE])
405 if ((node == NULL) || (ip == NULL)) {
409 rte_rib6_copy_addr(ip, node->ip);
414 rte_rib6_get_depth(const struct rte_rib6_node *node, uint8_t *depth)
416 if ((node == NULL) || (depth == NULL)) {
420 *depth = node->depth;
425 rte_rib6_get_ext(struct rte_rib6_node *node)
427 return (node == NULL) ? NULL : &node->ext[0];
431 rte_rib6_get_nh(const struct rte_rib6_node *node, uint64_t *nh)
433 if ((node == NULL) || (nh == NULL)) {
442 rte_rib6_set_nh(struct rte_rib6_node *node, uint64_t nh)
453 rte_rib6_create(const char *name, int socket_id,
454 const struct rte_rib6_conf *conf)
456 char mem_name[RTE_RIB6_NAMESIZE];
457 struct rte_rib6 *rib = NULL;
458 struct rte_tailq_entry *te;
459 struct rte_rib6_list *rib6_list;
460 struct rte_mempool *node_pool;
462 /* Check user arguments. */
463 if (name == NULL || conf == NULL || conf->max_nodes <= 0) {
468 snprintf(mem_name, sizeof(mem_name), "MP_%s", name);
469 node_pool = rte_mempool_create(mem_name, conf->max_nodes,
470 sizeof(struct rte_rib6_node) + conf->ext_sz, 0, 0,
471 NULL, NULL, NULL, NULL, socket_id, 0);
473 if (node_pool == NULL) {
475 "Can not allocate mempool for RIB6 %s\n", name);
479 snprintf(mem_name, sizeof(mem_name), "RIB6_%s", name);
480 rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
482 rte_mcfg_tailq_write_lock();
484 /* guarantee there's no existing */
485 TAILQ_FOREACH(te, rib6_list, next) {
486 rib = (struct rte_rib6 *)te->data;
487 if (strncmp(name, rib->name, RTE_RIB6_NAMESIZE) == 0)
496 /* allocate tailq entry */
497 te = rte_zmalloc("RIB6_TAILQ_ENTRY", sizeof(*te), 0);
500 "Can not allocate tailq entry for RIB6 %s\n", name);
505 /* Allocate memory to store the RIB6 data structures. */
506 rib = rte_zmalloc_socket(mem_name,
507 sizeof(struct rte_rib6), RTE_CACHE_LINE_SIZE, socket_id);
509 RTE_LOG(ERR, LPM, "RIB6 %s memory allocation failed\n", name);
514 rte_strlcpy(rib->name, name, sizeof(rib->name));
516 rib->max_nodes = conf->max_nodes;
517 rib->node_pool = node_pool;
519 te->data = (void *)rib;
520 TAILQ_INSERT_TAIL(rib6_list, te, next);
522 rte_mcfg_tailq_write_unlock();
529 rte_mcfg_tailq_write_unlock();
530 rte_mempool_free(node_pool);
536 rte_rib6_find_existing(const char *name)
538 struct rte_rib6 *rib = NULL;
539 struct rte_tailq_entry *te;
540 struct rte_rib6_list *rib6_list;
542 if (unlikely(name == NULL)) {
547 rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
549 rte_mcfg_tailq_read_lock();
550 TAILQ_FOREACH(te, rib6_list, next) {
551 rib = (struct rte_rib6 *) te->data;
552 if (strncmp(name, rib->name, RTE_RIB6_NAMESIZE) == 0)
555 rte_mcfg_tailq_read_unlock();
566 rte_rib6_free(struct rte_rib6 *rib)
568 struct rte_tailq_entry *te;
569 struct rte_rib6_list *rib6_list;
570 struct rte_rib6_node *tmp = NULL;
572 if (unlikely(rib == NULL)) {
577 rib6_list = RTE_TAILQ_CAST(rte_rib6_tailq.head, rte_rib6_list);
579 rte_mcfg_tailq_write_lock();
581 /* find our tailq entry */
582 TAILQ_FOREACH(te, rib6_list, next) {
583 if (te->data == (void *)rib)
587 TAILQ_REMOVE(rib6_list, te, next);
589 rte_mcfg_tailq_write_unlock();
591 while ((tmp = rte_rib6_get_nxt(rib, 0, 0, tmp,
592 RTE_RIB6_GET_NXT_ALL)) != NULL)
593 rte_rib6_remove(rib, tmp->ip, tmp->depth);
595 rte_mempool_free(rib->node_pool);