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