ipvs: Use kthreads for stats
Subsystems (5)
Julian Anastasov
maintainerv3 cover ipvs: Use kthreads for stats
Hello,
Posting v3 (not final yet). New patch 5 just
for debugging, do not apply if not needed.
This patchset implements stats estimation in
kthread context. Simple tests do not show any problem.
Overview of the basic concepts. More in the
commit messages...
RCU Locking:
- As stats are now RCU-locked, tot_stats, svc and dest which hold estimator structures are now always freed from RCU callback. This ensures RCU grace period after the ip_vs_stop_estimator() call.
Kthread data:
- every kthread works over its own data structure and all such structures are attached to array
- even while there can be a kthread structure, its task may not be running, eg. before first service is added or while the sysctl var is set to an empty cpulist or when run_estimation is 0.
- a task and its structure may be released if all estimators are unlinked from its chains, leaving the slot in the array empty
- every kthread data structure allows limited number of estimators
- to add new estimators we use the last added kthread context (est_add_ktid). The new estimators are linked to the chains just before the estimated one, based on add_row. This ensures their estimation will start after 2 seconds. If estimators are added in bursts, common case if all services and dests are initially configured, we may spread the estimators to more chains. This will reduce the chain imbalance.
Not done yet:
* start kthread to calculate chain_max_len and IPVS_EST_TICK_CHAINS suitable for 100us cond_resched rate and 10% of 40ms. Current value of IPVS_EST_TICK_CHAINS=48 determines tick time of 4.8ms (i.e. in units of 100us) which is 12% of max tick time of 40ms. The question is how valid will be such test. For example, we can add all ests in temp list until the calculation is done and then to requeue all estimators into the chains.
Changes in v3:
* calculate chain_max_len (was IPVS_EST_CHAIN_DEPTH) but it needs further tuning based on real estimation test
* est_max_threads set from rlimit(RLIMIT_NPROC). I don't see analog to get_ucounts_value() to get the max value.
* the atomic bitop for td->present is not needed, remove it
* start filling based on est_row after 2 ticks are fully allocated. As 2/50 is 4% this can be increased more.
Changes in v2:
Patch 2:
* kd->mutex is gone, cond_resched rate determined by IPVS_EST_CHAIN_DEPTH
* IPVS_EST_MAX_COUNT is a hard limit now
* kthread data is now 1-50 allocated tick structures, each containing heads for limited chains. Bitmaps should allow faster access. We avoid large allocations for structs.
* as the td->present bitmap is shared, use atomic bitops
* ip_vs_start_estimator now returns error code
* _bh locking removed from stats->lock
* bump arg is gone from ip_vs_est_reload_start
* prepare for upcoming changes that remove _irq from u64_stats_fetch_begin_irq/u64_stats_fetch_retry_irq
* est_add_ktid is now always valid
Patch 3:
* use .. in est_nice docs
Julian Anastasov (5):
ipvs: add rcu protection to stats
ipvs: use kthreads for stats estimation
ipvs: add est_cpulist and est_nice sysctl vars
ipvs: run_estimation should control the kthread tasks
ipvs: debug the tick time
Documentation/networking/ipvs-sysctl.rst | 24 +- include/net/ip_vs.h | 125 +++++- net/netfilter/ipvs/ip_vs_core.c | 10 +- net/netfilter/ipvs/ip_vs_ctl.c | 367 ++++++++++++++--- net/netfilter/ipvs/ip_vs_est.c | 487 +++++++++++++++++++---- 5 files changed, 883 insertions(+), 130 deletions(-)
Signature · 1 line
2.37.3
Julian Anastasov
maintainerRFC PATCH v3 1/5 ipvs: add rcu protection to stats
In preparation to using RCU locking for the list with estimators, make sure the struct ip_vs_stats are released after RCU grace period by using RCU callbacks. This affects ipvs->tot_stats where we can not use RCU callbacks for ipvs, so we use allocated struct ip_vs_stats_rcu. For services and dests we force RCU callbacks for all cases.
--- include/net/ip_vs.h | 8 ++++- net/netfilter/ipvs/ip_vs_core.c | 10 ++++-- net/netfilter/ipvs/ip_vs_ctl.c | 64 ++++++++++++++++++++++----------- 3 files changed, 57 insertions(+), 25 deletions(-)
include/net/ip_vs.h +7 -1
@@ -405,6 +405,11 @@ struct ip_vs_stats {struct ip_vs_kstats kstats0;/* reset values */ }; +struct ip_vs_stats_rcu { +struct ip_vs_stats s; +struct rcu_head rcu_head; +}; +struct dst_entry ;struct iphdr ;struct ip_vs_conn ;
@@ -688,6 +693,7 @@ struct ip_vs_dest {union nf_inet_addr vaddr;/* virtual IP address */ __u32 vfwmark;/* firewall mark of service */ +struct rcu_head rcu_head;struct list_head t_list;/* in dest_trash */ unsigned int in_rs_table:1; /* we are in rs_table */ };
@@ -869,7 +875,7 @@ struct netns_ipvs {atomic_t conn_count;/* connection counter */ /* ip_vs_ctl */ -struct ip_vs_stats tot_stats;/* Statistics & est. */ +struct ip_vs_stats_rcu *tot_stats;/* Statistics & est. */ int num_services;/* no of virtual services */ int num_services6;/* IPv6 virtual services */
net/netfilter/ipvs/ip_vs_core.c +7 -3
@@ -143,7 +143,7 @@ ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb) s->cnt.inbytes += skb->len;u64_stats_update_end (&s->syncp); - s =this_cpu_ptr (ipvs->tot_stats.cpustats); + s =this_cpu_ptr (ipvs->tot_stats->s.cpustats);u64_stats_update_begin (&s->syncp); s->cnt.inpkts++; s->cnt.inbytes += skb->len;
@@ -179,7 +179,7 @@ ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb) s->cnt.outbytes += skb->len;u64_stats_update_end (&s->syncp); - s =this_cpu_ptr (ipvs->tot_stats.cpustats); + s =this_cpu_ptr (ipvs->tot_stats->s.cpustats);u64_stats_update_begin (&s->syncp); s->cnt.outpkts++; s->cnt.outbytes += skb->len;
@@ -208,7 +208,7 @@ ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc) s->cnt.conns++;u64_stats_update_end (&s->syncp); - s =this_cpu_ptr (ipvs->tot_stats.cpustats); + s =this_cpu_ptr (ipvs->tot_stats->s.cpustats);u64_stats_update_begin (&s->syncp); s->cnt.conns++;u64_stats_update_end (&s->syncp);
@@ -2448,6 +2448,10 @@ static void __exit ip_vs_cleanup(void)ip_vs_conn_cleanup ();ip_vs_protocol_cleanup ();ip_vs_control_cleanup (); +/* common rcu_barrier() used by: +* - ip_vs_control_cleanup() +*/ +rcu_barrier ();pr_info ("ipvs unloaded.\n" ); }
net/netfilter/ipvs/ip_vs_ctl.c +43 -21
171 lines not on this page · raw message
Signature · 1 line
2.37.3
Julian Anastasov
maintainerRFC PATCH v3 2/5 ipvs: use kthreads for stats estimation
In reply to Julian AnastasovEstimating all entries in single list in timer context causes large latency with multiple rules.
Spread the estimator structures in multiple chains and use kthread(s) for the estimation. Every chain is processed under RCU lock. The kthreads are tuned for maximum number of estimators to process.
We also add delayed work est_reload_work that will make sure the kthread tasks are properly started/stopped.
ip_vs_start_estimator() is changed to report errors which allows to safely store the estimators in allocated structures.
--- include/net/ip_vs.h | 65 ++++- net/netfilter/ipvs/ip_vs_ctl.c | 137 +++++++--- net/netfilter/ipvs/ip_vs_est.c | 465 ++++++++++++++++++++++++++++----- 3 files changed, 564 insertions(+), 103 deletions(-)
include/net/ip_vs.h +60 -5
@@ -365,7 +365,7 @@ struct ip_vs_cpu_stats {/* IPVS statistics objects */ struct ip_vs_estimator { -struct list_head list; +struct hlist_node list;u64 last_inbytes;u64 last_outbytes;
@@ -378,6 +378,52 @@ struct ip_vs_estimator {u64 outpps;u64 inbps;u64 outbps; + + u32 ktid:16, /* kthread ID */ + ktrow:8, /* row ID for kthread */ + ktcid:8; /* chain ID for kthread */ +}; + +/* Process estimators in multiple timer ticks */ +#define IPVS_EST_NTICKS 50 +/* Estimation uses a 2-second period */ +#define IPVS_EST_TICK ((2 * HZ) / IPVS_EST_NTICKS) + +/* Desired number of chains per tick */ +#define IPVS_EST_CHAIN_FACTOR 48 + +/* Compiled number of chains per tick +* The defines should match cond_resched_rcu +*/ +#if defined(CONFIG_DEBUG_ATOMIC_SLEEP ) || !defined(CONFIG_PREEMPT_RCU ) +#define IPVS_EST_TICK_CHAINS IPVS_EST_CHAIN_FACTOR +#else +#define IPVS_EST_TICK_CHAINS 1 +#endif + +/* Multiple chains processed in same tick */ +struct ip_vs_est_tick_data { +struct hlist_head chains[IPVS_EST_TICK_CHAINS ]; + DECLARE_BITMAP(present, IPVS_EST_TICK_CHAINS); + DECLARE_BITMAP(full, IPVS_EST_TICK_CHAINS); +int chain_len[IPVS_EST_TICK_CHAINS ]; +}; + +/* Context for estimation kthread */ +struct ip_vs_est_kt_data { +struct netns_ipvs *ipvs; +struct task_struct *task;/* task if running */ +struct ip_vs_est_tick_data __rcu *ticks[IPVS_EST_NTICKS ]; + DECLARE_BITMAP(avail, IPVS_EST_NTICKS); /* tick has space for ests */ +unsigned long est_timer;/* estimation timer (jiffies) */ +int tick_len[IPVS_EST_NTICKS ];/* est count */ +int id;/* ktid per netns */ +int chain_max_len;/* max ests per tick chain */ +int tick_max_len;/* max ests per tick */ +int est_count;/* attached ests to kthread */ +int est_max_count;/* max ests per kthread */ +int add_row;/* row for new ests */ +int est_row;/* estimated row */ };/*
@@ -948,9 +994,14 @@ struct netns_ipvs {struct ctl_table_header *lblcr_ctl_header;struct ctl_table *lblcr_ctl_table;/* ip_vs_est */ -struct list_head est_list;/* estimator list */ -spinlock_t est_lock; -struct timer_list est_timer;/* Estimation timer */ +struct delayed_work est_reload_work;/* Reload kthread tasks */ +struct mutex est_mutex;/* protect kthread tasks */ +struct ip_vs_est_kt_data **est_kt_arr;/* Array of kthread data ptrs */ +unsigned long est_max_threads;/* rlimit */ +int est_kt_count;/* Allocated ptrs */ +int est_add_ktid;/* ktid where to add ests */ +atomic_t est_genid;/* kthreads reload genid */ +atomic_t est_genid_done;/* applied genid */ /* ip_vs_sync */ spinlock_t sync_lock;struct ipvs_master_sync_state *ms;
@@ -1481,10 +1532,14 @@ int stop_sync_thread(struct netns_ipvs *ipvs, int state);void ip_vs_sync_conn (struct netns_ipvs *ipvs,struct ip_vs_conn *cp,int pkts);/* IPVS rate estimator prototypes (from ip_vs_est.c) */ -void ip_vs_start_estimator (struct netns_ipvs *ipvs,struct ip_vs_stats *stats); +int ip_vs_start_estimator (struct netns_ipvs *ipvs,struct ip_vs_stats *stats);void ip_vs_stop_estimator (struct netns_ipvs *ipvs,struct ip_vs_stats *stats);void ip_vs_zero_estimator (struct ip_vs_stats *stats);void ip_vs_read_estimator (struct ip_vs_kstats *dst,struct ip_vs_stats *stats); +void ip_vs_est_reload_start (struct netns_ipvs *ipvs); +int ip_vs_est_kthread_start (struct netns_ipvs *ipvs, +struct ip_vs_est_kt_data *kd); +void ip_vs_est_kthread_stop (struct ip_vs_est_kt_data *kd);/* Various IPVS packet transmitters (from ip_vs_xmit.c) */ int ip_vs_null_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
net/netfilter/ipvs/ip_vs_ctl.c +107 -30
306 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_est.c +397 -68
518 lines not on this page · raw message
Signature · 1 line
2.37.3
Julian Anastasov
maintainerRFC PATCH v3 3/5 ipvs: add est_cpulist and est_nice sysctl vars
In reply to Julian AnastasovAllow the kthreads for stats to be configured for specific cpulist (isolation) and niceness (scheduling priority).
--- Documentation/networking/ipvs-sysctl.rst | 20 ++++ include/net/ip_vs.h | 50 ++++++++ net/netfilter/ipvs/ip_vs_ctl.c | 141 ++++++++++++++++++++++- net/netfilter/ipvs/ip_vs_est.c | 9 +- 4 files changed, 218 insertions(+), 2 deletions(-)
Documentation/networking/ipvs-sysctl.rst +20 -0
@@ -129,6 +129,26 @@ drop_packet - INTEGER threshold. When the mode 3 is set, the always mode drop rate is controlled by the /proc/sys/net/ipv4/vs/am_droprate. +est_cpulist - CPULIST + Allowed CPUs for estimation kthreads + + Syntax: standard cpulist format + empty list - stop kthread tasks and estimation + default - the system's housekeeping CPUs for kthreads + + Example: + "all": all possible CPUs + "0-N": all possible CPUs, N denotes last CPU number + "0,1-N:1/2": first and all CPUs with odd number + "": empty list + +est_nice - INTEGER + default 0 + Valid range: -20 (more favorable) .. 19 (less favorable) + + Niceness value to use for the estimation kthreads (scheduling + priority) + expire_nodest_conn - BOOLEAN - 0 - disabled (default) - not 0 - enabled
include/net/ip_vs.h +50 -0
86 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_ctl.c +140 -1
177 lines not on this page · raw message
net/netfilter/ipvs/ip_vs_est.c +8 -1
@@ -55,6 +55,9 @@ - kthread contexts are created and attached to array - the kthread tasks are created when first service is added, before that the total stats are not estimated + - when configuration (cpulist/nice) is changed, the tasks are restarted + by work (est_reload_work) + - kthread tasks are stopped while the cpulist is empty - the kthread context holds lists with estimators (chains) which are processed every 2 seconds - as estimators can be added dynamically and in bursts, we try to spread
@@ -205,6 +208,7 @@ void ip_vs_est_reload_start(struct netns_ipvs *ipvs)/* Ignore reloads before first service is added */ if (!ipvs->enable)return ; +ip_vs_est_stopped_recalc (ipvs);/* Bump the kthread configuration genid */ atomic_inc (&ipvs->est_genid);queue_delayed_work (system_long_wq, &ipvs->est_reload_work,0 );
@@ -235,6 +239,9 @@ int ip_vs_est_kthread_start(struct netns_ipvs *ipvs, goto out; } +set_user_nice (kd->task,sysctl_est_nice (ipvs)); +set_cpus_allowed_ptr (kd->task,sysctl_est_cpulist (ipvs)); +pr_info ("starting estimator thread %d...\n" , kd->id);wake_up_process (kd->task);
@@ -300,7 +307,7 @@ static int ip_vs_est_add_kthread(struct netns_ipvs *ipvs) kd->tick_max_len =IPVS_EST_TICK_CHAINS * kd->chain_max_len; kd->est_max_count =IPVS_EST_NTICKS * kd->tick_max_len;/* Start kthread tasks only when services are present */ -if (ipvs->enable) { +if (ipvs->enable && !ip_vs_est_stopped (ipvs)) { ret =ip_vs_est_kthread_start (ipvs, kd);if (ret <0 ) goto out;
Signature · 1 line
2.37.3
Julian Anastasov
maintainerRFC PATCH v3 4/5 ipvs: run_estimation should control the kthread tasks
In reply to Julian AnastasovChange the run_estimation flag to start/stop the kthread tasks.
--- Documentation/networking/ipvs-sysctl.rst | 4 ++-- include/net/ip_vs.h | 6 +++-- net/netfilter/ipvs/ip_vs_ctl.c | 29 +++++++++++++++++++++++- net/netfilter/ipvs/ip_vs_est.c | 3 +-- 4 files changed, 35 insertions(+), 7 deletions(-)
Documentation/networking/ipvs-sysctl.rst +2 -2
@@ -324,8 +324,8 @@ run_estimation - BOOLEAN 0 - disabled not 0 - enabled (default) - If disabled, the estimation will be stop, and you can't see - any update on speed estimation data. + If disabled, the estimation will be suspended and kthread tasks + stopped. You can always re-enable estimation by setting this value to 1. But be careful, the first estimation after re-enable is not
include/net/ip_vs.h +4 -2
@@ -1577,8 +1577,10 @@ void ip_vs_est_kthread_stop(struct ip_vs_est_kt_data *kd);static inline void ip_vs_est_stopped_recalc (struct netns_ipvs *ipvs) {#ifdef CONFIG_SYSCTL - ipvs->est_stopped = ipvs->est_cpulist_valid && -cpumask_empty (sysctl_est_cpulist (ipvs)); +/* Stop tasks while cpulist is empty or if disabled with flag */ + ipvs->est_stopped = !sysctl_run_estimation (ipvs) || + (ipvs->est_cpulist_valid && +cpumask_empty (sysctl_est_cpulist (ipvs)));#endif }
net/netfilter/ipvs/ip_vs_ctl.c +28 -1
@@ -2036,6 +2036,32 @@ static int ipvs_proc_est_nice(struct ctl_table *table, int write,return ret; } +static int ipvs_proc_run_estimation (struct ctl_table *table,int write, +void *buffer,size_t *lenp,loff_t *ppos) +{ +struct netns_ipvs *ipvs = table->extra2; +int *valp = table->data; +int val = *valp; +int ret; + +struct ctl_table tmp_table = { + .data = &val, + .maxlen =sizeof (int ), + .mode = table->mode, + }; + + ret =proc_dointvec (&tmp_table, write, buffer, lenp, ppos); +if (write && ret >=0 ) { +mutex_lock (&ipvs->est_mutex); +if (*valp != val) { + *valp = val; +ip_vs_est_reload_start (ipvs); + } +mutex_unlock (&ipvs->est_mutex); + } +return ret; +} +/* * IPVS sysctl table (under the /proc/sys/net/ipv4/vs/) * Do not change order or insert new entries without
@@ -2210,7 +2236,7 @@ static struct ctl_table vs_vars[] = { .procname = "run_estimation", .maxlen = sizeof(int), .mode = 0644, - .proc_handler = proc_dointvec, + .proc_handler = ipvs_proc_run_estimation, }, { .procname = "est_cpulist",
@@ -4302,6 +4328,7 @@ static int __net_init ip_vs_control_net_init_sysctl(struct netns_ipvs *ipvs) tbl[idx++].data = &ipvs->sysctl_schedule_icmp; tbl[idx++].data = &ipvs->sysctl_ignore_tunneled; ipvs->sysctl_run_estimation =1 ; + tbl[idx].extra2 = ipvs; tbl[idx++].data = &ipvs->sysctl_run_estimation; ipvs->est_cpulist_valid =0 ;
net/netfilter/ipvs/ip_vs_est.c +1 -2
@@ -163,7 +163,6 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row)static int ip_vs_estimation_kthread (void *data) {struct ip_vs_est_kt_data *kd = data; -struct netns_ipvs *ipvs = kd->ipvs;int row = kd->est_row;unsigned long now;long gap;
@@ -188,7 +187,7 @@ static int ip_vs_estimation_kthread(void *data) kd->est_timer = now; } -if (sysctl_run_estimation (ipvs) && kd->tick_len[row]) +if (kd->tick_len[row])ip_vs_tick_estimation (kd, row); row++;
Signature · 1 line
2.37.3
Julian Anastasov
maintainerRFC PATCH v3 5/5 ipvs: debug the tick time
In reply to Julian AnastasovJust for testing print the tick time every 2 seconds
--- net/netfilter/ipvs/ip_vs_est.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
net/netfilter/ipvs/ip_vs_est.c +16 -0
@@ -141,7 +141,13 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row) {struct ip_vs_est_tick_data *td;int cid; +u64 ns =0 ; +static int used_row =-1 ; +if (used_row <0 ) + used_row = row; +if (row == used_row && !kd->id) + ns =ktime_get_ns ();rcu_read_lock (); td =rcu_dereference (kd->ticks[row]);if (!td)
@@ -158,6 +164,16 @@ static void ip_vs_tick_estimation(struct ip_vs_est_kt_data *kd, int row) out:rcu_read_unlock (); +if (row == used_row && !kd->id) { +static int ncpu; + + ns =ktime_sub (ktime_get_ns (), ns); +if (!ncpu) + ncpu =num_possible_cpus (); +pr_info ("tick time: %lluns for %d CPUs, %d ests, %d chains, max depth %d\n" , + (unsigned long long ) ns, ncpu, kd->tick_len[row], +IPVS_EST_TICK_CHAINS , kd->chain_max_len); + } }static int ip_vs_estimation_kthread (void *data)
Signature · 1 line
2.37.3