Skip to contentSkip to conversation

netns: correctly use per-netns ipv4 sysctl_tcp_mem

PATCH In review Huang Qiang 5 participants 6 messages Jul 12, 2012 – Jul 19, 2012 2 messages not in the archive

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

PATCH v2 1/1 netns: correctly use per-netns ipv4 sysctl_tcp_mem

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash [root@net-test socket]# ip route add default via 192.168.58.2 [root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem [root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code) always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.

---
 include/net/sock.h |   24 ++++++++++++++++--------
 1 files changed, 16 insertions(+), 8 deletions(-)
include/net/sock.h +16 -8
index 4a45216..ee85d8b 100644
@@ -59,6 +59,7 @@
 #include <linux/static_key.h>
 #include <linux/aio.h>
 #include <linux/sched.h>
+#include <linux/in.h>
 
 #include <linux/filter.h>
 #include <linux/rculist_nulls.h>
@@ -1060,14 +1061,6 @@ static inline void sk_enter_memory_pressure(struct sock *sk)
 	sk->sk_prot->enter_memory_pressure(sk);
 }
 
-static inline long sk_prot_mem_limits(const struct sock *sk, int index)
-{
-	long *prot = sk->sk_prot->sysctl_mem;
-	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
-		prot = sk->sk_cgrp->sysctl_mem;
-	return prot[index];
-}
-
 static inline void memcg_memory_allocated_add(struct cg_proto *prot,
 					      unsigned long amt,
 					      int *parent_status)
@@ -2150,6 +2143,21 @@ static inline void sk_change_net(struct sock *sk, struct net *net)
 	sock_net_set(sk, hold_net(net));
 }
 
+static inline long sk_prot_mem_limits(const struct sock *sk, int index)
+{
+	long *prot = sk->sk_prot->sysctl_mem;
+
+	if (sk->sk_protocol == IPPROTO_TCP) {
+		struct net *net = sock_net(sk);
+		prot = net->ipv4.sysctl_tcp_mem;
+	}
+
+	if (mem_cgroup_sockets_enabled && sk->sk_cgrp)
+		prot = sk->sk_cgrp->sysctl_mem;
+
+	return prot[index];
+}
+
 static inline struct sock *skb_steal_sock(struct sk_buff *skb)
 {
 	if (unlikely(skb->sk)) {
Signature · 1 line
1.7.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 12 lines · Huang Qiang

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash
[root@net-test socket]# ip route add default via 192.168.58.2
[root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem
[root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

Quoted 7 lines

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code)

s/protocal/protocol/

Quoted 7 lines

always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.

#3 lore raw
In reply to a message not in the archive

From: Huang Qiang <[email protected]>
Date: Thu, 12 Jul 2012 17:33:34 +0800

Quoted 18 lines

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash
[root@net-test socket]# ip route add default via 192.168.58.2
[root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem
[root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

Quoted 8 lines

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code) always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.

This patch doesn't apply cleanly to net-next, please respin it.

Thank you.

A message that is not in the archive (sent off-list or not yet received): 20120718.094137.2279894799753732191.davem-fT/PcQaiUtIeIZ0/[email protected]

#5 lore raw
Quoted 27 lines · David Miller

From: Huang Qiang <[email protected]>
Date: Thu, 12 Jul 2012 17:33:34 +0800

Quoted 24 lines

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash
[root@net-test socket]# ip route add default via 192.168.58.2
[root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem
[root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code) always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.

Quoted 6 lines
Quoted 2 lines

This patch doesn't apply cleanly to net-next, please respin it.

Thank you.

Ok, I will send patch v3 for net-next. Thank you!

#6 lore raw
In reply to David Miller
Quoted 27 lines · David Miller

From: Huang Qiang <[email protected]>
Date: Thu, 12 Jul 2012 17:33:34 +0800

Quoted 24 lines

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash [root@net-test socket]# ip route add default via 192.168.58.2 [root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem [root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code) always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.

Quoted 6 lines
Quoted 2 lines

This patch doesn't apply cleanly to net-next, please respin it.

Thank you.

Ok, I will send patch v3 for net-next. Thank you!

#7 lore raw
In reply to Huang Qiang
Quoted 12 lines · Huang Qiang

From: Yang Zhenzhang <[email protected]>

Now, kernel allows each net namespace to independently set up its levels for tcp memory pressure thresholds.

But it seems there is a bug, as using the following steps:

[root@host socket]# lxc-start -n test -f config /bin/bash [root@net-test socket]# ip route add default via 192.168.58.2 [root@net-test socket]# echo 0 0 0 > /proc/sys/net/ipv4/tcp_mem [root@net-test socket]# scp [email protected]:/home/tcp_mem_test .

Quoted 7 lines

and it still can transport the "tcp_mem_test" file which we hope it would not.

It's because inet_init() (net/ipv4/af_inet.c)initialize the tcp_prot.sysctl_mem:
tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;

So when the protocal is TCP, sk->sk_prot->sysctl_mem(following code)

s/protocal/protocol/

Quoted 7 lines

always use the ipv4 sysctl_tcp_mem of init_net namespace rather than it's own net namespace.
This patch simply set "prot" equal to net->ipv4.sysctl_tcp_mem when the protocol type is TCP.