test_skb_cgroup_id_kern.c 1.0 KB

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