Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2018 Facebook
0003 
0004 #include <linux/bpf.h>
0005 #include <linux/pkt_cls.h>
0006 
0007 #include <string.h>
0008 
0009 #include <bpf/bpf_helpers.h>
0010 
0011 #define NUM_CGROUP_LEVELS   4
0012 
0013 struct {
0014     __uint(type, BPF_MAP_TYPE_ARRAY);
0015     __type(key, __u32);
0016     __type(value, __u64);
0017     __uint(max_entries, NUM_CGROUP_LEVELS);
0018 } cgroup_ids SEC(".maps");
0019 
0020 static __always_inline void log_nth_level(struct __sk_buff *skb, __u32 level)
0021 {
0022     __u64 id;
0023 
0024     /* [1] &level passed to external function that may change it, it's
0025      *     incompatible with loop unroll.
0026      */
0027     id = bpf_skb_ancestor_cgroup_id(skb, level);
0028     bpf_map_update_elem(&cgroup_ids, &level, &id, 0);
0029 }
0030 
0031 SEC("cgroup_id_logger")
0032 int log_cgroup_id(struct __sk_buff *skb)
0033 {
0034     /* Loop unroll can't be used here due to [1]. Unrolling manually.
0035      * Number of calls should be in sync with NUM_CGROUP_LEVELS.
0036      */
0037     log_nth_level(skb, 0);
0038     log_nth_level(skb, 1);
0039     log_nth_level(skb, 2);
0040     log_nth_level(skb, 3);
0041 
0042     return TC_ACT_OK;
0043 }
0044 
0045 char _license[] SEC("license") = "GPL";