0001
0002
0003
0004
0005
0006
0007
0008 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0009
0010 #include <linux/module.h>
0011 #include <linux/slab.h>
0012 #include <linux/types.h>
0013 #include <linux/string.h>
0014 #include <linux/errno.h>
0015 #include <linux/skbuff.h>
0016 #include <linux/cgroup.h>
0017 #include <linux/rcupdate.h>
0018 #include <linux/atomic.h>
0019 #include <linux/sched/task.h>
0020
0021 #include <net/rtnetlink.h>
0022 #include <net/pkt_cls.h>
0023 #include <net/sock.h>
0024 #include <net/netprio_cgroup.h>
0025
0026 #include <linux/fdtable.h>
0027
0028
0029
0030
0031
0032 #define NETPRIO_ID_MAX USHRT_MAX
0033
0034 #define PRIOMAP_MIN_SZ 128
0035
0036
0037
0038
0039
0040
0041 static int extend_netdev_table(struct net_device *dev, u32 target_idx)
0042 {
0043 struct netprio_map *old, *new;
0044 size_t new_sz, new_len;
0045
0046
0047 old = rtnl_dereference(dev->priomap);
0048 if (old && old->priomap_len > target_idx)
0049 return 0;
0050
0051
0052
0053
0054
0055
0056 new_sz = PRIOMAP_MIN_SZ;
0057 while (true) {
0058 new_len = (new_sz - offsetof(struct netprio_map, priomap)) /
0059 sizeof(new->priomap[0]);
0060 if (new_len > target_idx)
0061 break;
0062 new_sz *= 2;
0063
0064 if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
0065 return -ENOSPC;
0066 }
0067
0068
0069 new = kzalloc(new_sz, GFP_KERNEL);
0070 if (!new)
0071 return -ENOMEM;
0072
0073 if (old)
0074 memcpy(new->priomap, old->priomap,
0075 old->priomap_len * sizeof(old->priomap[0]));
0076
0077 new->priomap_len = new_len;
0078
0079
0080 rcu_assign_pointer(dev->priomap, new);
0081 if (old)
0082 kfree_rcu(old, rcu);
0083 return 0;
0084 }
0085
0086
0087
0088
0089
0090
0091
0092
0093 static u32 netprio_prio(struct cgroup_subsys_state *css, struct net_device *dev)
0094 {
0095 struct netprio_map *map = rcu_dereference_rtnl(dev->priomap);
0096 int id = css->id;
0097
0098 if (map && id < map->priomap_len)
0099 return map->priomap[id];
0100 return 0;
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 static int netprio_set_prio(struct cgroup_subsys_state *css,
0113 struct net_device *dev, u32 prio)
0114 {
0115 struct netprio_map *map;
0116 int id = css->id;
0117 int ret;
0118
0119
0120 map = rtnl_dereference(dev->priomap);
0121 if (!prio && (!map || map->priomap_len <= id))
0122 return 0;
0123
0124 ret = extend_netdev_table(dev, id);
0125 if (ret)
0126 return ret;
0127
0128 map = rtnl_dereference(dev->priomap);
0129 map->priomap[id] = prio;
0130 return 0;
0131 }
0132
0133 static struct cgroup_subsys_state *
0134 cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
0135 {
0136 struct cgroup_subsys_state *css;
0137
0138 css = kzalloc(sizeof(*css), GFP_KERNEL);
0139 if (!css)
0140 return ERR_PTR(-ENOMEM);
0141
0142 return css;
0143 }
0144
0145 static int cgrp_css_online(struct cgroup_subsys_state *css)
0146 {
0147 struct cgroup_subsys_state *parent_css = css->parent;
0148 struct net_device *dev;
0149 int ret = 0;
0150
0151 if (css->id > NETPRIO_ID_MAX)
0152 return -ENOSPC;
0153
0154 if (!parent_css)
0155 return 0;
0156
0157 rtnl_lock();
0158
0159
0160
0161
0162 for_each_netdev(&init_net, dev) {
0163 u32 prio = netprio_prio(parent_css, dev);
0164
0165 ret = netprio_set_prio(css, dev, prio);
0166 if (ret)
0167 break;
0168 }
0169 rtnl_unlock();
0170 return ret;
0171 }
0172
0173 static void cgrp_css_free(struct cgroup_subsys_state *css)
0174 {
0175 kfree(css);
0176 }
0177
0178 static u64 read_prioidx(struct cgroup_subsys_state *css, struct cftype *cft)
0179 {
0180 return css->id;
0181 }
0182
0183 static int read_priomap(struct seq_file *sf, void *v)
0184 {
0185 struct net_device *dev;
0186
0187 rcu_read_lock();
0188 for_each_netdev_rcu(&init_net, dev)
0189 seq_printf(sf, "%s %u\n", dev->name,
0190 netprio_prio(seq_css(sf), dev));
0191 rcu_read_unlock();
0192 return 0;
0193 }
0194
0195 static ssize_t write_priomap(struct kernfs_open_file *of,
0196 char *buf, size_t nbytes, loff_t off)
0197 {
0198 char devname[IFNAMSIZ + 1];
0199 struct net_device *dev;
0200 u32 prio;
0201 int ret;
0202
0203 if (sscanf(buf, "%"__stringify(IFNAMSIZ)"s %u", devname, &prio) != 2)
0204 return -EINVAL;
0205
0206 dev = dev_get_by_name(&init_net, devname);
0207 if (!dev)
0208 return -ENODEV;
0209
0210 rtnl_lock();
0211
0212 ret = netprio_set_prio(of_css(of), dev, prio);
0213
0214 rtnl_unlock();
0215 dev_put(dev);
0216 return ret ?: nbytes;
0217 }
0218
0219 static int update_netprio(const void *v, struct file *file, unsigned n)
0220 {
0221 struct socket *sock = sock_from_file(file);
0222
0223 if (sock)
0224 sock_cgroup_set_prioidx(&sock->sk->sk_cgrp_data,
0225 (unsigned long)v);
0226 return 0;
0227 }
0228
0229 static void net_prio_attach(struct cgroup_taskset *tset)
0230 {
0231 struct task_struct *p;
0232 struct cgroup_subsys_state *css;
0233
0234 cgroup_taskset_for_each(p, css, tset) {
0235 void *v = (void *)(unsigned long)css->id;
0236
0237 task_lock(p);
0238 iterate_fd(p->files, 0, update_netprio, v);
0239 task_unlock(p);
0240 }
0241 }
0242
0243 static struct cftype ss_files[] = {
0244 {
0245 .name = "prioidx",
0246 .read_u64 = read_prioidx,
0247 },
0248 {
0249 .name = "ifpriomap",
0250 .seq_show = read_priomap,
0251 .write = write_priomap,
0252 },
0253 { }
0254 };
0255
0256 struct cgroup_subsys net_prio_cgrp_subsys = {
0257 .css_alloc = cgrp_css_alloc,
0258 .css_online = cgrp_css_online,
0259 .css_free = cgrp_css_free,
0260 .attach = net_prio_attach,
0261 .legacy_cftypes = ss_files,
0262 };
0263
0264 static int netprio_device_event(struct notifier_block *unused,
0265 unsigned long event, void *ptr)
0266 {
0267 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0268 struct netprio_map *old;
0269
0270
0271
0272
0273
0274
0275 switch (event) {
0276 case NETDEV_UNREGISTER:
0277 old = rtnl_dereference(dev->priomap);
0278 RCU_INIT_POINTER(dev->priomap, NULL);
0279 if (old)
0280 kfree_rcu(old, rcu);
0281 break;
0282 }
0283 return NOTIFY_DONE;
0284 }
0285
0286 static struct notifier_block netprio_device_notifier = {
0287 .notifier_call = netprio_device_event
0288 };
0289
0290 static int __init init_cgroup_netprio(void)
0291 {
0292 register_netdevice_notifier(&netprio_device_notifier);
0293 return 0;
0294 }
0295 subsys_initcall(init_cgroup_netprio);