netprio_cgroup.c 7.1 KB

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