linux/tools/testing/selftests/bpf/progs/test_skb_cgroup_id_kern.c
<<
>>
Prefs
   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 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,
  18};
  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
  45int _version SEC("version") = 1;
  46
  47char _license[] SEC("license") = "GPL";
  48