cls_cgroup.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * cls_cgroup.h Control Group Classifier
  3. *
  4. * Authors: Thomas Graf <tgraf@suug.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _NET_CLS_CGROUP_H
  13. #define _NET_CLS_CGROUP_H
  14. #include <linux/cgroup.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/rcupdate.h>
  17. #include <net/sock.h>
  18. #ifdef CONFIG_CGROUP_NET_CLASSID
  19. struct cgroup_cls_state {
  20. struct cgroup_subsys_state css;
  21. u32 classid;
  22. };
  23. struct cgroup_cls_state *task_cls_state(struct task_struct *p);
  24. static inline u32 task_cls_classid(struct task_struct *p)
  25. {
  26. u32 classid;
  27. if (in_interrupt())
  28. return 0;
  29. rcu_read_lock();
  30. classid = container_of(task_css(p, net_cls_cgrp_id),
  31. struct cgroup_cls_state, css)->classid;
  32. rcu_read_unlock();
  33. return classid;
  34. }
  35. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  36. {
  37. u32 classid;
  38. classid = task_cls_classid(current);
  39. sock_cgroup_set_classid(skcd, classid);
  40. }
  41. static inline u32 task_get_classid(const struct sk_buff *skb)
  42. {
  43. u32 classid = task_cls_state(current)->classid;
  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. /* If there is an sock_cgroup_classid we'll use that. */
  55. if (!skb->sk)
  56. return 0;
  57. classid = sock_cgroup_classid(&skb->sk->sk_cgrp_data);
  58. }
  59. return classid;
  60. }
  61. #else /* !CONFIG_CGROUP_NET_CLASSID */
  62. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  63. {
  64. }
  65. static inline u32 task_get_classid(const struct sk_buff *skb)
  66. {
  67. return 0;
  68. }
  69. #endif /* CONFIG_CGROUP_NET_CLASSID */
  70. #endif /* _NET_CLS_CGROUP_H */