netprio_cgroup.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/types.h>
  14. #include <linux/string.h>
  15. #include <linux/errno.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/cgroup.h>
  18. #include <linux/rcupdate.h>
  19. #include <linux/atomic.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/sock.h>
  23. #include <net/netprio_cgroup.h>
  24. static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp);
  25. static void cgrp_destroy(struct cgroup *cgrp);
  26. static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
  27. struct cgroup_subsys net_prio_subsys = {
  28. .name = "net_prio",
  29. .create = cgrp_create,
  30. .destroy = cgrp_destroy,
  31. .populate = cgrp_populate,
  32. #ifdef CONFIG_NETPRIO_CGROUP
  33. .subsys_id = net_prio_subsys_id,
  34. #endif
  35. .module = THIS_MODULE
  36. };
  37. #define PRIOIDX_SZ 128
  38. static unsigned long prioidx_map[PRIOIDX_SZ];
  39. static DEFINE_SPINLOCK(prioidx_map_lock);
  40. static atomic_t max_prioidx = ATOMIC_INIT(0);
  41. static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
  42. {
  43. return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
  44. struct cgroup_netprio_state, css);
  45. }
  46. static int get_prioidx(u32 *prio)
  47. {
  48. unsigned long flags;
  49. u32 prioidx;
  50. spin_lock_irqsave(&prioidx_map_lock, flags);
  51. prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
  52. if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
  53. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  54. return -ENOSPC;
  55. }
  56. set_bit(prioidx, prioidx_map);
  57. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  58. atomic_set(&max_prioidx, prioidx);
  59. *prio = prioidx;
  60. return 0;
  61. }
  62. static void put_prioidx(u32 idx)
  63. {
  64. unsigned long flags;
  65. spin_lock_irqsave(&prioidx_map_lock, flags);
  66. clear_bit(idx, prioidx_map);
  67. spin_unlock_irqrestore(&prioidx_map_lock, flags);
  68. }
  69. static void extend_netdev_table(struct net_device *dev, u32 new_len)
  70. {
  71. size_t new_size = sizeof(struct netprio_map) +
  72. ((sizeof(u32) * new_len));
  73. struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
  74. struct netprio_map *old_priomap;
  75. int i;
  76. old_priomap = rtnl_dereference(dev->priomap);
  77. if (!new_priomap) {
  78. printk(KERN_WARNING "Unable to alloc new priomap!\n");
  79. return;
  80. }
  81. for (i = 0;
  82. old_priomap && (i < old_priomap->priomap_len);
  83. i++)
  84. new_priomap->priomap[i] = old_priomap->priomap[i];
  85. new_priomap->priomap_len = new_len;
  86. rcu_assign_pointer(dev->priomap, new_priomap);
  87. if (old_priomap)
  88. kfree_rcu(old_priomap, rcu);
  89. }
  90. static void update_netdev_tables(void)
  91. {
  92. struct net_device *dev;
  93. u32 max_len = atomic_read(&max_prioidx) + 1;
  94. struct netprio_map *map;
  95. rtnl_lock();
  96. for_each_netdev(&init_net, dev) {
  97. map = rtnl_dereference(dev->priomap);
  98. if ((!map) ||
  99. (map->priomap_len < max_len))
  100. extend_netdev_table(dev, max_len);
  101. }
  102. rtnl_unlock();
  103. }
  104. static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
  105. {
  106. struct cgroup_netprio_state *cs;
  107. int ret;
  108. cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  109. if (!cs)
  110. return ERR_PTR(-ENOMEM);
  111. if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
  112. kfree(cs);
  113. return ERR_PTR(-EINVAL);
  114. }
  115. ret = get_prioidx(&cs->prioidx);
  116. if (ret != 0) {
  117. printk(KERN_WARNING "No space in priority index array\n");
  118. kfree(cs);
  119. return ERR_PTR(ret);
  120. }
  121. return &cs->css;
  122. }
  123. static void cgrp_destroy(struct cgroup *cgrp)
  124. {
  125. struct cgroup_netprio_state *cs;
  126. struct net_device *dev;
  127. struct netprio_map *map;
  128. cs = cgrp_netprio_state(cgrp);
  129. rtnl_lock();
  130. for_each_netdev(&init_net, dev) {
  131. map = rtnl_dereference(dev->priomap);
  132. if (map && cs->prioidx < map->priomap_len)
  133. map->priomap[cs->prioidx] = 0;
  134. }
  135. rtnl_unlock();
  136. put_prioidx(cs->prioidx);
  137. kfree(cs);
  138. }
  139. static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
  140. {
  141. return (u64)cgrp_netprio_state(cgrp)->prioidx;
  142. }
  143. static int read_priomap(struct cgroup *cont, struct cftype *cft,
  144. struct cgroup_map_cb *cb)
  145. {
  146. struct net_device *dev;
  147. u32 prioidx = cgrp_netprio_state(cont)->prioidx;
  148. u32 priority;
  149. struct netprio_map *map;
  150. rcu_read_lock();
  151. for_each_netdev_rcu(&init_net, dev) {
  152. map = rcu_dereference(dev->priomap);
  153. priority = (map && prioidx < map->priomap_len) ? map->priomap[prioidx] : 0;
  154. cb->fill(cb, dev->name, priority);
  155. }
  156. rcu_read_unlock();
  157. return 0;
  158. }
  159. static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
  160. const char *buffer)
  161. {
  162. char *devname = kstrdup(buffer, GFP_KERNEL);
  163. int ret = -EINVAL;
  164. u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
  165. unsigned long priority;
  166. char *priostr;
  167. struct net_device *dev;
  168. struct netprio_map *map;
  169. if (!devname)
  170. return -ENOMEM;
  171. /*
  172. * Minimally sized valid priomap string
  173. */
  174. if (strlen(devname) < 3)
  175. goto out_free_devname;
  176. priostr = strstr(devname, " ");
  177. if (!priostr)
  178. goto out_free_devname;
  179. /*
  180. *Separate the devname from the associated priority
  181. *and advance the priostr poitner to the priority value
  182. */
  183. *priostr = '\0';
  184. priostr++;
  185. /*
  186. * If the priostr points to NULL, we're at the end of the passed
  187. * in string, and its not a valid write
  188. */
  189. if (*priostr == '\0')
  190. goto out_free_devname;
  191. ret = kstrtoul(priostr, 10, &priority);
  192. if (ret < 0)
  193. goto out_free_devname;
  194. ret = -ENODEV;
  195. dev = dev_get_by_name(&init_net, devname);
  196. if (!dev)
  197. goto out_free_devname;
  198. update_netdev_tables();
  199. ret = 0;
  200. rcu_read_lock();
  201. map = rcu_dereference(dev->priomap);
  202. if (map)
  203. map->priomap[prioidx] = priority;
  204. rcu_read_unlock();
  205. dev_put(dev);
  206. out_free_devname:
  207. kfree(devname);
  208. return ret;
  209. }
  210. static struct cftype ss_files[] = {
  211. {
  212. .name = "prioidx",
  213. .read_u64 = read_prioidx,
  214. },
  215. {
  216. .name = "ifpriomap",
  217. .read_map = read_priomap,
  218. .write_string = write_priomap,
  219. },
  220. };
  221. static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  222. {
  223. return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
  224. }
  225. static int netprio_device_event(struct notifier_block *unused,
  226. unsigned long event, void *ptr)
  227. {
  228. struct net_device *dev = ptr;
  229. struct netprio_map *old;
  230. /*
  231. * Note this is called with rtnl_lock held so we have update side
  232. * protection on our rcu assignments
  233. */
  234. switch (event) {
  235. case NETDEV_UNREGISTER:
  236. old = rtnl_dereference(dev->priomap);
  237. RCU_INIT_POINTER(dev->priomap, NULL);
  238. if (old)
  239. kfree_rcu(old, rcu);
  240. break;
  241. }
  242. return NOTIFY_DONE;
  243. }
  244. static struct notifier_block netprio_device_notifier = {
  245. .notifier_call = netprio_device_event
  246. };
  247. static int __init init_cgroup_netprio(void)
  248. {
  249. int ret;
  250. ret = cgroup_load_subsys(&net_prio_subsys);
  251. if (ret)
  252. goto out;
  253. #ifndef CONFIG_NETPRIO_CGROUP
  254. smp_wmb();
  255. net_prio_subsys_id = net_prio_subsys.subsys_id;
  256. #endif
  257. register_netdevice_notifier(&netprio_device_notifier);
  258. out:
  259. return ret;
  260. }
  261. static void __exit exit_cgroup_netprio(void)
  262. {
  263. struct netprio_map *old;
  264. struct net_device *dev;
  265. unregister_netdevice_notifier(&netprio_device_notifier);
  266. cgroup_unload_subsys(&net_prio_subsys);
  267. #ifndef CONFIG_NETPRIO_CGROUP
  268. net_prio_subsys_id = -1;
  269. synchronize_rcu();
  270. #endif
  271. rtnl_lock();
  272. for_each_netdev(&init_net, dev) {
  273. old = rtnl_dereference(dev->priomap);
  274. RCU_INIT_POINTER(dev->priomap, NULL);
  275. if (old)
  276. kfree_rcu(old, rcu);
  277. }
  278. rtnl_unlock();
  279. }
  280. module_init(init_cgroup_netprio);
  281. module_exit(exit_cgroup_netprio);
  282. MODULE_LICENSE("GPL v2");