tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/map_tests/lpm_trie_map_basic_ops.c- Extension
.c- Size
- 36028 bytes
- Lines
- 1189
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
assert.herrno.hinttypes.hlinux/bpf.hpthread.hstdio.hstdlib.hstring.htime.hunistd.hendian.harpa/inet.hsys/time.hbpf/bpf.htest_maps.hbpf_util.h
Detected Declarations
struct tlpm_nodestruct lpm_trie_bytes_keystruct lpm_trie_int_keystruct lpm_mt_test_infofunction tlpm_clearfunction test_lpm_basicfunction assertfunction test_lpm_orderfunction test_lpm_mapfunction test_lpm_ipaddrfunction test_lpm_deletefunction test_lpm_get_next_keyfunction setup_lpm_mt_test_infofunction test_lpm_multi_threadfunction lpm_trie_createfunction test_lpm_trie_update_flagsfunction test_lpm_trie_update_full_mapfunction cmp_strfunction test_lpm_trie_iterate_strsfunction keyfunction test_lpm_trie_map_basic_ops
Annotated Snippet
struct tlpm_node {
struct tlpm_node *next;
size_t n_bits;
uint8_t key[];
};
struct lpm_trie_bytes_key {
union {
struct bpf_lpm_trie_key_hdr hdr;
__u32 prefixlen;
};
unsigned char data[8];
};
struct lpm_trie_int_key {
union {
struct bpf_lpm_trie_key_hdr hdr;
__u32 prefixlen;
};
unsigned int data;
};
static struct tlpm_node *tlpm_match(struct tlpm_node *list,
const uint8_t *key,
size_t n_bits);
static struct tlpm_node *tlpm_add(struct tlpm_node *list,
const uint8_t *key,
size_t n_bits)
{
struct tlpm_node *node;
size_t n;
n = (n_bits + 7) / 8;
/* 'overwrite' an equivalent entry if one already exists */
node = tlpm_match(list, key, n_bits);
if (node && node->n_bits == n_bits) {
memcpy(node->key, key, n);
return list;
}
/* add new entry with @key/@n_bits to @list and return new head */
node = malloc(sizeof(*node) + n);
assert(node);
node->next = list;
node->n_bits = n_bits;
memcpy(node->key, key, n);
return node;
}
static void tlpm_clear(struct tlpm_node *list)
{
struct tlpm_node *node;
/* free all entries in @list */
while ((node = list)) {
list = list->next;
free(node);
}
}
static struct tlpm_node *tlpm_match(struct tlpm_node *list,
const uint8_t *key,
size_t n_bits)
{
struct tlpm_node *best = NULL;
size_t i;
/* Perform longest prefix-match on @key/@n_bits. That is, iterate all
* entries and match each prefix against @key. Remember the "best"
* entry we find (i.e., the longest prefix that matches) and return it
* to the caller when done.
*/
for ( ; list; list = list->next) {
for (i = 0; i < n_bits && i < list->n_bits; ++i) {
if ((key[i / 8] & (1 << (7 - i % 8))) !=
(list->key[i / 8] & (1 << (7 - i % 8))))
break;
}
if (i >= list->n_bits) {
if (!best || i > best->n_bits)
best = list;
}
Annotation
- Immediate include surface: `assert.h`, `errno.h`, `inttypes.h`, `linux/bpf.h`, `pthread.h`, `stdio.h`, `stdlib.h`, `string.h`.
- Detected declarations: `struct tlpm_node`, `struct lpm_trie_bytes_key`, `struct lpm_trie_int_key`, `struct lpm_mt_test_info`, `function tlpm_clear`, `function test_lpm_basic`, `function assert`, `function test_lpm_order`, `function test_lpm_map`, `function test_lpm_ipaddr`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.