samples/bpf/hbm.c
Source file repositories/reference/linux-study-clean/samples/bpf/hbm.c
File Facts
- System
- Linux kernel
- Corpus path
samples/bpf/hbm.c- Extension
.c- Size
- 13936 bytes
- Lines
- 516
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- 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
stdio.hstdlib.hassert.hsys/time.hunistd.herrno.hfcntl.hlinux/unistd.hlinux/compiler.hlinux/bpf.hbpf/bpf.hgetopt.hcgroup_helpers.hhbm.hbpf_util.hbpf/libbpf.h
Detected Declarations
function read_trace_pipe2function do_errorfunction prog_loadfunction bpf_object__for_each_programfunction run_bpf_progfunction Usagefunction main
Annotated Snippet
if (sz > 0) {
buf[sz] = 0;
puts(buf);
if (outf != NULL) {
fprintf(outf, "%s\n", buf);
fflush(outf);
}
}
}
}
static void do_error(char *msg, bool errno_flag)
{
if (errno_flag)
printf("ERROR: %s, errno: %d\n", msg, errno);
else
printf("ERROR: %s\n", msg);
exit(1);
}
static int prog_load(char *prog)
{
struct bpf_program *pos;
const char *sec_name;
obj = bpf_object__open_file(prog, NULL);
if (libbpf_get_error(obj)) {
printf("ERROR: opening BPF object file failed\n");
return 1;
}
/* load BPF program */
if (bpf_object__load(obj)) {
printf("ERROR: loading BPF object file failed\n");
goto err;
}
bpf_object__for_each_program(pos, obj) {
sec_name = bpf_program__section_name(pos);
if (sec_name && !strcmp(sec_name, "cgroup_skb/egress")) {
bpf_prog = pos;
break;
}
}
if (!bpf_prog) {
printf("ERROR: finding a prog in obj file failed\n");
goto err;
}
queue_stats_fd = bpf_object__find_map_fd_by_name(obj, "queue_stats");
if (queue_stats_fd < 0) {
printf("ERROR: finding a map in obj file failed\n");
goto err;
}
return 0;
err:
bpf_object__close(obj);
return 1;
}
static int run_bpf_prog(char *prog, int cg_id)
{
struct hbm_queue_stats qstats = {0};
char cg_dir[100], cg_pin_path[100];
struct bpf_link *link = NULL;
int key = 0;
int cg1 = 0;
int rc = 0;
sprintf(cg_dir, "/hbm%d", cg_id);
rc = prog_load(prog);
if (rc != 0)
return rc;
if (setup_cgroup_environment()) {
printf("ERROR: setting cgroup environment\n");
goto err;
}
cg1 = create_and_get_cgroup(cg_dir);
if (!cg1) {
printf("ERROR: create_and_get_cgroup\n");
goto err;
}
if (join_cgroup(cg_dir)) {
printf("ERROR: join_cgroup\n");
goto err;
}
Annotation
- Immediate include surface: `stdio.h`, `stdlib.h`, `assert.h`, `sys/time.h`, `unistd.h`, `errno.h`, `fcntl.h`, `linux/unistd.h`.
- Detected declarations: `function read_trace_pipe2`, `function do_error`, `function prog_load`, `function bpf_object__for_each_program`, `function run_bpf_prog`, `function Usage`, `function main`.
- Atlas domain: Support Tooling And Documentation / samples.
- 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.