tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/xdp_bpf2bpf.c- Extension
.c- Size
- 4517 bytes
- Lines
- 162
- 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
test_progs.hnetwork_helpers.hnet/if.htest_xdp.skel.htest_xdp_bpf2bpf.skel.h
Detected Declarations
struct metastruct test_ctx_sfunction on_samplefunction run_xdp_bpf2bpf_pkt_sizefunction test_xdp_bpf2bpf
Annotated Snippet
struct meta {
int ifindex;
int pkt_len;
};
struct test_ctx_s {
bool passed;
int pkt_size;
};
struct test_ctx_s test_ctx;
static void on_sample(void *ctx, int cpu, void *data, __u32 size)
{
struct meta *meta = (struct meta *)data;
struct ipv4_packet *trace_pkt_v4 = data + sizeof(*meta);
unsigned char *raw_pkt = data + sizeof(*meta);
struct test_ctx_s *tst_ctx = ctx;
ASSERT_GE(size, sizeof(pkt_v4) + sizeof(*meta), "check_size");
ASSERT_EQ(meta->ifindex, if_nametoindex("lo"), "check_meta_ifindex");
ASSERT_EQ(meta->pkt_len, tst_ctx->pkt_size, "check_meta_pkt_len");
ASSERT_EQ(memcmp(trace_pkt_v4, &pkt_v4, sizeof(pkt_v4)), 0,
"check_packet_content");
if (meta->pkt_len > sizeof(pkt_v4)) {
for (int i = 0; i < meta->pkt_len - sizeof(pkt_v4); i++)
ASSERT_EQ(raw_pkt[i + sizeof(pkt_v4)], (unsigned char)i,
"check_packet_content");
}
tst_ctx->passed = true;
}
#define BUF_SZ 9000
static void run_xdp_bpf2bpf_pkt_size(int pkt_fd, struct perf_buffer *pb,
struct test_xdp_bpf2bpf *ftrace_skel,
int pkt_size)
{
__u8 *buf, *buf_in;
int err;
LIBBPF_OPTS(bpf_test_run_opts, topts);
if (!ASSERT_LE(pkt_size, BUF_SZ, "pkt_size") ||
!ASSERT_GE(pkt_size, sizeof(pkt_v4), "pkt_size"))
return;
buf_in = malloc(BUF_SZ);
if (!ASSERT_OK_PTR(buf_in, "buf_in malloc()"))
return;
buf = malloc(BUF_SZ);
if (!ASSERT_OK_PTR(buf, "buf malloc()")) {
free(buf_in);
return;
}
test_ctx.passed = false;
test_ctx.pkt_size = pkt_size;
memcpy(buf_in, &pkt_v4, sizeof(pkt_v4));
if (pkt_size > sizeof(pkt_v4)) {
for (int i = 0; i < (pkt_size - sizeof(pkt_v4)); i++)
buf_in[i + sizeof(pkt_v4)] = i;
}
/* Run test program */
topts.data_in = buf_in;
topts.data_size_in = pkt_size;
topts.data_out = buf;
topts.data_size_out = BUF_SZ;
err = bpf_prog_test_run_opts(pkt_fd, &topts);
ASSERT_OK(err, "ipv4");
ASSERT_EQ(topts.retval, XDP_PASS, "ipv4 retval");
ASSERT_EQ(topts.data_size_out, pkt_size, "ipv4 size");
/* Make sure bpf_xdp_output() was triggered and it sent the expected
* data to the perf ring buffer.
*/
err = perf_buffer__poll(pb, 100);
ASSERT_GE(err, 0, "perf_buffer__poll");
ASSERT_TRUE(test_ctx.passed, "test passed");
/* Verify test results */
ASSERT_EQ(ftrace_skel->bss->test_result_fentry, if_nametoindex("lo"),
"fentry result");
ASSERT_EQ(ftrace_skel->bss->test_result_fexit, XDP_PASS, "fexit result");
Annotation
- Immediate include surface: `test_progs.h`, `network_helpers.h`, `net/if.h`, `test_xdp.skel.h`, `test_xdp_bpf2bpf.skel.h`.
- Detected declarations: `struct meta`, `struct test_ctx_s`, `function on_sample`, `function run_xdp_bpf2bpf_pkt_size`, `function test_xdp_bpf2bpf`.
- 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.