netprio_cgroup.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 PRIOIDX_SZ 128
  27. static unsigned long prioidx_map[PRIOIDX_SZ];
  28. static DEFINE_SPINLOCK(prioidx_map_lock);
  29. static atomic_t max_prioidx = ATOMIC_INIT(0);
  30. static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
  31. {
  32. return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
  33. struct cgroup_netprio_state, css);
  34. }
  35. static int get_prioidx(u32 *prio)
  36. {
  37. unsigned long flags;
  38. u32 prioidx;
  39. spin_lock_irqsave(&prioidx_map_lock, flags);
  40. prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
  41. if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
  42. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  43. return -ENOSPC;
  44. }
  45. set_bit(prioidx, prioidx_map);
  46. if (atomic_read(&max_prioidx) < prioidx)
  47. atomic_set(&max_prioidx, prioidx);
  48. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  49. *prio = prioidx;
  50. return 0;
  51. }
  52. static void put_prioidx(u32 idx)
  53. {
  54. unsigned long flags;
  55. spin_lock_irqsave(&prioidx_map_lock, flags);
  56. clear_bit(idx, prioidx_map);
  57. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  58. }
  59. static int extend_netdev_table(struct net_device *dev, u32 new_len)
  60. {
  61. size_t new_size = sizeof(struct netprio_map) +
  62. ((sizeof(u32) * new_len));
  63. struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
  64. struct netprio_map *old_priomap;
  65. int i;
  66. old_priomap = rtnl_dereference(dev->priomap);
  67. if (!new_priomap) {
  68. pr_warn("Unable to alloc new priomap!\n");
  69. return -ENOMEM;
  70. }
  71. for (i = 0;
  72. old_priomap && (i < old_priomap->priomap_len);
  73. i++)
  74. new_priomap->priomap[i] = old_priomap->priomap[i];
  75. new_priomap->priomap_len = new_len;
  76. rcu_assign_pointer(dev->priomap, new_priomap);
  77. if (old_priomap)
  78. kfree_rcu(old_priomap, rcu);
  79. return 0;
  80. }
  81. static int write_update_netdev_table(struct net_device *dev)
  82. {
  83. int ret = 0;
  84. u32 max_len;
  85. struct netprio_map *map;
  86. max_len = atomic_read(&max_prioidx) + 1;
  87. map = rtnl_dereference(dev->priomap);
  88. if (!map || map->priomap_len < max_len)
  89. ret = extend_netdev_table(dev, max_len);
  90. return ret;
  91. }
  92. static int update_netdev_tables(void)
  93. {
  94. int ret = 0;
  95. struct net_device *dev;
  96. u32 max_len;
  97. struct netprio_map *map;
  98. rtnl_lock();
  99. max_len = atomic_read(&max_prioidx) + 1;
  100. for_each_netdev(&init_net, dev) {
  101. map = rtnl_dereference(dev->priomap);
  102. /*
  103. * don't allocate priomap if we didn't
  104. * change net_prio.ifpriomap (map == NULL),
  105. * this will speed up skb_update_prio.
  106. */
  107. if (map && map->priomap_len < max_len) {
  108. ret = extend_netdev_table(dev, max_len);
  109. if (ret < 0)
  110. break;
  111. }
  112. }
  113. rtnl_unlock();
  114. return ret;
  115. }
  116. static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
  117. {
  118. struct cgroup_netprio_state *cs;
  119. int ret = -EINVAL;
  120. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  121. if (!cs)
  122. return ERR_PTR(-ENOMEM);
  123. if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx)
  124. goto out;
  125. ret = get_prioidx(&cs->prioidx);
  126. if (ret < 0) {
  127. pr_warn("No space in priority index array\n");
  128. goto out;
  129. }
  130. ret = update_netdev_tables();
  131. if (ret < 0) {
  132. put_prioidx(cs->prioidx);
  133. goto out;
  134. }
  135. return &cs->css;
  136. out:
  137. kfree(cs);
  138. return ERR_PTR(ret);
  139. }
  140. static void cgrp_destroy(struct cgroup *cgrp)
  141. {
  142. struct cgroup_netprio_state *cs;
  143. struct net_device *dev;
  144. struct netprio_map *map;
  145. cs = cgrp_netprio_state(cgrp);
  146. rtnl_lock();
  147. for_each_netdev(&init_net, dev) {
  148. map = rtnl_dereference(dev->priomap);
  149. if (map && cs->prioidx < map->priomap_len)
  150. map->priomap[cs->prioidx] = 0;
  151. }
  152. rtnl_unlock();
  153. put_prioidx(cs->prioidx);
  154. kfree(cs);
  155. }
  156. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  157. {
  158. return (u64)cgrp_netprio_state(cgrp)->prioidx;
  159. }
  160. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  161. struct cgroup_map_cb *cb)
  162. {
  163. struct net_device *dev;
  164. u32 prioidx = cgrp_netprio_state(cont)->prioidx;
  165. u32 priority;
  166. struct netprio_map *map;
  167. rcu_read_lock();
  168. for_each_netdev_rcu(&init_net, dev) {
  169. map = rcu_dereference(dev->priomap);
  170. priority = (map && prioidx < map->priomap_len) ? map->priomap[prioidx] : 0;
  171. cb->fill(cb, dev->name, priority);
  172. }
  173. rcu_read_unlock();
  174. return 0;
  175. }
  176. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  177. const char *buffer)
  178. {
  179. char *devname = kstrdup(buffer, GFP_KERNEL);
  180. int ret = -EINVAL;
  181. u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
  182. unsigned long priority;
  183. char *priostr;
  184. struct net_device *dev;
  185. struct netprio_map *map;
  186. if (!devname)
  187. return -ENOMEM;
  188. /*
  189. * Minimally sized valid priomap string
  190. */
  191. if (strlen(devname) < 3)
  192. goto out_free_devname;
  193. priostr = strstr(devname, " ");
  194. if (!priostr)
  195. goto out_free_devname;
  196. /*
  197. *Separate the devname from the associated priority
  198. *and advance the priostr pointer to the priority value
  199. */
  200. *priostr = '\0';
  201. priostr++;
  202. /*
  203. * If the priostr points to NULL, we're at the end of the passed
  204. * in string, and its not a valid write
  205. */
  206. if (*priostr == '\0')
  207. goto out_free_devname;
  208. ret = kstrtoul(priostr, 10, &priority);
  209. if (ret < 0)
  210. goto out_free_devname;
  211. ret = -ENODEV;
  212. dev = dev_get_by_name(&init_net, devname);
  213. if (!dev)
  214. goto out_free_devname;
  215. rtnl_lock();
  216. ret = write_update_netdev_table(dev);
  217. if (ret < 0)
  218. goto out_put_dev;
  219. map = rtnl_dereference(dev->priomap);
  220. if (map)
  221. map->priomap[prioidx] = priority;
  222. out_put_dev:
  223. rtnl_unlock();
  224. dev_put(dev);
  225. out_free_devname:
  226. kfree(devname);
  227. return ret;
  228. }
  229. void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
  230. {
  231. struct task_struct *p;
  232. cgroup_taskset_for_each(p, cgrp, tset) {
  233. unsigned int fd;
  234. struct fdtable *fdt;
  235. struct files_struct *files;
  236. task_lock(p);
  237. files = p->files;
  238. if (!files) {
  239. task_unlock(p);
  240. continue;
  241. }
  242. spin_lock(&files->file_lock);
  243. fdt = files_fdtable(files);
  244. for (fd = 0; fd < fdt->max_fds; fd++) {
  245. struct file *file;
  246. struct socket *sock;
  247. int err;
  248. file = fcheck_files(files, fd);
  249. if (!file)
  250. continue;
  251. sock = sock_from_file(file, &err);
  252. if (sock)
  253. sock_update_netprioidx(sock->sk, p);
  254. }
  255. spin_unlock(&files->file_lock);
  256. task_unlock(p);
  257. }
  258. }
  259. static struct cftype ss_files[] = {
  260. {
  261. .name = "prioidx",
  262. .read_u64 = read_prioidx,
  263. },
  264. {
  265. .name = "ifpriomap",
  266. .read_map = read_priomap,
  267. .write_string = write_priomap,
  268. },
  269. { } /* terminate */
  270. };
  271. struct cgroup_subsys net_prio_subsys = {
  272. .name = "net_prio",
  273. .create = cgrp_create,
  274. .destroy = cgrp_destroy,
  275. .attach = net_prio_attach,
  276. #ifdef CONFIG_NETPRIO_CGROUP
  277. .subsys_id = net_prio_subsys_id,
  278. #endif
  279. .base_cftypes = ss_files,
  280. .module = THIS_MODULE
  281. };
  282. static int netprio_device_event(struct notifier_block *unused,
  283. unsigned long event, void *ptr)
  284. {
  285. struct net_device *dev = ptr;
  286. struct netprio_map *old;
  287. /*
  288. * Note this is called with rtnl_lock held so we have update side
  289. * protection on our rcu assignments
  290. */
  291. switch (event) {
  292. case NETDEV_UNREGISTER:
  293. old = rtnl_dereference(dev->priomap);
  294. RCU_INIT_POINTER(dev->priomap, NULL);
  295. if (old)
  296. kfree_rcu(old, rcu);
  297. break;
  298. }
  299. return NOTIFY_DONE;
  300. }
  301. static struct notifier_block netprio_device_notifier = {
  302. .notifier_call = netprio_device_event
  303. };
  304. static int __init init_cgroup_netprio(void)
  305. {
  306. int ret;
  307. ret = cgroup_load_subsys(&net_prio_subsys);
  308. if (ret)
  309. goto out;
  310. #ifndef CONFIG_NETPRIO_CGROUP
  311. smp_wmb();
  312. net_prio_subsys_id = net_prio_subsys.subsys_id;
  313. #endif
  314. register_netdevice_notifier(&netprio_device_notifier);
  315. out:
  316. return ret;
  317. }
  318. static void __exit exit_cgroup_netprio(void)
  319. {
  320. struct netprio_map *old;
  321. struct net_device *dev;
  322. unregister_netdevice_notifier(&netprio_device_notifier);
  323. cgroup_unload_subsys(&net_prio_subsys);
  324. #ifndef CONFIG_NETPRIO_CGROUP
  325. net_prio_subsys_id = -1;
  326. synchronize_rcu();
  327. #endif
  328. rtnl_lock();
  329. for_each_netdev(&init_net, dev) {
  330. old = rtnl_dereference(dev->priomap);
  331. RCU_INIT_POINTER(dev->priomap, NULL);
  332. if (old)
  333. kfree_rcu(old, rcu);
  334. }
  335. rtnl_unlock();
  336. }
  337. module_init(init_cgroup_netprio);
  338. module_exit(exit_cgroup_netprio);
  339. MODULE_LICENSE("GPL v2");