: Avoid `division by zero' errors
PATCH 7/10 : Avoid `division by zero' errors
[CCID 3]: Avoid `division by zero' errors
Several places of the code divide by the current RTT value. A division-by-zero error results if this value is 0.
To protect against this,
* DCCP_BUG_ON conditions are added throughout the tx code;
* in ccid3_hc_tx_packet_recv, a code block is shifted, to avoid not updating
p and x_recv in case the RTT estimation results in an error;
Since this could probably be done in a smarter way, a FIXME is added. Lastly, some minor code comment updates.
--- net/dccp/ccids/ccid3.c | 53 ++++++++++++++++++++----------------- net/dccp/ccids/ccid3.h | 2 - net/dccp/ccids/lib/tfrc_equation.c | 7 +--- 3 files changed, 33 insertions(+), 29 deletions(-)
net/dccp/ccids/ccid3.c +30 -23
@@ -117,7 +117,7 @@ static inline void ccid3_update_send_tim TFRC_OPSYS_HALF_TIME_GRAN); }/* -* Update X by +* Update X by (this implements step (4) of [RFC 3448, 4.3]): * If (p > 0) * x_calc = calcX(s, R, p); * X = max(min(X_calc, 2 * X_recv), s / t_mbi);
@@ -125,12 +125,17 @@ static inline void ccid3_update_send_tim* If (now - tld >= R) * X = max(min(2 * X, 2 * X_recv), s / R); * tld = now; +* +* If X has changed, we also update the scheduled send time t_now, +* the inter-packet interval t_ipi, and the delta value. */ static void ccid3_hc_tx_update_x (struct sock *sk) {struct ccid3_hc_tx_sock *hctx =ccid3_hc_tx_sk (sk);const __u32 old_x = hctx->ccid3hctx_x; +DCCP_BUG_ON (hctx->ccid3hctx_rtt =0 ); +/* To avoid large error in calcX */ if (hctx->ccid3hctx_p >=TFRC_SMALLEST_P ) { hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,
@@ -421,7 +426,6 @@ static void ccid3_hc_tx_packet_recv(struunsigned long next_tmout;u32 t_elapsed;u32 pinv; -u32 x_recv;u32 r_sample;BUG_ON (hctx =NULL );
@@ -434,15 +438,11 @@ static void ccid3_hc_tx_packet_recv(stru opt_recv = &hctx->ccid3hctx_options_received; t_elapsed = dp->dccps_options_received.dccpor_elapsed_time *10 ; - x_recv = opt_recv->ccid3or_receive_rate; pinv = opt_recv->ccid3or_loss_event_rate;switch (hctx->ccid3hctx_state) {case TFRC_SSTATE_NO_FBACK :case TFRC_SSTATE_FBACK : -/* Calculate new round trip sample by -* R_sample = (now - t_recvdata) - t_delay */ -/* get t_recvdata from history */ packet =dccp_tx_hist_find_entry (&hctx->ccid3hctx_hist,DCCP_SKB_CB (skb)->dccpd_ack_seq);if (unlikely (packet =NULL )) {
@@ -453,7 +453,25 @@ static void ccid3_hc_tx_packet_recv(strureturn ; } -/* Update RTT */ +/* Update receive rate */ + hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate; + +/* Update loss event rate */ +if (pinv = ~0 || pinv =0 ) + hctx->ccid3hctx_p =0 ; +else { + hctx->ccid3hctx_p =1000000 / pinv; + +if (hctx->ccid3hctx_p <TFRC_SMALLEST_P ) { + hctx->ccid3hctx_p =TFRC_SMALLEST_P ; +ccid3_pr_debug ("%s, sk=%p, Smallest p used!\n" , +dccp_role (sk), sk); + } + } + +/* Calculate new round trip sample by +* R_sample = (t_now - t_recvdata) - t_delay +* t_recvdata is taken from packet history */ dccp_timestamp (sk, &now); r_sample =timeval_delta (&now, &packet->dccphtx_tstamp);if (unlikely (r_sample <= t_elapsed))
@@ -462,7 +480,7 @@ static void ccid3_hc_tx_packet_recv(struelse r_sample -= t_elapsed; -/* Update RTT estimate by +/* Update RTT estimate by (step (2) of [RFC 3448, 4.3]): * If (No feedback recv) * R = R_sample; * Else
@@ -481,21 +499,10 @@ static void ccid3_hc_tx_packet_recv(stru"r_sample=%us\n" ,dccp_role (sk), sk, hctx->ccid3hctx_rtt, r_sample); -/* Update receive rate */ - hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */ - -/* Update loss event rate */ -if (pinv = ~0 || pinv =0 ) - hctx->ccid3hctx_p =0 ; -else { - hctx->ccid3hctx_p =1000000 / pinv; - -if (hctx->ccid3hctx_p <TFRC_SMALLEST_P ) { - hctx->ccid3hctx_p =TFRC_SMALLEST_P ; -ccid3_pr_debug ("%s, sk=%p, Smallest p used!\n" , -dccp_role (sk), sk); - } - } +/* Avoid `division-by zero' errors. +* FIXME: We need a smarter and more robust way to +* protect against this possibility */ +DCCP_BUG_ON (hctx->ccid3hctx_rtt =0 );/* unschedule no feedback timer */ sk_stop_timer (sk, &hctx->ccid3hctx_no_feedback_timer);
net/dccp/ccids/lib/tfrc_equation.c +2 -5
@@ -578,6 +578,8 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p)u32 f;u64 tmp1, tmp2; +DCCP_BUG_ON (R =0 );/* RTT can't be zero or else divide by zero */ +if (p <TFRC_CALC_X_SPLIT ) index = (p / (TFRC_CALC_X_SPLIT /TFRC_CALC_X_ARRSIZE )) -1 ;else
@@ -587,11 +589,6 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p)/* p should be 0 unless there is a bug in my code */ index =0 ; -if (R =0 ) { -DCCP_WARN ("RTT=0, setting to 1\n" ); -R =1 ;/* RTT can't be zero or else divide by zero */ - } -BUG_ON (index >=TFRC_CALC_X_ARRSIZE );if (p >=TFRC_CALC_X_SPLIT )
net/dccp/ccids/ccid3.h +1 -1
@@ -78,7 +78,7 @@ enum ccid3_hc_tx_states {/** struct ccid3_hc_tx_sock - CCID3 sender half-connection socket * * @ccid3hctx_x - Current sending rate -* @ccid3hctx_x_recv - Receive rate +* @ccid3hctx_x_recv - Receive rate in bytes per second * @ccid3hctx_x_calc - Calculated send rate (RFC 3448, 3.1) * @ccid3hctx_rtt - Estimate of current round trip time in usecs * @ccid3hctx_p - Current loss event rate (0-1) scaled by 1000000
Quoted 6 lines · Gerrit Renker
[CCID 3]: Avoid `division by zero' errors
Several places of the code divide by the current RTT value. A division-by-zero error results if this value is 0.
To protect against this,
*DCCP_BUG_ONconditions are added throughout the tx code;
I can't remember what the definition of DCCP_BUG_ON was in the end.
If it does follow the BUG path then I would like this changed to DCCP_WARN... so that we don't kill the machine.
Ian
Signature · 7 lines
Ian McDonald Web: http://wand.net.nz/~iam4 Blog: http://imcdnzl.blogspot.com WAND Network Research Group Department of Computer Science University of Waikato New Zealand
Quoting Ian McDonald:
| On 11/25/06, Gerrit Renker <[email protected]> wrote: | > [CCID 3]: Avoid `division by zero' errors | > | > Several places of the code divide by the current RTT value. A division-by-zero | > error results if this value is 0. | > To protect against this, | > * DCCP_BUG_ON conditions are added throughout the tx code; | | I can't remember what the definition of DCCP_BUG_ON was in the end. | | If it does follow the BUG path then I would like this changed to | DCCP_WARN... so that we don't kill the machine.
DCCP_BUG_ON is exactly for this purpose since we discussed it earlier:
#define DCCP_BUG(a...) do { DCCP_CRIT("BUG: " a); dump_stack(); } while(0)
#define DCCP_BUG_ON(cond) do { if (unlikely((cond) != 0)) \
DCCP_BUG("\"%s\" holds (exception!)", \
__stringify(cond)); \
} while (0)The most important thing is that in places where DCCP_BUG(_ON) is called, it means that something _internal_ is going wrong; and this should be fixed. Once it is fixed, and we are sure it is unlikely to occur, we can simply replace
DCCP_BUG() with BUG() and DCCP_BUG_ON() with BUG_ON()
DCCP_WARN() on the other side is used for _external_ conditions (outside our control)] going wrong, hence it is rate-limited, as suggested by Arnaldo.
Same comment (it's probably safe to use the Req-Response exchange for an initial RTT estimate).
E
Quoted 172 lines · Gerrit Renker
[CCID 3]: Avoid `division by zero' errors
Several places of the code divide by the current RTT value. A division-by-zero error results if this value is 0.
To protect against this,
*DCCP_BUG_ONconditions are added throughout the tx code;
* inccid3_hc_tx_packet_recv, a code block is shifted, to avoid not updatingp and x_recv in case the RTT estimation results in an error;Since this could probably be done in a smarter way, a FIXME is added. Lastly, some minor code comment updates.
Signed-off-by Gerrit Renker <[email protected]>--- net/dccp/ccids/ccid3.c | 53 ++++++++++++++++++++----------------- net/dccp/ccids/ccid3.h | 2 - net/dccp/ccids/lib/tfrc_equation.c | 7 +--- 3 files changed, 33 insertions(+), 29 deletions(-)net/dccp/ccids/ccid3.c +30
-23@@ -117,7 +117,7 @@ static inline void ccid3_update_send_tim TFRC_OPSYS_HALF_TIME_GRAN); }/* -* Update X by +* Update X by (this implements step (4) of [RFC 3448, 4.3]): * If (p > 0) * x_calc = calcX(s, R, p); * X = max(min(X_calc, 2 * X_recv), s / t_mbi); @@ -125,12 +125,17 @@ static inline void ccid3_update_send_tim* If (now - tld >= R) * X = max(min(2 * X, 2 * X_recv), s / R); * tld = now; +* +* If X has changed, we also update the scheduled send time t_now, +* the inter-packet interval t_ipi, and the delta value. */ static void ccid3_hc_tx_update_x (struct sock *sk) {struct ccid3_hc_tx_sock *hctx =ccid3_hc_tx_sk (sk);const __u32 old_x = hctx->ccid3hctx_x; +DCCP_BUG_ON (hctx->ccid3hctx_rtt =0 ); +/* To avoid large error in calcX */ if (hctx->ccid3hctx_p >=TFRC_SMALLEST_P ) { hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s,@@ -421,7 +426,6 @@ static void ccid3_hc_tx_packet_recv(struunsigned long next_tmout;u32 t_elapsed;u32 pinv; -u32 x_recv;u32 r_sample;BUG_ON (hctx =NULL );@@ -434,15 +438,11 @@ static void ccid3_hc_tx_packet_recv(stru opt_recv = &hctx->ccid3hctx_options_received; t_elapsed = dp->dccps_options_received.dccpor_elapsed_time *10 ; - x_recv = opt_recv->ccid3or_receive_rate; pinv = opt_recv->ccid3or_loss_event_rate;switch (hctx->ccid3hctx_state) {case TFRC_SSTATE_NO_FBACK :case TFRC_SSTATE_FBACK : -/* Calculate new round trip sample by -* R_sample = (now - t_recvdata) - t_delay */ -/* get t_recvdata from history */ packet =dccp_tx_hist_find_entry (&hctx->ccid3hctx_hist,DCCP_SKB_CB (skb)->dccpd_ack_seq);if (unlikely (packet =NULL )) {@@ -453,7 +453,25 @@ static void ccid3_hc_tx_packet_recv(strureturn ; } -/* Update RTT */ +/* Update receive rate */ + hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate; + +/* Update loss event rate */ +if (pinv = ~0 || pinv =0 ) + hctx->ccid3hctx_p =0 ; +else { + hctx->ccid3hctx_p =1000000 / pinv; + +if (hctx->ccid3hctx_p <TFRC_SMALLEST_P ) { + hctx->ccid3hctx_p =TFRC_SMALLEST_P ; +ccid3_pr_debug ("%s, sk=%p, Smallest p used!\n" , +dccp_role (sk), sk); + } + } + +/* Calculate new round trip sample by +* R_sample = (t_now - t_recvdata) - t_delay +* t_recvdata is taken from packet history */ dccp_timestamp (sk, &now); r_sample =timeval_delta (&now, &packet->dccphtx_tstamp);if (unlikely (r_sample <= t_elapsed))@@ -462,7 +480,7 @@ static void ccid3_hc_tx_packet_recv(struelse r_sample -= t_elapsed; -/* Update RTT estimate by +/* Update RTT estimate by (step (2) of [RFC 3448, 4.3]): * If (No feedback recv) * R = R_sample; * Else @@ -481,21 +499,10 @@ static void ccid3_hc_tx_packet_recv(stru"r_sample=%us\n" ,dccp_role (sk), sk, hctx->ccid3hctx_rtt, r_sample); -/* Update receive rate */ - hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */ - -/* Update loss event rate */ -if (pinv = ~0 || pinv =0 ) - hctx->ccid3hctx_p =0 ; -else { - hctx->ccid3hctx_p =1000000 / pinv; - -if (hctx->ccid3hctx_p <TFRC_SMALLEST_P ) { - hctx->ccid3hctx_p =TFRC_SMALLEST_P ; -ccid3_pr_debug ("%s, sk=%p, Smallest p used!\n" , -dccp_role (sk), sk); - } - } +/* Avoid `division-by zero' errors. +* FIXME: We need a smarter and more robust way to +* protect against this possibility */ +DCCP_BUG_ON (hctx->ccid3hctx_rtt =0 );/* unschedule no feedback timer */ sk_stop_timer (sk, &hctx->ccid3hctx_no_feedback_timer);net/dccp/ccids/lib/tfrc_equation.c +2
-5@@ -578,6 +578,8 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p)u32 f;u64 tmp1, tmp2; +DCCP_BUG_ON (R =0 );/* RTT can't be zero or else divide by zero */ +if (p <TFRC_CALC_X_SPLIT ) index = (p / (TFRC_CALC_X_SPLIT /TFRC_CALC_X_ARRSIZE )) -1 ;else @@ -587,11 +589,6 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p)/* p should be 0 unless there is a bug in my code */ index =0 ; -if (R =0 ) { -DCCP_WARN ("RTT=0, setting to 1\n" ); -R =1 ;/* RTT can't be zero or else divide by zero */ - } -BUG_ON (index >=TFRC_CALC_X_ARRSIZE );if (p >=TFRC_CALC_X_SPLIT )net/dccp/ccids/ccid3.h +1
-2@@ -78,7 +78,7 @@ enum ccid3_hc_tx_states {/** struct ccid3_hc_tx_sock - CCID3 sender half-connection socket * * @ccid3hctx_x - Current sending rate -* @ccid3hctx_x_recv - Receive rate +* @ccid3hctx_x_recv - Receive rate in bytes per second * @ccid3hctx_x_calc - Calculated send rate (RFC 3448, 3.1) * @ccid3hctx_rtt - Estimate of current round trip time in usecs * @ccid3hctx_p - Current loss event rate (0-1) scaled by 1000000 -To unsubscribe from this list: send the line "unsubscribe dccp" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoting Eddie Kohler:
| Same comment (it's probably safe to use the Req-Response exchange for an | initial RTT estimate). Same answer: this is part of draft-ietf-dccp-rfc3448bis-00.txt, but not part of RFC 3448; therefore I am against it. | | E | | | Gerrit Renker wrote: | > [CCID 3]: Avoid `division by zero' errors | > | > Several places of the code divide by the current RTT value. A division-by-zero | > error results if this value is 0. | > To protect against this, | > * DCCP_BUG_ON conditions are added throughout the tx code; | > * in ccid3_hc_tx_packet_recv, a code block is shifted, to avoid not updating | > p and x_recv in case the RTT estimation results in an error; | > | > Since this could probably be done in a smarter way, a FIXME is added. Lastly, | > some minor code comment updates. | > | > Signed-off-by: Gerrit Renker <[email protected]> | > --- | > net/dccp/ccids/ccid3.c | 53 ++++++++++++++++++++----------------- | > net/dccp/ccids/ccid3.h | 2 - | > net/dccp/ccids/lib/tfrc_equation.c | 7 +--- | > 3 files changed, 33 insertions(+), 29 deletions(-) | > | > --- a/net/dccp/ccids/ccid3.c | > +++ b/net/dccp/ccids/ccid3.c | > @@ -117,7 +117,7 @@ static inline void ccid3_update_send_tim | > TFRC_OPSYS_HALF_TIME_GRAN); | > } | > /* | > - * Update X by | > + * Update X by (this implements step (4) of [RFC 3448, 4.3]): | > * If (p > 0) | > * x_calc = calcX(s, R, p); | > * X = max(min(X_calc, 2 * X_recv), s / t_mbi); | > @@ -125,12 +125,17 @@ static inline void ccid3_update_send_tim | > * If (now - tld >= R) | > * X = max(min(2 * X, 2 * X_recv), s / R); | > * tld = now; | > + * | > + * If X has changed, we also update the scheduled send time t_now, | > + * the inter-packet interval t_ipi, and the delta value. | > */ | > static void ccid3_hc_tx_update_x(struct sock *sk) | > { | > struct ccid3_hc_tx_sock *hctx = ccid3_hc_tx_sk(sk); | > const __u32 old_x = hctx->ccid3hctx_x; | > | > + DCCP_BUG_ON(hctx->ccid3hctx_rtt = 0); | > + | > /* To avoid large error in calcX */ | > if (hctx->ccid3hctx_p >= TFRC_SMALLEST_P) { | > hctx->ccid3hctx_x_calc = tfrc_calc_x(hctx->ccid3hctx_s, | > @@ -421,7 +426,6 @@ static void ccid3_hc_tx_packet_recv(stru | > unsigned long next_tmout; | > u32 t_elapsed; | > u32 pinv; | > - u32 x_recv; | > u32 r_sample; | > | > BUG_ON(hctx = NULL); | > @@ -434,15 +438,11 @@ static void ccid3_hc_tx_packet_recv(stru | > opt_recv = &hctx->ccid3hctx_options_received; | > | > t_elapsed = dp->dccps_options_received.dccpor_elapsed_time * 10; | > - x_recv = opt_recv->ccid3or_receive_rate; | > pinv = opt_recv->ccid3or_loss_event_rate; | > | > switch (hctx->ccid3hctx_state) { | > case TFRC_SSTATE_NO_FBACK: | > case TFRC_SSTATE_FBACK: | > - /* Calculate new round trip sample by | > - * R_sample = (now - t_recvdata) - t_delay */ | > - /* get t_recvdata from history */ | > packet = dccp_tx_hist_find_entry(&hctx->ccid3hctx_hist, | > DCCP_SKB_CB(skb)->dccpd_ack_seq); | > if (unlikely(packet = NULL)) { | > @@ -453,7 +453,25 @@ static void ccid3_hc_tx_packet_recv(stru | > return; | > } | > | > - /* Update RTT */ | > + /* Update receive rate */ | > + hctx->ccid3hctx_x_recv = opt_recv->ccid3or_receive_rate; | > + | > + /* Update loss event rate */ | > + if (pinv = ~0 || pinv = 0) | > + hctx->ccid3hctx_p = 0; | > + else { | > + hctx->ccid3hctx_p = 1000000 / pinv; | > + | > + if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) { | > + hctx->ccid3hctx_p = TFRC_SMALLEST_P; | > + ccid3_pr_debug("%s, sk=%p, Smallest p used!\n", | > + dccp_role(sk), sk); | > + } | > + } | > + | > + /* Calculate new round trip sample by | > + * R_sample = (t_now - t_recvdata) - t_delay | > + * t_recvdata is taken from packet history */ | > dccp_timestamp(sk, &now); | > r_sample = timeval_delta(&now, &packet->dccphtx_tstamp); | > if (unlikely(r_sample <= t_elapsed)) | > @@ -462,7 +480,7 @@ static void ccid3_hc_tx_packet_recv(stru | > else | > r_sample -= t_elapsed; | > | > - /* Update RTT estimate by | > + /* Update RTT estimate by (step (2) of [RFC 3448, 4.3]): | > * If (No feedback recv) | > * R = R_sample; | > * Else | > @@ -481,21 +499,10 @@ static void ccid3_hc_tx_packet_recv(stru | > "r_sample=%us\n", dccp_role(sk), sk, | > hctx->ccid3hctx_rtt, r_sample); | > | > - /* Update receive rate */ | > - hctx->ccid3hctx_x_recv = x_recv;/* X_recv in bytes per sec */ | > - | > - /* Update loss event rate */ | > - if (pinv = ~0 || pinv = 0) | > - hctx->ccid3hctx_p = 0; | > - else { | > - hctx->ccid3hctx_p = 1000000 / pinv; | > - | > - if (hctx->ccid3hctx_p < TFRC_SMALLEST_P) { | > - hctx->ccid3hctx_p = TFRC_SMALLEST_P; | > - ccid3_pr_debug("%s, sk=%p, Smallest p used!\n", | > - dccp_role(sk), sk); | > - } | > - } | > + /* Avoid `division-by zero' errors. | > + * FIXME: We need a smarter and more robust way to | > + * protect against this possibility */ | > + DCCP_BUG_ON(hctx->ccid3hctx_rtt = 0); | > | > /* unschedule no feedback timer */ | > sk_stop_timer(sk, &hctx->ccid3hctx_no_feedback_timer); | > --- a/net/dccp/ccids/lib/tfrc_equation.c | > +++ b/net/dccp/ccids/lib/tfrc_equation.c | > @@ -578,6 +578,8 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p) | > u32 f; | > u64 tmp1, tmp2; | > | > + DCCP_BUG_ON(R = 0); /* RTT can't be zero or else divide by zero */ | > + | > if (p < TFRC_CALC_X_SPLIT) | > index = (p / (TFRC_CALC_X_SPLIT / TFRC_CALC_X_ARRSIZE)) - 1; | > else | > @@ -587,11 +589,6 @@ u32 tfrc_calc_x(u16 s, u32 R, u32 p) | > /* p should be 0 unless there is a bug in my code */ | > index = 0; | > | > - if (R = 0) { | > - DCCP_WARN("RTT=0, setting to 1\n"); | > - R = 1; /* RTT can't be zero or else divide by zero */ | > - } | > - | > BUG_ON(index >= TFRC_CALC_X_ARRSIZE); | > | > if (p >= TFRC_CALC_X_SPLIT) | > --- a/net/dccp/ccids/ccid3.h | > +++ b/net/dccp/ccids/ccid3.h | > @@ -78,7 +78,7 @@ enum ccid3_hc_tx_states { | > /** struct ccid3_hc_tx_sock - CCID3 sender half-connection socket | > * | > * @ccid3hctx_x - Current sending rate | > - * @ccid3hctx_x_recv - Receive rate | > + * @ccid3hctx_x_recv - Receive rate in bytes per second | > * @ccid3hctx_x_calc - Calculated send rate (RFC 3448, 3.1) | > * @ccid3hctx_rtt - Estimate of current round trip time in usecs | > * @ccid3hctx_p - Current loss event rate (0-1) scaled by 1000000 | > - | > To unsubscribe from this list: send the line "unsubscribe dccp" in | > the body of a message to [email protected] | > More majordomo info at http://vger.kernel.org/majordomo-info.html | |