[CCID3]: Introduce usecs_div

To avoid open coding this all over the place.

Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Arnaldo Carvalho de Melo and committed by
David S. Miller
6b5e633a b6ee3d4a

+39 -70
+39 -70
net/dccp/ccids/ccid3.c
··· 40 #include "../packet_history.h" 41 #include "ccid3.h" 42 43 #ifdef CCID3_DEBUG 44 extern int ccid3_debug; 45 ··· 757 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */ 758 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx) 759 { 760 - if (hctx->ccid3hctx_state == TFRC_SSTATE_NO_FBACK) 761 - return; 762 - /* if no feedback spec says t_ipi is 1 second (set elsewhere and then 763 - * doubles after every no feedback timer (separate function) */ 764 - 765 - if (hctx->ccid3hctx_x < 10) { 766 - ccid3_pr_debug("ccid3_calc_new_t_ipi - ccid3hctx_x < 10\n"); 767 - hctx->ccid3hctx_x = 10; 768 - } 769 - hctx->ccid3hctx_t_ipi = (hctx->ccid3hctx_s * 100000) 770 - / (hctx->ccid3hctx_x / 10); 771 - /* reason for above maths with 10 in there is to avoid 32 bit 772 - * overflow for jumbo packets */ 773 - 774 } 775 776 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */ ··· 771 { 772 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2, 773 TFRC_OPSYS_HALF_TIME_GRAN); 774 - 775 } 776 777 /* ··· 803 do_gettimeofday(&now); 804 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >= 805 hctx->ccid3hctx_rtt) { 806 - /* Avoid divide by zero below */ 807 - const u32 rtt = max_t(u32, hctx->ccid3hctx_rtt, 10); 808 - 809 - hctx->ccid3hctx_x = max_t(u32, min_t(u32, 2 * hctx->ccid3hctx_x_recv, 810 - 2 * hctx->ccid3hctx_x), 811 - ((hctx->ccid3hctx_s * 100000) / 812 - (rtt / 10))); 813 - /* Using 100000 and 10 to avoid 32 bit overflow for jumbo frames */ 814 hctx->ccid3hctx_t_ld = now; 815 } 816 - } 817 - 818 - if (hctx->ccid3hctx_x == 0) { 819 - ccid3_pr_debug("ccid3hctx_x = 0!\n"); 820 - hctx->ccid3hctx_x = 1; 821 } 822 } 823 ··· 818 struct dccp_sock *dp = dccp_sk(sk); 819 unsigned long next_tmout = 0; 820 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private; 821 - u32 rtt; 822 823 bh_lock_sock(sk); 824 if (sock_owned_by_user(sk)) { ··· 831 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk, 832 ccid3_tx_state_name(hctx->ccid3hctx_state)); 833 834 - if (hctx->ccid3hctx_x < 10) { 835 - ccid3_pr_debug("TFRC_SSTATE_NO_FBACK ccid3hctx_x < 10\n"); 836 - hctx->ccid3hctx_x = 10; 837 - } 838 - 839 switch (hctx->ccid3hctx_state) { 840 case TFRC_SSTATE_TERM: 841 goto out; 842 case TFRC_SSTATE_NO_FBACK: 843 /* Halve send rate */ 844 hctx->ccid3hctx_x /= 2; 845 - if (hctx->ccid3hctx_x < 846 - (hctx->ccid3hctx_s / TFRC_MAX_BACK_OFF_TIME)) 847 hctx->ccid3hctx_x = (hctx->ccid3hctx_s / 848 TFRC_MAX_BACK_OFF_TIME); 849 ··· 847 dccp_role(sk), sk, 848 ccid3_tx_state_name(hctx->ccid3hctx_state), 849 hctx->ccid3hctx_x); 850 - next_tmout = max_t(u32, 2 * (hctx->ccid3hctx_s * 100000) / (hctx->ccid3hctx_x / 10), 851 TFRC_INITIAL_TIMEOUT); 852 - /* do above maths with 100000 and 10 to prevent overflow on 32 bit */ 853 /* 854 * FIXME - not sure above calculation is correct. See section 855 * 5 of CCID3 11 should adjust tx_t_ipi and double that to ··· 861 * Check if IDLE since last timeout and recv rate is less than 862 * 4 packets per RTT 863 */ 864 - rtt = hctx->ccid3hctx_rtt; 865 - if (rtt < 10) 866 - rtt = 10; 867 - /* stop divide by zero below */ 868 if (!hctx->ccid3hctx_idle || 869 - (hctx->ccid3hctx_x_recv >= 4 * (hctx->ccid3hctx_s * 100000) / (rtt / 10))) { 870 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n", 871 dccp_role(sk), sk, 872 ccid3_tx_state_name(hctx->ccid3hctx_state)); ··· 888 /* Update sending rate */ 889 ccid3_hc_tx_update_x(sk); 890 } 891 - if (hctx->ccid3hctx_x == 0) { 892 - ccid3_pr_debug("TFRC_SSTATE_FBACK ccid3hctx_x = 0!\n"); 893 - hctx->ccid3hctx_x = 10; 894 - } 895 - /* Schedule no feedback timer to expire in max(4 * R, 2 * s / X) */ 896 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto, 897 - 2 * (hctx->ccid3hctx_s * 100000) / (hctx->ccid3hctx_x / 10)); 898 break; 899 default: 900 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n", ··· 1036 * Algorithm in "8.1. Window Counter Valuer" in 1037 * draft-ietf-dccp-ccid3-11.txt 1038 */ 1039 - quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count) / 1040 - (hctx->ccid3hctx_rtt / 4); 1041 if (quarter_rtt > 0) { 1042 hctx->ccid3hctx_t_last_win_count = now; 1043 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count + ··· 1156 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 + 1157 r_sample / 10; 1158 1159 - /* 1160 - * XXX: this is to avoid a division by zero in ccid3_hc_tx_packet_sent 1161 - * implemention of the new window count. 1162 - */ 1163 - if (hctx->ccid3hctx_rtt < 4) 1164 - hctx->ccid3hctx_rtt = 4; 1165 - 1166 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, " 1167 "r_sample=%us\n", dccp_role(sk), sk, 1168 hctx->ccid3hctx_rtt, r_sample); ··· 1198 dccp_tx_hist_purge_older(ccid3_tx_hist, 1199 &hctx->ccid3hctx_hist, packet); 1200 1201 - if (hctx->ccid3hctx_x < 10) { 1202 - ccid3_pr_debug("ccid3_hc_tx_packet_recv hctx_x < 10\n"); 1203 - hctx->ccid3hctx_x = 10; 1204 - } 1205 - /* to prevent divide by zero below */ 1206 - 1207 /* 1208 * Schedule no feedback timer to expire in 1209 * max(4 * R, 2 * s / X) 1210 */ 1211 next_tmout = max(hctx->ccid3hctx_t_rto, 1212 - (2 * (hctx->ccid3hctx_s * 100000) / 1213 - (hctx->ccid3hctx_x / 10))); 1214 - /* maths with 100000 and 10 is to prevent overflow with 32 bit */ 1215 - 1216 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to " 1217 "expire in %lu jiffies (%luus)\n", 1218 dccp_role(sk), sk, ··· 1321 1322 /* Set transmission rate to 1 packet per second */ 1323 hctx->ccid3hctx_x = hctx->ccid3hctx_s; 1324 - /* See ccid3_hc_tx_packet_sent win_count calculatation */ 1325 - hctx->ccid3hctx_rtt = 4; 1326 hctx->ccid3hctx_t_rto = USEC_PER_SEC; 1327 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT; 1328 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);
··· 40 #include "../packet_history.h" 41 #include "ccid3.h" 42 43 + /* 44 + * Reason for maths with 10 here is to avoid 32 bit overflow when a is big. 45 + */ 46 + static inline u32 usecs_div(const u32 a, const u32 b) 47 + { 48 + const u32 tmp = a * (USEC_PER_SEC / 10); 49 + return b > 20 ? tmp / (b / 10) : tmp; 50 + } 51 + 52 #ifdef CCID3_DEBUG 53 extern int ccid3_debug; 54 ··· 748 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */ 749 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx) 750 { 751 + /* 752 + * If no feedback spec says t_ipi is 1 second (set elsewhere and then 753 + * doubles after every no feedback timer (separate function) 754 + */ 755 + if (hctx->ccid3hctx_state != TFRC_SSTATE_NO_FBACK) 756 + hctx->ccid3hctx_t_ipi = usecs_div(hctx->ccid3hctx_s, 757 + hctx->ccid3hctx_x); 758 } 759 760 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */ ··· 769 { 770 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2, 771 TFRC_OPSYS_HALF_TIME_GRAN); 772 } 773 774 /* ··· 802 do_gettimeofday(&now); 803 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >= 804 hctx->ccid3hctx_rtt) { 805 + hctx->ccid3hctx_x = max_t(u32, min_t(u32, hctx->ccid3hctx_x_recv, 806 + hctx->ccid3hctx_x) * 2, 807 + usecs_div(hctx->ccid3hctx_s, 808 + hctx->ccid3hctx_rtt)); 809 hctx->ccid3hctx_t_ld = now; 810 } 811 } 812 } 813 ··· 826 struct dccp_sock *dp = dccp_sk(sk); 827 unsigned long next_tmout = 0; 828 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private; 829 830 bh_lock_sock(sk); 831 if (sock_owned_by_user(sk)) { ··· 840 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk, 841 ccid3_tx_state_name(hctx->ccid3hctx_state)); 842 843 switch (hctx->ccid3hctx_state) { 844 case TFRC_SSTATE_TERM: 845 goto out; 846 case TFRC_SSTATE_NO_FBACK: 847 /* Halve send rate */ 848 hctx->ccid3hctx_x /= 2; 849 + if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s / 850 + TFRC_MAX_BACK_OFF_TIME)) 851 hctx->ccid3hctx_x = (hctx->ccid3hctx_s / 852 TFRC_MAX_BACK_OFF_TIME); 853 ··· 861 dccp_role(sk), sk, 862 ccid3_tx_state_name(hctx->ccid3hctx_state), 863 hctx->ccid3hctx_x); 864 + next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s, 865 + hctx->ccid3hctx_x), 866 TFRC_INITIAL_TIMEOUT); 867 /* 868 * FIXME - not sure above calculation is correct. See section 869 * 5 of CCID3 11 should adjust tx_t_ipi and double that to ··· 875 * Check if IDLE since last timeout and recv rate is less than 876 * 4 packets per RTT 877 */ 878 if (!hctx->ccid3hctx_idle || 879 + (hctx->ccid3hctx_x_recv >= 880 + 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) { 881 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n", 882 dccp_role(sk), sk, 883 ccid3_tx_state_name(hctx->ccid3hctx_state)); ··· 905 /* Update sending rate */ 906 ccid3_hc_tx_update_x(sk); 907 } 908 + /* 909 + * Schedule no feedback timer to expire in 910 + * max(4 * R, 2 * s / X) 911 + */ 912 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto, 913 + 2 * usecs_div(hctx->ccid3hctx_s, 914 + hctx->ccid3hctx_x)); 915 break; 916 default: 917 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n", ··· 1053 * Algorithm in "8.1. Window Counter Valuer" in 1054 * draft-ietf-dccp-ccid3-11.txt 1055 */ 1056 + quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count); 1057 + if (likely(hctx->ccid3hctx_rtt > 8)) 1058 + quarter_rtt /= hctx->ccid3hctx_rtt / 4; 1059 + 1060 if (quarter_rtt > 0) { 1061 hctx->ccid3hctx_t_last_win_count = now; 1062 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count + ··· 1171 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 + 1172 r_sample / 10; 1173 1174 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, " 1175 "r_sample=%us\n", dccp_role(sk), sk, 1176 hctx->ccid3hctx_rtt, r_sample); ··· 1220 dccp_tx_hist_purge_older(ccid3_tx_hist, 1221 &hctx->ccid3hctx_hist, packet); 1222 1223 /* 1224 * Schedule no feedback timer to expire in 1225 * max(4 * R, 2 * s / X) 1226 */ 1227 next_tmout = max(hctx->ccid3hctx_t_rto, 1228 + 2 * usecs_div(hctx->ccid3hctx_s, 1229 + hctx->ccid3hctx_x)); 1230 + 1231 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to " 1232 "expire in %lu jiffies (%luus)\n", 1233 dccp_role(sk), sk, ··· 1350 1351 /* Set transmission rate to 1 packet per second */ 1352 hctx->ccid3hctx_x = hctx->ccid3hctx_s; 1353 hctx->ccid3hctx_t_rto = USEC_PER_SEC; 1354 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT; 1355 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);