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 for IP.
   7  *
   8  * Version:     @(#)timer.c     1.0.7   05/25/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 doesn't 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 net_bh is
  26  *                                      active or the destroy causes crashes.
  27  *
  28  *              This program is free software; you can redistribute it and/or
  29  *              modify it under the terms of the GNU General Public License
  30  *              as published by the Free Software Foundation; either version
  31  *              2 of the License, or (at your option) any later version.
  32  */
  33 
  34 #include <linux/types.h>
  35 #include <linux/errno.h>
  36 #include <linux/socket.h>
  37 #include <linux/in.h>
  38 #include <linux/kernel.h>
  39 #include <linux/sched.h>
  40 #include <linux/timer.h>
  41 #include <asm/system.h>
  42 #include <linux/interrupt.h>
  43 #include <linux/inet.h>
  44 #include <linux/netdevice.h>
  45 #include "ip.h"
  46 #include "protocol.h"
  47 #include "tcp.h"
  48 #include <linux/skbuff.h>
  49 #include "sock.h"
  50 #include "arp.h"
  51 
  52 void delete_timer (struct sock *t)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54         unsigned long flags;
  55 
  56         save_flags (flags);
  57         cli();
  58 
  59         t->timeout = 0;
  60         del_timer (&t->timer);
  61 
  62         restore_flags (flags);
  63 }
  64 
  65 void reset_timer (struct sock *t, int timeout, unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67         delete_timer (t);
  68         t->timeout = timeout;
  69 #if 1
  70   /* FIXME: ??? */
  71         if ((int) len < 0)      /* prevent close to infinite timers. THEY _DO_ */
  72                 len = 3;        /* happen (negative values ?) - don't ask me why ! -FB */
  73 #endif
  74         t->timer.expires = len;
  75         add_timer (&t->timer);
  76 }
  77 
  78 
  79 /*
  80  *      Now we will only be called whenever we need to do
  81  *      something, but we must be sure to process all of the
  82  *      sockets that need it.
  83  */
  84 
  85 void net_timer (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         struct sock *sk = (struct sock*)data;
  88         int why = sk->timeout;
  89 
  90         /* 
  91          * only process if socket is not in use
  92          */
  93 
  94         cli();
  95         if (sk->inuse || in_bh) 
  96         {
  97                 sk->timer.expires = 10;
  98                 add_timer(&sk->timer);
  99                 sti();
 100                 return;
 101         }
 102 
 103         sk->inuse = 1;
 104         sti();
 105 
 106 #ifdef NOTDEF
 107         /* 
 108          * what the hell is this doing here?  this belongs in tcp.c.
 109          * I believe that this code is the cause of a lot of timer
 110          * screwups, especially during close (like FIN_WAIT1 states
 111          * with a KEEPOPEN timeout rather then a WRITE timeout).
 112          */
 113         if (skb_peek(&sk->write_queue) && 
 114               before(sk->window_seq, sk->write_queue.next->h.seq) &&
 115               sk->send_head == NULL &&
 116               sk->ack_backlog == 0 &&
 117               sk->state != TCP_TIME_WAIT)
 118                 reset_timer(sk, TIME_PROBE0, sk->rto);
 119         else if (sk->keepopen)
 120                 reset_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
 121 #endif
 122 
 123         /* Always see if we need to send an ack. */
 124 
 125         if (sk->ack_backlog) 
 126         {
 127                 sk->prot->read_wakeup (sk);
 128                 if (! sk->dead)
 129                 sk->data_ready(sk,0);
 130         }
 131 
 132         /* Now we need to figure out why the socket was on the timer. */
 133 
 134         switch (why) 
 135         {
 136                 case TIME_DONE:
 137                         if (! sk->dead || sk->state != TCP_CLOSE) 
 138                         {
 139                                 printk ("non dead socket in time_done\n");
 140                                 release_sock (sk);
 141                                 break;
 142                         }
 143                         destroy_sock (sk);
 144                         break;
 145 
 146                 case TIME_DESTROY:
 147                 /*
 148                  *      We've waited for a while for all the memory associated with
 149                  *      the socket to be freed.
 150                  */
 151                         if(sk->wmem_alloc!=0 || sk->rmem_alloc!=0)
 152                         {
 153                                 sk->wmem_alloc++;       /* So it DOESN'T go away */
 154                                 destroy_sock (sk);
 155                                 sk->wmem_alloc--;       /* Might now have hit 0 - fall through and do it again if so */
 156                                 sk->inuse = 0;  /* This will be ok, the destroy won't totally work */
 157                         }
 158                         if(sk->wmem_alloc==0 && sk->rmem_alloc==0)
 159                                 destroy_sock(sk);       /* Socket gone, DON'T update sk->inuse! */
 160                                 break;
 161                 case TIME_CLOSE:
 162                         /* We've waited long enough, close the socket. */
 163                         sk->state = TCP_CLOSE;
 164                         delete_timer (sk);
 165                         /* Kill the ARP entry in case the hardware has changed. */
 166                         arp_destroy (sk->daddr, 0);
 167                         if (!sk->dead)
 168                                 sk->state_change(sk);
 169                         sk->shutdown = SHUTDOWN_MASK;
 170                         reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
 171                         release_sock (sk);
 172                         break;
 173                 case TIME_PROBE0:
 174                         tcp_send_probe0(sk);
 175                         release_sock (sk);
 176                         break;
 177                 case TIME_WRITE:        /* try to retransmit. */
 178                         /* It could be we got here because we needed to send an ack.
 179                          * So we need to check for that.
 180                          */
 181                 {
 182                         struct sk_buff *skb;
 183                         unsigned long flags;
 184 
 185                         save_flags(flags);
 186                         cli();
 187                         skb = sk->send_head;
 188                         if (!skb) 
 189                         {
 190                                 restore_flags(flags);
 191                         } 
 192                         else 
 193                         {
 194                                 if (jiffies < skb->when + sk->rto) 
 195                                 {
 196                                         reset_timer (sk, TIME_WRITE, skb->when + sk->rto - jiffies);
 197                                         restore_flags(flags);
 198                                         release_sock (sk);
 199                                         break;
 200                                 }
 201                                 restore_flags(flags);
 202                                 /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
 203                                         sk->retransmits, sk->packets_out, sk->cong_window); */
 204                                 sk->prot->retransmit (sk, 0);
 205                                 if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 206                                         || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) 
 207                                 {
 208                                         arp_destroy (sk->daddr, 0);
 209                                         ip_route_check (sk->daddr);
 210                                 }
 211                                 if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) 
 212                                 {
 213                                         sk->err = ETIMEDOUT;
 214                                         if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2 || sk->state == TCP_CLOSING) 
 215                                         {
 216                                                 sk->state = TCP_TIME_WAIT;
 217                                                 reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
 218                                         }
 219                                         else
 220                                         {
 221                                                 sk->prot->close (sk, 1);
 222                                                         break;
 223                                         }
 224                                 }
 225                         }
 226                         release_sock (sk);
 227                         break;
 228                 }
 229                 case TIME_KEEPOPEN:
 230                         /* 
 231                          * this reset_timer() call is a hack, this is not
 232                          * how KEEPOPEN is supposed to work.
 233                          */
 234                         reset_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
 235 
 236                         /* Send something to keep the connection open. */
 237                         if (sk->prot->write_wakeup)
 238                                   sk->prot->write_wakeup (sk);
 239                         sk->retransmits++;
 240                         if (sk->shutdown == SHUTDOWN_MASK) 
 241                         {
 242                                 sk->prot->close (sk, 1);
 243                                 sk->state = TCP_CLOSE;
 244                         }
 245                         if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 246                                 || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) 
 247                         {
 248                                 arp_destroy (sk->daddr, 0);
 249                                 ip_route_check (sk->daddr);
 250                                 release_sock (sk);
 251                                 break;
 252                         }
 253                         if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) 
 254                         {
 255                                 arp_destroy (sk->daddr, 0);
 256                                 sk->err = ETIMEDOUT;
 257                                 if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) 
 258                                 {
 259                                         sk->state = TCP_TIME_WAIT;
 260                                         if (!sk->dead)
 261                                                 sk->state_change(sk);
 262                                         release_sock (sk);
 263                                   } 
 264                                   else 
 265                                   {
 266                                         sk->prot->close (sk, 1);
 267                                   }
 268                                   break;
 269                         }
 270                         release_sock (sk);
 271                         break;
 272                 default:
 273                         printk ("net_timer: timer expired - reason unknown\n");
 274                         release_sock (sk);
 275                         break;
 276         }
 277 }
 278 

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