cls_cgroup.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * cls_cgroup.h Control Group Classifier
  4. *
  5. * Authors: Thomas Graf <[email protected]>
  6. */
  7. #ifndef _NET_CLS_CGROUP_H
  8. #define _NET_CLS_CGROUP_H
  9. #include <linux/cgroup.h>
  10. #include <linux/hardirq.h>
  11. #include <linux/rcupdate.h>
  12. #include <net/sock.h>
  13. #include <net/inet_sock.h>
  14. #ifdef CONFIG_CGROUP_NET_CLASSID
  15. struct cgroup_cls_state {
  16. struct cgroup_subsys_state css;
  17. u32 classid;
  18. };
  19. struct cgroup_cls_state *task_cls_state(struct task_struct *p);
  20. static inline u32 task_cls_classid(struct task_struct *p)
  21. {
  22. u32 classid;
  23. if (in_interrupt())
  24. return 0;
  25. rcu_read_lock();
  26. classid = container_of(task_css(p, net_cls_cgrp_id),
  27. struct cgroup_cls_state, css)->classid;
  28. rcu_read_unlock();
  29. return classid;
  30. }
  31. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  32. {
  33. u32 classid;
  34. classid = task_cls_classid(current);
  35. sock_cgroup_set_classid(skcd, classid);
  36. }
  37. static inline u32 __task_get_classid(struct task_struct *task)
  38. {
  39. return task_cls_state(task)->classid;
  40. }
  41. static inline u32 task_get_classid(const struct sk_buff *skb)
  42. {
  43. u32 classid = __task_get_classid(current);
  44. /* Due to the nature of the classifier it is required to ignore all
  45. * packets originating from softirq context as accessing `current'
  46. * would lead to false results.
  47. *
  48. * This test assumes that all callers of dev_queue_xmit() explicitly
  49. * disable bh. Knowing this, it is possible to detect softirq based
  50. * calls by looking at the number of nested bh disable calls because
  51. * softirqs always disables bh.
  52. */
  53. if (in_serving_softirq()) {
  54. struct sock *sk = skb_to_full_sk(skb);
  55. /* If there is an sock_cgroup_classid we'll use that. */
  56. if (!sk || !sk_fullsock(sk))
  57. return 0;
  58. classid = sock_cgroup_classid(&sk->sk_cgrp_data);
  59. }
  60. return classid;
  61. }
  62. #else /* !CONFIG_CGROUP_NET_CLASSID */
  63. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  64. {
  65. }
  66. static inline u32 task_get_classid(const struct sk_buff *skb)
  67. {
  68. return 0;
  69. }
  70. #endif /* CONFIG_CGROUP_NET_CLASSID */
  71. #endif /* _NET_CLS_CGROUP_H */