netprio_cgroup.c 8.9 KB

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