Skip to contentSkip to conversation

pkt_sched: Control group classifier

PATCH Superseded Thomas Graf 3 participants 10 messages Oct 13, 2008 – Oct 19, 2008

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pkt_sched: Control group classifier

This patch implements a very basic cgroup subsystem which allows assigning classids to cgroups. It also adds a trivial classifier to filter packets based on that classid and map them to classes.

The classifier should cover the most common use case and will work without any special configuration.

The principle of the classifier is to directly access the task_struct via get_current(). In order for this to work, classification requests from softirqs must be ignored. This is not a problem because the vast majority of packets in softirq context are not assigned to a task anyway. For this to work, a mechanism is needed to trace softirq context. The only existing way to do so is to use the TRACE_IRQFLAGS feature, unfortunately this debug feature is heavy and depends on lockdep which is overkill. Therefore the code required to trace soft/hard irqs was extracted from TRACE_IRQFLAGS and made available under a new config option TRACE_CONTEXT which can now be used from both the cgroup classifier and lockdep. The consequence is that task_struct grows by 2 ints and a counter is bumped/decreased for each soft/hard irq being fired if the cgroup classifier is compiled in.

I've taken over the idea of inheriting the classid of cgroup parents from the patchset posted by Ranjit Manomohan <[email protected]>.

include/linux/cgroup_subsys.h +6 -0
Index: net-2.6-next/include/linux/cgroup_subsys.h
===================================================================
@@ -48,3 +48,9 @@
 #endif
 
 /* */
+
+#ifdef CONFIG_NET_CLS_CGROUP
+SUBSYS(net_cls)
+#endif
+
+/* */
include/linux/pkt_cls.h +14 -0
Index: net-2.6-next/include/linux/pkt_cls.h
===================================================================
@@ -394,6 +394,20 @@
 
 #define TCA_BASIC_MAX (__TCA_BASIC_MAX - 1)
 
+
+/* Cgroup classifier */
+
+enum
+{
+	TCA_CGROUP_UNSPEC,
+	TCA_CGROUP_ACT,
+	TCA_CGROUP_POLICE,
+	TCA_CGROUP_EMATCHES,
+	__TCA_CGROUP_MAX,
+};
+
+#define TCA_CGROUP_MAX (__TCA_CGROUP_MAX - 1)
+
 /* Extended Matches */
 
 struct tcf_ematch_tree_hdr
net/sched/Kconfig +12 -0
Index: net-2.6-next/net/sched/Kconfig
===================================================================
@@ -316,6 +316,18 @@
 	  To compile this code as a module, choose M here: the
 	  module will be called cls_flow.
 
+config NET_CLS_CGROUP
+	tristate "Control Group Classifier"
+	select NET_CLS
+	select TRACE_CONTEXT
+	depends on CGROUPS
+	---help---
+	  Say Y here if you want to classify packets based on the control
+	  cgroup of their process.
+
+	  To compile this code as a module, choose M here: the
+	  module will be called cls_cgroup.
+
 config NET_EMATCH
 	bool "Extended Matches"
 	select NET_CLS
net/sched/Makefile +1 -0
Index: net-2.6-next/net/sched/Makefile
===================================================================
@@ -38,6 +38,7 @@
 obj-$(CONFIG_NET_CLS_RSVP6)	+= cls_rsvp6.o
 obj-$(CONFIG_NET_CLS_BASIC)	+= cls_basic.o
 obj-$(CONFIG_NET_CLS_FLOW)	+= cls_flow.o
+obj-$(CONFIG_NET_CLS_CGROUP)	+= cls_cgroup.o
 obj-$(CONFIG_NET_EMATCH)	+= ematch.o
 obj-$(CONFIG_NET_EMATCH_CMP)	+= em_cmp.o
 obj-$(CONFIG_NET_EMATCH_NBYTE)	+= em_nbyte.o
net/sched/cls_cgroup.c new file +280 -0

280 lines not on this page · raw message

include/linux/sched.h +4 -2
Index: net-2.6-next/include/linux/sched.h
===================================================================
@@ -1216,6 +1216,10 @@
 	/* mutex deadlock detection */
 	struct mutex_waiter *blocked_on;
 #endif
