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  *              This program is free software; you can redistribute it and/or
  17  *              modify it under the terms of the GNU General Public License
  18  *              as published by the Free Software Foundation; either version
  19  *              2 of the License, or (at your option) any later version.
  20  */
  21 
  22 #include <linux/types.h>
  23 #include <linux/errno.h>
  24 #include <linux/socket.h>
  25 #include <linux/in.h>
  26 #include <linux/kernel.h>
  27 #include <linux/sched.h>
  28 #include <linux/timer.h>
  29 #include <asm/system.h>
  30 #include <linux/interrupt.h>
  31 #include "inet.h"
  32 #include "dev.h"
  33 #include "ip.h"
  34 #include "protocol.h"
  35 #include "tcp.h"
  36 #include "skbuff.h"
  37 #include "sock.h"
  38 #include "arp.h"
  39 
  40 void
  41 delete_timer (struct sock *t)
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43   unsigned long flags;
  44 
  45   save_flags (flags);
  46   cli();
  47 
  48   t->timeout = 0;
  49   del_timer (&t->timer);
  50 
  51   restore_flags (flags);
  52 }
  53 
  54 void
  55 reset_timer (struct sock *t, int timeout, unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57   delete_timer (t);
  58 
  59   if (timeout != -1)
  60     t->timeout = timeout;
  61 
  62 #if 1
  63   /* FIXME: ??? */
  64   if ((int) len < 0)    /* prevent close to infinite timers. THEY _DO_ */
  65         len = 3;        /* happen (negative values ?) - don't ask me why ! -FB */
  66 #endif
  67   t->timer.expires = len;
  68   add_timer (&t->timer);
  69 }
  70 
  71 
  72 /*
  73  * Now we will only be called whenever we need to do
  74  * something, but we must be sure to process all of the
  75  * sockets that need it.
  76  */
  77 void
  78 net_timer (unsigned long data)
     /* [previous][next][first][last][top][bottom][index][help] */
  79 {
  80   struct sock *sk = (struct sock*)data;
  81   int why = sk->timeout;
  82   /* timeout is overwritten by 'delete_timer' and 'reset_timer' */
  83 
  84   if (sk->inuse) {
  85     sk->timer.expires = 10;
  86     add_timer(&sk->timer);
  87     return;
  88   }
  89   sk->inuse = 1;
  90 
  91   DPRINTF ((DBG_TMR, "net_timer: found sk=%X why = %d\n", sk, why));
  92   if (sk->keepopen)
  93     reset_timer (sk, TIME_KEEPOPEN, TCP_TIMEOUT_LEN);
  94 
  95   /* Always see if we need to send an ack. */
  96   if (sk->ack_backlog) {
  97     sk->prot->read_wakeup (sk);
  98     if (! sk->dead)
  99       wake_up (sk->sleep);
 100   }
 101 
 102   /* Now we need to figure out why the socket was on the timer. */
 103   switch (why) {
 104     case TIME_DONE:
 105         if (! sk->dead || sk->state != TCP_CLOSE) {
 106           printk ("non dead socket in time_done\n");
 107           release_sock (sk);
 108           break;
 109         }
 110         destroy_sock (sk);
 111         break;
 112     case TIME_DESTROY:
 113         /* We've waited for a while for all the memory associated with
 114          * the socket to be freed.  We need to print an error message.
 115          */
 116         DPRINTF ((DBG_TMR, "possible memory leak.  sk = %X\n", sk));
 117         destroy_sock (sk);
 118         sk->inuse = 0;
 119         break;
 120     case TIME_CLOSE:
 121         /* We've waited long enough, close the socket. */
 122         sk->state = TCP_CLOSE;
 123         delete_timer (sk);
 124         /* Kill the ARP entry in case the hardware has changed. */
 125         arp_destroy (sk->daddr);
 126         if (!sk->dead)
 127           wake_up (sk->sleep);
 128         sk->shutdown = SHUTDOWN_MASK;
 129         reset_timer (sk, TIME_DESTROY, TCP_DONE_TIME);
 130         release_sock (sk);
 131         break;
 132     case TIME_WRITE:    /* try to retransmit. */
 133         /* It could be we got here because we needed to send an ack.
 134          * So we need to check for that.
 135          */
 136         if (sk->send_head) {
 137           if (jiffies < (sk->send_head->when + backoff (sk->backoff)
 138             * (2 * sk->mdev + sk->rtt))) {
 139             reset_timer (sk, TIME_WRITE, (sk->send_head->when
 140                 + backoff (sk->backoff) * (2 * sk->mdev + sk->rtt)) - jiffies);
 141             release_sock (sk);
 142             break;
 143           }
 144           /* printk("timer: seq %d retrans %d out %d cong %d\n", sk->send_head->h.seq,
 145              sk->retransmits, sk->packets_out, sk->cong_window); */
 146           DPRINTF ((DBG_TMR, "retransmitting.\n"));
 147           sk->prot->retransmit (sk, 0);
 148           if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 149             || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 150             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 1\n"));
 151             arp_destroy (sk->daddr);
 152             ip_route_check (sk->daddr);
 153           }
 154           if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 155             DPRINTF ((DBG_TMR, "timer.c TIME_WRITE time-out 2\n"));
 156             sk->err = ETIMEDOUT;
 157             if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2
 158               || sk->state == TCP_LAST_ACK) {
 159               sk->state = TCP_TIME_WAIT;
 160               reset_timer (sk, TIME_CLOSE, TCP_TIMEWAIT_LEN);
 161             } else {
 162               sk->prot->close (sk, 1);
 163               break;
 164             }
 165           }
 166         }
 167         release_sock (sk);
 168         break;
 169     case TIME_KEEPOPEN:
 170         /* Send something to keep the connection open. */
 171         if (sk->prot->write_wakeup)
 172           sk->prot->write_wakeup (sk);
 173         sk->retransmits++;
 174         if (sk->shutdown == SHUTDOWN_MASK) {
 175           sk->prot->close (sk, 1);
 176           sk->state = TCP_CLOSE;
 177         }
 178 
 179         if ((sk->state == TCP_ESTABLISHED && sk->retransmits && !(sk->retransmits & 7))
 180           || (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR1)) {
 181           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 1\n"));
 182           arp_destroy (sk->daddr);
 183           ip_route_check (sk->daddr);
 184           release_sock (sk);
 185           break;
 186         }
 187         if (sk->state != TCP_ESTABLISHED && sk->retransmits > TCP_RETR2) {
 188           DPRINTF ((DBG_TMR, "timer.c TIME_KEEPOPEN time-out 2\n"));
 189           arp_destroy (sk->daddr);
 190           sk->err = ETIMEDOUT;
 191           if (sk->state == TCP_FIN_WAIT1 || sk->state == TCP_FIN_WAIT2) {
 192             sk->state = TCP_TIME_WAIT;
 193             if (!sk->dead)
 194               wake_up (sk->sleep);
 195             release_sock (sk);
 196           } else {
 197             sk->prot->close (sk, 1);
 198           }
 199           break;
 200         }
 201         release_sock (sk);
 202         break;
 203     default:
 204         printk ("net timer expired - reason unknown, sk=%08X\n", (int)sk);
 205         release_sock (sk);
 206         break;
 207   }
 208 }
 209 

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