netprio_cgroup.c 7.2 KB

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