ipvs: per-net tables and optimizations
Subsystems (7)
Julian Anastasov
maintainerv2 cover ipvs: per-net tables and optimizations
Hello,
This patchset targets more netns isolation when IPVS
is used in large setups and also includes some optimizations. This is a RFC submission to get wider review and comments.
First patch adds useful wrappers to rculist_bl, thehlist_bl methods IPVS will use in the following patches. The other patches are IPVS-specific.
The following patches will:
* Convert the global __ip_vs_mutex to per-net service_mutex and switch the service tables to be per-net, cowork by Jiejian Wu and Dust Li
* Convert some code that walks the service lists to use RCU instead of the service_mutex
* We used two tables for services (non-fwmark and fwmark), merge them into single svc_table
* The list for unavailable destinations (dest_trash) holds dsts and thus dev references causing extra work for the ip_vs_dst_event() dev notifier handler. Change this by dropping the reference when dest is removed and saved into dest_trash. The dest_trash will need more changes to make it light for lookups. TODO.
* On new connection we can do multiple lookups for services by tryng different fallback options. Add more counters for service types, so that we can avoid unneeded lookups for services.
* Add infrastructure for resizable hash tables based on hlist_bl which we will use for services and connections: hlists with per-bucket bit lock in the heads. The resizing delays RCU lookups on a bucket level with seqcounts which are protected with spin locks.
* Change the 256-bucket service hash table to be resizable in the range of 4..20 bits depending on the added services and use jhash hashing to reduce the collisions.
* Change the global connection table to be per-net and resizable in the range of 256..ip_vs_conn_tab_size. As the connections are hashed by using remote addresses and ports, use siphash instead of jhash for better security.
* As the connection table is not fixed size, show its current size to user space
* As the connection table is not global anymore, the no_cport and dropentry counters can be per-net
* Make the connection hashing more secure for setups with multiple services. Hashing only by remote address and port (client info) is not enough. To reduce the possible hash collisions add the used virtual address/port (local info) into the hash and as a side effect the MASQ connections will be double hashed into the hash table to match the traffic from real servers:
OLD:
- all methods: c_list node: proto, caddr:cport
NEW:
- all methods: hn0 node (dir 0): proto, caddr:cport -> vaddr:vport
- MASQ method: hn1 node (dir 1): proto, daddr:dport -> caddr:cport
* Add /proc/net/ip_vs_status to show current state of IPVS, per-net
cat /proc/net/ip_vs_status
Conns: 9401
Conn buckets: 524288 (19 bits, lfactor 5)
Conn buckets empty: 505633 (96%)
Conn buckets len-1: 18322 (98%)
Conn buckets len-2: 329 (1%)
Conn buckets len-3: 3 (0%)
Conn buckets len-4: 1 (0%)
Services: 12
Service buckets: 128 (7 bits, lfactor 3)
Service buckets empty: 116 (90%)
Service buckets len-1: 12 (100%)
Stats thread slots: 1 (max 16)
Stats chain max len: 16
Stats thread ests: 38400
It shows the table size, the load factor, how many are the empty buckets, with percents from the all buckets, the number of buckets with length 1..7 where len-7 catches all len>=7 (zero values are not shown). The len-N percents ignore the empty buckets, so they are relative among all len-N buckets. It shows that large lfactor is needed to achieve len-1 buckets to be ~98%. Only real tests can show if relying on len-1 buckets is a better option because the hash table becomes too large with multiple connections. And as every table uses random key, the services may not avoid collision in all cases.
* add conn_lfactor and svc_lfactor sysctl vars, so that one can tune the connection/service hash table sizing
Changes in v2:
Patch 1:
* add comments to hlist_bl_for_each_entry_continue_rcu and fix sparse warnings
Patch 9:
* Simon Kirby reports that backup server crashes if conn_tab is not created. Create it just to sync conns before any services are added.
Patch 11:
* kernel test robot reported for dropentry_counters problem when compiling with !CONFIG_SYSCTL, so it is time to wrap todrop_entry, ip_vs_conn_ops_mode and ip_vs_random_dropentry under CONFIG_SYSCTL
Patch 13:
* remove extra old_gen assignment at start of ip_vs_status_show()
Jiejian Wu (1):
ipvs: make ip_vs_svc_table and ip_vs_svc_fwm_table per netns
Julian Anastasov (13):
rculist_bl: add hlist_bl_for_each_entry_continue_rcu
ipvs: some service readers can use RCU
ipvs: use single svc table
ipvs: do not keep dest_dst after dest is removed
ipvs: use more counters to avoid service lookups
ipvs: add resizable hash tables
ipvs: use resizable hash table for services
ipvs: switch to per-net connection table
ipvs: show the current conn_tab size to users
ipvs: no_cport and dropentry counters can be per-net
ipvs: use more keys for connection hashing
ipvs: add ip_vs_status info
ipvs: add conn_lfactor and svc_lfactor sysctl vars
Documentation/networking/ipvs-sysctl.rst | 31 + include/linux/rculist_bl.h | 49 +- include/net/ip_vs.h | 395 ++++++- net/netfilter/ipvs/ip_vs_conn.c | 1074 ++++++++++++++----- net/netfilter/ipvs/ip_vs_core.c | 171 ++- net/netfilter/ipvs/ip_vs_ctl.c | 1231 ++++++++++++++++------ net/netfilter/ipvs/ip_vs_est.c | 18 +- net/netfilter/ipvs/ip_vs_pe_sip.c | 4 +- net/netfilter/ipvs/ip_vs_sync.c | 23 + net/netfilter/ipvs/ip_vs_xmit.c | 39 +- 10 files changed, 2342 insertions(+), 693 deletions(-)
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 1/14 rculist_bl: add hlist_bl_for_each_entry_continue_rcu
Change the old hlist_bl_first_rcu to hlist_bl_first_rcu_dereference to indicate that it is a RCU dereference.
Add hlist_bl_next_rcu and hlist_bl_first_rcu to use RCU pointers and use them to fix sparse warnings.
Add hlist_bl_for_each_entry_continue_rcu.
--- include/linux/rculist_bl.h | 49 +++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 9 deletions(-)
include/linux/rculist_bl.h +40 -9
@@ -8,21 +8,31 @@#include <linux/list_bl.h> #include <linux/rcupdate.h> +/* return the first ptr or next element in an RCU protected list */ +#define hlist_bl_first_rcu (head) \ + (*((struct hlist_bl_node __rcu **)(&(head)->first))) +#define hlist_bl_next_rcu (node) \ + (*((struct hlist_bl_node __rcu **)(&(node)->next))) +static inline void hlist_bl_set_first_rcu (struct hlist_bl_head *h,struct hlist_bl_node *n) {LIST_BL_BUG_ON ((unsigned long )n &LIST_BL_LOCKMASK );LIST_BL_BUG_ON (((unsigned long )h->first &LIST_BL_LOCKMASK ) !=LIST_BL_LOCKMASK ); -rcu_assign_pointer (h->first, +rcu_assign_pointer (hlist_bl_first_rcu (h), (struct hlist_bl_node *)((unsigned long )n |LIST_BL_LOCKMASK )); } -static inline struct hlist_bl_node *hlist_bl_first_rcu (struct hlist_bl_head *h) -{ -return (struct hlist_bl_node *) - ((unsigned long )rcu_dereference_check (h->first,hlist_bl_is_locked (h)) & ~LIST_BL_LOCKMASK ); -} +#define hlist_bl_first_rcu_dereference (head) \ +({ \ + struct hlist_bl_head *__head = (head); \ + \ + (struct hlist_bl_node *) \ + ((unsigned long)rcu_dereference_check(hlist_bl_first_rcu(__head), \ + hlist_bl_is_locked(__head)) & \ + ~LIST_BL_LOCKMASK); \ +})/** * hlist_bl_del_rcu - deletes entry from hash list without re-initialization
@@ -73,7 +83,7 @@ static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n, {struct hlist_bl_node *first; -/* don't need hlist_bl_first_rcu because we're under lock */ +/* don't need hlist_bl_first_rcu* because we're under lock */ first =hlist_bl_first (h); n->next = first;
@@ -93,9 +103,30 @@ static inline void hlist_bl_add_head_rcu(struct hlist_bl_node *n,* */ #define hlist_bl_for_each_entry_rcu (tpos, pos, head, member) \ - for (pos = hlist_bl_first_rcu(head); \ + for (pos = hlist_bl_first_rcu_dereference(head); \ pos && \ ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; }); \ - pos = rcu_dereference_raw(pos->next)) + pos = rcu_dereference_raw(hlist_bl_next_rcu(pos))) + +/** +* hlist_bl_for_each_entry_continue_rcu - continue iteration over list of given +* type +* @tpos: the type * to use as a loop cursor. +* @pos: the &struct hlist_bl_node to use as a loop cursor. +* @member: the name of the hlist_bl_node within the struct. +* +* Continue to iterate over list of given type, continuing after +* the current position which must have been in the list when the RCU read +* lock was taken. +* This would typically require either that you obtained the node from a +* previous walk of the list in the same RCU read-side critical section, or +* that you held some sort of non-RCU reference (such as a reference count) +* to keep the node alive *and* in the list. +*/ +#define hlist_bl_for_each_entry_continue_rcu (tpos, pos, member) \ + for (pos = rcu_dereference_raw(hlist_bl_next_rcu(&(tpos)->member)); \ + pos && \ + ({ tpos = hlist_bl_entry(pos, typeof(*tpos), member); 1; }); \ + pos = rcu_dereference_raw(hlist_bl_next_rcu(pos)))#endif
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 2/14 ipvs: make ip_vs_svc_table and ip_vs_svc_fwm_table per netns
In reply to Julian AnastasovFrom: Jiejian Wu <[email protected]>
Current ipvs uses one global mutex "__ip_vs_mutex" to keep the global "ip_vs_svc_table" and "ip_vs_svc_fwm_table" safe. But when there are tens of thousands of services from different netns in the table, it takes a long time to look up the table, for example, using "ipvsadm -ln" from different netns simultaneously.
We make "ip_vs_svc_table" and "ip_vs_svc_fwm_table" per netns, and we add "service_mutex" per netns to keep these two tables safe instead of the global "__ip_vs_mutex" in current version. To this end, looking up services from different netns simultaneously will not get stuck, shortening the time consumption in large-scale deployment. It can be reproduced using the simple scripts below.
init.sh: #!/bin/bash
for((i=1;i<=4;i++));do
ip netns add ns$i
ip netns exec ns$i ip link set dev lo up
ip netns exec ns$i sh add-services.sh
done
add-services.sh: #!/bin/bash
for((i=0;i<30000;i++)); do
ipvsadm -A -t 10.10.10.10:$((80+$i)) -s rr
done
runtest.sh: #!/bin/bash for((i=1;i<4;i++));do
ip netns exec ns$i ipvsadm -ln > /dev/null &
done
ip netns exec ns4 ipvsadm -ln > /dev/null
Run "sh init.sh" to initiate the network environment. Then run "time ./runtest.sh" to evaluate the time consumption. Our testbed is a 4-core Intel Xeon ECS. The result of the original version is around 8 seconds, while the result of the modified version is only 0.8 seconds.
--- include/net/ip_vs.h | 13 +++ net/netfilter/ipvs/ip_vs_ctl.c | 167 ++++++++++++++------------------- net/netfilter/ipvs/ip_vs_est.c | 18 ++-- 3 files changed, 94 insertions(+), 104 deletions(-)
include/net/ip_vs.h +13 -0
@@ -33,6 +33,12 @@#define IP_VS_HDR_INVERSE 1#define IP_VS_HDR_ICMP 2 +/* +* Hash table: for virtual service lookups +*/ +#define IP_VS_SVC_TAB_BITS 8 +#define IP_VS_SVC_TAB_SIZE BIT(IP_VS_SVC_TAB_BITS) +#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1)/* Generic access of ipvs struct */ static inline struct netns_ipvs *net_ipvs (struct net * net)
@@ -1041,6 +1047,13 @@ struct netns_ipvs {*/ unsigned int mixed_address_family_dests;unsigned int hooks_afmask;/* &1=AF_INET, &2=AF_INET6 */ + +/* the service mutex that protect svc_table and svc_fwm_table */ +struct mutex service_mutex; +/* the service table hashed by <protocol, addr, port> */ +struct hlist_head svc_table[IP_VS_SVC_TAB_SIZE ]; +/* the service table hashed by fwmark */ +struct hlist_head svc_fwm_table[IP_VS_SVC_TAB_SIZE ]; };#define DEFAULT_SYNC_THRESHOLD 3
net/netfilter/ipvs/ip_vs_ctl.c +72 -95
423 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_est.c +9 -9
@@ -601,7 +601,7 @@ static void ip_vs_est_drain_temp_list(struct netns_ipvs *ipvs)while (1 ) {int max =16 ; -mutex_lock (&__ip_vs_mutex); +mutex_lock (&ipvs->service_mutex);while (max-- >0 ) { est = hlist_entry_safe(ipvs->est_temp_list.first,
@@ -621,12 +621,12 @@ static void ip_vs_est_drain_temp_list(struct netns_ipvs *ipvs) } goto unlock; } -mutex_unlock (&__ip_vs_mutex); +mutex_unlock (&ipvs->service_mutex);cond_resched (); } unlock: -mutex_unlock (&__ip_vs_mutex); +mutex_unlock (&ipvs->service_mutex); }/* Calculate limits for all kthreads */
@@ -646,9 +646,9 @@ static int ip_vs_est_calc_limits(struct netns_ipvs *ipvs, int *chain_max)u64 val;INIT_HLIST_HEAD (&chain); -mutex_lock (&__ip_vs_mutex); +mutex_lock (&ipvs->service_mutex); kd = ipvs->est_kt_arr[0 ]; -mutex_unlock (&__ip_vs_mutex); +mutex_unlock (&ipvs->service_mutex); s = kd ? kd->calc_stats :NULL ;if (!s) goto out;
@@ -747,7 +747,7 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)if (!ip_vs_est_calc_limits (ipvs, &chain_max))return ; -mutex_lock (&__ip_vs_mutex); +mutex_lock (&ipvs->service_mutex);/* Stop all other tasks, so that we can immediately move the * estimators to est_temp_list without RCU grace period
@@ -814,9 +814,9 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)/* Give chance estimators to be added (to est_temp_list) * and deleted (releasing kthread contexts) */ -mutex_unlock (&__ip_vs_mutex); +mutex_unlock (&ipvs->service_mutex);cond_resched (); -mutex_lock (&__ip_vs_mutex); +mutex_lock (&ipvs->service_mutex);/* Current kt released ? */ if (id >= ipvs->est_kt_count)
@@ -892,7 +892,7 @@ static void ip_vs_est_calc_phase(struct netns_ipvs *ipvs)mutex_unlock (&ipvs->est_mutex); unlock: -mutex_unlock (&__ip_vs_mutex); +mutex_unlock (&ipvs->service_mutex); }void ip_vs_zero_estimator (struct ip_vs_stats *stats)
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 3/14 ipvs: some service readers can use RCU
In reply to Julian AnastasovSome places walk the services under mutex but they can just use RCU:
* ip_vs_dst_event() uses ip_vs_forget_dev() which uses its own lock to modify dest
* ip_vs_genl_dump_services(): ip_vs_genl_fill_service() just fills skb
* ip_vs_genl_parse_service(): move RCU lock to callers ip_vs_genl_set_cmd(), ip_vs_genl_dump_dests() and ip_vs_genl_get_cmd()
* ip_vs_genl_dump_dests(): just fill skb
--- net/netfilter/ipvs/ip_vs_ctl.c | 47 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 24 deletions(-)
net/netfilter/ipvs/ip_vs_ctl.c +23 -24
@@ -1758,23 +1758,21 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name); -mutex_lock (&ipvs->service_mutex); +rcu_read_lock ();for (idx =0 ; idx <IP_VS_SVC_TAB_SIZE ; idx++) { -hlist_for_each_entry (svc, &ipvs->svc_table[idx], s_list) { -list_for_each_entry (dest, &svc->destinations, - n_list) { +hlist_for_each_entry_rcu (svc, &ipvs->svc_table[idx], s_list) +list_for_each_entry_rcu (dest, &svc->destinations, + n_list)ip_vs_forget_dev (dest, dev); - } - } -hlist_for_each_entry (svc, &ipvs->svc_fwm_table[idx], f_list) { -list_for_each_entry (dest, &svc->destinations, - n_list) { +hlist_for_each_entry_rcu (svc, &ipvs->svc_fwm_table[idx], f_list) +list_for_each_entry_rcu (dest, &svc->destinations, + n_list)ip_vs_forget_dev (dest, dev); - } - } } +rcu_read_unlock (); +mutex_lock (&ipvs->service_mutex);spin_lock_bh (&ipvs->dest_trash_lock);list_for_each_entry (dest, &ipvs->dest_trash, t_list) {ip_vs_forget_dev (dest, dev);
@@ -3317,9 +3315,9 @@ static int ip_vs_genl_fill_service(struct sk_buff *skb, goto nla_put_failure; } - sched =rcu_dereference_protected (svc->scheduler,1 ); + sched =rcu_dereference (svc->scheduler); sched_name = sched ? sched->name :"none" ; - pe =rcu_dereference_protected (svc->pe,1 ); + pe =rcu_dereference (svc->pe); if (nla_put_string(skb, IPVS_SVC_ATTR_SCHED_NAME, sched_name) || (pe && nla_put_string(skb, IPVS_SVC_ATTR_PE_NAME, pe->name)) || nla_put(skb, IPVS_SVC_ATTR_FLAGS, sizeof(flags), &flags) ||
@@ -3373,9 +3371,9 @@ static int ip_vs_genl_dump_services(struct sk_buff *skb,struct net *net =sock_net (skb->sk);struct netns_ipvs *ipvs =net_ipvs (net); -mutex_lock (&ipvs->service_mutex); +rcu_read_lock ();for (i =0 ; i <IP_VS_SVC_TAB_SIZE ; i++) { -hlist_for_each_entry (svc, &ipvs->svc_table[i], s_list) { +hlist_for_each_entry_rcu (svc, &ipvs->svc_table[i], s_list) {if (++idx <= start)continue ;if (ip_vs_genl_dump_service (skb, svc, cb) <0 ) {
@@ -3386,7 +3384,7 @@ static int ip_vs_genl_dump_services(struct sk_buff *skb, }for (i =0 ; i <IP_VS_SVC_TAB_SIZE ; i++) { -hlist_for_each_entry (svc, &ipvs->svc_fwm_table[i], f_list) { +hlist_for_each_entry_rcu (svc, &ipvs->svc_fwm_table[i], f_list) {if (++idx <= start)continue ;if (ip_vs_genl_dump_service (skb, svc, cb) <0 ) {
@@ -3397,7 +3395,7 @@ static int ip_vs_genl_dump_services(struct sk_buff *skb, } nla_put_failure: -mutex_unlock (&ipvs->service_mutex); +rcu_read_unlock (); cb->args[0 ] = idx;return skb->len;
@@ -3453,13 +3451,11 @@ static int ip_vs_genl_parse_service(struct netns_ipvs *ipvs, usvc->fwmark =0 ; } -rcu_read_lock ();if (usvc->fwmark) svc =__ip_vs_svc_fwm_find (ipvs, usvc->af, usvc->fwmark);else svc =__ip_vs_service_find (ipvs, usvc->af, usvc->protocol, &usvc->addr, usvc->port); -rcu_read_unlock (); *ret_svc = svc;/* If a full entry was requested, check for the additional fields */
@@ -3586,7 +3582,7 @@ static int ip_vs_genl_dump_dests(struct sk_buff *skb,struct net *net =sock_net (skb->sk);struct netns_ipvs *ipvs =net_ipvs (net); -mutex_lock (&ipvs->service_mutex); +rcu_read_lock ();/* Try to find the service for which to dump destinations */ if (nlmsg_parse_deprecated (cb->nlh,GENL_HDRLEN , attrs,IPVS_CMD_ATTR_MAX , ip_vs_cmd_policy, cb->extack))
@@ -3598,7 +3594,7 @@ static int ip_vs_genl_dump_dests(struct sk_buff *skb, goto out_err;/* Dump the destinations */ -list_for_each_entry (dest, &svc->destinations, n_list) { +list_for_each_entry_rcu (dest, &svc->destinations, n_list) {if (++idx <= start)continue ;if (ip_vs_genl_dump_dest (skb, dest, cb) <0 ) {
@@ -3611,7 +3607,7 @@ static int ip_vs_genl_dump_dests(struct sk_buff *skb, cb->args[0 ] = idx; out_err: -mutex_unlock (&ipvs->service_mutex); +rcu_read_unlock ();return skb->len; }
@@ -3917,9 +3913,12 @@ static int ip_vs_genl_set_cmd(struct sk_buff *skb, struct genl_info *info)if (cmd ==IPVS_CMD_NEW_SERVICE || cmd ==IPVS_CMD_SET_SERVICE ) need_full_svc = true; +/* We use function that requires RCU lock */ +rcu_read_lock (); ret =ip_vs_genl_parse_service (ipvs, &usvc, info->attrs[IPVS_CMD_ATTR_SERVICE ], need_full_svc, &svc); +rcu_read_unlock ();if (ret) goto out;
@@ -4039,7 +4038,7 @@ static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)if (!msg)return -ENOMEM ; -mutex_lock (&ipvs->service_mutex); +rcu_read_lock (); reply =genlmsg_put_reply (msg, info, &ip_vs_genl_family,0 , reply_cmd);if (reply ==NULL )
@@ -4107,7 +4106,7 @@ static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info) out_err:nlmsg_free (msg); out: -mutex_unlock (&ipvs->service_mutex); +rcu_read_unlock ();return ret; }
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 4/14 ipvs: use single svc table
In reply to Julian Anastasovfwmark based services and non-fwmark based services can be hashed in same service table. This reduces the burden of working with two tables.
--- include/net/ip_vs.h | 8 +- net/netfilter/ipvs/ip_vs_ctl.c | 146 +++++---------------------------- 2 files changed, 22 insertions(+), 132 deletions(-)
include/net/ip_vs.h +2 -6
@@ -679,8 +679,7 @@ struct ip_vs_dest_user_kern {* forwarding entries. */ struct ip_vs_service { -struct hlist_node s_list;/* for normal service table */ -struct hlist_node f_list;/* for fwmark-based service table */ +struct hlist_node s_list;/* node in service table */ atomic_t refcnt;/* reference counter */ u16 af;/* address family */
@@ -1050,10 +1049,7 @@ struct netns_ipvs {/* the service mutex that protect svc_table and svc_fwm_table */ struct mutex service_mutex; -/* the service table hashed by <protocol, addr, port> */ -struct hlist_head svc_table[IP_VS_SVC_TAB_SIZE ]; -/* the service table hashed by fwmark */ -struct hlist_head svc_fwm_table[IP_VS_SVC_TAB_SIZE ]; +struct hlist_head svc_table[IP_VS_SVC_TAB_SIZE ];/* Services */ };#define DEFAULT_SYNC_THRESHOLD 3
net/netfilter/ipvs/ip_vs_ctl.c +20 -126
282 lines not on this page · raw message
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 5/14 ipvs: do not keep dest_dst after dest is removed
In reply to Julian AnastasovBefore now dest->dest_dst is not released when server is moved into dest_trash list after removal. As result, we can keep dst/dev references for long time without actively using them.
It is better to avoid walking the dest_trash list when ip_vs_dst_event() receives dev events. So, make sure we do not hold dev references in dest_trash list. As packets can be flying while server is being removed, check the IP_VS_DEST_F_AVAILABLE flag in slow path to ensure we do not save new dev references to removed servers.
--- net/netfilter/ipvs/ip_vs_ctl.c | 20 +++++++---------- net/netfilter/ipvs/ip_vs_xmit.c | 39 ++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 22 deletions(-)
net/netfilter/ipvs/ip_vs_ctl.c +8 -12
@@ -810,7 +810,6 @@ static void ip_vs_dest_free(struct ip_vs_dest *dest) {struct ip_vs_service *svc =rcu_dereference_protected (dest->svc,1 ); -__ip_vs_dst_cache_reset (dest);__ip_vs_svc_put (svc);call_rcu (&dest->rcu_head, ip_vs_dest_rcu_free); }
@@ -1013,10 +1012,6 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest, dest->af = udest->af; -spin_lock_bh (&dest->dst_lock); -__ip_vs_dst_cache_reset (dest); -spin_unlock_bh (&dest->dst_lock); -if (add) {list_add_rcu (&dest->n_list, &svc->destinations); svc->num_dests++;
@@ -1024,6 +1019,10 @@ __ip_vs_update_dest(struct ip_vs_service *svc, struct ip_vs_dest *dest,if (sched && sched->add_dest) sched->add_dest (svc, dest); }else { +spin_lock_bh (&dest->dst_lock); +__ip_vs_dst_cache_reset (dest); +spin_unlock_bh (&dest->dst_lock); + sched =rcu_dereference_protected (svc->scheduler,1 );if (sched && sched->upd_dest) sched->upd_dest (svc, dest);
@@ -1258,6 +1257,10 @@ static void __ip_vs_unlink_dest(struct ip_vs_service *svc, { dest->flags &= ~IP_VS_DEST_F_AVAILABLE ; +spin_lock_bh (&dest->dst_lock); +__ip_vs_dst_cache_reset (dest); +spin_unlock_bh (&dest->dst_lock); +/* * Remove it from the d-linked destination list. */
@@ -1747,13 +1750,6 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, }rcu_read_unlock (); -mutex_lock (&ipvs->service_mutex); -spin_lock_bh (&ipvs->dest_trash_lock); -list_for_each_entry (dest, &ipvs->dest_trash, t_list) { -ip_vs_forget_dev (dest, dev); - } -spin_unlock_bh (&ipvs->dest_trash_lock); -mutex_unlock (&ipvs->service_mutex);return NOTIFY_DONE ; }
net/netfilter/ipvs/ip_vs_xmit.c +29 -10
@@ -317,9 +317,11 @@ __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,if (dest) { dest_dst =__ip_vs_dst_check (dest); -if (likely (dest_dst)) +if (likely (dest_dst)) { rt = (struct rtable *) dest_dst->dst_cache; -else { +if (ret_saddr) + *ret_saddr = dest_dst->dst_saddr.ip; + }else { dest_dst =ip_vs_dest_dst_alloc ();spin_lock_bh (&dest->dst_lock);if (!dest_dst) {
@@ -335,14 +337,24 @@ __ip_vs_get_out_rt(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,ip_vs_dest_dst_free (dest_dst); goto err_unreach; } -__ip_vs_dst_set (dest, dest_dst, &rt->dst,0 ); +/* It is forbidden to attach dest->dest_dst if +* server is deleted. We can see the flag going down, +* for very short period and it must be checked under +* dst_lock. +*/ +if (dest->flags &IP_VS_DEST_F_AVAILABLE ) +__ip_vs_dst_set (dest, dest_dst, &rt->dst,0 ); +else + noref =0 ;spin_unlock_bh (&dest->dst_lock);IP_VS_DBG (10 ,"new dst %pI4, src %pI4, refcnt=%d\n" , &dest->addr.ip, &dest_dst->dst_saddr.ip,rcuref_read (&rt->dst.__rcuref)); +if (ret_saddr) + *ret_saddr = dest_dst->dst_saddr.ip; +if (!noref) +ip_vs_dest_dst_free (dest_dst); } -if (ret_saddr) - *ret_saddr = dest_dst->dst_saddr.ip; }else {__be32 saddr =htonl (INADDR_ANY );
@@ -480,9 +492,11 @@ __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb,if (dest) { dest_dst =__ip_vs_dst_check (dest); -if (likely (dest_dst)) +if (likely (dest_dst)) { rt = (struct rt6_info *) dest_dst->dst_cache; -else { +if (ret_saddr) + *ret_saddr = dest_dst->dst_saddr.in6; + }else {u32 cookie; dest_dst =ip_vs_dest_dst_alloc ();
@@ -503,14 +517,19 @@ __ip_vs_get_out_rt_v6(struct netns_ipvs *ipvs, int skb_af, struct sk_buff *skb, } rt = (struct rt6_info *) dst; cookie =rt6_get_cookie (rt); -__ip_vs_dst_set (dest, dest_dst, &rt->dst, cookie); +if (dest->flags &IP_VS_DEST_F_AVAILABLE ) +__ip_vs_dst_set (dest, dest_dst, &rt->dst, cookie); +else + noref =0 ;spin_unlock_bh (&dest->dst_lock);IP_VS_DBG (10 ,"new dst %pI6, src %pI6, refcnt=%d\n" , &dest->addr.in6, &dest_dst->dst_saddr.in6,rcuref_read (&rt->dst.__rcuref)); +if (ret_saddr) + *ret_saddr = dest_dst->dst_saddr.in6; +if (!noref) +ip_vs_dest_dst_free (dest_dst); } -if (ret_saddr) - *ret_saddr = dest_dst->dst_saddr.in6; }else { noref =0 ; dst = __ip_vs_route_output_v6(net, daddr, ret_saddr, do_xfrm,
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 6/14 ipvs: use more counters to avoid service lookups
In reply to Julian AnastasovWhen new connection is created we can lookup for services multiple times to support fallback options. We already have some counters to skip specific lookups because it costs CPU cycles for hash calculation, etc.
Add more counters for fwmark/non-fwmark services (fwm_services and nonfwm_services) and make all counters per address family.
--- include/net/ip_vs.h | 24 ++++++--- net/netfilter/ipvs/ip_vs_core.c | 2 +- net/netfilter/ipvs/ip_vs_ctl.c | 86 +++++++++++++++++++-------------- 3 files changed, 69 insertions(+), 43 deletions(-)
include/net/ip_vs.h +18 -6
@@ -271,6 +271,18 @@ static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len, pr_err(msg, ##__VA_ARGS__); \ }while (0 ) +/* For arrays per family */ +enum { +IP_VS_AF_INET , +IP_VS_AF_INET6 , + IP_VS_AF_MAX +}; + +static inline int ip_vs_af_index (int af) +{ +return af ==AF_INET6 ?IP_VS_AF_INET6 :IP_VS_AF_INET ; +} +/* The port number of FTP service (in network order). */ #define FTPPORT cpu_to_be16(21)#define FTPDATA cpu_to_be16(20)
@@ -940,17 +952,17 @@ struct netns_ipvs {/* ip_vs_ctl */ struct ip_vs_stats_rcu *tot_stats;/* Statistics & est. */ -int num_services;/* no of virtual services */ -int num_services6;/* IPv6 virtual services */ -/* Trash for destinations */ struct list_head dest_trash;spinlock_t dest_trash_lock;struct timer_list dest_trash_timer;/* expiration timer */ /* Service counters */ -atomic_t ftpsvc_counter; -atomic_t nullsvc_counter; -atomic_t conn_out_counter; +atomic_t num_services[IP_VS_AF_MAX ];/* Services */ +atomic_t fwm_services[IP_VS_AF_MAX ];/* Services */ +atomic_t nonfwm_services[IP_VS_AF_MAX ];/* Services */ +atomic_t ftpsvc_counter[IP_VS_AF_MAX ];/* FTPPORT */ +atomic_t nullsvc_counter[IP_VS_AF_MAX ];/* Zero port */ +atomic_t conn_out_counter[IP_VS_AF_MAX ];/* out conn */ #ifdef CONFIG_SYSCTL /* delayed work for expiring no dest connections */
net/netfilter/ipvs/ip_vs_core.c +1 -1
@@ -1404,7 +1404,7 @@ ip_vs_out_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *statreturn handle_response (af, skb, pd, cp, &iph, hooknum);/* Check for real-server-started requests */ -if (atomic_read (&ipvs->conn_out_counter)) { +if (atomic_read (&ipvs->conn_out_counter[ip_vs_af_index (af)])) {/* Currently only for UDP: * connection oriented protocols typically use * ephemeral ports for outgoing connections, so
net/netfilter/ipvs/ip_vs_ctl.c +50 -36
179 lines not on this page · raw message
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 7/14 ipvs: add resizable hash tables
In reply to Julian AnastasovAdd infrastructure for resizable hash tables based on hlist_bl which we will use in followup patches.
--- include/net/ip_vs.h | 197 ++++++++++++++++++++++++++++++++ net/netfilter/ipvs/ip_vs_conn.c | 5 - net/netfilter/ipvs/ip_vs_core.c | 169 +++++++++++++++++++++++++++ 3 files changed, 366 insertions(+), 5 deletions(-)
include/net/ip_vs.h +197 -0
@@ -11,6 +11,7 @@#include <asm/types.h> /* for __uXX types */ #include <linux/list.h> /* for struct list_head */ +#include <linux/rculist_bl.h> /* for struct hlist_bl_head */ #include <linux/spinlock.h> /* for struct rwlock_t */ #include <linux/atomic.h> /* for struct atomic_t */ #include <linux/refcount.h> /* for struct refcount_t */
@@ -30,6 +31,7 @@#endif #include <net/net_namespace.h> /* Netw namespace */ #include <linux/sched/isolation.h> +#include <linux/siphash.h> #define IP_VS_HDR_INVERSE 1#define IP_VS_HDR_ICMP 2
@@ -271,6 +273,10 @@ static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len, pr_err(msg, ##__VA_ARGS__); \ }while (0 ) +struct ip_vs_aligned_lock { +spinlock_t l;/* Protect buckets */ +}____cacheline_aligned_in_smp ; +/* For arrays per family */ enum {IP_VS_AF_INET ,
@@ -484,6 +490,197 @@ struct ip_vs_est_kt_data {int est_row;/* estimated row */ }; +/* IPVS resizable hash tables */ +struct ip_vs_rht { +struct hlist_bl_head *buckets; +struct ip_vs_rht __rcu *new_tbl;/* New/Same table */ +seqcount_t *seqc;/* Protects moves */ +struct ip_vs_aligned_lock *lock;/* Protect seqc */ +int mask;/* Buckets mask */ +int size;/* Buckets */ +int seqc_mask;/* seqc mask */ +int lock_mask;/* lock mask */ +u32 table_id; +int u_thresh;/* upper threshold */ +int l_thresh;/* lower threshold */ +int lfactor;/* Load Factor (shift)*/ +int bits;/* size = 1 << bits */ +siphash_key_t hash_key; +struct rcu_head rcu_head; +}; + +/** +* ip_vs_rht_for_each_table() - Walk the hash tables +* @table: struct ip_vs_rht __rcu *table +* @t: current table, used as cursor, struct ip_vs_rht *var +* @p: previous table, temp struct ip_vs_rht *var +* +* Walk tables assuming others can not change the installed tables +*/ +#define ip_vs_rht_for_each_table (table, t, p) \ + for (p = NULL, t = rcu_dereference_protected(table, 1); \ + t != p; \ + p = t, t = rcu_dereference_protected(t->new_tbl, 1)) + +/** +* ip_vs_rht_for_each_table_rcu() - Walk the hash tables under RCU reader lock +* @table: struct ip_vs_rht __rcu *table +* @t: current table, used as cursor, struct ip_vs_rht *var +* @p: previous table, temp struct ip_vs_rht *var +* +* We usually search in one table and also in second table on resizing +*/ +#define ip_vs_rht_for_each_table_rcu (table, t, p) \ + for (p = NULL, t = rcu_dereference(table); \ + t != p; \ + p = t, t = rcu_dereference(t->new_tbl)) + +/** +* ip_vs_rht_for_each_bucket() - Walk all table buckets +* @t: current table, used as cursor, struct ip_vs_rht *var +* @bucket: bucket index, used as cursor, u32 var +* @head: bucket address, used as cursor, struct hlist_bl_head *var +*/ +#define ip_vs_rht_for_each_bucket (t, bucket, head) \ + for (bucket = 0, head = (t)->buckets; \ + bucket < t->size; bucket++, head++) + +/** +* ip_vs_rht_for_bucket_retry() - Retry bucket if entries are moved +* @t: current table, used as cursor, struct ip_vs_rht *var +* @bucket: index of current bucket or hash key +* @sc: temp seqcount_t *var +* @retry: temp int var +*/ +#define ip_vs_rht_for_bucket_retry (t, bucket, sc, seq, retry) \ + for (retry = 1, sc = &(t)->seqc[(bucket) & (t)->seqc_mask]; \ + retry && ({ seq = read_seqcount_begin(sc); 1; }); \ + retry = read_seqcount_retry(sc, seq)) + +/** +* DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU() - Declare variables +* +* Variables for ip_vs_rht_walk_buckets_rcu +*/ +#define DECLARE_IP_VS_RHT_WALK_BUCKETS_RCU () \ + struct ip_vs_rht *_t, *_p; \ + unsigned int _seq; \ + seqcount_t *_sc; \ + u32 _bucket; \ + int _retry +/** +* ip_vs_rht_walk_buckets_rcu() - Walk all buckets under RCU read lock +* @table: struct ip_vs_rht __rcu *table +* @head: bucket address, used as cursor, struct hlist_bl_head *var +* +* Can be used while others add/delete/move entries +* Not suitable if duplicates are not desired +* Possible cases for reader that uses cond_resched_rcu() in the loop: +* - new table can not be installed, no need to repeat +* - new table can be installed => check and repeat if new table is +* installed, needed for !PREEMPT_RCU +*/ +#define ip_vs_rht_walk_buckets_rcu (table, head) \ + ip_vs_rht_for_each_table_rcu(table, _t, _p) \ + ip_vs_rht_for_each_bucket(_t, _bucket, head) \ + ip_vs_rht_for_bucket_retry(_t, _bucket, _sc, \ + _seq, _retry) + +/** +* DECLARE_IP_VS_RHT_WALK_BUCKET_RCU() - Declare variables +* +* Variables for ip_vs_rht_walk_bucket_rcu +*/ +#define DECLARE_IP_VS_RHT_WALK_BUCKET_RCU () \ + unsigned int _seq; \ + seqcount_t *_sc; \ + int _retry +/** +* ip_vs_rht_walk_bucket_rcu() - Walk bucket under RCU read lock +* @t: current table, struct ip_vs_rht *var +* @bucket: index of current bucket or hash key +* @head: bucket address, used as cursor, struct hlist_bl_head *var +* +* Can be used while others add/delete/move entries +* Not suitable if duplicates are not desired +* Possible cases for reader that uses cond_resched_rcu() in the loop: +* - new table can not be installed, no need to repeat +* - new table can be installed => check and repeat if new table is +* installed, needed for !PREEMPT_RCU +*/ +#define ip_vs_rht_walk_bucket_rcu (t, bucket, head) \ + if (({ head = (t)->buckets + ((bucket) & (t)->mask); 0; })) \ + {} \ + else \ + ip_vs_rht_for_bucket_retry(t, (bucket), _sc, _seq, _retry) + +/** +* DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU() - Declare variables +* +* Variables for ip_vs_rht_walk_buckets_safe_rcu +*/ +#define DECLARE_IP_VS_RHT_WALK_BUCKETS_SAFE_RCU () \ + struct ip_vs_rht *_t, *_p; \ + u32 _bucket +/** +* ip_vs_rht_walk_buckets_safe_rcu() - Walk all buckets under RCU read lock +* @table: struct ip_vs_rht __rcu *table +* @head: bucket address, used as cursor, struct hlist_bl_head *var +* +* Can be used while others add/delete entries but moving is disabled +* Using cond_resched_rcu() should be safe if tables do not change +*/ +#define ip_vs_rht_walk_buckets_safe_rcu (table, head) \ + ip_vs_rht_for_each_table_rcu(table, _t, _p) \ + ip_vs_rht_for_each_bucket(_t, _bucket, head) + +/** +* DECLARE_IP_VS_RHT_WALK_BUCKETS() - Declare variables +* +* Variables for ip_vs_rht_walk_buckets +*/ +#define DECLARE_IP_VS_RHT_WALK_BUCKETS () \ + struct ip_vs_rht *_t, *_p; \ + u32 _bucket + +/** +* ip_vs_rht_walk_buckets() - Walk all buckets +* @table: struct ip_vs_rht __rcu *table +* @head: bucket address, used as cursor, struct hlist_bl_head *var +* +* Use if others can not add/delete/move entries +*/ +#define ip_vs_rht_walk_buckets (table, head) \ + ip_vs_rht_for_each_table(table, _t, _p) \ + ip_vs_rht_for_each_bucket(_t, _bucket, head) + +/* Entries can be in one of two tables, so we flip bit when new table is +* created and store it as highest bit in hash keys +*/ +#define IP_VS_RHT_TABLE_ID_MASK BIT(31) + +/* Check if hash key is from this table */ +static inline bool ip_vs_rht_same_table (struct ip_vs_rht *t,u32 hash_key) +{ +return !((t->table_id ^ hash_key) &IP_VS_RHT_TABLE_ID_MASK ); +} + +/* Build per-table hash key from hash value */ +static inline u32 ip_vs_rht_build_hash_key (struct ip_vs_rht *t,u32 hash) +{ +return t->table_id | (hash & ~IP_VS_RHT_TABLE_ID_MASK ); +} + +void ip_vs_rht_free (struct ip_vs_rht *t); +void ip_vs_rht_rcu_free (struct rcu_head *head); +struct ip_vs_rht *ip_vs_rht_alloc (int buckets,int scounts,int locks); +int ip_vs_rht_desired_size (struct netns_ipvs *ipvs,struct ip_vs_rht *t,int n, +int lfactor,int min_bits,int max_bits); +void ip_vs_rht_set_thresholds (struct ip_vs_rht *t,int size,int lfactor, +int min_bits,int max_bits); +u32 ip_vs_rht_hash_linfo (struct ip_vs_rht *t,int af, +const union nf_inet_addr *addr,u32 v1,u32 v2); +struct dst_entry ;struct iphdr ;struct ip_vs_conn ;
net/netfilter/ipvs/ip_vs_conn.c +0 -5
@@ -79,11 +79,6 @@ static unsigned int ip_vs_conn_rnd __read_mostly;#define IP_VS_ADDRSTRLEN (8+1)#endif -struct ip_vs_aligned_lock -{ -spinlock_t l; -} __attribute__((__aligned__ (SMP_CACHE_BYTES ))); -/* lock array for conn table */ static struct ip_vs_aligned_lock __ip_vs_conntbl_lock_array[CT_LOCKARRAY_SIZE] __cacheline_aligned;
net/netfilter/ipvs/ip_vs_core.c +169 -0
175 lines not on this page · raw message
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 8/14 ipvs: use resizable hash table for services
In reply to Julian AnastasovMake the hash table for services resizable in the bit range of 4-20. Table is attached only while services are present. Resizing is done by delayed work based on load (the number of hashed services). Table grows when load increases 2+ times (above 12.5% with factor=3) and shrinks 8+ times when load decreases 16+ times (below 0.78%).
Switch to jhash hashing to reduce the collisions for multiple services.
Add a hash_key field into the service that includes table ID and bucket ID. This helps the lookup and delete operations.
--- include/net/ip_vs.h | 49 ++- net/netfilter/ipvs/ip_vs_ctl.c | 676 +++++++++++++++++++++++++++------ 2 files changed, 594 insertions(+), 131 deletions(-)
include/net/ip_vs.h +34 -15
@@ -35,12 +35,10 @@#define IP_VS_HDR_INVERSE 1#define IP_VS_HDR_ICMP 2 -/* -* Hash table: for virtual service lookups -*/ -#define IP_VS_SVC_TAB_BITS 8 -#define IP_VS_SVC_TAB_SIZE BIT(IP_VS_SVC_TAB_BITS) -#define IP_VS_SVC_TAB_MASK (IP_VS_SVC_TAB_SIZE - 1) + +/* svc_table limits */ +#define IP_VS_SVC_TAB_MIN_BITS 4 +#define IP_VS_SVC_TAB_MAX_BITS 20/* Generic access of ipvs struct */ static inline struct netns_ipvs *net_ipvs (struct net * net)
@@ -51,8 +49,6 @@ static inline struct netns_ipvs *net_ipvs(struct net* net)/* Connections' size value needed by ip_vs_ctl.c */ extern int ip_vs_conn_tab_size; -extern struct mutex __ip_vs_mutex; -struct ip_vs_iphdr {int hdr_flags;/* ipvs flags */ __u32 off;/* Where IP or IPv4 header starts */
@@ -289,6 +285,12 @@ static inline int ip_vs_af_index(int af)return af ==AF_INET6 ?IP_VS_AF_INET6 :IP_VS_AF_INET ; } +/* work_flags */ +enum { +IP_VS_WORK_SVC_RESIZE ,/* Schedule svc_resize_work */ +IP_VS_WORK_SVC_NORESIZE ,/* Stopping svc_resize_work */ +}; +/* The port number of FTP service (in network order). */ #define FTPPORT cpu_to_be16(21)#define FTPDATA cpu_to_be16(20)
@@ -888,14 +890,15 @@ struct ip_vs_dest_user_kern {* forwarding entries. */ struct ip_vs_service { -struct hlist_node s_list;/* node in service table */ -atomic_t refcnt;/* reference counter */ - +struct hlist_bl_node s_list;/* node in service table */ +u32 hash_key;/* Key for the hash table */ u16 af;/* address family */ __u16 protocol;/* which protocol (TCP/UDP) */ +union nf_inet_addr addr;/* IP address for virtual service */ -__be16 port;/* port number for the service */ __u32 fwmark;/* firewall mark of the service */ +atomic_t refcnt;/* reference counter */ +__be16 port;/* port number for the service */ unsigned int flags;/* service status flags */ unsigned int timeout;/* persistent timeout in ticks */ __be32 netmask;/* grouping granularity, mask/plen */
@@ -1153,6 +1156,10 @@ struct netns_ipvs {struct list_head dest_trash;spinlock_t dest_trash_lock;struct timer_list dest_trash_timer;/* expiration timer */ +struct mutex service_mutex;/* service reconfig */ +struct rw_semaphore svc_resize_sem;/* svc_table resizing */ +struct delayed_work svc_resize_work;/* resize svc_table */ +atomic_t svc_table_changes;/* ++ on new table */ /* Service counters */ atomic_t num_services[IP_VS_AF_MAX ];/* Services */ atomic_t fwm_services[IP_VS_AF_MAX ];/* Services */
@@ -1216,6 +1223,7 @@ struct netns_ipvs {int sysctl_est_nice;/* kthread nice */ int est_stopped;/* stop tasks */ #endif +int sysctl_svc_lfactor;/* ip_vs_lblc */ int sysctl_lblc_expiration;
@@ -1225,6 +1233,7 @@ struct netns_ipvs {int sysctl_lblcr_expiration;struct ctl_table_header *lblcr_ctl_header;struct ctl_table *lblcr_ctl_table; +unsigned long work_flags;/* IP_VS_WORK_* flags */ /* ip_vs_est */ struct delayed_work est_reload_work;/* Reload kthread tasks */ struct mutex est_mutex;/* protect kthread tasks */
@@ -1256,9 +1265,7 @@ struct netns_ipvs {unsigned int mixed_address_family_dests;unsigned int hooks_afmask;/* &1=AF_INET, &2=AF_INET6 */ -/* the service mutex that protect svc_table and svc_fwm_table */ -struct mutex service_mutex; -struct hlist_head svc_table[IP_VS_SVC_TAB_SIZE ];/* Services */ +struct ip_vs_rht __rcu *svc_table;/* Services */ };#define DEFAULT_SYNC_THRESHOLD 3
@@ -1495,6 +1502,18 @@ static inline int sysctl_est_nice(struct netns_ipvs *ipvs)#endif +/* Get load factor to map num_services/u_thresh to t->size +* Large value decreases u_thresh to reduce collisions but increases +* the table size +* Returns factor where: +* - non-negative: u_thresh = size >> factor, eg. lfactor 2 = 25% load +* - negative: u_thresh = size << factor, eg. lfactor -1 = 200% load +*/ +static inline int sysctl_svc_lfactor (struct netns_ipvs *ipvs) +{ +return READ_ONCE (ipvs->sysctl_svc_lfactor); +} +/* IPVS core functions * (from ip_vs_core.c) */
net/netfilter/ipvs/ip_vs_ctl.c +560 -116
915 lines not on this page · raw message
Signature · 1 line
2.43.0
Simon Horman
reviewerRe: [PATCHv2 RFC net-next 08/14] ipvs: use resizable hash table for services
Quoted 13 lines · Julian Anastasov
Make the hash table for services resizable in the bit range of 4-20. Table is attached only while services are present. Resizing is done by delayed work based on load (the number of hashed services). Table grows when load increases 2+ times (above 12.5% with factor=3) and shrinks 8+ times when load decreases 16+ times (below 0.78%).
Switch to jhash hashing to reduce the collisions for multiple services.
Add a
hash_keyfield into the service that includes table ID and bucket ID. This helps the lookup and delete operations.Signed-off-by Julian Anastasov <[email protected]>
...
Quoted 19 lines
net/netfilter/ipvs/ip_vs_ctl.c +3
-11@@ -391,18 +440,29 @@ static inline struct ip_vs_service * __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol, const union nf_inet_addr *vaddr, __be16 vport) { - unsigned int hash; +DECLARE_IP_VS_RHT_WALK_BUCKET_RCU (); +struct hlist_bl_head *head;struct ip_vs_service *svc; - -/* Check for "full" addressed entries */ - hash =ip_vs_svc_hashkey (ipvs, af, protocol, vaddr, vport); - -hlist_for_each_entry_rcu (svc, &ipvs->svc_table[hash], s_list) { -if (svc->af == af &&ip_vs_addr_equal (af, &svc->addr, vaddr) && - svc->port == vport && svc->protocol == protocol && - !svc->fwmark) { -/* HIT */ -return svc; +struct ip_vs_rht *t, *p;
net/netfilter/ipvs/ip_vs_ctl.c static inline struct ip_vs_service * · line 456
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +10
-0+struct hlist_bl_node *e; +u32 hash, hash_key; + +ip_vs_rht_for_each_table_rcu (ipvs->svc_table, t, p) { +/* Check for "full" addressed entries */ + hash =ip_vs_svc_hashval (t, af, protocol, vaddr, vport); + + hash_key =ip_vs_rht_build_hash_key (t, hash); + ip_vs_rht_walk_bucket_rcu(t, hash_key, head) { +hlist_bl_for_each_entry_rcu (svc, e, head, s_list) {
Hi Julian,
Smatch thinks that head is used uninitialised here. This does seem to be the case to me too.
Quoted 29 lines
net/netfilter/ipvs/ip_vs_ctl.c +12
-9+if (READ_ONCE (svc->hash_key) == hash_key && + svc->af == af && +ip_vs_addr_equal (af, &svc->addr, vaddr) && + svc->port == vport && + svc->protocol == protocol && !svc->fwmark) { +/* HIT */ +return svc; + } + } } }@@ -416,16 +476,26 @@ __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol,static inline struct ip_vs_service *__ip_vs_svc_fwm_find (struct netns_ipvs *ipvs,int af,__u32 fwmark) { -unsigned int hash; +DECLARE_IP_VS_RHT_WALK_BUCKET_RCU (); +struct hlist_bl_head *head;struct ip_vs_service *svc; - -/* Check for fwmark addressed entries */ - hash =ip_vs_svc_fwm_hashkey (ipvs, fwmark); - -hlist_for_each_entry_rcu (svc, &ipvs->svc_table[hash], s_list) { -if (svc->fwmark == fwmark && svc->af == af) { -/* HIT */ -return svc; +struct ip_vs_rht *t, *p;
net/netfilter/ipvs/ip_vs_ctl.c __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol, · line 492
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +10
-0+struct hlist_bl_node *e; +u32 hash, hash_key; + +ip_vs_rht_for_each_table_rcu (ipvs->svc_table, t, p) { +/* Check for fwmark addressed entries */ + hash =ip_vs_svc_fwm_hashval (t, af, fwmark); + + hash_key =ip_vs_rht_build_hash_key (t, hash); + ip_vs_rht_walk_bucket_rcu(t, hash_key, head) { +hlist_bl_for_each_entry_rcu (svc, e, head, s_list) {
Ditto.
Quoted 9 lines
net/netfilter/ipvs/ip_vs_ctl.c +6
-0+if (READ_ONCE (svc->hash_key) == hash_key && + svc->fwmark == fwmark && svc->af == af) { +/* HIT */ +return svc; + } + } } }
...
Julian Anastasov
maintainerRe: [PATCHv2 RFC net-next 08/14] ipvs: use resizable hash table for services
Hello,
Quoted 49 lines · Simon Horman
Quoted 13 lines · Julian Anastasov
Make the hash table for services resizable in the bit range of 4-20. Table is attached only while services are present. Resizing is done by delayed work based on load (the number of hashed services). Table grows when load increases 2+ times (above 12.5% with factor=3) and shrinks 8+ times when load decreases 16+ times (below 0.78%).
Switch to jhash hashing to reduce the collisions for multiple services.
Add a
hash_keyfield into the service that includes table ID and bucket ID. This helps the lookup and delete operations.Signed-off-by Julian Anastasov <[email protected]>...
Quoted 19 lines
net/netfilter/ipvs/ip_vs_ctl.c +3
-11@@ -391,18 +440,29 @@ static inline struct ip_vs_service * __ip_vs_service_find(struct netns_ipvs *ipvs, int af, __u16 protocol, const union nf_inet_addr *vaddr, __be16 vport) { - unsigned int hash; +DECLARE_IP_VS_RHT_WALK_BUCKET_RCU (); +struct hlist_bl_head *head;struct ip_vs_service *svc; - -/* Check for "full" addressed entries */ - hash =ip_vs_svc_hashkey (ipvs, af, protocol, vaddr, vport); - -hlist_for_each_entry_rcu (svc, &ipvs->svc_table[hash], s_list) { -if (svc->af == af &&ip_vs_addr_equal (af, &svc->addr, vaddr) && - svc->port == vport && svc->protocol == protocol && - !svc->fwmark) { -/* HIT */ -return svc; +struct ip_vs_rht *t, *p;net/netfilter/ipvs/ip_vs_ctl.c static inline struct ip_vs_service * · line 456
Quoted 10 lines
net/netfilter/ipvs/ip_vs_ctl.c +10
-0+struct hlist_bl_node *e; +u32 hash, hash_key; + +ip_vs_rht_for_each_table_rcu (ipvs->svc_table, t, p) { +/* Check for "full" addressed entries */ + hash =ip_vs_svc_hashval (t, af, protocol, vaddr, vport); + + hash_key =ip_vs_rht_build_hash_key (t, hash); + ip_vs_rht_walk_bucket_rcu(t, hash_key, head) { +hlist_bl_for_each_entry_rcu (svc, e, head, s_list) {Hi Julian,
Quoted 2 lines
Smatch thinks that head is used uninitialised here. This does seem to be the case to me too.
It would crash and not find svc if that was true. May be it is
caused by the complex 'if (INIT then FAIL) {} else ANYTHING' construct used in ip_vs_rht_walk_bucket_rcu() which allows 'if () {} else for () for () for () ...'.
Regards
--
Julian Anastasov <[email protected]>
Julian Anastasov
maintainerRFC PATCH v2 9/14 ipvs: switch to per-net connection table
In reply to Julian AnastasovUse per-net resizable hash table for connections. The global table is slow to walk when using many namespaces.
The table can be resized in the range of [256 - ip_vs_conn_tab_size]. Table is attached only while services are present. Resizing is done by delayed work based on load (the number of connections).
Add a hash_key field into the connection that includes table ID and bucket ID. This helps the lookup and delete operations. ip_vs_conn_unlink() will not calculate the hash but will use and validate the saved hash_key under lock.
For better security switch from jhash to siphash for the default connection hashing but the persistence engines may use their own function. Keeping the hash table loaded with entries below the size (12%) allows to avoid collision for 96+% of the conns.
ip_vs_conn_fill_cport() now will rehash the connection with proper locking because unhash+hash is not safe for RCU readers.
To invalidate the templates setting just dport to 0xffff is enough, no need to rehash them. As result, ip_vs_conn_unhash() is now unused and removed.
--- include/net/ip_vs.h | 34 +- net/netfilter/ipvs/ip_vs_conn.c | 868 +++++++++++++++++++++--------- net/netfilter/ipvs/ip_vs_ctl.c | 18 + net/netfilter/ipvs/ip_vs_pe_sip.c | 4 +- net/netfilter/ipvs/ip_vs_sync.c | 23 + 5 files changed, 679 insertions(+), 268 deletions(-)
include/net/ip_vs.h +29 -5
@@ -36,6 +36,14 @@#define IP_VS_HDR_INVERSE 1#define IP_VS_HDR_ICMP 2 +/* conn_tab limits (as per Kconfig) */ +#define IP_VS_CONN_TAB_MIN_BITS 8 +#if BITS_PER_LONG >32 +#define IP_VS_CONN_TAB_MAX_BITS 27 +#else +#define IP_VS_CONN_TAB_MAX_BITS 20 +#endif +/* svc_table limits */ #define IP_VS_SVC_TAB_MIN_BITS 4#define IP_VS_SVC_TAB_MAX_BITS 20
@@ -289,6 +297,7 @@ static inline int ip_vs_af_index(int af)enum {IP_VS_WORK_SVC_RESIZE ,/* Schedule svc_resize_work */ IP_VS_WORK_SVC_NORESIZE ,/* Stopping svc_resize_work */ +IP_VS_WORK_CONN_RESIZE ,/* Schedule conn_resize_work */ };/* The port number of FTP service (in network order). */
@@ -778,18 +787,19 @@ struct ip_vs_conn_param {/* IP_VS structure allocated for each dynamically scheduled connection */ struct ip_vs_conn { -struct hlist_node c_list;/* hashed list heads */ +struct hlist_bl_node c_list;/* node in conn_tab */ +__u32 hash_key;/* Key for the hash table */ /* Protocol, addresses and port numbers */ __be16 cport;__be16 dport;__be16 vport;u16 af;/* address family */ +__u16 protocol;/* Which protocol (TCP/UDP) */ +__u16 daf;/* Address family of the dest */ 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) */ -__u16 daf;/* Address family of the dest */ struct netns_ipvs *ipvs;/* counter and timer */
@@ -1008,8 +1018,8 @@ struct ip_vs_pe {int (*fill_param)(struct ip_vs_conn_param *p,struct sk_buff *skb);bool (*ct_match)(const struct ip_vs_conn_param *p,struct ip_vs_conn *ct); -u32 (*hashkey_raw)(const struct ip_vs_conn_param *p,u32 initval, -bool inverse); +u32 (*hashkey_raw)(const struct ip_vs_conn_param *p, +struct ip_vs_rht *t,bool inverse);int (*show_pe_data)(const struct ip_vs_conn *cp,char *buf);/* create connections for real-server outgoing packets */ struct ip_vs_conn* (*conn_out)(struct ip_vs_service *svc,
@@ -1148,6 +1158,7 @@ struct netns_ipvs {#endif /* ip_vs_conn */ atomic_t conn_count;/* connection counter */ +struct delayed_work conn_resize_work;/* resize conn_tab */ /* ip_vs_ctl */ struct ip_vs_stats_rcu *tot_stats;/* Statistics & est. */
@@ -1223,6 +1234,7 @@ struct netns_ipvs {int sysctl_est_nice;/* kthread nice */ int est_stopped;/* stop tasks */ #endif +int sysctl_conn_lfactor;int sysctl_svc_lfactor;/* ip_vs_lblc */
@@ -1266,6 +1278,8 @@ struct netns_ipvs {unsigned int hooks_afmask;/* &1=AF_INET, &2=AF_INET6 */ struct ip_vs_rht __rcu *svc_table;/* Services */ +struct ip_vs_rht __rcu *conn_tab;/* Connections */ +atomic_t conn_tab_changes;/* ++ on new table */ };#define DEFAULT_SYNC_THRESHOLD 3
@@ -1502,6 +1516,12 @@ static inline int sysctl_est_nice(struct netns_ipvs *ipvs)#endif +/* Get load factor to map conn_count/u_thresh to t->size */ +static inline int sysctl_conn_lfactor (struct netns_ipvs *ipvs) +{ +return READ_ONCE (ipvs->sysctl_conn_lfactor); +} +/* Get load factor to map num_services/u_thresh to t->size * Large value decreases u_thresh to reduce collisions but increases * the table size
@@ -1587,6 +1607,10 @@ static inline void __ip_vs_conn_put(struct ip_vs_conn *cp) }void ip_vs_conn_put (struct ip_vs_conn *cp);void ip_vs_conn_fill_cport (struct ip_vs_conn *cp,__be16 cport); +int ip_vs_conn_desired_size (struct netns_ipvs *ipvs,struct ip_vs_rht *t, +int lfactor); +struct ip_vs_rht *ip_vs_conn_tab_alloc (struct netns_ipvs *ipvs,int buckets, +int lfactor); struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af, const union nf_inet_addr *daddr,
net/netfilter/ipvs/ip_vs_conn.c +607 -261
1,220 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_ctl.c +18 -0
@@ -1642,6 +1642,7 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, struct ip_vs_service **svc_p) {struct ip_vs_scheduler *sched =NULL ; +struct ip_vs_rht *tc_new =NULL ;struct ip_vs_rht *t, *t_new =NULL ;int af_id =ip_vs_af_index (u->af);struct ip_vs_service *svc =NULL ;
@@ -1701,6 +1702,17 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, } } +if (!rcu_dereference_protected (ipvs->conn_tab,1 )) { +int lfactor =sysctl_conn_lfactor (ipvs); +int new_size =ip_vs_conn_desired_size (ipvs,NULL , lfactor); + + tc_new =ip_vs_conn_tab_alloc (ipvs, new_size, lfactor); +if (!tc_new) { + ret = -ENOMEM ; + goto out_err; + } + } +if (!atomic_read (&ipvs->num_services[af_id])) { ret =ip_vs_register_hooks (ipvs, u->af);if (ret <0 )
@@ -1755,6 +1767,10 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u,rcu_assign_pointer (ipvs->svc_table, t_new); t_new =NULL ; } +if (tc_new) { +rcu_assign_pointer (ipvs->conn_tab, tc_new); + tc_new =NULL ; + }/* Hash the service into the service table */ ip_vs_svc_hash (svc);
@@ -1793,6 +1809,8 @@ ip_vs_add_service(struct netns_ipvs *ipvs, struct ip_vs_service_user_kern *u, out_err: +if (tc_new) +ip_vs_rht_free (tc_new);if (t_new)ip_vs_rht_free (t_new);if (ret_hooks >=0 )
net/netfilter/ipvs/ip_vs_pe_sip.c +2 -2
@@ -133,9 +133,9 @@ static bool ip_vs_sip_ct_match(const struct ip_vs_conn_param *p, }static u32 ip_vs_sip_hashkey_raw (const struct ip_vs_conn_param *p, -u32 initval,bool inverse) +struct ip_vs_rht *t,bool inverse) { -return jhash (p->pe_data, p->pe_data_len, initval); +return jhash (p->pe_data, p->pe_data_len, (u32 )t->hash_key.key[0 ]); }static int ip_vs_sip_show_pe_data (const struct ip_vs_conn *cp,char *buf)
net/netfilter/ipvs/ip_vs_sync.c +23 -0
@@ -1756,6 +1756,28 @@ int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *c,if (!ip_vs_use_count_inc ())return -ENOPROTOOPT ; +/* Backup server can be started without services just to sync conns, +* make sure conn_tab is created even if ipvs->enable is 0. +*/ +if (state ==IP_VS_STATE_BACKUP ) { +mutex_lock (&ipvs->service_mutex); +if (!rcu_dereference_protected (ipvs->conn_tab,1 )) { +int lfactor =sysctl_conn_lfactor (ipvs); +int new_size =ip_vs_conn_desired_size (ipvs,NULL , + lfactor); +struct ip_vs_rht *tc_new; + + tc_new =ip_vs_conn_tab_alloc (ipvs, new_size, lfactor); +if (!tc_new) { +mutex_unlock (&ipvs->service_mutex); + result = -ENOMEM ; + goto out_module; + } +rcu_assign_pointer (ipvs->conn_tab, tc_new); + } +mutex_unlock (&ipvs->service_mutex); + } +/* Do not hold one mutex and then to block on another */ for (;;) {rtnl_lock ();
@@ -1924,6 +1946,7 @@ int start_sync_thread(struct netns_ipvs *ipvs, struct ipvs_sync_daemon_cfg *c,mutex_unlock (&ipvs->sync_mutex);rtnl_unlock (); +out_module:/* decrease the module use count */ ip_vs_use_count_dec ();return result;
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 10/14 ipvs: show the current conn_tab size to users
In reply to Julian AnastasovAs conn_tab is per-net, better to show the current hash table size to users instead of the ip_vs_conn_tab_size (max).
--- net/netfilter/ipvs/ip_vs_ctl.c | 36 +++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
net/netfilter/ipvs/ip_vs_ctl.c +31 -5
@@ -2741,10 +2741,16 @@ static void ip_vs_info_seq_stop(struct seq_file *seq, void *v)static int ip_vs_info_seq_show (struct seq_file *seq,void *v) { +struct net *net =seq_file_net (seq); +struct netns_ipvs *ipvs =net_ipvs (net); +if (v ==SEQ_START_TOKEN ) { +struct ip_vs_rht *tc =rcu_dereference (ipvs->conn_tab); +int csize = tc ? tc->size :0 ; +seq_printf (seq,"IP Virtual Server version %d.%d.%d (size=%d)\n" , -NVERSION (IP_VS_VERSION_CODE ), ip_vs_conn_tab_size); +NVERSION (IP_VS_VERSION_CODE ), csize);seq_puts (seq,"Prot LocalAddress:Port Scheduler Flags\n" ); seq_puts(seq,
@@ -3422,10 +3428,17 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)switch (cmd) {case IP_VS_SO_GET_VERSION : { +struct ip_vs_rht *t; +int csize =0 ;char buf[64 ]; +rcu_read_lock (); + t =rcu_dereference (ipvs->conn_tab); +if (t) + csize = t->size; +rcu_read_unlock ();sprintf (buf,"IP Virtual Server version %d.%d.%d (size=%d)" , -NVERSION (IP_VS_VERSION_CODE ), ip_vs_conn_tab_size); +NVERSION (IP_VS_VERSION_CODE ), csize);if (copy_to_user (user, buf,strlen (buf)+1 ) !=0 ) { ret = -EFAULT ; goto out;
@@ -3437,8 +3450,16 @@ do_ip_vs_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)case IP_VS_SO_GET_INFO : {struct ip_vs_getinfo info; +struct ip_vs_rht *t; +int csize =0 ; + +rcu_read_lock (); + t =rcu_dereference (ipvs->conn_tab); +if (t) + csize = t->size; +rcu_read_unlock (); info.version =IP_VS_VERSION_CODE ; - info.size = ip_vs_conn_tab_size; + info.size = csize; info.num_services =atomic_read (&ipvs->num_services[IP_VS_AF_INET ]);if (copy_to_user (user, &info,sizeof (info)) !=0 )
@@ -4380,6 +4401,8 @@ static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info)int ret, cmd, reply_cmd;struct net *net =sock_net (skb->sk);struct netns_ipvs *ipvs =net_ipvs (net); +struct ip_vs_rht *t; +int csize; cmd = info->genlhdr->cmd;
@@ -4447,10 +4470,13 @@ static int ip_vs_genl_get_cmd(struct sk_buff *skb, struct genl_info *info) }case IPVS_CMD_GET_INFO : + csize =0 ; + t =rcu_dereference (ipvs->conn_tab); +if (t) + csize = t->size;if (nla_put_u32 (msg,IPVS_INFO_ATTR_VERSION ,IP_VS_VERSION_CODE ) || -nla_put_u32 (msg,IPVS_INFO_ATTR_CONN_TAB_SIZE , - ip_vs_conn_tab_size)) +nla_put_u32 (msg,IPVS_INFO_ATTR_CONN_TAB_SIZE , csize)) goto nla_put_failure;break ; }
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 11/14 ipvs: no_cport and dropentry counters can be per-net
In reply to Julian AnastasovWith using per-net conn_tab these counters do not need to be global anymore.
--- include/net/ip_vs.h | 2 ++ net/netfilter/ipvs/ip_vs_conn.c | 62 ++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 25 deletions(-)
include/net/ip_vs.h +2 -0
@@ -1158,6 +1158,7 @@ struct netns_ipvs {#endif /* ip_vs_conn */ atomic_t conn_count;/* connection counter */ +atomic_t no_cport_conns[IP_VS_AF_MAX ];struct delayed_work conn_resize_work;/* resize conn_tab */ /* ip_vs_ctl */
@@ -1188,6 +1189,7 @@ struct netns_ipvs {int drop_counter;int old_secure_tcp;atomic_t dropentry; +s8 dropentry_counters[8 ];/* locks in ctl.c */ spinlock_t dropentry_lock;/* drop entry handling */ spinlock_t droppacket_lock;/* drop packet handling */
net/netfilter/ipvs/ip_vs_conn.c +37 -25
132 lines not on this page · raw message
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 12/14 ipvs: use more keys for connection hashing
In reply to Julian AnastasovSimon Kirby reported long time ago that IPVS connection hashing based only on the client address/port (caddr, cport) as hash keys is not suitable for setups that accept traffic on multiple virtual IPs and ports. It can happen for multiple VIP:VPORT services, for single or many fwmark service(s) that match multiple virtual IPs and ports or even for passive FTP with peristence in DR/TUN mode where we expect traffic on multiple ports for the virtual IP.
Fix it by adding virtual addresses and ports to the hash function. This causes the traffic from NAT real servers to clients to use second hashing for the in->out direction.
As result:
- the IN direction from client will use hash node hn0 where the source/dest addresses and ports used by client will be used as hash keys
- the OUT direction from NAT real servers will use hash node hn1 for the traffic from real server to client
- the persistence templates are hashed only with parameters based on the IN direction, so they now will also use the virtual address, port and fwmark from the service.
OLD:
- all methods: c_list node: proto, caddr:cport
- persistence templates: c_list node: proto, caddr_net:0
- persistence engine templates: c_list node: per-PE, PE-SIP uses jhash
NEW:
- all methods: hn0 node (dir 0): proto, caddr:cport -> vaddr:vport
- MASQ method: hn1 node (dir 1): proto, daddr:dport -> caddr:cport
- persistence templates: hn0 node (dir 0):
proto, caddr_net:0 -> vaddr:vport_or_0
proto, caddr_net:0 -> fwmark:0
- persistence engine templates: hn0 node (dir 0): as before
Also reorder the ip_vs_conn fields, so that hash nodes are on same read-mostly cache line while write-mostly fields are on separate cache line.
--- include/net/ip_vs.h | 104 ++++++++---- net/netfilter/ipvs/ip_vs_conn.c | 279 ++++++++++++++++++++++++-------- 2 files changed, 279 insertions(+), 104 deletions(-)
include/net/ip_vs.h +69 -35
@@ -785,51 +785,48 @@ struct ip_vs_conn_param {__u8 pe_data_len; }; +/* Hash node in conn_tab */ +struct ip_vs_conn_hnode { +struct hlist_bl_node node;/* node in conn_tab */ +u32 hash_key;/* Key for the hash table */ +u8 dir;/* 0=out->in, 1=in->out */ +} __packed; +/* IP_VS structure allocated for each dynamically scheduled connection */ struct ip_vs_conn { -struct hlist_bl_node c_list;/* node in conn_tab */ -__u32 hash_key;/* Key for the hash table */ -/* Protocol, addresses and port numbers */ +/* Cacheline for hash table nodes - rarely modified */ + +struct ip_vs_conn_hnode hn0;/* Original direction */ +u8 af;/* address family */ __be16 cport; +struct ip_vs_conn_hnode hn1;/* Reply direction */ +u8 daf;/* Address family of the dest */ __be16 dport; -__be16 vport; -u16 af;/* address family */ -__u16 protocol;/* Which protocol (TCP/UDP) */ -__u16 daf;/* Address family of the dest */ -union nf_inet_addr caddr;/* client address */ -union nf_inet_addr vaddr;/* virtual address */ -union nf_inet_addr daddr;/* destination address */ +struct ip_vs_dest *dest;/* real server */ +atomic_t n_control;/* Number of controlled ones */ volatile __u32 flags;/* status flags */ -struct netns_ipvs *ipvs; - -/* counter and timer */ -refcount_t refcnt;/* reference count */ -struct timer_list timer;/* Expiration timer */ -volatile unsigned long timeout;/* timeout */ +/* 44/64 */ -/* Flags and state transition */ -spinlock_t lock;/* lock for state transition */ +struct ip_vs_conn *control;/* Master control connection */ +const struct ip_vs_pe *pe; +char *pe_data; +__u8 pe_data_len;volatile __u16 state;/* state info */ volatile __u16 old_state;/* old state, to be used for * state transition triggered * synchronization */ -__u32 fwmark;/* Fire wall mark from skb */ -unsigned long sync_endtime;/* jiffies + sent_retries */ +/* 2-byte hole */ +/* 64/96 */ -/* Control members */ -struct ip_vs_conn *control;/* Master control connection */ -atomic_t n_control;/* Number of controlled ones */ -struct ip_vs_dest *dest;/* real server */ -atomic_t in_pkts;/* incoming packet counter */ +union nf_inet_addr caddr;/* client address */ +union nf_inet_addr vaddr;/* virtual address */ +/* 96/128 */ -/* Packet transmitter for different forwarding methods. If it -* mangles the packet, it must return NF_DROP or better NF_STOLEN, -* otherwise this must be changed to a sk_buff **. -* NF_ACCEPT can be returned when destination is local. -*/ -int (*packet_xmit)(struct sk_buff *skb,struct ip_vs_conn *cp, -struct ip_vs_protocol *pp,struct ip_vs_iphdr *iph); +union nf_inet_addr daddr;/* destination address */ +__u32 fwmark;/* Fire wall mark from skb */ +__be16 vport; +__u16 protocol;/* Which protocol (TCP/UDP) */ /* Note: we can group the following members into a structure, * in order to save more space, and the following members are
@@ -837,14 +834,31 @@ struct ip_vs_conn {*/ struct ip_vs_app *app;/* bound ip_vs_app object */ void *app_data;/* Application private data */ +/* 128/168 */ struct_group (sync_conn_opt, struct ip_vs_seq in_seq; /* incoming seq. struct */struct ip_vs_seq out_seq;/* outgoing seq. struct */ ); +/* 152/192 */ -const struct ip_vs_pe *pe; -char *pe_data; -__u8 pe_data_len; +struct timer_list timer;/* Expiration timer */ +volatile unsigned long timeout;/* timeout */ +spinlock_t lock;/* lock for state transition */ +refcount_t refcnt;/* reference count */ +atomic_t in_pkts;/* incoming packet counter */ +/* 64-bit: 4-byte gap */ + +/* 188/256 */ +unsigned long sync_endtime;/* jiffies + sent_retries */ +struct netns_ipvs *ipvs; + +/* Packet transmitter for different forwarding methods. If it +* mangles the packet, it must return NF_DROP or better NF_STOLEN, +* otherwise this must be changed to a sk_buff **. +* NF_ACCEPT can be returned when destination is local. +*/ +int (*packet_xmit)(struct sk_buff *skb,struct ip_vs_conn *cp, +struct ip_vs_protocol *pp,struct ip_vs_iphdr *iph);struct rcu_head rcu_head; };
@@ -1614,6 +1628,19 @@ int ip_vs_conn_desired_size(struct netns_ipvs *ipvs, struct ip_vs_rht *t,struct ip_vs_rht *ip_vs_conn_tab_alloc (struct netns_ipvs *ipvs,int buckets,int lfactor); +static inline struct ip_vs_conn * +ip_vs_hn0_to_conn (struct ip_vs_conn_hnode *hn) +{ + return container_of(hn, struct ip_vs_conn, hn0); +} + +static inline struct ip_vs_conn * +ip_vs_hn_to_conn (struct ip_vs_conn_hnode *hn) +{ + return hn->dir ? container_of(hn, struct ip_vs_conn, hn1) : + container_of(hn, struct ip_vs_conn, hn0); +} + struct ip_vs_conn *ip_vs_conn_new(const struct ip_vs_conn_param *p, int dest_af, const union nf_inet_addr *daddr, __be16 dport, unsigned int flags,
@@ -1966,6 +1993,13 @@ static inline char ip_vs_fwd_tag(struct ip_vs_conn *cp)return fwd; } +/* Check if connection uses double hashing */ +static inline bool ip_vs_conn_use_hash2 (struct ip_vs_conn *cp) +{ +return IP_VS_FWD_METHOD (cp) ==IP_VS_CONN_F_MASQ && + !(cp->flags &IP_VS_CONN_F_TEMPLATE ); +} +void ip_vs_nat_icmp (struct sk_buff *skb,struct ip_vs_protocol *pp,struct ip_vs_conn *cp,int dir);
net/netfilter/ipvs/ip_vs_conn.c +210 -69
602 lines not on this page · raw message
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 13/14 ipvs: add ip_vs_status info
In reply to Julian AnastasovAdd /proc/net/ip_vs_status to show current state of IPVS.
--- net/netfilter/ipvs/ip_vs_ctl.c | 145 +++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+)
net/netfilter/ipvs/ip_vs_ctl.c +145 -0
@@ -2913,6 +2913,144 @@ static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)return 0 ; } + +static int ip_vs_status_show (struct seq_file *seq,void *v) +{ +struct net *net =seq_file_single_net (seq); +struct netns_ipvs *ipvs =net_ipvs (net); +unsigned int resched_score =0 ; +struct ip_vs_conn_hnode *hn; +struct hlist_bl_head *head; +struct ip_vs_service *svc; +struct ip_vs_rht *t, *pt; +struct hlist_bl_node *e; +int old_gen, new_gen; +u32 counts[8 ]; +u32 bucket; +int count; +u32 sum1; +u32 sum; +int i; + +rcu_read_lock (); + + t =rcu_dereference (ipvs->conn_tab); + +seq_printf (seq,"Conns:\t%d\n" ,atomic_read (&ipvs->conn_count)); +seq_printf (seq,"Conn buckets:\t%d (%d bits, lfactor %d)\n" , + t ? t->size :0 , t ? t->bits :0 , t ? t->lfactor :0 ); + +if (!atomic_read (&ipvs->conn_count)) + goto after_conns; + old_gen =atomic_read (&ipvs->conn_tab_changes); + +repeat_conn: +smp_rmb ();/* ipvs->conn_tab and conn_tab_changes */ +memset (counts,0 ,sizeof (counts)); +ip_vs_rht_for_each_table_rcu (ipvs->conn_tab, t, pt) { +for (bucket =0 ; bucket < t->size; bucket++) { +DECLARE_IP_VS_RHT_WALK_BUCKET_RCU (); + + count =0 ; + resched_score++; + ip_vs_rht_walk_bucket_rcu(t, bucket, head) { + count =0 ; +hlist_bl_for_each_entry_rcu (hn, e, head, node) + count++; + } + resched_score += count; +if (resched_score >=100 ) { + resched_score =0 ; +cond_resched_rcu (); + new_gen =atomic_read (&ipvs->conn_tab_changes); +/* New table installed ? */ +if (old_gen != new_gen) { + old_gen = new_gen; + goto repeat_conn; + } + } + counts[min (count, (int )ARRAY_SIZE (counts) -1 )]++; + } + } +for (sum =0 , i =0 ; i <ARRAY_SIZE (counts); i++) + sum += counts[i]; + sum1 = sum - counts[0 ]; +seq_printf (seq,"Conn buckets empty:\t%u (%lu%%)\n" , + counts[0 ], (unsigned long )counts[0 ] *100 /max (sum,1U )); +for (i =1 ; i <ARRAY_SIZE (counts); i++) { +if (!counts[i]) +continue ; +seq_printf (seq,"Conn buckets len-%d:\t%u (%lu%%)\n" , + i, counts[i], + (unsigned long )counts[i] *100 /max (sum1,1U )); + } + +after_conns: + t =rcu_dereference (ipvs->svc_table); + + count =ip_vs_get_num_services (ipvs); +seq_printf (seq,"Services:\t%d\n" , count); +seq_printf (seq,"Service buckets:\t%d (%d bits, lfactor %d)\n" , + t ? t->size :0 , t ? t->bits :0 , t ? t->lfactor :0 ); + +if (!count) + goto after_svc; + old_gen =atomic_read (&ipvs->svc_table_changes); + +repeat_svc: +smp_rmb ();/* ipvs->svc_table and svc_table_changes */ +memset (counts,0 ,sizeof (counts)); +ip_vs_rht_for_each_table_rcu (ipvs->svc_table, t, pt) { +for (bucket =0 ; bucket < t->size; bucket++) { +DECLARE_IP_VS_RHT_WALK_BUCKET_RCU (); + + count =0 ; + resched_score++; + ip_vs_rht_walk_bucket_rcu(t, bucket, head) { + count =0 ; +hlist_bl_for_each_entry_rcu (svc, e, head, + s_list) + count++; + } + resched_score += count; +if (resched_score >=100 ) { + resched_score =0 ; +cond_resched_rcu (); + new_gen =atomic_read (&ipvs->svc_table_changes); +/* New table installed ? */ +if (old_gen != new_gen) { + old_gen = new_gen; + goto repeat_svc; + } + } + counts[min (count, (int )ARRAY_SIZE (counts) -1 )]++; + } + } +for (sum =0 , i =0 ; i <ARRAY_SIZE (counts); i++) + sum += counts[i]; + sum1 = sum - counts[0 ]; +seq_printf (seq,"Service buckets empty:\t%u (%lu%%)\n" , + counts[0 ], (unsigned long )counts[0 ] *100 /max (sum,1U )); +for (i =1 ; i <ARRAY_SIZE (counts); i++) { +if (!counts[i]) +continue ; +seq_printf (seq,"Service buckets len-%d:\t%u (%lu%%)\n" , + i, counts[i], + (unsigned long )counts[i] *100 /max (sum1,1U )); + } + +after_svc: +seq_printf (seq,"Stats thread slots:\t%d (max %lu)\n" , + ipvs->est_kt_count, ipvs->est_max_threads); +seq_printf (seq,"Stats chain max len:\t%d\n" , ipvs->est_chain_max); +seq_printf (seq,"Stats thread ests:\t%d\n" , + ipvs->est_chain_max *IPVS_EST_CHAIN_FACTOR * +IPVS_EST_NTICKS ); + +rcu_read_unlock (); +return 0 ; +} +#endif /*
@@ -4827,6 +4965,9 @@ int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs) ipvs->net->proc_net, ip_vs_stats_percpu_show, NULL)) goto err_percpu; +if (!proc_create_net_single ("ip_vs_status" ,0 , ipvs->net->proc_net, + ip_vs_status_show,NULL )) + goto err_status;#endif ret =ip_vs_control_net_init_sysctl (ipvs);
@@ -4837,6 +4978,9 @@ int __net_init ip_vs_control_net_init(struct netns_ipvs *ipvs) err:#ifdef CONFIG_PROC_FS +remove_proc_entry ("ip_vs_status" , ipvs->net->proc_net); + +err_status:remove_proc_entry ("ip_vs_stats_percpu" , ipvs->net->proc_net); err_percpu:
@@ -4862,6 +5006,7 @@ void __net_exit ip_vs_control_net_cleanup(struct netns_ipvs *ipvs)ip_vs_control_net_cleanup_sysctl (ipvs);cancel_delayed_work_sync (&ipvs->est_reload_work);#ifdef CONFIG_PROC_FS +remove_proc_entry ("ip_vs_status" , ipvs->net->proc_net);remove_proc_entry ("ip_vs_stats_percpu" , ipvs->net->proc_net);remove_proc_entry ("ip_vs_stats" , ipvs->net->proc_net);remove_proc_entry ("ip_vs" , ipvs->net->proc_net);
Signature · 1 line
2.43.0
Julian Anastasov
maintainerRFC PATCH v2 14/14 ipvs: add conn_lfactor and svc_lfactor sysctl vars
In reply to Julian AnastasovAllow the default load factor for the connection and service tables to be configured.
--- Documentation/networking/ipvs-sysctl.rst | 31 ++++++++++ net/netfilter/ipvs/ip_vs_ctl.c | 72 ++++++++++++++++++++++++ 2 files changed, 103 insertions(+)
Documentation/networking/ipvs-sysctl.rst +31 -0
@@ -29,6 +29,28 @@ backup_only - BOOLEAN If set, disable the director function while the server is in backup mode to avoid packet loops for DR/TUN methods. +conn_lfactor - INTEGER + 4 - default + Valid range: -8 (smaller table) .. 8 (larger table) + + Controls the sizing of the connection hash table based on the + load factor (number of connections per table buckets). + As result, the table grows if load increases and shrinks when + load decreases in the range of 2^8 - 2^conn_tab_bits (module + parameter). + The value is a shift count where positive values select + buckets = (connection hash nodes << value) while negative + values select buckets = (connection hash nodes >> value). The + positive values reduce the collisions and reduce the time for + lookups but increase the table size. Negative values will + tolerate load above 100% when using smaller table is + preferred. If using NAT connections consider increasing the + value with one because they add two nodes in the hash table. + + Example: + 4: grow if load goes above 6% (buckets = nodes * 16) + -2: grow if load goes above 400% (buckets = nodes / 4) + conn_reuse_mode - INTEGER 1 - default
@@ -219,6 +241,15 @@ secure_tcp - INTEGER The value definition is the same as that of drop_entry and drop_packet. +svc_lfactor - INTEGER + 3 - default + Valid range: -8 (smaller table) .. 8 (larger table) + + Controls the sizing of the service hash table based on the + load factor (number of services per table buckets). The table + will grow and shrink in the range of 2^4 - 2^20. + See conn_lfactor for explanation. + sync_threshold - vector of 2 INTEGERs: sync_threshold, sync_period default 3 50
net/netfilter/ipvs/ip_vs_ctl.c +72 -0
90 lines not on this page · raw message
Signature · 1 line
2.43.0