ipv4: Do not cache routing failures due to disabled forwarding.
Subsystems (3)
[Resending to netdev instead of linux-netdev, sorry again.]
For whatever reason, I didn't receive your reply and I'm not subscribed to the list but I saw it on archives.
Quoted 11 lines
Two alternatives are possible:
1. set res.fi = NULL after '
no_route:' labelor better
2. set
do_cache= false after 'no_route:' label,
then instead of 'gotolocal_input;' jump to a new
label 'create_rt:' just beforert_dst_alloc.Not sure, they may generate less code in the fast path.
If I implement the first alternative, GCC will optimize it to the second. And it does not do that same optimization with my patch...
Will submit alternative 1 if there are no further issues/comments.
PATCH v2 1/1 ipv4: Do not cache routing failures due to disabled forwarding.
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $src
---
v2: simplify patch using julian anastasov's suggestion.
net/ipv4/route.c | 1 + 1 file changed, 1 insertion(+)
net/ipv4/route.c +1 -0
@@ -1798,6 +1798,7 @@ local_input: no_route:RT_CACHE_STAT_INC (in_no_route); res.type =RTN_UNREACHABLE ; + res.fi =NULL ; goto local_input;/*
Signature · 1 line
2.1.0
David Miller
maintainerFrom: Nicolas Cavallari <[email protected]>
Date: Mon, 15 Sep 2014 12:28:13 +0200
Quoted 6 lines
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
Quoted 7 lines
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $srcSigned-off-by Nicolas Cavallari <[email protected]>---
v2: simplify patch using julian anastasov's suggestion.
This also disables caching for the cases of a simple fib lookup failure.
Handle cached route invalidation the way it's meant to be, by bumping the rt_genid.
net/ipv4/devinet.c +2 -0
@@ -1965,6 +1965,8 @@ static void inet_forward_change(struct net *net) }rcu_read_unlock (); } + +rt_cache_flush (net); }static int devinet_conf_ifindex (struct net *net,struct ipv4_devconf *cnf)
Quoted 13 lines · David Miller
From: Nicolas Cavallari <[email protected]>
Date: Mon, 15 Sep 2014 12:28:13 +0200Quoted 10 lines
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $src
Quoted 5 lines
Quoted 3 lines
Signed-off-by Nicolas Cavallari <[email protected]>---
v2: simplify patch using julian anastasov's suggestion.This also disables caching for the cases of a simple fib lookup failure.
Caching is already not done on a fib lookup failure. On which fib_info would you cache anyway ? res.fi is already NULL in this case.
Quoted 8 lines
Handle cached route invalidation the way it's meant to be, by bumping the
rt_genid.net/ipv4/devinet.c +0
-0
net/ipv4/devinet.c static void inet_forward_change(struct net *net) · line 1972
Quoted 8 lines
net/ipv4/devinet.c +2
-0@@ -1965,6 +1965,8 @@ static void inet_forward_change(struct net *net) }rcu_read_unlock (); } + +rt_cache_flush (net); }static int devinet_conf_ifindex (struct net *net,struct ipv4_devconf *cnf)
This doesn't solve the problem. Flushing the cache only changes how the bug manifests:
ip route flush cache
ip route get 10.0.0.1 from 10.0.0.2 iif A
# unreachable (because forwarding on A is disabled). This is cached. ip route get 10.0.0.1 from 10.0.0.2 iif B # unreachable (at first fib_lookup finds it reachable and forwarding on B is enabled. And then there is a valid cache entry, which is reused blindly even if it says "unreachable").
ip route flush cache
ip route get 10.0.0.1 from 10.0.0.2 iif B
# reachable (because forwarding on B is enabled). This is cached ip route get 10.0.0.1 from 10.0.0.2 iif A # reachable (forwarding on A is disabled, so it is unreachable initially. But there is a valid cache entry, which says "reachable", which is reused)
Hello,
Quoted 11 lines · Nicolas Cavallari
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $srcSigned-off-by Nicolas Cavallari <[email protected]>
Looks good to me,
Still, I fail to see how the compiler optimizes
the jump, 'goto local_input' still jumps to res.fi check. I tried even likely() after 'local_input:" checks for res.fi and !itag but the 'if (res.fi) {' block is still moved below and reached with jnz. I expected likely() to prefer the res.fi != NULL path.
Quoted 20 lines
---
v2: simplify patch using julian anastasov's suggestion.net/ipv4/route.c | 1 + 1 file changed, 1 insertion(+)net/ipv4/route.c +1
-0@@ -1798,6 +1798,7 @@ local_input: no_route:RT_CACHE_STAT_INC (in_no_route); res.type =RTN_UNREACHABLE ; + res.fi =NULL ; goto local_input;/* Signature · 1 line
2.1.0
Regards
--
Julian Anastasov <[email protected]>
Quoted 19 lines · Julian Anastasov
Hello,
Quoted 11 lines · Nicolas Cavallari
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $srcSigned-off-by Nicolas Cavallari <[email protected]>Looks good to me,
Quoted 8 lines
Reviewed-by Julian Anastasov <[email protected]>Still, I fail to see how the compiler optimizes
the jump, 'gotolocal_input' still jumps to res.fi check. I tried evenlikely()after 'local_input:" checks for res.fi and !itag but the 'if (res.fi) {' block is still moved below and reached with jnz. I expectedlikely()to prefer the res.fi != NULL path.
Different compiler/arch/options perhaps ? I'm using Debian's GCC 4.9.1-11 on amd64 with a defconfig + CONFIG_DEBUG_INFO. It does this:
.LBE2727:
.loc 1 1800 0
movb $7, -102(%rbp) #, res.type
.loc 1 1801 0
movq $0, -96(%rbp) #, res.fi
xorl %ecx, %ecx # D.60248
.loc 1 1653 0
xorl %r12d, %r12d # flags
.LVL737:
.loc 1 1749 0
xorl %r15d, %r15d # do_cache
.LVL738:
jmp .L784 #where .L784 is :
.loc 1 1762 0
movl 324(%r11), %edi # in_dev_74->cnf.data,
xorl %esi, %esi # D.60250
.LVL601:
movq %r10, -128(%rbp) # skb, %sfp
testl %edi, %edi #
movq 264(%r14), %rdi # _75->loopback_dev, _75->loopback_dev
setne %sil #, D.60250
xorl %edx, %edx #.LVL602:
call rt_dst_alloc #
As for the res.fi and itag check in local_input, it jumps only when the test fails.
Hello,
Quoted 11 lines · Nicolas Cavallari
Quoted 7 lines · Julian Anastasov
Still, I fail to see how the compiler optimizes
the jump, 'gotolocal_input' still jumps to res.fi check. I tried evenlikely()after 'local_input:" checks for res.fi and !itag but the 'if (res.fi) {' block is still moved below and reached with jnz. I expectedlikely()to prefer the res.fi != NULL path.Different compiler/arch/options perhaps ? I'm using Debian's GCC 4.9.1-11 on amd64 with a defconfig +
CONFIG_DEBUG_INFO. It does this:
I see, new compilers can do better, I'm on x86 32-bit:
# rpm -qi gcc | grep -e Version -e Date Version : 4.8.3 Install Date: Sat 06 Sep 2014 05:56:15 PM EEST Build Date : Tue 24 Jun 2014 10:18:10 PM EEST
# make net/ipv4/route.s
---> local_input:
.L1031:
movl -56(%ebp), %edx # res.fi, D.55132
testl %edx, %edx # D.55132---> jump for likely(res.fi != NULL):
jne .L1219 #,
.L1095:
movb $0, -88(%ebp) #, %sfp
.L1059:
movzbl -88(%ebp), %eax # %sfp, D.55118
xorl %edx, %edx # D.55127
cmpl $0, 236(%edi) #, in_dev_129->cnf.data
pushl %eax # D.55118
movl init_net+172, %eax # init_net.loopback_dev,
setne %dl #, D.55127
xorl %ecx, %ecx #
call rt_dst_alloc #...
---> no_route:
.L1027:
movb $7, -62(%ebp) #, res.type
movl $0, -56(%ebp) #, res.fi#APP
# 1773 "net/ipv4/route.c" 1
incl rt_cache_stat+8 # rt_cache_stat.in_no_route
# 0 "" 2
#NO_APP
.L1214:
movl $0, -92(%ebp) #, %sfp
---> jump to local_input:
jmp .L1031 #
Regards
--
Julian Anastasov <[email protected]>
Quoted 51 lines · Nicolas Cavallari
Quoted 13 lines · David Miller
From: Nicolas Cavallari <[email protected]>
Date: Mon, 15 Sep 2014 12:28:13 +0200Quoted 10 lines
If we cache them, the kernel will reuse them, independently of whether forwarding is enabled or not. Which means that if forwarding is disabled on the input interface where the first routing request comes from, then that unreachable result will be cached and reused for other interfaces, even if forwarding is enabled on them.
This can be verified with two interfaces A and B and an output interface C, where B has forwarding enabled, but not A and trying
ip route get $dst iif A from $src && ip route get $dst iif B from $srcQuoted 5 lines
Quoted 3 lines
Signed-off-by Nicolas Cavallari <[email protected]>---
v2: simplify patch using julian anastasov's suggestion.This also disables caching for the cases of a simple fib lookup failure.
Caching is already not done on a fib lookup failure. On which
fib_infowould you cache anyway ? res.fi is already NULL in this case.Quoted 8 lines
Handle cached route invalidation the way it's meant to be, by bumping the
rt_genid.net/ipv4/devinet.c +0
-0net/ipv4/devinet.c static void inet_forward_change(struct net *net) · line 1972
Quoted 8 lines
net/ipv4/devinet.c +2
-0@@ -1965,6 +1965,8 @@ static void inet_forward_change(struct net *net) }rcu_read_unlock (); } + +rt_cache_flush (net); }static int devinet_conf_ifindex (struct net *net,struct ipv4_devconf *cnf)This doesn't solve the problem. Flushing the cache only changes how the bug manifests:
ip route flush cache
ip route get 10.0.0.1 from 10.0.0.2 iif A# unreachable (because forwarding on A is disabled). This is cached. ip route get 10.0.0.1 from 10.0.0.2 iif B # unreachable (at first fib_lookup finds it reachable and forwarding on B is enabled. And then there is a valid cache entry, which is reused blindly even if it says "unreachable").
Quoted 7 lines
ip route flush cache
ip route get 10.0.0.1 from 10.0.0.2 iif B# reachable (because forwarding on B is enabled). This is cached ip route get 10.0.0.1 from 10.0.0.2 iif A # reachable (forwarding on A is disabled, so it is unreachable initially. But there is a valid cache entry, which says "reachable", which is reused)
Any news on this patch ? Is there anything I can do so that this bug is finally fixed ?
David Miller
maintainerFrom: Nicolas Cavallari <[email protected]>
Date: Tue, 23 Sep 2014 10:34:08 +0200
Quoted 2 lines
Any news on this patch ? Is there anything I can do so that this bug is finally fixed ?
Getting back to this is on my TODO list, I haven't forgotten about it, thanks :)