Commit fe1217c4f3f7d7cbf8efdd8dd5fdc7204a1d65a8

Authored by Daniel Borkmann
Committed by Pablo Neira Ayuso
1 parent 14abfa161d

net: net_cls: move cgroupfs classid handling into core

Zefan Li requested [1] to perform the following cleanup/refactoring:

- Split cgroupfs classid handling into net core to better express a
  possible more generic use.

- Disable module support for cgroupfs bits as the majority of other
  cgroupfs subsystems do not have that, and seems to be not wished
  from cgroup side. Zefan probably might want to follow-up for netprio
  later on.

- By this, code can be further reduced which previously took care of
  functionality built when compiled as module.

cgroupfs bits are being placed under net/core/netclassid_cgroup.c, so
that we are consistent with {netclassid,netprio}_cgroup naming that is
under net/core/ as suggested by Zefan.

No change in functionality, but only code refactoring that is being
done here.

 [1] http://patchwork.ozlabs.org/patch/304825/

Suggested-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Cc: Zefan Li <lizefan@huawei.com>
Cc: Thomas Graf <tgraf@suug.ch>
Cc: cgroups@vger.kernel.org
Acked-by: Li Zefan <lizefan@huawei.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

Showing 8 changed files with 143 additions and 151 deletions Side-by-side Diff

include/linux/cgroup_subsys.h
... ... @@ -31,7 +31,7 @@
31 31 SUBSYS(freezer)
32 32 #endif
33 33  
34   -#if IS_SUBSYS_ENABLED(CONFIG_NET_CLS_CGROUP)
  34 +#if IS_SUBSYS_ENABLED(CONFIG_CGROUP_NET_CLASSID)
35 35 SUBSYS(net_cls)
36 36 #endif
37 37  
include/net/cls_cgroup.h
... ... @@ -16,17 +16,16 @@
16 16 #include <linux/cgroup.h>
17 17 #include <linux/hardirq.h>
18 18 #include <linux/rcupdate.h>
  19 +#include <net/sock.h>
