netprio_cgroup.c 6.5 KB

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