cls_cgroup.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 *sk)
  36. {
  37. u32 classid;
  38. classid = task_cls_classid(current);
  39. if (classid != sk->sk_classid)
  40. sk->sk_classid = 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. /* If there is an sk_classid we'll use that. */
  56. if (!skb->sk)
  57. return 0;
  58. classid = skb->sk->sk_classid;
  59. }
  60. return classid;
  61. }
  62. #else /* !CONFIG_CGROUP_NET_CLASSID */
  63. static inline void sock_update_classid(struct sock *sk)
  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 */