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:
9 __________________________________________________________________
11 | m_type | DEVICE | PROC_NR | COUNT | POSITION | ADDRESS |
12 |_______________|___________|_________|_______|__________|_________|
14 | NW_OPEN | minor dev | proc nr | mode | | |
15 |_______________|___________|_________|_______|__________|_________|
17 | NW_CLOSE | minor dev | proc nr | | | |
18 |_______________|___________|_________|_______|__________|_________|
20 | NW_IOCTL | minor dev | proc nr | | NWIO.. | address |
21 |_______________|___________|_________|_______|__________|_________|
23 | NW_READ | minor dev | proc nr | count | | address |
24 |_______________|___________|_________|_______|__________|_________|
26 | NW_WRITE | minor dev | proc nr | count | | address |
27 |_______________|___________|_________|_______|__________|_________|
29 | NW_CANCEL | minor dev | proc nr | | | |
30 |_______________|___________|_________|_______|__________|_________|
33 _______________________________________________________________________
35 | m_type | DL_PORT | DL_PROC | DL_COUNT | DL_STAT | DL_TIME |
36 |_______________|___________|_________|__________|____________|_________|
38 | DL_CONF_REPLY | minor dev | proc nr | rd_count | 0 | stat | time |
39 |_______________|___________|_________|__________|____________|_________|
41 | DL_TASK_REPLY | minor dev | proc nr | rd_count | err | stat | time |
42 |_______________|___________|_________|__________|____________|_________|
47 #define _MINIX_SOURCE 1
51 #include <sys/svrctl.h>
53 #include <minix/endpoint.h>
54 #include <minix/drivers.h>
55 #include <minix/driver.h>
60 #include "generic/type.h"
62 #include "generic/arp.h"
63 #include "generic/assert.h"
64 #include "generic/buf.h"
65 #include "generic/clock.h"
66 #include "generic/eth.h"
67 #include "generic/event.h"
68 #include "generic/ip.h"
69 #include "generic/psip.h"
70 #include "generic/rand256.h"
71 #include "generic/sr.h"
72 #include "generic/tcp.h"
73 #include "generic/udp.h"
77 #define RANDOM_DEV_NAME "/dev/random"
79 endpoint_t this_proc
; /* Process number of this server. */
84 #ifdef BUF_CONSISTENCY_CHECK
85 extern int inet_buf_debug
;
92 _PROTOTYPE( void main
, (void) );
94 FORWARD
_PROTOTYPE( void nw_conf
, (void) );
95 FORWARD
_PROTOTYPE( void nw_init
, (void) );
96 FORWARD
_PROTOTYPE( void ds_event
, (void) );
98 /* SEF functions and variables. */
99 FORWARD
_PROTOTYPE( void sef_local_startup
, (void) );
100 FORWARD
_PROTOTYPE( int sef_cb_init_fresh
, (int type
, sef_init_info_t
*info
) );
110 /* SEF local startup. */
115 #ifdef BUF_CONSISTENCY_CHECK
118 static int buf_debug_count
= 0;
120 if (++buf_debug_count
>= inet_buf_debug
)
123 if (!bf_consistency_check())
133 if (clck_call_expire
)
135 clck_expire_timers();
140 ip_panic(("out of messages"));
142 r
= sef_receive_status(ANY
, &mq
->mq_mess
, &ipc_status
);
145 ip_panic(("unable to receive: %d", r
));
148 source
= mq
->mq_mess
.m_source
;
149 m_type
= mq
->mq_mess
.m_type
;
150 if (source
== FS_PROC_NR
)
154 else if (is_ipc_notify(ipc_status
))
158 clck_tick(&mq
->mq_mess
);
161 else if (source
== PM_PROC_NR
)
164 /* probably SIGTERM */
167 else if (source
== DS_PROC_NR
)
169 /* DS notifies us of an event. */
175 printf("inet: got unexpected notify from %d\n",
176 mq
->mq_mess
.m_source
);
180 else if (m_type
== DL_CONF_REPLY
|| m_type
== DL_TASK_REPLY
||
181 m_type
== DL_NAME_REPLY
|| m_type
== DL_STAT_REPLY
)
183 eth_rec(&mq
->mq_mess
);
188 printf("inet: got bad message type 0x%x from %d\n",
189 mq
->mq_mess
.m_type
, mq
->mq_mess
.m_source
);
193 ip_panic(("task is not allowed to terminate"));
196 /*===========================================================================*
197 * sef_local_startup *
198 *===========================================================================*/
199 PRIVATE
void sef_local_startup()
201 /* Register init callbacks. */
202 sef_setcb_init_fresh(sef_cb_init_fresh
);
203 sef_setcb_init_restart(sef_cb_init_fresh
);
205 /* No live update support for now. */
207 /* Let SEF perform startup. */
211 /*===========================================================================*
212 * sef_cb_init_fresh *
213 *===========================================================================*/
214 PRIVATE
int sef_cb_init_fresh(int type
, sef_init_info_t
*info
)
216 /* Initialize the inet server. */
225 printf("Starting inet...\n");
226 printf("%s\n", version
);
230 system_hz
= sys_hz();
233 /* Read configuration. */
236 /* Get a random number */
238 fd
= open(RANDOM_DEV_NAME
, O_RDONLY
| O_NONBLOCK
);
241 r
= read(fd
, randbits
, sizeof(randbits
));
242 if (r
== sizeof(randbits
))
246 printf("inet: unable to read random data from %s: %s\n",
247 RANDOM_DEV_NAME
, r
== -1 ? strerror(errno
) :
248 r
== 0 ? "EOF" : "not enough data");
254 printf("inet: unable to open random device %s: %s\n",
255 RANDOM_DEV_NAME
, strerror(errno
));
259 printf("inet: using current time for random-number seed\n");
260 r
= gettimeofday(&tv
, NULL
);
263 printf("sysutime failed: %s\n", strerror(errno
));
266 memcpy(randbits
, &tv
, sizeof(tv
));
268 init_rand256(randbits
);
270 /* Our new identity as a server. */
271 r
= sys_whoami(&tasknr
, my_name
, sizeof(my_name
));
273 ip_panic(("inet: sys_whoami failed for 'inet': %d", r
));
276 #ifdef BUF_CONSISTENCY_CHECK
277 inet_buf_debug
= (getenv("inetbufdebug") &&
278 (strcmp(getenv("inetbufdebug"), "on") == 0));
282 ip_warning(( "buffer consistency check enabled" ));
286 if (getenv("killerinet"))
288 ip_warning(( "killer inet active" ));
294 /* Subscribe to driver events for network drivers. */
295 r
= ds_subscribe("drv\\.net\\..*", DSF_INITIAL
| DSF_OVERWRITE
);
297 ip_panic(("inet: can't subscribe to driver events"));
300 /* Announce we are up. INET announces its presence to VFS just like
308 PRIVATE
void nw_conf()
319 PRIVATE
void nw_init()
334 /*===========================================================================*
336 *===========================================================================*/
337 PRIVATE
void ds_event()
339 char key
[DS_MAX_KEYLEN
];
340 char *driver_prefix
= "drv.net.";
343 endpoint_t owner_endpoint
;
346 /* Get the event and the owner from DS. */
347 r
= ds_check(key
, &type
, &owner_endpoint
);
350 printf("inet: ds_event: ds_check failed: %d\n", r
);
353 r
= ds_retrieve_u32(key
, &value
);
355 printf("inet: ds_event: ds_retrieve_u32 failed\n");
359 /* Only check for network driver up events. */
360 if(strncmp(key
, driver_prefix
, sizeof(driver_prefix
))
361 || value
!= DS_DRIVER_UP
) {
365 /* A driver is (re)started. */
366 eth_check_driver(owner_endpoint
);
369 PUBLIC
void panic0(file
, line
)
373 printf("panic at %s, %d: ", file
, line
);
376 PUBLIC
void inet_panic()
378 printf("\ninet stacktrace: ");
380 (panic
)("aborted due to a panic");
385 PUBLIC
void bad_assertion(file
, line
, what
)
391 printf("assertion \"%s\" failed", what
);
396 PUBLIC
void bad_compare(file
, line
, lhs
, what
, rhs
)
404 printf("compare (%d) %s (%d) failed", lhs
, what
, rhs
);
410 * $PchId: inet.c,v 1.23 2005/06/28 14:27:22 philip Exp $