ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / decnet / dn_timer.c
1 /*
2  * DECnet       An implementation of the DECnet protocol suite for the LINUX
3  *              operating system.  DECnet is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              DECnet Socket Timer Functions
7  *
8  * Author:      Steve Whitehouse <SteveW@ACM.org>
9  *
10  *
11  * Changes:
12  *       Steve Whitehouse      : Made keepalive timer part of the same
13  *                               timer idea.
14  *       Steve Whitehouse      : Added checks for sk->sock_readers
15  *       David S. Miller       : New socket locking
16  *       Steve Whitehouse      : Timer grabs socket ref.
17  */
18 #include <linux/net.h>
19 #include <linux/socket.h>
20 #include <linux/skbuff.h>
21 #include <linux/netdevice.h>
22 #include <linux/timer.h>
23 #include <linux/spinlock.h>
24 #include <net/sock.h>
25 #include <asm/atomic.h>
26 #include <net/flow.h>
27 #include <net/dn.h>
28
29 /*
30  * Fast timer is for delayed acks (200mS max)
31  * Slow timer is for everything else (n * 500mS)
32  */
33
34 #define FAST_INTERVAL (HZ/5)
35 #define SLOW_INTERVAL (HZ/2)
36
37 static void dn_slow_timer(unsigned long arg);
38
39 void dn_start_slow_timer(struct sock *sk)
40 {
41         sk->sk_timer.expires    = jiffies + SLOW_INTERVAL;
42         sk->sk_timer.function   = dn_slow_timer;
43         sk->sk_timer.data       = (unsigned long)sk;
44
45         add_timer(&sk->sk_timer);
46 }
47
48 void dn_stop_slow_timer(struct sock *sk)
49 {
50         del_timer(&sk->sk_timer);
51 }
52
53 static void dn_slow_timer(unsigned long arg)
54 {
55         struct sock *sk = (struct sock *)arg;
56         struct dn_scp *scp = DN_SK(sk);
57
58         sock_hold(sk);
59         bh_lock_sock(sk);
60
61         if (sock_owned_by_user(sk)) {
62                 sk->sk_timer.expires = jiffies + HZ / 10;
63                 add_timer(&sk->sk_timer);
64                 goto out;
65         }
66
67         /*
68          * The persist timer is the standard slow timer used for retransmits
69          * in both connection establishment and disconnection as well as
70          * in the RUN state. The different states are catered for by changing
71          * the function pointer in the socket. Setting the timer to a value
72          * of zero turns it off. We allow the persist_fxn to turn the
73          * timer off in a permant way by returning non-zero, so that
74          * timer based routines may remove sockets. This is why we have a
75          * sock_hold()/sock_put() around the timer to prevent the socket
76          * going away in the middle.
77          */
78         if (scp->persist && scp->persist_fxn) {
79                 if (scp->persist <= SLOW_INTERVAL) {
80                         scp->persist = 0;
81
82                         if (scp->persist_fxn(sk))
83                                 goto out;
84                 } else {
85                         scp->persist -= SLOW_INTERVAL;
86                 }
87         }
88
89         /*
90          * Check for keepalive timeout. After the other timer 'cos if
91          * the previous timer caused a retransmit, we don't need to
92          * do this. scp->stamp is the last time that we sent a packet.
93          * The keepalive function sends a link service packet to the
94          * other end. If it remains unacknowledged, the standard
95          * socket timers will eventually shut the socket down. Each
96          * time we do this, scp->stamp will be updated, thus
97          * we won't try and send another until scp->keepalive has passed
98          * since the last successful transmission.
99          */
100         if (scp->keepalive && scp->keepalive_fxn && (scp->state == DN_RUN)) {
101                 if ((jiffies - scp->stamp) >= scp->keepalive)
102                         scp->keepalive_fxn(sk);
103         }
104
105         sk->sk_timer.expires = jiffies + SLOW_INTERVAL;
106
107         add_timer(&sk->sk_timer);
108 out:
109         bh_unlock_sock(sk);
110         sock_put(sk);
111 }
112
113 static void dn_fast_timer(unsigned long arg)
114 {
115         struct sock *sk = (struct sock *)arg;
116         struct dn_scp *scp = DN_SK(sk);
117
118         bh_lock_sock(sk);
119         if (sock_owned_by_user(sk)) {
120                 scp->delack_timer.expires = jiffies + HZ / 20;
121                 add_timer(&scp->delack_timer);
122                 goto out;
123         }
124
125         scp->delack_pending = 0;
126
127         if (scp->delack_fxn)
128                 scp->delack_fxn(sk);
129 out:
130         bh_unlock_sock(sk);
131 }
132
133 void dn_start_fast_timer(struct sock *sk)
134 {
135         struct dn_scp *scp = DN_SK(sk);
136
137         if (!scp->delack_pending) {
138                 scp->delack_pending = 1;
139                 init_timer(&scp->delack_timer);
140                 scp->delack_timer.expires = jiffies + FAST_INTERVAL;
141                 scp->delack_timer.data = (unsigned long)sk;
142                 scp->delack_timer.function = dn_fast_timer;
143                 add_timer(&scp->delack_timer);
144         }
145 }
146
147 void dn_stop_fast_timer(struct sock *sk)
148 {
149         struct dn_scp *scp = DN_SK(sk);
150
151         if (scp->delack_pending) {
152                 scp->delack_pending = 0;
153                 del_timer(&scp->delack_timer);
154         }
155 }
156