netprio_cgroup.c 7.5 KB

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