netprio_cgroup.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 PRIOMAP_MIN_SZ 128
  27. static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
  28. {
  29. return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
  30. struct cgroup_netprio_state, css);
  31. }
  32. /*
  33. * Extend @dev->priomap so that it's large enough to accomodate
  34. * @target_idx. @dev->priomap.priomap_len > @target_idx after successful
  35. * return. Must be called under rtnl lock.
  36. */
  37. static int extend_netdev_table(struct net_device *dev, u32 target_idx)
  38. {
  39. struct netprio_map *old, *new;
  40. size_t new_sz, new_len;
  41. /* is the existing priomap large enough? */
  42. old = rtnl_dereference(dev->priomap);
  43. if (old && old->priomap_len > target_idx)
  44. return 0;
  45. /*
  46. * Determine the new size. Let's keep it power-of-two. We start
  47. * from PRIOMAP_MIN_SZ and double it until it's large enough to
  48. * accommodate @target_idx.
  49. */
  50. new_sz = PRIOMAP_MIN_SZ;
  51. while (true) {
  52. new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
  53. sizeof(new->priomap[0]);
  54. if (new_len > target_idx)
  55. break;
  56. new_sz *= 2;
  57. /* overflowed? */
  58. if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
  59. return -ENOSPC;
  60. }
  61. /* allocate & copy */
  62. new = kzalloc(new_sz, GFP_KERNEL);
  63. if (!new) {
  64. pr_warn("Unable to alloc new priomap!\n");
  65. return -ENOMEM;
  66. }
  67. if (old)
  68. memcpy(new->priomap, old->priomap,
  69. old->priomap_len * sizeof(old->priomap[0]));
  70. new->priomap_len = new_len;
  71. /* install the new priomap */
  72. rcu_assign_pointer(dev->priomap, new);
  73. if (old)
  74. kfree_rcu(old, rcu);
  75. return 0;
  76. }
  77. /**
  78. * netprio_prio - return the effective netprio of a cgroup-net_device pair
  79. * @cgrp: cgroup part of the target pair
  80. * @dev: net_device part of the target pair
  81. *
  82. * Should be called under RCU read or rtnl lock.
  83. */
  84. static u32 netprio_prio(struct cgroup *cgrp, struct net_device *dev)
  85. {
  86. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  87. if (map && cgrp->id < map->priomap_len)
  88. return map->priomap[cgrp->id];
  89. return 0;
  90. }
  91. /**
  92. * netprio_set_prio - set netprio on a cgroup-net_device pair
  93. * @cgrp: cgroup part of the target pair
  94. * @dev: net_device part of the target pair
  95. * @prio: prio to set
  96. *
  97. * Set netprio to @prio on @cgrp-@dev pair. Should be called under rtnl
  98. * lock and may fail under memory pressure for non-zero @prio.
  99. */
  100. static int netprio_set_prio(struct cgroup *cgrp, struct net_device *dev,
  101. u32 prio)
  102. {
  103. struct netprio_map *map;
  104. int ret;
  105. /* avoid extending priomap for zero writes */
  106. map = rtnl_dereference(dev->priomap);
  107. if (!prio && (!map || map->priomap_len <= cgrp->id))
  108. return 0;
  109. ret = extend_netdev_table(dev, cgrp->id);
  110. if (ret)
  111. return ret;
  112. map = rtnl_dereference(dev->priomap);
  113. map->priomap[cgrp->id] = prio;
  114. return 0;
  115. }
  116. static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
  117. {
  118. struct cgroup_netprio_state *cs;
  119. if (cgrp->parent && cgrp->parent->id)
  120. return ERR_PTR(-EINVAL);
  121. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  122. if (!cs)
  123. return ERR_PTR(-ENOMEM);
  124. return &cs->css;
  125. }
  126. static void cgrp_css_free(struct cgroup *cgrp)
  127. {
  128. struct cgroup_netprio_state *cs = cgrp_netprio_state(cgrp);
  129. struct net_device *dev;
  130. rtnl_lock();
  131. for_each_netdev(&init_net, dev)
  132. WARN_ON_ONCE(netprio_set_prio(cgrp, dev, 0));
  133. rtnl_unlock();
  134. kfree(cs);
  135. }
  136. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  137. {
  138. return cgrp->id;
  139. }
  140. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  141. struct cgroup_map_cb *cb)
  142. {
  143. struct net_device *dev;
  144. rcu_read_lock();
  145. for_each_netdev_rcu(&init_net, dev)
  146. cb->fill(cb, dev->name, netprio_prio(cont, dev));
  147. rcu_read_unlock();
  148. return 0;
  149. }
  150. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  151. const char *buffer)
  152. {
  153. char devname[IFNAMSIZ + 1];
  154. struct net_device *dev;
  155. u32 prio;
  156. int ret;
  157. if (sscanf(buffer, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  158. return -EINVAL;
  159. dev = dev_get_by_name(&init_net, devname);
  160. if (!dev)
  161. return -ENODEV;
  162. rtnl_lock();
  163. ret = netprio_set_prio(cgrp, dev, prio);
  164. rtnl_unlock();
  165. dev_put(dev);
  166. return ret;
  167. }
  168. static int update_netprio(const void *v, struct file *file, unsigned n)
  169. {
  170. int err;
  171. struct socket *sock = sock_from_file(file, &err);
  172. if (sock)
  173. sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v;
  174. return 0;
  175. }
  176. void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  177. {
  178. struct task_struct *p;
  179. void *v;
  180. cgroup_taskset_for_each(p, cgrp, tset) {
  181. task_lock(p);
  182. v = (void *)(unsigned long)task_netprioidx(p);
  183. iterate_fd(p->files, 0, update_netprio, v);
  184. task_unlock(p);
  185. }
  186. }
  187. static struct cftype ss_files[] = {
  188. {
  189. .name = "prioidx",
  190. .read_u64 = read_prioidx,
  191. },
  192. {
  193. .name = "ifpriomap",
  194. .read_map = read_priomap,
  195. .write_string = write_priomap,
  196. },
  197. { } /* terminate */
  198. };
  199. struct cgroup_subsys net_prio_subsys = {
  200. .name = "net_prio",
  201. .css_alloc = cgrp_css_alloc,
  202. .css_free = cgrp_css_free,
  203. .attach = net_prio_attach,
  204. .subsys_id = net_prio_subsys_id,
  205. .base_cftypes = ss_files,
  206. .module = THIS_MODULE,
  207. /*
  208. * net_prio has artificial limit on the number of cgroups and
  209. * disallows nesting making it impossible to co-mount it with other
  210. * hierarchical subsystems. Remove the artificially low PRIOIDX_SZ
  211. * limit and properly nest configuration such that children follow
  212. * their parents' configurations by default and are allowed to
  213. * override and remove the following.
  214. */
  215. .broken_hierarchy = true,
  216. };
  217. static int netprio_device_event(struct notifier_block *unused,
  218. unsigned long event, void *ptr)
  219. {
  220. struct net_device *dev = ptr;
  221. struct netprio_map *old;
  222. /*
  223. * Note this is called with rtnl_lock held so we have update side
  224. * protection on our rcu assignments
  225. */
  226. switch (event) {
  227. case NETDEV_UNREGISTER:
  228. old = rtnl_dereference(dev->priomap);
  229. RCU_INIT_POINTER(dev->priomap, NULL);
  230. if (old)
  231. kfree_rcu(old, rcu);
  232. break;
  233. }
  234. return NOTIFY_DONE;
  235. }
  236. static struct notifier_block netprio_device_notifier = {
  237. .notifier_call = netprio_device_event
  238. };
  239. static int __init init_cgroup_netprio(void)
  240. {
  241. int ret;
  242. ret = cgroup_load_subsys(&net_prio_subsys);
  243. if (ret)
  244. goto out;
  245. register_netdevice_notifier(&netprio_device_notifier);
  246. out:
  247. return ret;
  248. }
  249. static void __exit exit_cgroup_netprio(void)
  250. {
  251. struct netprio_map *old;
  252. struct net_device *dev;
  253. unregister_netdevice_notifier(&netprio_device_notifier);
  254. cgroup_unload_subsys(&net_prio_subsys);
  255. rtnl_lock();
  256. for_each_netdev(&init_net, dev) {
  257. old = rtnl_dereference(dev->priomap);
  258. RCU_INIT_POINTER(dev->priomap, NULL);
  259. if (old)
  260. kfree_rcu(old, rcu);
  261. }
  262. rtnl_unlock();
  263. }
  264. module_init(init_cgroup_netprio);
  265. module_exit(exit_cgroup_netprio);
  266. MODULE_LICENSE("GPL v2");