1  /*      this file contains the interface of the network software with rest of
     2          minix. Furthermore it contains the main loop of the network task.
       
     3  Copyright 1995 Philip Homburg
       
     4  The valid messages and their parameters are:
       
     5  from FS:
     6   __________________________________________________________________
     7  |               |           |         |       |          |         |
     8  | m_type        |   DEVICE  | PROC_NR | COUNT | POSITION | ADDRESS |
     9  |_______________|___________|_________|_______|__________|_________|
    10  |               |           |         |       |          |         |
    11  | NW_OPEN       | minor dev | proc nr | mode  |          |         |
    12  |_______________|___________|_________|_______|__________|_________|
    13  |               |           |         |       |          |         |
    14  | NW_CLOSE      | minor dev | proc nr |       |          |         |
    15  |_______________|___________|_________|_______|__________|_________|
    16  |               |           |         |       |          |         |
    17  | NW_IOCTL      | minor dev | proc nr |       | NWIO..   | address |
    18  |_______________|___________|_________|_______|__________|_________|
    19  |               |           |         |       |          |         |
    20  | NW_READ       | minor dev | proc nr | count |          | address |
    21  |_______________|___________|_________|_______|__________|_________|
    22  |               |           |         |       |          |         |
    23  | NW_WRITE      | minor dev | proc nr | count |          | address |
    24  |_______________|___________|_________|_______|__________|_________|
    25  |               |           |         |       |          |         |
    26  | NW_CANCEL     | minor dev | proc nr |       |          |         |
    27  |_______________|___________|_________|_______|__________|_________|
       
    28  from DL_ETH:
    29   _______________________________________________________________________
    30  |               |           |         |          |            |         |
    31  | m_type        |  DL_PORT  | DL_PROC | DL_COUNT |  DL_STAT   | DL_TIME |
    32  |_______________|___________|_________|__________|____________|_________|
    33  |               |           |         |          |            |         |
    34  | DL_TASK_INT   | minor dev | proc nr | rd_count |  0  | stat |  time   |
    35  |_______________|___________|_________|__________|____________|_________|
    36  |               |           |         |          |            |         |
    37  | DL_TASK_REPLY | minor dev | proc nr | rd_count | err | stat |  time   |         |
    38  |_______________|___________|_________|__________|____________|_________|
    39  */
       
    40  #include "inet.h"
       
    41  #define _MINIX_SOURCE 1
    42  #define _MINIX
       
    43  #include <unistd.h>
    44  #include <sys/stat.h>
       
    45  #include "mq.h"
    46  #include "proto.h"
    47  #include "generic/type.h"
       
    48  #include "generic/assert.h"
    49  #include "generic/buf.h"
    50  #include "generic/clock.h"
    51  #include "generic/eth.h"
    52  #include "generic/event.h"
    53  #if !CRAMPED
    54  #include "generic/arp.h"
    55  #include "generic/ip.h"
    56  #include "generic/psip.h"
    57  #include "generic/sr.h"
    58  #include "generic/tcp.h"
    59  #include "generic/udp.h"
    60  #endif
       
    61  THIS_FILE
       
    62  #ifdef BUF_CONSISTENCY_CHECK
    63  extern int inet_buf_debug;
    64  #endif
       
    65  _PROTOTYPE( void main, (void) );
       
    66  FORWARD _PROTOTYPE( void nw_init, (void) );
       

