blob: 63d15e8f80e91c1f86461df2918d63cffeab5e8a [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
Joe Perchese005d192012-05-16 19:58:40 +000012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Neil Horman5bc14212011-11-22 05:10:51 +000014#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/types.h>
17#include <linux/string.h>
18#include <linux/errno.h>
19#include <linux/skbuff.h>
20#include <linux/cgroup.h>
21#include <linux/rcupdate.h>
22#include <linux/atomic.h>
23#include <net/rtnetlink.h>
24#include <net/pkt_cls.h>
25#include <net/sock.h>
26#include <net/netprio_cgroup.h>
27
John Fastabend406a3c62012-07-20 10:39:25 +000028#include <linux/fdtable.h>
29
Neil Horman5bc14212011-11-22 05:10:51 +000030#define PRIOIDX_SZ 128
31
32static unsigned long prioidx_map[PRIOIDX_SZ];
33static DEFINE_SPINLOCK(prioidx_map_lock);
34static atomic_t max_prioidx = ATOMIC_INIT(0);
35
36static inline struct cgroup_netprio_state *cgrp_netprio_state(struct cgroup *cgrp)
37{
38 return container_of(cgroup_subsys_state(cgrp, net_prio_subsys_id),
39 struct cgroup_netprio_state, css);
40}
41
42static int get_prioidx(u32 *prio)
43{
44 unsigned long flags;
45 u32 prioidx;
46
47 spin_lock_irqsave(&prioidx_map_lock, flags);
48 prioidx = find_first_zero_bit(prioidx_map, sizeof(unsigned long) * PRIOIDX_SZ);
Neil Horman5962b352012-02-03 05:18:43 +000049 if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) {
50 spin_unlock_irqrestore(&prioidx_map_lock, flags);
51 return -ENOSPC;
52 }
Neil Horman5bc14212011-11-22 05:10:51 +000053 set_bit(prioidx, prioidx_map);
Gao fengb761c9b2012-07-04 23:28:40 +000054 if (atomic_read(&max_prioidx) < prioidx)
55 atomic_set(&max_prioidx, prioidx);
Neil Horman5bc14212011-11-22 05:10:51 +000056 spin_unlock_irqrestore(&prioidx_map_lock, flags);
Neil Horman5bc14212011-11-22 05:10:51 +000057 *prio = prioidx;
58 return 0;
59}
60
61static void put_prioidx(u32 idx)
62{
63 unsigned long flags;
64
65 spin_lock_irqsave(&prioidx_map_lock, flags);
66 clear_bit(idx, prioidx_map);
67 spin_unlock_irqrestore(&prioidx_map_lock, flags);
68}
69
Gao fengef209f12012-07-11 21:50:15 +000070static int extend_netdev_table(struct net_device *dev, u32 new_len)
Neil Horman5bc14212011-11-22 05:10:51 +000071{
72 size_t new_size = sizeof(struct netprio_map) +
73 ((sizeof(u32) * new_len));
74 struct netprio_map *new_priomap = kzalloc(new_size, GFP_KERNEL);
75 struct netprio_map *old_priomap;
76 int i;
77
78 old_priomap = rtnl_dereference(dev->priomap);
79
80 if (!new_priomap) {
Joe Perchese005d192012-05-16 19:58:40 +000081 pr_warn("Unable to alloc new priomap!\n");
Gao fengef209f12012-07-11 21:50:15 +000082 return -ENOMEM;
Neil Horman5bc14212011-11-22 05:10:51 +000083 }
84
85 for (i = 0;
86 old_priomap && (i < old_priomap->priomap_len);
87 i++)
88 new_priomap->priomap[i] = old_priomap->priomap[i];
89
90 new_priomap->priomap_len = new_len;
91
92 rcu_assign_pointer(dev->priomap, new_priomap);
93 if (old_priomap)
94 kfree_rcu(old_priomap, rcu);
Gao fengef209f12012-07-11 21:50:15 +000095 return 0;
Neil Horman5bc14212011-11-22 05:10:51 +000096}
97
Gao fengef209f12012-07-11 21:50:15 +000098static int write_update_netdev_table(struct net_device *dev)
Neil Horman5bc14212011-11-22 05:10:51 +000099{
Gao fengef209f12012-07-11 21:50:15 +0000100 int ret = 0;
101 u32 max_len;
Neil Horman5bc14212011-11-22 05:10:51 +0000102 struct netprio_map *map;
103
104 rtnl_lock();
Gao fengef209f12012-07-11 21:50:15 +0000105 max_len = atomic_read(&max_prioidx) + 1;
106 map = rtnl_dereference(dev->priomap);
107 if (!map || map->priomap_len < max_len)
108 ret = extend_netdev_table(dev, max_len);
109 rtnl_unlock();
110
111 return ret;
112}
113
114static int update_netdev_tables(void)
115{
116 int ret = 0;
117 struct net_device *dev;
118 u32 max_len;
119 struct netprio_map *map;
120
121 rtnl_lock();
122 max_len = atomic_read(&max_prioidx) + 1;
Neil Horman5bc14212011-11-22 05:10:51 +0000123 for_each_netdev(&init_net, dev) {
124 map = rtnl_dereference(dev->priomap);
Gao fengef209f12012-07-11 21:50:15 +0000125 /*
126 * don't allocate priomap if we didn't
127 * change net_prio.ifpriomap (map == NULL),
128 * this will speed up skb_update_prio.
129 */
130 if (map && map->priomap_len < max_len) {
131 ret = extend_netdev_table(dev, max_len);
132 if (ret < 0)
133 break;
134 }
Neil Horman5bc14212011-11-22 05:10:51 +0000135 }
136 rtnl_unlock();
Gao fengef209f12012-07-11 21:50:15 +0000137 return ret;
Neil Horman5bc14212011-11-22 05:10:51 +0000138}
139
Li Zefan761b3ef2012-01-31 13:47:36 +0800140static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000141{
142 struct cgroup_netprio_state *cs;
Gao fengef209f12012-07-11 21:50:15 +0000143 int ret = -EINVAL;
Neil Horman5bc14212011-11-22 05:10:51 +0000144
145 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
146 if (!cs)
147 return ERR_PTR(-ENOMEM);
148
Gao fengef209f12012-07-11 21:50:15 +0000149 if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx)
150 goto out;
Neil Horman5bc14212011-11-22 05:10:51 +0000151
152 ret = get_prioidx(&cs->prioidx);
Gao fengef209f12012-07-11 21:50:15 +0000153 if (ret < 0) {
Joe Perchese005d192012-05-16 19:58:40 +0000154 pr_warn("No space in priority index array\n");
Gao fengef209f12012-07-11 21:50:15 +0000155 goto out;
156 }
157
158 ret = update_netdev_tables();
159 if (ret < 0) {
160 put_prioidx(cs->prioidx);
161 goto out;
Neil Horman5bc14212011-11-22 05:10:51 +0000162 }
163
164 return &cs->css;
Gao fengef209f12012-07-11 21:50:15 +0000165out:
166 kfree(cs);
167 return ERR_PTR(ret);
Neil Horman5bc14212011-11-22 05:10:51 +0000168}
169
Li Zefan761b3ef2012-01-31 13:47:36 +0800170static void cgrp_destroy(struct cgroup *cgrp)
Neil Horman5bc14212011-11-22 05:10:51 +0000171{
172 struct cgroup_netprio_state *cs;
173 struct net_device *dev;
174 struct netprio_map *map;
175
176 cs = cgrp_netprio_state(cgrp);
177 rtnl_lock();
178 for_each_netdev(&init_net, dev) {
179 map = rtnl_dereference(dev->priomap);
Eric Dumazet91c68ce2012-07-08 21:45:10 +0000180 if (map && cs->prioidx < map->priomap_len)
Neil Horman5bc14212011-11-22 05:10:51 +0000181 map->priomap[cs->prioidx] = 0;
182 }
183 rtnl_unlock();
184 put_prioidx(cs->prioidx);
185 kfree(cs);
186}
187
188static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft)
189{
190 return (u64)cgrp_netprio_state(cgrp)->prioidx;
191}
192
193static int read_priomap(struct cgroup *cont, struct cftype *cft,
194 struct cgroup_map_cb *cb)
195{
196 struct net_device *dev;
197 u32 prioidx = cgrp_netprio_state(cont)->prioidx;
198 u32 priority;
199 struct netprio_map *map;
200
201 rcu_read_lock();
202 for_each_netdev_rcu(&init_net, dev) {
203 map = rcu_dereference(dev->priomap);
Eric Dumazet91c68ce2012-07-08 21:45:10 +0000204 priority = (map && prioidx < map->priomap_len) ? map->priomap[prioidx] : 0;
Neil Horman5bc14212011-11-22 05:10:51 +0000205 cb->fill(cb, dev->name, priority);
206 }
207 rcu_read_unlock();
208 return 0;
209}
210
211static int write_priomap(struct cgroup *cgrp, struct cftype *cft,
212 const char *buffer)
213{
214 char *devname = kstrdup(buffer, GFP_KERNEL);
215 int ret = -EINVAL;
216 u32 prioidx = cgrp_netprio_state(cgrp)->prioidx;
217 unsigned long priority;
218 char *priostr;
219 struct net_device *dev;
220 struct netprio_map *map;
221
222 if (!devname)
223 return -ENOMEM;
224
225 /*
226 * Minimally sized valid priomap string
227 */
228 if (strlen(devname) < 3)
229 goto out_free_devname;
230
231 priostr = strstr(devname, " ");
232 if (!priostr)
233 goto out_free_devname;
234
235 /*
236 *Separate the devname from the associated priority
237 *and advance the priostr poitner to the priority value
238 */
239 *priostr = '\0';
240 priostr++;
241
242 /*
243 * If the priostr points to NULL, we're at the end of the passed
244 * in string, and its not a valid write
245 */
246 if (*priostr == '\0')
247 goto out_free_devname;
248
249 ret = kstrtoul(priostr, 10, &priority);
250 if (ret < 0)
251 goto out_free_devname;
252
253 ret = -ENODEV;
254
255 dev = dev_get_by_name(&init_net, devname);
256 if (!dev)
257 goto out_free_devname;
258
Gao fengef209f12012-07-11 21:50:15 +0000259 ret = write_update_netdev_table(dev);
260 if (ret < 0)
261 goto out_put_dev;
262
Neil Horman5bc14212011-11-22 05:10:51 +0000263 rcu_read_lock();
264 map = rcu_dereference(dev->priomap);
265 if (map)
266 map->priomap[prioidx] = priority;
267 rcu_read_unlock();
Gao fengef209f12012-07-11 21:50:15 +0000268
269out_put_dev:
Neil Horman5bc14212011-11-22 05:10:51 +0000270 dev_put(dev);
271
272out_free_devname:
273 kfree(devname);
274 return ret;
275}
276
John Fastabend406a3c62012-07-20 10:39:25 +0000277void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
278{
279 struct task_struct *p;
280 char *tmp = kzalloc(sizeof(char) * PATH_MAX, GFP_KERNEL);
281
282 if (!tmp) {
283 pr_warn("Unable to attach cgrp due to alloc failure!\n");
284 return;
285 }
286
287 cgroup_taskset_for_each(p, cgrp, tset) {
288 unsigned int fd;
289 struct fdtable *fdt;
290 struct files_struct *files;
291
292 task_lock(p);
293 files = p->files;
294 if (!files) {
295 task_unlock(p);
296 continue;
297 }
298
299 rcu_read_lock();
300 fdt = files_fdtable(files);
301 for (fd = 0; fd < fdt->max_fds; fd++) {
302 char *path;
303 struct file *file;
304 struct socket *sock;
305 unsigned long s;
306 int rv, err = 0;
307
308 file = fcheck_files(files, fd);
309 if (!file)
310 continue;
311
312 path = d_path(&file->f_path, tmp, PAGE_SIZE);
313 rv = sscanf(path, "socket:[%lu]", &s);
314 if (rv <= 0)
315 continue;
316
317 sock = sock_from_file(file, &err);
318 if (!err)
319 sock_update_netprioidx(sock->sk, p);
320 }
321 rcu_read_unlock();
322 task_unlock(p);
323 }
324 kfree(tmp);
325}
326
Neil Horman5bc14212011-11-22 05:10:51 +0000327static struct cftype ss_files[] = {
328 {
329 .name = "prioidx",
330 .read_u64 = read_prioidx,
331 },
332 {
333 .name = "ifpriomap",
334 .read_map = read_priomap,
335 .write_string = write_priomap,
336 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700337 { } /* terminate */
Neil Horman5bc14212011-11-22 05:10:51 +0000338};
339
Tejun Heo676f7c82012-04-01 12:09:55 -0700340struct cgroup_subsys net_prio_subsys = {
341 .name = "net_prio",
342 .create = cgrp_create,
343 .destroy = cgrp_destroy,
John Fastabend406a3c62012-07-20 10:39:25 +0000344 .attach = net_prio_attach,
Tejun Heo676f7c82012-04-01 12:09:55 -0700345#ifdef CONFIG_NETPRIO_CGROUP
346 .subsys_id = net_prio_subsys_id,
347#endif
Tejun Heo4baf6e32012-04-01 12:09:55 -0700348 .base_cftypes = ss_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700349 .module = THIS_MODULE
350};
Neil Horman5bc14212011-11-22 05:10:51 +0000351
352static int netprio_device_event(struct notifier_block *unused,
353 unsigned long event, void *ptr)
354{
355 struct net_device *dev = ptr;
356 struct netprio_map *old;
Neil Horman5bc14212011-11-22 05:10:51 +0000357
358 /*
359 * Note this is called with rtnl_lock held so we have update side
360 * protection on our rcu assignments
361 */
362
363 switch (event) {
Neil Horman5bc14212011-11-22 05:10:51 +0000364 case NETDEV_UNREGISTER:
365 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000366 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000367 if (old)
368 kfree_rcu(old, rcu);
369 break;
370 }
371 return NOTIFY_DONE;
372}
373
374static struct notifier_block netprio_device_notifier = {
375 .notifier_call = netprio_device_event
376};
377
378static int __init init_cgroup_netprio(void)
379{
380 int ret;
381
382 ret = cgroup_load_subsys(&net_prio_subsys);
383 if (ret)
384 goto out;
385#ifndef CONFIG_NETPRIO_CGROUP
386 smp_wmb();
387 net_prio_subsys_id = net_prio_subsys.subsys_id;
388#endif
389
390 register_netdevice_notifier(&netprio_device_notifier);
391
392out:
393 return ret;
394}
395
396static void __exit exit_cgroup_netprio(void)
397{
398 struct netprio_map *old;
399 struct net_device *dev;
400
401 unregister_netdevice_notifier(&netprio_device_notifier);
402
403 cgroup_unload_subsys(&net_prio_subsys);
404
405#ifndef CONFIG_NETPRIO_CGROUP
406 net_prio_subsys_id = -1;
407 synchronize_rcu();
408#endif
409
410 rtnl_lock();
411 for_each_netdev(&init_net, dev) {
412 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000413 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000414 if (old)
415 kfree_rcu(old, rcu);
416 }
417 rtnl_unlock();
418}
419
420module_init(init_cgroup_netprio);
421module_exit(exit_cgroup_netprio);
422MODULE_LICENSE("GPL v2");