ipvs: Netfilter connection tracking changes
Subsystems (4)
Julian Anastasov
maintainerRFC PATCH 1/1 ipvs: Netfilter connection tracking changes
Add more code to IPVS to work with Netfilter connection
tracking and fix some problems.
- Allow IPVS to be compiled without connection tracking as in 2.6.35 and before. This can avoid keeping conntracks for all IPVS connections because this costs memory. ip_vs_ftp still depends on connection tracking and NAT as implemented for 2.6.36.
- Add sysctl var "conntrack" to enable connection tracking for all IPVS connections. For loaded IPVS directors it needs tuning of nf_conntrack_max limit.
- Add IP_VS_CONN_F_NFCT connection flag to request the connection to use connection tracking. This allows user space to provide this flag, for example, in dest->conn_flags. This can be useful to request connection tracking per real server instead of forcing it for all connections with the "conntrack" sysctl. This flag is set currently only by ip_vs_ftp and of course by "conntrack" sysctl.
- Add ip_vs_nfct.c file to hold all connection tracking code, by this way main code should not depend of netfilter conntrack support.
- Return back the ip_vs_post_routing handler as in 2.6.35 and use skb->ipvs_property=1 to allow IPVS to work without connection tracking
- new sysctl flag "snat_reroute". Recent kernels use ip_route_me_harder() to route LVS-NAT responses properly by VIP when there are multiple paths to client. But setups that do not have alternative default routes can skip this routing lookup by using snat_reroute=0.
Connection tracking:
- most of the code is already in 2.6.36-rc
- alter conntrack reply tuple for LVS-NAT connections when first packet from client is forwarded and conntrack state is NEW or RELATED. Additionally, alter reply for RELATED connections from real server, again for packet in original direction.
- add IP_VS_XMIT_TUNNEL to confirm conntrack (without altering reply) for LVS-TUN early because we want to call nf_reset. It is needed because we add IPIP header and we want the original conntrack to be preserved, not destroyed. The transmitted IPIP packets can reuse same conntrack, so we do not set skb->ipvs_property
- try to destroy conntrack when the IPVS connection is destroyed. It is not fatal if conntrack disappears before that, it depends on the used timers.
Fix problems from long time:
- add skb->ip_summed = CHECKSUM_NONE for the LVS-TUN transmitters
Fix problems only in 2.6.36-rc:
- when altering PASV response in ip_vs_ftp we should check with nfct_nat(ct) if NAT is enabled for conntrack before calling nf_nat_mangle_tcp_packet. This avoids possible crash when iptable_nat is not loaded.
- do not create expectation in ip_vs_ftp when PORT command is forwarded, leave it to nf_conntrack_ftp.c:help(). This avoids packet drops because nf_ct_expect_related() fails in help().
---
Simon, this patch is against 2.6.36-rc2, I tested it
for IPv4. In fact, it is ready for inclusion in net-next but I'm not sure if it applies properly. After first comments we can move the discussion to netdev. The snat_reroute part can be separated, if needed at all. Also, IP_VS_CONN_F_NFCT can be added to ipvsadm as real server option. The two problems in ip_vs_ftp can be also fixed in next 2.6.36-rc kernels. May be Hannes Eder can comment if he can see these problems. But I worry that this full patch is needed for 2.6.36 because not all systems use netfilter conntrack and are not tuned for many conntracks. Now 2.6.36 can come as surprise to them because IPVS requires Netfilter connection tracking support.
include/linux/ip_vs.h renamed from linux/include/linux/ip_vs.h +1 -0
@@ -87,6 +87,7 @@#define IP_VS_CONN_F_NO_CPORT 0x0800/* no client port set yet */ #define IP_VS_CONN_F_TEMPLATE 0x1000/* template, not connection */ #define IP_VS_CONN_F_ONE_PACKET 0x2000/* forward only one packet */ +#define IP_VS_CONN_F_NFCT 0x4000/* use netfilter conntrack */ #define IP_VS_SCHEDNAME_MAXLEN 16#define IP_VS_IFNAME_MAXLEN 16
include/net/ip_vs.h renamed from linux/include/net/ip_vs.h +48 -1
67 lines not on this page · raw message
net/netfilter/ipvs/Kconfig renamed from linux/net/netfilter/ipvs/Kconfig +11 -2
@@ -3,7 +3,7 @@ # menuconfig IP_VS tristate "IP virtual server support" - depends on NET && INET && NETFILTER && NF_CONNTRACK + depends on NET && INET && NETFILTER ---help--- IP Virtual Server support will let you build a high-performance virtual server based on cluster of two or more real servers. This
@@ -235,7 +235,8 @@ comment 'IPVS application helper' config IP_VS_FTP tristate "FTP protocol helper" - depends on IP_VS_PROTO_TCP && NF_NAT + depends on IP_VS_PROTO_TCP && NF_CONNTRACK && NF_NAT + select IP_VS_NFCT ---help--- FTP is a protocol that transfers IP address and/or port number in the payload. In the virtual server via Network Address Translation,
@@ -247,4 +248,12 @@ config IP_VS_FTP If you want to compile it in kernel, say Y. To compile it as a module, choose M here. If unsure, say N. +config IP_VS_NFCT + bool "Netfilter connection tracking" + depends on NF_CONNTRACK + ---help--- + The Netfilter connection tracking support allows the IPVS + connection state to be exported to the Netfilter framework + for filtering purposes. + endif # IP_VS
net/netfilter/ipvs/Makefile renamed from linux/net/netfilter/ipvs/Makefile +4 -1
@@ -9,10 +9,13 @@ ip_vs_proto-objs-$(CONFIG_IP_VS_PROTO_UD ip_vs_proto-objs-$(CONFIG_IP_VS_PROTO_AH_ESP) += ip_vs_proto_ah_esp.o ip_vs_proto-objs-$(CONFIG_IP_VS_PROTO_SCTP) += ip_vs_proto_sctp.o +ip_vs-extra_objs-y := +ip_vs-extra_objs-$(CONFIG_IP_VS_NFCT) += ip_vs_nfct.o + ip_vs-objs := ip_vs_conn.o ip_vs_core.o ip_vs_ctl.o ip_vs_sched.o \ ip_vs_xmit.o ip_vs_app.o ip_vs_sync.o \ ip_vs_est.o ip_vs_proto.o \ - $(ip_vs_proto-objs-y) + $(ip_vs_proto-objs-y) $(ip_vs-extra_objs-y) # IPVS core
net/netfilter/ipvs/ip_vs_conn.c renamed from linux/net/netfilter/ipvs/ip_vs_conn.c +13 -0
@@ -717,6 +717,9 @@ static void ip_vs_conn_expire(unsigned lif (cp->control)ip_vs_control_del (cp); +if (cp->flags &IP_VS_CONN_F_NFCT ) +ip_vs_conn_drop_conntrack (cp); +if (unlikely (cp->app !=NULL ))ip_vs_unbind_app (cp);ip_vs_unbind_dest (cp);
@@ -812,6 +815,16 @@ ip_vs_conn_new(int af, int proto, constif (unlikely (pp &&atomic_read (&pp->appcnt)))ip_vs_bind_app (cp, pp); +/* +* Allow conntrack to be preserved. By default, conntrack +* is created and destroyed for every packet. +* Sometimes keeping conntrack can be useful for +* IP_VS_CONN_F_ONE_PACKET too. +*/ + +if (ip_vs_conntrack_enabled ()) + cp->flags |=IP_VS_CONN_F_NFCT ; +/* Hash it in the ip_vs_conn_tab finally */ ip_vs_conn_hash (cp);
net/netfilter/ipvs/ip_vs_core.c renamed from linux/net/netfilter/ipvs/ip_vs_core.c +72 -11
147 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_ctl.c renamed from linux/net/netfilter/ipvs/ip_vs_ctl.c +20 -0
@@ -88,6 +88,10 @@ int sysctl_ip_vs_expire_nodest_conn = 0;int sysctl_ip_vs_expire_quiescent_template =0 ;int sysctl_ip_vs_sync_threshold[2 ] = {3 ,50 };int sysctl_ip_vs_nat_icmp_send =0 ; +int sysctl_ip_vs_snat_reroute =1 ; +#ifdef CONFIG_IP_VS_NFCT +int sysctl_ip_vs_conntrack; +#endif #ifdef CONFIG_IP_VS_DEBUG
@@ -1579,6 +1583,15 @@ static struct ctl_table vs_vars[] = { .mode = 0644, .proc_handler = proc_do_defense_mode, }, +#ifdef CONFIG_IP_VS_NFCT + { + .procname = "conntrack", + .data = &sysctl_ip_vs_conntrack, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif { .procname = "secure_tcp", .data = &sysctl_ip_vs_secure_tcp,
@@ -1586,6 +1599,13 @@ static struct ctl_table vs_vars[] = { .mode = 0644, .proc_handler = proc_do_defense_mode, }, + { + .procname = "snat_reroute", + .data = &sysctl_ip_vs_snat_reroute, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, #if 0 { .procname = "timeout_established",
net/netfilter/ipvs/ip_vs_ftp.c renamed from linux/net/netfilter/ipvs/ip_vs_ftp.c +19 -145
230 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_nfct.c renamed from linux/net/netfilter/ipvs/ip_vs_nfct.c +292 -0
292 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_xmit.c renamed from linux/net/netfilter/ipvs/ip_vs_xmit.c +49 -43
204 lines not on this page · raw message
Simon Horman
maintainerHi Julian,
Quoted 67 lines · Julian Anastasov
Add more code to IPVS to work with Netfilter connection
tracking and fix some problems.- Allow IPVS to be compiled without connection tracking as in 2.6.35 and before. This can avoid keeping conntracks for all IPVS connections because this costs memory.
ip_vs_ftpstill depends on connection tracking and NAT as implemented for 2.6.36.- Add sysctl var "conntrack" to enable connection tracking for all IPVS connections. For loaded IPVS directors it needs tuning of
nf_conntrack_maxlimit.- Add
IP_VS_CONN_F_NFCTconnection flag to request the connection to use connection tracking. This allows user space to provide this flag, for example, indest->conn_flags. This can be useful to request connection tracking per real server instead of forcing it for all connections with the "conntrack" sysctl. This flag is set currently only byip_vs_ftpand of course by "conntrack" sysctl.- Add
ip_vs_nfct.cfile to hold all connection tracking code, by this way main code should not depend of netfilter conntrack support.- Return back the
ip_vs_post_routinghandler as in 2.6.35 and use skb->ipvs_property=1 to allow IPVS to work without connection tracking- new sysctl flag "
snat_reroute". Recent kernels useip_route_me_harder()to route LVS-NAT responses properly by VIP when there are multiple paths to client. But setups that do not have alternative default routes can skip this routing lookup by using snat_reroute=0.Connection tracking:
- most of the code is already in 2.6.36-rc
- alter conntrack reply tuple for LVS-NAT connections when first packet from client is forwarded and conntrack state is NEW or RELATED. Additionally, alter reply for RELATED connections from real server, again for packet in original direction.
- add
IP_VS_XMIT_TUNNELto confirm conntrack (without altering reply) for LVS-TUN early because we want to callnf_reset. It is needed because we add IPIP header and we want the original conntrack to be preserved, not destroyed. The transmitted IPIP packets can reuse same conntrack, so we do not setskb->ipvs_property- try to destroy conntrack when the IPVS connection is destroyed. It is not fatal if conntrack disappears before that, it depends on the used timers.
Fix problems from long time:
- add
skb->ip_summed=CHECKSUM_NONEfor the LVS-TUN transmittersFix problems only in 2.6.36-rc:
- when altering PASV response in
ip_vs_ftpwe should check withnfct_nat(ct)if NAT is enabled for conntrack before callingnf_nat_mangle_tcp_packet. This avoids possible crash wheniptable_natis not loaded.- do not create expectation in
ip_vs_ftpwhen PORT command is forwarded, leave it to nf_conntrack_ftp.c:help(). This avoids packet drops becausenf_ct_expect_related()fails inhelp().
Quoted 6 lines
Signed-off-by Julian Anastasov <[email protected]>---
Simon, this patch is against 2.6.36-rc2, I tested it
for IPv4. In fact, it is ready for inclusion in net-next but I'm not sure if it applies properly.
I can fix that if you prefer.
Quoted 3 lines
After first comments we can
move the discussion to netdev. Thesnat_reroutepart can be separated, if needed at all.
I'd prefer a separate patch.
Quoted 2 lines
Also,
IP_VS_CONN_F_NFCTcan be
added to ipvsadm as real server option.
I can handle ipvsadm patches these days.
I can even make the patch if you'd prefer.
Quoted 2 lines
The two problems
inip_vs_ftpcan be also fixed in next 2.6.36-rc kernels.
Yes, as they are regressions since 2.6.35 please break those changes out into one or two separate patches so they can be submitted for inclusion in 2.6.36.
Quoted 6 lines
May be Hannes Eder can comment if he can see these problems. But I worry that this full patch is needed for 2.6.36 because not all systems use netfilter conntrack and are not tuned for many conntracks. Now 2.6.36 can come as surprise to them because IPVS requires Netfilter connection tracking support.
Unfortunately I think its too late to get the bulk of this into 2.6.36. Are you concerned about a performance problem? Even when Hannes's new features aren't used? Is the snat_reroute flag a solution/workaround? If so we could try to get that into 2.6.36, although I'm not sure how well it would be received.
Julian Anastasov
maintainerHello,
Quoted 5 lines · Simon Horman
Quoted 3 lines
Simon, this patch is against 2.6.36-rc2, I tested it
for IPv4. In fact, it is ready for inclusion in net-next but I'm not sure if it applies properly.I can fix that if you prefer.
Yes, please. Check if it applies and fix any conflicts.
But let's first see what changes to what kernels should go and what flags will be used, see below.
Quoted 5 lines
Quoted 3 lines
After first comments we can
move the discussion to netdev. Thesnat_reroutepart can be separated, if needed at all.I'd prefer a separate patch.
Yes, I'll do it
Quoted 5 lines
Quoted 2 lines
Also,
IP_VS_CONN_F_NFCTcan be
added to ipvsadm as real server option.I can handle ipvsadm patches these days.
I can even make the patch if you'd prefer.
Yes, please. I worry that flags in struct ip_vs_conn
are u16, we are reaching the limit. In real server it is 4 bytes, in struct ip_vs_dest conn_flags is atomic_t, I'm not sure how many bits we can use there. The problem is that struct ip_vs_sync_conn has 16-bit flags and we can not change it. May be we should be more careful about flags that are useful for sync. Others should not be added in cp->flags, may be in cp->local_flags.
For example, we should decide where to put IP_VS_CONN_F_NFCT.
For now, IP_VS_CONN_F_HASHED can be excluded and this bit can be used for other flag that needs syncing. But such change for IP_VS_CONN_F_HASHED is incompatible (the flag is ignored in backup server), so this flag should be occupied when all other 15 bits are occupied.
Even IP_VS_CONN_F_NFCT does not need to be sync-ed
because the backup server can use its own flags. It can use "conntrack" flag, real server flags, even ip_vs_ftp enables conntracking for its own connections. So, I think, IP_VS_CONN_F_NFCT can go to cp->local_flags. Or better, first 16 bits to be used for sync, other local flags to be added from 16 above (after changing cp->flags to 4 bytes). This will not change the way we get flags from user space.
Quoted 6 lines
Quoted 2 lines
The two problems
inip_vs_ftpcan be also fixed in next 2.6.36-rc kernels.Yes, as they are regressions since 2.6.35 please break those changes out into one or two separate patches so they can be submitted for inclusion in 2.6.36.
OK
Quoted 7 lines
Quoted 6 lines
May be Hannes Eder can comment if he can see these problems. But I worry that this full patch is needed for 2.6.36 because not all systems use netfilter conntrack and are not tuned for many conntracks. Now 2.6.36 can come as surprise to them because IPVS requires Netfilter connection tracking support.
Quoted 5 lines
Unfortunately I think its too late to get the bulk of this into 2.6.36. Are you concerned about a performance problem? Even when Hannes's new features aren't used? Is the
snat_rerouteflag a solution/workaround? If so we could try to get that into 2.6.36, although I'm not sure how well it would be received.
snat_reroute is not related to NFCT, it helps only
for performance. It is for 2.6.37.
Hannes's new features bring connection tracking even
for LVS-DR, not only for FTP. For some setups with large nf_conntrack_max the difference should be only in performance. And of course, IPVS is 160 bytes and conntrack is 200 bytes, the space in memory for connections is decreased more than twice. Only tests can prove if performance is increased when conntracks are kept or when destroyed for every connection. I can not test the performance difference properly in my test setup. For some systems the increased memory for conntracks can play role too.
Here is the picture:
Connection tracking for LVS-DR, LVS-TUN:
2.6.35: No conntracks (destroyed for every packet) Hannes 2.6.36-rc: Yes, conntrack enforced for all conns,
conntracks stay in SYN_RECV state if forward_shared is not used
My patch: conntrack is optional, depends on sysctl.
If disabled, conntracks are destroyed for every packet.
Connection tracking for LVS-NAT, ip_vs_ftp for example:
2.6.35: No conntracks (destroyed for every packet) Hannes 2.6.36-rc: Yes, conntracks in correct state My patch: Yes, conntracks in correct state. If
sysctl "conntrack" is not set, only ip_vs_ftp conns use connection tracking, all other LVS-NAT and other methods do not keep conntracks.
BTW, I didn't tested connection tracking support
combined with NAT rules, I don't know what kind of NAT some users apply for the IPVS connections. My tests were only for -m state matching, FTP connections:
- LVS-NAT (Local Process too)
- LVS-DR
- LVS-TUN
Regards
--
Julian Anastasov <[email protected]>