add/re-enable at_wini debug output
[minix3.git] / servers / inet / inet.c
blob9ce7db78c8e6e1b05411b839beb74f094bced015
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.
4 Copyright 1995 Philip Homburg
6 The valid messages and their parameters are:
8 from FS:
9 __________________________________________________________________
10 | | | | | | |
11 | m_type | DEVICE | PROC_NR | COUNT | POSITION | ADDRESS |
12 |_______________|___________|_________|_______|__________|_________|
13 | | | | | | |
14 | NW_OPEN | minor dev | proc nr | mode | | |
15 |_______________|___________|_________|_______|__________|_________|
16 | | | | | | |
17 | NW_CLOSE | minor dev | proc nr | | | |
18 |_______________|___________|_________|_______|__________|_________|
19 | | | | | | |
20 | NW_IOCTL | minor dev | proc nr | | NWIO.. | address |
21 |_______________|___________|_________|_______|__________|_________|
22 | | | | | | |
23 | NW_READ | minor dev | proc nr | count | | address |
24 |_______________|___________|_________|_______|__________|_________|
25 | | | | | | |
26 | NW_WRITE | minor dev | proc nr | count | | address |
27 |_______________|___________|_________|_______|__________|_________|
28 | | | | | | |
29 | NW_CANCEL | minor dev | proc nr | | | |
30 |_______________|___________|_________|_______|__________|_________|
32 from DL_ETH:
33 _______________________________________________________________________
34 | | | | | | |
35 | m_type | DL_PORT | DL_PROC | DL_COUNT | DL_STAT | DL_TIME |
36 |_______________|___________|_________|__________|____________|_________|
37 | | | | | | |
38 | DL_INIT_REPLY | minor dev | proc nr | rd_count | 0 | stat | time |
39 |_______________|___________|_________|__________|____________|_________|
40 | | | | | | |
41 | DL_TASK_REPLY | minor dev | proc nr | rd_count | err | stat | time |
42 |_______________|___________|_________|__________|____________|_________|
45 #include "inet.h"
47 #define _MINIX_SOURCE 1
49 #include <fcntl.h>
50 #include <time.h>
51 #include <unistd.h>
52 #include <sys/svrctl.h>
54 #include "mq.h"
55 #include "qp.h"
56 #include "proto.h"
57 #include "generic/type.h"
59 #include "generic/arp.h"
60 #include "generic/assert.h"
61 #include "generic/buf.h"
62 #include "generic/clock.h"
63 #include "generic/eth.h"
64 #include "generic/event.h"
65 #include "generic/ip.h"
66 #include "generic/psip.h"
67 #include "generic/rand256.h"
68 #include "generic/sr.h"
69 #include "generic/tcp.h"
70 #include "generic/udp.h"
72 THIS_FILE
74 #define RANDOM_DEV_NAME "/dev/random"
76 int this_proc; /* Process number of this server. */
78 #ifdef __minix_vmd
79 static int synal_tasknr= ANY;
80 #endif
82 /* Killing Solaris */
83 int killer_inet= 0;
85 #ifdef BUF_CONSISTENCY_CHECK
86 extern int inet_buf_debug;
87 #endif
89 _PROTOTYPE( void main, (void) );
91 FORWARD _PROTOTYPE( void nw_conf, (void) );
92 FORWARD _PROTOTYPE( void nw_init, (void) );
94 PUBLIC void main()
96 mq_t *mq;
97 int r;
98 int source, timerand, fd;
99 struct fssignon device;
100 #ifdef __minix_vmd
101 struct systaskinfo info;
102 #endif
103 u8_t randbits[32];
104 struct timeval tv;
106 #if DEBUG
107 printf("Starting inet...\n");
108 printf("%s\n", version);
109 #endif
111 /* Read configuration. */
112 nw_conf();
114 /* Get a random number */
115 timerand= 1;
116 fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
117 if (fd != -1)
119 r= read(fd, randbits, sizeof(randbits));
120 if (r == sizeof(randbits))
121 timerand= 0;
122 else
124 printf("unable to read random data from %s: %s\n",
125 RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
126 r == 0 ? "EOF" : "not enough data");
128 close(fd);
130 else
132 printf("unable to open random device %s: %s\n",
133 RANDOM_DEV_NAME, strerror(errno));
135 if (timerand)
137 printf("using current time for random-number seed\n");
138 #ifdef __minix_vmd
139 r= sysutime(UTIME_TIMEOFDAY, &tv);
140 #else /* Minix 3 */
141 r= gettimeofday(&tv, NULL);
142 #endif
143 if (r == -1)
145 printf("sysutime failed: %s\n", strerror(errno));
146 exit(1);
148 memcpy(randbits, &tv, sizeof(tv));
150 init_rand256(randbits);
152 #ifdef __minix_vmd
153 if (svrctl(SYSSIGNON, (void *) &info) == -1) pause();
155 /* Our new identity as a server. */
156 this_proc = info.proc_nr;
157 #else /* Minix 3 */
159 /* Our new identity as a server. */
160 if ((this_proc = getprocnr()) < 0)
161 ip_panic(( "unable to get own process nr\n"));
162 #endif
164 /* Register the device group. */
165 device.dev= ip_dev;
166 device.style= STYLE_CLONE;
167 if (svrctl(FSSIGNON, (void *) &device) == -1) {
168 printf("inet: error %d on registering ethernet devices\n",
169 errno);
170 pause();
173 #ifdef BUF_CONSISTENCY_CHECK
174 inet_buf_debug= (getenv("inetbufdebug") &&
175 (strcmp(getenv("inetbufdebug"), "on") == 0));
176 inet_buf_debug= 100;
177 if (inet_buf_debug)
179 ip_warning(( "buffer consistency check enabled" ));
181 #endif
183 if (getenv("killerinet"))
185 ip_warning(( "killer inet active" ));
186 killer_inet= 1;
189 #ifdef __minix_vmd
190 r= sys_findproc(SYN_AL_NAME, &synal_tasknr, 0);
191 if (r != OK)
192 ip_panic(( "unable to find synchronous alarm task: %d\n", r ));
193 #endif
195 nw_init();
196 while (TRUE)
198 #ifdef BUF_CONSISTENCY_CHECK
199 if (inet_buf_debug)
201 static int buf_debug_count= 0;
203 if (++buf_debug_count >= inet_buf_debug)
205 buf_debug_count= 0;
206 if (!bf_consistency_check())
207 break;
210 #endif
211 if (ev_head)
213 ev_process();
214 continue;
216 if (clck_call_expire)
218 clck_expire_timers();
219 continue;
221 mq= mq_get();
222 if (!mq)
223 ip_panic(("out of messages"));
225 r= receive (ANY, &mq->mq_mess);
226 if (r<0)
228 ip_panic(("unable to receive: %d", r));
230 reset_time();
231 source= mq->mq_mess.m_source;
232 if (source == FS_PROC_NR)
234 sr_rec(mq);
236 #ifdef __minix_vmd
237 else if (source == synal_tasknr)
239 clck_tick (&mq->mq_mess);
240 mq_free(mq);
242 #else /* Minix 3 */
243 else if (mq->mq_mess.m_type == SYN_ALARM)
245 clck_tick(&mq->mq_mess);
246 mq_free(mq);
248 else if (mq->mq_mess.m_type == PROC_EVENT)
250 /* signaled */
251 /* probably SIGTERM */
252 mq_free(mq);
254 else if (mq->mq_mess.m_type & NOTIFY_MESSAGE)
256 /* A driver is (re)started. */
257 eth_check_drivers(&mq->mq_mess);
258 mq_free(mq);
260 #endif
261 else if (mq->mq_mess.m_type == DL_TASK_REPLY)
263 eth_rec(&mq->mq_mess);
264 mq_free(mq);
266 else
268 printf("inet: got bad message type 0x%x from %d\n",
269 mq->mq_mess.m_type, mq->mq_mess.m_source);
270 mq_free(mq);
273 ip_panic(("task is not allowed to terminate"));
276 PRIVATE void nw_conf()
278 read_conf();
279 eth_prep();
280 arp_prep();
281 psip_prep();
282 ip_prep();
283 tcp_prep();
284 udp_prep();
287 PRIVATE void nw_init()
289 mq_init();
290 bf_init();
291 clck_init();
292 sr_init();
293 qp_init();
294 eth_init();
295 arp_init();
296 psip_init();
297 ip_init();
298 tcp_init();
299 udp_init();
302 PUBLIC void panic0(file, line)
303 char *file;
304 int line;
306 printf("panic at %s, %d: ", file, line);
309 PUBLIC void inet_panic()
311 printf("\ninet stacktrace: ");
312 stacktrace();
313 #ifdef __minix_vmd
314 sys_abort(RBT_PANIC);
315 #else /* Minix 3 */
316 (panic)("INET","aborted due to a panic",NO_NUM);
317 #endif
318 for(;;);
321 #if !NDEBUG
322 PUBLIC void bad_assertion(file, line, what)
323 char *file;
324 int line;
325 char *what;
327 panic0(file, line);
328 printf("assertion \"%s\" failed", what);
329 panic();
333 PUBLIC void bad_compare(file, line, lhs, what, rhs)
334 char *file;
335 int line;
336 int lhs;
337 char *what;
338 int rhs;
340 panic0(file, line);
341 printf("compare (%d) %s (%d) failed", lhs, what, rhs);
342 panic();
344 #endif /* !NDEBUG */
347 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $