Skip to contentSkip to conversation

Traffic control cgroups subsystem

PATCH In review Ranjit Manomohan 4 participants 13 messages Sep 10, 2008 – Sep 11, 2008

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

PATCH 1/2 Traffic control cgroups subsystem

This patch adds a traffic control cgroup subsystem that is used to associate all packets originating from tasks in this cgroup with a specific identifier (tc_classid).

---

include/linux/cgroup_subsys.h +6 -0
index e287745..4b12372 100644
@@ -48,3 +48,9 @@ SUBSYS(devices)
 #endif
 
 /* */
+
+#ifdef CONFIG_CGROUP_TC
+SUBSYS(tc)
+#endif
+
+/* */
include/linux/cgroup_tc.h new file +20 -0
new file mode 100644
index 0000000..e4ba6a1
@@ -0,0 +1,20 @@
+#ifndef __LINUX_CGROUP_TC_H
+#define __LINUX_CGROUP_TC_H
+
+/* Interface to obtain tasks cgroup identifier. */
+
+#include <linux/cgroup.h>
+#include <linux/skbuff.h>
+#include <net/sock.h>
+
+#ifdef CONFIG_CGROUP_TC
+
+void cgroup_tc_set_sock_classid(struct sock *sk);
+
+#else
+
+#define cgroup_tc_set_sock_classid(sk)
+
+#endif /* CONFIG_CGROUP_TC */
+
+#endif /* __LINUX_CGROUP_TC_H */
include/net/sock.h +3 -0
index dc42b44..7a4e09c 100644
@@ -271,6 +271,9 @@ struct sock {
 	int			sk_write_pending;
 	void			*sk_security;
 	__u32			sk_mark;
+#ifdef CONFIG_CGROUP_TC
+	__u32			sk_cgroup_classid;
+#endif
 	/* XXX 4 bytes hole on 64 bit */
 	void			(*sk_state_change)(struct sock *sk);
 	void			(*sk_data_ready)(struct sock *sk, int bytes);
init/Kconfig +11 -0
index 6135d07..c28fde8 100644
@@ -289,6 +289,17 @@ config CGROUP_DEBUG
 
 	  Say N if unsure
 
+config CGROUP_TC
+	bool "Traffic control cgroup subsystem"
+	depends on CGROUPS
+	default n
+	help
+	  This option enables a simple cgroup subsystem that
+	  allows network traffic to be classified based on the
+	  cgroup of the task originating the traffic.
+
+	  Say N if unsure
+
 config CGROUP_NS
         bool "Namespace cgroup subsystem"
         depends on CGROUPS
kernel/Makefile +1 -0
index 1c9938a..08b217b 100644
@@ -42,6 +42,7 @@ obj-$(CONFIG_CGROUPS) += cgroup.o
 obj-$(CONFIG_CGROUP_DEBUG) += cgroup_debug.o
 obj-$(CONFIG_CPUSETS) += cpuset.o
 obj-$(CONFIG_CGROUP_NS) += ns_cgroup.o
+obj-$(CONFIG_CGROUP_TC) += tc_cgroup.o
 obj-$(CONFIG_UTS_NS) += utsname.o
 obj-$(CONFIG_USER_NS) += user_namespace.o
 obj-$(CONFIG_PID_NS) += pid_namespace.o
kernel/tc_cgroup.c new file +105 -0

105 lines not on this page · raw message

net/socket.c +5 -0
index 66c4a8c..b7421ec 100644
@@ -93,6 +93,7 @@
 
 #include <net/sock.h>
 #include <linux/netfilter.h>
+#include <linux/cgroup_tc.h>
 
 static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
 static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
@@ -1170,6 +1171,8 @@ static int __sock_create(struct net *net, int family, int type, int protocol,
 	if (err < 0)
 		goto out_module_put;
 
+	cgroup_tc_set_sock_classid(sock->sk);
+
 	/*
 	 * Now to bump the refcnt of the [loadable] module that owns this
 	 * socket at sock_release time we decrement its refcnt.
@@ -1444,6 +1447,8 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
 	if (err < 0)
 		goto out_fd;
 
+	cgroup_tc_set_sock_classid(newsock->sk);
+
 	if (upeer_sockaddr) {
 		if (newsock->ops->getname(newsock, (struct sockaddr *)address,
 					  &len, 2) < 0) {
#1 lore raw

* Ranjit Manomohan <[email protected]> 2008-09-10 10:42

Quoted 16 lines
kernel/tc_cgroup.c new file +8 -0
+void cgroup_tc_set_sock_classid(struct sock *sk)
+{
+	if (sk)
+		sk->sk_cgroup_classid = cgroup_tc_classid(current);
+}
+
@@ -1170,6 +1171,8 @@ static int __sock_create(struct net *net, int family, int type, int protocol,
 	if (err < 0)
 		goto out_module_put;
 
+	cgroup_tc_set_sock_classid(sock->sk);
+
 	/*
 	 * Now to bump the refcnt of the [loadable] module that owns this
 	 * socket at sock_release time we decrement its refcnt.

kernel/tc_cgroup.c asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, · line 1454

Quoted 8 lines
kernel/tc_cgroup.c new file +2 -0
@@ -1444,6 +1447,8 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
 	if (err < 0)
 		goto out_fd;
 
+	cgroup_tc_set_sock_classid(newsock->sk);
+
 	if (upeer_sockaddr) {
 		if (newsock->ops->getname(newsock, (struct sockaddr *)address,
 					  &len, 2) < 0) {

The big disadvantage of this method is that it does not allow to change the classid for sockets which already exist. It inherits the classid at socket creation time and then sticks to it. So if you want to follow this approach I'd suggest to at least store a reference to the cgroup state and reference count it properly.

As for the locking that you mentioned in the other thread. IMHO it is not possible to lookup a socket without taking at least one lock, but I might be wrong there. Actually I think it will take even more locks as different locks are used to f.e. protect listening and established tcp sockets.

#2 lore raw
Quoted 20 lines · Thomas Graf

* Ranjit Manomohan <[email protected]> 2008-09-10 10:42

Quoted 19 lines
kernel/tc_cgroup.c new file +8 -0
+void cgroup_tc_set_sock_classid(struct sock *sk)
+{
+     if (sk)
+             sk->sk_cgroup_classid = cgroup_tc_classid(current);
+}
+
@@ -1170,6 +1171,8 @@ static int __sock_create(struct net *net, int family, int type, int protocol,
      if (err < 0)
              goto out_module_put;
 
+     cgroup_tc_set_sock_classid(sock->sk);
+
      /*
       * Now to bump the refcnt of the [loadable] module that owns this
       * socket at sock_release time we decrement its refcnt.
@@ -1444,6 +1447,8 @@ asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
      if (err < 0)
              goto out_fd;
 
Quoted 12 lines

net/socket.c asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, · line 1454

Quoted 5 lines
net/socket.c +2 -0
+     cgroup_tc_set_sock_classid(newsock->sk);
+
      if (upeer_sockaddr) {
              if (newsock->ops->getname(newsock, (struct sockaddr *)address,
                                        &len, 2) < 0) {

The big disadvantage of this method is that it does not allow to change the classid for sockets which already exist. It inherits the classid at socket creation time and then sticks to it. So if you want to follow this approach I'd suggest to at least store a reference to the cgroup state and reference count it properly.

I had considered adding support for moving tasks between cgroups by going through all the open fds of the task and updating the sockets (in the cgroup attach method). It is a very heavy weight operation and not considered a common use case (and even undesirable at times) so I dropped the idea. I can add it back in if it is considered essential.

Quoted 5 lines

As for the locking that you mentioned in the other thread. IMHO it is not possible to lookup a socket without taking at least one lock, but I might be wrong there. Actually I think it will take even more locks as different locks are used to f.e. protect listening and established tcp sockets.

That is correct for ingress, for egress the sk is already available in the skb so should be fine.

-Thanks,
Ranjit

Quoted 1 line
maintainer
#3 lore raw

From: "Ranjit Manomohan" <[email protected]>
Date: Wed, 10 Sep 2008 15:56:55 -0700

Quoted 2 lines

That is correct for ingress, for egress the sk is already available in the skb so should be fine.

That is not something you can rely upon, even for egress, %100 of the time.

Some forms of reallocation and mangling might decide to orphan the SKB and thus drop the skb->sk reference before you see the packet. And they are absolutely free to do this.

Just grep for skb_orphan().

Therefore, it is absolutely something you should not rely upon for correct operation.

Like I said from the beginning, Thomas's approach is the superior one.

#4 lore raw
Quoted 8 lines · David Miller

From: "Ranjit Manomohan" <[email protected]>
Date: Wed, 10 Sep 2008 15:56:55 -0700

Quoted 2 lines

That is correct for ingress, for egress the sk is already available in the skb so should be fine.

That is not something you can rely upon, even for egress, %100 of the time.

Quoted 8 lines

Some forms of reallocation and mangling might decide to orphan the SKB and thus drop the skb->sk reference before you see the packet. And they are absolutely free to do this.

Just grep for skb_orphan().

Therefore, it is absolutely something you should not rely upon for correct operation.

Thats fine and we do not rely on this. Those packets would just not be classified, the cgroup classid is only a hint and used when available (which is most of the time).

Quoted 2 lines

Like I said from the beginning, Thomas's approach is the superior one.

It would leave a lot of packets (like acks) unaccounted for and these do take up a significant portion of network packets transmitted on a high speed link). I am ok with Thomas' simpler approach too, just pointing out that it is not as accurate as the proposed alternative.

-Thanks,
Ranjit

Quoted 1 line
#5 lore raw
In reply to Ranjit Manomohan
Quoted 8 lines · Ranjit Manomohan
Quoted 7 lines

The big disadvantage of this method is that it does not allow to change the classid for sockets which already exist. It inherits the classid at socket creation time and then sticks to it. So if you want to follow this approach I'd suggest to at least store a reference to the cgroup state and reference count it properly.

Quoted 6 lines

I had considered adding support for moving tasks between cgroups by going through all the open fds of the task and updating the sockets (in the cgroup attach method). It is a very heavy weight operation and not considered a common use case (and even undesirable at times) so I dropped the idea. I can add it back in if it is considered essential.

That's a bit different from what Thomas is suggesting (I think).

There are three options:

a) socket acquires class id at creation time from its creator task or its parent socket. So the class id is fixed for the lifetime of the socket

b) socket acquires a reference to a cgroup at creation time, from its creator task or its parent socket. So the class id can be updated by changing the cgroup's class id, but the cgroup of the socket can't be changed. This can prevent the cgroup from being properly destroyed.

c) socket acquires a reference to a cgroup at creation time, and can be moved to a different cgroup when tasks that reference it move between cgroups.

Our patches use option a. Option c is too heavyweight IMO, and has vague semantics for exactly when movement should occur. Option b *could* be useful, if you wanted to be able to share class ids between cgroups, *and* shuffle the sharing relationships around on the fly. I think that Thomas is suggestion option b. I'm not sure that I see a concrete use case for it though - Thomas, what use cases did you see?

Paul

#6 lore raw

* Paul Menage <[email protected]> 2008-09-10 16:04

Quoted 17 lines

That's a bit different from what Thomas is suggesting (I think).

There are three options:

a) socket acquires class id at creation time from its creator task or its parent socket. So the class id is fixed for the lifetime of the socket

b) socket acquires a reference to a cgroup at creation time, from its creator task or its parent socket. So the class id can be updated by changing the cgroup's class id, but the cgroup of the socket can't be changed. This can prevent the cgroup from being properly destroyed.

c) socket acquires a reference to a cgroup at creation time, and can be moved to a different cgroup when tasks that reference it move between cgroups.

Quoted 5 lines

Our patches use option a. Option c is too heavyweight IMO, and has vague semantics for exactly when movement should occur. Option b *could* be useful, if you wanted to be able to share class ids between cgroups, *and* shuffle the sharing relationships around on the fly. I think that Thomas is suggestion option b.

That's right, I had option b) in mind.

Quoted 2 lines

I'm not sure that I see a
concrete use case for it though - Thomas, what use cases did you see?

Without a) this whole feature is very limited. It requires a process to be registered to the cgroup before it creates any sockets. Otherwise these sockets will not have the proper classid value and traffic from and to this sockets will not be classified. I don't see how this is practical since many applications create their sockets when the application is started. F.e. a web browser is causing a bulk data transfer, admin/user notices this and wants to put it in a restricted cgroup, won't work.

#7 lore raw
Quoted 2 lines · Thomas Graf

Without a) this whole feature is very limited. It requires a process to be registered to the cgroup before it creates any sockets.

I think that it's a bit more flexible than that - any newly created sockets (or newly accepted sockets) will inherit the correct classid, so it's only existing connections that don't get reclassified.

But the common case is that applications are going to be dropped in the correct cgroup *before* they start, via mechanisms such as pam login modules or job-control middleware.

Quoted 8 lines

Otherwise
these sockets will not have the proper classid value and traffic from and to this sockets will not be classified. I don't see how this is practical since many applications create their sockets when the application is started. F.e. a web browser is causing a bulk data transfer, admin/user notices this and wants to put it in a restricted cgroup, won't work.

Yes, for this particular case it doesn't work. But isn't it much more likely that the admin/user will know that web-browsers tend to trigger bulk data transfers and configure the system so that the web browser always starts in its own cgroup, rather than trying to jump on it after the fact?

Paul

#8 lore raw

* Paul Menage <[email protected]> 2008-09-10 16:31

Quoted 5 lines

Yes, for this particular case it doesn't work. But isn't it much more likely that the admin/user will know that web-browsers tend to trigger bulk data transfers and configure the system so that the web browser always starts in its own cgroup, rather than trying to jump on it after the fact?

That's argueable I guess. Likely or not, it doesn't make sense to add such restrictions if not required, especially not when it is trivial to just look at the cgroup of the task directly.

If the ingress case is of such importance, I'd propose to lookup the classid via skb -> socket -> task -> classid.

Anyways, I think we've listed all the pros and cons of either approach.

#9 lore raw
Quoted 4 lines · Thomas Graf

That's argueable I guess. Likely or not, it doesn't make sense to add such restrictions if not required, especially not when it is trivial to just look at the cgroup of the task directly.

Isn't a very large fraction of outgoing network traffic driven from net_tx_action(), which doesn't execute in the context of the sending process? Or am I missing something?

You claimed "In the most common case, the packet is not queued before it hits the qdisc so this takes away a lot of complexity and is very fast but should still be sufficient.". But is that really true on a system where the network is busy? (Which is after all exactly the case where you care about accurate traffic accounting/policing)

Paul

#10 lore raw

* Paul Menage <[email protected]> 2008-09-10 16:51

Quoted 9 lines · Thomas Graf
Quoted 4 lines

That's argueable I guess. Likely or not, it doesn't make sense to add such restrictions if not required, especially not when it is trivial to just look at the cgroup of the task directly.

Isn't a very large fraction of outgoing network traffic driven from net_tx_action(), which doesn't execute in the context of the sending process? Or am I missing something?

net_tx_action() dequeues from the qdisc. The classification is done before the packets is enqueued onto the qdisc.

A special case exists when the packets needs to be requeued, in this case the enqueue() is done in softirq context but it should not invoke the classifier again if I remember correctly.

Quoted 5 lines

You claimed "In the most common case, the packet is not queued before it hits the qdisc so this takes away a lot of complexity and is very fast but should still be sufficient.". But is that really true on a system where the network is busy? (Which is after all exactly the case where you care about accurate traffic accounting/policing)

To my best knowledge it is, otherwise I wouldn't say it. Feel free to prove me wrong.

F.e. it doesn't work if an ifb device is added to egress between the user and the cgroup classifier. That's what I mean with common cases.

#11 lore raw
Quoted 6 lines · Thomas Graf
Quoted 3 lines

Isn't a very large fraction of outgoing network traffic driven from net_tx_action(), which doesn't execute in the context of the sending process? Or am I missing something?

net_tx_action() dequeues from the qdisc. The classification is done before the packets is enqueued onto the qdisc.

Ah, good point.

Thanks,

Paul

#12 lore raw
In reply to Thomas Graf
Quoted 4 lines · Thomas Graf

That's argueable I guess. Likely or not, it doesn't make sense to add such restrictions if not required, especially not when it is trivial to just look at the cgroup of the task directly.

Isn't a very large fraction of outgoing network traffic driven from net_tx_action(), which doesn't execute in the context of the sending process? Or am I missing something?

You claimed "In the most common case, the packet is not queued before it hits the qdisc so this takes away a lot of complexity and is very fast but should still be sufficient.". But is that really true on a system where the network is busy? (Which is after all exactly the case where you care about accurate traffic accounting/policing)

Paul