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 *cgrp);
27 static void cgrp_destroy(struct cgroup *cgrp);
28 static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
30 struct cgroup_subsys net_prio_subsys = {
32 .create = cgrp_create,
33 .destroy = cgrp_destroy,
34 .populate = cgrp_populate,
35 #ifdef CONFIG_NETPRIO_CGROUP
36 .subsys_id = net_prio_subsys_id,
41 #define PRIOIDX_SZ 128
43 static unsigned long prioidx_map[PRIOIDX_SZ];
44 static DEFINE_SPINLOCK(prioidx_map_lock);
45 static atomic_t max_prioidx = ATOMIC_INIT(0);
47 static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
49 return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
50 struct cgroup_netprio_state, css);
53 static int get_prioidx(u32 *prio)
58 spin_lock_irqsave(&prioidx_map_lock, flags);
59 prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
60 set_bit(prioidx, prioidx_map);
61 spin_unlock_irqrestore(&prioidx_map_lock, flags);
62 if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ)
65 atomic_set(&max_prioidx, prioidx);
70 static void put_prioidx(u32 idx)
74 spin_lock_irqsave(&prioidx_map_lock, flags);
75 clear_bit(idx, prioidx_map);
76 spin_unlock_irqrestore(&prioidx_map_lock, flags);
79 static void extend_netdev_table(struct net_device *dev, u32 new_len)
81 size_t new_size = sizeof(struct netprio_map) +
82 ((sizeof(u32) * new_len));
83 struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
84 struct netprio_map *old_priomap;
87 old_priomap = rtnl_dereference(dev->priomap);
90 printk(KERN_WARNING "Unable to alloc new priomap!\n");
95 old_priomap && (i < old_priomap->priomap_len);
97 new_priomap->priomap[i] = old_priomap->priomap[i];
99 new_priomap->priomap_len = new_len;
101 rcu_assign_pointer(dev->priomap, new_priomap);
103 kfree_rcu(old_priomap, rcu);
106 static void update_netdev_tables(void)
108 struct net_device *dev;
109 u32 max_len = atomic_read(&max_prioidx);
110 struct netprio_map *map;
113 for_each_netdev(&init_net, dev) {
114 map = rtnl_dereference(dev->priomap);
116 (map->priomap_len < max_len))
117 extend_netdev_table(dev, max_len);
122 static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
124 struct cgroup_netprio_state *cs;
127 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
129 return ERR_PTR(-ENOMEM);
131 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
133 return ERR_PTR(-EINVAL);
136 ret = get_prioidx(&cs->prioidx);
138 printk(KERN_WARNING "No space in priority index array\n");
146 static void cgrp_destroy(struct cgroup *cgrp)
148 struct cgroup_netprio_state *cs;
149 struct net_device *dev;
150 struct netprio_map *map;
152 cs = cgrp_netprio_state(cgrp);
154 for_each_netdev(&init_net, dev) {
155 map = rtnl_dereference(dev->priomap);
157 map->priomap[cs->prioidx] = 0;
160 put_prioidx(cs->prioidx);
164 static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
166 return (u64)cgrp_netprio_state(cgrp)->prioidx;
169 static int read_priomap(struct cgroup *cont, struct cftype *cft,
170 struct cgroup_map_cb *cb)
172 struct net_device *dev;
173 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
175 struct netprio_map *map;
178 for_each_netdev_rcu(&init_net, dev) {
179 map = rcu_dereference(dev->priomap);
180 priority = map ? map->priomap[prioidx] : 0;
181 cb->fill(cb, dev->name, priority);
187 static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
190 char *devname = kstrdup(buffer, GFP_KERNEL);
192 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
193 unsigned long priority;
195 struct net_device *dev;
196 struct netprio_map *map;
202 * Minimally sized valid priomap string
204 if (strlen(devname) < 3)
205 goto out_free_devname;
207 priostr = strstr(devname, " ");
209 goto out_free_devname;
212 *Separate the devname from the associated priority
213 *and advance the priostr poitner to the priority value
219 * If the priostr points to NULL, we're at the end of the passed
220 * in string, and its not a valid write
222 if (*priostr == '\0')
223 goto out_free_devname;
225 ret = kstrtoul(priostr, 10, &priority);
227 goto out_free_devname;
231 dev = dev_get_by_name(&init_net, devname);
233 goto out_free_devname;
235 update_netdev_tables();
238 map = rcu_dereference(dev->priomap);
240 map->priomap[prioidx] = priority;
249 static struct cftype ss_files[] = {
252 .read_u64 = read_prioidx,
256 .read_map = read_priomap,
257 .write_string = write_priomap,
261 static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
263 return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
266 static int netprio_device_event(struct notifier_block *unused,
267 unsigned long event, void *ptr)
269 struct net_device *dev = ptr;
270 struct netprio_map *old;
271 u32 max_len = atomic_read(&max_prioidx);
274 * Note this is called with rtnl_lock held so we have update side
275 * protection on our rcu assignments
280 case NETDEV_REGISTER:
282 extend_netdev_table(dev, max_len);
284 case NETDEV_UNREGISTER:
285 old = rtnl_dereference(dev->priomap);
286 RCU_INIT_POINTER(dev->priomap, NULL);
294 static struct notifier_block netprio_device_notifier = {
295 .notifier_call = netprio_device_event
298 static int __init init_cgroup_netprio(void)
302 ret = cgroup_load_subsys(&net_prio_subsys);
305 #ifndef CONFIG_NETPRIO_CGROUP
307 net_prio_subsys_id = net_prio_subsys.subsys_id;
310 register_netdevice_notifier(&netprio_device_notifier);
316 static void __exit exit_cgroup_netprio(void)
318 struct netprio_map *old;
319 struct net_device *dev;
321 unregister_netdevice_notifier(&netprio_device_notifier);
323 cgroup_unload_subsys(&net_prio_subsys);
325 #ifndef CONFIG_NETPRIO_CGROUP
326 net_prio_subsys_id = -1;
331 for_each_netdev(&init_net, dev) {
332 old = rtnl_dereference(dev->priomap);
333 RCU_INIT_POINTER(dev->priomap, NULL);
340 module_init(init_cgroup_netprio);
341 module_exit(exit_cgroup_netprio);
342 MODULE_LICENSE("GPL v2");