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 TAILQ_HEAD(rte_rib_list, rte_tailq_entry);
21 static struct rte_tailq_elem rte_rib_tailq = {
24 EAL_REGISTER_TAILQ(rte_rib_tailq)
26 #define RTE_RIB_VALID_NODE 1
27 /* Maximum depth value possible for IPv4 RIB. */
28 #define RIB_MAXDEPTH 32
29 /* Maximum length of a RIB name. */
30 #define RTE_RIB_NAMESIZE 64
33 struct rte_rib_node *left;
34 struct rte_rib_node *right;
35 struct rte_rib_node *parent;
40 __extension__ uint64_t ext[0];
44 char name[RTE_RIB_NAMESIZE];
45 struct rte_rib_node *tree;
46 struct rte_mempool *node_pool;
53 is_valid_node(struct rte_rib_node *node)
55 return (node->flag & RTE_RIB_VALID_NODE) == RTE_RIB_VALID_NODE;
59 is_right_node(struct rte_rib_node *node)
61 return node->parent->right == node;
65 * Check if ip1 is covered by ip2/depth prefix
68 is_covered(uint32_t ip1, uint32_t ip2, uint8_t depth)
70 return ((ip1 ^ ip2) & rte_rib_depth_to_mask(depth)) == 0;
73 static inline struct rte_rib_node *
74 get_nxt_node(struct rte_rib_node *node, uint32_t ip)
76 return (ip & (1 << (31 - node->depth))) ? node->right : node->left;
79 static struct rte_rib_node *
80 node_alloc(struct rte_rib *rib)
82 struct rte_rib_node *ent;
85 ret = rte_mempool_get(rib->node_pool, (void *)&ent);
86 if (unlikely(ret != 0))
93 node_free(struct rte_rib *rib, struct rte_rib_node *ent)
96 rte_mempool_put(rib->node_pool, ent);
100 rte_rib_lookup(struct rte_rib *rib, uint32_t ip)
102 struct rte_rib_node *cur, *prev = NULL;
110 while ((cur != NULL) && is_covered(ip, cur->ip, cur->depth)) {
111 if (is_valid_node(cur))
113 cur = get_nxt_node(cur, ip);
118 struct rte_rib_node *
119 rte_rib_lookup_parent(struct rte_rib_node *ent)
121 struct rte_rib_node *tmp;
126 while ((tmp != NULL) && !is_valid_node(tmp))
131 static struct rte_rib_node *
132 __rib_lookup_exact(struct rte_rib *rib, uint32_t ip, uint8_t depth)
134 struct rte_rib_node *cur;
137 while (cur != NULL) {
138 if ((cur->ip == ip) && (cur->depth == depth) &&
141 if ((cur->depth > depth) ||
142 !is_covered(ip, cur->ip, cur->depth))
144 cur = get_nxt_node(cur, ip);
149 struct rte_rib_node *
150 rte_rib_lookup_exact(struct rte_rib *rib, uint32_t ip, uint8_t depth)
152 if ((rib == NULL) || (depth > RIB_MAXDEPTH)) {
156 ip &= rte_rib_depth_to_mask(depth);
158 return __rib_lookup_exact(rib, ip, depth);
162 * Traverses on subtree and retrieves more specific routes
163 * for a given in args ip/depth prefix
164 * last = NULL means the first invocation
166 struct rte_rib_node *
167 rte_rib_get_nxt(struct rte_rib *rib, uint32_t ip,
168 uint8_t depth, struct rte_rib_node *last, int flag)
170 struct rte_rib_node *tmp, *prev = NULL;
172 if ((rib == NULL) || (depth > RIB_MAXDEPTH)) {
179 while ((tmp) && (tmp->depth < depth))
180 tmp = get_nxt_node(tmp, ip);
183 while ((tmp->parent != NULL) && (is_right_node(tmp) ||
184 (tmp->parent->right == NULL))) {
186 if (is_valid_node(tmp) &&
187 (is_covered(tmp->ip, ip, depth) &&
188 (tmp->depth > depth)))
191 tmp = (tmp->parent) ? tmp->parent->right : NULL;
194 if (is_valid_node(tmp) &&
195 (is_covered(tmp->ip, ip, depth) &&
196 (tmp->depth > depth))) {
198 if (flag == RTE_RIB_GET_NXT_COVER)
201 tmp = (tmp->left) ? tmp->left : tmp->right;
207 rte_rib_remove(struct rte_rib *rib, uint32_t ip, uint8_t depth)
209 struct rte_rib_node *cur, *prev, *child;
211 cur = rte_rib_lookup_exact(rib, ip, depth);
216 cur->flag &= ~RTE_RIB_VALID_NODE;
217 while (!is_valid_node(cur)) {
218 if ((cur->left != NULL) && (cur->right != NULL))
220 child = (cur->left == NULL) ? cur->right : cur->left;
222 child->parent = cur->parent;
223 if (cur->parent == NULL) {
228 if (cur->parent->left == cur)
229 cur->parent->left = child;
231 cur->parent->right = child;
234 node_free(rib, prev);
238 struct rte_rib_node *
239 rte_rib_insert(struct rte_rib *rib, uint32_t ip, uint8_t depth)
241 struct rte_rib_node **tmp;
242 struct rte_rib_node *prev = NULL;
243 struct rte_rib_node *new_node = NULL;
244 struct rte_rib_node *common_node = NULL;
246 uint32_t common_prefix;
247 uint8_t common_depth;
249 if ((rib == NULL) || (depth > RIB_MAXDEPTH)) {
255 ip &= rte_rib_depth_to_mask(depth);
256 new_node = __rib_lookup_exact(rib, ip, depth);
257 if (new_node != NULL) {
262 new_node = node_alloc(rib);
263 if (new_node == NULL) {
267 new_node->left = NULL;
268 new_node->right = NULL;
269 new_node->parent = NULL;
271 new_node->depth = depth;
272 new_node->flag = RTE_RIB_VALID_NODE;
274 /* traverse down the tree to find matching node or closest matching */
276 /* insert as the last node in the branch */
279 new_node->parent = prev;
284 * Intermediate node found.
285 * Previous rte_rib_lookup_exact() returned NULL
286 * but node with proper search criteria is found.
287 * Validate intermediate node and return.
289 if ((ip == (*tmp)->ip) && (depth == (*tmp)->depth)) {
290 node_free(rib, new_node);
291 (*tmp)->flag |= RTE_RIB_VALID_NODE;
296 if ((d >= depth) || !is_covered(ip, (*tmp)->ip, d))
299 tmp = (ip & (1 << (31 - d))) ? &(*tmp)->right : &(*tmp)->left;
301 /* closest node found, new_node should be inserted in the middle */
302 common_depth = RTE_MIN(depth, (*tmp)->depth);
303 common_prefix = ip ^ (*tmp)->ip;
304 d = (common_prefix == 0) ? 32 : __builtin_clz(common_prefix);
306 common_depth = RTE_MIN(d, common_depth);
307 common_prefix = ip & rte_rib_depth_to_mask(common_depth);
308 if ((common_prefix == ip) && (common_depth == depth)) {
309 /* insert as a parent */
310 if ((*tmp)->ip & (1 << (31 - depth)))
311 new_node->right = *tmp;
313 new_node->left = *tmp;
314 new_node->parent = (*tmp)->parent;
315 (*tmp)->parent = new_node;
318 /* create intermediate node */
319 common_node = node_alloc(rib);
320 if (common_node == NULL) {
321 node_free(rib, new_node);
325 common_node->ip = common_prefix;
326 common_node->depth = common_depth;
327 common_node->flag = 0;
328 common_node->parent = (*tmp)->parent;
329 new_node->parent = common_node;
330 (*tmp)->parent = common_node;
331 if ((new_node->ip & (1 << (31 - common_depth))) == 0) {
332 common_node->left = new_node;
333 common_node->right = *tmp;
335 common_node->left = *tmp;
336 common_node->right = new_node;
345 rte_rib_get_ip(const struct rte_rib_node *node, uint32_t *ip)
347 if ((node == NULL) || (ip == NULL)) {
356 rte_rib_get_depth(const struct rte_rib_node *node, uint8_t *depth)
358 if ((node == NULL) || (depth == NULL)) {
362 *depth = node->depth;
367 rte_rib_get_ext(struct rte_rib_node *node)
369 return (node == NULL) ? NULL : &node->ext[0];
373 rte_rib_get_nh(const struct rte_rib_node *node, uint64_t *nh)
375 if ((node == NULL) || (nh == NULL)) {
384 rte_rib_set_nh(struct rte_rib_node *node, uint64_t nh)
395 rte_rib_create(const char *name, int socket_id, const struct rte_rib_conf *conf)
397 char mem_name[RTE_RIB_NAMESIZE];
398 struct rte_rib *rib = NULL;
399 struct rte_tailq_entry *te;
400 struct rte_rib_list *rib_list;
401 struct rte_mempool *node_pool;
403 /* Check user arguments. */
404 if (name == NULL || conf == NULL || conf->max_nodes <= 0) {
409 snprintf(mem_name, sizeof(mem_name), "MP_%s", name);
410 node_pool = rte_mempool_create(mem_name, conf->max_nodes,
411 sizeof(struct rte_rib_node) + conf->ext_sz, 0, 0,
412 NULL, NULL, NULL, NULL, socket_id, 0);
414 if (node_pool == NULL) {
416 "Can not allocate mempool for RIB %s\n", name);
420 snprintf(mem_name, sizeof(mem_name), "RIB_%s", name);
421 rib_list = RTE_TAILQ_CAST(rte_rib_tailq.head, rte_rib_list);
423 rte_mcfg_tailq_write_lock();
425 /* guarantee there's no existing */
426 TAILQ_FOREACH(te, rib_list, next) {
427 rib = (struct rte_rib *)te->data;
428 if (strncmp(name, rib->name, RTE_RIB_NAMESIZE) == 0)
437 /* allocate tailq entry */
438 te = rte_zmalloc("RIB_TAILQ_ENTRY", sizeof(*te), 0);
441 "Can not allocate tailq entry for RIB %s\n", name);
446 /* Allocate memory to store the RIB data structures. */
447 rib = rte_zmalloc_socket(mem_name,
448 sizeof(struct rte_rib), RTE_CACHE_LINE_SIZE, socket_id);
450 RTE_LOG(ERR, LPM, "RIB %s memory allocation failed\n", name);
455 rte_strlcpy(rib->name, name, sizeof(rib->name));
457 rib->max_nodes = conf->max_nodes;
458 rib->node_pool = node_pool;
459 te->data = (void *)rib;
460 TAILQ_INSERT_TAIL(rib_list, te, next);
462 rte_mcfg_tailq_write_unlock();
469 rte_mcfg_tailq_write_unlock();
470 rte_mempool_free(node_pool);
476 rte_rib_find_existing(const char *name)
478 struct rte_rib *rib = NULL;
479 struct rte_tailq_entry *te;
480 struct rte_rib_list *rib_list;
482 rib_list = RTE_TAILQ_CAST(rte_rib_tailq.head, rte_rib_list);
484 rte_mcfg_tailq_read_lock();
485 TAILQ_FOREACH(te, rib_list, next) {
486 rib = (struct rte_rib *) te->data;
487 if (strncmp(name, rib->name, RTE_RIB_NAMESIZE) == 0)
490 rte_mcfg_tailq_read_unlock();
501 rte_rib_free(struct rte_rib *rib)
503 struct rte_tailq_entry *te;
504 struct rte_rib_list *rib_list;
505 struct rte_rib_node *tmp = NULL;
510 rib_list = RTE_TAILQ_CAST(rte_rib_tailq.head, rte_rib_list);
512 rte_mcfg_tailq_write_lock();
514 /* find our tailq entry */
515 TAILQ_FOREACH(te, rib_list, next) {
516 if (te->data == (void *)rib)
520 TAILQ_REMOVE(rib_list, te, next);
522 rte_mcfg_tailq_write_unlock();
524 while ((tmp = rte_rib_get_nxt(rib, 0, 0, tmp,
525 RTE_RIB_GET_NXT_ALL)) != NULL)
526 rte_rib_remove(rib, tmp->ip, tmp->depth);
528 rte_mempool_free(rib->node_pool);