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 | ||
e005d193 JP |
12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
13 | ||
5bc1421e NH |
14 | #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 | ||
406a3c63 JF |
28 | #include <linux/fdtable.h> |
29 | ||
4a6ee25c | 30 | #define PRIOMAP_MIN_SZ 128 |
5bc1421e NH |
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 | ||
4a6ee25c TH |
38 | /* |
39 | * Extend @dev->priomap so that it's large enough to accomodate | |
40 | * @target_idx. @dev->priomap.priomap_len > @target_idx after successful | |
41 | * return. Must be called under rtnl lock. | |
42 | */ | |
43 | static int extend_netdev_table(struct net_device *dev, u32 target_idx) | |
5bc1421e | 44 | { |
4a6ee25c TH |
45 | struct netprio_map *old, *new; |
46 | size_t new_sz, new_len; | |
5bc1421e | 47 | |
4a6ee25c | 48 | /* is the existing priomap large enough? */ |
52bca930 | 49 | old = rtnl_dereference(dev->priomap); |
4a6ee25c TH |
50 | if (old && old->priomap_len > target_idx) |
51 | return 0; | |
52 | ||
53 | /* | |
54 | * Determine the new size. Let's keep it power-of-two. We start | |
55 | * from PRIOMAP_MIN_SZ and double it until it's large enough to | |
56 | * accommodate @target_idx. | |
57 | */ | |
58 | new_sz = PRIOMAP_MIN_SZ; | |
59 | while (true) { | |
60 | new_len = (new_sz - offsetof(struct netprio_map, priomap)) / | |
61 | sizeof(new->priomap[0]); | |
62 | if (new_len > target_idx) | |
63 | break; | |
64 | new_sz *= 2; | |
65 | /* overflowed? */ | |
66 | if (WARN_ON(new_sz < PRIOMAP_MIN_SZ)) | |
67 | return -ENOSPC; | |
68 | } | |
5bc1421e | 69 | |
4a6ee25c TH |
70 | /* allocate & copy */ |
71 | new = kzalloc(new_sz, GFP_KERNEL); | |
52bca930 | 72 | if (!new) { |
e005d193 | 73 | pr_warn("Unable to alloc new priomap!\n"); |
ef209f15 | 74 | return -ENOMEM; |
5bc1421e NH |
75 | } |
76 | ||
52bca930 TH |
77 | if (old) |
78 | memcpy(new->priomap, old->priomap, | |
79 | old->priomap_len * sizeof(old->priomap[0])); | |
5bc1421e | 80 | |
52bca930 | 81 | new->priomap_len = new_len; |
5bc1421e | 82 | |
4a6ee25c | 83 | /* install the new priomap */ |
52bca930 TH |
84 | rcu_assign_pointer(dev->priomap, new); |
85 | if (old) | |
86 | kfree_rcu(old, rcu); | |
ef209f15 G |
87 | return 0; |
88 | } | |
89 | ||
92fb9748 | 90 | static struct cgroup_subsys_state *cgrp_css_alloc(struct cgroup *cgrp) |
5bc1421e NH |
91 | { |
92 | struct cgroup_netprio_state *cs; | |
88d642fa TH |
93 | |
94 | if (cgrp->parent && cgrp->parent->id) | |
95 | return ERR_PTR(-EINVAL); | |
5bc1421e NH |
96 | |
97 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); | |
98 | if (!cs) | |
99 | return ERR_PTR(-ENOMEM); | |
100 | ||
5bc1421e NH |
101 | return &cs->css; |
102 | } | |
103 | ||
92fb9748 | 104 | static void cgrp_css_free(struct cgroup *cgrp) |
5bc1421e | 105 | { |
88d642fa | 106 | struct cgroup_netprio_state *cs = cgrp_netprio_state(cgrp); |
5bc1421e NH |
107 | struct net_device *dev; |
108 | struct netprio_map *map; | |
109 | ||
5bc1421e NH |
110 | rtnl_lock(); |
111 | for_each_netdev(&init_net, dev) { | |
112 | map = rtnl_dereference(dev->priomap); | |
88d642fa TH |
113 | if (map && cgrp->id < map->priomap_len) |
114 | map->priomap[cgrp->id] = 0; | |
5bc1421e NH |
115 | } |
116 | rtnl_unlock(); | |
5bc1421e NH |
117 | kfree(cs); |
118 | } | |
119 | ||
120 | static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft) | |
121 | { | |
88d642fa | 122 | return cgrp->id; |
5bc1421e NH |
123 | } |
124 | ||
125 | static int read_priomap(struct cgroup *cont, struct cftype *cft, | |
126 | struct cgroup_map_cb *cb) | |
127 | { | |
128 | struct net_device *dev; | |
88d642fa | 129 | u32 id = cont->id; |
5bc1421e NH |
130 | u32 priority; |
131 | struct netprio_map *map; | |
132 | ||
133 | rcu_read_lock(); | |
134 | for_each_netdev_rcu(&init_net, dev) { | |
135 | map = rcu_dereference(dev->priomap); | |
88d642fa | 136 | priority = (map && id < map->priomap_len) ? map->priomap[id] : 0; |
5bc1421e NH |
137 | cb->fill(cb, dev->name, priority); |
138 | } | |
139 | rcu_read_unlock(); | |
140 | return 0; | |
141 | } | |
142 | ||
143 | static int write_priomap(struct cgroup *cgrp, struct cftype *cft, | |
144 | const char *buffer) | |
145 | { | |
6d5759dd | 146 | char devname[IFNAMSIZ + 1]; |
5bc1421e NH |
147 | struct net_device *dev; |
148 | struct netprio_map *map; | |
6d5759dd TH |
149 | u32 prio; |
150 | int ret; | |
5bc1421e | 151 | |
6d5759dd TH |
152 | if (sscanf(buffer, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2) |
153 | return -EINVAL; | |
5bc1421e NH |
154 | |
155 | dev = dev_get_by_name(&init_net, devname); | |
156 | if (!dev) | |
6d5759dd | 157 | return -ENODEV; |
5bc1421e | 158 | |
476ad154 | 159 | rtnl_lock(); |
6d5759dd | 160 | |
88d642fa | 161 | ret = extend_netdev_table(dev, cgrp->id); |
6d5759dd TH |
162 | if (ret) |
163 | goto out_unlock; | |
ef209f15 | 164 | |
476ad154 | 165 | map = rtnl_dereference(dev->priomap); |
5bc1421e | 166 | if (map) |
88d642fa | 167 | map->priomap[cgrp->id] = prio; |
6d5759dd | 168 | out_unlock: |
476ad154 | 169 | rtnl_unlock(); |
5bc1421e | 170 | dev_put(dev); |
5bc1421e NH |
171 | return ret; |
172 | } | |
173 | ||
c3c073f8 AV |
174 | static int update_netprio(const void *v, struct file *file, unsigned n) |
175 | { | |
176 | int err; | |
177 | struct socket *sock = sock_from_file(file, &err); | |
178 | if (sock) | |
179 | sock->sk->sk_cgrp_prioidx = (u32)(unsigned long)v; | |
180 | return 0; | |
181 | } | |
182 | ||
406a3c63 JF |
183 | void net_prio_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) |
184 | { | |
185 | struct task_struct *p; | |
c3c073f8 | 186 | void *v; |
406a3c63 JF |
187 | |
188 | cgroup_taskset_for_each(p, cgrp, tset) { | |
406a3c63 | 189 | task_lock(p); |
c3c073f8 AV |
190 | v = (void *)(unsigned long)task_netprioidx(p); |
191 | iterate_fd(p->files, 0, update_netprio, v); | |
406a3c63 JF |
192 | task_unlock(p); |
193 | } | |
406a3c63 JF |
194 | } |
195 | ||
5bc1421e NH |
196 | static struct cftype ss_files[] = { |
197 | { | |
198 | .name = "prioidx", | |
199 | .read_u64 = read_prioidx, | |
200 | }, | |
201 | { | |
202 | .name = "ifpriomap", | |
203 | .read_map = read_priomap, | |
204 | .write_string = write_priomap, | |
205 | }, | |
4baf6e33 | 206 | { } /* terminate */ |
5bc1421e NH |
207 | }; |
208 | ||
676f7c8f TH |
209 | struct cgroup_subsys net_prio_subsys = { |
210 | .name = "net_prio", | |
92fb9748 TH |
211 | .css_alloc = cgrp_css_alloc, |
212 | .css_free = cgrp_css_free, | |
406a3c63 | 213 | .attach = net_prio_attach, |
676f7c8f | 214 | .subsys_id = net_prio_subsys_id, |
4baf6e33 | 215 | .base_cftypes = ss_files, |
8c7f6edb TH |
216 | .module = THIS_MODULE, |
217 | ||
218 | /* | |
219 | * net_prio has artificial limit on the number of cgroups and | |
220 | * disallows nesting making it impossible to co-mount it with other | |
221 | * hierarchical subsystems. Remove the artificially low PRIOIDX_SZ | |
222 | * limit and properly nest configuration such that children follow | |
223 | * their parents' configurations by default and are allowed to | |
224 | * override and remove the following. | |
225 | */ | |
226 | .broken_hierarchy = true, | |
676f7c8f | 227 | }; |
5bc1421e NH |
228 | |
229 | static int netprio_device_event(struct notifier_block *unused, | |
230 | unsigned long event, void *ptr) | |
231 | { | |
232 | struct net_device *dev = ptr; | |
233 | struct netprio_map *old; | |
5bc1421e NH |
234 | |
235 | /* | |
236 | * Note this is called with rtnl_lock held so we have update side | |
237 | * protection on our rcu assignments | |
238 | */ | |
239 | ||
240 | switch (event) { | |
5bc1421e NH |
241 | case NETDEV_UNREGISTER: |
242 | old = rtnl_dereference(dev->priomap); | |
2cfa5a04 | 243 | RCU_INIT_POINTER(dev->priomap, NULL); |
5bc1421e NH |
244 | if (old) |
245 | kfree_rcu(old, rcu); | |
246 | break; | |
247 | } | |
248 | return NOTIFY_DONE; | |
249 | } | |
250 | ||
251 | static struct notifier_block netprio_device_notifier = { | |
252 | .notifier_call = netprio_device_event | |
253 | }; | |
254 | ||
255 | static int __init init_cgroup_netprio(void) | |
256 | { | |
257 | int ret; | |
258 | ||
259 | ret = cgroup_load_subsys(&net_prio_subsys); | |
260 | if (ret) | |
261 | goto out; | |
5bc1421e NH |
262 | |
263 | register_netdevice_notifier(&netprio_device_notifier); | |
264 | ||
265 | out: | |
266 | return ret; | |
267 | } | |
268 | ||
269 | static void __exit exit_cgroup_netprio(void) | |
270 | { | |
271 | struct netprio_map *old; | |
272 | struct net_device *dev; | |
273 | ||
274 | unregister_netdevice_notifier(&netprio_device_notifier); | |
275 | ||
276 | cgroup_unload_subsys(&net_prio_subsys); | |
277 | ||
5bc1421e NH |
278 | rtnl_lock(); |
279 | for_each_netdev(&init_net, dev) { | |
280 | old = rtnl_dereference(dev->priomap); | |
2cfa5a04 | 281 | RCU_INIT_POINTER(dev->priomap, NULL); |
5bc1421e NH |
282 | if (old) |
283 | kfree_rcu(old, rcu); | |
284 | } | |
285 | rtnl_unlock(); | |
286 | } | |
287 | ||
288 | module_init(init_cgroup_netprio); | |
289 | module_exit(exit_cgroup_netprio); | |
290 | MODULE_LICENSE("GPL v2"); |