IPVS optimizations
Subsystems (5)
Julian Anastasov
maintainercover IPVS optimizations
This is a first patchset for IPVS optimizations.
Another patchset will address the locking in schedulers and moving the global _bh disabling from LOCAL_OUT to all locks. It is in TODO list.
All patches are for net-next. The idea is
after discussion and review Simon to apply the patchset to ipvs-next tree.
The changes in this patchset eliminate locks
and dst refcnt operations from packet processing by using RCU. There are more details in the patches.
Julian Anastasov (12):
net: add dst_get_noref and refdst_ptr helpers
ipvs: avoid routing by TOS for real server
ipvs: prefer NETDEV_DOWN event to free cached dsts
ipvs: convert the IP_VS_XMIT macros to functions
ipvs: rename functions related to dst_cache reset
ipvs: optimize dst usage for real server
ipvs: convert app locks
ipvs: remove rs_lock by using RCU
ipvs: convert locks used in persistence engines
ipvs: convert connection locking
ipvs: reorder keys in connection structure
ipvs: avoid kmem_cache_zalloc in ip_vs_conn_new
include/net/dst.h | 14 +- include/net/ip_vs.h | 56 +++-- net/netfilter/ipvs/ip_vs_app.c | 27 ++- net/netfilter/ipvs/ip_vs_conn.c | 254 +++++++++------- net/netfilter/ipvs/ip_vs_core.c | 16 +- net/netfilter/ipvs/ip_vs_ctl.c | 128 ++++---- net/netfilter/ipvs/ip_vs_ftp.c | 2 + net/netfilter/ipvs/ip_vs_pe.c | 43 +-- net/netfilter/ipvs/ip_vs_pe_sip.c | 1 + net/netfilter/ipvs/ip_vs_proto_sctp.c | 18 +- net/netfilter/ipvs/ip_vs_proto_tcp.c | 18 +- net/netfilter/ipvs/ip_vs_proto_udp.c | 19 +- net/netfilter/ipvs/ip_vs_xmit.c | 532 ++++++++++++++++++++------------- 13 files changed, 633 insertions(+), 495 deletions(-)
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 1/12 net: add dst_get_noref and refdst_ptr helpers
Needed to hide refdst details.
--- include/net/dst.h | 14 +++++++++++++- 1 files changed, 13 insertions(+), 1 deletions(-)
include/net/dst.h +13 -1
@@ -264,10 +264,22 @@ static inline struct dst_entry *dst_clone(struct dst_entry *dst)extern void dst_release (struct dst_entry *dst); +/* Get noref version of dst */ +static inline struct dst_entry *dst_get_noref (struct dst_entry *dst) +{ +return (struct dst_entry *) ((unsigned long ) dst |SKB_DST_NOREF ); +} + +/* Return dst pointer from refdst */ +static inline struct dst_entry *refdst_ptr (unsigned long refdst) +{ +return (struct dst_entry *)(refdst &SKB_DST_PTRMASK ); +} +static inline void refdst_drop (unsigned long refdst) {if (!(refdst &SKB_DST_NOREF )) -dst_release ((struct dst_entry *)(refdst &SKB_DST_PTRMASK )); +dst_release (refdst_ptr (refdst)); }/**
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 2/12 ipvs: avoid routing by TOS for real server
In reply to Julian Anastasov Avoid replacing the cached route for real server
on every packet with different TOS. I doubt that routing by TOS for real server is used at all, so we should be better with such optimization.
--- include/net/ip_vs.h | 1 - net/netfilter/ipvs/ip_vs_xmit.c | 58 +++++++++++++++++---------------------- 2 files changed, 25 insertions(+), 34 deletions(-)
include/net/ip_vs.h +0 -1
@@ -753,7 +753,6 @@ struct ip_vs_dest {/* for destination cache */ spinlock_t dst_lock;/* lock of dst_cache */ struct dst_entry *dst_cache;/* destination cache entry */ -u32 dst_rtos;/* RT_TOS(tos) for dst */ u32 dst_cookie;union nf_inet_addr dst_saddr;
net/netfilter/ipvs/ip_vs_xmit.c +25 -33
169 lines not on this page · raw message
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 3/12 ipvs: prefer NETDEV_DOWN event to free cached dsts
In reply to Julian Anastasov The real server becomes unreachable on down event,
no need to wait device unregistration. Should help in releasing dsts early before dst->dev is replaced with lo.
--- net/netfilter/ipvs/ip_vs_ctl.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-)
net/netfilter/ipvs/ip_vs_ctl.c +3 -5
@@ -1512,10 +1512,8 @@ __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)spin_unlock_bh (&dest->dst_lock); } -/* -* Netdev event receiver -* Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to -* a device that is "unregister" it must be released. +/* Netdev event receiver +* Currently only NETDEV_DOWN is handled to release refs to cached dsts */ static int ip_vs_dst_event (struct notifier_block *this,unsigned long event,void *ptr)
@@ -1527,7 +1525,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,struct ip_vs_dest *dest;unsigned int idx; -if (event !=NETDEV_UNREGISTER || !ipvs) +if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name);EnterFunction (2 );
Signature · 1 line
1.7.3.4
Re: [PATCH net-next 03/12] ipvs: prefer NETDEV_DOWN event to free cached dsts
Hi Julian
Quoted 3 lines · Julian Anastasov
The real server becomes unreachable on down event,
no need to wait device unregistration. Should help in releasing dsts early beforedst->devis replaced with lo.
Have you test this in a network namespace ? i.e. kill the namespace with heave traffic through it
From what I remember this was a tricky area...
I have some test cases for this, should I run them ?
Quoted 23 lines
Signed-off-by Julian Anastasov <[email protected]>--- net/netfilter/ipvs/ip_vs_ctl.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-)net/netfilter/ipvs/ip_vs_ctl.c +2
-4@@ -1512,10 +1512,8 @@ __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)spin_unlock_bh (&dest->dst_lock); } -/* -* Netdev event receiver -* Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to -* a device that is "unregister" it must be released. +/* Netdev event receiver +* Currently only NETDEV_DOWN is handled to release refs to cached dsts */ static int ip_vs_dst_event (struct notifier_block *this,unsigned long event,void *ptr)
net/netfilter/ipvs/ip_vs_ctl.c static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, · line 1531
Quoted 8 lines
net/netfilter/ipvs/ip_vs_ctl.c +1
-1@@ -1527,7 +1525,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,struct ip_vs_dest *dest;unsigned int idx; -if (event !=NETDEV_UNREGISTER || !ipvs) +if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name);EnterFunction (2 );
Regards
Hans
Julian Anastasov
maintainerRe: [PATCH net-next 03/12] ipvs: prefer NETDEV_DOWN event to free cached dsts
Hello,
Quoted 9 lines · Hans Schillstrom
Hi Julian
Quoted 3 lines · Julian Anastasov
The real server becomes unreachable on down event,
no need to wait device unregistration. Should help in releasing dsts early beforedst->devis replaced with lo.Have you test this in a network namespace ? i.e. kill the namespace with heave traffic through it
This should not be a problem. Even without ns exit
situation, we can have a case where device goes down, the output routes will start to fail and we will not cache route anymore. It can happen while the input device floods us with requests.
Quoted 3 lines
From what I remember this was a tricky area...
I have some test cases for this, should I run them ?
If it is easy...
Quoted 26 lines
Quoted 26 lines
Signed-off-by Julian Anastasov <[email protected]>--- net/netfilter/ipvs/ip_vs_ctl.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-)net/netfilter/ipvs/ip_vs_ctl.c +2
-4@@ -1512,10 +1512,8 @@ __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)spin_unlock_bh (&dest->dst_lock); } -/* -* Netdev event receiver -* Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to -* a device that is "unregister" it must be released. +/* Netdev event receiver +* Currently only NETDEV_DOWN is handled to release refs to cached dsts */ static int ip_vs_dst_event (struct notifier_block *this,unsigned long event,void *ptr)@@ -1527,7 +1525,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,struct ip_vs_dest *dest;unsigned int idx;
Quoted 9 lines
net/netfilter/ipvs/ip_vs_ctl.c static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, · line 1531
Quoted 5 lines
net/netfilter/ipvs/ip_vs_ctl.c +1
-1-if (event !=NETDEV_UNREGISTER || !ipvs) +if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name);EnterFunction (2 );Regards
Hans
Regards
--
Julian Anastasov <[email protected]>
Re: [PATCH net-next 03/12] ipvs: prefer NETDEV_DOWN event to free cached dsts
Hello
Quoted 11 lines · Julian Anastasov
Hello,
Quoted 7 lines · Hans Schillstrom
Hi Julian
Quoted 3 lines · Julian Anastasov
The real server becomes unreachable on down event,
no need to wait device unregistration. Should help in releasing dsts early beforedst->devis replaced with lo.
Quoted 8 lines
Quoted 2 lines
Have you test this in a network namespace ? i.e. kill the namespace with heave traffic through it
This should not be a problem. Even without ns exit
situation, we can have a case where device goes down, the output routes will start to fail and we will not cache route anymore. It can happen while the input device floods us with requests.
I've gone through the unregister code a couple of times now and in theory it should work.
The test suit have also been running without problem
Quoted 7 lines
Quoted 3 lines
From what I remember this was a tricky area...
I have some test cases for this, should I run them ?
If it is easy...
Quoted 1 line
Quoted 1 line
Signed-off-by Julian Anastasov <[email protected]>
Signed-off by: Hans Schillstrom <[email protected]>
Quoted 35 lines
Quoted 25 lines
Quoted 25 lines
--- net/netfilter/ipvs/ip_vs_ctl.c | 8 +++----- 1 files changed, 3 insertions(+), 5 deletions(-)net/netfilter/ipvs/ip_vs_ctl.c +2
-4@@ -1512,10 +1512,8 @@ __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)spin_unlock_bh (&dest->dst_lock); } -/* -* Netdev event receiver -* Currently only NETDEV_UNREGISTER is handled, i.e. if we hold a reference to -* a device that is "unregister" it must be released. +/* Netdev event receiver +* Currently only NETDEV_DOWN is handled to release refs to cached dsts */ static int ip_vs_dst_event (struct notifier_block *this,unsigned long event,void *ptr)@@ -1527,7 +1525,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,struct ip_vs_dest *dest;unsigned int idx;Quoted 9 lines
net/netfilter/ipvs/ip_vs_ctl.c static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, · line 1531
Quoted 5 lines
net/netfilter/ipvs/ip_vs_ctl.c +1
-1-if (event !=NETDEV_UNREGISTER || !ipvs) +if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name);EnterFunction (2 );Regards
Hans
Quoted 4 lines
Regards
--
Julian Anastasov <[email protected]>
Regargs
Hans
Julian Anastasov
maintainerPATCH 4/12 ipvs: convert the IP_VS_XMIT macros to functions
In reply to Julian AnastasovIt was a bad idea to hide return statements in macros.
--- net/netfilter/ipvs/ip_vs_xmit.c | 134 +++++++++++++++++++++------------------ 1 files changed, 72 insertions(+), 62 deletions(-)
net/netfilter/ipvs/ip_vs_xmit.c +72 -62
@@ -376,45 +376,59 @@ ip_vs_dst_reset(struct ip_vs_dest *dest) dest->dst_saddr.ip =0 ; } -#define IP_VS_XMIT_TUNNEL (skb, cp) \ -({ \ - int __ret = NF_ACCEPT; \ - \ - (skb)->ipvs_property = 1; \ - if (unlikely((cp)->flags & IP_VS_CONN_F_NFCT)) \ - __ret = ip_vs_confirm_conntrack(skb); \ - if (__ret == NF_ACCEPT) { \ - nf_reset(skb); \ - skb_forward_csum(skb); \ - } \ - __ret; \ -}) - -#define IP_VS_XMIT_NAT (pf, skb, cp, local) \ -do { \ - (skb)->ipvs_property = 1; \ - if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ - ip_vs_notrack(skb); \ - else \ - ip_vs_update_conntrack(skb, cp, 1); \ - if (local) \ - return NF_ACCEPT; \ - skb_forward_csum(skb); \ - NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ - skb_dst(skb)->dev, dst_output); \ -} while (0) - -#define IP_VS_XMIT (pf, skb, cp, local) \ -do { \ - (skb)->ipvs_property = 1; \ - if (likely(!((cp)->flags & IP_VS_CONN_F_NFCT))) \ - ip_vs_notrack(skb); \ - if (local) \ - return NF_ACCEPT; \ - skb_forward_csum(skb); \ - NF_HOOK(pf, NF_INET_LOCAL_OUT, (skb), NULL, \ - skb_dst(skb)->dev, dst_output); \ -} while (0) +/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */ +static inline int ip_vs_tunnel_xmit_prepare (struct sk_buff *skb, +struct ip_vs_conn *cp) +{ +int ret =NF_ACCEPT ; + + skb->ipvs_property =1 ; +if (unlikely (cp->flags &IP_VS_CONN_F_NFCT )) + ret =ip_vs_confirm_conntrack (skb); +if (ret ==NF_ACCEPT ) { +nf_reset (skb); +skb_forward_csum (skb); + } +return ret; +} + +/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */ +static inline int ip_vs_nat_send_or_cont (int pf,struct sk_buff *skb, +struct ip_vs_conn *cp,int local) +{ +int ret =NF_STOLEN ; + + skb->ipvs_property =1 ; +if (likely (!(cp->flags &IP_VS_CONN_F_NFCT ))) +ip_vs_notrack (skb); +else +ip_vs_update_conntrack (skb, cp,1 ); +if (!local) { +skb_forward_csum (skb); +NF_HOOK (pf,NF_INET_LOCAL_OUT , skb,NULL ,skb_dst (skb)->dev, + dst_output); + }else + ret =NF_ACCEPT ; +return ret; +} + +/* return NF_STOLEN (sent) or NF_ACCEPT if local=1 (not sent) */ +static inline int ip_vs_send_or_cont (int pf,struct sk_buff *skb, +struct ip_vs_conn *cp,int local) +{ +int ret =NF_STOLEN ; + + skb->ipvs_property =1 ; +if (likely (!(cp->flags &IP_VS_CONN_F_NFCT ))) +ip_vs_notrack (skb); +if (!local) { +skb_forward_csum (skb); +NF_HOOK (pf,NF_INET_LOCAL_OUT , skb,NULL ,skb_dst (skb)->dev, + dst_output); + }else + ret =NF_ACCEPT ; +return ret; +}/*
@@ -425,7 +439,7 @@ ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh) {/* we do not touch skb and do not need pskb ptr */ - IP_VS_XMIT(NFPROTO_IPV4, skb, cp, 1); +return ip_vs_send_or_cont (NFPROTO_IPV4 , skb, cp,1 ); }
@@ -476,7 +490,7 @@ ip_vs_bypass_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT (NFPROTO_IPV4 , skb, cp,0 ); +ip_vs_send_or_cont (NFPROTO_IPV4 , skb, cp,0 );LeaveFunction (10 );return NF_STOLEN ;
@@ -537,7 +551,7 @@ ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT (NFPROTO_IPV6 , skb, cp,0 ); +ip_vs_send_or_cont (NFPROTO_IPV6 , skb, cp,0 );LeaveFunction (10 );return NF_STOLEN ;
@@ -562,7 +576,7 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,struct rtable *rt;/* Route to the other host */ int mtu;struct iphdr *iph =ip_hdr (skb); -int local; +int local, rc;EnterFunction (10 );
@@ -655,10 +669,10 @@ ip_vs_nat_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT_NAT (NFPROTO_IPV4 , skb, cp, local); + rc =ip_vs_nat_send_or_cont (NFPROTO_IPV4 , skb, cp, local);LeaveFunction (10 ); -return NF_STOLEN ; +return rc; tx_error_icmp:dst_link_failure (skb);
@@ -678,7 +692,7 @@ ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, {struct rt6_info *rt;/* Route to the other host */ int mtu; -int local; +int local, rc;EnterFunction (10 );
@@ -771,10 +785,10 @@ ip_vs_nat_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT_NAT (NFPROTO_IPV6 , skb, cp, local); + rc =ip_vs_nat_send_or_cont (NFPROTO_IPV6 , skb, cp, local);LeaveFunction (10 ); -return NF_STOLEN ; +return rc; tx_error_icmp:dst_link_failure (skb);
@@ -833,7 +847,7 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, goto tx_error_icmp;if (rt->rt_flags &RTCF_LOCAL ) {ip_rt_put (rt); -IP_VS_XMIT (NFPROTO_IPV4 , skb, cp,1 ); +return ip_vs_send_or_cont (NFPROTO_IPV4 , skb, cp,1 ); } tdev = rt->dst.dev;
@@ -905,7 +919,7 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; - ret =IP_VS_XMIT_TUNNEL (skb, cp); + ret =ip_vs_tunnel_xmit_prepare (skb, cp);if (ret ==NF_ACCEPT )ip_local_out (skb);else if (ret ==NF_DROP )
@@ -948,7 +962,7 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, goto tx_error_icmp;if (__ip_vs_is_local_route6 (rt)) {dst_release (&rt->dst); -IP_VS_XMIT (NFPROTO_IPV6 , skb, cp,1 ); +return ip_vs_send_or_cont (NFPROTO_IPV6 , skb, cp,1 ); } tdev = rt->dst.dev;
@@ -1023,7 +1037,7 @@ ip_vs_tunnel_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; - ret =IP_VS_XMIT_TUNNEL (skb, cp); + ret =ip_vs_tunnel_xmit_prepare (skb, cp);if (ret ==NF_ACCEPT )ip6_local_out (skb);else if (ret ==NF_DROP )
@@ -1067,7 +1081,7 @@ ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, goto tx_error_icmp;if (rt->rt_flags &RTCF_LOCAL ) {ip_rt_put (rt); -IP_VS_XMIT (NFPROTO_IPV4 , skb, cp,1 ); +return ip_vs_send_or_cont (NFPROTO_IPV4 , skb, cp,1 ); }/* MTU checking */
@@ -1097,7 +1111,7 @@ ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT (NFPROTO_IPV4 , skb, cp,0 ); +ip_vs_send_or_cont (NFPROTO_IPV4 , skb, cp,0 );LeaveFunction (10 );return NF_STOLEN ;
@@ -1126,7 +1140,7 @@ ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp, goto tx_error_icmp;if (__ip_vs_is_local_route6 (rt)) {dst_release (&rt->dst); -IP_VS_XMIT (NFPROTO_IPV6 , skb, cp,1 ); +return ip_vs_send_or_cont (NFPROTO_IPV6 , skb, cp,1 ); }/* MTU checking */
@@ -1162,7 +1176,7 @@ ip_vs_dr_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT (NFPROTO_IPV6 , skb, cp,0 ); +ip_vs_send_or_cont (NFPROTO_IPV6 , skb, cp,0 );LeaveFunction (10 );return NF_STOLEN ;
@@ -1283,9 +1297,7 @@ ip_vs_icmp_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT_NAT (NFPROTO_IPV4 , skb, cp, local); - - rc =NF_STOLEN ; + rc =ip_vs_nat_send_or_cont (NFPROTO_IPV4 , skb, cp, local); goto out; tx_error_icmp:
@@ -1404,9 +1416,7 @@ ip_vs_icmp_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,/* Another hack: avoid icmp_send in ip_fragment */ skb->local_df =1 ; -IP_VS_XMIT_NAT (NFPROTO_IPV6 , skb, cp, local); - - rc =NF_STOLEN ; + rc =ip_vs_nat_send_or_cont (NFPROTO_IPV6 , skb, cp, local); goto out; tx_error_icmp:
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 5/12 ipvs: rename functions related to dst_cache reset
In reply to Julian AnastasovMove and give better names to two functions:
- ip_vs_dst_reset to __ip_vs_dst_cache_reset
- __ip_vs_dev_reset to ip_vs_forget_dev
--- include/net/ip_vs.h | 1 - net/netfilter/ipvs/ip_vs_ctl.c | 34 ++++++++++++++++++++++------------ net/netfilter/ipvs/ip_vs_xmit.c | 14 -------------- 3 files changed, 22 insertions(+), 27 deletions(-)
include/net/ip_vs.h +0 -1
@@ -1415,7 +1415,6 @@ extern int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,extern int ip_vs_icmp_xmit (struct sk_buff *skb,struct ip_vs_conn *cp,struct ip_vs_protocol *pp,int offset,unsigned int hooknum,struct ip_vs_iphdr *iph); -extern void ip_vs_dst_reset (struct ip_vs_dest *dest);#ifdef CONFIG_IP_VS_IPV6 extern int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
net/netfilter/ipvs/ip_vs_ctl.c +22 -12
@@ -639,6 +639,17 @@ struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af,return dest; } +/* Release dst_cache for dest in user context */ +static void __ip_vs_dst_cache_reset (struct ip_vs_dest *dest) +{ +struct dst_entry *old_dst; + + old_dst = dest->dst_cache; + dest->dst_cache =NULL ; +dst_release (old_dst); + dest->dst_saddr.ip =0 ; +} +/* * Lookup dest by {svc,addr,port} in the destination trash. * The destination trash is used to hold the destinations that are removed
@@ -688,7 +699,7 @@ ip_vs_trash_get_dest(struct ip_vs_service *svc, const union nf_inet_addr *daddr,IP_VS_DBG_ADDR (svc->af, &dest->addr), ntohs(dest->port));list_del (&dest->n_list); -ip_vs_dst_reset (dest); +__ip_vs_dst_cache_reset (dest);__ip_vs_unbind_svc (dest);free_percpu (dest->stats.cpustats);kfree (dest);
@@ -715,7 +726,7 @@ static void ip_vs_trash_cleanup(struct net *net)list_for_each_entry_safe (dest, nxt, &ipvs->dest_trash, n_list) {list_del (&dest->n_list); -ip_vs_dst_reset (dest); +__ip_vs_dst_cache_reset (dest);__ip_vs_unbind_svc (dest);free_percpu (dest->stats.cpustats);kfree (dest);
@@ -809,7 +820,7 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest, dest->l_threshold = udest->l_threshold;spin_lock_bh (&dest->dst_lock); -ip_vs_dst_reset (dest); +__ip_vs_dst_cache_reset (dest);spin_unlock_bh (&dest->dst_lock);if (add)
@@ -1035,7 +1046,7 @@ static void __ip_vs_del_dest(struct net *net, struct ip_vs_dest *dest) dest->vfwmark,IP_VS_DBG_ADDR (dest->af, &dest->addr), ntohs(dest->port)); -ip_vs_dst_reset (dest); +__ip_vs_dst_cache_reset (dest);/* simply decrease svc->refcnt here, let the caller check and release the service if nobody refers to it. Only user context can release destination and service,
@@ -1494,11 +1505,10 @@ void ip_vs_service_net_cleanup(struct net *net)mutex_unlock (&__ip_vs_mutex);LeaveFunction (2 ); } -/* -* Release dst hold by dst_cache -*/ + +/* Put all references for device (dst_cache) */ static inline void -__ip_vs_dev_reset (struct ip_vs_dest *dest,struct net_device *dev) +ip_vs_forget_dev (struct ip_vs_dest *dest,struct net_device *dev) {spin_lock_bh (&dest->dst_lock);if (dest->dst_cache && dest->dst_cache->dev == dev) {
@@ -1507,7 +1517,7 @@ __ip_vs_dev_reset(struct ip_vs_dest *dest, struct net_device *dev)IP_VS_DBG_ADDR (dest->af, &dest->addr),ntohs (dest->port), atomic_read(&dest->refcnt)); -ip_vs_dst_reset (dest); +__ip_vs_dst_cache_reset (dest); }spin_unlock_bh (&dest->dst_lock);
@@ -1535,7 +1545,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,if (net_eq (svc->net, net)) {list_for_each_entry (dest, &svc->destinations, n_list) { -__ip_vs_dev_reset (dest, dev); +ip_vs_forget_dev (dest, dev); } } }
@@ -1544,7 +1554,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,if (net_eq (svc->net, net)) {list_for_each_entry (dest, &svc->destinations, n_list) { -__ip_vs_dev_reset (dest, dev); +ip_vs_forget_dev (dest, dev); } }
@@ -1552,7 +1562,7 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, }list_for_each_entry (dest, &ipvs->dest_trash, n_list) { -__ip_vs_dev_reset (dest, dev); +ip_vs_forget_dev (dest, dev); }mutex_unlock (&__ip_vs_mutex);LeaveFunction (2 );
net/netfilter/ipvs/ip_vs_xmit.c +0 -14
@@ -362,20 +362,6 @@ __ip_vs_get_out_rt_v6(struct sk_buff *skb, struct ip_vs_dest *dest,#endif -/* -* Release dest->dst_cache before a dest is removed -*/ -void -ip_vs_dst_reset (struct ip_vs_dest *dest) -{ -struct dst_entry *old_dst; - - old_dst = dest->dst_cache; - dest->dst_cache =NULL ; -dst_release (old_dst); - dest->dst_saddr.ip =0 ; -} -/* return NF_ACCEPT to allow forwarding or other NF_xxx on error */ static inline int ip_vs_tunnel_xmit_prepare (struct sk_buff *skb,struct ip_vs_conn *cp)
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 6/12 ipvs: optimize dst usage for real server
In reply to Julian Anastasov Currently when forwarding requests to real servers
we use dst_lock and atomic operations when cloning the dst_cache value. As the dst_cache value does not change most of the time it is better to use RCU and to lock dst_lock only when we need to replace the obsoleted dst. For this to work we keep dst_cache in new structure protected by RCU. For packets to remote real servers we will use noref version of dst_cache, it will be valid while we are in RCU read-side critical section because now dst_release for replaced dsts will be invoked after the grace period. NAT-ed packets via loopback that are not sent but are passed to local stack with NF_ACCEPT need a dst clone (skb_dst_force).
--- include/net/ip_vs.h | 12 +- net/netfilter/ipvs/ip_vs_core.c | 11 +- net/netfilter/ipvs/ip_vs_ctl.c | 24 ++- net/netfilter/ipvs/ip_vs_xmit.c | 366 ++++++++++++++++++++++++++------------- 4 files changed, 275 insertions(+), 138 deletions(-)
include/net/ip_vs.h +9 -3
@@ -724,6 +724,13 @@ struct ip_vs_service {struct ip_vs_pe *pe; }; +/* Information for cached dst */ +struct ip_vs_dest_dst { +struct dst_entry *dst_cache;/* destination cache entry */ +u32 dst_cookie; +union nf_inet_addr dst_saddr; +struct rcu_head rcu_head; +};/* * The real server destination forwarding entry
@@ -752,9 +759,7 @@ struct ip_vs_dest {/* for destination cache */ spinlock_t dst_lock;/* lock of dst_cache */ -struct dst_entry *dst_cache;/* destination cache entry */ -u32 dst_cookie; -union nf_inet_addr dst_saddr; +struct ip_vs_dest_dst __rcu *dest_dst;/* cached dst info */ /* for virtual service */ struct ip_vs_service *svc;/* service it belongs to */
@@ -1415,6 +1420,7 @@ extern int ip_vs_dr_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,extern int ip_vs_icmp_xmit (struct sk_buff *skb,struct ip_vs_conn *cp,struct ip_vs_protocol *pp,int offset,unsigned int hooknum,struct ip_vs_iphdr *iph); +extern void ip_vs_dest_dst_rcu_free (struct rcu_head *head);#ifdef CONFIG_IP_VS_IPV6 extern int ip_vs_bypass_xmit_v6(struct sk_buff *skb, struct ip_vs_conn *cp,
net/netfilter/ipvs/ip_vs_core.c +7 -4
@@ -1403,10 +1403,13 @@ ip_vs_in_icmp(struct sk_buff *skb, int *related, unsigned int hooknum) goto ignore_ipip;/* Prefer the resulting PMTU */ if (dest) { -spin_lock (&dest->dst_lock); -if (dest->dst_cache) - mtu =dst_mtu (dest->dst_cache); -spin_unlock (&dest->dst_lock); +struct ip_vs_dest_dst *dest_dst; + +rcu_read_lock (); + dest_dst =rcu_dereference (dest->dest_dst); +if (dest_dst) + mtu =dst_mtu (dest_dst->dst_cache); +rcu_read_unlock (); }if (mtu >68 +sizeof (struct iphdr )) mtu -=sizeof (struct iphdr );
net/netfilter/ipvs/ip_vs_ctl.c +17 -7
@@ -639,15 +639,25 @@ struct ip_vs_dest *ip_vs_find_dest(struct net *net, int af,return dest; } -/* Release dst_cache for dest in user context */ +void ip_vs_dest_dst_rcu_free (struct rcu_head *head) +{ +struct ip_vs_dest_dst *dest_dst =container_of (head, + struct ip_vs_dest_dst, + rcu_head); + +dst_release (dest_dst->dst_cache); +kfree (dest_dst); +} + +/* Release dest_dst and dst_cache for dest in user context */ static void __ip_vs_dst_cache_reset (struct ip_vs_dest *dest) { -struct dst_entry *old_dst; +struct ip_vs_dest_dst *old =rcu_dereference_raw (dest->dest_dst); - old_dst = dest->dst_cache; - dest->dst_cache =NULL ; -dst_release (old_dst); - dest->dst_saddr.ip =0 ; +if (old) { +RCU_INIT_POINTER (dest->dest_dst,NULL ); +call_rcu (&old->rcu_head, ip_vs_dest_dst_rcu_free); + } }/*
@@ -1511,7 +1521,7 @@ static inline void ip_vs_forget_dev(struct ip_vs_dest *dest, struct net_device *dev) { spin_lock_bh(&dest->dst_lock); -if (dest->dst_cache && dest->dst_cache->dev == dev) { +if (dest->dest_dst && dest->dest_dst->dst_cache->dev == dev) { IP_VS_DBG_BUF(3, "Reset dev:%s dest %s:%u ,dest->refcnt=%d\n", dev->name, IP_VS_DBG_ADDR(dest->af, &dest->addr),
net/netfilter/ipvs/ip_vs_xmit.c +242 -124
900 lines not on this page · raw message
Signature · 1 line
1.7.3.4
David Miller
maintainerRe: [PATCH net-next 06/12] ipvs: optimize dst usage for real server
From: Julian Anastasov <[email protected]>
Date: Wed, 6 Mar 2013 10:42:16 +0200
Quoted 13 lines
Currently when forwarding requests to real servers
we usedst_lockand atomic operations when cloning thedst_cachevalue. As thedst_cachevalue does not change most of the time it is better to use RCU and to lockdst_lockonly when we need to replace the obsoleted dst. For this to work we keepdst_cachein new structure protected by RCU. For packets to remote real servers we will use noref version ofdst_cache, it will be valid while we are in RCU read-side critical section because nowdst_releasefor replaced dsts will be invoked after the grace period. NAT-ed packets via loopback that are not sent but are passed to local stack withNF_ACCEPTneed a dst clone (skb_dst_force).
Quoted 1 line
Signed-off-by Julian Anastasov <[email protected]>
It think it's much cleaner to have the callers of route lookups do skb_set_dst_noref().
Then you don't need any new interfaces, and you therefore don't need to expose bits of the noref implementation and semantics at all.
Julian Anastasov
maintainerRe: [PATCH net-next 06/12] ipvs: optimize dst usage for real server
Hello,
Quoted 16 lines · David Miller
From: Julian Anastasov <[email protected]>
Date: Wed, 6 Mar 2013 10:42:16 +0200Quoted 13 lines
Currently when forwarding requests to real servers
we usedst_lockand atomic operations when cloning thedst_cachevalue. As thedst_cachevalue does not change most of the time it is better to use RCU and to lockdst_lockonly when we need to replace the obsoleted dst. For this to work we keepdst_cachein new structure protected by RCU. For packets to remote real servers we will use noref version ofdst_cache, it will be valid while we are in RCU read-side critical section because nowdst_releasefor replaced dsts will be invoked after the grace period. NAT-ed packets via loopback that are not sent but are passed to local stack withNF_ACCEPTneed a dst clone (skb_dst_force).
Quoted 4 lines
Quoted 1 line
Signed-off-by Julian Anastasov <[email protected]>It think it's much cleaner to have the callers of route lookups do
skb_set_dst_noref().
This was my first thought but commit 27b75c95f1
(net: avoid RCU for NOCACHE dst) uses dst_hold for DST_NOCACHE dsts. IPVS wants to cache DST_NOCACHE entries (due to known-NH reasons) and later to use them for noref dsts...
Also, in the IPVS case we can not set skb_dst
at the time when route is looked up because due to MTU and other checks we can fail, functions like icmp_send() will need the original skb_dst in such case.
There are two choices: __ip_vs_get_out_rt
to return refdst as implemented in this patchset or to return dst with additional 'bool *noref' argument, so that caller can decide between skb_dst_set or skb_set_dst_noref. Then may be we will need just a new skb_dst_set_noref_{always,force} func that will contain the old skb_set_dst_noref code, i.e. without dst_hold? Not sure which variant sounds better.
Quoted 2 lines
Then you don't need any new interfaces, and you therefore don't need to expose bits of the noref implementation and semantics at all.
Regards
--
Julian Anastasov <[email protected]>
David Miller
maintainerRe: [PATCH net-next 06/12] ipvs: optimize dst usage for real server
From: Julian Anastasov <[email protected]>
Date: Wed, 6 Mar 2013 23:58:09 +0200 (EET)
Quoted 8 lines
There are two choices:
__ip_vs_get_out_rt
to return refdst as implemented in this patchset or to return dst with additional 'bool *noref' argument, so that caller can decide betweenskb_dst_setorskb_set_dst_noref. Then may be we will need just a new skb_dst_set_noref_{always,force} func that will contain the oldskb_set_dst_norefcode, i.e. withoutdst_hold? Not sure which variant sounds better.
Both variants are starting to sound equally ugly :-)
Let me ask about this situation in another way.
IP input route lookup clients handle this transparently, even for routes with next-hop exceptions and nocache routes, by passing the SKB into the lookup function and there it will sort out whether noref is actually possible.
Is there a reason that IPVS's route lookup architecture can't work this way too?
Julian Anastasov
maintainerRe: [PATCH net-next 06/12] ipvs: optimize dst usage for real server
Hello,
Quoted 16 lines · David Miller
From: Julian Anastasov <[email protected]>
Date: Wed, 6 Mar 2013 23:58:09 +0200 (EET)Quoted 8 lines
There are two choices:
__ip_vs_get_out_rt
to return refdst as implemented in this patchset or to return dst with additional 'bool *noref' argument, so that caller can decide betweenskb_dst_setorskb_set_dst_noref. Then may be we will need just a new skb_dst_set_noref_{always,force} func that will contain the oldskb_set_dst_norefcode, i.e. withoutdst_hold? Not sure which variant sounds better.Both variants are starting to sound equally ugly :-)
Let me ask about this situation in another way.
Quoted 7 lines
IP input route lookup clients handle this transparently, even for routes with next-hop exceptions and nocache routes, by passing the SKB into the lookup function and there it will sort out whether noref is actually possible.
Is there a reason that IPVS's route lookup architecture can't work this way too?
May be it is possible, eg. by adding more
arguments to __ip_vs_get_out_rt and moving all dst checks and icmp_send there. But there are some dst checks specific to the forwarding method, so I'm not sure in the end result yet.
For IPVS, noref is always possible because thedst_cache var holds reference, even for the NOCACHE case which is more likely to occur. So, some new variant of skb_dst_set_noref that supports NOCACHE without dst_hold is still needed.
Regards
--
Julian Anastasov <[email protected]>
Julian Anastasov
maintainerPATCH 7/12 ipvs: convert app locks
In reply to Julian Anastasov We use locks like tcp_app_lock, udp_app_lock,sctp_app_lock to protect access to the protocol hash tables from readers in packet context while the application instances (inc) are [un]registered under global mutex.
As the hash tables are mostly read when conns are
created and bound to app, use RCU for readers and reclaim app instance after grace period.
Simplify ip_vs_app_inc_get because we use usecnt
only for statistics and rely on module refcounting.
--- include/net/ip_vs.h | 4 +--- net/netfilter/ipvs/ip_vs_app.c | 27 +++++++++++++++++++-------- net/netfilter/ipvs/ip_vs_ftp.c | 2 ++ net/netfilter/ipvs/ip_vs_proto_sctp.c | 18 ++++++------------ net/netfilter/ipvs/ip_vs_proto_tcp.c | 18 ++++++------------ net/netfilter/ipvs/ip_vs_proto_udp.c | 19 ++++++------------- 6 files changed, 40 insertions(+), 48 deletions(-)
include/net/ip_vs.h +1 -3
@@ -823,6 +823,7 @@ struct ip_vs_app {struct ip_vs_app *app;/* its real application */ __be16 port;/* port number in net order */ atomic_t usecnt;/* usage counter */ +struct rcu_head rcu_head;/* * output hook: Process packet in inout direction, diff set for TCP.
@@ -908,7 +909,6 @@ struct netns_ipvs {#define TCP_APP_TAB_SIZE (1 << TCP_APP_TAB_BITS)#define TCP_APP_TAB_MASK (TCP_APP_TAB_SIZE - 1)struct list_head tcp_apps[TCP_APP_TAB_SIZE ]; -spinlock_t tcp_app_lock;#endif /* ip_vs_proto_udp */ #ifdef CONFIG_IP_VS_PROTO_UDP
@@ -916,7 +916,6 @@ struct netns_ipvs {#define UDP_APP_TAB_SIZE (1 << UDP_APP_TAB_BITS)#define UDP_APP_TAB_MASK (UDP_APP_TAB_SIZE - 1)struct list_head udp_apps[UDP_APP_TAB_SIZE ]; -spinlock_t udp_app_lock;#endif /* ip_vs_proto_sctp */ #ifdef CONFIG_IP_VS_PROTO_SCTP
@@ -925,7 +924,6 @@ struct netns_ipvs {#define SCTP_APP_TAB_MASK (SCTP_APP_TAB_SIZE - 1)/* Hash table for SCTP application incarnations */ struct list_head sctp_apps[SCTP_APP_TAB_SIZE ]; -spinlock_t sctp_app_lock;#endif /* ip_vs_conn */ atomic_t conn_count;/* connection counter */
net/netfilter/ipvs/ip_vs_app.c +19 -8
@@ -58,6 +58,18 @@ static inline void ip_vs_app_put(struct ip_vs_app *app)module_put (app->module); } +static void ip_vs_app_inc_destroy (struct ip_vs_app *inc) +{ +kfree (inc->timeout_table); +kfree (inc); +} + +static void ip_vs_app_inc_rcu_free (struct rcu_head *head) +{ + struct ip_vs_app *inc = container_of(head, struct ip_vs_app, rcu_head); + +ip_vs_app_inc_destroy (inc); +}/* * Allocate/initialize app incarnation and register it in proto apps.
@@ -106,8 +118,7 @@ ip_vs_app_inc_new(struct net *net, struct ip_vs_app *app, __u16 proto,return 0 ; out: -kfree (inc->timeout_table); -kfree (inc); +ip_vs_app_inc_destroy (inc);return ret; }
@@ -131,8 +142,7 @@ ip_vs_app_inc_release(struct net *net, struct ip_vs_app *inc)list_del (&inc->a_list); -kfree (inc->timeout_table); -kfree (inc); +call_rcu (&inc->rcu_head, ip_vs_app_inc_rcu_free); }
@@ -144,9 +154,9 @@ int ip_vs_app_inc_get(struct ip_vs_app *inc) {int result; -atomic_inc (&inc->usecnt); -if (unlikely ((result =ip_vs_app_get (inc->app)) !=1 )) -atomic_dec (&inc->usecnt); + result =ip_vs_app_get (inc->app); +if (result) +atomic_inc (&inc->usecnt);return result; }
@@ -156,8 +166,8 @@ int ip_vs_app_inc_get(struct ip_vs_app *inc)*/ void ip_vs_app_inc_put (struct ip_vs_app *inc) { -ip_vs_app_put (inc->app);atomic_dec (&inc->usecnt); +ip_vs_app_put (inc->app); }
@@ -218,6 +228,7 @@ out_unlock:/* * ip_vs_app unregistration routine * We are sure there are no app incarnations attached to services +* Caller should use synchronize_rcu() or rcu_barrier() */ void unregister_ip_vs_app (struct net *net,struct ip_vs_app *app) {
net/netfilter/ipvs/ip_vs_ftp.c +2 -0
@@ -480,6 +480,7 @@ static int __init ip_vs_ftp_init(void)int rv; rv =register_pernet_subsys (&ip_vs_ftp_ops); +/* rcu_barrier() is called by netns on error */ return rv; }
@@ -489,6 +490,7 @@ static int __init ip_vs_ftp_init(void) static void __exit ip_vs_ftp_exit(void) {unregister_pernet_subsys (&ip_vs_ftp_ops); +/* rcu_barrier() is called by netns */ }
net/netfilter/ipvs/ip_vs_proto_sctp.c +6 -12
@@ -1014,30 +1014,25 @@ static int sctp_register_app(struct net *net, struct ip_vs_app *inc) hash =sctp_app_hashkey (port); -spin_lock_bh (&ipvs->sctp_app_lock);list_for_each_entry (i, &ipvs->sctp_apps[hash], p_list) {if (i->port == port) { ret = -EEXIST ; goto out; } } -list_add (&inc->p_list, &ipvs->sctp_apps[hash]); +list_add_rcu (&inc->p_list, &ipvs->sctp_apps[hash]);atomic_inc (&pd->appcnt); out: -spin_unlock_bh (&ipvs->sctp_app_lock);return ret; }static void sctp_unregister_app (struct net *net,struct ip_vs_app *inc) { -struct netns_ipvs *ipvs =net_ipvs (net);struct ip_vs_proto_data *pd =ip_vs_proto_data_get (net,IPPROTO_SCTP ); -spin_lock_bh (&ipvs->sctp_app_lock);atomic_dec (&pd->appcnt); -list_del (&inc->p_list); -spin_unlock_bh (&ipvs->sctp_app_lock); +list_del_rcu (&inc->p_list); }static int sctp_app_conn_bind (struct ip_vs_conn *cp)
@@ -1053,12 +1048,12 @@ static int sctp_app_conn_bind(struct ip_vs_conn *cp)/* Lookup application incarnations and bind the right one */ hash =sctp_app_hashkey (cp->vport); -spin_lock (&ipvs->sctp_app_lock); -list_for_each_entry (inc, &ipvs->sctp_apps[hash], p_list) { +rcu_read_lock (); +list_for_each_entry_rcu (inc, &ipvs->sctp_apps[hash], p_list) {if (inc->port == cp->vport) {if (unlikely (!ip_vs_app_inc_get (inc)))break ; -spin_unlock (&ipvs->sctp_app_lock); +rcu_read_unlock (); IP_VS_DBG_BUF(9, "%s: Binding conn %s:%u->" "%s:%u to app %s on port %u\n",
@@ -1074,7 +1069,7 @@ static int sctp_app_conn_bind(struct ip_vs_conn *cp) goto out; } } -spin_unlock (&ipvs->sctp_app_lock); +rcu_read_unlock (); out:return result; }
@@ -1088,7 +1083,6 @@ static int __ip_vs_sctp_init(struct net *net, struct ip_vs_proto_data *pd)struct netns_ipvs *ipvs =net_ipvs (net);ip_vs_init_hash_table (ipvs->sctp_apps,SCTP_APP_TAB_SIZE ); -spin_lock_init (&ipvs->sctp_app_lock); pd->timeout_table =ip_vs_create_timeout_table ((int *)sctp_timeouts,sizeof (sctp_timeouts));if (!pd->timeout_table)
net/netfilter/ipvs/ip_vs_proto_tcp.c +6 -12
@@ -580,18 +580,16 @@ static int tcp_register_app(struct net *net, struct ip_vs_app *inc) hash =tcp_app_hashkey (port); -spin_lock_bh (&ipvs->tcp_app_lock);list_for_each_entry (i, &ipvs->tcp_apps[hash], p_list) {if (i->port == port) { ret = -EEXIST ; goto out; } } -list_add (&inc->p_list, &ipvs->tcp_apps[hash]); +list_add_rcu (&inc->p_list, &ipvs->tcp_apps[hash]);atomic_inc (&pd->appcnt); out: -spin_unlock_bh (&ipvs->tcp_app_lock);return ret; }
@@ -599,13 +597,10 @@ static int tcp_register_app(struct net *net, struct ip_vs_app *inc)static void tcp_unregister_app (struct net *net,struct ip_vs_app *inc) { -struct netns_ipvs *ipvs =net_ipvs (net);struct ip_vs_proto_data *pd =ip_vs_proto_data_get (net,IPPROTO_TCP ); -spin_lock_bh (&ipvs->tcp_app_lock);atomic_dec (&pd->appcnt); -list_del (&inc->p_list); -spin_unlock_bh (&ipvs->tcp_app_lock); +list_del_rcu (&inc->p_list); }
@@ -624,12 +619,12 @@ tcp_app_conn_bind(struct ip_vs_conn *cp)/* Lookup application incarnations and bind the right one */ hash =tcp_app_hashkey (cp->vport); -spin_lock (&ipvs->tcp_app_lock); -list_for_each_entry (inc, &ipvs->tcp_apps[hash], p_list) { +rcu_read_lock (); +list_for_each_entry_rcu (inc, &ipvs->tcp_apps[hash], p_list) {if (inc->port == cp->vport) {if (unlikely (!ip_vs_app_inc_get (inc)))break ; -spin_unlock (&ipvs->tcp_app_lock); +rcu_read_unlock (); IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" "%s:%u to app %s on port %u\n",
@@ -646,7 +641,7 @@ tcp_app_conn_bind(struct ip_vs_conn *cp) goto out; } } -spin_unlock (&ipvs->tcp_app_lock); +rcu_read_unlock (); out:return result;
@@ -676,7 +671,6 @@ static int __ip_vs_tcp_init(struct net *net, struct ip_vs_proto_data *pd)struct netns_ipvs *ipvs =net_ipvs (net);ip_vs_init_hash_table (ipvs->tcp_apps,TCP_APP_TAB_SIZE ); -spin_lock_init (&ipvs->tcp_app_lock); pd->timeout_table =ip_vs_create_timeout_table ((int *)tcp_timeouts,sizeof (tcp_timeouts));if (!pd->timeout_table)
net/netfilter/ipvs/ip_vs_proto_udp.c +6 -13
@@ -359,19 +359,16 @@ static int udp_register_app(struct net *net, struct ip_vs_app *inc) hash =udp_app_hashkey (port); - -spin_lock_bh (&ipvs->udp_app_lock);list_for_each_entry (i, &ipvs->udp_apps[hash], p_list) {if (i->port == port) { ret = -EEXIST ; goto out; } } -list_add (&inc->p_list, &ipvs->udp_apps[hash]); +list_add_rcu (&inc->p_list, &ipvs->udp_apps[hash]);atomic_inc (&pd->appcnt); out: -spin_unlock_bh (&ipvs->udp_app_lock);return ret; }
@@ -380,12 +377,9 @@ static void udp_unregister_app(struct net *net, struct ip_vs_app *inc) { struct ip_vs_proto_data *pd = ip_vs_proto_data_get(net, IPPROTO_UDP); -struct netns_ipvs *ipvs =net_ipvs (net); -spin_lock_bh (&ipvs->udp_app_lock);atomic_dec (&pd->appcnt); -list_del (&inc->p_list); -spin_unlock_bh (&ipvs->udp_app_lock); +list_del_rcu (&inc->p_list); }
@@ -403,12 +397,12 @@ static int udp_app_conn_bind(struct ip_vs_conn *cp)/* Lookup application incarnations and bind the right one */ hash =udp_app_hashkey (cp->vport); -spin_lock (&ipvs->udp_app_lock); -list_for_each_entry (inc, &ipvs->udp_apps[hash], p_list) { +rcu_read_lock (); +list_for_each_entry_rcu (inc, &ipvs->udp_apps[hash], p_list) {if (inc->port == cp->vport) {if (unlikely (!ip_vs_app_inc_get (inc)))break ; -spin_unlock (&ipvs->udp_app_lock); +rcu_read_unlock (); IP_VS_DBG_BUF(9, "%s(): Binding conn %s:%u->" "%s:%u to app %s on port %u\n",
@@ -425,7 +419,7 @@ static int udp_app_conn_bind(struct ip_vs_conn *cp) goto out; } } -spin_unlock (&ipvs->udp_app_lock); +rcu_read_unlock (); out:return result;
@@ -467,7 +461,6 @@ static int __udp_init(struct net *net, struct ip_vs_proto_data *pd)struct netns_ipvs *ipvs =net_ipvs (net);ip_vs_init_hash_table (ipvs->udp_apps,UDP_APP_TAB_SIZE ); -spin_lock_init (&ipvs->udp_app_lock); pd->timeout_table =ip_vs_create_timeout_table ((int *)udp_timeouts,sizeof (udp_timeouts));if (!pd->timeout_table)
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 8/12 ipvs: remove rs_lock by using RCU
In reply to Julian Anastasov rs_lock was used to protect rs_table (hash table)
from updaters (under global mutex) and readers (packet handlers). We can remove rs_lock by using RCU lock for readers. Reclaiming dest only with kfree_rcu is enough because the readers access only fields from the ip_vs_dest structure.
Use hlist for rs_table.
As we are now using hlist_del_rcu, introduce in_rs_table
flag as replacement for the list_empty checks which do not work with RCU. It is needed because only NAT dests are in the rs_table.
--- include/net/ip_vs.h | 14 ++++--- net/netfilter/ipvs/ip_vs_core.c | 5 +-- net/netfilter/ipvs/ip_vs_ctl.c | 74 ++++++++++++++------------------------- 3 files changed, 36 insertions(+), 57 deletions(-)
include/net/ip_vs.h +8 -6
@@ -738,7 +738,7 @@ struct ip_vs_dest_dst {*/ struct ip_vs_dest {struct list_head n_list;/* for the dests in the service */ -struct list_head d_list;/* for table with all the dests */ +struct hlist_node d_list;/* for table with all the dests */ u16 af;/* address family */ __be16 port;/* port number of the server */
@@ -767,6 +767,9 @@ struct ip_vs_dest {__be16 vport;/* virtual port number */ union nf_inet_addr vaddr;/* virtual IP address */ __u32 vfwmark;/* firewall mark of service */ + +struct rcu_head rcu_head; + unsigned int in_rs_table:1; /* we are in rs_table */ };
@@ -897,7 +900,7 @@ struct netns_ipvs {#define IP_VS_RTAB_SIZE (1 << IP_VS_RTAB_BITS)#define IP_VS_RTAB_MASK (IP_VS_RTAB_SIZE - 1) -struct list_head rs_table[IP_VS_RTAB_SIZE ]; +struct hlist_head rs_table[IP_VS_RTAB_SIZE ];/* ip_vs_app */ struct list_head app_list;/* ip_vs_proto */
@@ -933,7 +936,6 @@ struct netns_ipvs {int num_services;/* no of virtual services */ -rwlock_t rs_lock;/* real services table */ /* Trash for destinations */ struct list_head dest_trash;/* Service counters */
@@ -1364,9 +1366,9 @@ static inline void ip_vs_service_put(struct ip_vs_service *svc)atomic_dec (&svc->usecnt); } -extern struct ip_vs_dest * -ip_vs_lookup_real_service (struct net *net,int af,__u16 protocol, -const union nf_inet_addr *daddr,__be16 dport); +extern bool +ip_vs_has_real_service (struct net *net,int af,__u16 protocol, +const union nf_inet_addr *daddr,__be16 dport);extern int ip_vs_use_count_inc (void );extern void ip_vs_use_count_dec (void );
net/netfilter/ipvs/ip_vs_core.c +2 -3
@@ -1164,9 +1164,8 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af) sizeof(_ports), _ports, &iph);if (pptr ==NULL )return NF_ACCEPT ;/* Not for me */ -if (ip_vs_lookup_real_service (net, af, iph.protocol, - &iph.saddr, - pptr[0 ])) { +if (ip_vs_has_real_service (net, af, iph.protocol, &iph.saddr, + pptr[0 ])) {/* * Notify the real server: there is no * existing entry if it is not RST
net/netfilter/ipvs/ip_vs_ctl.c +24 -45
155 lines not on this page · raw message
Julian Anastasov
maintainerPATCH 9/12 ipvs: convert locks used in persistence engines
In reply to Julian Anastasov Allow the readers to use RCU lock and for
PE module registrations use global mutex instead of spinlock. All PE modules need to use rcu_barrier in their module exit handler.
--- net/netfilter/ipvs/ip_vs_pe.c | 43 +++++++++++------------------------- net/netfilter/ipvs/ip_vs_pe_sip.c | 1 + 2 files changed, 14 insertions(+), 30 deletions(-)
net/netfilter/ipvs/ip_vs_pe.c +13 -30
@@ -13,8 +13,8 @@/* IPVS pe list */ static LIST_HEAD (ip_vs_pe); -/* lock for service table */ -static DEFINE_SPINLOCK (ip_vs_pe_lock); +/* semaphore for IPVS PEs. */ +static DEFINE_MUTEX (ip_vs_pe_mutex);/* Bind a service with a pe */ void ip_vs_bind_pe (struct ip_vs_service *svc,struct ip_vs_pe *pe)
@@ -36,9 +36,8 @@ struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name)IP_VS_DBG (10 ,"%s(): pe_name \"%s\"\n" , __func__, pe_name); -spin_lock_bh (&ip_vs_pe_lock); - -list_for_each_entry (pe, &ip_vs_pe, n_list) { +rcu_read_lock (); +list_for_each_entry_rcu (pe, &ip_vs_pe, n_list) {/* Test and get the modules atomically */ if (pe->module && !try_module_get (pe->module)) {
@@ -47,14 +46,14 @@ struct ip_vs_pe *__ip_vs_pe_getbyname(const char *pe_name) }if (strcmp (pe_name, pe->name)==0 ) {/* HIT */ -spin_unlock_bh (&ip_vs_pe_lock); +rcu_read_unlock ();return pe; }if (pe->module)module_put (pe->module); } +rcu_read_unlock (); -spin_unlock_bh (&ip_vs_pe_lock);return NULL ; }
@@ -83,22 +82,13 @@ int register_ip_vs_pe(struct ip_vs_pe *pe)/* increase the module use count */ ip_vs_use_count_inc (); -spin_lock_bh (&ip_vs_pe_lock); - -if (!list_empty (&pe->n_list)) { -spin_unlock_bh (&ip_vs_pe_lock); -ip_vs_use_count_dec (); -pr_err ("%s(): [%s] pe already linked\n" , - __func__, pe->name); -return -EINVAL ; - } - +mutex_lock (&ip_vs_pe_mutex);/* Make sure that the pe with this name doesn't exist * in the pe list. */ list_for_each_entry (tmp, &ip_vs_pe, n_list) {if (strcmp (tmp->name, pe->name) ==0 ) { -spin_unlock_bh (&ip_vs_pe_lock); +mutex_unlock (&ip_vs_pe_mutex);ip_vs_use_count_dec ();pr_err ("%s(): [%s] pe already existed " "in the system\n" , __func__, pe->name);
@@ -106,8 +96,8 @@ int register_ip_vs_pe(struct ip_vs_pe *pe) } }/* Add it into the d-linked pe list */ -list_add (&pe->n_list, &ip_vs_pe); -spin_unlock_bh (&ip_vs_pe_lock); +list_add_rcu (&pe->n_list, &ip_vs_pe); +mutex_unlock (&ip_vs_pe_mutex);pr_info ("[%s] pe registered.\n" , pe->name);
@@ -118,17 +108,10 @@ EXPORT_SYMBOL_GPL(register_ip_vs_pe);/* Unregister a pe from the pe list */ int unregister_ip_vs_pe (struct ip_vs_pe *pe) { -spin_lock_bh (&ip_vs_pe_lock); -if (list_empty (&pe->n_list)) { -spin_unlock_bh (&ip_vs_pe_lock); -pr_err ("%s(): [%s] pe is not in the list. failed\n" , - __func__, pe->name); -return -EINVAL ; - } - +mutex_lock (&ip_vs_pe_mutex);/* Remove it from the d-linked pe list */ -list_del (&pe->n_list); -spin_unlock_bh (&ip_vs_pe_lock); +list_del_rcu (&pe->n_list); +mutex_unlock (&ip_vs_pe_mutex);/* decrease the module use count */ ip_vs_use_count_dec ();
net/netfilter/ipvs/ip_vs_pe_sip.c +1 -0
@@ -172,6 +172,7 @@ static int __init ip_vs_sip_init(void) static void __exit ip_vs_sip_cleanup(void) {unregister_ip_vs_pe (&ip_vs_sip_pe); +rcu_barrier (); }module_init (ip_vs_sip_init);
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 10/12 ipvs: convert connection locking
In reply to Julian Anastasov Convert __ip_vs_conntbl_lock_array as follows:
- readers that do not modify conn lists will use RCU lock
- updaters that modify lists will use spinlock_t
Now for conn lookups we will use RCU read-side
critical section. Without using __ip_vs_conn_get such places have access to connection fields and can dereference some pointers like pe and pe_data plus the ability to update timer expiration. If full access is required we contend for reference.
We add barrier in __ip_vs_conn_put, so that
other CPUs see the refcnt operation after other writes.
With the introduction of ip_vs_conn_unlink()
we try to reorganize ip_vs_conn_expire(), so that unhashing of connections that should stay more time is avoided, even if it is for very short time.
--- include/net/ip_vs.h | 12 ++ net/netfilter/ipvs/ip_vs_conn.c | 230 +++++++++++++++++++++------------------ 2 files changed, 134 insertions(+), 108 deletions(-)
include/net/ip_vs.h +12 -0
@@ -620,6 +620,8 @@ struct ip_vs_conn {const struct ip_vs_pe *pe;char *pe_data;__u8 pe_data_len; + +struct rcu_head rcu_head; };/*
@@ -1173,9 +1175,19 @@ struct ip_vs_conn * ip_vs_conn_out_get_proto(int af, const struct sk_buff *skb,const struct ip_vs_iphdr *iph, int inverse); +/* Get reference to gain full access to conn. +* By default, RCU read-side critical sections have access only to +* conn fields and its PE data, see ip_vs_conn_rcu_free() for reference. +*/ +static inline bool __ip_vs_conn_get (struct ip_vs_conn *cp) +{ +return atomic_inc_not_zero (&cp->refcnt); +} +/* put back the conn without restarting its timer */ static inline void __ip_vs_conn_put (struct ip_vs_conn *cp) { +smp_mb__before_atomic_dec ();atomic_dec (&cp->refcnt); }extern void ip_vs_conn_put (struct ip_vs_conn *cp);
net/netfilter/ipvs/ip_vs_conn.c +122 -108
469 lines not on this page · raw message
Signature · 1 line
1.7.3.4
Julian Anastasov
maintainerPATCH 11/12 ipvs: reorder keys in connection structure
In reply to Julian Anastasov __ip_vs_conn_in_get and ip_vs_conn_out_get are
hot places. Optimize them, so that ports are matched first. By moving net and fwmark below, on 32-bit arch we can fit caddr in 32-byte cache line and all addresses in 64-byte cache line.
--- include/net/ip_vs.h | 12 ++++++------ net/netfilter/ipvs/ip_vs_conn.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)
include/net/ip_vs.h +6 -6
@@ -566,20 +566,19 @@ struct ip_vs_conn_param {*/ struct ip_vs_conn {struct hlist_node c_list;/* hashed list heads */ -#ifdef CONFIG_NET_NS -struct net *net;/* Name space */ -#endif /* Protocol, addresses and port numbers */ -u16 af;/* address family */ __be16 cport; -__be16 vport;__be16 dport; -__u32 fwmark;/* Fire wall mark from skb */ +__be16 vport; +u16 af;/* address family */ union nf_inet_addr caddr;/* client address */ union nf_inet_addr vaddr;/* virtual address */ union nf_inet_addr daddr;/* destination address */ volatile __u32 flags;/* status flags */ __u16 protocol;/* Which protocol (TCP/UDP) */ +#ifdef CONFIG_NET_NS +struct net *net;/* Name space */ +#endif /* counter and timer */ atomic_t refcnt;/* reference count */
@@ -593,6 +592,7 @@ struct ip_vs_conn {* state transition triggerd * synchronization */ +__u32 fwmark;/* Fire wall mark from skb */ unsigned long sync_endtime;/* jiffies + sent_retries */ /* Control members */
net/netfilter/ipvs/ip_vs_conn.c +4 -4
@@ -265,8 +265,8 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->cport == cp->cport && p->vport == cp->vport && + if (p->cport == cp->cport && p->vport == cp->vport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&
@@ -404,8 +404,8 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->vport == cp->cport && p->cport == cp->dport && + if (p->vport == cp->cport && p->cport == cp->dport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && p->protocol == cp->protocol &&
Signature · 1 line
1.7.3.4
Re: [PATCH net-next 11/12] ipvs: reorder keys in connection structure
Hi Julian
Great job you have done !
I'll test it immediate...
Quoted 5 lines · Julian Anastasov
__ip_vs_conn_in_getandip_vs_conn_out_getare
hot places. Optimize them, so that ports are matched first. By moving net and fwmark below, on 32-bit arch we can fit caddr in 32-byte cache line and all addresses in 64-byte cache line.
Earlier I made some rearrangements like the one you have made. My conclusion at that time was that the best gain was to have fwmark and net within the first 64 bytes, and move daddr to the next cache line.
I uesd UDP at ~7Gbit/sec and 256k source address into a x86_64 machine, and a 50/50 mix of fwmarks and port in that tests.
I guess that you have made similar test, and even take ip_vs_conn_out_get() into your calculations ?
Regards
Hans
Quoted 71 lines
Signed-off-by Julian Anastasov <[email protected]>--- include/net/ip_vs.h | 12 ++++++------ net/netfilter/ipvs/ip_vs_conn.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)include/net/ip_vs.h +6
-6@@ -566,20 +566,19 @@ struct ip_vs_conn_param {*/ struct ip_vs_conn {struct hlist_node c_list;/* hashed list heads */ -#ifdef CONFIG_NET_NS -struct net *net;/* Name space */ -#endif /* Protocol, addresses and port numbers */ -u16 af;/* address family */ __be16 cport; -__be16 vport;__be16 dport; -__u32 fwmark;/* Fire wall mark from skb */ +__be16 vport; +u16 af;/* address family */ union nf_inet_addr caddr;/* client address */ union nf_inet_addr vaddr;/* virtual address */ union nf_inet_addr daddr;/* destination address */ volatile __u32 flags;/* status flags */ __u16 protocol;/* Which protocol (TCP/UDP) */ +#ifdef CONFIG_NET_NS +struct net *net;/* Name space */ +#endif /* counter and timer */ atomic_t refcnt;/* reference count */ @@ -593,6 +592,7 @@ struct ip_vs_conn {* state transition triggerd * synchronization */ +__u32 fwmark;/* Fire wall mark from skb */ unsigned long sync_endtime;/* jiffies + sent_retries */ /* Control members */ net/netfilter/ipvs/ip_vs_conn.c +4
-4@@ -265,8 +265,8 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->cport == cp->cport && p->vport == cp->vport && + if (p->cport == cp->cport && p->vport == cp->vport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&@@ -404,8 +404,8 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->vport == cp->cport && p->cport == cp->dport && + if (p->vport == cp->cport && p->cport == cp->dport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && p->protocol == cp->protocol &&
Julian Anastasov
maintainerRe: [PATCH net-next 11/12] ipvs: reorder keys in connection structure
Hello,
Quoted 3 lines · Hans Schillstrom
Hi Julian
Great job you have done !
I'll test it immediate...
Thanks, it would be good to catch the problems
in early phase...
Quoted 11 lines · Julian Anastasov
Quoted 5 lines
__ip_vs_conn_in_getandip_vs_conn_out_getare
hot places. Optimize them, so that ports are matched first. By moving net and fwmark below, on 32-bit arch we can fit caddr in 32-byte cache line and all addresses in 64-byte cache line.Earlier I made some rearrangements like the one you have made. My conclusion at that time was that the best gain was to have fwmark and net within the first 64 bytes, and move daddr to the next cache line.
But fwmark is used only for lookups in backup
server. The net field is checked first only in ip_vs_ct_in_get (on scheduling), it can be optimized too. Modern CPUs have 64-byte cache line and may be the places of these fields do not play much because checking the two ports is enough to differentiate most of the connections. The addresses play when ports do not differ, i.e. mostly for persistent connections. So, on 64-byte cache line it would be more difficult to see any difference.
Quoted 5 lines
I uesd UDP at ~7Gbit/sec and 256k source address into a
x86_64machine, and a 50/50 mix of fwmarks and port in that tests.I guess that you have made similar test, and even take
ip_vs_conn_out_get()into your calculations ?
No, I have only virtual boxes for tests...
Quoted 66 lines
Regards
HansQuoted 63 lines
Signed-off-by Julian Anastasov <[email protected]>--- include/net/ip_vs.h | 12 ++++++------ net/netfilter/ipvs/ip_vs_conn.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)include/net/ip_vs.h +6
-6@@ -566,20 +566,19 @@ struct ip_vs_conn_param {*/ struct ip_vs_conn {struct hlist_node c_list;/* hashed list heads */ -#ifdef CONFIG_NET_NS -struct net *net;/* Name space */ -#endif /* Protocol, addresses and port numbers */ -u16 af;/* address family */ __be16 cport; -__be16 vport;__be16 dport; -__u32 fwmark;/* Fire wall mark from skb */ +__be16 vport; +u16 af;/* address family */ union nf_inet_addr caddr;/* client address */ union nf_inet_addr vaddr;/* virtual address */ union nf_inet_addr daddr;/* destination address */ volatile __u32 flags;/* status flags */ __u16 protocol;/* Which protocol (TCP/UDP) */ +#ifdef CONFIG_NET_NS +struct net *net;/* Name space */ +#endif /* counter and timer */ atomic_t refcnt;/* reference count */ @@ -593,6 +592,7 @@ struct ip_vs_conn {* state transition triggerd * synchronization */ +__u32 fwmark;/* Fire wall mark from skb */ unsigned long sync_endtime;/* jiffies + sent_retries */ /* Control members */ net/netfilter/ipvs/ip_vs_conn.c +2
-2@@ -265,8 +265,8 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->cport == cp->cport && p->vport == cp->vport && + if (p->cport == cp->cport && p->vport == cp->vport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&@@ -404,8 +404,8 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)rcu_read_lock ();
Quoted 8 lines
net/netfilter/ipvs/ip_vs_conn.c struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p) · line 411
Quoted 8 lines
net/netfilter/ipvs/ip_vs_conn.c +2
-2hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->vport == cp->cport && p->cport == cp->dport && + if (p->vport == cp->cport && p->cport == cp->dport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && p->protocol == cp->protocol &&
Regards
--
Julian Anastasov <[email protected]>
Re: [PATCH net-next 11/12] ipvs: reorder keys in connection structure
Hi Julian
Quoted 23 lines · Julian Anastasov
Hello,
Quoted 3 lines · Hans Schillstrom
Hi Julian
Great job you have done !
I'll test it immediate...Thanks, it would be good to catch the problems
in early phase...Quoted 11 lines · Julian Anastasov
Quoted 5 lines
__ip_vs_conn_in_getandip_vs_conn_out_getare
hot places. Optimize them, so that ports are matched first. By moving net and fwmark below, on 32-bit arch we can fit caddr in 32-byte cache line and all addresses in 64-byte cache line.Earlier I made some rearrangements like the one you have made. My conclusion at that time was that the best gain was to have fwmark and net within the first 64 bytes, and move daddr to the next cache line.
Quoted 10 lines
But fwmark is used only for lookups in backup
server. The net field is checked first only inip_vs_ct_in_get(on scheduling), it can be optimized too. Modern CPUs have 64-byte cache line and may be the places of these fields do not play much because checking the two ports is enough to differentiate most of the connections. The addresses play when ports do not differ, i.e. mostly for persistent connections. So, on 64-byte cache line it would be more difficult to see any difference.
I made some tests on weaker machine (i7-3930K) with moderate background load, there is absolute no measurable difference with daddr in first cache line or in second line.
So based on that I prefer your solution since it keeps data together.
Quoted 13 lines
Quoted 5 lines
I uesd UDP at ~7Gbit/sec and 256k source address into a
x86_64machine, and a 50/50 mix of fwmarks and port in that tests.I guess that you have made similar test, and even take
ip_vs_conn_out_get()into your calculations ?No, I have only virtual boxes for tests...
Quoted 5 lines
Regards
HansQuoted 2 lines
Signed-off-by Julian Anastasov <[email protected]>
Quoted 74 lines
Quoted 61 lines
Quoted 61 lines
--- include/net/ip_vs.h | 12 ++++++------ net/netfilter/ipvs/ip_vs_conn.c | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-)include/net/ip_vs.h +6
-6@@ -566,20 +566,19 @@ struct ip_vs_conn_param {*/ struct ip_vs_conn {struct hlist_node c_list;/* hashed list heads */ -#ifdef CONFIG_NET_NS -struct net *net;/* Name space */ -#endif /* Protocol, addresses and port numbers */ -u16 af;/* address family */ __be16 cport; -__be16 vport;__be16 dport; -__u32 fwmark;/* Fire wall mark from skb */ +__be16 vport; +u16 af;/* address family */ union nf_inet_addr caddr;/* client address */ union nf_inet_addr vaddr;/* virtual address */ union nf_inet_addr daddr;/* destination address */ volatile __u32 flags;/* status flags */ __u16 protocol;/* Which protocol (TCP/UDP) */ +#ifdef CONFIG_NET_NS +struct net *net;/* Name space */ +#endif /* counter and timer */ atomic_t refcnt;/* reference count */ @@ -593,6 +592,7 @@ struct ip_vs_conn {* state transition triggerd * synchronization */ +__u32 fwmark;/* Fire wall mark from skb */ unsigned long sync_endtime;/* jiffies + sent_retries */ /* Control members */ net/netfilter/ipvs/ip_vs_conn.c +2
-2@@ -265,8 +265,8 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)rcu_read_lock ();hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->cport == cp->cport && p->vport == cp->vport && + if (p->cport == cp->cport && p->vport == cp->vport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->vaddr, &cp->vaddr) && ((!p->cport) ^ (!(cp->flags & IP_VS_CONN_F_NO_CPORT))) &&@@ -404,8 +404,8 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)rcu_read_lock ();Quoted 8 lines
net/netfilter/ipvs/ip_vs_conn.c struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p) · line 411
Quoted 8 lines
net/netfilter/ipvs/ip_vs_conn.c +2
-2hlist_for_each_entry_rcu (cp, &ip_vs_conn_tab[hash], c_list) { - if (cp->af == p->af && - p->vport == cp->cport && p->cport == cp->dport && + if (p->vport == cp->cport && p->cport == cp->dport && + cp->af == p->af && ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) && ip_vs_addr_equal(p->af, p->caddr, &cp->daddr) && p->protocol == cp->protocol &&Regards
--
Julian Anastasov <[email protected]>
Julian Anastasov
maintainerRe: [PATCH net-next 11/12] ipvs: reorder keys in connection structure
Hello,
Quoted 4 lines · Hans Schillstrom
I made some tests on weaker machine (i7-3930K) with moderate background load, there is absolute no measurable difference with daddr in first cache line or in second line.
So based on that I prefer your solution since it keeps data together.
Thanks for the review and the tests. But I'll
slightly extend this patch in v2 with more optimizations...
Regards
--
Julian Anastasov <[email protected]>
Julian Anastasov
maintainerPATCH 12/12 ipvs: avoid kmem_cache_zalloc in ip_vs_conn_new
In reply to Julian Anastasov We have many fields to set and few to reset,
use kmem_cache_alloc instead to save some cycles.
--- net/netfilter/ipvs/ip_vs_conn.c | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-)
net/netfilter/ipvs/ip_vs_conn.c +15 -1
@@ -860,7 +860,7 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p,struct ip_vs_proto_data *pd =ip_vs_proto_data_get (p->net, p->protocol); - cp =kmem_cache_zalloc (ip_vs_conn_cachep,GFP_ATOMIC ); + cp =kmem_cache_alloc (ip_vs_conn_cachep,GFP_ATOMIC );if (cp ==NULL ) {IP_VS_ERR_RL ("%s(): no memory\n" , __func__);return NULL ;
@@ -886,6 +886,10 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p, cp->pe = p->pe; cp->pe_data = p->pe_data; cp->pe_data_len = p->pe_data_len; + }else { + cp->pe =NULL ; + cp->pe_data =NULL ; + cp->pe_data_len =0 ; }spin_lock_init (&cp->lock);
@@ -896,18 +900,28 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p,*/ atomic_set (&cp->refcnt,1 ); + cp->control =NULL ;atomic_set (&cp->n_control,0 );atomic_set (&cp->in_pkts,0 ); + cp->packet_xmit =NULL ; + cp->app =NULL ; + cp->app_data =NULL ; +/* reset struct ip_vs_seq */ + cp->in_seq.delta =0 ; + cp->out_seq.delta =0 ; +atomic_inc (&ipvs->conn_count);if (flags &IP_VS_CONN_F_NO_CPORT )atomic_inc (&ip_vs_conn_no_cport_cnt);/* Bind the connection with a destination server */ + cp->dest =NULL ;ip_vs_bind_dest (cp, dest);/* Set its state and timeout */ cp->state =0 ; + cp->old_state =0 ; cp->timeout =3 *HZ ; cp->sync_endtime = jiffies & ~3UL ;
Signature · 1 line
1.7.3.4
Quoted 4 lines · Julian Anastasov
This is a first patchset for IPVS optimizations.
Another patchset will address the locking in schedulers and moving the global_bhdisabling fromLOCAL_OUTto all locks. It is in TODO list.
Do you have any performance measurements?
Or is this code primarily cleanup optimizations, and the next patchset will address the performance part?
Signature · 5 lines
Best regards, Jesper Dangaard Brouer MSc.CS, Sr. Network Kernel Developer at Red Hat Author of http://www.iptv-analyzer.org LinkedIn: http://www.linkedin.com/in/brouer
Julian Anastasov
maintainerHello,
Quoted 7 lines · Jesper Dangaard Brouer
Quoted 4 lines · Julian Anastasov
This is a first patchset for IPVS optimizations.
Another patchset will address the locking in schedulers and moving the global_bhdisabling fromLOCAL_OUTto all locks. It is in TODO list.Do you have any performance measurements?
Unfortunately, I don't have suitable
hardware to do real tests, only virtual setup.
Quoted 2 lines
Or is this code primarily cleanup optimizations, and the next patchset will address the performance part?
This patchset converts all global locks to RCU,
except the __ip_vs_svc_lock usage. The states in some schedulers probably will need some protection with lock as before now, others like WLC and LC will run (after patchset2) just under RCU lock for the svc->destinations list. But I'll need some time to finalize this 2nd patchset because there are other patches in the queue...
So, the answer is that packets that create
connections (via schedulers) hit this __ip_vs_svc_lock with read_lock and some write lock in scheduler. This happens only when connection is created. Next packets should not reach any global locks in IPVS. The cached dst->refcnt is not changed anymore for traffic to real server.
Regards
--
Julian Anastasov <[email protected]>