Inet starts in inet/inet.c in the main function.

    67  PUBLIC void main()
    68  {
    69          mq_t *mq;
    70          int r;
    71          int source;
    72          struct stat stb;
       
    73          DBLOCK(1, printf("%s\n", version));
       
    74  #ifdef BUF_CONSISTENCY_CHECK
    75          inet_buf_debug= 100;
    76          if (inet_buf_debug)
    77          {
    78                  ip_warning(( "buffer consistency check enabled" ));
    79          }
    80  #endif
       

Inet calls nw_init() which does the initilization. nw_init() calls mq_init().

    81          nw_init();
    82          while (TRUE)
    83          {
    84  #ifdef BUF_CONSISTENCY_CHECK
    85                  if (inet_buf_debug)
    86                  {
    87                          static int buf_debug_count= 0;
       
    88                          if (buf_debug_count++ > inet_buf_debug)
    89                          {
    90                                  buf_debug_count= 0;
    91                                  if (!bf_consistency_check())
    92                                          break;
    93                          }
    94                  }
    95  #endif
    96                  if (ev_head)
    97                  {
    98                          ev_process();
    99                          continue;
   100                  }
   101                  if (clck_call_expire)
   102                  {
   103                          clck_expire_timers();
   104                          continue;
   105                  }

inet gets a free message entry from the message queue to place a new message in by calling mq_get().

   106                  mq= mq_get();
   107                  if (!mq)
   108                          ip_panic(("out of messages"));
       

Wait to receive a message. Put the message in the message queue after it is received.

   109                  r= receive (ANY, &mq->mq_mess);
   110                  if (r<0)
   111                  {
   112                          ip_panic(("unable to receive: %d", r));
   113                  }
   114                  reset_time();
   115                  source= mq->mq_mess.m_source;

sr_rec handles the different types of messages passed to inet by the file process fs.
sr_rec is defined in sr.c.

   116                  if (source == FS_PROC_NR)
   117                  {
   118                          sr_rec(mq);
   119                  }
   120                  else if (source == DL_ETH)
   121                  {
   122  compare(mq->mq_mess.m_type, ==, DL_TASK_REPLY);
   123                          eth_rec(&mq->mq_mess);
   124                          mq_free(mq);
   125                  }
   126                  else if (source == SYN_ALRM_TASK)
   127                  {
   128                          clck_tick (&mq->mq_mess);
   129                          mq_free(mq);
   130                  }
   131                  else
   132                  {
   133                          ip_panic(("message from unknown source: %d",
   134                                  mq->mq_mess.m_source));
   135                  }
   136          }
   137          ip_panic(("task is not allowed to terminate"));
   138  }
       

nw_init is the initialization routine for inet. nw_init is called in main()

   139  PRIVATE void nw_init()
   140  {

mq_init() is the initialization routine for the message queue. mq_init() is defined in inet/mq.c.

   141          mq_init();

bf_init() is the initialization routine for the buffer. bf_init() is defined in inet/buf.c.

   142          bf_init();
   143          clck_init();

sr_init() is the initialization routine for the sr_fd_table. sr_init() is defined in inet/sr.c.

   144          sr_init();

eth_init() is the initialization routine for the ethernet interface. eth_init() is defined in inet/generic/eth.c.

   145          eth_init();

arp_init() is the initialization routine for the arp interface. arp_init() is defined in inet/generic/arp.c.

   146  #if ENABLE_ARP
   147          arp_init();
   148  #endif

psip_init() is the initialization routine for the pseudo-ip interface. arp_init() is defined in inet/generic/psip.c.

   149  #if ENABLE_PSIP
   150          psip_init();
   151  #endif

ip_init() is the initialization routine for the ip interface. ip_init() is defined in inet/generic/ip.c.

   152  #if ENABLE_IP
   153          ip_init();
   154  #endif

tcp_init() is the initialization routine for the ip interface. tcp_init() is defined in inet/generic/tcp.c.

   155  #if ENABLE_TCP
   156          tcp_init();
   157  #endif

udp_init() is the initialization routine for the udp interface. udp_init() is defined in inet/generic/udp.c.

   158  #if ENABLE_UDP
   159          udp_init();
   160  #endif
   161  }
       
   162  #if !CRAMPED
   163  PUBLIC void panic0(file, line)
   164  char *file;
   165  int line;
   166  {
   167          printf("panic at %s, %d: ", file, line);
   168  }
       
   169  PUBLIC void panic()
   170  {
   171          printf("\ninet stacktrace: ");
   172          stacktrace();
   173          sys_abort(RBT_PANIC);
   174  }
       
   175  #else /* CRAMPED */
       
   176  PUBLIC void panic(file, line)
   177  char *file;
   178  int line;
   179  {
   180          printf("panic at %s, %d\n", file, line);
   181          sys_abort(RBT_PANIC);
   182  }
   183  #endif
       
   184  #if !NDEBUG
   185  PUBLIC void bad_assertion(file, line, what)
   186  char *file;
   187  int line;
   188  char *what;
   189  {
   190          panic0(file, line);
   191          printf("assertion \"%s\" failed", what);
   192          panic();
   193  }
       
       
   194  PUBLIC void bad_compare(file, line, lhs, what, rhs)
   195  char *file;
   196  int line;
   197  int lhs;
   198  char *what;
   199  int rhs;
   200  {
   201          panic0(file, line);
   202          printf("compare (%d) %s (%d) failed", lhs, what, rhs);
   203          panic();
   204  }
   205  #endif /* !NDEBUG */
       
   206  /*
   207   * $PchId: inet.c,v 1.12 1996/12/17 07:58:19 philip Exp $
   208   */