net: sched: fix some issues
Subsystems (3)
v5 cover net: sched: fix some issues
fix calculation of max_size in tbf
fix quantum calculation introduced by 64bit rates.
v2
patch 1/2: redescribe the regression.
patch 2/2: add Eric's ack.
v3
patch 1/2: use psched_l2t_ns to calculate max_size and cleanup exit/done section suggested by Jesper.
v4
patch 1/2:
1.Update commit message suggested by Jesper.
2.Use a macro to replace 65535 constant.
3.Add condition that when peakrate is lower than rate, return -EINVAL. 4.Don't use cell_log anymore.
v5
patch 1/2:
1.Remove rtab and ptab use suggested by Eric. 2.Don't reduce max_size to 65536, as Eric suggested that
if a burst set to 200KB, we do not want tbf use 64KB or even less. So add a helper psched_ns_t2l to calculate max_size directly.
Yang Yingliang (2):
net: sched: tbf: fix calculation of max_size
net: sched: htb: fix calculation of quantum
include/net/sch_generic.h | 46 ++++++++++++++++++++++++++++ net/sched/sch_htb.c | 18 ++++++----- net/sched/sch_tbf.c | 76 ++++++++++++++++++++--------------------------- 3 files changed, 89 insertions(+), 51 deletions(-)
Signature · 1 line
1.8.0
PATCH v5 1/2 net: sched: tbf: fix calculation of max_size
Current max_size is caluated from rate table. Now, the rate table has been replaced and it's wrong to caculate max_size based on this rate table. It can lead wrong calculation of max_size.
The burst in kernel may be lower than user asked, because burst may gets some loss when transform it to buffer(E.g. "burst 40kb rate 30mbit/s") and it seems we cannot avoid this loss. Burst's value(max_size) based on rate table may be equal user asked. If a packet's length is max_size, this packet will be stalled in tbf_dequeue() because its length is above the burst in kernel so that it cannot get enough tokens. The max_size guards against enqueuing packet sizes above q->buffer "time" in tbf_enqueue().
To make consistent with the calculation of tokens, this patch add a helper psched_ns_t2l() to calculate burst(max_size) directly to fix this problem.
After this fix, we can support to using 64bit rates to calculate burst as well.
--- include/net/sch_generic.h | 46 ++++++++++++++++++++++++++++ net/sched/sch_tbf.c | 76 ++++++++++++++++++++--------------------------- 2 files changed, 79 insertions(+), 43 deletions(-)
include/net/sch_generic.h +46 -0
@@ -701,6 +701,52 @@ static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,return ((u64 )len * r->mult) >> r->shift; } +/* Time to Length, convert time in ns to length in bytes +* to determinate how many bytes can be sent in given time. +*/ +static inline u64 psched_ns_t2l (const struct psched_ratecfg *r, +u64 time_in_ns) +{ +u64 len = time_in_ns; +u8 shift = r->shift; +bool is_div = false; + +/* The formula is : +* len = (time_in_ns << shift) / mult +* when time_in_ns does shift, it would overflow. +* If overflow happens first time, do division. +* Then do shift. If it happens again, +* set lenth to ~0ULL. +*/ +while (shift) { +if (len & (1ULL <<63 )) { +if (!is_div) { + len =div64_u64 (len, r->mult); + is_div = true; + }else { +/* overflow happens */ + len = ~0ULL ; + is_div = true; +break ; + } + } + len <<=1 ; + shift--; + } +if (!is_div) + len =div64_u64 (len, r->mult); + +if (unlikely (r->linklayer ==TC_LINKLAYER_ATM )) + len = (len /53 ) *48 ; + +if (len > r->overhead) + len -= r->overhead; +else + len =0 ; + +return len; +} +void psched_ratecfg_precompute (struct psched_ratecfg *r,const struct tc_ratespec *conf,u64 rate64);
net/sched/sch_tbf.c +33 -43
111 lines not on this page · raw message
Signature · 1 line
1.8.0
Re: [PATCH net v5 1/2] net: sched: tbf: fix calculation of max_size
Quoted 12 lines · Yang Yingliang
Current
max_sizeis caluated from rate table. Now, the rate table has been replaced and it's wrong to caculatemax_sizebased on this rate table. It can lead wrong calculation ofmax_size.The burst in kernel may be lower than user asked, because burst may gets some loss when transform it to buffer(E.g. "burst 40kb rate 30mbit/s") and it seems we cannot avoid this loss. Burst's
value(max_size)based on rate table may be equal user asked. If a packet's length ismax_size, this packet will be stalled intbf_dequeue()because its length is above the burst in kernel so that it cannot get enough tokens. Themax_sizeguards against enqueuing packet sizes aboveq->buffer"time" intbf_enqueue().
Quoted 7 lines
To make consistent with the calculation of tokens, this patch add a helper
psched_ns_t2l()to calculateburst(max_size)directly to fix this problem.After this fix, we can support to using 64bit rates to calculate burst as well.
Suggested-by Jesper Dangaard Brouer <[email protected]>Suggested-by Eric Dumazet <[email protected]>
No, I did not suggest this patch.
Quoted 11 lines
Signed-off-by Yang Yingliang <[email protected]>--- include/net/sch_generic.h | 46 ++++++++++++++++++++++++++++ net/sched/sch_tbf.c | 76 ++++++++++++++++++++--------------------------- 2 files changed, 79 insertions(+), 43 deletions(-)include/net/sch_generic.h +0
-0
include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 708
Quoted 8 lines
include/net/sch_generic.h +5
-0@@ -701,6 +701,52 @@ static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,return ((u64 )len * r->mult) >> r->shift; } +/* Time to Length, convert time in ns to length in bytes +* to determinate how many bytes can be sent in given time. +*/ +static inline u64 psched_ns_t2l (const struct psched_ratecfg *r, +u64 time_in_ns)
inline ?
Really ?
This is management path, there is no point inlining this.
Quoted 19 lines
include/net/sch_generic.h +19
-0+{ +u64 len = time_in_ns; +u8 shift = r->shift; +bool is_div = false; + +/* The formula is : +* len = (time_in_ns << shift) / mult +* when time_in_ns does shift, it would overflow. +* If overflow happens first time, do division. +* Then do shift. If it happens again, +* set lenth to ~0ULL. +*/ +while (shift) { +if (len & (1ULL <<63 )) { +if (!is_div) { + len =div64_u64 (len, r->mult); + is_div = true; + }else { +/* overflow happens */
include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 737
Quoted 10 lines
include/net/sch_generic.h +10
-0+ len = ~0ULL ; + is_div = true; +break ; + } + } + len <<=1 ; + shift--; + } +if (!is_div) + len =div64_u64 (len, r->mult);
Thats terrible.
Given that the intended formula was :
time_in_ns = (NSEC_PER_SEC * len) / rate_bps;
This translates to following optimal C code
u64 len = time_in_ns * r->rate_bytes_ps;do_div(len, NSEC_PER_SEC);
Why do you use r->shift and r->mult which are optimized for the reverse operation in fast path (no divide), I do not know.
include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 745
Quoted 9 lines
include/net/sch_generic.h +6
-1+ max_size =min_t (u64,psched_ns_t2l (&q->rate, q->buffer), ~0U ); + +if (qopt->peakrate.rate) {if (tb[TCA_TBF_PRATE64 ]) prate64 =nla_get_u64 (tb[TCA_TBF_PRATE64 ]); -psched_ratecfg_precompute (&q->peak, &ptab->rate, prate64); +psched_ratecfg_precompute (&q->peak, &qopt->peakrate, prate64); +if (q->peak.rate_bytes_ps <= q->rate.rate_bytes_ps) { +pr_err ("Peakrate must be higher than rate!\n" );
Do not add messages like that without rate limiting them.
Plus there is no context, we know nothing.
Re: [PATCH net v5 1/2] net: sched: tbf: fix calculation of max_size
Hi, Eric
Thanks for your reviewing and advice!
Quoted 6 lines · Eric Dumazet
Quoted 3 lines · Yang Yingliang
Suggested-by Jesper Dangaard Brouer <[email protected]>Suggested-by Eric Dumazet <[email protected]>No, I did not suggest this patch.
You suggested that
if (qopt->rate.linklayer == TC_LINKLAYER_UNAWARE) qdisc_put_rtab(qdisc_get_rtab(&qopt->rate, tb[TCA_TBF_RTAB]));
so I added your suggestion.
Quoted 63 lines
Quoted 12 lines
Signed-off-by Yang Yingliang <[email protected]>--- include/net/sch_generic.h | 46 ++++++++++++++++++++++++++++ net/sched/sch_tbf.c | 76 ++++++++++++++++++++--------------------------- 2 files changed, 79 insertions(+), 43 deletions(-)include/net/sch_generic.h +0
-0include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 708
Quoted 8 lines
include/net/sch_generic.h +5
-0@@ -701,6 +701,52 @@ static inline u64 psched_l2t_ns(const struct psched_ratecfg *r,return ((u64 )len * r->mult) >> r->shift; } +/* Time to Length, convert time in ns to length in bytes +* to determinate how many bytes can be sent in given time. +*/ +static inline u64 psched_ns_t2l (const struct psched_ratecfg *r, +u64 time_in_ns)inline ?
Really ?
This is management path, there is no point inlining this.
Quoted 19 lines
include/net/sch_generic.h +19
-0+{ +u64 len = time_in_ns; +u8 shift = r->shift; +bool is_div = false; + +/* The formula is : +* len = (time_in_ns << shift) / mult +* when time_in_ns does shift, it would overflow. +* If overflow happens first time, do division. +* Then do shift. If it happens again, +* set lenth to ~0ULL. +*/ +while (shift) { +if (len & (1ULL <<63 )) { +if (!is_div) { + len =div64_u64 (len, r->mult); + is_div = true; + }else { +/* overflow happens */ include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 737
Quoted 10 lines
include/net/sch_generic.h +10
-0+ len = ~0ULL ; + is_div = true; +break ; + } + } + len <<=1 ; + shift--; + } +if (!is_div) + len =div64_u64 (len, r->mult);Thats terrible.
Given that the intended formula was :
time_in_ns= (NSEC_PER_SEC* len) /rate_bps;
Quoted 7 lines
This translates to following optimal C code
u64 len =
time_in_ns*r->rate_bytes_ps;do_div(len, NSEC_PER_SEC);Why do you use
r->shiftandr->multwhich are optimized for the reverse operation in fast path (no divide), I do not know.
The formula to calculate tokens is :
(len * r->mult) >> r->shift
so I tried to use r->shift and r->mult to calculate the len.
Your way looks better, I'll change it in v6.
Quoted 13 lines
include/net/sch_generic.h static inline u64 psched_l2t_ns(const struct psched_ratecfg *r, · line 745
Quoted 10 lines
include/net/sch_generic.h +6
-1+ max_size =min_t (u64,psched_ns_t2l (&q->rate, q->buffer), ~0U ); + +if (qopt->peakrate.rate) {if (tb[TCA_TBF_PRATE64 ]) prate64 =nla_get_u64 (tb[TCA_TBF_PRATE64 ]); -psched_ratecfg_precompute (&q->peak, &ptab->rate, prate64); +psched_ratecfg_precompute (&q->peak, &qopt->peakrate, prate64); +if (q->peak.rate_bytes_ps <= q->rate.rate_bytes_ps) { +pr_err ("Peakrate must be higher than rate!\n" );Do not add messages like that without rate limiting them.
Quoted 1 line
Plus there is no context, we know nothing.
OK, I'll use pr_warn_ratelimited instead.
Regards,
Yang
Re: [PATCH net v5 1/2] net: sched: tbf: fix calculation of max_size
Quoted 11 lines · Yang Yingliang
Hi, Eric
Thanks for your reviewing and advice!Quoted 6 lines · Eric Dumazet
Quoted 3 lines · Yang Yingliang
Suggested-by Jesper Dangaard Brouer <[email protected]>Suggested-by Eric Dumazet <[email protected]>No, I did not suggest this patch.
Quoted 7 lines
You suggested that
if (qopt->rate.linklayer == TC_LINKLAYER_UNAWARE) qdisc_put_rtab(qdisc_get_rtab(&qopt->rate, tb[TCA_TBF_RTAB]));so I added your suggestion.
OK : The exact meaning of 'Suggested-by: XX' is :
1) the patch idea was given by XX.
2) Then you implemented it.
This is not really the case here : You had the idea of this patch.
I made some feedbacks, you have no special tag to add for that.
Later, if I agree with your next patch set I'll add my own "Acked-by: ..."
If not (for example I am slow to respond and it got merged), its not a big deal.
Thanks
Re: [PATCH net v5 1/2] net: sched: tbf: fix calculation of max_size
Quoted 29 lines · Eric Dumazet
Quoted 11 lines · Yang Yingliang
Hi, Eric
Thanks for your reviewing and advice!Quoted 6 lines · Eric Dumazet
Quoted 3 lines · Yang Yingliang
Suggested-by Jesper Dangaard Brouer <[email protected]>Suggested-by Eric Dumazet <[email protected]>No, I did not suggest this patch.
Quoted 7 lines
You suggested that
if (qopt->rate.linklayer == TC_LINKLAYER_UNAWARE) qdisc_put_rtab(qdisc_get_rtab(&qopt->rate, tb[TCA_TBF_RTAB]));so I added your suggestion.
OK : The exact meaning of 'Suggested-by: XX' is :
1) the patch idea was given by XX.
2) Then you implemented it.This is not really the case here : You had the idea of this patch.
I made some feedbacks, you have no special tag to add for that.
Quoted 8 lines
Later, if I agree with your next patch set I'll add my own "Acked-by: ..."
If not (for example I am slow to respond and it got merged), its not a big deal.
Thanks
Got it.
Thanks for your explanation!
Quoted 3 lines
PATCH v5 2/2 net: sched: htb: fix calculation of quantum
In reply to Yang YingliangNow, 32bit rates may be not the true rate.
So use rate_bytes_ps which is frommax(rate32, rate64) to calcualte quantum.
--- net/sched/sch_htb.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)
net/sched/sch_htb.c +10 -8
@@ -1477,11 +1477,20 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,sch_tree_lock (sch); } + rate64 = tb[TCA_HTB_RATE64 ] ?nla_get_u64 (tb[TCA_HTB_RATE64 ]) :0 ; + + ceil64 = tb[TCA_HTB_CEIL64 ] ?nla_get_u64 (tb[TCA_HTB_CEIL64 ]) :0 ; + +psched_ratecfg_precompute (&cl->rate, &hopt->rate, rate64); +psched_ratecfg_precompute (&cl->ceil, &hopt->ceil, ceil64); +/* it used to be a nasty bug here, we have to check that node * is really leaf before changing cl->un.leaf ! */ if (!cl->level) { - cl->quantum = hopt->rate.rate / q->rate2quantum; +u64 quantum =div64_u64 (cl->rate.rate_bytes_ps, + q->rate2quantum); + cl->quantum =min_t (u64, quantum,INT_MAX );if (!hopt->quantum && cl->quantum <1000 ) { pr_warning( "HTB: quantum of class %X is small. Consider r2q change.\n",
@@ -1500,13 +1509,6 @@ static int htb_change_class(struct Qdisc *sch, u32 classid, cl->prio =TC_HTB_NUMPRIO -1 ; } - rate64 = tb[TCA_HTB_RATE64 ] ?nla_get_u64 (tb[TCA_HTB_RATE64 ]) :0 ; - - ceil64 = tb[TCA_HTB_CEIL64 ] ?nla_get_u64 (tb[TCA_HTB_CEIL64 ]) :0 ; - -psched_ratecfg_precompute (&cl->rate, &hopt->rate, rate64); -psched_ratecfg_precompute (&cl->ceil, &hopt->ceil, ceil64); - cl->buffer =PSCHED_TICKS2NS (hopt->buffer); cl->cbuffer =PSCHED_TICKS2NS (hopt->cbuffer);
Signature · 1 line
1.8.0
Re: [PATCH net v5 2/2] net: sched: htb: fix calculation of quantum
Quoted 22 lines · Yang Yingliang
Now, 32bit rates may be not the true rate.
So userate_bytes_pswhich is frommax(rate32, rate64)to calcualte quantum.Signed-off-by Yang Yingliang <[email protected]>Acked-by Eric Dumazet <[email protected]>--- net/sched/sch_htb.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-)net/sched/sch_htb.c +4
-0@@ -1477,11 +1477,20 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,sch_tree_lock (sch); } + rate64 = tb[TCA_HTB_RATE64 ] ?nla_get_u64 (tb[TCA_HTB_RATE64 ]) :0 ; + + ceil64 = tb[TCA_HTB_CEIL64 ] ?nla_get_u64 (tb[TCA_HTB_CEIL64 ]) :0 ; +
net/sched/sch_htb.c static int htb_change_class(struct Qdisc *sch, u32 classid, · line 1492
Quoted 10 lines
net/sched/sch_htb.c +5
-1+psched_ratecfg_precompute (&cl->rate, &hopt->rate, rate64); +psched_ratecfg_precompute (&cl->ceil, &hopt->ceil, ceil64); +/* it used to be a nasty bug here, we have to check that node * is really leaf before changing cl->un.leaf ! */ if (!cl->level) { - cl->quantum = hopt->rate.rate / q->rate2quantum; +u64 quantum =div64_u64 (cl->rate.rate_bytes_ps, + q->rate2quantum);
q->rate2quantum being 32bit, prefer the following : u64 quantum = cl->rate.rate_bytes_ps; do_div(quantum, q->rate2quantum); Since it maps to better assembly code on 32bit arches.
Quoted 18 lines
net/sched/sch_htb.c +1
-7+ cl->quantum =min_t (u64, quantum,INT_MAX );if (!hopt->quantum && cl->quantum <1000 ) { pr_warning( "HTB: quantum of class %X is small. Consider r2q change.\n",@@ -1500,13 +1509,6 @@ static int htb_change_class(struct Qdisc *sch, u32 classid, cl->prio =TC_HTB_NUMPRIO -1 ; } - rate64 = tb[TCA_HTB_RATE64 ] ?nla_get_u64 (tb[TCA_HTB_RATE64 ]) :0 ; - - ceil64 = tb[TCA_HTB_CEIL64 ] ?nla_get_u64 (tb[TCA_HTB_CEIL64 ]) :0 ; - -psched_ratecfg_precompute (&cl->rate, &hopt->rate, rate64); -psched_ratecfg_precompute (&cl->ceil, &hopt->ceil, ceil64); - cl->buffer =PSCHED_TICKS2NS (hopt->buffer); cl->cbuffer =PSCHED_TICKS2NS (hopt->cbuffer);
Re: [PATCH net v5 2/2] net: sched: htb: fix calculation of quantum
Quoted 13 lines · Eric Dumazet
net/sched/sch_htb.c static int htb_change_class(struct Qdisc *sch, u32 classid, · line 1492
Quoted 4 lines · Yang Yingliang
net/sched/sch_htb.c +2
-1if (!cl->level) { - cl->quantum = hopt->rate.rate / q->rate2quantum; +u64 quantum =div64_u64 (cl->rate.rate_bytes_ps, + q->rate2quantum);q->rate2quantum being 32bit, prefer the following : u64 quantum = cl->rate.rate_bytes_ps; do_div(quantum, q->rate2quantum); Since it maps to better assembly code on 32bit arches.
Change it in v6, thanks!
Regards,
Yang