blob: b2a59866699f7cae8a21ff809775d02754b51ff7 [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
Neil Horman5bc14212011-11-22 05:10:51 +000026#define PRIOIDX_SZ 128
27
28static unsigned long prioidx_map[PRIOIDX_SZ];
29static DEFINE_SPINLOCK(prioidx_map_lock);
30static atomic_t max_prioidx = ATOMIC_INIT(0);
31
32static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
33{
34 return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
35 struct cgroup_netprio_state, css);
36}
37
38static int get_prioidx(u32 *prio)
39{
40 unsigned long flags;
41 u32 prioidx;
42
43 spin_lock_irqsave(&prioidx_map_lock, flags);
44 prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
Neil Horman5962b352012-02-03 05:18:43 +000045 if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
46 spin_unlock_irqrestore(&prioidx_map_lock, flags);
47 return -ENOSPC;
48 }
Neil Horman5bc14212011-11-22 05:10:51 +000049 set_bit(prioidx, prioidx_map);
50 spin_unlock_irqrestore(&prioidx_map_lock, flags);
Neil Horman5bc14212011-11-22 05:10:51 +000051 atomic_set(&max_prioidx, prioidx);
52 *prio = prioidx;
53 return 0;
54}
55
56static void put_prioidx(u32 idx)
57{
58 unsigned long flags;
59
60 spin_lock_irqsave(&prioidx_map_lock, flags);
61 clear_bit(idx, prioidx_map);
62 spin_unlock_irqrestore(&prioidx_map_lock, flags);
63}
64
65static void extend_netdev_table(struct net_device *dev, u32 new_len)
66{
67 size_t new_size = sizeof(struct netprio_map) +
68 ((sizeof(u32) * new_len));
69 struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
70 struct netprio_map *old_priomap;
71 int i;
72
73 old_priomap = rtnl_dereference(dev->priomap);
74
75 if (!new_priomap) {
76 printk(KERN_WARNING "Unable to alloc new priomap!\n");
77 return;
78 }
79
80 for (i = 0;
81 old_priomap && (i < old_priomap->priomap_len);
82 i++)
83 new_priomap->priomap[i] = old_priomap->priomap[i];
84
85 new_priomap->priomap_len = new_len;
86
87 rcu_assign_pointer(dev->priomap, new_priomap);
88 if (old_priomap)
89 kfree_rcu(old_priomap, rcu);
90}
91
92static void update_netdev_tables(void)
93{
94 struct net_device *dev;
Neil Hormana87dfe12012-02-10 05:43:36 +000095 u32 max_len = atomic_read(&max_prioidx) + 1;
Neil Horman5bc14212011-11-22 05:10:51 +000096 struct netprio_map *map;
97
98 rtnl_lock();
99 for_each_netdev(&init_net, dev) {
100 map = rtnl_dereference(dev->priomap);
101 if ((!map) ||
102 (map->priomap_len < max_len))
103 extend_netdev_table(dev, max_len);
104 }
105 rtnl_unlock();
106}
107
Li Zefan761b3ef2012-01-31 13:47:36 +0800108static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000109{
110 struct cgroup_netprio_state *cs;
111 int ret;
112
113 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
114 if (!cs)
115 return ERR_PTR(-ENOMEM);
116
117 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) {
118 kfree(cs);
119 return ERR_PTR(-EINVAL);
120 }
121
122 ret = get_prioidx(&cs->prioidx);
123 if (ret != 0) {
124 printk(KERN_WARNING "No space in priority index array\n");
125 kfree(cs);
126 return ERR_PTR(ret);
127 }
128
129 return &cs->css;
130}
131
Li Zefan761b3ef2012-01-31 13:47:36 +0800132static void cgrp_destroy(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000133{
134 struct cgroup_netprio_state *cs;
135 struct net_device *dev;
136 struct netprio_map *map;
137
138 cs = cgrp_netprio_state(cgrp);
139 rtnl_lock();
140 for_each_netdev(&init_net, dev) {
141 map = rtnl_dereference(dev->priomap);
142 if (map)
143 map->priomap[cs->prioidx] = 0;
144 }
145 rtnl_unlock();
146 put_prioidx(cs->prioidx);
147 kfree(cs);
148}
149
150static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
151{
152 return (u64)cgrp_netprio_state(cgrp)->prioidx;
153}
154
155static int read_priomap(struct cgroup *cont, struct cftype *cft,
156 struct cgroup_map_cb *cb)
157{
158 struct net_device *dev;
159 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
160 u32 priority;
161 struct netprio_map *map;
162
163 rcu_read_lock();
164 for_each_netdev_rcu(&init_net, dev) {
165 map = rcu_dereference(dev->priomap);
166 priority = map ? map->priomap[prioidx] : 0;
167 cb->fill(cb, dev->name, priority);
168 }
169 rcu_read_unlock();
170 return 0;
171}
172
173static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
174 const char *buffer)
175{
176 char *devname = kstrdup(buffer, GFP_KERNEL);
177 int ret = -EINVAL;
178 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
179 unsigned long priority;
180 char *priostr;
181 struct net_device *dev;
182 struct netprio_map *map;
183
184 if (!devname)
185 return -ENOMEM;
186
187 /*
188 * Minimally sized valid priomap string
189 */
190 if (strlen(devname) < 3)
191 goto out_free_devname;
192
193 priostr = strstr(devname, " ");
194 if (!priostr)
195 goto out_free_devname;
196
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 /*
205 * If the priostr points to NULL, we're at the end of the passed
206 * in string, and its not a valid write
207 */
208 if (*priostr == '\0')
209 goto out_free_devname;
210
211 ret = kstrtoul(priostr, 10, &priority);
212 if (ret < 0)
213 goto out_free_devname;
214
215 ret = -ENODEV;
216
217 dev = dev_get_by_name(&init_net, devname);
218 if (!dev)
219 goto out_free_devname;
220
221 update_netdev_tables();
222 ret = 0;
223 rcu_read_lock();
224 map = rcu_dereference(dev->priomap);
225 if (map)
226 map->priomap[prioidx] = priority;
227 rcu_read_unlock();
228 dev_put(dev);
229
230out_free_devname:
231 kfree(devname);
232 return ret;
233}
234
235static struct cftype ss_files[] = {
236 {
237 .name = "prioidx",
238 .read_u64 = read_prioidx,
239 },
240 {
241 .name = "ifpriomap",
242 .read_map = read_priomap,
243 .write_string = write_priomap,
244 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700245 { } /* terminate */
Neil Horman5bc14212011-11-22 05:10:51 +0000246};
247
Tejun Heo676f7c82012-04-01 12:09:55 -0700248struct cgroup_subsys net_prio_subsys = {
249 .name = "net_prio",
250 .create = cgrp_create,
251 .destroy = cgrp_destroy,
Tejun Heo676f7c82012-04-01 12:09:55 -0700252#ifdef CONFIG_NETPRIO_CGROUP
253 .subsys_id = net_prio_subsys_id,
254#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -0700255 .base_cftypes = ss_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700256 .module = THIS_MODULE
257};
258
Neil Horman5bc14212011-11-22 05:10:51 +0000259static int netprio_device_event(struct notifier_block *unused,
260 unsigned long event, void *ptr)
261{
262 struct net_device *dev = ptr;
263 struct netprio_map *old;
Neil Horman5bc14212011-11-22 05:10:51 +0000264
265 /*
266 * Note this is called with rtnl_lock held so we have update side
267 * protection on our rcu assignments
268 */
269
270 switch (event) {
Neil Horman5bc14212011-11-22 05:10:51 +0000271 case NETDEV_UNREGISTER:
272 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000273 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000274 if (old)
275 kfree_rcu(old, rcu);
276 break;
277 }
278 return NOTIFY_DONE;
279}
280
281static struct notifier_block netprio_device_notifier = {
282 .notifier_call = netprio_device_event
283};
284
285static int __init init_cgroup_netprio(void)
286{
287 int ret;
288
289 ret = cgroup_load_subsys(&net_prio_subsys);
290 if (ret)
291 goto out;
292#ifndef CONFIG_NETPRIO_CGROUP
293 smp_wmb();
294 net_prio_subsys_id = net_prio_subsys.subsys_id;
295#endif
296
297 register_netdevice_notifier(&netprio_device_notifier);
298
299out:
300 return ret;
301}
302
303static void __exit exit_cgroup_netprio(void)
304{
305 struct netprio_map *old;
306 struct net_device *dev;
307
308 unregister_netdevice_notifier(&netprio_device_notifier);
309
310 cgroup_unload_subsys(&net_prio_subsys);
311
312#ifndef CONFIG_NETPRIO_CGROUP
313 net_prio_subsys_id = -1;
314 synchronize_rcu();
315#endif
316
317 rtnl_lock();
318 for_each_netdev(&init_net, dev) {
319 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000320 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000321 if (old)
322 kfree_rcu(old, rcu);
323 }
324 rtnl_unlock();
325}
326
327module_init(init_cgroup_netprio);
328module_exit(exit_cgroup_netprio);
329MODULE_LICENSE("GPL v2");