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