0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-11-09 20:58:44 +00:00

Refactor printing prefixes in trie

This commit is contained in:
Igor Putovny 2023-12-20 12:22:53 +01:00
parent 518f3969e5
commit b265df526c

View File

@ -568,47 +568,87 @@ get_trie_depth(const struct trie_node *node)
} }
static void static void
print_prefixes_helper(const struct trie_node *node, struct net_addr_ip4 addr, int depth) print_prefixes_ip4_helper(const struct trie_node *node, struct net_addr_ip4 *addr, int depth)
{ {
assert(node != NULL); assert(node != NULL);
if (is_leaf(node)) if (is_leaf(node))
{ {
log("%I4/%d\t-> %p", addr.prefix, addr.pxlen, node->bucket); log("%I4/%d\t-> %p", addr->prefix, addr->pxlen, node->bucket);
return; return;
} }
if (node->bucket != NULL) if (node->bucket != NULL)
{ {
log("%I4/%d\t-> %p", addr.prefix, addr.pxlen, node->bucket); log("%I4/%d\t-> %p", addr->prefix, addr->pxlen, node->bucket);
} }
if (node->child[0]) if (node->child[0])
{ {
struct net_addr_ip4 new = { ip4_clrbit(&addr->prefix, depth);
.prefix = (_I(addr.prefix) | (0 << (31 - depth))), addr->pxlen = depth + 1;
.pxlen = depth + 1, print_prefixes_ip4_helper(node->child[0], addr, depth + 1);
};
print_prefixes_helper(node->child[0], new, depth + 1);
} }
if (node->child[1]) if (node->child[1])
{ {
struct net_addr_ip4 new = { ip4_setbit(&addr->prefix, depth);
.prefix = (_I(addr.prefix) | (1 << (31 - depth))), addr->pxlen = depth + 1;
.pxlen = depth + 1, print_prefixes_ip4_helper(node->child[1], addr, depth + 1);
}; }
}
print_prefixes_helper(node->child[1], new, depth + 1);
static void
print_prefixes_ip6_helper(const struct trie_node *node, struct net_addr_ip6 *addr, int depth)
{
assert(node != NULL);
if (is_leaf(node))
{
log("%I6/%d\t-> %p", addr->prefix, addr->pxlen, node->bucket);
return;
}
if (node->bucket != NULL)
{
log("%I6/%d\n-> %p", addr->prefix, addr->pxlen, node->bucket);
}
if (node->child[0])
{
ip6_clrbit(&addr->prefix, depth);
addr->pxlen = depth + 1;
print_prefixes_ip6_helper(node->child[0], addr, depth + 1);
}
if (node->child[1])
{
ip6_setbit(&addr->prefix, depth);
addr->pxlen = depth + 1;
print_prefixes_ip6_helper(node->child[1], addr, depth + 1);
}
}
static void
print_prefixes(const struct trie_node *node, int type)
{
if (type == NET_IP4)
{
struct net_addr_ip4 *addr = allocz(sizeof(struct net_addr_ip4));
print_prefixes_ip4_helper(node, addr, 0);
}
else if (type == NET_IP6)
{
struct net_addr_ip6 *addr = allocz(sizeof(struct net_addr_ip6));
print_prefixes_ip6_helper(node, addr, 0);
}
}
} }
} }
static void static void
print_prefixes(const struct trie_node *node)
{ {
struct net_addr_ip4 addr = { 0 };
print_prefixes_helper(node, addr, 0);
} }
/* /*
@ -620,19 +660,19 @@ calculate_trie(void *p)
struct aggregator_proto *proto = (struct aggregator_proto *)p; struct aggregator_proto *proto = (struct aggregator_proto *)p;
log("====PREFIXES BEFORE ===="); log("====PREFIXES BEFORE ====");
print_prefixes(proto->root); print_prefixes(proto->root, NET_IP4);
first_pass(proto->root, proto->trie_slab); first_pass(proto->root, proto->trie_slab);
log("====FIRST PASS===="); log("====FIRST PASS====");
print_prefixes(proto->root); print_prefixes(proto->root, NET_IP4);
second_pass(proto->root); second_pass(proto->root);
log("====SECOND PASS===="); log("====SECOND PASS====");
print_prefixes(proto->root); print_prefixes(proto->root, NET_IP4);
third_pass(proto->root); third_pass(proto->root);
log("====THIRD PASS===="); log("====THIRD PASS====");
print_prefixes(proto->root); print_prefixes(proto->root, NET_IP4);
} }
/* /*