bpf-cgroup.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef _BPF_CGROUP_H
  2. #define _BPF_CGROUP_H
  3. #include <linux/jump_label.h>
  4. #include <uapi/linux/bpf.h>
  5. struct sock;
  6. struct cgroup;
  7. struct sk_buff;
  8. #ifdef CONFIG_CGROUP_BPF
  9. extern struct static_key_false cgroup_bpf_enabled_key;
  10. #define cgroup_bpf_enabled static_branch_unlikely(&cgroup_bpf_enabled_key)
  11. struct cgroup_bpf {
  12. /*
  13. * Store two sets of bpf_prog pointers, one for programs that are
  14. * pinned directly to this cgroup, and one for those that are effective
  15. * when this cgroup is accessed.
  16. */
  17. struct bpf_prog *prog[MAX_BPF_ATTACH_TYPE];
  18. struct bpf_prog *effective[MAX_BPF_ATTACH_TYPE];
  19. };
  20. void cgroup_bpf_put(struct cgroup *cgrp);
  21. void cgroup_bpf_inherit(struct cgroup *cgrp, struct cgroup *parent);
  22. void __cgroup_bpf_update(struct cgroup *cgrp,
  23. struct cgroup *parent,
  24. struct bpf_prog *prog,
  25. enum bpf_attach_type type);
  26. /* Wrapper for __cgroup_bpf_update() protected by cgroup_mutex */
  27. void cgroup_bpf_update(struct cgroup *cgrp,
  28. struct bpf_prog *prog,
  29. enum bpf_attach_type type);
  30. int __cgroup_bpf_run_filter(struct sock *sk,
  31. struct sk_buff *skb,
  32. enum bpf_attach_type type);
  33. /* Wrappers for __cgroup_bpf_run_filter() guarded by cgroup_bpf_enabled. */
  34. #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) \
  35. ({ \
  36. int __ret = 0; \
  37. if (cgroup_bpf_enabled) \
  38. __ret = __cgroup_bpf_run_filter(sk, skb, \
  39. BPF_CGROUP_INET_INGRESS); \
  40. \
  41. __ret; \
  42. })
  43. #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) \
  44. ({ \
  45. int __ret = 0; \
  46. if (cgroup_bpf_enabled && sk && sk == skb->sk) { \
  47. typeof(sk) __sk = sk_to_full_sk(sk); \
  48. if (sk_fullsock(__sk)) \
  49. __ret = __cgroup_bpf_run_filter(__sk, skb, \
  50. BPF_CGROUP_INET_EGRESS); \
  51. } \
  52. __ret; \
  53. })
  54. #else
  55. struct cgroup_bpf {};
  56. static inline void cgroup_bpf_put(struct cgroup *cgrp) {}
  57. static inline void cgroup_bpf_inherit(struct cgroup *cgrp,
  58. struct cgroup *parent) {}
  59. #define BPF_CGROUP_RUN_PROG_INET_INGRESS(sk,skb) ({ 0; })
  60. #define BPF_CGROUP_RUN_PROG_INET_EGRESS(sk,skb) ({ 0; })
  61. #endif /* CONFIG_CGROUP_BPF */
  62. #endif /* _BPF_CGROUP_H */