From: Robert Sanford <[email protected]>

count_empty_levels() treats the tbl8 levels from
RTE_ALIGN_CEIL(parent depth, 8) to RTE_ALIGN_CEIL(depth, 8) as the
node's own. When the parent depth is a multiple of 8 and the node has a
sibling, the sibling already occupies the level at the parent depth,
and the node counts it again. Each such ADD reserves one tbl8 group too
many, so ADD fails with -ENOSPC while groups are still free.

For example, 2001:db8:aa00::/48 and 2001:db8:aa80::/48 sit below a /40
branch node and use three tbl8 groups, but four are reserved. On a real
table of 921,787 IPv6 prefixes, 1,553,579 groups were reserved for
1,495,269 used.

Skip the level at the parent depth when the node has a sibling. DEL
runs the same count before it removes the node, so ADD and DEL stay
balanced.

Fixes: 7ff51847e270 ("fib6: fix tbl8 reservation drift in trie")
Cc: [email protected]

Signed-off-by: Robert Sanford <[email protected]>
---
 app/test/test_fib6.c    | 128 ++++++++++++++++++++++++++++++++++++++++
 lib/fib/trie.c          |   7 +++
 lib/rib/rib6_internal.h |   4 ++
 lib/rib/rte_rib6.c      |   9 +++
 4 files changed, 148 insertions(+)

diff --git a/app/test/test_fib6.c b/app/test/test_fib6.c
index 7204047941..ecc7b588ce 100644
--- a/app/test/test_fib6.c
+++ b/app/test/test_fib6.c
@@ -31,6 +31,9 @@ static int32_t test_drift_compression(void);
 static int32_t test_drift_multilevel(void);
 static int32_t test_drift_stress(void);
 static int32_t test_drift_tight_pool(void);
+static int32_t test_sibling_branch(void);
+static int32_t test_sibling_tbl24(void);
+static int32_t test_sibling_route(void);
 static int32_t test_tbl8_install_peak(void);
 static int32_t test_tbl8_install_peak_dq(void);
 static int32_t test_tbl8_dq_modify(void);
@@ -1034,6 +1037,128 @@ test_drift_tight_pool(void)
        return TEST_SUCCESS;
 }
 
