netprio_cgroup.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. /*
  27. * netprio allocates per-net_device priomap array which is indexed by
  28. * css->id. Limiting css ID to 16bits doesn't lose anything.
  29. */
  30. #define NETPRIO_ID_MAX USHRT_MAX
  31. #define PRIOMAP_MIN_SZ 128
  32. /*
  33. * Extend @dev->priomap so that it's large enough to accommodate
  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. * @css: css 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_subsys_state *css, struct net_device *dev)
  83. {
  84. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  85. int id = css->cgroup->id;
  86. if (map && id < map->priomap_len)
  87. return map->priomap[id];
  88. return 0;
  89. }
  90. /**
  91. * netprio_set_prio - set netprio on a cgroup-net_device pair
  92. * @css: css part of the target pair
  93. * @dev: net_device part of the target pair
  94. * @prio: prio to set
  95. *
  96. * Set netprio to @prio on @css-@dev pair. Should be called under rtnl
  97. * lock and may fail under memory pressure for non-zero @prio.
  98. */
  99. static int netprio_set_prio(struct cgroup_subsys_state *css,
  100. struct net_device *dev, u32 prio)
  101. {
  102. struct netprio_map *map;
  103. int id = css->cgroup->id;
  104. int ret;
  105. /* avoid extending priomap for zero writes */
  106. map = rtnl_dereference(dev->priomap);
  107. if (!prio && (!map || map->priomap_len <= id))
  108. return 0;
  109. ret = extend_netdev_table(dev, id);
  110. if (ret)
  111. return ret;
  112. map = rtnl_dereference(dev->priomap);
  113. map->priomap[id] = prio;
  114. return 0;
  115. }
  116. static struct cgroup_subsys_state *
  117. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  118. {
  119. struct cgroup_subsys_state *css;
  120. css = kzalloc(sizeof(*css), GFP_KERNEL);
  121. if (!css)
  122. return ERR_PTR(-ENOMEM);
  123. return css;
  124. }
  125. static int cgrp_css_online(struct cgroup_subsys_state *css)
  126. {
  127. struct cgroup_subsys_state *parent_css = css->parent;
  128. struct net_device *dev;
  129. int ret = 0;
  130. if (css->id > NETPRIO_ID_MAX)
  131. return -ENOSPC;
  132. if (!parent_css)
  133. return 0;
  134. rtnl_lock();
  135. /*
  136. * Inherit prios from the parent. As all prios are set during
  137. * onlining, there is no need to clear them on offline.
  138. */
  139. for_each_netdev(&init_net, dev) {
  140. u32 prio = netprio_prio(parent_css, dev);
  141. ret = netprio_set_prio(css, dev, prio);
  142. if (ret)
  143. break;
  144. }
  145. rtnl_unlock();
  146. return ret;
  147. }
  148. static void cgrp_css_free(struct cgroup_subsys_state *css)
  149. {
  150. kfree(css);
  151. }
  152. static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
  153. {
  154. return css->cgroup->id;
  155. }
  156. static int read_priomap(struct seq_file *sf, void *v)
  157. {
  158. struct net_device *dev;
  159. rcu_read_lock();
  160. for_each_netdev_rcu(&init_net, dev)
  161. seq_printf(sf, "%s %u\n", dev->name,
  162. netprio_prio(seq_css(sf), dev));
  163. rcu_read_unlock();
  164. return 0;
  165. }
  166. static ssize_t write_priomap(struct kernfs_open_file *of,
  167. char *buf, size_t nbytes, loff_t off)
  168. {
  169. char devname[IFNAMSIZ + 1];
  170. struct net_device *dev;
  171. u32 prio;
  172. int ret;
  173. if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  174. return -EINVAL;
  175. dev = dev_get_by_name(&init_net, devname);
  176. if (!dev)
  177. return -ENODEV;
  178. cgroup_sk_alloc_disable();
  179. rtnl_lock();
  180. ret = netprio_set_prio(of_css(of), dev, prio);
  181. rtnl_unlock();
  182. dev_put(dev);
  183. return ret ?: nbytes;
  184. }
  185. static int update_netprio(const void *v, struct file *file, unsigned n)
  186. {
  187. int err;
  188. struct socket *sock = sock_from_file(file, &err);
  189. if (sock) {
  190. spin_lock(&cgroup_sk_update_lock);
  191. sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
  192. (unsigned long)v);
  193. spin_unlock(&cgroup_sk_update_lock);
  194. }
  195. return 0;
  196. }
  197. static void net_prio_attach(struct cgroup_taskset *tset)
  198. {
  199. struct task_struct *p;
  200. struct cgroup_subsys_state *css;
  201. cgroup_taskset_for_each(p, css, tset) {
  202. void *v = (void *)(unsigned long)css->cgroup->id;
  203. task_lock(p);
  204. iterate_fd(p->files, 0, update_netprio, v);
  205. task_unlock(p);
  206. }
  207. }
  208. static struct cftype ss_files[] = {
  209. {
  210. .name = "prioidx",
  211. .read_u64 = read_prioidx,
  212. },
  213. {
  214. .name = "ifpriomap",
  215. .seq_show = read_priomap,
  216. .write = write_priomap,
  217. },
  218. { } /* terminate */
  219. };
  220. struct cgroup_subsys net_prio_cgrp_subsys = {
  221. .css_alloc = cgrp_css_alloc,
  222. .css_online = cgrp_css_online,
  223. .css_free = cgrp_css_free,
  224. .attach = net_prio_attach,
  225. .legacy_cftypes = ss_files,
  226. };
  227. static int netprio_device_event(struct notifier_block *unused,
  228. unsigned long event, void *ptr)
  229. {
  230. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  231. struct netprio_map *old;
  232. /*
  233. * Note this is called with rtnl_lock held so we have update side
  234. * protection on our rcu assignments
  235. */
  236. switch (event) {
  237. case NETDEV_UNREGISTER:
  238. old = rtnl_dereference(dev->priomap);
  239. RCU_INIT_POINTER(dev->priomap, NULL);
  240. if (old)
  241. kfree_rcu(old, rcu);
  242. break;
  243. }
  244. return NOTIFY_DONE;
  245. }
  246. static struct notifier_block netprio_device_notifier = {
  247. .notifier_call = netprio_device_event
  248. };
  249. static int __init init_cgroup_netprio(void)
  250. {
  251. register_netdevice_notifier(&netprio_device_notifier);
  252. return 0;
  253. }
  254. subsys_initcall(init_cgroup_netprio);
  255. MODULE_LICENSE("GPL v2");