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.

Dependency Surface

Detected Declarations

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

Implementation Notes