+/*
+ * Two /48 siblings that differ in bit 41 sit below a /40 branch node and
+ * share the tbl8 groups for bits 25-32, 33-40 and 41-48: three in all. The
+ * second /48 must not reserve the /40 level again.
+ */
+static int32_t
+test_sibling_branch(void)
+{
+       struct rte_fib6_conf config = { 0 };
+       struct rte_fib6 *fib;
+       struct rte_ipv6_addr left = RTE_IPV6(0x2001, 0xdb8, 0xaa00, 0, 0, 0, 0, 
0);
+       struct rte_ipv6_addr right = RTE_IPV6(0x2001, 0xdb8, 0xaa80, 0, 0, 0, 
0, 0);
+       int ret;
+
+       config.max_routes = 16;
+       config.rib_ext_sz = 0;
+       config.default_nh = 0;
+       config.type = RTE_FIB6_TRIE;
+       config.trie.nh_sz = RTE_FIB6_TRIE_4B;
+       config.trie.num_tbl8 = 3;
+
+       fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
+       RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
+
+       ret = rte_fib6_add(fib, &left, 48, 1);
+       RTE_TEST_ASSERT(ret == 0, "ADD left /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_add(fib, &right, 48, 2);
+       RTE_TEST_ASSERT(ret == 0,
+               "ADD right /48 spuriously failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &left, 48);
+       RTE_TEST_ASSERT(ret == 0, "DEL left /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_add(fib, &left, 48, 1);
+       RTE_TEST_ASSERT(ret == 0,
+               "Re-ADD left /48 spuriously failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &right, 48);
+       RTE_TEST_ASSERT(ret == 0, "DEL right /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &left, 48);
+       RTE_TEST_ASSERT(ret == 0, "DEL left /48 failed (ret=%d)\n", ret);
+
+       rte_fib6_free(fib);
+       return TEST_SUCCESS;
+}
+
+/*
+ * Two /32 siblings that differ in bit 25 sit below a /24 branch node and
+ * share the one tbl8 group for bits 25-32.
+ */
+static int32_t
+test_sibling_tbl24(void)
+{
+       struct rte_fib6_conf config = { 0 };
+       struct rte_fib6 *fib;
+       struct rte_ipv6_addr left = RTE_IPV6(0x2001, 0x0d00, 0, 0, 0, 0, 0, 0);
+       struct rte_ipv6_addr right = RTE_IPV6(0x2001, 0x0d80, 0, 0, 0, 0, 0, 0);
+       int ret;
+
+       config.max_routes = 16;
+       config.rib_ext_sz = 0;
+       config.default_nh = 0;
+       config.type = RTE_FIB6_TRIE;
+       config.trie.nh_sz = RTE_FIB6_TRIE_4B;
+       config.trie.num_tbl8 = 1;
+
+       fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
+       RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
+
+       ret = rte_fib6_add(fib, &left, 32, 1);
+       RTE_TEST_ASSERT(ret == 0, "ADD left /32 failed (ret=%d)\n", ret);
+       ret = rte_fib6_add(fib, &right, 32, 2);
+       RTE_TEST_ASSERT(ret == 0,
+               "ADD right /32 spuriously failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &left, 32);
+       RTE_TEST_ASSERT(ret == 0, "DEL left /32 failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &right, 32);
+       RTE_TEST_ASSERT(ret == 0, "DEL right /32 failed (ret=%d)\n", ret);
+
+       rte_fib6_free(fib);
+       return TEST_SUCCESS;
+}
+
+/*
+ * A /40 route with two /48 children that differ in bit 41: the children share
+ * the tbl8 group for bits 41-48 below the /40, three groups in all.
+ */
+static int32_t
+test_sibling_route(void)
+{
+       struct rte_fib6_conf config = { 0 };
+       struct rte_fib6 *fib;
+       struct rte_ipv6_addr parent = RTE_IPV6(0x2001, 0xdb8, 0xaa00, 0, 0, 0, 
0, 0);
+       struct rte_ipv6_addr left = RTE_IPV6(0x2001, 0xdb8, 0xaa00, 0, 0, 0, 0, 
0);
+       struct rte_ipv6_addr right = RTE_IPV6(0x2001, 0xdb8, 0xaa80, 0, 0, 0, 
0, 0);
+       int ret;
+
+       config.max_routes = 16;
+       config.rib_ext_sz = 0;
+       config.default_nh = 0;
+       config.type = RTE_FIB6_TRIE;
+       config.trie.nh_sz = RTE_FIB6_TRIE_4B;
+       config.trie.num_tbl8 = 3;
+
+       fib = rte_fib6_create(__func__, SOCKET_ID_ANY, &config);
+       RTE_TEST_ASSERT(fib != NULL, "Failed to create FIB\n");
+
+       ret = rte_fib6_add(fib, &parent, 40, 1);
+       RTE_TEST_ASSERT(ret == 0, "ADD /40 failed (ret=%d)\n", ret);
+       ret = rte_fib6_add(fib, &left, 48, 2);
+       RTE_TEST_ASSERT(ret == 0, "ADD left /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_add(fib, &right, 48, 3);
+       RTE_TEST_ASSERT(ret == 0,
+               "ADD right /48 spuriously failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &left, 48);
+       RTE_TEST_ASSERT(ret == 0, "DEL left /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &right, 48);
+       RTE_TEST_ASSERT(ret == 0, "DEL right /48 failed (ret=%d)\n", ret);
+       ret = rte_fib6_delete(fib, &parent, 40);
+       RTE_TEST_ASSERT(ret == 0, "DEL /40 failed (ret=%d)\n", ret);
+
+       rte_fib6_free(fib);
+       return TEST_SUCCESS;
+}
+
 /*
  * Byte-aligned prefixes must not take a temporary tbl8: a /32 fits a pool of
  * one, a /24 and a /16 still fit once the pool is full, and the /32 can be
@@ -1292,6 +1417,9 @@ static struct unit_test_suite fib6_fast_tests = {
        TEST_CASE(test_drift_multilevel),
        TEST_CASE(test_drift_stress),
        TEST_CASE(test_drift_tight_pool),
+       TEST_CASE(test_sibling_branch),
+       TEST_CASE(test_sibling_tbl24),
+       TEST_CASE(test_sibling_route),
        TEST_CASE(test_tbl8_install_peak),
        TEST_CASE(test_tbl8_install_peak_dq),
        TEST_CASE(test_tbl8_dq_modify),
diff --git a/lib/fib/trie.c b/lib/fib/trie.c
index 871bb923ab..bfc230f0f6 100644
--- a/lib/fib/trie.c
+++ b/lib/fib/trie.c
@@ -592,6 +592,13 @@ count_empty_levels(const struct rte_rib6_node *node)
        parent = rte_rib6_get_parent(node);
        if (parent != NULL) {
                rte_rib6_get_depth(parent, &parent_depth);
+               /*
+                * A sibling shares the first parent_depth bits and is longer,
+                * so it already occupies the level at a byte-aligned parent
+                * depth: count from the next one.
+                */
+               if (rte_rib6_node_has_sibling(node))
+                       parent_depth++;
                parent_depth = RTE_MAX(parent_depth, 24);
        }
 
diff --git a/lib/rib/rib6_internal.h b/lib/rib/rib6_internal.h
index 6d5218a08f..2b4b76abe9 100644
--- a/lib/rib/rib6_internal.h
+++ b/lib/rib/rib6_internal.h
@@ -15,6 +15,10 @@ __rte_internal
 bool
 rte_rib6_node_has_children(const struct rte_rib6_node *node);
 
+__rte_internal
+bool
+rte_rib6_node_has_sibling(const struct rte_rib6_node *node);
+
 __rte_internal
 struct rte_rib6_node *
 rte_rib6_get_parent(const struct rte_rib6_node *node);
diff --git a/lib/rib/rte_rib6.c b/lib/rib/rte_rib6.c
index 808aac79ce..847bd95319 100644
--- a/lib/rib/rte_rib6.c
+++ b/lib/rib/rte_rib6.c
@@ -432,6 +432,15 @@ rte_rib6_node_has_children(const struct rte_rib6_node 
*node)
        return node->left != NULL || node->right != NULL;
 }
 
+RTE_EXPORT_INTERNAL_SYMBOL(rte_rib6_node_has_sibling)
+bool
+rte_rib6_node_has_sibling(const struct rte_rib6_node *node)
+{
+       const struct rte_rib6_node *parent = node->parent;
+
+       return parent != NULL && parent->left != NULL && parent->right != NULL;
+}
+
 RTE_EXPORT_INTERNAL_SYMBOL(rte_rib6_get_parent)
 struct rte_rib6_node *
 rte_rib6_get_parent(const struct rte_rib6_node *node)
-- 
2.34.1

Reply via email to