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 if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
61 spin_unlock_irqrestore(&prioidx_map_lock, flags);
64 set_bit(prioidx, prioidx_map);
65 spin_unlock_irqrestore(&prioidx_map_lock, flags);
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) + 1;
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 *cgrp)
125 struct cgroup_netprio_state *cs;
128 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
130 return ERR_PTR(-ENOMEM);
132 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
134 return ERR_PTR(-EINVAL);
137 ret = get_prioidx(&cs->prioidx);
139 printk(KERN_WARNING "No space in priority index array\n");
147 static void cgrp_destroy(struct cgroup *cgrp)
149 struct cgroup_netprio_state *cs;
150 struct net_device *dev;
151 struct netprio_map *map;
153 cs = cgrp_netprio_state(cgrp);
155 for_each_netdev(&init_net, dev) {
156 map = rtnl_dereference(dev->priomap);
158 map->priomap[cs->prioidx] = 0;
161 put_prioidx(cs->prioidx);
165 static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
167 return (u64)cgrp_netprio_state(cgrp)->prioidx;
170 static int read_priomap(struct cgroup *cont, struct cftype *cft,
171 struct cgroup_map_cb *cb)
173 struct net_device *dev;
174 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
176 struct netprio_map *map;
179 for_each_netdev_rcu(&init_net, dev) {
180 map = rcu_dereference(dev->priomap);
181 priority = map ? map->priomap[prioidx] : 0;
182 cb->fill(cb, dev->name, priority);
188 static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
191 char *devname = kstrdup(buffer, GFP_KERNEL);
193 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
194 unsigned long priority;
196 struct net_device *dev;
197 struct netprio_map *map;
203 * Minimally sized valid priomap string
205 if (strlen(devname) < 3)
206 goto out_free_devname;
208 priostr = strstr(devname, " ");
210 goto out_free_devname;
213 *Separate the devname from the associated priority
214 *and advance the priostr poitner to the priority value
220 * If the priostr points to NULL, we're at the end of the passed
221 * in string, and its not a valid write
223 if (*priostr == '\0')
224 goto out_free_devname;
226 ret = kstrtoul(priostr, 10, &priority);
228 goto out_free_devname;
232 dev = dev_get_by_name(&init_net, devname);
234 goto out_free_devname;
236 update_netdev_tables();
239 map = rcu_dereference(dev->priomap);
241 map->priomap[prioidx] = priority;
250 static struct cftype ss_files[] = {
253 .read_u64 = read_prioidx,
257 .read_map = read_priomap,
258 .write_string = write_priomap,
262 static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
264 return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
267 static int netprio_device_event(struct notifier_block *unused,
268 unsigned long event, void *ptr)
270 struct net_device *dev = ptr;
271 struct netprio_map *old;
274 * Note this is called with rtnl_lock held so we have update side
275 * protection on our rcu assignments
279 case NETDEV_UNREGISTER:
280 old = rtnl_dereference(dev->priomap);
281 RCU_INIT_POINTER(dev->priomap, NULL);
289 static struct notifier_block netprio_device_notifier = {
290 .notifier_call = netprio_device_event
293 static int __init init_cgroup_netprio(void)
297 ret = cgroup_load_subsys(&net_prio_subsys);
300 #ifndef CONFIG_NETPRIO_CGROUP
302 net_prio_subsys_id = net_prio_subsys.subsys_id;
305 register_netdevice_notifier(&netprio_device_notifier);
311 static void __exit exit_cgroup_netprio(void)
313 struct netprio_map *old;
314 struct net_device *dev;
316 unregister_netdevice_notifier(&netprio_device_notifier);
318 cgroup_unload_subsys(&net_prio_subsys);
320 #ifndef CONFIG_NETPRIO_CGROUP
321 net_prio_subsys_id = -1;
326 for_each_netdev(&init_net, dev) {
327 old = rtnl_dereference(dev->priomap);
328 RCU_INIT_POINTER(dev->priomap, NULL);
335 module_init(init_cgroup_netprio);
336 module_exit(exit_cgroup_netprio);
337 MODULE_LICENSE("GPL v2");