1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2018 Facebook
5 #include <linux/pkt_cls.h>
9 #include <bpf/bpf_helpers.h>
11 #define NUM_CGROUP_LEVELS 4
13 struct bpf_map_def
SEC("maps") cgroup_ids
= {
14 .type
= BPF_MAP_TYPE_ARRAY
,
15 .key_size
= sizeof(__u32
),
16 .value_size
= sizeof(__u64
),
17 .max_entries
= NUM_CGROUP_LEVELS
,
20 static __always_inline
void log_nth_level(struct __sk_buff
*skb
, __u32 level
)
24 /* [1] &level passed to external function that may change it, it's
25 * incompatible with loop unroll.
27 id
= bpf_skb_ancestor_cgroup_id(skb
, level
);
28 bpf_map_update_elem(&cgroup_ids
, &level
, &id
, 0);
31 SEC("cgroup_id_logger")
32 int log_cgroup_id(struct __sk_buff
*skb
)
34 /* Loop unroll can't be used here due to [1]. Unrolling manually.
35 * Number of calls should be in sync with NUM_CGROUP_LEVELS.
37 log_nth_level(skb
, 0);
38 log_nth_level(skb
, 1);
39 log_nth_level(skb
, 2);
40 log_nth_level(skb
, 3);
45 int _version
SEC("version") = 1;
47 char _license
[] SEC("license") = "GPL";