netprio_cgroup.c 6.9 KB

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