netprio_cgroup.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * net/core/netprio_cgroup.c Priority Control Group
  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: Neil Horman <nhorman@tuxdriver.com>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/cgroup.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/atomic.h>
  21. #include <net/rtnetlink.h>
  22. #include <net/pkt_cls.h>
  23. #include <net/sock.h>
  24. #include <net/netprio_cgroup.h>
  25. #include <linux/fdtable.h>
  26. #define PRIOIDX_SZ 128
  27. static unsigned long prioidx_map[PRIOIDX_SZ];
  28. static DEFINE_SPINLOCK(prioidx_map_lock);
  29. static atomic_t max_prioidx = ATOMIC_INIT(0);
  30. static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
  31. {
  32. return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
  33. struct cgroup_netprio_state, css);
  34. }
  35. static int get_prioidx(u32 *prio)
  36. {
  37. unsigned long flags;
  38. u32 prioidx;
  39. spin_lock_irqsave(&prioidx_map_lock, flags);
  40. prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
  41. if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
  42. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  43. return -ENOSPC;
  44. }
  45. set_bit(prioidx, prioidx_map);
  46. if (atomic_read(&max_prioidx) < prioidx)
  47. atomic_set(&max_prioidx, prioidx);
  48. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  49. *prio = prioidx;
  50. return 0;
  51. }
  52. static void put_prioidx(u32 idx)
  53. {
  54. unsigned long flags;
  55. spin_lock_irqsave(&prioidx_map_lock, flags);
  56. clear_bit(idx, prioidx_map);
  57. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  58. }
  59. static int extend_netdev_table(struct net_device *dev, u32 new_len)
  60. {
  61. size_t new_size = sizeof(struct netprio_map) +
  62. ((sizeof(u32) * new_len));
  63. struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
  64. struct netprio_map *old_priomap;
  65. old_priomap = rtnl_dereference(dev->priomap);
  66. if (!new_priomap) {
  67. pr_warn("Unable to alloc new priomap!\n");
  68. return -ENOMEM;
  69. }
  70. if (old_priomap)
  71. memcpy(new_priomap->priomap, old_priomap->priomap,
  72. old_priomap->priomap_len *
  73. sizeof(old_priomap->priomap[0]));
  74. new_priomap->priomap_len = new_len;
  75. rcu_assign_pointer(dev->priomap, new_priomap);
  76. if (old_priomap)
  77. kfree_rcu(old_priomap, rcu);
  78. return 0;
  79. }
  80. static int write_update_netdev_table(struct net_device *dev)
  81. {
  82. int ret = 0;
  83. u32 max_len;
  84. struct netprio_map *map;
  85. max_len = atomic_read(&max_prioidx) + 1;
  86. map = rtnl_dereference(dev->priomap);
  87. if (!map || map->priomap_len < max_len)
  88. ret = extend_netdev_table(dev, max_len);
  89. return ret;
  90. }
  91. static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
  92. {
  93. struct cgroup_netprio_state *cs;
  94. int ret = -EINVAL;
  95. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  96. if (!cs)
  97. return ERR_PTR(-ENOMEM);
  98. if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx)
  99. goto out;
  100. ret = get_prioidx(&cs->prioidx);
  101. if (ret < 0) {
  102. pr_warn("No space in priority index array\n");
  103. goto out;
  104. }
  105. return &cs->css;
  106. out:
  107. kfree(cs);
  108. return ERR_PTR(ret);
  109. }
  110. static void cgrp_css_free(struct cgroup *cgrp)
  111. {
  112. struct cgroup_netprio_state *cs;
  113. struct net_device *dev;
  114. struct netprio_map *map;
  115. cs = cgrp_netprio_state(cgrp);
  116. rtnl_lock();
  117. for_each_netdev(&init_net, dev) {
  118. map = rtnl_dereference(dev->priomap);
  119. if (map && cs->prioidx < map->priomap_len)
  120. map->priomap[cs->prioidx] = 0;
  121. }
  122. rtnl_unlock();
  123. put_prioidx(cs->prioidx);
  124. kfree(cs);
  125. }
  126. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  127. {
  128. return (u64)cgrp_netprio_state(cgrp)->prioidx;
  129. }
  130. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  131. struct cgroup_map_cb *cb)
  132. {
  133. struct net_device *dev;
  134. u32 prioidx = cgrp_netprio_state(cont)->prioidx;
  135. u32 priority;
  136. struct netprio_map *map;
  137. rcu_read_lock();
  138. for_each_netdev_rcu(&init_net, dev) {
  139. map = rcu_dereference(dev->priomap);
  140. priority = (map && prioidx < map->priomap_len) ? map->priomap[prioidx] : 0;
  141. cb->fill(cb, dev->name, priority);
  142. }
  143. rcu_read_unlock();
  144. return 0;
  145. }
  146. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  147. const char *buffer)
  148. {
  149. u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
  150. char devname[IFNAMSIZ + 1];
  151. struct net_device *dev;
  152. struct netprio_map *map;
  153. u32 prio;
  154. int ret;
  155. if (sscanf(buffer, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  156. return -EINVAL;
  157. dev = dev_get_by_name(&init_net, devname);
  158. if (!dev)
  159. return -ENODEV;
  160. rtnl_lock();
  161. ret = write_update_netdev_table(dev);
  162. if (ret)
  163. goto out_unlock;
  164. map = rtnl_dereference(dev->priomap);
  165. if (map)
  166. map->priomap[prioidx] = prio;
  167. out_unlock:
  168. rtnl_unlock();
  169. dev_put(dev);
  170. return ret;
  171. }
  172. static int update_netprio(const void *v, struct file *file, unsigned n)
  173. {
  174. int err;
  175. struct socket *sock = sock_from_file(file, &err);
  176. if (sock)
  177. sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v;
  178. return 0;
  179. }
  180. void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  181. {
  182. struct task_struct *p;
  183. void *v;
  184. cgroup_taskset_for_each(p, cgrp, tset) {
  185. task_lock(p);
  186. v = (void *)(unsigned long)task_netprioidx(p);
  187. iterate_fd(p->files, 0, update_netprio, v);
  188. task_unlock(p);
  189. }
  190. }
  191. static struct cftype ss_files[] = {
  192. {
  193. .name = "prioidx",
  194. .read_u64 = read_prioidx,
  195. },
  196. {
  197. .name = "ifpriomap",
  198. .read_map = read_priomap,
  199. .write_string = write_priomap,
  200. },
  201. { } /* terminate */
  202. };
  203. struct cgroup_subsys net_prio_subsys = {
  204. .name = "net_prio",
  205. .css_alloc = cgrp_css_alloc,
  206. .css_free = cgrp_css_free,
  207. .attach = net_prio_attach,
  208. .subsys_id = net_prio_subsys_id,
  209. .base_cftypes = ss_files,
  210. .module = THIS_MODULE,
  211. /*
  212. * net_prio has artificial limit on the number of cgroups and
  213. * disallows nesting making it impossible to co-mount it with other
  214. * hierarchical subsystems. Remove the artificially low PRIOIDX_SZ
  215. * limit and properly nest configuration such that children follow
  216. * their parents' configurations by default and are allowed to
  217. * override and remove the following.
  218. */
  219. .broken_hierarchy = true,
  220. };
  221. static int netprio_device_event(struct notifier_block *unused,
  222. unsigned long event, void *ptr)
  223. {
  224. struct net_device *dev = ptr;
  225. struct netprio_map *old;
  226. /*
  227. * Note this is called with rtnl_lock held so we have update side
  228. * protection on our rcu assignments
  229. */
  230. switch (event) {
  231. case NETDEV_UNREGISTER:
  232. old = rtnl_dereference(dev->priomap);
  233. RCU_INIT_POINTER(dev->priomap, NULL);
  234. if (old)
  235. kfree_rcu(old, rcu);
  236. break;
  237. }
  238. return NOTIFY_DONE;
  239. }
  240. static struct notifier_block netprio_device_notifier = {
  241. .notifier_call = netprio_device_event
  242. };
  243. static int __init init_cgroup_netprio(void)
  244. {
  245. int ret;
  246. ret = cgroup_load_subsys(&net_prio_subsys);
  247. if (ret)
  248. goto out;
  249. register_netdevice_notifier(&netprio_device_notifier);
  250. out:
  251. return ret;
  252. }
  253. static void __exit exit_cgroup_netprio(void)
  254. {
  255. struct netprio_map *old;
  256. struct net_device *dev;
  257. unregister_netdevice_notifier(&netprio_device_notifier);
  258. cgroup_unload_subsys(&net_prio_subsys);
  259. rtnl_lock();
  260. for_each_netdev(&init_net, dev) {
  261. old = rtnl_dereference(dev->priomap);
  262. RCU_INIT_POINTER(dev->priomap, NULL);
  263. if (old)
  264. kfree_rcu(old, rcu);
  265. }
  266. rtnl_unlock();
  267. }
  268. module_init(init_cgroup_netprio);
  269. module_exit(exit_cgroup_netprio);
  270. MODULE_LICENSE("GPL v2");