root/net/inet/timer.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. delete_timer
  2. reset_timer
  3. net_timer

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              TIMER - implementation of software timers.
   7  *
   8  * Version:     @(#)timer.c     1.28    22/12/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *              Corey Minyard <wf-rch!minyard@relay.EU.net>
  13  *              Fred Baumgarten, <dc6iq@insu1.etec.uni-karlsruhe.de>
  14  *              Florian La Roche, <flla@stud.uni-sb.de>
  15  *
  16  * Fixes:
  17  *              Alan Cox        :       To avoid destroying a wait queue as we use it
  18  *                                      we defer destruction until the destroy timer goes
  19  *                                      off.
  20  *              Alan Cox        :       Destroy socket doesnt write a status value to the
  21  *                                      socket buffer _AFTER_ freeing it! Also sock ensures
  22  *                                      the socket will get removed BEFORE this is called
  23  *                                      otherwise if the timer TIME_DESTROY occurs inside
  24  *                                      of inet_bh() with this socket being handled it goes
  25  *                                      BOOM! Have to stop timer going off if inet_bh is
  26  *                                      active or the destroy causes crashes.
  27  *              Alan Cox        :       Clean up for final release
  28  *              Alan Cox        :       Oops - timeouts destroyed permanent arp entries!
  29  *
  30  *              This program is free software; you can redistribute it and/or
  31  *              modify it under the terms of the GNU General Public License
  32  *              as published by the Free Software Foundation; either version
  33  *              2 of the License, or (at your option) any later version.
  34  */
  35 
  36 #include <linux/types.h>
  37 #include <linux/errno.h>
  38 #include <linux/socket.h>
  39 #include <linux/in.h>
  40 #include <linux/kernel.h>
  41 #include <linux/sched.h>
  42 #include <linux/timer.h>
  43 #include <asm/system.h>
  44 #include <linux/interrupt.h>
  45 #include "inet.h"
  46 #include "devinet.h"
  47 #include "ip.h"
  48 #include "protocol.h"
  49 #include "tcp.h"
  50 #include "skbuff.h"
  51 #include "sockinet.h"
  52 #include "arp.h"
  53 
  54 void delete_timer (struct sock *t)
     /* [previous][next][first][last][top][bottom][index][help] */
  55 {
  56         unsigned long flags;
  57 
  58         save_flags (flags);
  59         cli();
  60 
  61         t->timeout = 0;
  62         del_timer (&t->timer);
  63 
  64         restore_flags (flags);
  65 }
  66 
  67 void reset_timer (struct sock *t, int timeout, unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
  68 {
  69         delete_timer (t);
  70 
  71         if (timeout != -1)
  72                 t->timeout = timeout;
  73 #if 1
  74         /* FIXME: ??? */
  75         if ((int) len < 0)      /* prevent close to infinite timers. THEY _DO_ */
  76                 len = 3;        /* happen (negative values ?) - don't ask me why ! -FB */
  77 #endif
  78         t->timer.expires = len;
  79         add_timer (&t->timer);
  80 }
  81 
  82 
  83 /*
  84  *      Now we will only be called whenever we need to do
  85  *      something, but we must be sure to process all of the
  86  *      sockets that need it.
  87  */
  88  
  89 void net_timer (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
  90 {
  91         struct sock *sk = (struct sock*)data;
  92         int why = sk->timeout;
  93         /* timeout is overwritten by 'delete_timer' and 'reset_timer' */
  94 
  95         if (sk->inuse || in_inet_bh()) 
  96         {
  97                 sk->timer.expires = 10;
  98                 add_timer(&sk->timer);
  99                 return;
 100         }
 101         sk->inuse = 1;
 102 
 103         DPRINTF ((DBG_TMR, "net_timer: found sk=%X why = %d\n", sk, why));
 104         if (sk->keepopen)
 105                 reset_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
 106 
 107         /* Always see if we need to send an ack. */
 108         if (sk->ack_backlog) 
 109         {
 110                 sk->prot->read_wakeup (sk);
 111                 if (! sk->dead)
 112                         wake_up (sk->sleep);
 113         }
 114 
 115         /* Now we need to figure out why the socket was on the timer. */
 116         switch (why) 
 117         {
 118                 case TIME_DONE:
 119                         if (! sk->dead || sk->state != TCP_CLOSE) 
 120                         {
 121                                 printk ("non dead socket in time_done\n");
 122                                 release_sock (sk);
 123                                 break;
 124                         }
 125                         destroy_sock (sk);
 126                         break;
 127                 case TIME_DESTROY:
 128                         /* We've waited for a while for all the memory associated with
 129                          * the socket to be freed.  We need to print an error message.
 130                          */
 131                         if(sk->wmem_alloc!=0 || sk->rmem_alloc!=0)
 132                         {
 133                                 DPRINTF ((DBG_TMR, "possible memory leak.  sk = %X\n", sk));
 134                                 sk->wmem_alloc++;       /* So it DOESNT go away */
 135                                 destroy_sock (sk);
 136                                 sk->wmem_alloc--;       /* Might now have hit 0 - fall through and do it again if so */
 137                                 sk->inuse = 0;  /* This will be ok, the destroy won't totally work */
 138                         }
 139                         if(sk->wmem_alloc==0 && sk->rmem_alloc==0)
 140                                 destroy_sock(sk);       /* Socket gone, DONT update sk->inuse! */
 141                         break;
 142                 case TIME_CLOSE:
 143                         /* We've waited long enough, close the socket. */
 144                         sk->state = TCP_CLOSE;
 145                         delete_timer (sk);
 146                         /* Kill the ARP entry in case the hardware has changed. */
 147                         arp_destroy_maybe (sk->daddr);
 148                         if (!sk->dead)
 149                                 wake_up (sk->sleep);
 150                         sk->shutdown = SHUTDOWN_MASK;
 151                         reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
 152                         release_sock (sk);
 153                         break;
 154                 case TIME_WRITE:        /* try to retransmit. */
 155                         /* It could be we got here because we needed to send an ack.
 156                          * So we need to check for that.
 157                          */
 158                         if (sk->send_head) 
 159                         {
 160                                 if (jiffies < (sk->send_head->when + backoff (sk->backoff)
 161                                         * (2 * sk->mdev + sk->rtt))) 
 162                                 {
 163                                         reset_timer (sk, TIME_WRITE, (sk->send_head->when
 164                                                 + backoff (sk->backoff) * (2 * sk->mdev + sk->rtt)) - jiffies);
 165                                         release_sock (sk);
 166                                         break;
 167                                 }
 168                                 /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
 169                                         sk->retransmits, sk->packets_out, sk->cong_window); */
 170                                 DPRINTF ((DBG_TMR, "retransmitting.\n"));
 171                                 sk->prot->retransmit (sk, 0);
 172                                 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 173                                         || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) 
 174                                 {
 175                                         DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 1\n"));
 176                                         arp_destroy_maybe (sk->daddr);
 177                                         ip_route_check (sk->daddr);
 178                                 }
 179                                 if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) 
 180                                 {
 181                                         DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 2\n"));
 182                                         sk->err = ETIMEDOUT;
 183                                         if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2
 184                                                 || sk->state == TCP_LAST_ACK) 
 185                                         {
 186                                                 sk->state = TCP_TIME_WAIT;
 187                                                 reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
 188                                         } 
 189                                         else 
 190                                         {
 191                                                 sk->prot->close (sk, 1);
 192                                                 break;
 193                                         }
 194                                 }
 195                         }
 196                         release_sock (sk);
 197                         break;
 198                 case TIME_KEEPOPEN:
 199                         /* Send something to keep the connection open. */
 200                         if (sk->prot->write_wakeup)
 201                                 sk->prot->write_wakeup (sk);
 202                         sk->retransmits++;
 203                         if (sk->shutdown == SHUTDOWN_MASK) 
 204                         {
 205                                 sk->prot->close (sk, 1);
 206                                 sk->state = TCP_CLOSE;
 207                         }
 208 
 209                         if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 210                                 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) 
 211                         {
 212                                 DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 1\n"));
 213                                 arp_destroy_maybe (sk->daddr);
 214                                 ip_route_check (sk->daddr);
 215                                 release_sock (sk);
 216                                 break;
 217                         }
 218                         if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) 
 219                         {
 220                                 DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 2\n"));
 221                                 arp_destroy_maybe (sk->daddr);
 222                                 sk->err = ETIMEDOUT;
 223                                 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) 
 224                                 {
 225                                         sk->state = TCP_TIME_WAIT;
 226                                         if (!sk->dead)
 227                                                 wake_up (sk->sleep);
 228                                         release_sock (sk);
 229                                 } 
 230                                 else 
 231                                 {
 232                                         sk->prot->close (sk, 1);
 233                                 }
 234                                 break;
 235                         }
 236                         release_sock (sk);
 237                         break;
 238                 default:
 239                         printk ("net timer expired - reason unknown, sk=%08X\n", (int)sk);
 240                         release_sock (sk);
 241                         break;
 242         }
 243 }
 244 

/* [previous][next][first][last][top][bottom][index][help] */