dn_timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * DECnet An implementation of the DECnet protocol suite for the LINUX
  4. * operating system. DECnet is implemented using the BSD Socket
  5. * interface as the means of communication with the user level.
  6. *
  7. * DECnet Socket Timer Functions
  8. *
  9. * Author: Steve Whitehouse <SteveW@ACM.org>
  10. *
  11. *
  12. * Changes:
  13. * Steve Whitehouse : Made keepalive timer part of the same
  14. * timer idea.
  15. * Steve Whitehouse : Added checks for sk->sock_readers
  16. * David S. Miller : New socket locking
  17. * Steve Whitehouse : Timer grabs socket ref.
  18. */
  19. #include <linux/net.h>
  20. #include <linux/socket.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/timer.h>
  24. #include <linux/spinlock.h>
  25. #include <net/sock.h>
  26. #include <linux/atomic.h>
  27. #include <linux/jiffies.h>
  28. #include <net/flow.h>
  29. #include <net/dn.h>
  30. /*
  31. * Slow timer is for everything else (n * 500mS)
  32. */
  33. #define SLOW_INTERVAL (HZ/2)
  34. static void dn_slow_timer(struct timer_list *t);
  35. void dn_start_slow_timer(struct sock *sk)
  36. {
  37. timer_setup(&sk->sk_timer, dn_slow_timer, 0);
  38. sk_reset_timer(sk, &sk->sk_timer, jiffies + SLOW_INTERVAL);
  39. }
  40. void dn_stop_slow_timer(struct sock *sk)
  41. {
  42. sk_stop_timer(sk, &sk->sk_timer);
  43. }
  44. static void dn_slow_timer(struct timer_list *t)
  45. {
  46. struct sock *sk = from_timer(sk, t, sk_timer);
  47. struct dn_scp *scp = DN_SK(sk);
  48. bh_lock_sock(sk);
  49. if (sock_owned_by_user(sk)) {
  50. sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 10);
  51. goto out;
  52. }
  53. /*
  54. * The persist timer is the standard slow timer used for retransmits
  55. * in both connection establishment and disconnection as well as
  56. * in the RUN state. The different states are catered for by changing
  57. * the function pointer in the socket. Setting the timer to a value
  58. * of zero turns it off. We allow the persist_fxn to turn the
  59. * timer off in a permant way by returning non-zero, so that
  60. * timer based routines may remove sockets. This is why we have a
  61. * sock_hold()/sock_put() around the timer to prevent the socket
  62. * going away in the middle.
  63. */
  64. if (scp->persist && scp->persist_fxn) {
  65. if (scp->persist <= SLOW_INTERVAL) {
  66. scp->persist = 0;
  67. if (scp->persist_fxn(sk))
  68. goto out;
  69. } else {
  70. scp->persist -= SLOW_INTERVAL;
  71. }
  72. }
  73. /*
  74. * Check for keepalive timeout. After the other timer 'cos if
  75. * the previous timer caused a retransmit, we don't need to
  76. * do this. scp->stamp is the last time that we sent a packet.
  77. * The keepalive function sends a link service packet to the
  78. * other end. If it remains unacknowledged, the standard
  79. * socket timers will eventually shut the socket down. Each
  80. * time we do this, scp->stamp will be updated, thus
  81. * we won't try and send another until scp->keepalive has passed
  82. * since the last successful transmission.
  83. */
  84. if (scp->keepalive && scp->keepalive_fxn && (scp->state == DN_RUN)) {
  85. if (time_after_eq(jiffies, scp->stamp + scp->keepalive))
  86. scp->keepalive_fxn(sk);
  87. }
  88. sk_reset_timer(sk, &sk->sk_timer, jiffies + SLOW_INTERVAL);
  89. out:
  90. bh_unlock_sock(sk);
  91. sock_put(sk);
  92. }