blob: 22036ab732cfbb5c2d99504eb3af05b292ea1be4 [file] [log] [blame]
Neil Horman5bc14212011-11-22 05:10:51 +00001/*
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
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
Li Zefan761b3ef2012-01-31 13:47:36 +080026static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp);
27static void cgrp_destroy(struct cgroup *cgrp);
Neil Horman5bc14212011-11-22 05:10:51 +000028static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp);
29
John Fastabend0221cd52011-12-09 13:39:27 -050030struct cgroup_subsys net_prio_subsys = {
Neil Horman5bc14212011-11-22 05:10:51 +000031 .name = "net_prio",
32 .create = cgrp_create,
33 .destroy = cgrp_destroy,
34 .populate = cgrp_populate,
35#ifdef CONFIG_NETPRIO_CGROUP
36 .subsys_id = net_prio_subsys_id,
37#endif
38 .module = THIS_MODULE
39};
40
41#define PRIOIDX_SZ 128
42
43static unsigned long prioidx_map[PRIOIDX_SZ];
44static DEFINE_SPINLOCK(prioidx_map_lock);
45static atomic_t max_prioidx = ATOMIC_INIT(0);
46
47static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
48{
49 return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
50 struct cgroup_netprio_state, css);
51}
52
53static int get_prioidx(u32 *prio)
54{
55 unsigned long flags;
56 u32 prioidx;
57
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)
63 return -ENOSPC;
64
65 atomic_set(&max_prioidx, prioidx);
66 *prio = prioidx;
67 return 0;
68}
69
70static void put_prioidx(u32 idx)
71{
72 unsigned long flags;
73
74 spin_lock_irqsave(&prioidx_map_lock, flags);
75 clear_bit(idx, prioidx_map);
76 spin_unlock_irqrestore(&prioidx_map_lock, flags);
77}
78
79static void extend_netdev_table(struct net_device *dev, u32 new_len)
80{
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;
85 int i;
86
87 old_priomap = rtnl_dereference(dev->priomap);
88
89 if (!new_priomap) {
90 printk(KERN_WARNING "Unable to alloc new priomap!\n");
91 return;
92 }
93
94 for (i = 0;
95 old_priomap && (i < old_priomap->priomap_len);
96 i++)
97 new_priomap->priomap[i] = old_priomap->priomap[i];
98
99 new_priomap->priomap_len = new_len;
100
101 rcu_assign_pointer(dev->priomap, new_priomap);
102 if (old_priomap)
103 kfree_rcu(old_priomap, rcu);
104}
105
106static void update_netdev_tables(void)
107{
108 struct net_device *dev;
109 u32 max_len = atomic_read(&max_prioidx);
110 struct netprio_map *map;
111
112 rtnl_lock();
113 for_each_netdev(&init_net, dev) {
114 map = rtnl_dereference(dev->priomap);
115 if ((!map) ||
116 (map->priomap_len < max_len))
117 extend_netdev_table(dev, max_len);
118 }
119 rtnl_unlock();
120}
121
Li Zefan761b3ef2012-01-31 13:47:36 +0800122static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000123{
124 struct cgroup_netprio_state *cs;
125 int ret;
126
127 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
128 if (!cs)
129 return ERR_PTR(-ENOMEM);
130
131 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
132 kfree(cs);
133 return ERR_PTR(-EINVAL);
134 }
135
136 ret = get_prioidx(&cs->prioidx);
137 if (ret != 0) {
138 printk(KERN_WARNING "No space in priority index array\n");
139 kfree(cs);
140 return ERR_PTR(ret);
141 }
142
143 return &cs->css;
144}
145
Li Zefan761b3ef2012-01-31 13:47:36 +0800146static void cgrp_destroy(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000147{
148 struct cgroup_netprio_state *cs;
149 struct net_device *dev;
150 struct netprio_map *map;
151
152 cs = cgrp_netprio_state(cgrp);
153 rtnl_lock();
154 for_each_netdev(&init_net, dev) {
155 map = rtnl_dereference(dev->priomap);
156 if (map)
157 map->priomap[cs->prioidx] = 0;
158 }
159 rtnl_unlock();
160 put_prioidx(cs->prioidx);
161 kfree(cs);
162}
163
164static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
165{
166 return (u64)cgrp_netprio_state(cgrp)->prioidx;
167}
168
169static int read_priomap(struct cgroup *cont, struct cftype *cft,
170 struct cgroup_map_cb *cb)
171{
172 struct net_device *dev;
173 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
174 u32 priority;
175 struct netprio_map *map;
176
177 rcu_read_lock();
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);
182 }
183 rcu_read_unlock();
184 return 0;
185}
186
187static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
188 const char *buffer)
189{
190 char *devname = kstrdup(buffer, GFP_KERNEL);
191 int ret = -EINVAL;
192 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
193 unsigned long priority;
194 char *priostr;
195 struct net_device *dev;
196 struct netprio_map *map;
197
198 if (!devname)
199 return -ENOMEM;
200
201 /*
202 * Minimally sized valid priomap string
203 */
204 if (strlen(devname) < 3)
205 goto out_free_devname;
206
207 priostr = strstr(devname, " ");
208 if (!priostr)
209 goto out_free_devname;
210
211 /*
212 *Separate the devname from the associated priority
213 *and advance the priostr poitner to the priority value
214 */
215 *priostr = '\0';
216 priostr++;
217
218 /*
219 * If the priostr points to NULL, we're at the end of the passed
220 * in string, and its not a valid write
221 */
222 if (*priostr == '\0')
223 goto out_free_devname;
224
225 ret = kstrtoul(priostr, 10, &priority);
226 if (ret < 0)
227 goto out_free_devname;
228
229 ret = -ENODEV;
230
231 dev = dev_get_by_name(&init_net, devname);
232 if (!dev)
233 goto out_free_devname;
234
235 update_netdev_tables();
236 ret = 0;
237 rcu_read_lock();
238 map = rcu_dereference(dev->priomap);
239 if (map)
240 map->priomap[prioidx] = priority;
241 rcu_read_unlock();
242 dev_put(dev);
243
244out_free_devname:
245 kfree(devname);
246 return ret;
247}
248
249static struct cftype ss_files[] = {
250 {
251 .name = "prioidx",
252 .read_u64 = read_prioidx,
253 },
254 {
255 .name = "ifpriomap",
256 .read_map = read_priomap,
257 .write_string = write_priomap,
258 },
259};
260
261static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
262{
263 return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files));
264}
265
266static int netprio_device_event(struct notifier_block *unused,
267 unsigned long event, void *ptr)
268{
269 struct net_device *dev = ptr;
270 struct netprio_map *old;
271 u32 max_len = atomic_read(&max_prioidx);
272
273 /*
274 * Note this is called with rtnl_lock held so we have update side
275 * protection on our rcu assignments
276 */
277
278 switch (event) {
279
280 case NETDEV_REGISTER:
281 if (max_len)
282 extend_netdev_table(dev, max_len);
283 break;
284 case NETDEV_UNREGISTER:
285 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000286 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000287 if (old)
288 kfree_rcu(old, rcu);
289 break;
290 }
291 return NOTIFY_DONE;
292}
293
294static struct notifier_block netprio_device_notifier = {
295 .notifier_call = netprio_device_event
296};
297
298static int __init init_cgroup_netprio(void)
299{
300 int ret;
301
302 ret = cgroup_load_subsys(&net_prio_subsys);
303 if (ret)
304 goto out;
305#ifndef CONFIG_NETPRIO_CGROUP
306 smp_wmb();
307 net_prio_subsys_id = net_prio_subsys.subsys_id;
308#endif
309
310 register_netdevice_notifier(&netprio_device_notifier);
311
312out:
313 return ret;
314}
315
316static void __exit exit_cgroup_netprio(void)
317{
318 struct netprio_map *old;
319 struct net_device *dev;
320
321 unregister_netdevice_notifier(&netprio_device_notifier);
322
323 cgroup_unload_subsys(&net_prio_subsys);
324
325#ifndef CONFIG_NETPRIO_CGROUP
326 net_prio_subsys_id = -1;
327 synchronize_rcu();
328#endif
329
330 rtnl_lock();
331 for_each_netdev(&init_net, dev) {
332 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000333 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000334 if (old)
335 kfree_rcu(old, rcu);
336 }
337 rtnl_unlock();
338}
339
340module_init(init_cgroup_netprio);
341module_exit(exit_cgroup_netprio);
342MODULE_LICENSE("GPL v2");