netprio_cgroup.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
  28. {
  29. return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
  30. struct cgroup_netprio_state, css);
  31. }
  32. /*
  33. * Extend @dev->priomap so that it's large enough to accomodate
  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. pr_warn("Unable to alloc new priomap!\n");
  65. return -ENOMEM;
  66. }
  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. * @cgrp: cgroup 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 *cgrp, struct net_device *dev)
  85. {
  86. struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
  87. if (map && cgrp->id < map->priomap_len)
  88. return map->priomap[cgrp->id];
  89. return 0;
  90. }
  91. /**
  92. * netprio_set_prio - set netprio on a cgroup-net_device pair
  93. * @cgrp: cgroup 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 @cgrp-@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 *cgrp, struct net_device *dev,
  101. u32 prio)
  102. {
  103. struct netprio_map *map;
  104. int ret;
  105. /* avoid extending priomap for zero writes */
  106. map = rtnl_dereference(dev->priomap);
  107. if (!prio && (!map || map->priomap_len <= cgrp->id))
  108. return 0;
  109. ret = extend_netdev_table(dev, cgrp->id);
  110. if (ret)
  111. return ret;
  112. map = rtnl_dereference(dev->priomap);
  113. map->priomap[cgrp->id] = prio;
  114. return 0;
  115. }
  116. static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp)
  117. {
  118. struct cgroup_netprio_state *cs;
  119. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  120. if (!cs)
  121. return ERR_PTR(-ENOMEM);
  122. return &cs->css;
  123. }
  124. static int cgrp_css_online(struct cgroup *cgrp)
  125. {
  126. struct cgroup *parent = cgrp->parent;
  127. struct net_device *dev;
  128. int ret = 0;
  129. if (!parent)
  130. return 0;
  131. rtnl_lock();
  132. /*
  133. * Inherit prios from the parent. As all prios are set during
  134. * onlining, there is no need to clear them on offline.
  135. */
  136. for_each_netdev(&init_net, dev) {
  137. u32 prio = netprio_prio(parent, dev);
  138. ret = netprio_set_prio(cgrp, dev, prio);
  139. if (ret)
  140. break;
  141. }
  142. rtnl_unlock();
  143. return ret;
  144. }
  145. static void cgrp_css_free(struct cgroup *cgrp)
  146. {
  147. kfree(cgrp_netprio_state(cgrp));
  148. }
  149. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  150. {
  151. return cgrp->id;
  152. }
  153. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  154. struct cgroup_map_cb *cb)
  155. {
  156. struct net_device *dev;
  157. rcu_read_lock();
  158. for_each_netdev_rcu(&init_net, dev)
  159. cb->fill(cb, dev->name, netprio_prio(cont, dev));
  160. rcu_read_unlock();
  161. return 0;
  162. }
  163. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  164. const char *buffer)
  165. {
  166. char devname[IFNAMSIZ + 1];
  167. struct net_device *dev;
  168. u32 prio;
  169. int ret;
  170. if (sscanf(buffer, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
  171. return -EINVAL;
  172. dev = dev_get_by_name(&init_net, devname);
  173. if (!dev)
  174. return -ENODEV;
  175. rtnl_lock();
  176. ret = netprio_set_prio(cgrp, dev, prio);
  177. rtnl_unlock();
  178. dev_put(dev);
  179. return ret;
  180. }
  181. static int update_netprio(const void *v, struct file *file, unsigned n)
  182. {
  183. int err;
  184. struct socket *sock = sock_from_file(file, &err);
  185. if (sock)
  186. sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v;
  187. return 0;
  188. }
  189. static void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  190. {
  191. struct task_struct *p;
  192. void *v;
  193. cgroup_taskset_for_each(p, cgrp, tset) {
  194. task_lock(p);
  195. v = (void *)(unsigned long)task_netprioidx(p);
  196. iterate_fd(p->files, 0, update_netprio, v);
  197. task_unlock(p);
  198. }
  199. }
  200. static struct cftype ss_files[] = {
  201. {
  202. .name = "prioidx",
  203. .read_u64 = read_prioidx,
  204. },
  205. {
  206. .name = "ifpriomap",
  207. .read_map = read_priomap,
  208. .write_string = write_priomap,
  209. },
  210. { } /* terminate */
  211. };
  212. struct cgroup_subsys net_prio_subsys = {
  213. .name = "net_prio",
  214. .css_alloc = cgrp_css_alloc,
  215. .css_online = cgrp_css_online,
  216. .css_free = cgrp_css_free,
  217. .attach = net_prio_attach,
  218. .subsys_id = net_prio_subsys_id,
  219. .base_cftypes = ss_files,
  220. .module = THIS_MODULE,
  221. };
  222. static int netprio_device_event(struct notifier_block *unused,
  223. unsigned long event, void *ptr)
  224. {
  225. struct net_device *dev = ptr;
  226. struct netprio_map *old;
  227. /*
  228. * Note this is called with rtnl_lock held so we have update side
  229. * protection on our rcu assignments
  230. */
  231. switch (event) {
  232. case NETDEV_UNREGISTER:
  233. old = rtnl_dereference(dev->priomap);
  234. RCU_INIT_POINTER(dev->priomap, NULL);
  235. if (old)
  236. kfree_rcu(old, rcu);
  237. break;
  238. }
  239. return NOTIFY_DONE;
  240. }
  241. static struct notifier_block netprio_device_notifier = {
  242. .notifier_call = netprio_device_event
  243. };
  244. static int __init init_cgroup_netprio(void)
  245. {
  246. int ret;
  247. ret = cgroup_load_subsys(&net_prio_subsys);
  248. if (ret)
  249. goto out;
  250. register_netdevice_notifier(&netprio_device_notifier);
  251. out:
  252. return ret;
  253. }
  254. static void __exit exit_cgroup_netprio(void)
  255. {
  256. struct netprio_map *old;
  257. struct net_device *dev;
  258. unregister_netdevice_notifier(&netprio_device_notifier);
  259. cgroup_unload_subsys(&net_prio_subsys);
  260. rtnl_lock();
  261. for_each_netdev(&init_net, dev) {
  262. old = rtnl_dereference(dev->priomap);
  263. RCU_INIT_POINTER(dev->priomap, NULL);
  264. if (old)
  265. kfree_rcu(old, rcu);
  266. }
  267. rtnl_unlock();
  268. }
  269. module_init(init_cgroup_netprio);
  270. module_exit(exit_cgroup_netprio);
  271. MODULE_LICENSE("GPL v2");