1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2018 Facebook
3
4#include <linux/bpf.h>
5#include <linux/pkt_cls.h>
6
7#include <string.h>
8
9#include <bpf/bpf_helpers.h>
10
11#define NUM_CGROUP_LEVELS	4
12
13struct {
14	__uint(type, BPF_MAP_TYPE_ARRAY);
15	__type(key, __u32);
16	__type(value, __u64);
17	__uint(max_entries, NUM_CGROUP_LEVELS);
18} cgroup_ids SEC(".maps");
19
20static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)
21{
22	__u64 id;
23
24	/* [1] &level passed to external function that may change it, it's
25	 *     incompatible with loop unroll.
26	 */
27	id = bpf_skb_ancestor_cgroup_id(skb, level);
28	bpf_map_update_elem(&cgroup_ids, &level, &id, 0);
29}
30
31SEC("cgroup_id_logger")
32int log_cgroup_id(struct __sk_buff *skb)
33{
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.
36	 */
37	log_nth_level(skb, 0);
38	log_nth_level(skb, 1);
39	log_nth_level(skb, 2);
40	log_nth_level(skb, 3);
41
42	return TC_ACT_OK;
43}
44
45char _license[] SEC("license") = "GPL";
46