netclassid_cgroup.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * net/core/netclassid_cgroup.c Classid Cgroupfs Handling
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Thomas Graf <tgraf@suug.ch>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/cgroup.h>
  14. #include <linux/fdtable.h>
  15. #include <net/cls_cgroup.h>
  16. #include <net/sock.h>
  17. static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
  18. {
  19. return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
  20. }
  21. struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  22. {
  23. return css_cls_state(task_css(p, net_cls_cgrp_id));
  24. }
  25. EXPORT_SYMBOL_GPL(task_cls_state);
  26. static struct cgroup_subsys_state *
  27. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  28. {
  29. struct cgroup_cls_state *cs;
  30. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  31. if (!cs)
  32. return ERR_PTR(-ENOMEM);
  33. return &cs->css;
  34. }
  35. static int cgrp_css_online(struct cgroup_subsys_state *css)
  36. {
  37. struct cgroup_cls_state *cs = css_cls_state(css);
  38. struct cgroup_cls_state *parent = css_cls_state(css->parent);
  39. if (parent)
  40. cs->classid = parent->classid;
  41. return 0;
  42. }
  43. static void cgrp_css_free(struct cgroup_subsys_state *css)
  44. {
  45. kfree(css_cls_state(css));
  46. }
  47. static int update_classid(const void *v, struct file *file, unsigned n)
  48. {
  49. int err;
  50. struct socket *sock = sock_from_file(file, &err);
  51. if (sock)
  52. sock->sk->sk_classid = (u32)(unsigned long)v;
  53. return 0;
  54. }
  55. static void cgrp_attach(struct cgroup_subsys_state *css,
  56. struct cgroup_taskset *tset)
  57. {
  58. struct cgroup_cls_state *cs = css_cls_state(css);
  59. void *v = (void *)(unsigned long)cs->classid;
  60. struct task_struct *p;
  61. cgroup_taskset_for_each(p, tset) {
  62. task_lock(p);
  63. iterate_fd(p->files, 0, update_classid, v);
  64. task_unlock(p);
  65. }
  66. }
  67. static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
  68. {
  69. return css_cls_state(css)->classid;
  70. }
  71. static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
  72. u64 value)
  73. {
  74. css_cls_state(css)->classid = (u32) value;
  75. return 0;
  76. }
  77. static struct cftype ss_files[] = {
  78. {
  79. .name = "classid",
  80. .read_u64 = read_classid,
  81. .write_u64 = write_classid,
  82. },
  83. { } /* terminate */
  84. };
  85. struct cgroup_subsys net_cls_cgrp_subsys = {
  86. .css_alloc = cgrp_css_alloc,
  87. .css_online = cgrp_css_online,
  88. .css_free = cgrp_css_free,
  89. .attach = cgrp_attach,
  90. .legacy_cftypes = ss_files,
  91. };