+#ifdef CONFIG_TRACE_CONTEXT
+	int hardirq_context;
+	int softirq_context;
+#endif
 #ifdef CONFIG_TRACE_IRQFLAGS
 	unsigned int irq_events;
 	int hardirqs_enabled;
@@ -1228,8 +1232,6 @@
 	unsigned int softirq_disable_event;
 	unsigned long softirq_enable_ip;
 	unsigned int softirq_enable_event;
-	int hardirq_context;
-	int softirq_context;
 #endif
 #ifdef CONFIG_LOCKDEP
 # define MAX_LOCK_DEPTH 48UL
include/linux/irqflags.h +16 -12
Index: net-2.6-next/include/linux/irqflags.h
===================================================================
@@ -13,33 +13,37 @@
 
 #include <linux/typecheck.h>
 
+#ifdef CONFIG_TRACE_CONTEXT
+# define trace_hardirq_context(p)	((p)->hardirq_context)
+# define trace_softirq_context(p)	((p)->softirq_context)
+# define trace_hardirq_enter()	do { current->hardirq_context++; } while (0)
+# define trace_hardirq_exit()	do { current->hardirq_context--; } while (0)
+# define trace_softirq_enter()	do { current->softirq_context++; } while (0)
+# define trace_softirq_exit()	do { current->softirq_context--; } while (0)
+#else
+# define trace_hardirq_context(p)	0
+# define trace_softirq_context(p)	0
+# define trace_hardirq_enter()		do { } while (0)
+# define trace_hardirq_exit()		do { } while (0)
+# define trace_softirq_enter()		do { } while (0)
+# define trace_softirq_exit()		do { } while (0)
+#endif
+
 #ifdef CONFIG_TRACE_IRQFLAGS
   extern void trace_softirqs_on(unsigned long ip);
   extern void trace_softirqs_off(unsigned long ip);
   extern void trace_hardirqs_on(void);
   extern void trace_hardirqs_off(void);
-# define trace_hardirq_context(p)	((p)->hardirq_context)
-# define trace_softirq_context(p)	((p)->softirq_context)
 # define trace_hardirqs_enabled(p)	((p)->hardirqs_enabled)
 # define trace_softirqs_enabled(p)	((p)->softirqs_enabled)
-# define trace_hardirq_enter()	do { current->hardirq_context++; } while (0)
-# define trace_hardirq_exit()	do { current->hardirq_context--; } while (0)
-# define trace_softirq_enter()	do { current->softirq_context++; } while (0)
-# define trace_softirq_exit()	do { current->softirq_context--; } while (0)
 # define INIT_TRACE_IRQFLAGS	.softirqs_enabled = 1,
 #else
 # define trace_hardirqs_on()		do { } while (0)
 # define trace_hardirqs_off()		do { } while (0)
 # define trace_softirqs_on(ip)		do { } while (0)
 # define trace_softirqs_off(ip)		do { } while (0)
-# define trace_hardirq_context(p)	0
-# define trace_softirq_context(p)	0
 # define trace_hardirqs_enabled(p)	0
 # define trace_softirqs_enabled(p)	0
-# define trace_hardirq_enter()		do { } while (0)
-# define trace_hardirq_exit()		do { } while (0)
-# define trace_softirq_enter()		do { } while (0)
-# define trace_softirq_exit()		do { } while (0)
 # define INIT_TRACE_IRQFLAGS
 #endif
 
kernel/fork.c +4 -2
Index: net-2.6-next/kernel/fork.c
===================================================================
@@ -1022,6 +1022,10 @@
  	}
 	mpol_fix_fork_child_flag(p);
 #endif
+#ifdef CONFIG_TRACE_CONTEXT
+	p->hardirq_context = 0;
+	p->softirq_context = 0;
+#endif
 #ifdef CONFIG_TRACE_IRQFLAGS
 	p->irq_events = 0;
 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
@@ -1038,8 +1042,6 @@
 	p->softirq_enable_event = 0;
 	p->softirq_disable_ip = 0;
 	p->softirq_disable_event = 0;
-	p->hardirq_context = 0;
-	p->softirq_context = 0;
 #endif
 #ifdef CONFIG_LOCKDEP
 	p->lockdep_depth = 0; /* no locks held yet */
