cls_cgroup.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include <net/inet_sock.h>
  19. #ifdef CONFIG_CGROUP_NET_CLASSID
  20. struct cgroup_cls_state {
  21. struct cgroup_subsys_state css;
  22. u32 classid;
  23. };
  24. struct cgroup_cls_state *task_cls_state(struct task_struct *p);
  25. static inline u32 task_cls_classid(struct task_struct *p)
  26. {
  27. u32 classid;
  28. if (in_interrupt())
  29. return 0;
  30. rcu_read_lock();
  31. classid = container_of(task_css(p, net_cls_cgrp_id),
  32. struct cgroup_cls_state, css)->classid;
  33. rcu_read_unlock();
  34. return classid;
  35. }
  36. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  37. {
  38. u32 classid;
  39. classid = task_cls_classid(current);
  40. sock_cgroup_set_classid(skcd, classid);
  41. }
  42. static inline u32 task_get_classid(const struct sk_buff *skb)
  43. {
  44. u32 classid = task_cls_state(current)->classid;
  45. /* Due to the nature of the classifier it is required to ignore all
  46. * packets originating from softirq context as accessing `current'
  47. * would lead to false results.
  48. *
  49. * This test assumes that all callers of dev_queue_xmit() explicitly
  50. * disable bh. Knowing this, it is possible to detect softirq based
  51. * calls by looking at the number of nested bh disable calls because
  52. * softirqs always disables bh.
  53. */
  54. if (in_serving_softirq()) {
  55. struct sock *sk = skb_to_full_sk(skb);
  56. /* If there is an sock_cgroup_classid we'll use that. */
  57. if (!sk || !sk_fullsock(sk))
  58. return 0;
  59. classid = sock_cgroup_classid(&sk->sk_cgrp_data);
  60. }
  61. return classid;
  62. }
  63. #else /* !CONFIG_CGROUP_NET_CLASSID */
  64. static inline void sock_update_classid(struct sock_cgroup_data *skcd)
  65. {
  66. }
  67. static inline u32 task_get_classid(const struct sk_buff *skb)
  68. {
  69. return 0;
  70. }
  71. #endif /* CONFIG_CGROUP_NET_CLASSID */
  72. #endif /* _NET_CLS_CGROUP_H */