19 20  
20   -#if IS_ENABLED(CONFIG_NET_CLS_CGROUP)
21   -struct cgroup_cls_state
22   -{
  21 +#ifdef CONFIG_CGROUP_NET_CLASSID
  22 +struct cgroup_cls_state {
23 23 struct cgroup_subsys_state css;
24 24 u32 classid;
25 25 };
26 26  
27   -void sock_update_classid(struct sock *sk);
  27 +struct cgroup_cls_state *task_cls_state(struct task_struct *p);
28 28  
29   -#if IS_BUILTIN(CONFIG_NET_CLS_CGROUP)
30 29 static inline u32 task_cls_classid(struct task_struct *p)
31 30 {
32 31 u32 classid;
33 32  
34 33  
35 34  
36 35  
... ... @@ -41,34 +40,19 @@
41 40  
42 41 return classid;
43 42 }
44   -#elif IS_MODULE(CONFIG_NET_CLS_CGROUP)
45   -static inline u32 task_cls_classid(struct task_struct *p)
  43 +
  44 +static inline void sock_update_classid(struct sock *sk)
46 45 {
47   - struct cgroup_subsys_state *css;
48   - u32 classid = 0;
  46 + u32 classid;
49 47  
50   - if (in_interrupt())
51   - return 0;
52   -
53   - rcu_read_lock();
54   - css = task_css(p, net_cls_subsys_id);
55   - if (css)
56   - classid = container_of(css,
57   - struct cgroup_cls_state, css)->classid;
58   - rcu_read_unlock();
59   -
60   - return classid;
  48 + classid = task_cls_classid(current);
  49 + if (classid != sk->sk_classid)
  50 + sk->sk_classid = classid;
61 51 }
62   -#endif
63   -#else /* !CGROUP_NET_CLS_CGROUP */
  52 +#else /* !CONFIG_CGROUP_NET_CLASSID */
64 53 static inline void sock_update_classid(struct sock *sk)
65 54 {
66 55 }
67   -
68   -static inline u32 task_cls_classid(struct task_struct *p)
69   -{
70   - return 0;
71   -}
72   -#endif /* CGROUP_NET_CLS_CGROUP */
  56 +#endif /* CONFIG_CGROUP_NET_CLASSID */
73 57 #endif /* _NET_CLS_CGROUP_H */
... ... @@ -245,6 +245,13 @@
245 245 Cgroup subsystem for use in assigning processes to network priorities on
246 246 a per-interface basis
247 247  
  248 +config CGROUP_NET_CLASSID
  249 + boolean "Network classid cgroup"
  250 + depends on CGROUPS
  251 + ---help---
  252 + Cgroup subsystem for use as general purpose socket classid marker that is
  253 + being used in cls_cgroup and for netfilter matching.
  254 +
248 255 config NET_RX_BUSY_POLL
249 256 boolean
250 257 default y
... ... @@ -22,4 +22,5 @@
22 22 obj-$(CONFIG_NET_DROP_MONITOR) += drop_monitor.o
23 23 obj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += timestamping.o
24 24 obj-$(CONFIG_NETPRIO_CGROUP) += netprio_cgroup.o
  25 +obj-$(CONFIG_CGROUP_NET_CLASSID) += netclassid_cgroup.o
net/core/netclassid_cgroup.c
  1 +/*
  2 + * net/core/netclassid_cgroup.c Classid Cgroupfs Handling
  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: Thomas Graf <tgraf@suug.ch>
  10 + */
  11 +
  12 +#include <linux/module.h>
  13 +#include <linux/slab.h>
  14 +#include <linux/cgroup.h>
  15 +#include <linux/fdtable.h>
  16 +#include <net/cls_cgroup.h>
  17 +#include <net/sock.h>
  18 +
  19 +static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
  20 +{
  21 + return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
  22 +}
  23 +
  24 +struct cgroup_cls_state *task_cls_state(struct task_struct *p)
  25 +{
  26 + return css_cls_state(task_css(p, net_cls_subsys_id));
  27 +}
  28 +EXPORT_SYMBOL_GPL(task_cls_state);
  29 +
  30 +static struct cgroup_subsys_state *
  31 +cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
  32 +{
  33 + struct cgroup_cls_state *cs;
  34 +
  35 + cs = kzalloc(sizeof(*cs), GFP_KERNEL);
  36 + if (!cs)
  37 + return ERR_PTR(-ENOMEM);
  38 +
  39 + return &cs->css;
  40 +}
  41 +
  42 +static int cgrp_css_online(struct cgroup_subsys_state *css)
  43 +{
  44 + struct cgroup_cls_state *cs = css_cls_state(css);
  45 + struct cgroup_cls_state *parent = css_cls_state(css_parent(css));
  46 +
  47 + if (parent)
  48 + cs->classid = parent->classid;
  49 +
  50 + return 0;
  51 +}
  52 +
  53 +static void cgrp_css_free(struct cgroup_subsys_state *css)
  54 +{
  55 + kfree(css_cls_state(css));
  56 +}
  57 +
  58 +static int update_classid(const void *v, struct file *file, unsigned n)
  59 +{
  60 + int err;
  61 + struct socket *sock = sock_from_file(file, &err);
  62 +
  63 + if (sock)
  64 + sock->sk->sk_classid = (u32)(unsigned long)v;
  65 +
  66 + return 0;
  67 +}
  68 +
  69 +static void cgrp_attach(struct cgroup_subsys_state *css,
  70 + struct cgroup_taskset *tset)
  71 +{
  72 + struct cgroup_cls_state *cs = css_cls_state(css);
  73 + void *v = (void *)(unsigned long)cs->classid;
  74 + struct task_struct *p;
  75 +
  76 + cgroup_taskset_for_each(p, css, tset) {
  77 + task_lock(p);
  78 + iterate_fd(p->files, 0, update_classid, v);
  79 + task_unlock(p);
  80 + }
  81 +}
  82 +
  83 +static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
  84 +{
  85 + return css_cls_state(css)->classid;
  86 +}
  87 +
  88 +static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
  89 + u64 value)
  90 +{
  91 + css_cls_state(css)->classid = (u32) value;
  92 +
  93 + return 0;
  94 +}
  95 +
  96 +static struct cftype ss_files[] = {
  97 + {
  98 + .name = "classid",
  99 + .read_u64 = read_classid,
  100 + .write_u64 = write_classid,
  101 + },
  102 + { } /* terminate */
  103 +};
  104 +
  105 +struct cgroup_subsys net_cls_subsys = {
  106 + .name = "net_cls",
  107 + .css_alloc = cgrp_css_alloc,
  108 + .css_online = cgrp_css_online,
  109 + .css_free = cgrp_css_free,
  110 + .attach = cgrp_attach,
  111 + .subsys_id = net_cls_subsys_id,
  112 + .base_cftypes = ss_files,
  113 + .module = THIS_MODULE,
  114 +};
  115 +
  116 +static int __init init_netclassid_cgroup(void)
  117 +{
  118 + return cgroup_load_subsys(&net_cls_subsys);
  119 +}
  120 +__initcall(init_netclassid_cgroup);
... ... @@ -1308,18 +1308,6 @@
1308 1308 module_put(owner);
1309 1309 }
1310 1310  
1311   -#if IS_ENABLED(CONFIG_NET_CLS_CGROUP)
1312   -void sock_update_classid(struct sock *sk)
1313   -{
1314   - u32 classid;
1315   -
1316   - classid = task_cls_classid(current);
1317   - if (classid != sk->sk_classid)
1318   - sk->sk_classid = classid;
1319   -}
1320   -EXPORT_SYMBOL(sock_update_classid);
1321   -#endif
1322   -
1323 1311 #if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
1324 1312 void sock_update_netprioidx(struct sock *sk)
1325 1313 {
... ... @@ -435,6 +435,7 @@
435 435 config NET_CLS_CGROUP
436 436 tristate "Control Group Classifier"
437 437 select NET_CLS
  438 + select CGROUP_NET_CLASSID
438 439 depends on CGROUPS
439 440 ---help---
440 441 Say Y here if you want to classify packets based on the control
net/sched/cls_cgroup.c
... ... @@ -11,109 +11,13 @@
11 11  
12 12 #include <linux/module.h>
13 13 #include <linux/slab.h>
14   -#include <linux/types.h>
15   -#include <linux/string.h>
16   -#include <linux/errno.h>
17 14 #include <linux/skbuff.h>
18   -#include <linux/cgroup.h>
19 15 #include <linux/rcupdate.h>
20   -#include <linux/fdtable.h>
21 16 #include <net/rtnetlink.h>
22 17 #include <net/pkt_cls.h>
23 18 #include <net/sock.h>
24 19 #include <net/cls_cgroup.h>
25 20  
26   -static inline struct cgroup_cls_state *css_cls_state(struct cgroup_subsys_state *css)
27   -{
28   - return css ? container_of(css, struct cgroup_cls_state, css) : NULL;
29   -}
30   -
31   -static inline struct cgroup_cls_state *task_cls_state(struct task_struct *p)
32   -{
33   - return css_cls_state(task_css(p, net_cls_subsys_id));
34   -}
35   -
36   -static struct cgroup_subsys_state *
37   -cgrp_css_alloc(struct cgroup_subsys_state *parent_css)
38   -{
39   - struct cgroup_cls_state *cs;
40   -
41   - cs = kzalloc(sizeof(*cs), GFP_KERNEL);
42   - if (!cs)
43   - return ERR_PTR(-ENOMEM);
44   - return &cs->css;
45   -}
46   -
47   -static int cgrp_css_online(struct cgroup_subsys_state *css)
48   -{
49   - struct cgroup_cls_state *cs = css_cls_state(css);
50   - struct cgroup_cls_state *parent = css_cls_state(css_parent(css));
51   -
52   - if (parent)
53   - cs->classid = parent->classid;
54   - return 0;
55   -}
56   -
57   -static void cgrp_css_free(struct cgroup_subsys_state *css)
58   -{
59   - kfree(css_cls_state(css));
60   -}
61   -
62   -static int update_classid(const void *v, struct file *file, unsigned n)
63   -{
64   - int err;
65   - struct socket *sock = sock_from_file(file, &err);
66   - if (sock)
67   - sock->sk->sk_classid = (u32)(unsigned long)v;
68   - return 0;
69   -}
70   -
71   -static void cgrp_attach(struct cgroup_subsys_state *css,
72   - struct cgroup_taskset *tset)
73   -{
74   - struct task_struct *p;
75   - struct cgroup_cls_state *cs = css_cls_state(css);
76   - void *v = (void *)(unsigned long)cs->classid;
77   -
78   - cgroup_taskset_for_each(p, css, tset) {
79   - task_lock(p);
80   - iterate_fd(p->files, 0, update_classid, v);
81   - task_unlock(p);
82   - }
83   -}
84   -
85   -static u64 read_classid(struct cgroup_subsys_state *css, struct cftype *cft)
86   -{
87   - return css_cls_state(css)->classid;
88   -}
89   -
90   -static int write_classid(struct cgroup_subsys_state *css, struct cftype *cft,
91   - u64 value)
92   -{
93   - css_cls_state(css)->classid = (u32) value;
94   - return 0;
95   -}
96   -
97   -static struct cftype ss_files[] = {
98   - {
99   - .name = "classid",
100   - .read_u64 = read_classid,
101   - .write_u64 = write_classid,
102   - },
103   - { } /* terminate */
104   -};
105   -
106   -struct cgroup_subsys net_cls_subsys = {
107   - .name = "net_cls",
108   - .css_alloc = cgrp_css_alloc,
109   - .css_online = cgrp_css_online,
110   - .css_free = cgrp_css_free,
111   - .attach = cgrp_attach,
112   - .subsys_id = net_cls_subsys_id,
113   - .base_cftypes = ss_files,
114   - .module = THIS_MODULE,
115   -};
116   -
117 21 struct cls_cgroup_head {
118 22 u32 handle;
119 23 struct tcf_exts exts;
120 24  
... ... @@ -309,25 +213,12 @@
309 213  
310 214 static int __init init_cgroup_cls(void)
311 215 {
312   - int ret;
313   -
314   - ret = cgroup_load_subsys(&net_cls_subsys);
315   - if (ret)
316   - goto out;
317   -
318   - ret = register_tcf_proto_ops(&cls_cgroup_ops);
319   - if (ret)
320   - cgroup_unload_subsys(&net_cls_subsys);
321   -
322   -out:
323   - return ret;
  216 + return register_tcf_proto_ops(&cls_cgroup_ops);
324 217 }
325 218  
326 219 static void __exit exit_cgroup_cls(void)
327 220 {
328 221 unregister_tcf_proto_ops(&cls_cgroup_ops);
329   -
330   - cgroup_unload_subsys(&net_cls_subsys);
331 222 }
332 223  
333 224 module_init(init_cgroup_cls);