Re: help with pctrie

From: Mark Johnston <markj_at_freebsd.org>
Date: Sun, 03 Aug 2025 21:44:29 UTC
On Sun, Aug 03, 2025 at 08:22:43PM +0100, Lexi Winter wrote:
> hello,
> 
> i'm trying to use <sys/pctrie.h> and running into a problem i don't
> really understand.  since it's largely undocumented, this is probably
> something i'm doing wrong rather than bug, but i can't work out what.
> 
> so, i have a function that adds a new entry to a pctrie:
> https://git.le-fay.org/freebsd/src/tree/sys/net/bridge_fdb.c?h=lf/dev/bridge-pctrie#n98
> 
> in testing, this passes the "returning okay" printf, so it definitely
> called FDB_PCTRIE_INSERT().
> 
> then i have a very basic iterator interface which just wraps the pctrie
> iterator:
> https://git.le-fay.org/freebsd/src/tree/sys/net/bridge_fdb.c?h=lf/dev/bridge-pctrie#n196
> 
> i'm trying to use the iterator to iterate the pctrie here:
> https://git.le-fay.org/freebsd/src/tree/sys/net/if_bridge.c?h=lf/dev/bridge-pctrie#n1715

I think the problem there is that you're missing a
FDB_PCTRIE_ITER_LOOKUP() to return the value at the current position in
the iterator.  The general pattern should be something like:

pctrie_iter_init(&iter);
for (node = FDB_PCTRIE_ITER_LOOKUP(&iter); node != NULL;
     node = FDB_PCTRIE_ITER_NEXT(&iter)) {

In particular, PCTRIE_ITER_NEXT is a combined "advance the iterator to
the next key and look up the value there" operation.

> this prints "count=1" (which confirms that fdb_add_or_replace() did
> actually run), but fdb_iter_next() immediately returns NULL and the "got
> a host" printf never triggers.
> 
> so it seems like the entry i added to the pctrie has gotten lost
> somewhere and the tree is still empty, even though i don't expect
> it to be.
> 
> could anyone help me understand what i'm doing wrong?