phys addr arg of 0 must be possible for pt_writemap too (instead of meaning
[minix.git] / servers / inet / inet.c
blobcfc06acebcfa424494caea3fa7fb0b4366112299
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_CONF_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>
53 #include <minix/ds.h>
55 #include "mq.h"
56 #include "qp.h"
57 #include "proto.h"
58 #include "generic/type.h"
60 #include "generic/arp.h"
61 #include "generic/assert.h"
62 #include "generic/buf.h"
63 #include "generic/clock.h"
64 #include "generic/eth.h"
65 #include "generic/event.h"
66 #include "generic/ip.h"
67 #include "generic/psip.h"
68 #include "generic/rand256.h"
69 #include "generic/sr.h"
70 #include "generic/tcp.h"
71 #include "generic/udp.h"
73 THIS_FILE
75 #define RANDOM_DEV_NAME "/dev/random"
77 int this_proc; /* Process number of this server. */
79 #ifdef __minix_vmd
80 static int synal_tasknr= ANY;
81 #endif
83 /* Killing Solaris */
84 int killer_inet= 0;
86 #ifdef BUF_CONSISTENCY_CHECK
87 extern int inet_buf_debug;
88 #endif
90 #if HZ_DYNAMIC
91 u32_t system_hz;
92 #endif
94 _PROTOTYPE( void main, (void) );
96 FORWARD _PROTOTYPE( void nw_conf, (void) );
97 FORWARD _PROTOTYPE( void nw_init, (void) );
99 PUBLIC void main()
101 mq_t *mq;
102 int r;
103 int source, m_type, timerand, fd;
104 u32_t tasknr;
105 struct fssignon device;
106 #ifdef __minix_vmd
107 struct systaskinfo info;
108 #endif
109 u8_t randbits[32];
110 struct timeval tv;
112 #if DEBUG
113 printf("Starting inet...\n");
114 printf("%s\n", version);
115 #endif
117 #if HZ_DYNAMIC
118 system_hz = sys_hz();
119 #endif
121 /* Read configuration. */
122 nw_conf();
124 /* Get a random number */
125 timerand= 1;
126 fd= open(RANDOM_DEV_NAME, O_RDONLY | O_NONBLOCK);
127 if (fd != -1)
129 r= read(fd, randbits, sizeof(randbits));
130 if (r == sizeof(randbits))
131 timerand= 0;
132 else
134 printf("unable to read random data from %s: %s\n",
135 RANDOM_DEV_NAME, r == -1 ? strerror(errno) :
136 r == 0 ? "EOF" : "not enough data");
138 close(fd);
140 else
142 printf("unable to open random device %s: %s\n",
143 RANDOM_DEV_NAME, strerror(errno));
145 if (timerand)
147 printf("using current time for random-number seed\n");
148 #ifdef __minix_vmd
149 r= sysutime(UTIME_TIMEOFDAY, &tv);
150 #else /* Minix 3 */
151 r= gettimeofday(&tv, NULL);
152 #endif
153 if (r == -1)
155 printf("sysutime failed: %s\n", strerror(errno));
156 exit(1);
158 memcpy(randbits, &tv, sizeof(tv));
160 init_rand256(randbits);
162 #ifdef __minix_vmd
163 if (svrctl(SYSSIGNON, (void *) &info) == -1) pause();
165 /* Our new identity as a server. */
166 this_proc = info.proc_nr;
167 #else /* Minix 3 */
169 /* Our new identity as a server. */
170 r= ds_retrieve_u32("inet", &tasknr);
171 if (r != OK)
172 ip_panic(("inet: ds_retrieve_u32 failed for 'inet': %d", r));
173 this_proc= tasknr;
174 #endif
176 /* Register the device group. */
177 device.dev= ip_dev;
178 device.style= STYLE_CLONE;
179 if (svrctl(FSSIGNON, (void *) &device) == -1) {
180 printf("inet: error %d on registering ethernet devices\n",
181 errno);
182 pause();
185 #ifdef BUF_CONSISTENCY_CHECK
186 inet_buf_debug= (getenv("inetbufdebug") &&
187 (strcmp(getenv("inetbufdebug"), "on") == 0));
188 inet_buf_debug= 100;
189 if (inet_buf_debug)
191 ip_warning(( "buffer consistency check enabled" ));
193 #endif
195 if (getenv("killerinet"))
197 ip_warning(( "killer inet active" ));
198 killer_inet= 1;
201 #ifdef __minix_vmd
202 r= sys_findproc(SYN_AL_NAME, &synal_tasknr, 0);
203 if (r != OK)
204 ip_panic(( "unable to find synchronous alarm task: %d\n", r ));
205 #endif
207 nw_init();
208 while (TRUE)
210 #ifdef BUF_CONSISTENCY_CHECK
211 if (inet_buf_debug)
213 static int buf_debug_count= 0;
215 if (++buf_debug_count >= inet_buf_debug)
217 buf_debug_count= 0;
218 if (!bf_consistency_check())
219 break;
222 #endif
223 if (ev_head)
225 ev_process();
226 continue;
228 if (clck_call_expire)
230 clck_expire_timers();
231 continue;
233 mq= mq_get();
234 if (!mq)
235 ip_panic(("out of messages"));
237 r= receive (ANY, &mq->mq_mess);
238 if (r<0)
240 ip_panic(("unable to receive: %d", r));
242 reset_time();
243 source= mq->mq_mess.m_source;
244 m_type= mq->mq_mess.m_type;
245 if (source == FS_PROC_NR)
247 sr_rec(mq);
249 #ifdef __minix_vmd
250 else if (source == synal_tasknr)
252 clck_tick (&mq->mq_mess);
253 mq_free(mq);
255 #else /* Minix 3 */
256 else if (m_type == SYN_ALARM)
258 clck_tick(&mq->mq_mess);
259 mq_free(mq);
261 else if (m_type == PROC_EVENT)
263 /* signaled */
264 /* probably SIGTERM */
265 mq_free(mq);
267 else if (m_type & NOTIFY_MESSAGE)
269 /* A driver is (re)started. */
270 eth_check_drivers(&mq->mq_mess);
271 mq_free(mq);
273 #endif
274 else if (m_type == DL_CONF_REPLY || m_type == DL_TASK_REPLY ||
275 m_type == DL_NAME_REPLY || m_type == DL_STAT_REPLY)
277 eth_rec(&mq->mq_mess);
278 mq_free(mq);
280 else
282 printf("inet: got bad message type 0x%x from %d\n",
283 mq->mq_mess.m_type, mq->mq_mess.m_source);
284 mq_free(mq);
287 ip_panic(("task is not allowed to terminate"));
290 PRIVATE void nw_conf()
292 read_conf();
293 eth_prep();
294 arp_prep();
295 psip_prep();
296 ip_prep();
297 tcp_prep();
298 udp_prep();
301 PRIVATE void nw_init()
303 mq_init();
304 bf_init();
305 clck_init();
306 sr_init();
307 qp_init();
308 eth_init();
309 arp_init();
310 psip_init();
311 ip_init();
312 tcp_init();
313 udp_init();
316 PUBLIC void panic0(file, line)
317 char *file;
318 int line;
320 printf("panic at %s, %d: ", file, line);
323 PUBLIC void inet_panic()
325 printf("\ninet stacktrace: ");
326 util_stacktrace();
327 #ifdef __minix_vmd
328 sys_abort(RBT_PANIC);
329 #else /* Minix 3 */
330 (panic)("INET","aborted due to a panic",NO_NUM);
331 #endif
332 for(;;);
335 #if !NDEBUG
336 PUBLIC void bad_assertion(file, line, what)
337 char *file;
338 int line;
339 char *what;
341 panic0(file, line);
342 printf("assertion \"%s\" failed", what);
343 panic();
347 PUBLIC void bad_compare(file, line, lhs, what, rhs)
348 char *file;
349 int line;
350 int lhs;
351 char *what;
352 int rhs;
354 panic0(file, line);
355 printf("compare (%d) %s (%d) failed", lhs, what, rhs);
356 panic();
358 #endif /* !NDEBUG */
361 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $