Skip to content

Commit fe1217c

Browse files
Daniel Borkmannummakynes
Daniel Borkmann
authored andcommitted
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Cc: Zefan Li <[email protected]> Cc: Thomas Graf <[email protected]> Cc: [email protected] Acked-by: Li Zefan <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 14abfa1 commit fe1217c

File tree

8 files changed

+143
-151
lines changed

8 files changed

+143
-151
lines changed

include/linux/cgroup_subsys.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ SUBSYS(devices)
3131
SUBSYS(freezer)
3232
#endif
3333

34-
#if IS_SUBSYS_ENABLED(CONFIG_NET_CLS_CGROUP)
34+
#if IS_SUBSYS_ENABLED(CONFIG_CGROUP_NET_CLASSID)
3535
SUBSYS(net_cls)
3636
#endif
3737

include/net/cls_cgroup.h

+12-28
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
#include <linux/cgroup.h>
1717
#include <linux/hardirq.h>
1818
#include <linux/rcupdate.h>
19+
#include <net/sock.h>
1920

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 {
2323
struct cgroup_subsys_state css;
2424
u32 classid;
2525
};
2626

27-
void sock_update_classid(struct sock *sk);
27+
struct cgroup_cls_state *task_cls_state(struct task_struct *p);
2828

29-
#if IS_BUILTIN(CONFIG_NET_CLS_CGROUP)
3029
static inline u32 task_cls_classid(struct task_struct *p)
3130
{
3231
u32 classid;
@@ -41,33 +40,18 @@ static inline u32 task_cls_classid(struct task_struct *p)
4140

4241
return classid;
4342
}
44-
#elif IS_MODULE(CONFIG_NET_CLS_CGROUP)
45-
static inline u32 task_cls_classid(struct task_struct *p)
46-
{
47-
struct cgroup_subsys_state *css;
48-
u32 classid = 0;
49-
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();
5943

60-
return classid;
61-
}
62-
#endif
63-
#else /* !CGROUP_NET_CLS_CGROUP */
6444
static inline void sock_update_classid(struct sock *sk)
6545
{
66-
}
46+
u32 classid;
6747

68-
static inline u32 task_cls_classid(struct task_struct *p)
48+
classid = task_cls_classid(current);
49+
if (classid != sk->sk_classid)
50+
sk->sk_classid = classid;
51+
}
52+
#else /* !CONFIG_CGROUP_NET_CLASSID */
53+
static inline void sock_update_classid(struct sock *sk)
6954
{
70-
return 0;
7155
}
72-
#endif /* CGROUP_NET_CLS_CGROUP */
56+
#endif /* CONFIG_CGROUP_NET_CLASSID */
7357
#endif /* _NET_CLS_CGROUP_H */

net/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ config NETPRIO_CGROUP
245245
Cgroup subsystem for use in assigning processes to network priorities on
246246
a per-interface basis
247247

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+
248255
config NET_RX_BUSY_POLL
249256
boolean
250257
default y

net/core/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ obj-$(CONFIG_TRACEPOINTS) += net-traces.o
2222
obj-$(CONFIG_NET_DROP_MONITOR) += drop_monitor.o
2323
obj-$(CONFIG_NETWORK_PHY_TIMESTAMPING) += timestamping.o
2424
obj-$(CONFIG_NETPRIO_CGROUP) += netprio_cgroup.o
25+
obj-$(CONFIG_CGROUP_NET_CLASSID) += netclassid_cgroup.o

net/core/netclassid_cgroup.c

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 <[email protected]>
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);

net/core/sock.c

-12
Original file line numberDiff line numberDiff line change
@@ -1308,18 +1308,6 @@ static void sk_prot_free(struct proto *prot, struct sock *sk)
13081308
module_put(owner);
13091309
}
13101310

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-
13231311
#if IS_ENABLED(CONFIG_NETPRIO_CGROUP)
13241312
void sock_update_netprioidx(struct sock *sk)
13251313
{

net/sched/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ config NET_CLS_FLOW
435435
config NET_CLS_CGROUP
436436
tristate "Control Group Classifier"
437437
select NET_CLS
438+
select CGROUP_NET_CLASSID
438439
depends on CGROUPS
439440
---help---
440441
Say Y here if you want to classify packets based on the control

net/sched/cls_cgroup.c

+1-110
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,13 @@
1111

1212
#include <linux/module.h>
1313
#include <linux/slab.h>
14-
#include <linux/types.h>
15-
#include <linux/string.h>
16-
#include <linux/errno.h>
1714
#include <linux/skbuff.h>
18-
#include <linux/cgroup.h>
1915
#include <linux/rcupdate.h>
20-
#include <linux/fdtable.h>
2116
#include <net/rtnetlink.h>
2217
#include <net/pkt_cls.h>
2318
#include <net/sock.h>
2419
#include <net/cls_cgroup.h>
2520

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-
11721
struct cls_cgroup_head {
11822
u32 handle;
11923
struct tcf_exts exts;
@@ -309,25 +213,12 @@ static struct tcf_proto_ops cls_cgroup_ops __read_mostly = {
309213

310214
static int __init init_cgroup_cls(void)
311215
{
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);
324217
}
325218

326219
static void __exit exit_cgroup_cls(void)
327220
{
328221
unregister_tcf_proto_ops(&cls_cgroup_ops);
329-
330-
cgroup_unload_subsys(&net_cls_subsys);
331222
}
332223

333224
module_init(init_cgroup_cls);

0 commit comments

Comments
 (0)