Merge pull request #41 from aozima/aozima

fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
This commit is contained in:
Bernard Xiong 2013-02-28 02:46:24 -08:00
commit 9b5d6887eb
1 changed files with 280 additions and 252 deletions

View File

@ -14,6 +14,7 @@
* 2012-04-10 Bernard add more compatible with RT-Thread. * 2012-04-10 Bernard add more compatible with RT-Thread.
* 2012-11-12 Bernard The network interface can be initialized * 2012-11-12 Bernard The network interface can be initialized
* after lwIP initialization. * after lwIP initialization.
* 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
*/ */
/* /*
@ -71,8 +72,8 @@
*/ */
struct eth_tx_msg struct eth_tx_msg
{ {
struct netif *netif; struct netif *netif;
struct pbuf *buf; struct pbuf *buf;
}; };
static struct rt_mailbox eth_tx_thread_mb; static struct rt_mailbox eth_tx_thread_mb;
@ -99,288 +100,288 @@ static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p) static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
{ {
struct eth_tx_msg msg; struct eth_tx_msg msg;
struct eth_device* enetif; struct eth_device* enetif;
enetif = (struct eth_device*)netif->state; enetif = (struct eth_device*)netif->state;
/* send a message to eth tx thread */ /* send a message to eth tx thread */
msg.netif = netif; msg.netif = netif;
msg.buf = p; msg.buf = p;
if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK) if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
{ {
/* waiting for ack */ /* waiting for ack */
rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER); rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
} }
return ERR_OK; return ERR_OK;
} }
static err_t eth_netif_device_init(struct netif *netif) static err_t eth_netif_device_init(struct netif *netif)
{ {
struct eth_device *ethif; struct eth_device *ethif;
ethif = (struct eth_device*)netif->state; ethif = (struct eth_device*)netif->state;
if (ethif != RT_NULL) if (ethif != RT_NULL)
{ {
rt_device_t device; rt_device_t device;
/* get device object */ /* get device object */
device = (rt_device_t) ethif; device = (rt_device_t) ethif;
if (rt_device_init(device) != RT_EOK) if (rt_device_init(device) != RT_EOK)
{ {
return ERR_IF; return ERR_IF;
} }
/* copy device flags to netif flags */ /* copy device flags to netif flags */
netif->flags = ethif->flags; netif->flags = ethif->flags;
/* set default netif */ /* set default netif */
if (netif_default == RT_NULL) if (netif_default == RT_NULL)
netif_set_default(ethif->netif); netif_set_default(ethif->netif);
#if LWIP_DHCP #if LWIP_DHCP
if (ethif->flags & NETIF_FLAG_DHCP) if (ethif->flags & NETIF_FLAG_DHCP)
{ {
/* if this interface uses DHCP, start the DHCP client */ /* if this interface uses DHCP, start the DHCP client */
dhcp_start(ethif->netif); dhcp_start(ethif->netif);
} }
else else
#endif #endif
{ {
/* set interface up */ /* set interface up */
netif_set_up(ethif->netif); netif_set_up(ethif->netif);
} }
#ifdef LWIP_NETIF_LINK_CALLBACK #ifdef LWIP_NETIF_LINK_CALLBACK
netif_set_link_up(ethif->netif); netif_set_link_up(ethif->netif);
#endif #endif
return ERR_OK; return ERR_OK;
} }
return ERR_IF; return ERR_IF;
} }
/* Keep old drivers compatible in RT-Thread */ /* Keep old drivers compatible in RT-Thread */
rt_err_t eth_device_init_with_flag(struct eth_device *dev, char *name, rt_uint8_t flags) rt_err_t eth_device_init_with_flag(struct eth_device *dev, char *name, rt_uint8_t flags)
{ {
struct netif* netif; struct netif* netif;
netif = (struct netif*) rt_malloc (sizeof(struct netif)); netif = (struct netif*) rt_malloc (sizeof(struct netif));
if (netif == RT_NULL) if (netif == RT_NULL)
{ {
rt_kprintf("malloc netif failed\n"); rt_kprintf("malloc netif failed\n");
return -RT_ERROR; return -RT_ERROR;
} }
rt_memset(netif, 0, sizeof(struct netif)); rt_memset(netif, 0, sizeof(struct netif));
/* set netif */ /* set netif */
dev->netif = netif; dev->netif = netif;
/* device flags, which will be set to netif flags when initializing */ /* device flags, which will be set to netif flags when initializing */
dev->flags = flags; dev->flags = flags;
/* link changed status of device */ /* link changed status of device */
dev->link_changed = 0x00; dev->link_changed = 0x00;
dev->parent.type = RT_Device_Class_NetIf; dev->parent.type = RT_Device_Class_NetIf;
/* register to RT-Thread device manager */ /* register to RT-Thread device manager */
rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR); rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO); rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
/* set name */ /* set name */
netif->name[0] = name[0]; netif->name[0] = name[0];
netif->name[1] = name[1]; netif->name[1] = name[1];
/* set hw address to 6 */ /* set hw address to 6 */
netif->hwaddr_len = 6; netif->hwaddr_len = 6;
/* maximum transfer unit */ /* maximum transfer unit */
netif->mtu = ETHERNET_MTU; netif->mtu = ETHERNET_MTU;
/* get hardware MAC address */ /* get hardware MAC address */
rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr); rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
/* set output */ /* set output */
netif->output = etharp_output; netif->output = etharp_output;
netif->linkoutput = ethernetif_linkoutput; netif->linkoutput = ethernetif_linkoutput;
/* if tcp thread has been started up, we add this netif to the system */ /* if tcp thread has been started up, we add this netif to the system */
if (rt_thread_find("tcpip") != RT_NULL) if (rt_thread_find("tcpip") != RT_NULL)
{ {
struct ip_addr ipaddr, netmask, gw; struct ip_addr ipaddr, netmask, gw;
#if !LWIP_DHCP #if !LWIP_DHCP
IP4_ADDR(&ipaddr, RT_LWIP_IPADDR0, RT_LWIP_IPADDR1, RT_LWIP_IPADDR2, RT_LWIP_IPADDR3); IP4_ADDR(&ipaddr, RT_LWIP_IPADDR0, RT_LWIP_IPADDR1, RT_LWIP_IPADDR2, RT_LWIP_IPADDR3);
IP4_ADDR(&gw, RT_LWIP_GWADDR0, RT_LWIP_GWADDR1, RT_LWIP_GWADDR2, RT_LWIP_GWADDR3); IP4_ADDR(&gw, RT_LWIP_GWADDR0, RT_LWIP_GWADDR1, RT_LWIP_GWADDR2, RT_LWIP_GWADDR3);
IP4_ADDR(&netmask, RT_LWIP_MSKADDR0, RT_LWIP_MSKADDR1, RT_LWIP_MSKADDR2, RT_LWIP_MSKADDR3); IP4_ADDR(&netmask, RT_LWIP_MSKADDR0, RT_LWIP_MSKADDR1, RT_LWIP_MSKADDR2, RT_LWIP_MSKADDR3);
#else #else
IP4_ADDR(&ipaddr, 0, 0, 0, 0); IP4_ADDR(&ipaddr, 0, 0, 0, 0);
IP4_ADDR(&gw, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0);
IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0);
#endif #endif
netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input); netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
} }
return RT_EOK; return RT_EOK;
} }
rt_err_t eth_device_init(struct eth_device * dev, char *name) rt_err_t eth_device_init(struct eth_device * dev, char *name)
{ {
rt_uint8_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP; rt_uint8_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
#if LWIP_DHCP #if LWIP_DHCP
/* DHCP support */ /* DHCP support */
flags |= NETIF_FLAG_DHCP; flags |= NETIF_FLAG_DHCP;
#endif #endif
#if LWIP_IGMP #if LWIP_IGMP
/* IGMP support */ /* IGMP support */
flags |= NETIF_FLAG_IGMP; flags |= NETIF_FLAG_IGMP;
#endif #endif
return eth_device_init_with_flag(dev, name, flags); return eth_device_init_with_flag(dev, name, flags);
} }
rt_err_t eth_device_ready(struct eth_device* dev) rt_err_t eth_device_ready(struct eth_device* dev)
{ {
if (dev->netif) if (dev->netif)
/* post message to Ethernet thread */ /* post message to Ethernet thread */
return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev); return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
else else
return ERR_OK; /* netif is not initialized yet, just return. */ return ERR_OK; /* netif is not initialized yet, just return. */
} }
rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up) rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
{ {
rt_uint32_t level; rt_uint32_t level;
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
dev->link_changed = 0x01; dev->link_changed = 0x01;
if (up == RT_TRUE) if (up == RT_TRUE)
dev->link_status = 0x01; dev->link_status = 0x01;
else else
dev->link_status = 0x00; dev->link_status = 0x00;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
/* post message to ethernet thread */ /* post message to ethernet thread */
return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev); return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
} }
/* Ethernet Tx Thread */ /* Ethernet Tx Thread */
static void eth_tx_thread_entry(void* parameter) static void eth_tx_thread_entry(void* parameter)
{ {
struct eth_tx_msg* msg; struct eth_tx_msg* msg;
while (1) while (1)
{ {
if (rt_mb_recv(&eth_tx_thread_mb, (rt_uint32_t*)&msg, RT_WAITING_FOREVER) == RT_EOK) if (rt_mb_recv(&eth_tx_thread_mb, (rt_uint32_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
{ {
struct eth_device* enetif; struct eth_device* enetif;
RT_ASSERT(msg->netif != RT_NULL); RT_ASSERT(msg->netif != RT_NULL);
RT_ASSERT(msg->buf != RT_NULL); RT_ASSERT(msg->buf != RT_NULL);
enetif = (struct eth_device*)msg->netif->state; enetif = (struct eth_device*)msg->netif->state;
if (enetif != RT_NULL) if (enetif != RT_NULL)
{ {
/* call driver's interface */ /* call driver's interface */
if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK) if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
{ {
rt_kprintf("transmit eth packet failed\n"); rt_kprintf("transmit eth packet failed\n");
} }
} }
/* send ACK */ /* send ACK */
rt_sem_release(&(enetif->tx_ack)); rt_sem_release(&(enetif->tx_ack));
} }
} }
} }
/* Ethernet Rx Thread */ /* Ethernet Rx Thread */
static void eth_rx_thread_entry(void* parameter) static void eth_rx_thread_entry(void* parameter)
{ {
struct eth_device* device; struct eth_device* device;
while (1) while (1)
{ {
if (rt_mb_recv(&eth_rx_thread_mb, (rt_uint32_t*)&device, RT_WAITING_FOREVER) == RT_EOK) if (rt_mb_recv(&eth_rx_thread_mb, (rt_uint32_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
{ {
struct pbuf *p; struct pbuf *p;
/* check link status */ /* check link status */
if (device->link_changed) if (device->link_changed)
{ {
int status; int status;
rt_uint32_t level; rt_uint32_t level;
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
status = device->link_status; status = device->link_status;
device->link_changed = 0x00; device->link_changed = 0x00;
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
if (status) if (status)
netifapi_netif_set_link_up(device->netif); netifapi_netif_set_link_up(device->netif);
else else
netifapi_netif_set_link_down(device->netif); netifapi_netif_set_link_down(device->netif);
} }
/* receive all of buffer */ /* receive all of buffer */
while (1) while (1)
{ {
p = device->eth_rx(&(device->parent)); p = device->eth_rx(&(device->parent));
if (p != RT_NULL) if (p != RT_NULL)
{ {
/* notify to upper layer */ /* notify to upper layer */
if( device->netif->input(p, device->netif) != ERR_OK ) if( device->netif->input(p, device->netif) != ERR_OK )
{ {
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n")); LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
pbuf_free(p); pbuf_free(p);
p = NULL; p = NULL;
} }
} }
else break; else break;
} }
} }
else else
{ {
LWIP_ASSERT("Should not happen!\n",0); LWIP_ASSERT("Should not happen!\n",0);
} }
} }
} }
void eth_system_device_init() void eth_system_device_init()
{ {
rt_err_t result = RT_EOK; rt_err_t result = RT_EOK;
/* initialize Rx thread. /* initialize Rx thread.
* initialize mailbox and create Ethernet Rx thread */ * initialize mailbox and create Ethernet Rx thread */
result = rt_mb_init(&eth_rx_thread_mb, "erxmb", result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
&eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4, &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
RT_IPC_FLAG_FIFO); RT_IPC_FLAG_FIFO);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL, result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
&eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack), &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
RT_LWIP_ETHTHREAD_PRIORITY, 16); RT_LWIP_ETHTHREAD_PRIORITY, 16);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
result = rt_thread_startup(&eth_rx_thread); result = rt_thread_startup(&eth_rx_thread);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
/* initialize Tx thread */ /* initialize Tx thread */
/* initialize mailbox and create Ethernet Tx thread */ /* initialize mailbox and create Ethernet Tx thread */
result = rt_mb_init(&eth_tx_thread_mb, "etxmb", result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
&eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4, &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
RT_IPC_FLAG_FIFO); RT_IPC_FLAG_FIFO);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL, result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
&eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack), &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
RT_ETHERNETIF_THREAD_PREORITY, 16); RT_ETHERNETIF_THREAD_PREORITY, 16);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
result = rt_thread_startup(&eth_tx_thread); result = rt_thread_startup(&eth_tx_thread);
RT_ASSERT(result == RT_EOK); RT_ASSERT(result == RT_EOK);
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
@ -436,12 +437,12 @@ FINSH_FUNCTION_EXPORT(set_if, set network interface address);
#include <lwip/dns.h> #include <lwip/dns.h>
void set_dns(char* dns_server) void set_dns(char* dns_server)
{ {
struct ip_addr addr; struct ip_addr addr;
if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr)) if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
{ {
dns_setserver(0, &addr); dns_setserver(0, &addr);
} }
} }
FINSH_FUNCTION_EXPORT(set_dns, set DNS server address); FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
#endif #endif
@ -451,11 +452,16 @@ void list_if(void)
rt_ubase_t index; rt_ubase_t index;
struct netif * netif; struct netif * netif;
rt_enter_critical();
netif = netif_list; netif = netif_list;
while( netif != RT_NULL ) while( netif != RT_NULL )
{ {
rt_kprintf("network interface: %c%c%s\n", netif->name[0], netif->name[1], (netif == netif_default)?" (Default)":""); rt_kprintf("network interface: %c%c%s\n",
netif->name[0],
netif->name[1],
(netif == netif_default)?" (Default)":"");
rt_kprintf("MTU: %d\n", netif->mtu); rt_kprintf("MTU: %d\n", netif->mtu);
rt_kprintf("MAC: "); rt_kprintf("MAC: ");
for (index = 0; index < netif->hwaddr_len; index ++) for (index = 0; index < netif->hwaddr_len; index ++)
@ -489,6 +495,8 @@ void list_if(void)
} }
} }
#endif /**< #if LWIP_DNS */ #endif /**< #if LWIP_DNS */
rt_exit_critical();
} }
FINSH_FUNCTION_EXPORT(list_if, list network interface information); FINSH_FUNCTION_EXPORT(list_if, list network interface information);
@ -496,42 +504,62 @@ FINSH_FUNCTION_EXPORT(list_if, list network interface information);
#include <lwip/tcp.h> #include <lwip/tcp.h>
#include <lwip/tcp_impl.h> #include <lwip/tcp_impl.h>
void list_tcps() void list_tcps(void)
{ {
struct tcp_pcb *pcb; rt_uint32_t num = 0;
extern struct tcp_pcb *tcp_active_pcbs; struct tcp_pcb *pcb;
extern union tcp_listen_pcbs_t tcp_listen_pcbs; char local_ip_str[16];
extern struct tcp_pcb *tcp_tw_pcbs; char remote_ip_str[16];
extern const char *tcp_state_str[];
rt_enter_critical(); extern struct tcp_pcb *tcp_active_pcbs;
rt_kprintf("Active PCB states:\n"); extern union tcp_listen_pcbs_t tcp_listen_pcbs;
for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) extern struct tcp_pcb *tcp_tw_pcbs;
{ extern const char *tcp_state_str[];
rt_kprintf("%s:%d <==> %s:%d snd_nxt %d rcv_nxt %d ",
ipaddr_ntoa(&(pcb->local_ip)), pcb->local_port,
ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port,
pcb->snd_nxt, pcb->rcv_nxt);
rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
}
rt_kprintf("Listen PCB states:\n"); rt_enter_critical();
for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next) rt_kprintf("Active PCB states:\n");
{ for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
rt_kprintf("local port %d ", pcb->local_port); {
rt_kprintf("state: %s\n", tcp_state_str[pcb->state]); strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
} strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
rt_kprintf("TIME-WAIT PCB states:\n"); rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next) num++,
{ local_ip_str,
rt_kprintf("%s:%d <==> %s:%d snd_nxt %d rcv_nxt %d ", pcb->local_port,
ipaddr_ntoa(&(pcb->local_ip)), pcb->local_port, remote_ip_str,
ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port, pcb->remote_port,
pcb->snd_nxt, pcb->rcv_nxt); pcb->snd_nxt,
rt_kprintf("state: %s\n", tcp_state_str[pcb->state]); pcb->rcv_nxt);
} rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
rt_exit_critical(); }
rt_kprintf("Listen PCB states:\n");
num = 0;
for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
{
rt_kprintf("#%d local port %d ", num++, pcb->local_port);
rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
}
rt_kprintf("TIME-WAIT PCB states:\n");
num = 0;
for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
{
strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
num++,
local_ip_str,
pcb->local_port,
remote_ip_str,
pcb->remote_port,
pcb->snd_nxt,
pcb->rcv_nxt);
rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
}
rt_exit_critical();
} }
FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections); FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
#endif #endif