blob: 1c4810919a0a35900d45a659de0cd780b7e500d3 [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
Andrew Lunnc6e970a2017-03-28 23:45:06 +020014#include <linux/module.h>
Neil Horman5bc14212011-11-22 05:10:51 +000015#include <linux/slab.h>
16#include <linux/types.h>
Russell King43cc2772017-02-07 15:02:55 -080017#include <linux/module.h>
Neil Horman5bc14212011-11-22 05:10:51 +000018#include <linux/string.h>
19#include <linux/errno.h>
20#include <linux/skbuff.h>
21#include <linux/cgroup.h>
22#include <linux/rcupdate.h>
23#include <linux/atomic.h>
Ingo Molnarf719ff92017-02-06 10:57:33 +010024#include <linux/sched/task.h>
25
Neil Horman5bc14212011-11-22 05:10:51 +000026#include <net/rtnetlink.h>
27#include <net/pkt_cls.h>
28#include <net/sock.h>
29#include <net/netprio_cgroup.h>
30
John Fastabend406a3c62012-07-20 10:39:25 +000031#include <linux/fdtable.h>
32
Tejun Heo297dbde2015-12-07 17:38:51 -050033/*
34 * netprio allocates per-net_device priomap array which is indexed by
35 * css->id. Limiting css ID to 16bits doesn't lose anything.
36 */
37#define NETPRIO_ID_MAX USHRT_MAX
38
Tejun Heo4a6ee252012-11-22 07:32:46 -080039#define PRIOMAP_MIN_SZ 128
Neil Horman5bc14212011-11-22 05:10:51 +000040
Tejun Heo4a6ee252012-11-22 07:32:46 -080041/*
stephen hemminger8e3bff92013-12-08 12:15:44 -080042 * Extend @dev->priomap so that it's large enough to accommodate
Tejun Heo4a6ee252012-11-22 07:32:46 -080043 * @target_idx. @dev->priomap.priomap_len > @target_idx after successful
44 * return. Must be called under rtnl lock.
45 */
46static int extend_netdev_table(struct net_device *dev, u32 target_idx)
Neil Horman5bc14212011-11-22 05:10:51 +000047{
Tejun Heo4a6ee252012-11-22 07:32:46 -080048 struct netprio_map *old, *new;
49 size_t new_sz, new_len;
Neil Horman5bc14212011-11-22 05:10:51 +000050
Tejun Heo4a6ee252012-11-22 07:32:46 -080051 /* is the existing priomap large enough? */
Tejun Heo52bca932012-11-22 07:32:46 -080052 old = rtnl_dereference(dev->priomap);
Tejun Heo4a6ee252012-11-22 07:32:46 -080053 if (old && old->priomap_len > target_idx)
54 return 0;
Neil Horman5bc14212011-11-22 05:10:51 +000055
Tejun Heo4a6ee252012-11-22 07:32:46 -080056 /*
57 * Determine the new size. Let's keep it power-of-two. We start
58 * from PRIOMAP_MIN_SZ and double it until it's large enough to
59 * accommodate @target_idx.
60 */
61 new_sz = PRIOMAP_MIN_SZ;
62 while (true) {
63 new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
64 sizeof(new->priomap[0]);
65 if (new_len > target_idx)
66 break;
67 new_sz *= 2;
68 /* overflowed? */
69 if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
70 return -ENOSPC;
71 }
72
73 /* allocate & copy */
74 new = kzalloc(new_sz, GFP_KERNEL);
Joe Perches62b59422013-02-04 16:48:16 +000075 if (!new)
Gao fengef209f12012-07-11 21:50:15 +000076 return -ENOMEM;
Neil Horman5bc14212011-11-22 05:10:51 +000077
Tejun Heo52bca932012-11-22 07:32:46 -080078 if (old)
79 memcpy(new->priomap, old->priomap,
80 old->priomap_len * sizeof(old->priomap[0]));
Neil Horman5bc14212011-11-22 05:10:51 +000081
Tejun Heo52bca932012-11-22 07:32:46 -080082 new->priomap_len = new_len;
Neil Horman5bc14212011-11-22 05:10:51 +000083
Tejun Heo4a6ee252012-11-22 07:32:46 -080084 /* install the new priomap */
Tejun Heo52bca932012-11-22 07:32:46 -080085 rcu_assign_pointer(dev->priomap, new);
86 if (old)
87 kfree_rcu(old, rcu);
Gao fengef209f12012-07-11 21:50:15 +000088 return 0;
Neil Horman5bc14212011-11-22 05:10:51 +000089}
90
Tejun Heo666b0eb2012-11-22 07:32:47 -080091/**
92 * netprio_prio - return the effective netprio of a cgroup-net_device pair
Tejun Heo6d37b972013-08-08 20:11:22 -040093 * @css: css part of the target pair
Tejun Heo666b0eb2012-11-22 07:32:47 -080094 * @dev: net_device part of the target pair
95 *
96 * Should be called under RCU read or rtnl lock.
97 */
Tejun Heo6d37b972013-08-08 20:11:22 -040098static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
Tejun Heo666b0eb2012-11-22 07:32:47 -080099{
100 struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
Tejun Heo6d37b972013-08-08 20:11:22 -0400101 int id = css->cgroup->id;
Tejun Heo666b0eb2012-11-22 07:32:47 -0800102
Tejun Heo6d37b972013-08-08 20:11:22 -0400103 if (map && id < map->priomap_len)
104 return map->priomap[id];
Tejun Heo666b0eb2012-11-22 07:32:47 -0800105 return 0;
106}
107
108/**
109 * netprio_set_prio - set netprio on a cgroup-net_device pair
Tejun Heo6d37b972013-08-08 20:11:22 -0400110 * @css: css part of the target pair
Tejun Heo666b0eb2012-11-22 07:32:47 -0800111 * @dev: net_device part of the target pair
112 * @prio: prio to set
113 *
Tejun Heo6d37b972013-08-08 20:11:22 -0400114 * Set netprio to @prio on @css-@dev pair. Should be called under rtnl
Tejun Heo666b0eb2012-11-22 07:32:47 -0800115 * lock and may fail under memory pressure for non-zero @prio.
116 */
Tejun Heo6d37b972013-08-08 20:11:22 -0400117static int netprio_set_prio(struct cgroup_subsys_state *css,
118 struct net_device *dev, u32 prio)
Tejun Heo666b0eb2012-11-22 07:32:47 -0800119{
120 struct netprio_map *map;
Tejun Heo6d37b972013-08-08 20:11:22 -0400121 int id = css->cgroup->id;
Tejun Heo666b0eb2012-11-22 07:32:47 -0800122 int ret;
123
124 /* avoid extending priomap for zero writes */
125 map = rtnl_dereference(dev->priomap);
Tejun Heo6d37b972013-08-08 20:11:22 -0400126 if (!prio && (!map || map->priomap_len <= id))
Tejun Heo666b0eb2012-11-22 07:32:47 -0800127 return 0;
128
Tejun Heo6d37b972013-08-08 20:11:22 -0400129 ret = extend_netdev_table(dev, id);
Tejun Heo666b0eb2012-11-22 07:32:47 -0800130 if (ret)
131 return ret;
132
133 map = rtnl_dereference(dev->priomap);
Tejun Heo6d37b972013-08-08 20:11:22 -0400134 map->priomap[id] = prio;
Tejun Heo666b0eb2012-11-22 07:32:47 -0800135 return 0;
136}
137
Tejun Heoeb954192013-08-08 20:11:23 -0400138static struct cgroup_subsys_state *
139cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
Neil Horman5bc14212011-11-22 05:10:51 +0000140{
Tejun Heo6d37b972013-08-08 20:11:22 -0400141 struct cgroup_subsys_state *css;
Tejun Heo88d642f2012-11-22 07:32:47 -0800142
Tejun Heo6d37b972013-08-08 20:11:22 -0400143 css = kzalloc(sizeof(*css), GFP_KERNEL);
144 if (!css)
Neil Horman5bc14212011-11-22 05:10:51 +0000145 return ERR_PTR(-ENOMEM);
146
Tejun Heo6d37b972013-08-08 20:11:22 -0400147 return css;
Neil Horman5bc14212011-11-22 05:10:51 +0000148}
149
Tejun Heoeb954192013-08-08 20:11:23 -0400150static int cgrp_css_online(struct cgroup_subsys_state *css)
Neil Horman5bc14212011-11-22 05:10:51 +0000151{
Tejun Heo5c9d5352014-05-16 13:22:48 -0400152 struct cgroup_subsys_state *parent_css = css->parent;
Neil Horman5bc14212011-11-22 05:10:51 +0000153 struct net_device *dev;
Tejun Heo811d8d62012-11-22 07:32:47 -0800154 int ret = 0;
155
Tejun Heo297dbde2015-12-07 17:38:51 -0500156 if (css->id > NETPRIO_ID_MAX)
157 return -ENOSPC;
158
Tejun Heoeb954192013-08-08 20:11:23 -0400159 if (!parent_css)
Tejun Heo811d8d62012-11-22 07:32:47 -0800160 return 0;
Neil Horman5bc14212011-11-22 05:10:51 +0000161
Neil Horman5bc14212011-11-22 05:10:51 +0000162 rtnl_lock();
Tejun Heo811d8d62012-11-22 07:32:47 -0800163 /*
164 * Inherit prios from the parent. As all prios are set during
165 * onlining, there is no need to clear them on offline.
166 */
167 for_each_netdev(&init_net, dev) {
Tejun Heo6d37b972013-08-08 20:11:22 -0400168 u32 prio = netprio_prio(parent_css, dev);
Tejun Heo811d8d62012-11-22 07:32:47 -0800169
Tejun Heo6d37b972013-08-08 20:11:22 -0400170 ret = netprio_set_prio(css, dev, prio);
Tejun Heo811d8d62012-11-22 07:32:47 -0800171 if (ret)
172 break;
173 }
Neil Horman5bc14212011-11-22 05:10:51 +0000174 rtnl_unlock();
Tejun Heo811d8d62012-11-22 07:32:47 -0800175 return ret;
176}
177
Tejun Heoeb954192013-08-08 20:11:23 -0400178static void cgrp_css_free(struct cgroup_subsys_state *css)
Tejun Heo811d8d62012-11-22 07:32:47 -0800179{
Tejun Heoeb954192013-08-08 20:11:23 -0400180 kfree(css);
Neil Horman5bc14212011-11-22 05:10:51 +0000181}
182
Tejun Heo182446d2013-08-08 20:11:24 -0400183static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
Neil Horman5bc14212011-11-22 05:10:51 +0000184{
Tejun Heo182446d2013-08-08 20:11:24 -0400185 return css->cgroup->id;
Neil Horman5bc14212011-11-22 05:10:51 +0000186}
187
Tejun Heo2da8ca82013-12-05 12:28:04 -0500188static int read_priomap(struct seq_file *sf, void *v)
Neil Horman5bc14212011-11-22 05:10:51 +0000189{
190 struct net_device *dev;
Neil Horman5bc14212011-11-22 05:10:51 +0000191
192 rcu_read_lock();
Tejun Heo666b0eb2012-11-22 07:32:47 -0800193 for_each_netdev_rcu(&init_net, dev)
Tejun Heo2da8ca82013-12-05 12:28:04 -0500194 seq_printf(sf, "%s %u\n", dev->name,
195 netprio_prio(seq_css(sf), dev));
Neil Horman5bc14212011-11-22 05:10:51 +0000196 rcu_read_unlock();
197 return 0;
198}
199
Tejun Heo451af502014-05-13 12:16:21 -0400200static ssize_t write_priomap(struct kernfs_open_file *of,
201 char *buf, size_t nbytes, loff_t off)
Neil Horman5bc14212011-11-22 05:10:51 +0000202{
Tejun Heo6d5759d2012-11-22 07:32:46 -0800203 char devname[IFNAMSIZ + 1];
Neil Horman5bc14212011-11-22 05:10:51 +0000204 struct net_device *dev;
Tejun Heo6d5759d2012-11-22 07:32:46 -0800205 u32 prio;
206 int ret;
Neil Horman5bc14212011-11-22 05:10:51 +0000207
Tejun Heo451af502014-05-13 12:16:21 -0400208 if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
Tejun Heo6d5759d2012-11-22 07:32:46 -0800209 return -EINVAL;
Neil Horman5bc14212011-11-22 05:10:51 +0000210
211 dev = dev_get_by_name(&init_net, devname);
212 if (!dev)
Tejun Heo6d5759d2012-11-22 07:32:46 -0800213 return -ENODEV;
Neil Horman5bc14212011-11-22 05:10:51 +0000214
Tejun Heobd1060a2015-12-07 17:38:53 -0500215 cgroup_sk_alloc_disable();
216
John Fastabend476ad152012-08-14 12:34:35 +0000217 rtnl_lock();
Tejun Heo6d5759d2012-11-22 07:32:46 -0800218
Tejun Heo451af502014-05-13 12:16:21 -0400219 ret = netprio_set_prio(of_css(of), dev, prio);
Gao fengef209f12012-07-11 21:50:15 +0000220
John Fastabend476ad152012-08-14 12:34:35 +0000221 rtnl_unlock();
Neil Horman5bc14212011-11-22 05:10:51 +0000222 dev_put(dev);
Tejun Heo451af502014-05-13 12:16:21 -0400223 return ret ?: nbytes;
Neil Horman5bc14212011-11-22 05:10:51 +0000224}
225
Al Viroc3c073f2012-08-21 22:32:06 -0400226static int update_netprio(const void *v, struct file *file, unsigned n)
227{
228 int err;
229 struct socket *sock = sock_from_file(file, &err);
Tejun Heobd1060a2015-12-07 17:38:53 -0500230 if (sock) {
231 spin_lock(&cgroup_sk_update_lock);
Tejun Heo2a56a1f2015-12-07 17:38:52 -0500232 sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
233 (unsigned long)v);
Tejun Heobd1060a2015-12-07 17:38:53 -0500234 spin_unlock(&cgroup_sk_update_lock);
235 }
Al Viroc3c073f2012-08-21 22:32:06 -0400236 return 0;
237}
238
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500239static void net_prio_attach(struct cgroup_taskset *tset)
John Fastabend406a3c62012-07-20 10:39:25 +0000240{
241 struct task_struct *p;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500242 struct cgroup_subsys_state *css;
John Fastabend406a3c62012-07-20 10:39:25 +0000243
Tejun Heo1f7dd3e52015-12-03 10:18:21 -0500244 cgroup_taskset_for_each(p, css, tset) {
245 void *v = (void *)(unsigned long)css->cgroup->id;
246
John Fastabend406a3c62012-07-20 10:39:25 +0000247 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -0400248 iterate_fd(p->files, 0, update_netprio, v);
John Fastabend406a3c62012-07-20 10:39:25 +0000249 task_unlock(p);
250 }
John Fastabend406a3c62012-07-20 10:39:25 +0000251}
252
Neil Horman5bc14212011-11-22 05:10:51 +0000253static struct cftype ss_files[] = {
254 {
255 .name = "prioidx",
256 .read_u64 = read_prioidx,
257 },
258 {
259 .name = "ifpriomap",
Tejun Heo2da8ca82013-12-05 12:28:04 -0500260 .seq_show = read_priomap,
Tejun Heo451af502014-05-13 12:16:21 -0400261 .write = write_priomap,
Neil Horman5bc14212011-11-22 05:10:51 +0000262 },
Tejun Heo4baf6e32012-04-01 12:09:55 -0700263 { } /* terminate */
Neil Horman5bc14212011-11-22 05:10:51 +0000264};
265
Tejun Heo073219e2014-02-08 10:36:58 -0500266struct cgroup_subsys net_prio_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -0800267 .css_alloc = cgrp_css_alloc,
Tejun Heo811d8d62012-11-22 07:32:47 -0800268 .css_online = cgrp_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -0800269 .css_free = cgrp_css_free,
John Fastabend406a3c62012-07-20 10:39:25 +0000270 .attach = net_prio_attach,
Tejun Heo55779642014-07-15 11:05:09 -0400271 .legacy_cftypes = ss_files,
Tejun Heo676f7c82012-04-01 12:09:55 -0700272};
Neil Horman5bc14212011-11-22 05:10:51 +0000273
274static int netprio_device_event(struct notifier_block *unused,
275 unsigned long event, void *ptr)
276{
Jiri Pirko351638e2013-05-28 01:30:21 +0000277 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Neil Horman5bc14212011-11-22 05:10:51 +0000278 struct netprio_map *old;
Neil Horman5bc14212011-11-22 05:10:51 +0000279
280 /*
281 * Note this is called with rtnl_lock held so we have update side
282 * protection on our rcu assignments
283 */
284
285 switch (event) {
Neil Horman5bc14212011-11-22 05:10:51 +0000286 case NETDEV_UNREGISTER:
287 old = rtnl_dereference(dev->priomap);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000288 RCU_INIT_POINTER(dev->priomap, NULL);
Neil Horman5bc14212011-11-22 05:10:51 +0000289 if (old)
290 kfree_rcu(old, rcu);
291 break;
292 }
293 return NOTIFY_DONE;
294}
295
296static struct notifier_block netprio_device_notifier = {
297 .notifier_call = netprio_device_event
298};
299
300static int __init init_cgroup_netprio(void)
301{
Neil Horman5bc14212011-11-22 05:10:51 +0000302 register_netdevice_notifier(&netprio_device_notifier);
Tejun Heoaf636332014-02-08 10:36:58 -0500303 return 0;
Neil Horman5bc14212011-11-22 05:10:51 +0000304}
305
Tejun Heoaf636332014-02-08 10:36:58 -0500306subsys_initcall(init_cgroup_netprio);
Neil Horman5bc14212011-11-22 05:10:51 +0000307MODULE_LICENSE("GPL v2");