]>
Commit | Line | Data |
---|---|---|
5bc1421e NH |
1 | /* |
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 | ||
5bc1421e NH |
26 | #define PRIOIDX_SZ 128 |
27 | ||
28 | static unsigned long prioidx_map[PRIOIDX_SZ]; | |
29 | static DEFINE_SPINLOCK(prioidx_map_lock); | |
30 | static atomic_t max_prioidx = ATOMIC_INIT(0); | |
31 | ||
32 | static 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 | ||
38 | static 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); | |
5962b35c NH |
45 | if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) { |
46 | spin_unlock_irqrestore(&prioidx_map_lock, flags); | |
47 | return -ENOSPC; | |
48 | } | |
5bc1421e NH |
49 | set_bit(prioidx, prioidx_map); |
50 | spin_unlock_irqrestore(&prioidx_map_lock, flags); | |
5bc1421e NH |
51 | atomic_set(&max_prioidx, prioidx); |
52 | *prio = prioidx; | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static 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 | ||
65 | static 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 | ||
92 | static void update_netdev_tables(void) | |
93 | { | |
94 | struct net_device *dev; | |
a87dfe14 | 95 | u32 max_len = atomic_read(&max_prioidx) + 1; |
5bc1421e NH |
96 | 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 | ||
761b3ef5 | 108 | static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp) |
5bc1421e NH |
109 | { |
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 | ||
761b3ef5 | 132 | static void cgrp_destroy(struct cgroup *cgrp) |
5bc1421e NH |
133 | { |
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 | ||
150 | static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft) | |
151 | { | |
152 | return (u64)cgrp_netprio_state(cgrp)->prioidx; | |
153 | } | |
154 | ||
155 | static 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 | ||
173 | static 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 | ||
230 | out_free_devname: | |
231 | kfree(devname); | |
232 | return ret; | |
233 | } | |
234 | ||
235 | static 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 | }, | |
245 | }; | |
246 | ||
247 | static int cgrp_populate(struct cgroup_subsys *ss, struct cgroup *cgrp) | |
248 | { | |
249 | return cgroup_add_files(cgrp, ss, ss_files, ARRAY_SIZE(ss_files)); | |
250 | } | |
251 | ||
676f7c8f TH |
252 | struct cgroup_subsys net_prio_subsys = { |
253 | .name = "net_prio", | |
254 | .create = cgrp_create, | |
255 | .destroy = cgrp_destroy, | |
256 | .populate = cgrp_populate, | |
257 | #ifdef CONFIG_NETPRIO_CGROUP | |
258 | .subsys_id = net_prio_subsys_id, | |
259 | #endif | |
260 | .module = THIS_MODULE | |
261 | }; | |
262 | ||
5bc1421e NH |
263 | static int netprio_device_event(struct notifier_block *unused, |
264 | unsigned long event, void *ptr) | |
265 | { | |
266 | struct net_device *dev = ptr; | |
267 | struct netprio_map *old; | |
5bc1421e NH |
268 | |
269 | /* | |
270 | * Note this is called with rtnl_lock held so we have update side | |
271 | * protection on our rcu assignments | |
272 | */ | |
273 | ||
274 | switch (event) { | |
5bc1421e NH |
275 | case NETDEV_UNREGISTER: |
276 | old = rtnl_dereference(dev->priomap); | |
2cfa5a04 | 277 | RCU_INIT_POINTER(dev->priomap, NULL); |
5bc1421e NH |
278 | if (old) |
279 | kfree_rcu(old, rcu); | |
280 | break; | |
281 | } | |
282 | return NOTIFY_DONE; | |
283 | } | |
284 | ||
285 | static struct notifier_block netprio_device_notifier = { | |
286 | .notifier_call = netprio_device_event | |
287 | }; | |
288 | ||
289 | static int __init init_cgroup_netprio(void) | |
290 | { | |
291 | int ret; | |
292 | ||
293 | ret = cgroup_load_subsys(&net_prio_subsys); | |
294 | if (ret) | |
295 | goto out; | |
296 | #ifndef CONFIG_NETPRIO_CGROUP | |
297 | smp_wmb(); | |
298 | net_prio_subsys_id = net_prio_subsys.subsys_id; | |
299 | #endif | |
300 | ||
301 | register_netdevice_notifier(&netprio_device_notifier); | |
302 | ||
303 | out: | |
304 | return ret; | |
305 | } | |
306 | ||
307 | static void __exit exit_cgroup_netprio(void) | |
308 | { | |
309 | struct netprio_map *old; | |
310 | struct net_device *dev; | |
311 | ||
312 | unregister_netdevice_notifier(&netprio_device_notifier); | |
313 | ||
314 | cgroup_unload_subsys(&net_prio_subsys); | |
315 | ||
316 | #ifndef CONFIG_NETPRIO_CGROUP | |
317 | net_prio_subsys_id = -1; | |
318 | synchronize_rcu(); | |
319 | #endif | |
320 | ||
321 | rtnl_lock(); | |
322 | for_each_netdev(&init_net, dev) { | |
323 | old = rtnl_dereference(dev->priomap); | |
2cfa5a04 | 324 | RCU_INIT_POINTER(dev->priomap, NULL); |
5bc1421e NH |
325 | if (old) |
326 | kfree_rcu(old, rcu); | |
327 | } | |
328 | rtnl_unlock(); | |
329 | } | |
330 | ||
331 | module_init(init_cgroup_netprio); | |
332 | module_exit(exit_cgroup_netprio); | |
333 | MODULE_LICENSE("GPL v2"); |