lib/Kconfig +6 -0
Index: net-2.6-next/lib/Kconfig
===================================================================
@@ -157,4 +157,10 @@
 config HAVE_LMB
 	boolean
 
+#
+# soft/hard irq tracing
+#
+config TRACE_CONTEXT
+	boolean
+
 endmenu
lib/Kconfig.debug +1 -0
Index: net-2.6-next/lib/Kconfig.debug
===================================================================
@@ -423,6 +423,7 @@
 	depends on DEBUG_KERNEL
 	bool
 	default y
+	select TRACE_CONTEXT
 	depends on TRACE_IRQFLAGS_SUPPORT
 	depends on PROVE_LOCKING
 
#1 lore raw
Quoted 5 lines · Thomas Graf <tgraf <at> suug.ch> writes:

This patch implements a very basic cgroup subsystem which allows assigning classids to cgroups. It also adds a trivial classifier to filter packets based on that classid and map them to classes.

In order to test your patch, I need more programs such as iproute-x.x.x package modified for using your cgroup classifier.
You seem to test your code now. Please send me additional code and patch for testing environments.

Help me!

Your sincerely.

#2 lore raw

* Chei-yol Kim <[email protected]> 2008-10-14 11:56

Quoted 8 lines
Quoted 5 lines · Thomas Graf <tgraf <at> suug.ch> writes:

This patch implements a very basic cgroup subsystem which allows assigning classids to cgroups. It also adds a trivial classifier to filter packets based on that classid and map them to classes.

Quoted 4 lines

In order to test your patch, I need more programs such as iproute-x.x.x package modified for using your cgroup classifier.
You seem to test your code now. Please send me additional code and patch for testing environments.

Of course, this is the patch required for iproute2.

include/linux/pkt_cls.h +15 -0
index 99efbed..e6aa848 100644
@@ -374,6 +374,7 @@ enum
 	TCA_FLOW_ACT,
 	TCA_FLOW_POLICE,
 	TCA_FLOW_EMATCHES,
+	TCA_FLOW_PERTURB,
 	__TCA_FLOW_MAX
 };
 
@@ -393,6 +394,20 @@ enum
 
 #define TCA_BASIC_MAX (__TCA_BASIC_MAX - 1)
 
+
+/* Cgroup classifier */
+
+enum
+{
+	TCA_CGROUP_UNSPEC,
+	TCA_CGROUP_ACT,
+	TCA_CGROUP_POLICE,
+	TCA_CGROUP_EMATCHES,
+	__TCA_CGROUP_MAX,
+};
+
+#define TCA_CGROUP_MAX (__TCA_CGROUP_MAX - 1)
+
 /* Extended Matches */
 
 struct tcf_ematch_tree_hdr
tc/Makefile +1 -0
index 4116983..1a4459a 100644
@@ -19,6 +19,7 @@ TCMODULES += f_route.o
 TCMODULES += f_fw.o
 TCMODULES += f_basic.o
 TCMODULES += f_flow.o
+TCMODULES += f_cgroup.o
 TCMODULES += q_dsmark.o
 TCMODULES += q_gred.o
 TCMODULES += f_tcindex.o
tc/f_cgroup.c new file +115 -0

115 lines not on this page · raw message

#3 lore raw
In reply to Chei-yol Kim

* Chei-yol Kim <[email protected]> 2008-10-14 11:56

Quoted 8 lines
Quoted 5 lines · Thomas Graf <tgraf <at> suug.ch> writes:

This patch implements a very basic cgroup subsystem which allows assigning classids to cgroups. It also adds a trivial classifier to filter packets based on that classid and map them to classes.

Quoted 4 lines

In order to test your patch, I need more programs such as iproute-x.x.x package modified for using your cgroup classifier.
You seem to test your code now. Please send me additional code and patch for testing environments.

Here is an example of how to use it:

mkdir -p /dev/cgroup
mount -t cgroup net_cls -onet_cls /dev/cgroup
mkdir /dev/cgroup/foo

# packets in cgroup foo go to class 10:1
echo 0xA0001 > /dev/cgroup/foo/net_cls.classid

