2 * net/core/netprio_cgroup.c Priority Control Group
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.
9 * Authors: Neil Horman <nhorman@tuxdriver.com>
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>
24 #include <net/netprio_cgroup.h>
26 static struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
28 static void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp);
29 static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
31 struct cgroup_subsys net_prio_subsys = {
33 .create = cgrp_create,
34 .destroy = cgrp_destroy,
35 .populate = cgrp_populate,
36 #ifdef CONFIG_NETPRIO_CGROUP
37 .subsys_id = net_prio_subsys_id,
42 #define PRIOIDX_SZ 128
44 static unsigned long prioidx_map[PRIOIDX_SZ];
45 static DEFINE_SPINLOCK(prioidx_map_lock);
46 static atomic_t max_prioidx = ATOMIC_INIT(0);
48 static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
50 return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
51 struct cgroup_netprio_state, css);
54 static int get_prioidx(u32 *prio)
59 spin_lock_irqsave(&prioidx_map_lock, flags);
60 prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
61 set_bit(prioidx, prioidx_map);
62 spin_unlock_irqrestore(&prioidx_map_lock, flags);
63 if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ)
66 atomic_set(&max_prioidx, prioidx);
71 static void put_prioidx(u32 idx)
75 spin_lock_irqsave(&prioidx_map_lock, flags);
76 clear_bit(idx, prioidx_map);
77 spin_unlock_irqrestore(&prioidx_map_lock, flags);
80 static void extend_netdev_table(struct net_device *dev, u32 new_len)
82 size_t new_size = sizeof(struct netprio_map) +
83 ((sizeof(u32) * new_len));
84 struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
85 struct netprio_map *old_priomap;
88 old_priomap = rtnl_dereference(dev->priomap);
91 printk(KERN_WARNING "Unable to alloc new priomap!\n");
96 old_priomap && (i < old_priomap->priomap_len);
98 new_priomap->priomap[i] = old_priomap->priomap[i];
100 new_priomap->priomap_len = new_len;
102 rcu_assign_pointer(dev->priomap, new_priomap);
104 kfree_rcu(old_priomap, rcu);
107 static void update_netdev_tables(void)
109 struct net_device *dev;
110 u32 max_len = atomic_read(&max_prioidx);
111 struct netprio_map *map;
114 for_each_netdev(&init_net, dev) {
115 map = rtnl_dereference(dev->priomap);
117 (map->priomap_len < max_len))
118 extend_netdev_table(dev, max_len);
123 static struct cgroup_subsys_state *cgrp_create(struct cgroup_subsys *ss,
126 struct cgroup_netprio_state *cs;
129 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
131 return ERR_PTR(-ENOMEM);
133 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
135 return ERR_PTR(-EINVAL);
138 ret = get_prioidx(&cs->prioidx);
140 printk(KERN_WARNING "No space in priority index array\n");
148 static void cgrp_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
150 struct cgroup_netprio_state *cs;
151 struct net_device *dev;
152 struct netprio_map *map;
154 cs = cgrp_netprio_state(cgrp);
156 for_each_netdev(&init_net, dev) {
157 map = rtnl_dereference(dev->priomap);
159 map->priomap[cs->prioidx] = 0;
162 put_prioidx(cs->prioidx);
166 static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
168 return (u64)cgrp_netprio_state(cgrp)->prioidx;
171 static int read_priomap(struct cgroup *cont, struct cftype *cft,
172 struct cgroup_map_cb *cb)
174 struct net_device *dev;
175 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
177 struct netprio_map *map;
180 for_each_netdev_rcu(&init_net, dev) {
181 map = rcu_dereference(dev->priomap);
182 priority = map ? map->priomap[prioidx] : 0;
183 cb->fill(cb, dev->name, priority);
189 static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
192 char *devname = kstrdup(buffer, GFP_KERNEL);
194 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
195 unsigned long priority;
197 struct net_device *dev;
198 struct netprio_map *map;
204 * Minimally sized valid priomap string
206 if (strlen(devname) < 3)
207 goto out_free_devname;
209 priostr = strstr(devname, " ");
211 goto out_free_devname;
214 *Separate the devname from the associated priority
215 *and advance the priostr poitner to the priority value
221 * If the priostr points to NULL, we're at the end of the passed
222 * in string, and its not a valid write
224 if (*priostr == '\0')
225 goto out_free_devname;
227 ret = kstrtoul(priostr, 10, &priority);
229 goto out_free_devname;
233 dev = dev_get_by_name(&init_net, devname);
235 goto out_free_devname;
237 update_netdev_tables();
240 map = rcu_dereference(dev->priomap);
242 map->priomap[prioidx] = priority;
251 static struct cftype ss_files[] = {
254 .read_u64 = read_prioidx,
258 .read_map = read_priomap,
259 .write_string = write_priomap,
263 static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
265 return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
268 static int netprio_device_event(struct notifier_block *unused,
269 unsigned long event, void *ptr)
271 struct net_device *dev = ptr;
272 struct netprio_map *old;
273 u32 max_len = atomic_read(&max_prioidx);
276 * Note this is called with rtnl_lock held so we have update side
277 * protection on our rcu assignments
282 case NETDEV_REGISTER:
284 extend_netdev_table(dev, max_len);
286 case NETDEV_UNREGISTER:
287 old = rtnl_dereference(dev->priomap);
288 rcu_assign_pointer(dev->priomap, NULL);
296 static struct notifier_block netprio_device_notifier = {
297 .notifier_call = netprio_device_event
300 static int __init init_cgroup_netprio(void)
304 ret = cgroup_load_subsys(&net_prio_subsys);
307 #ifndef CONFIG_NETPRIO_CGROUP
309 net_prio_subsys_id = net_prio_subsys.subsys_id;
312 register_netdevice_notifier(&netprio_device_notifier);
318 static void __exit exit_cgroup_netprio(void)
320 struct netprio_map *old;
321 struct net_device *dev;
323 unregister_netdevice_notifier(&netprio_device_notifier);
325 cgroup_unload_subsys(&net_prio_subsys);
327 #ifndef CONFIG_NETPRIO_CGROUP
328 net_prio_subsys_id = -1;
333 for_each_netdev(&init_net, dev) {
334 old = rtnl_dereference(dev->priomap);
335 rcu_assign_pointer(dev->priomap, NULL);
342 module_init(init_cgroup_netprio);
343 module_exit(exit_cgroup_netprio);
344 MODULE_LICENSE("GPL v2");