netprio_cgroup.c 7.9 KB

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