tc qdisc add dev eth0 root handle 10: htb
tc class add dev eth0 parent 10:0 classid 10:1 htb rate 10mbit tc class add dev eth0 parent 10:0 classid 10:2 htb rate 10mbit

# cgroup filter -> 10:X
tc filter add dev eth0 parent 10: protocol ip prio 10 handle 20: cgroup
# everything else -> 10:1
tc filter add dev eth0 parent 10: protocol ip prio 20 basic classid 10:2
#4 lore raw
In reply to Thomas Graf
Quoted 4 lines · Thomas Graf

The principle of the classifier is to directly access the task_struct via get_current(). In order for this to work, classification requests from softirqs must be ignored.

You mean the classifier just wants to know if it runs as a softirq?

Quoted 6 lines

This is
not a problem because the vast majority of packets in softirq context are not assigned to a task anyway. For this to work, a mechanism is needed to trace softirq context.
The only existing
way to do so is to use the TRACE_IRQFLAGS feature, unfortunately

Does't softirq_count() > 0 check that?

-Andi

Signature · 1 line
[email protected]
#5 lore raw

* Andi Kleen <[email protected]> 2008-10-16 07:41

Quoted 8 lines
Quoted 4 lines · Thomas Graf

The principle of the classifier is to directly access the task_struct via get_current(). In order for this to work, classification requests from softirqs must be ignored.

You mean the classifier just wants to know if it runs as a softirq?

Yes, exactly.

Quoted 8 lines
Quoted 6 lines

This is
not a problem because the vast majority of packets in softirq context are not assigned to a task anyway. For this to work, a mechanism is needed to trace softirq context.
The only existing
way to do so is to use the TRACE_IRQFLAGS feature, unfortunately

Does't softirq_count() > 0 check that?

softirq_count() simply counts the levels of bh disable.

#6 lore raw
Quoted 1 line

softirq_count() simply counts the levels of bh disable.

softirqs always have bh disabled. So it's always true inside them. And you should know what _bh locks you hold.

-Andi

Signature · 1 line
[email protected]
#7 lore raw

* Andi Kleen <[email protected]> 2008-10-17 20:32

Quoted 4 lines
Quoted 1 line

softirq_count() simply counts the levels of bh disable.

softirqs always have bh disabled. So it's always true inside them. And you should know what _bh locks you hold.

I actually tried "softirq_count() == SOFTIRQ_OFFSET" to see if the call is made form user context. It works pretty well because all callers of dev_queue_xmit() are required to disable bh. Nevertheless I think it's quite fragile. If a dev_queue_xmit() caller from softirq does not disable bh explicitely on its own but instead relies on do_softirq() to have disabled bh already, the softirq count of such a call may be equal to the one of a user context call to dev_queue_xmit().

I found explicitely counting softirq_enter() to be more reliable without having much of a performance impact. I'm happy with both solutions though.

#8 lore raw
Quoted 7 lines · Thomas Graf

* Andi Kleen <[email protected]> 2008-10-17 20:32

Quoted 4 lines
Quoted 1 line

softirq_count() simply counts the levels of bh disable.

softirqs always have bh disabled. So it's always true inside them. And you should know what _bh locks you hold.

I actually tried "softirq_count() == SOFTIRQ_OFFSET" to see if the

softirq_count() > 0 should work.

Quoted 3 lines

I found explicitely counting softirq_enter() to be more reliable without having much of a performance impact. I'm happy with both solutions though.

I doubt you'll need a separate ocunter.

-Andi

Signature · 1 line
[email protected]
#9 lore raw

* Andi Kleen <[email protected]> 2008-10-18 07:38

Quoted 10 lines · Thomas Graf
Quoted 7 lines

* Andi Kleen <[email protected]> 2008-10-17 20:32

Quoted 4 lines
Quoted 1 line

softirq_count() simply counts the levels of bh disable.

softirqs always have bh disabled. So it's always true inside them. And you should know what _bh locks you hold.

I actually tried "softirq_count() == SOFTIRQ_OFFSET" to see if the

softirq_count() > 0 should work.

No, it doesn't. User context calls to dev_queue_xmit() will also disable bh and thus softirq_count() will always be > 0.