Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * net/core/netprio_cgroup.c    Priority Control Group
0004  *
0005  * Authors: Neil Horman <nhorman@tuxdriver.com>
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  * netprio allocates per-net_device priomap array which is indexed by
0030  * css->id.  Limiting css ID to 16bits doesn't lose anything.
0031  */
0032 #define NETPRIO_ID_MAX      USHRT_MAX
0033 
0034 #define PRIOMAP_MIN_SZ      128
0035 
0036 /*
0037  * Extend @dev->priomap so that it's large enough to accommodate
0038  * @target_idx.  @dev->priomap.priomap_len > @target_idx after successful
0039  * return.  Must be called under rtnl lock.
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     /* is the existing priomap large enough? */
0047     old = rtnl_dereference(dev->priomap);
0048     if (old && old->priomap_len > target_idx)
0049         return 0;
0050 
0051     /*
0052      * Determine the new size.  Let's keep it power-of-two.  We start
0053      * from PRIOMAP_MIN_SZ and double it until it's large enough to
0054      * accommodate @target_idx.
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         /* overflowed? */
0064         if (WARN_ON(new_sz < PRIOMAP_MIN_SZ))
0065             return -ENOSPC;
0066     }
0067 
0068     /* allocate & copy */
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     /* install the new priomap */
0080     rcu_assign_pointer(dev->priomap, new);
0081     if (old)
0082         kfree_rcu(old, rcu);
0083     return 0;
0084 }
0085 
0086 /**
0087  * netprio_prio - return the effective netprio of a cgroup-net_device pair
0088  * @css: css part of the target pair
0089  * @dev: net_device part of the target pair
0090  *
0091  * Should be called under RCU read or rtnl lock.
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  * netprio_set_prio - set netprio on a cgroup-net_device pair
0105  * @css: css part of the target pair
0106  * @dev: net_device part of the target pair
0107  * @prio: prio to set
0108  *
0109  * Set netprio to @prio on @css-@dev pair.  Should be called under rtnl
0110  * lock and may fail under memory pressure for non-zero @prio.
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     /* avoid extending priomap for zero writes */
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      * Inherit prios from the parent.  As all prios are set during
0160      * onlining, there is no need to clear them on offline.
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     { } /* terminate */
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      * Note this is called with rtnl_lock held so we have update side
0272      * protection on our rcu assignments
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);