netprio_cgroup.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. return -ENOMEM;
  65. if (old)
  66. memcpy(new->priomap, old->priomap,
  67. old->priomap_len * sizeof(old->priomap[0]));
  68. new->priomap_len = new_len;
  69. /* install the new priomap */
  70. rcu_assign_pointer(dev->priomap, new);
  71. if (old)
  72. kfree_rcu(old, rcu);
  73. return 0;
  74. }
  75. /**
  76. * netprio_prio - return the effective netprio of a cgroup-net_device pair
  77. * @cgrp: cgroup part of the target pair
  78. * @dev: net_device part of the target pair
  79. *
  80. * Should be called under RCU read or rtnl lock.
  81. */
  82. static u32 netprio_prio(struct cgroup *cgrp, struct net_device *dev)
  83. {
  84. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  85. if (map && cgrp->id < map->priomap_len)
  86. return map->priomap[cgrp->id];
  87. return 0;
  88. }
  89. /**
  90. * netprio_set_prio - set netprio on a cgroup-net_device pair
  91. * @cgrp: cgroup part of the target pair
  92. * @dev: net_device part of the target pair
  93. * @prio: prio to set
  94. *
  95. * Set netprio to @prio on @cgrp-@dev pair. Should be called under rtnl
  96. * lock and may fail under memory pressure for non-zero @prio.
  97. */
  98. static int netprio_set_prio(struct cgroup *cgrp, struct net_device *dev,
  99. u32 prio)
  100. {
  101. struct netprio_map *map;
  102. int ret;
  103. /* avoid extending priomap for zero writes */
  104. map = rtnl_dereference(dev->priomap);
  105. if (!prio && (!map || map->priomap_len <= cgrp->id))
  106. return 0;
  107. ret = extend_netdev_table(dev, cgrp->id);
  108. if (ret)
  109. return ret;
  110. map = rtnl_dereference(dev->priomap);
  111. map->priomap[cgrp->id] = prio;
  112. return 0;
  113. }
  114. static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
  115. {
  116. struct cgroup_netprio_state *cs;
  117. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  118. if (!cs)
  119. return ERR_PTR(-ENOMEM);
  120. return &cs->css;
  121. }
  122. static int cgrp_css_online(struct cgroup *cgrp)
  123. {
  124. struct cgroup *parent = cgrp->parent;
  125. struct net_device *dev;
  126. int ret = 0;
  127. if (!parent)
  128. return 0;
  129. rtnl_lock();
  130. /*
  131. * Inherit prios from the parent. As all prios are set during
  132. * onlining, there is no need to clear them on offline.
  133. */
  134. for_each_netdev(&init_net, dev) {
  135. u32 prio = netprio_prio(parent, dev);
  136. ret = netprio_set_prio(cgrp, dev, prio);
  137. if (ret)
  138. break;
  139. }
  140. rtnl_unlock();
  141. return ret;
  142. }
  143. static void cgrp_css_free(struct cgroup *cgrp)
  144. {
  145. kfree(cgrp_netprio_state(cgrp));
  146. }
  147. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  148. {
  149. return cgrp->id;
  150. }
  151. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  152. struct cgroup_map_cb *cb)
  153. {
  154. struct net_device *dev;
  155. rcu_read_lock();
  156. for_each_netdev_rcu(&init_net, dev)
  157. cb->fill(cb, dev->name, netprio_prio(cont, dev));
  158. rcu_read_unlock();
  159. return 0;
  160. }
  161. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  162. const char *buffer)
  163. {
  164. char devname[IFNAMSIZ + 1];
  165. struct net_device *dev;
  166. u32 prio;
  167. int ret;
  168. if (sscanf(buffer, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  169. return -EINVAL;
  170. dev = dev_get_by_name(&init_net, devname);
  171. if (!dev)
  172. return -ENODEV;
  173. rtnl_lock();
  174. ret = netprio_set_prio(cgrp, dev, prio);
  175. rtnl_unlock();
  176. dev_put(dev);
  177. return ret;
  178. }
  179. static int update_netprio(const void *v, struct file *file, unsigned n)
  180. {
  181. int err;
  182. struct socket *sock = sock_from_file(file, &err);
  183. if (sock)
  184. sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v;
  185. return 0;
  186. }
  187. static void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  188. {
  189. struct task_struct *p;
  190. void *v;
  191. cgroup_taskset_for_each(p, cgrp, tset) {
  192. task_lock(p);
  193. v = (void *)(unsigned long)task_netprioidx(p);
  194. iterate_fd(p->files, 0, update_netprio, v);
  195. task_unlock(p);
  196. }
  197. }
  198. static struct cftype ss_files[] = {
  199. {
  200. .name = "prioidx",
  201. .read_u64 = read_prioidx,
  202. },
  203. {
  204. .name = "ifpriomap",
  205. .read_map = read_priomap,
  206. .write_string = write_priomap,
  207. },
  208. { } /* terminate */
  209. };
  210. struct cgroup_subsys net_prio_subsys = {
  211. .name = "net_prio",
  212. .css_alloc = cgrp_css_alloc,
  213. .css_online = cgrp_css_online,
  214. .css_free = cgrp_css_free,
  215. .attach = net_prio_attach,
  216. .subsys_id = net_prio_subsys_id,
  217. .base_cftypes = ss_files,
  218. .module = THIS_MODULE,
  219. };
  220. static int netprio_device_event(struct notifier_block *unused,
  221. unsigned long event, void *ptr)
  222. {
  223. struct net_device *dev = ptr;
  224. struct netprio_map *old;
  225. /*
  226. * Note this is called with rtnl_lock held so we have update side
  227. * protection on our rcu assignments
  228. */
  229. switch (event) {
  230. case NETDEV_UNREGISTER:
  231. old = rtnl_dereference(dev->priomap);
  232. RCU_INIT_POINTER(dev->priomap, NULL);
  233. if (old)
  234. kfree_rcu(old, rcu);
  235. break;
  236. }
  237. return NOTIFY_DONE;
  238. }
  239. static struct notifier_block netprio_device_notifier = {
  240. .notifier_call = netprio_device_event
  241. };
  242. static int __init init_cgroup_netprio(void)
  243. {
  244. int ret;
  245. ret = cgroup_load_subsys(&net_prio_subsys);
  246. if (ret)
  247. goto out;
  248. register_netdevice_notifier(&netprio_device_notifier);
  249. out:
  250. return ret;
  251. }
  252. static void __exit exit_cgroup_netprio(void)
  253. {
  254. struct netprio_map *old;
  255. struct net_device *dev;
  256. unregister_netdevice_notifier(&netprio_device_notifier);
  257. cgroup_unload_subsys(&net_prio_subsys);
  258. rtnl_lock();
  259. for_each_netdev(&init_net, dev) {
  260. old = rtnl_dereference(dev->priomap);
  261. RCU_INIT_POINTER(dev->priomap, NULL);
  262. if (old)
  263. kfree_rcu(old, rcu);
  264. }
  265. rtnl_unlock();
  266. }
  267. module_init(init_cgroup_netprio);
  268. module_exit(exit_cgroup_netprio);
  269. MODULE_LICENSE("GPL v2");