提交 73e09839 创建 作者: Silas Boyd-Wickizer's avatar Silas Boyd-Wickizer

More lwIP initialization code

上级 dba994ab
#ifdef LWIP #ifdef LWIP
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#include "lwip/tcp_impl.h"
#include "lwip/tcpip.h" #include "lwip/tcpip.h"
#include "lwip/ip.h"
#include "lwip/netif.h"
#include "netif/etharp.h"
#pragma GCC diagnostic pop
#endif #endif
#include "types.h" #include "types.h"
...@@ -53,8 +60,77 @@ nettest(void) ...@@ -53,8 +60,77 @@ nettest(void)
} }
#ifdef LWIP #ifdef LWIP
struct timer_thread {
u64 nsec;
struct condvar waitcv;
struct spinlock waitlk;
void (*func)(void);
};
static struct timer_thread t_arp;
static struct timer_thread t_tcpf;
static struct timer_thread t_tcps;
int errno; int errno;
static void __attribute__((noreturn))
net_timer(void *x)
{
struct timer_thread *t = (struct timer_thread *) x;
for (;;) {
u64 cur = nsectime();
t->func();
acquire(&t->waitlk);
cv_sleepto(&t->waitcv, &t->waitlk, cur + t->nsec);
release(&t->waitlk);
}
}
static void
start_timer(struct timer_thread *t, void (*func)(void),
const char *name, u64 msec)
{
struct proc *p;
t->nsec = 1000000000 / 1000*msec;
t->func = func;
initcondvar(&t->waitcv, name);
initlock(&t->waitlk, name);
p = threadalloc(net_timer, t);
if (p == NULL)
panic("net: start_timer");
acquire(&p->lock);
safestrcpy(p->name, name, sizeof(p->name));
p->state = RUNNABLE;
addrun(p);
release(&p->lock);
}
static void
lwip_init(struct netif *nif, void *if_state,
u32 init_addr, u32 init_mask, u32 init_gw)
{
extern err_t if_init(struct netif *netif);
struct ip_addr ipaddr, netmask, gateway;
ipaddr.addr = init_addr;
netmask.addr = init_mask;
gateway.addr = init_gw;
if (0 == netif_add(nif, &ipaddr, &netmask, &gateway,
if_state,
if_init,
ip_input))
panic("lwip_init: error in netif_add\n");
netif_set_default(nif);
netif_set_up(nif);
}
static void static void
tcpip_init_done(void *arg) tcpip_init_done(void *arg)
{ {
...@@ -65,11 +141,19 @@ tcpip_init_done(void *arg) ...@@ -65,11 +141,19 @@ tcpip_init_done(void *arg)
void void
initnet_worker(void *x) initnet_worker(void *x)
{ {
static struct netif nif;
volatile long tcpip_done = 0; volatile long tcpip_done = 0;
tcpip_init(&tcpip_init_done, (void*)&tcpip_done); tcpip_init(&tcpip_init_done, (void*)&tcpip_done);
while (!tcpip_done) while (!tcpip_done)
yield(); yield();
memset(&nif, 0, sizeof(nif));
lwip_init(&nif, NULL, 0, 0, 0);
start_timer(&t_arp, &etharp_tmr, "arp timer", ARP_TMR_INTERVAL);
start_timer(&t_tcpf, &tcp_fasttmr, "tcp f timer", TCP_FAST_INTERVAL);
start_timer(&t_tcps, &tcp_slowtmr, "tcp s timer", TCP_SLOW_INTERVAL);
} }
void void
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论