tools/testing/selftests/bpf/prog_tests/bpftool_metadata.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/bpftool_metadata.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/bpftool_metadata.c- Extension
.c- Size
- 3598 bytes
- Lines
- 145
- 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
bpftool_helpers.htest_progs.hlinux/bpf.hstring.hunistd.hfcntl.hsys/stat.hstdbool.h
Detected Declarations
struct test_descfunction setupfunction cleanupfunction check_metadatafunction run_testfunction test_bpftool_metadata
Annotated Snippet
struct test_desc {
char *name;
char *bpf_prog;
char *bpffs_path;
char *expected_output[MAX_TOKENS_TO_CHECK];
char *expected_output_json[MAX_TOKENS_TO_CHECK];
char *metadata_map_name;
};
static int setup(struct test_desc *test)
{
return mkdir(BPFFS_DIR, 0700);
}
static void cleanup(struct test_desc *test)
{
unlink(test->bpffs_path);
rmdir(BPFFS_DIR);
}
static int check_metadata(char *buf, char * const *tokens, int count)
{
int i;
for (i = 0; i < count && tokens[i]; i++)
if (!strstr(buf, tokens[i]))
return 1;
return 0;
}
static void run_test(struct test_desc *test)
{
int ret;
char cmd[MAX_BPFTOOL_CMD_LEN];
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog load %s %s",
test->bpf_prog, test->bpffs_path);
if (!ASSERT_GT(ret, 0, "format prog insert command"))
return;
ret = run_bpftool_command(cmd);
if (!ASSERT_OK(ret, "load program"))
return;
/* Check output with default format */
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog show pinned %s",
test->bpffs_path);
if (!ASSERT_GT(ret, 0, "format pinned prog check command"))
return;
ret = get_bpftool_command_output(cmd, output,
MAX_BPFTOOL_OUTPUT_LEN);
if (ASSERT_OK(ret, "get program info")) {
ret = check_metadata(output, test->expected_output,
ARRAY_SIZE(test->expected_output));
ASSERT_OK(ret, "find metadata");
}
/* Check output with json format */
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "prog -j show pinned %s",
test->bpffs_path);
if (!ASSERT_GT(ret, 0, "format pinned prog check command in json"))
return;
ret = get_bpftool_command_output(cmd, output,
MAX_BPFTOOL_OUTPUT_LEN);
if (ASSERT_OK(ret, "get program info in json")) {
ret = check_metadata(output, test->expected_output_json,
ARRAY_SIZE(test->expected_output_json));
ASSERT_OK(ret, "find metadata in json");
}
/* Check that the corresponding map can be found and accessed */
ret = snprintf(cmd, MAX_BPFTOOL_CMD_LEN, "map show name %s",
test->metadata_map_name);
if (!ASSERT_GT(ret, 0, "format map check command"))
return;
ASSERT_OK(run_bpftool_command(cmd), "access metadata map");
}
static struct test_desc tests[] = {
{
.name = "metadata_unused",
.bpf_prog = BPF_FILE_UNUSED,
.bpffs_path = BPFFS_UNUSED,
.expected_output = {
"a = \"foo\"",
"b = 1"
},
.expected_output_json = {
"\"metadata\":{\"a\":\"foo\",\"b\":1}"
},
Annotation
- Immediate include surface: `bpftool_helpers.h`, `test_progs.h`, `linux/bpf.h`, `string.h`, `unistd.h`, `fcntl.h`, `sys/stat.h`, `stdbool.h`.
- Detected declarations: `struct test_desc`, `function setup`, `function cleanup`, `function check_metadata`, `function run_test`, `function test_bpftool_metadata`.
- 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.