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.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 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  *
  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 "inet.h"
  44 #include "dev.h"
  45 #include "ip.h"
  46 #include "protocol.h"
  47 #include "tcp.h"
  48 #include "skbuff.h"
  49 #include "sock.h"
  50 #include "arp.h"
  51 
  52 void
  53 delete_timer (struct sock *t)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55   unsigned long flags;
  56 
  57   save_flags (flags);
  58   cli();
  59 
  60   t->timeout = 0;
  61   del_timer (&t->timer);
  62 
  63   restore_flags (flags);
  64 }
  65 
  66 void
  67 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 
  74 #if 1
  75   /* FIXME: ??? */
  76   if ((int) len < 0)    /* prevent close to infinite timers. THEY _DO_ */
  77         len = 3;        /* happen (negative values ?) - don't ask me why ! -FB */
  78 #endif
  79   t->timer.expires = len;
  80   add_timer (&t->timer);
  81 }
  82 
  83 
  84 /*
  85  * Now we will only be called whenever we need to do
  86  * something, but we must be sure to process all of the
  87  * sockets that need it.
  88  */
  89 void
  90 net_timer (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92   struct sock *sk = (struct sock*)data;
  93   int why = sk->timeout;
  94   /* timeout is overwritten by 'delete_timer' and 'reset_timer' */
  95 
  96   if (sk->inuse || in_inet_bh()) {
  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     sk->prot->read_wakeup (sk);
 110     if (! sk->dead)
 111       wake_up (sk->sleep);
 112   }
 113 
 114   /* Now we need to figure out why the socket was on the timer. */
 115   switch (why) {
 116     case TIME_DONE:
 117         if (! sk->dead || sk->state != TCP_CLOSE) {
 118           printk ("non dead socket in time_done\n");
 119           release_sock (sk);
 120           break;
 121         }
 122         destroy_sock (sk);
 123         break;
 124     case TIME_DESTROY:
 125         /* We've waited for a while for all the memory associated with
 126          * the socket to be freed.  We need to print an error message.
 127          */
 128         if(sk->wmem_alloc!=0 || sk->rmem_alloc!=0)
 129         {
 130                 DPRINTF ((DBG_TMR, "possible memory leak.  sk = %X\n", sk));
 131                 sk->wmem_alloc++;       /* So it DOESNT go away */
 132                 destroy_sock (sk);
 133                 sk->wmem_alloc--;       /* Might now have hit 0 - fall through and do it again if so */
 134                 sk->inuse = 0;  /* This will be ok, the destroy won't totally work */
 135         }
 136         if(sk->wmem_alloc==0 && sk->rmem_alloc==0)
 137                 destroy_sock(sk);       /* Socket gone, DONT update sk->inuse! */
 138         break;
 139     case TIME_CLOSE:
 140         /* We've waited long enough, close the socket. */
 141         sk->state = TCP_CLOSE;
 142         delete_timer (sk);
 143         /* Kill the ARP entry in case the hardware has changed. */
 144         arp_destroy_maybe (sk->daddr);
 145         if (!sk->dead)
 146           wake_up (sk->sleep);
 147         sk->shutdown = SHUTDOWN_MASK;
 148         reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
 149         release_sock (sk);
 150         break;
 151     case TIME_PROBE0:
 152         tcp_send_probe0(sk);
 153         release_sock (sk);
 154         break;
 155     case TIME_WRITE:    /* try to retransmit. */
 156         /* It could be we got here because we needed to send an ack.
 157          * So we need to check for that.
 158          */
 159         if (sk->send_head) {
 160           if (jiffies < (sk->send_head->when + backoff (sk->backoff)
 161             * (2 * sk->mdev + sk->rtt))) {
 162             reset_timer (sk, TIME_WRITE, (sk->send_head->when
 163                 + backoff (sk->backoff) * (2 * sk->mdev + sk->rtt)) - jiffies);
 164             release_sock (sk);
 165             break;
 166           }
 167           /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
 168              sk->retransmits, sk->packets_out, sk->cong_window); */
 169           DPRINTF ((DBG_TMR, "retransmitting.\n"));
 170           sk->prot->retransmit (sk, 0);
 171           if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 172             || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 173             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 1\n"));
 174             arp_destroy_maybe (sk->daddr);
 175             ip_route_check (sk->daddr);
 176           }
 177           if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 178             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 2\n"));
 179             sk->err = ETIMEDOUT;
 180             if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2
 181               || sk->state == TCP_LAST_ACK) {
 182               sk->state = TCP_TIME_WAIT;
 183               reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
 184             } else {
 185               sk->prot->close (sk, 1);
 186               break;
 187             }
 188           }
 189         }
 190         release_sock (sk);
 191         break;
 192     case TIME_KEEPOPEN:
 193         /* Send something to keep the connection open. */
 194         if (sk->prot->write_wakeup)
 195           sk->prot->write_wakeup (sk);
 196         sk->retransmits++;
 197         if (sk->shutdown == SHUTDOWN_MASK) {
 198           sk->prot->close (sk, 1);
 199           sk->state = TCP_CLOSE;
 200         }
 201 
 202         if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 203           || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 204           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 1\n"));
 205           arp_destroy_maybe (sk->daddr);
 206           ip_route_check (sk->daddr);
 207           release_sock (sk);
 208           break;
 209         }
 210         if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 211           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 2\n"));
 212           arp_destroy_maybe (sk->daddr);
 213           sk->err = ETIMEDOUT;
 214           if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) {
 215             sk->state = TCP_TIME_WAIT;
 216             if (!sk->dead)
 217               wake_up (sk->sleep);
 218             release_sock (sk);
 219           } else {
 220             sk->prot->close (sk, 1);
 221           }
 222           break;
 223         }
 224         release_sock (sk);
 225         break;
 226     default:
 227         printk ("net timer expired - reason unknown, sk=%08X\n", (int)sk);
 228         release_sock (sk);
 229         break;
 230   }
 231 }
 232 

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