netprio_cgroup.c 8.2 KB

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