netprio_cgroup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <linux/sched/task.h>
  22. #include <net/rtnetlink.h>
  23. #include <net/pkt_cls.h>
  24. #include <net/sock.h>
  25. #include <net/netprio_cgroup.h>
  26. #include <linux/fdtable.h>
  27. /*
  28. * netprio allocates per-net_device priomap array which is indexed by
  29. * css->id. Limiting css ID to 16bits doesn't lose anything.
  30. */
  31. #define NETPRIO_ID_MAX USHRT_MAX
  32. #define PRIOMAP_MIN_SZ 128
  33. /*
  34. * Extend @dev->priomap so that it's large enough to accommodate
  35. * @target_idx. @dev->priomap.priomap_len > @target_idx after successful
  36. * return. Must be called under rtnl lock.
  37. */
  38. static int extend_netdev_table(struct net_device *dev, u32 target_idx)
  39. {
  40. struct netprio_map *old, *new;
  41. size_t new_sz, new_len;
  42. /* is the existing priomap large enough? */
  43. old = rtnl_dereference(dev->priomap);
  44. if (old && old->priomap_len > target_idx)
  45. return 0;
  46. /*
  47. * Determine the new size. Let's keep it power-of-two. We start
  48. * from PRIOMAP_MIN_SZ and double it until it's large enough to
  49. * accommodate @target_idx.
  50. */
  51. new_sz = PRIOMAP_MIN_SZ;
  52. while (true) {
  53. new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
  54. sizeof(new->priomap[0]);
  55. if (new_len > target_idx)
  56. break;
  57. new_sz *= 2;
  58. /* overflowed? */
  59. if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
  60. return -ENOSPC;
  61. }
  62. /* allocate & copy */
  63. new = kzalloc(new_sz, GFP_KERNEL);
  64. if (!new)
  65. return -ENOMEM;
  66. if (old)
  67. memcpy(new->priomap, old->priomap,
  68. old->priomap_len * sizeof(old->priomap[0]));
  69. new->priomap_len = new_len;
  70. /* install the new priomap */
  71. rcu_assign_pointer(dev->priomap, new);
  72. if (old)
  73. kfree_rcu(old, rcu);
  74. return 0;
  75. }
  76. /**
  77. * netprio_prio - return the effective netprio of a cgroup-net_device pair
  78. * @css: css part of the target pair
  79. * @dev: net_device part of the target pair
  80. *
  81. * Should be called under RCU read or rtnl lock.
  82. */
  83. static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
  84. {
  85. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  86. int id = css->cgroup->id;
  87. if (map && id < map->priomap_len)
  88. return map->priomap[id];
  89. return 0;
  90. }
  91. /**
  92. * netprio_set_prio - set netprio on a cgroup-net_device pair
  93. * @css: css part of the target pair
  94. * @dev: net_device part of the target pair
  95. * @prio: prio to set
  96. *
  97. * Set netprio to @prio on @css-@dev pair. Should be called under rtnl
  98. * lock and may fail under memory pressure for non-zero @prio.
  99. */
  100. static int netprio_set_prio(struct cgroup_subsys_state *css,
  101. struct net_device *dev, u32 prio)
  102. {
  103. struct netprio_map *map;
  104. int id = css->cgroup->id;
  105. int ret;
  106. /* avoid extending priomap for zero writes */
  107. map = rtnl_dereference(dev->priomap);
  108. if (!prio && (!map || map->priomap_len <= id))
  109. return 0;
  110. ret = extend_netdev_table(dev, id);
  111. if (ret)
  112. return ret;
  113. map = rtnl_dereference(dev->priomap);
  114. map->priomap[id] = prio;
  115. return 0;
  116. }
  117. static struct cgroup_subsys_state *
  118. cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  119. {
  120. struct cgroup_subsys_state *css;
  121. css = kzalloc(sizeof(*css), GFP_KERNEL);
  122. if (!css)
  123. return ERR_PTR(-ENOMEM);
  124. return css;
  125. }
  126. static int cgrp_css_online(struct cgroup_subsys_state *css)
  127. {
  128. struct cgroup_subsys_state *parent_css = css->parent;
  129. struct net_device *dev;
  130. int ret = 0;
  131. if (css->id > NETPRIO_ID_MAX)
  132. return -ENOSPC;
  133. if (!parent_css)
  134. return 0;
  135. rtnl_lock();
  136. /*
  137. * Inherit prios from the parent. As all prios are set during
  138. * onlining, there is no need to clear them on offline.
  139. */
  140. for_each_netdev(&init_net, dev) {
  141. u32 prio = netprio_prio(parent_css, dev);
  142. ret = netprio_set_prio(css, dev, prio);
  143. if (ret)
  144. break;
  145. }
  146. rtnl_unlock();
  147. return ret;
  148. }
  149. static void cgrp_css_free(struct cgroup_subsys_state *css)
  150. {
  151. kfree(css);
  152. }
  153. static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
  154. {
  155. return css->cgroup->id;
  156. }
  157. static int read_priomap(struct seq_file *sf, void *v)
  158. {
  159. struct net_device *dev;
  160. rcu_read_lock();
  161. for_each_netdev_rcu(&init_net, dev)
  162. seq_printf(sf, "%s %u\n", dev->name,
  163. netprio_prio(seq_css(sf), dev));
  164. rcu_read_unlock();
  165. return 0;
  166. }
  167. static ssize_t write_priomap(struct kernfs_open_file *of,
  168. char *buf, size_t nbytes, loff_t off)
  169. {
  170. char devname[IFNAMSIZ + 1];
  171. struct net_device *dev;
  172. u32 prio;
  173. int ret;
  174. if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  175. return -EINVAL;
  176. dev = dev_get_by_name(&init_net, devname);
  177. if (!dev)
  178. return -ENODEV;
  179. cgroup_sk_alloc_disable();
  180. rtnl_lock();
  181. ret = netprio_set_prio(of_css(of), dev, prio);
  182. rtnl_unlock();
  183. dev_put(dev);
  184. return ret ?: nbytes;
  185. }
  186. static int update_netprio(const void *v, struct file *file, unsigned n)
  187. {
  188. int err;
  189. struct socket *sock = sock_from_file(file, &err);
  190. if (sock) {
  191. spin_lock(&cgroup_sk_update_lock);
  192. sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
  193. (unsigned long)v);
  194. spin_unlock(&cgroup_sk_update_lock);
  195. }
  196. return 0;
  197. }
  198. static void net_prio_attach(struct cgroup_taskset *tset)
  199. {
  200. struct task_struct *p;
  201. struct cgroup_subsys_state *css;
  202. cgroup_sk_alloc_disable();
  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");