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/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/cgroup.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/atomic.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/sock.h>
  23. #include <net/netprio_cgroup.h>
  24. #include <linux/fdtable.h>
  25. /*
  26. * netprio allocates per-net_device priomap array which is indexed by
  27. * css->id. Limiting css ID to 16bits doesn't lose anything.
  28. */
  29. #define NETPRIO_ID_MAX USHRT_MAX
  30. #define PRIOMAP_MIN_SZ 128
  31. /*
  32. * Extend @dev->priomap so that it's large enough to accommodate
  33. * @target_idx. @dev->priomap.priomap_len > @target_idx after successful
  34. * return. Must be called under rtnl lock.
  35. */
  36. static int extend_netdev_table(struct net_device *dev, u32 target_idx)
  37. {
  38. struct netprio_map *old, *new;
  39. size_t new_sz, new_len;
  40. /* is the existing priomap large enough? */
  41. old = rtnl_dereference(dev->priomap);
  42. if (old && old->priomap_len > target_idx)
  43. return 0;
  44. /*
  45. * Determine the new size. Let's keep it power-of-two. We start
  46. * from PRIOMAP_MIN_SZ and double it until it's large enough to
  47. * accommodate @target_idx.
  48. */
  49. new_sz = PRIOMAP_MIN_SZ;
  50. while (true) {
  51. new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
  52. sizeof(new->priomap[0]);
  53. if (new_len > target_idx)
  54. break;
  55. new_sz *= 2;
  56. /* overflowed? */
  57. if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
  58. return -ENOSPC;
  59. }
  60. /* allocate & copy */
  61. new = kzalloc(new_sz, GFP_KERNEL);
  62. if (!new)
  63. return -ENOMEM;
  64. if (old)
  65. memcpy(new->priomap, old->priomap,
  66. old->priomap_len * sizeof(old->priomap[0]));
  67. new->priomap_len = new_len;
  68. /* install the new priomap */
  69. rcu_assign_pointer(dev->priomap, new);
  70. if (old)
  71. kfree_rcu(old, rcu);
  72. return 0;
  73. }
  74. /**
  75. * netprio_prio - return the effective netprio of a cgroup-net_device pair
  76. * @css: css part of the target pair
  77. * @dev: net_device part of the target pair
  78. *
  79. * Should be called under RCU read or rtnl lock.
  80. */
  81. static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
  82. {
  83. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  84. int id = css->cgroup->id;
  85. if (map && id < map->priomap_len)
  86. return map->priomap[id];
  87. return 0;
  88. }
  89. /**
  90. * netprio_set_prio - set netprio on a cgroup-net_device pair
  91. * @css: css 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 @css-@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_subsys_state *css,
  99. struct net_device *dev, u32 prio)
  100. {
  101. struct netprio_map *map;
  102. int id = css->cgroup->id;
  103. int ret;
  104. /* avoid extending priomap for zero writes */
  105. map = rtnl_dereference(dev->priomap);
  106. if (!prio && (!map || map->priomap_len <= id))
  107. return 0;
  108. ret = extend_netdev_table(dev, id);
  109. if (ret)
  110. return ret;
  111. map = rtnl_dereference(dev->priomap);
  112. map->priomap[id] = prio;
  113. return 0;
  114. }
  115. static struct cgroup_subsys_state *
  116. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  117. {
  118. struct cgroup_subsys_state *css;
  119. css = kzalloc(sizeof(*css), GFP_KERNEL);
  120. if (!css)
  121. return ERR_PTR(-ENOMEM);
  122. return css;
  123. }
  124. static int cgrp_css_online(struct cgroup_subsys_state *css)
  125. {
  126. struct cgroup_subsys_state *parent_css = css->parent;
  127. struct net_device *dev;
  128. int ret = 0;
  129. if (css->id > NETPRIO_ID_MAX)
  130. return -ENOSPC;
  131. if (!parent_css)
  132. return 0;
  133. rtnl_lock();
  134. /*
  135. * Inherit prios from the parent. As all prios are set during
  136. * onlining, there is no need to clear them on offline.
  137. */
  138. for_each_netdev(&init_net, dev) {
  139. u32 prio = netprio_prio(parent_css, dev);
  140. ret = netprio_set_prio(css, dev, prio);
  141. if (ret)
  142. break;
  143. }
  144. rtnl_unlock();
  145. return ret;
  146. }
  147. static void cgrp_css_free(struct cgroup_subsys_state *css)
  148. {
  149. kfree(css);
  150. }
  151. static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
  152. {
  153. return css->cgroup->id;
  154. }
  155. static int read_priomap(struct seq_file *sf, void *v)
  156. {
  157. struct net_device *dev;
  158. rcu_read_lock();
  159. for_each_netdev_rcu(&init_net, dev)
  160. seq_printf(sf, "%s %u\n", dev->name,
  161. netprio_prio(seq_css(sf), dev));
  162. rcu_read_unlock();
  163. return 0;
  164. }
  165. static ssize_t write_priomap(struct kernfs_open_file *of,
  166. char *buf, size_t nbytes, loff_t off)
  167. {
  168. char devname[IFNAMSIZ + 1];
  169. struct net_device *dev;
  170. u32 prio;
  171. int ret;
  172. if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  173. return -EINVAL;
  174. dev = dev_get_by_name(&init_net, devname);
  175. if (!dev)
  176. return -ENODEV;
  177. cgroup_sk_alloc_disable();
  178. rtnl_lock();
  179. ret = netprio_set_prio(of_css(of), dev, prio);
  180. rtnl_unlock();
  181. dev_put(dev);
  182. return ret ?: nbytes;
  183. }
  184. static int update_netprio(const void *v, struct file *file, unsigned n)
  185. {
  186. int err;
  187. struct socket *sock = sock_from_file(file, &err);
  188. if (sock) {
  189. spin_lock(&cgroup_sk_update_lock);
  190. sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
  191. (unsigned long)v);
  192. spin_unlock(&cgroup_sk_update_lock);
  193. }
  194. return 0;
  195. }
  196. static void net_prio_attach(struct cgroup_taskset *tset)
  197. {
  198. struct task_struct *p;
  199. struct cgroup_subsys_state *css;
  200. cgroup_taskset_for_each(p, css, tset) {
  201. void *v = (void *)(unsigned long)css->cgroup->id;
  202. task_lock(p);
  203. iterate_fd(p->files, 0, update_netprio, v);
  204. task_unlock(p);
  205. }
  206. }
  207. static struct cftype ss_files[] = {
  208. {
  209. .name = "prioidx",
  210. .read_u64 = read_prioidx,
  211. },
  212. {
  213. .name = "ifpriomap",
  214. .seq_show = read_priomap,
  215. .write = write_priomap,
  216. },
  217. { } /* terminate */
  218. };
  219. struct cgroup_subsys net_prio_cgrp_subsys = {
  220. .css_alloc = cgrp_css_alloc,
  221. .css_online = cgrp_css_online,
  222. .css_free = cgrp_css_free,
  223. .attach = net_prio_attach,
  224. .legacy_cftypes = ss_files,
  225. };
  226. static int netprio_device_event(struct notifier_block *unused,
  227. unsigned long event, void *ptr)
  228. {
  229. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  230. struct netprio_map *old;
  231. /*
  232. * Note this is called with rtnl_lock held so we have update side
  233. * protection on our rcu assignments
  234. */
  235. switch (event) {
  236. case NETDEV_UNREGISTER:
  237. old = rtnl_dereference(dev->priomap);
  238. RCU_INIT_POINTER(dev->priomap, NULL);
  239. if (old)
  240. kfree_rcu(old, rcu);
  241. break;
  242. }
  243. return NOTIFY_DONE;
  244. }
  245. static struct notifier_block netprio_device_notifier = {
  246. .notifier_call = netprio_device_event
  247. };
  248. static int __init init_cgroup_netprio(void)
  249. {
  250. register_netdevice_notifier(&netprio_device_notifier);
  251. return 0;
  252. }
  253. subsys_initcall(init_cgroup_netprio);
  254. MODULE_LICENSE("GPL v2");