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 (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_WRITE:    /* try to retransmit. */
 152         /* It could be we got here because we needed to send an ack.
 153          * So we need to check for that.
 154          */
 155         if (sk->send_head) {
 156           if (jiffies < (sk->send_head->when + backoff (sk->backoff)
 157             * (2 * sk->mdev + sk->rtt))) {
 158             reset_timer (sk, TIME_WRITE, (sk->send_head->when
 159                 + backoff (sk->backoff) * (2 * sk->mdev + sk->rtt)) - jiffies);
 160             release_sock (sk);
 161             break;
 162           }
 163           /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
 164              sk->retransmits, sk->packets_out, sk->cong_window); */
 165           DPRINTF ((DBG_TMR, "retransmitting.\n"));
 166           sk->prot->retransmit (sk, 0);
 167           if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 168             || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 169             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 1\n"));
 170             arp_destroy (sk->daddr);
 171             ip_route_check (sk->daddr);
 172           }
 173           if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 174             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 2\n"));
 175             sk->err = ETIMEDOUT;
 176             if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2
 177               || sk->state == TCP_LAST_ACK) {
 178               sk->state = TCP_TIME_WAIT;
 179               reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
 180             } else {
 181               sk->prot->close (sk, 1);
 182               break;
 183             }
 184           }
 185         }
 186         release_sock (sk);
 187         break;
 188     case TIME_KEEPOPEN:
 189         /* Send something to keep the connection open. */
 190         if (sk->prot->write_wakeup)
 191           sk->prot->write_wakeup (sk);
 192         sk->retransmits++;
 193         if (sk->shutdown == SHUTDOWN_MASK) {
 194           sk->prot->close (sk, 1);
 195           sk->state = TCP_CLOSE;
 196         }
 197 
 198         if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 199           || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 200           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 1\n"));
 201           arp_destroy (sk->daddr);
 202           ip_route_check (sk->daddr);
 203           release_sock (sk);
 204           break;
 205         }
 206         if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 207           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 2\n"));
 208           arp_destroy (sk->daddr);
 209           sk->err = ETIMEDOUT;
 210           if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) {
 211             sk->state = TCP_TIME_WAIT;
 212             if (!sk->dead)
 213               wake_up (sk->sleep);
 214             release_sock (sk);
 215           } else {
 216             sk->prot->close (sk, 1);
 217           }
 218           break;
 219         }
 220         release_sock (sk);
 221         break;
 222     default:
 223         printk ("net timer expired - reason unknown, sk=%08X\n", (int)sk);
 224         release_sock (sk);
 225         break;
 226   }
 227 }
 228 

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