netprio_cgroup.c 7.1 KB

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