xref: /linux/tools/testing/selftests/bpf/progs/cgroup_iter.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2022 Google */
3 #include <vmlinux.h>
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_tracing.h>
6 
7 char _license[] SEC("license") = "GPL";
8 int terminate_early = 0;
9 u64 terminal_cgroup = 0;
10 
11 static inline u64 cgroup_id(struct cgroup *cgrp)
12 {
13 	return cgrp->kn->id;
14 }
15 
16 SEC("iter/cgroup")
17 int cgroup_id_printer(struct bpf_iter__cgroup *ctx)
18 {
19 	struct seq_file *seq = ctx->meta->seq;
20 	struct cgroup *cgrp = ctx->cgroup;
21 
22 	/* epilogue */
23 	if (cgrp == NULL) {
24 		BPF_SEQ_PRINTF(seq, "epilogue\n");
25 		return 0;
26 	}
27 
28 	/* prologue */
29 	if (ctx->meta->seq_num == 0)
30 		BPF_SEQ_PRINTF(seq, "prologue\n");
31 
32 	BPF_SEQ_PRINTF(seq, "%8llu\n", cgroup_id(cgrp));
33 
34 	if (terminal_cgroup == cgroup_id(cgrp))
35 		return 1;
36 
37 	return terminate_early ? 1 : 0;
38 }
39