timer.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * net/dccp/timer.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/dccp.h>
  13. #include <linux/skbuff.h>
  14. #include "dccp.h"
  15. /* sysctl variables governing numbers of retransmission attempts */
  16. int sysctl_dccp_request_retries __read_mostly = TCP_SYN_RETRIES;
  17. int sysctl_dccp_retries1 __read_mostly = TCP_RETR1;
  18. int sysctl_dccp_retries2 __read_mostly = TCP_RETR2;
  19. static void dccp_write_err(struct sock *sk)
  20. {
  21. sk->sk_err = sk->sk_err_soft ? : ETIMEDOUT;
  22. sk->sk_error_report(sk);
  23. dccp_send_reset(sk, DCCP_RESET_CODE_ABORTED);
  24. dccp_done(sk);
  25. DCCP_INC_STATS_BH(DCCP_MIB_ABORTONTIMEOUT);
  26. }
  27. /* A write timeout has occurred. Process the after effects. */
  28. static int dccp_write_timeout(struct sock *sk)
  29. {
  30. const struct inet_connection_sock *icsk = inet_csk(sk);
  31. int retry_until;
  32. if (sk->sk_state == DCCP_REQUESTING || sk->sk_state == DCCP_PARTOPEN) {
  33. if (icsk->icsk_retransmits != 0)
  34. dst_negative_advice(sk);
  35. retry_until = icsk->icsk_syn_retries ?
  36. : sysctl_dccp_request_retries;
  37. } else {
  38. if (icsk->icsk_retransmits >= sysctl_dccp_retries1) {
  39. /* NOTE. draft-ietf-tcpimpl-pmtud-01.txt requires pmtu
  40. black hole detection. :-(
  41. It is place to make it. It is not made. I do not want
  42. to make it. It is disguisting. It does not work in any
  43. case. Let me to cite the same draft, which requires for
  44. us to implement this:
  45. "The one security concern raised by this memo is that ICMP black holes
  46. are often caused by over-zealous security administrators who block
  47. all ICMP messages. It is vitally important that those who design and
  48. deploy security systems understand the impact of strict filtering on
  49. upper-layer protocols. The safest web site in the world is worthless
  50. if most TCP implementations cannot transfer data from it. It would
  51. be far nicer to have all of the black holes fixed rather than fixing
  52. all of the TCP implementations."
  53. Golden words :-).
  54. */
  55. dst_negative_advice(sk);
  56. }
  57. retry_until = sysctl_dccp_retries2;
  58. /*
  59. * FIXME: see tcp_write_timout and tcp_out_of_resources
  60. */
  61. }
  62. if (icsk->icsk_retransmits >= retry_until) {
  63. /* Has it gone just too far? */
  64. dccp_write_err(sk);
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. /*
  70. * The DCCP retransmit timer.
  71. */
  72. static void dccp_retransmit_timer(struct sock *sk)
  73. {
  74. struct inet_connection_sock *icsk = inet_csk(sk);
  75. /*
  76. * More than than 4MSL (8 minutes) has passed, a RESET(aborted) was
  77. * sent, no need to retransmit, this sock is dead.
  78. */
  79. if (dccp_write_timeout(sk))
  80. return;
  81. /*
  82. * We want to know the number of packets retransmitted, not the
  83. * total number of retransmissions of clones of original packets.
  84. */
  85. if (icsk->icsk_retransmits == 0)
  86. DCCP_INC_STATS_BH(DCCP_MIB_TIMEOUTS);
  87. if (dccp_retransmit_skb(sk) != 0) {
  88. /*
  89. * Retransmission failed because of local congestion,
  90. * do not backoff.
  91. */
  92. if (--icsk->icsk_retransmits == 0)
  93. icsk->icsk_retransmits = 1;
  94. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  95. min(icsk->icsk_rto,
  96. TCP_RESOURCE_PROBE_INTERVAL),
  97. DCCP_RTO_MAX);
  98. return;
  99. }
  100. icsk->icsk_backoff++;
  101. icsk->icsk_rto = min(icsk->icsk_rto << 1, DCCP_RTO_MAX);
  102. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto,
  103. DCCP_RTO_MAX);
  104. if (icsk->icsk_retransmits > sysctl_dccp_retries1)
  105. __sk_dst_reset(sk);
  106. }
  107. static void dccp_write_timer(unsigned long data)
  108. {
  109. struct sock *sk = (struct sock *)data;
  110. struct inet_connection_sock *icsk = inet_csk(sk);
  111. int event = 0;
  112. bh_lock_sock(sk);
  113. if (sock_owned_by_user(sk)) {
  114. /* Try again later */
  115. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  116. jiffies + (HZ / 20));
  117. goto out;
  118. }
  119. if (sk->sk_state == DCCP_CLOSED || !icsk->icsk_pending)
  120. goto out;
  121. if (time_after(icsk->icsk_timeout, jiffies)) {
  122. sk_reset_timer(sk, &icsk->icsk_retransmit_timer,
  123. icsk->icsk_timeout);
  124. goto out;
  125. }
  126. event = icsk->icsk_pending;
  127. icsk->icsk_pending = 0;
  128. switch (event) {
  129. case ICSK_TIME_RETRANS:
  130. dccp_retransmit_timer(sk);
  131. break;
  132. }
  133. out:
  134. bh_unlock_sock(sk);
  135. sock_put(sk);
  136. }
  137. /*
  138. * Timer for listening sockets
  139. */
  140. static void dccp_response_timer(struct sock *sk)
  141. {
  142. inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT,
  143. DCCP_RTO_MAX);
  144. }
  145. static void dccp_keepalive_timer(unsigned long data)
  146. {
  147. struct sock *sk = (struct sock *)data;
  148. /* Only process if socket is not in use. */
  149. bh_lock_sock(sk);
  150. if (sock_owned_by_user(sk)) {
  151. /* Try again later. */
  152. inet_csk_reset_keepalive_timer(sk, HZ / 20);
  153. goto out;
  154. }
  155. if (sk->sk_state == DCCP_LISTEN) {
  156. dccp_response_timer(sk);
  157. goto out;
  158. }
  159. out:
  160. bh_unlock_sock(sk);
  161. sock_put(sk);
  162. }
  163. /* This is the same as tcp_delack_timer, sans prequeue & mem_reclaim stuff */
  164. static void dccp_delack_timer(unsigned long data)
  165. {
  166. struct sock *sk = (struct sock *)data;
  167. struct inet_connection_sock *icsk = inet_csk(sk);
  168. bh_lock_sock(sk);
  169. if (sock_owned_by_user(sk)) {
  170. /* Try again later. */
  171. icsk->icsk_ack.blocked = 1;
  172. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOCKED);
  173. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  174. jiffies + TCP_DELACK_MIN);
  175. goto out;
  176. }
  177. if (sk->sk_state == DCCP_CLOSED ||
  178. !(icsk->icsk_ack.pending & ICSK_ACK_TIMER))
  179. goto out;
  180. if (time_after(icsk->icsk_ack.timeout, jiffies)) {
  181. sk_reset_timer(sk, &icsk->icsk_delack_timer,
  182. icsk->icsk_ack.timeout);
  183. goto out;
  184. }
  185. icsk->icsk_ack.pending &= ~ICSK_ACK_TIMER;
  186. if (inet_csk_ack_scheduled(sk)) {
  187. if (!icsk->icsk_ack.pingpong) {
  188. /* Delayed ACK missed: inflate ATO. */
  189. icsk->icsk_ack.ato = min(icsk->icsk_ack.ato << 1,
  190. icsk->icsk_rto);
  191. } else {
  192. /* Delayed ACK missed: leave pingpong mode and
  193. * deflate ATO.
  194. */
  195. icsk->icsk_ack.pingpong = 0;
  196. icsk->icsk_ack.ato = TCP_ATO_MIN;
  197. }
  198. dccp_send_ack(sk);
  199. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKS);
  200. }
  201. out:
  202. bh_unlock_sock(sk);
  203. sock_put(sk);
  204. }
  205. /**
  206. * dccp_write_xmitlet - Workhorse for CCID packet dequeueing interface
  207. * See the comments above %ccid_dequeueing_decision for supported modes.
  208. */
  209. static void dccp_write_xmitlet(unsigned long data)
  210. {
  211. struct sock *sk = (struct sock *)data;
  212. bh_lock_sock(sk);
  213. if (sock_owned_by_user(sk))
  214. sk_reset_timer(sk, &dccp_sk(sk)->dccps_xmit_timer, jiffies + 1);
  215. else
  216. dccp_write_xmit(sk);
  217. bh_unlock_sock(sk);
  218. }
  219. static void dccp_write_xmit_timer(unsigned long data)
  220. {
  221. dccp_write_xmitlet(data);
  222. sock_put((struct sock *)data);
  223. }
  224. void dccp_init_xmit_timers(struct sock *sk)
  225. {
  226. struct dccp_sock *dp = dccp_sk(sk);
  227. tasklet_init(&dp->dccps_xmitlet, dccp_write_xmitlet, (unsigned long)sk);
  228. setup_timer(&dp->dccps_xmit_timer, dccp_write_xmit_timer,
  229. (unsigned long)sk);
  230. inet_csk_init_xmit_timers(sk, &dccp_write_timer, &dccp_delack_timer,
  231. &dccp_keepalive_timer);
  232. }
  233. static ktime_t dccp_timestamp_seed;
  234. /**
  235. * dccp_timestamp - 10s of microseconds time source
  236. * Returns the number of 10s of microseconds since loading DCCP. This is native
  237. * DCCP time difference format (RFC 4340, sec. 13).
  238. * Please note: This will wrap around about circa every 11.9 hours.
  239. */
  240. u32 dccp_timestamp(void)
  241. {
  242. s64 delta = ktime_us_delta(ktime_get_real(), dccp_timestamp_seed);
  243. do_div(delta, 10);
  244. return delta;
  245. }
  246. EXPORT_SYMBOL_GPL(dccp_timestamp);
  247. void __init dccp_timestamping_init(void)
  248. {
  249. dccp_timestamp_seed = ktime_get_real();
  250. }