Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 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 | |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 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 | |
John Fastabend | 406a3c6 | 2012-07-20 10:39:25 +0000 | [diff] [blame] | 28 | #include <linux/fdtable.h> |
| 29 | |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 30 | #define PRIOIDX_SZ 128 |
| 31 | |
| 32 | static unsigned long prioidx_map[PRIOIDX_SZ]; |
| 33 | static DEFINE_SPINLOCK(prioidx_map_lock); |
| 34 | static atomic_t max_prioidx = ATOMIC_INIT(0); |
| 35 | |
| 36 | static 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 | |
| 42 | static 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 Horman | 5962b35 | 2012-02-03 05:18:43 +0000 | [diff] [blame] | 49 | if (prioidx == sizeof(unsigned long) * PRIOIDX_SZ) { |
| 50 | spin_unlock_irqrestore(&prioidx_map_lock, flags); |
| 51 | return -ENOSPC; |
| 52 | } |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 53 | set_bit(prioidx, prioidx_map); |
Gao feng | b761c9b | 2012-07-04 23:28:40 +0000 | [diff] [blame] | 54 | if (atomic_read(&max_prioidx) < prioidx) |
| 55 | atomic_set(&max_prioidx, prioidx); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 56 | spin_unlock_irqrestore(&prioidx_map_lock, flags); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 57 | *prio = prioidx; |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static 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 feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 70 | static int extend_netdev_table(struct net_device *dev, u32 new_len) |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 71 | { |
| 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 Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 81 | pr_warn("Unable to alloc new priomap!\n"); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 82 | return -ENOMEM; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 83 | } |
| 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 feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 95 | return 0; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 98 | static int write_update_netdev_table(struct net_device *dev) |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 99 | { |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 100 | int ret = 0; |
| 101 | u32 max_len; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 102 | struct netprio_map *map; |
| 103 | |
| 104 | rtnl_lock(); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 105 | 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 | |
| 114 | static 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 Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 123 | for_each_netdev(&init_net, dev) { |
| 124 | map = rtnl_dereference(dev->priomap); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 125 | /* |
| 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 Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 135 | } |
| 136 | rtnl_unlock(); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 137 | return ret; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 140 | static struct cgroup_subsys_state *cgrp_create(struct cgroup *cgrp) |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 141 | { |
| 142 | struct cgroup_netprio_state *cs; |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 143 | int ret = -EINVAL; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 144 | |
| 145 | cs = kzalloc(sizeof(*cs), GFP_KERNEL); |
| 146 | if (!cs) |
| 147 | return ERR_PTR(-ENOMEM); |
| 148 | |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 149 | if (cgrp->parent && cgrp_netprio_state(cgrp->parent)->prioidx) |
| 150 | goto out; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 151 | |
| 152 | ret = get_prioidx(&cs->prioidx); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 153 | if (ret < 0) { |
Joe Perches | e005d19 | 2012-05-16 19:58:40 +0000 | [diff] [blame] | 154 | pr_warn("No space in priority index array\n"); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | ret = update_netdev_tables(); |
| 159 | if (ret < 0) { |
| 160 | put_prioidx(cs->prioidx); |
| 161 | goto out; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return &cs->css; |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 165 | out: |
| 166 | kfree(cs); |
| 167 | return ERR_PTR(ret); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Li Zefan | 761b3ef | 2012-01-31 13:47:36 +0800 | [diff] [blame] | 170 | static void cgrp_destroy(struct cgroup *cgrp) |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 171 | { |
| 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 Dumazet | 91c68ce | 2012-07-08 21:45:10 +0000 | [diff] [blame] | 180 | if (map && cs->prioidx < map->priomap_len) |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 181 | map->priomap[cs->prioidx] = 0; |
| 182 | } |
| 183 | rtnl_unlock(); |
| 184 | put_prioidx(cs->prioidx); |
| 185 | kfree(cs); |
| 186 | } |
| 187 | |
| 188 | static u64 read_prioidx(struct cgroup *cgrp, struct cftype *cft) |
| 189 | { |
| 190 | return (u64)cgrp_netprio_state(cgrp)->prioidx; |
| 191 | } |
| 192 | |
| 193 | static 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 Dumazet | 91c68ce | 2012-07-08 21:45:10 +0000 | [diff] [blame] | 204 | priority = (map && prioidx < map->priomap_len) ? map->priomap[prioidx] : 0; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 205 | cb->fill(cb, dev->name, priority); |
| 206 | } |
| 207 | rcu_read_unlock(); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static 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 |
Liu Bo | 0f30732 | 2012-07-07 10:51:56 +0800 | [diff] [blame] | 237 | *and advance the priostr pointer to the priority value |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 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 feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 259 | ret = write_update_netdev_table(dev); |
| 260 | if (ret < 0) |
| 261 | goto out_put_dev; |
| 262 | |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 263 | rcu_read_lock(); |
| 264 | map = rcu_dereference(dev->priomap); |
| 265 | if (map) |
| 266 | map->priomap[prioidx] = priority; |
| 267 | rcu_read_unlock(); |
Gao feng | ef209f1 | 2012-07-11 21:50:15 +0000 | [diff] [blame] | 268 | |
| 269 | out_put_dev: |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 270 | dev_put(dev); |
| 271 | |
| 272 | out_free_devname: |
| 273 | kfree(devname); |
| 274 | return ret; |
| 275 | } |
| 276 | |
John Fastabend | 406a3c6 | 2012-07-20 10:39:25 +0000 | [diff] [blame] | 277 | void 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 Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 327 | static 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 Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 337 | { } /* terminate */ |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 338 | }; |
| 339 | |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 340 | struct cgroup_subsys net_prio_subsys = { |
| 341 | .name = "net_prio", |
| 342 | .create = cgrp_create, |
| 343 | .destroy = cgrp_destroy, |
John Fastabend | 406a3c6 | 2012-07-20 10:39:25 +0000 | [diff] [blame] | 344 | .attach = net_prio_attach, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 345 | #ifdef CONFIG_NETPRIO_CGROUP |
| 346 | .subsys_id = net_prio_subsys_id, |
| 347 | #endif |
Tejun Heo | 4baf6e3 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 348 | .base_cftypes = ss_files, |
Tejun Heo | 676f7c8 | 2012-04-01 12:09:55 -0700 | [diff] [blame] | 349 | .module = THIS_MODULE |
| 350 | }; |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 351 | |
| 352 | static 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 Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 357 | |
| 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 Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 364 | case NETDEV_UNREGISTER: |
| 365 | old = rtnl_dereference(dev->priomap); |
Eric Dumazet | 2cfa5a0 | 2011-11-23 07:09:32 +0000 | [diff] [blame] | 366 | RCU_INIT_POINTER(dev->priomap, NULL); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 367 | if (old) |
| 368 | kfree_rcu(old, rcu); |
| 369 | break; |
| 370 | } |
| 371 | return NOTIFY_DONE; |
| 372 | } |
| 373 | |
| 374 | static struct notifier_block netprio_device_notifier = { |
| 375 | .notifier_call = netprio_device_event |
| 376 | }; |
| 377 | |
| 378 | static 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 | |
| 392 | out: |
| 393 | return ret; |
| 394 | } |
| 395 | |
| 396 | static 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 Dumazet | 2cfa5a0 | 2011-11-23 07:09:32 +0000 | [diff] [blame] | 413 | RCU_INIT_POINTER(dev->priomap, NULL); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 414 | if (old) |
| 415 | kfree_rcu(old, rcu); |
| 416 | } |
| 417 | rtnl_unlock(); |
| 418 | } |
| 419 | |
| 420 | module_init(init_cgroup_netprio); |
| 421 | module_exit(exit_cgroup_netprio); |
| 422 | MODULE_LICENSE("GPL v2"); |