3 #include <sys/svrctl.h>
5 #include <minix/endpoint.h>
11 #include <minix/chardriver.h>
12 #include <minix/syslib.h>
13 #include <minix/sysutil.h>
14 #include <minix/timers.h>
15 #include <minix/netsock.h>
20 #include <lwip/pbuf.h>
21 #include <lwip/stats.h>
22 #include <lwip/netif.h>
23 #include <netif/etharp.h>
24 #include <lwip/tcp_impl.h>
28 static timer_t tcp_ftmr
, tcp_stmr
, arp_tmr
;
29 static int arp_ticks
, tcp_fticks
, tcp_sticks
;
31 static struct netif
* netif_lo
;
33 extern struct sock_ops sock_udp_ops
;
34 extern struct sock_ops sock_tcp_ops
;
35 extern struct sock_ops sock_raw_ip_ops
;
41 static void arp_watchdog(__unused timer_t
*tp
)
44 set_timer(&arp_tmr
, arp_ticks
, arp_watchdog
, 0);
47 static void tcp_fwatchdog(__unused timer_t
*tp
)
50 set_timer(&tcp_ftmr
, tcp_fticks
, tcp_fwatchdog
, 0);
53 static void tcp_swatchdog(__unused timer_t
*tp
)
56 set_timer(&tcp_ftmr
, tcp_sticks
, tcp_swatchdog
, 0);
59 static int sef_cb_init_fresh(__unused
int type
, __unused sef_init_info_t
*info
)
67 err
= sys_whoami(&lwip_ep
, my_name
, sizeof(my_name
), &my_priv
);
69 panic("Cannot get own endpoint");
74 /* init lwip library */
83 arp_ticks
= ARP_TMR_INTERVAL
/ (1000 / hz
);
84 tcp_fticks
= TCP_FAST_INTERVAL
/ (1000 / hz
);
85 tcp_sticks
= TCP_SLOW_INTERVAL
/ (1000 / hz
);
89 set_timer(&arp_tmr
, arp_ticks
, arp_watchdog
, 0);
90 set_timer(&tcp_ftmr
, tcp_fticks
, tcp_fwatchdog
, 0);
91 set_timer(&tcp_stmr
, tcp_sticks
, tcp_swatchdog
, 0);
94 netif_lo
= netif_find("lo0");
96 /* Read configuration. */
100 /* Get a random number */
102 fd
= open(RANDOM_DEV_NAME
, O_RDONLY
| O_NONBLOCK
);
105 err
= read(fd
, randbits
, sizeof(randbits
));
106 if (err
== sizeof(randbits
))
110 printf("inet: unable to read random data from %s: %s\n",
111 RANDOM_DEV_NAME
, err
== -1 ? strerror(errno
) :
112 err
== 0 ? "EOF" : "not enough data");
118 printf("inet: unable to open random device %s: %s\n",
119 RANDOM_DEV_NAME
, strerror(errno
));
123 printf("inet: using current time for random-number seed\n");
124 err
= gettimeofday(&tv
, NULL
);
127 printf("sysutime failed: %s\n", strerror(errno
));
130 memcpy(randbits
, &tv
, sizeof(tv
));
132 init_rand256(randbits
);
135 /* Subscribe to driver events for network drivers. */
136 if ((err
= ds_subscribe("drv\\.net\\..*",
137 DSF_INITIAL
| DSF_OVERWRITE
)) != OK
)
138 panic(("inet: can't subscribe to driver events"));
140 /* Announce we are up. LWIP announces its presence to VFS just like
141 * any other character driver.
143 chardriver_announce();
148 static void sef_local_startup()
150 /* Register init callbacks. */
151 sef_setcb_init_fresh(sef_cb_init_fresh
);
152 sef_setcb_init_restart(sef_cb_init_fresh
);
154 /* No live update support for now. */
156 /* Let SEF perform startup. */
160 static void ds_event(void)
162 char key
[DS_MAX_KEYLEN
];
163 char *driver_prefix
= "drv.net.";
167 endpoint_t owner_endpoint
;
170 /* We may get one notification for multiple updates from DS. Get events
171 * and owners from DS, until DS tells us that there are no more.
173 while ((r
= ds_check(key
, &type
, &owner_endpoint
)) == OK
) {
174 r
= ds_retrieve_u32(key
, &value
);
176 printf("LWIP : ds_event: ds_retrieve_u32 failed\n");
180 /* Only check for network driver up events. */
181 if(strncmp(key
, driver_prefix
, sizeof(driver_prefix
))
182 || value
!= DS_DRIVER_UP
)
185 /* The driver label comes after the prefix. */
186 label
= key
+ strlen(driver_prefix
);
188 /* A driver is (re)started. */
189 driver_up(label
, owner_endpoint
);
193 printf("LWIP : ds_event: ds_check failed: %d\n", r
);
196 static void netif_poll_lo(void)
198 if (netif_lo
== NULL
)
201 while (netif_lo
->loop_first
)
202 netif_poll(netif_lo
);
205 void socket_open(message
* m
)
207 struct sock_ops
* ops
;
208 struct socket
* sock
;
219 ops
= &sock_raw_ip_ops
;
222 if (m
->DEVICE
- SOCK_TYPES
< MAX_DEVS
) {
223 m
->DEVICE
-= SOCK_TYPES
;
227 printf("LWIP unknown socket type %d\n", m
->DEVICE
);
228 send_reply_open(m
, EINVAL
);
232 sock
= get_unused_sock();
234 printf("LWIP : no free socket\n");
235 send_reply_open(m
, EAGAIN
);
240 sock
->select_ep
= NONE
;
241 sock
->recv_data_size
= 0;
243 if (sock
->ops
&& sock
->ops
->open
)
244 ret
= sock
->ops
->open(sock
, m
);
247 debug_print("new socket %ld", get_sock_num(sock
));
248 send_reply_open(m
, get_sock_num(sock
));
250 debug_print("failed %d", ret
);
251 send_reply_open(m
, ret
);
255 int main(__unused
int argc
, __unused
char ** argv
)
267 if ((err
= sef_receive_status(ANY
, &m
, &ipc_status
)) != OK
) {
268 printf("LWIP : sef_receive_status errr %d\n", err
);
272 if (m
.m_source
== VFS_PROC_NR
)
274 else if (is_ipc_notify(ipc_status
)) {
275 switch (m
.m_source
) {
277 expire_timers(m
.NOTIFY_TIMESTAMP
);
283 panic("LWIP : unhandled event from PM");
286 printf("LWIP : unexpected notify from %d\n",
291 /* all other request can be from drivers only */