github.com/castai/kvisor@v1.7.1-0.20240516114728-b3572a2607b5/pkg/ebpftracer/c/headers/common/cgroups.h (about)

     1  #ifndef __COMMON_CGROUPS_H__
     2  #define __COMMON_CGROUPS_H__
     3  
     4  #include <vmlinux.h>
     5  #include <vmlinux_flavors.h>
     6  
     7  #include <common/common.h>
     8  
     9  // PROTOTYPES
    10  
    11  statfunc const char *get_cgroup_dirname(struct cgroup *);
    12  statfunc const u64 get_cgroup_id(struct cgroup *);
    13  statfunc const u32 get_cgroup_hierarchy_id(struct cgroup *);
    14  statfunc const u64 get_cgroup_v1_subsys0_id(struct task_struct *);
    15  
    16  // FUNCTIONS
    17  
    18  statfunc const char *get_cgroup_dirname(struct cgroup *cgrp)
    19  {
    20      return BPF_CORE_READ(cgrp, kn, name);
    21  }
    22  
    23  statfunc const u64 get_cgroup_id(struct cgroup *cgrp)
    24  {
    25      struct kernfs_node *kn = BPF_CORE_READ(cgrp, kn);
    26  
    27      if (kn == NULL)
    28          return 0;
    29  
    30      u64 id; // was union kernfs_node_id before 5.5, can read it as u64 in both situations
    31  
    32      if (bpf_core_type_exists(union kernfs_node_id)) {
    33          struct kernfs_node___older_v55 *kn_old = (void *) kn;
    34          struct kernfs_node___rh8 *kn_rh8 = (void *) kn;
    35  
    36          if (bpf_core_field_exists(kn_rh8->id)) {
    37              // RHEL8 has both types declared: union and u64:
    38              //     kn->id
    39              //     rh->rh_kabi_hidden_172->id
    40              // pointing to the same data
    41              bpf_core_read(&id, sizeof(u64), &kn_rh8->id);
    42          } else {
    43              // all other regular kernels bellow v5.5
    44              bpf_core_read(&id, sizeof(u64), &kn_old->id);
    45          }
    46  
    47      } else {
    48          // kernel v5.5 and above
    49          bpf_core_read(&id, sizeof(u64), &kn->id);
    50      }
    51  
    52      // we only care about the lower 8 bits of the cgroup id, as the uppper 8 appear to be thread id
    53      return id & 0xFFFFFFFF;
    54  }
    55  
    56  statfunc const u32 get_cgroup_hierarchy_id(struct cgroup *cgrp)
    57  {
    58      return BPF_CORE_READ(cgrp, root, hierarchy_id);
    59  }
    60  
    61  statfunc const u64 get_cgroup_v1_subsys0_id(struct task_struct *task)
    62  {
    63      struct cgroup *cgroup = BPF_CORE_READ(task, cgroups, subsys[0], cgroup);
    64      return get_cgroup_id(cgroup);
    65  }
    66  
    67  #endif