[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 40 #include "../packet_history.h" 41 41 #include "ccid3.h" 42 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 + 43 52 #ifdef CCID3_DEBUG 44 53 extern int ccid3_debug; 45 54 ··· 757 748 /* Calculate new t_ipi (inter packet interval) by t_ipi = s / X_inst */ 758 749 static inline void ccid3_calc_new_t_ipi(struct ccid3_hc_tx_sock *hctx) 759 750 { 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 - 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); 774 758 } 775 759 776 760 /* Calculate new delta by delta = min(t_ipi / 2, t_gran / 2) */ ··· 771 769 { 772 770 hctx->ccid3hctx_delta = min_t(u32, hctx->ccid3hctx_t_ipi / 2, 773 771 TFRC_OPSYS_HALF_TIME_GRAN); 774 - 775 772 } 776 773 777 774 /* ··· 803 802 do_gettimeofday(&now); 804 803 if (timeval_delta(&now, &hctx->ccid3hctx_t_ld) >= 805 804 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 */ 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)); 814 809 hctx->ccid3hctx_t_ld = now; 815 810 } 816 - } 817 - 818 - if (hctx->ccid3hctx_x == 0) { 819 - ccid3_pr_debug("ccid3hctx_x = 0!\n"); 820 - hctx->ccid3hctx_x = 1; 821 811 } 822 812 } 823 813 ··· 818 826 struct dccp_sock *dp = dccp_sk(sk); 819 827 unsigned long next_tmout = 0; 820 828 struct ccid3_hc_tx_sock *hctx = dp->dccps_hc_tx_ccid_private; 821 - u32 rtt; 822 829 823 830 bh_lock_sock(sk); 824 831 if (sock_owned_by_user(sk)) { ··· 831 840 ccid3_pr_debug("%s, sk=%p, state=%s\n", dccp_role(sk), sk, 832 841 ccid3_tx_state_name(hctx->ccid3hctx_state)); 833 842 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 843 switch (hctx->ccid3hctx_state) { 840 844 case TFRC_SSTATE_TERM: 841 845 goto out; 842 846 case TFRC_SSTATE_NO_FBACK: 843 847 /* Halve send rate */ 844 848 hctx->ccid3hctx_x /= 2; 845 - if (hctx->ccid3hctx_x < 846 - (hctx->ccid3hctx_s / TFRC_MAX_BACK_OFF_TIME)) 849 + if (hctx->ccid3hctx_x < (hctx->ccid3hctx_s / 850 + TFRC_MAX_BACK_OFF_TIME)) 847 851 hctx->ccid3hctx_x = (hctx->ccid3hctx_s / 848 852 TFRC_MAX_BACK_OFF_TIME); 849 853 ··· 847 861 dccp_role(sk), sk, 848 862 ccid3_tx_state_name(hctx->ccid3hctx_state), 849 863 hctx->ccid3hctx_x); 850 - next_tmout = max_t(u32, 2 * (hctx->ccid3hctx_s * 100000) / (hctx->ccid3hctx_x / 10), 864 + next_tmout = max_t(u32, 2 * usecs_div(hctx->ccid3hctx_s, 865 + hctx->ccid3hctx_x), 851 866 TFRC_INITIAL_TIMEOUT); 852 - /* do above maths with 100000 and 10 to prevent overflow on 32 bit */ 853 867 /* 854 868 * FIXME - not sure above calculation is correct. See section 855 869 * 5 of CCID3 11 should adjust tx_t_ipi and double that to ··· 861 875 * Check if IDLE since last timeout and recv rate is less than 862 876 * 4 packets per RTT 863 877 */ 864 - rtt = hctx->ccid3hctx_rtt; 865 - if (rtt < 10) 866 - rtt = 10; 867 - /* stop divide by zero below */ 868 878 if (!hctx->ccid3hctx_idle || 869 - (hctx->ccid3hctx_x_recv >= 4 * (hctx->ccid3hctx_s * 100000) / (rtt / 10))) { 879 + (hctx->ccid3hctx_x_recv >= 880 + 4 * usecs_div(hctx->ccid3hctx_s, hctx->ccid3hctx_rtt))) { 870 881 ccid3_pr_debug("%s, sk=%p, state=%s, not idle\n", 871 882 dccp_role(sk), sk, 872 883 ccid3_tx_state_name(hctx->ccid3hctx_state)); ··· 888 905 /* Update sending rate */ 889 906 ccid3_hc_tx_update_x(sk); 890 907 } 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) */ 908 + /* 909 + * Schedule no feedback timer to expire in 910 + * max(4 * R, 2 * s / X) 911 + */ 896 912 next_tmout = max_t(u32, hctx->ccid3hctx_t_rto, 897 - 2 * (hctx->ccid3hctx_s * 100000) / (hctx->ccid3hctx_x / 10)); 913 + 2 * usecs_div(hctx->ccid3hctx_s, 914 + hctx->ccid3hctx_x)); 898 915 break; 899 916 default: 900 917 printk(KERN_CRIT "%s: %s, sk=%p, Illegal state (%d)!\n", ··· 1036 1053 * Algorithm in "8.1. Window Counter Valuer" in 1037 1054 * draft-ietf-dccp-ccid3-11.txt 1038 1055 */ 1039 - quarter_rtt = timeval_delta(&now, &hctx->ccid3hctx_t_last_win_count) / 1040 - (hctx->ccid3hctx_rtt / 4); 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 + 1041 1060 if (quarter_rtt > 0) { 1042 1061 hctx->ccid3hctx_t_last_win_count = now; 1043 1062 hctx->ccid3hctx_last_win_count = (hctx->ccid3hctx_last_win_count + ··· 1156 1171 hctx->ccid3hctx_rtt = (hctx->ccid3hctx_rtt * 9) / 10 + 1157 1172 r_sample / 10; 1158 1173 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 1174 ccid3_pr_debug("%s, sk=%p, New RTT estimate=%uus, " 1167 1175 "r_sample=%us\n", dccp_role(sk), sk, 1168 1176 hctx->ccid3hctx_rtt, r_sample); ··· 1198 1220 dccp_tx_hist_purge_older(ccid3_tx_hist, 1199 1221 &hctx->ccid3hctx_hist, packet); 1200 1222 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 1223 /* 1208 1224 * Schedule no feedback timer to expire in 1209 1225 * max(4 * R, 2 * s / X) 1210 1226 */ 1211 1227 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 - 1228 + 2 * usecs_div(hctx->ccid3hctx_s, 1229 + hctx->ccid3hctx_x)); 1230 + 1216 1231 ccid3_pr_debug("%s, sk=%p, Scheduled no feedback timer to " 1217 1232 "expire in %lu jiffies (%luus)\n", 1218 1233 dccp_role(sk), sk, ··· 1321 1350 1322 1351 /* Set transmission rate to 1 packet per second */ 1323 1352 hctx->ccid3hctx_x = hctx->ccid3hctx_s; 1324 - /* See ccid3_hc_tx_packet_sent win_count calculatation */ 1325 - hctx->ccid3hctx_rtt = 4; 1326 1353 hctx->ccid3hctx_t_rto = USEC_PER_SEC; 1327 1354 hctx->ccid3hctx_state = TFRC_SSTATE_NO_SENT; 1328 1355 INIT_LIST_HEAD(&hctx->ccid3hctx_hist);