netprio_cgroup.c 7.4 KB

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