ethernetif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * File : ethernetif.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-07-07 Bernard fix send mail to mailbox issue.
  13. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  14. * 2012-04-10 Bernard add more compatible with RT-Thread.
  15. * 2012-11-12 Bernard The network interface can be initialized
  16. * after lwIP initialization.
  17. */
  18. /*
  19. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source and binary forms, with or without modification,
  23. * are permitted provided that the following conditions are met:
  24. *
  25. * 1. Redistributions of source code must retain the above copyright notice,
  26. * this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright notice,
  28. * this list of conditions and the following disclaimer in the documentation
  29. * and/or other materials provided with the distribution.
  30. * 3. The name of the author may not be used to endorse or promote products
  31. * derived from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  34. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  35. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  36. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  37. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  38. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  41. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  42. * OF SUCH DAMAGE.
  43. *
  44. * This file is part of the lwIP TCP/IP stack.
  45. *
  46. * Author: Adam Dunkels <adam@sics.se>
  47. *
  48. */
  49. #include <rtthread.h>
  50. #include "lwip/opt.h"
  51. #include "lwip/debug.h"
  52. #include "lwip/def.h"
  53. #include "lwip/mem.h"
  54. #include "lwip/pbuf.h"
  55. #include "lwip/sys.h"
  56. #include "lwip/netif.h"
  57. #include "lwip/stats.h"
  58. #include "lwip/tcpip.h"
  59. #include "netif/etharp.h"
  60. #include "netif/ethernetif.h"
  61. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  62. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  63. /**
  64. * Tx message structure for Ethernet interface
  65. */
  66. struct eth_tx_msg
  67. {
  68. struct netif *netif;
  69. struct pbuf *buf;
  70. };
  71. static struct rt_mailbox eth_tx_thread_mb;
  72. static struct rt_thread eth_tx_thread;
  73. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  74. static char eth_tx_thread_mb_pool[32 * 4];
  75. static char eth_tx_thread_stack[512];
  76. #else
  77. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  78. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  79. #endif
  80. static struct rt_mailbox eth_rx_thread_mb;
  81. static struct rt_thread eth_rx_thread;
  82. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  83. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  84. static char eth_rx_thread_mb_pool[48 * 4];
  85. static char eth_rx_thread_stack[1024];
  86. #else
  87. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  88. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  89. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  90. #endif
  91. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  92. {
  93. struct eth_tx_msg msg;
  94. struct eth_device* enetif;
  95. enetif = (struct eth_device*)netif->state;
  96. /* send a message to eth tx thread */
  97. msg.netif = netif;
  98. msg.buf = p;
  99. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  100. {
  101. /* waiting for ack */
  102. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  103. }
  104. return ERR_OK;
  105. }
  106. static err_t eth_netif_device_init(struct netif *netif)
  107. {
  108. struct eth_device *ethif;
  109. ethif = (struct eth_device*)netif->state;
  110. if (ethif != RT_NULL)
  111. {
  112. rt_device_t device;
  113. /* get device object */
  114. device = (rt_device_t) ethif;
  115. if (rt_device_init(device) != RT_EOK)
  116. {
  117. return ERR_IF;
  118. }
  119. /* copy device flags to netif flags */
  120. netif->flags = ethif->flags;
  121. /* set default netif */
  122. if (netif_default == RT_NULL)
  123. netif_set_default(ethif->netif);
  124. #if LWIP_DHCP
  125. if (ethif->flags & NETIF_FLAG_DHCP)
  126. {
  127. /* if this interface uses DHCP, start the DHCP client */
  128. dhcp_start(ethif->netif);
  129. }
  130. else
  131. #endif
  132. {
  133. /* set interface up */
  134. netif_set_up(ethif->netif);
  135. }
  136. #ifdef LWIP_NETIF_LINK_CALLBACK
  137. netif_set_link_up(ethif->netif);
  138. #endif
  139. return ERR_OK;
  140. }
  141. return ERR_IF;
  142. }
  143. /* Keep old drivers compatible in RT-Thread */
  144. rt_err_t eth_device_init_with_flag(struct eth_device *dev, char *name, rt_uint8_t flags)
  145. {
  146. struct netif* netif;
  147. netif = (struct netif*) rt_malloc (sizeof(struct netif));
  148. if (netif == RT_NULL)
  149. {
  150. rt_kprintf("malloc netif failed\n");
  151. return -RT_ERROR;
  152. }
  153. rt_memset(netif, 0, sizeof(struct netif));
  154. /* set netif */
  155. dev->netif = netif;
  156. /* device flags, which will be set to netif flags when initializing */
  157. dev->flags = flags;
  158. /* link changed status of device */
  159. dev->link_changed = 0x00;
  160. dev->parent.type = RT_Device_Class_NetIf;
  161. /* register to RT-Thread device manager */
  162. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  163. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  164. /* set name */
  165. netif->name[0] = name[0];
  166. netif->name[1] = name[1];
  167. /* set hw address to 6 */
  168. netif->hwaddr_len = 6;
  169. /* maximum transfer unit */
  170. netif->mtu = ETHERNET_MTU;
  171. /* get hardware MAC address */
  172. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  173. /* set output */
  174. netif->output = etharp_output;
  175. netif->linkoutput = ethernetif_linkoutput;
  176. /* if tcp thread has been started up, we add this netif to the system */
  177. if (rt_thread_find("tcpip") != RT_NULL)
  178. {
  179. struct ip_addr ipaddr, netmask, gw;
  180. #if !LWIP_DHCP
  181. IP4_ADDR(&ipaddr, RT_LWIP_IPADDR0, RT_LWIP_IPADDR1, RT_LWIP_IPADDR2, RT_LWIP_IPADDR3);
  182. IP4_ADDR(&gw, RT_LWIP_GWADDR0, RT_LWIP_GWADDR1, RT_LWIP_GWADDR2, RT_LWIP_GWADDR3);
  183. IP4_ADDR(&netmask, RT_LWIP_MSKADDR0, RT_LWIP_MSKADDR1, RT_LWIP_MSKADDR2, RT_LWIP_MSKADDR3);
  184. #else
  185. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  186. IP4_ADDR(&gw, 0, 0, 0, 0);
  187. IP4_ADDR(&netmask, 0, 0, 0, 0);
  188. #endif
  189. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  190. }
  191. return RT_EOK;
  192. }
  193. rt_err_t eth_device_init(struct eth_device * dev, char *name)
  194. {
  195. rt_uint8_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  196. #if LWIP_DHCP
  197. /* DHCP support */
  198. flags |= NETIF_FLAG_DHCP;
  199. #endif
  200. #if LWIP_IGMP
  201. /* IGMP support */
  202. flags |= NETIF_FLAG_IGMP;
  203. #endif
  204. return eth_device_init_with_flag(dev, name, flags);
  205. }
  206. rt_err_t eth_device_ready(struct eth_device* dev)
  207. {
  208. if (dev->netif)
  209. /* post message to Ethernet thread */
  210. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  211. else
  212. return ERR_OK; /* netif is not initialized yet, just return. */
  213. }
  214. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  215. {
  216. rt_uint32_t level;
  217. RT_ASSERT(dev != RT_NULL);
  218. level = rt_hw_interrupt_disable();
  219. dev->link_changed = 0x01;
  220. if (up == RT_TRUE)
  221. dev->link_status = 0x01;
  222. else
  223. dev->link_status = 0x00;
  224. rt_hw_interrupt_enable(level);
  225. /* post message to ethernet thread */
  226. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  227. }
  228. /* Ethernet Tx Thread */
  229. static void eth_tx_thread_entry(void* parameter)
  230. {
  231. struct eth_tx_msg* msg;
  232. while (1)
  233. {
  234. if (rt_mb_recv(&eth_tx_thread_mb, (rt_uint32_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
  235. {
  236. struct eth_device* enetif;
  237. RT_ASSERT(msg->netif != RT_NULL);
  238. RT_ASSERT(msg->buf != RT_NULL);
  239. enetif = (struct eth_device*)msg->netif->state;
  240. if (enetif != RT_NULL)
  241. {
  242. /* call driver's interface */
  243. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  244. {
  245. rt_kprintf("transmit eth packet failed\n");
  246. }
  247. }
  248. /* send ACK */
  249. rt_sem_release(&(enetif->tx_ack));
  250. }
  251. }
  252. }
  253. /* Ethernet Rx Thread */
  254. static void eth_rx_thread_entry(void* parameter)
  255. {
  256. struct eth_device* device;
  257. while (1)
  258. {
  259. if (rt_mb_recv(&eth_rx_thread_mb, (rt_uint32_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
  260. {
  261. struct pbuf *p;
  262. /* check link status */
  263. if (device->link_changed)
  264. {
  265. int status;
  266. rt_uint32_t level;
  267. level = rt_hw_interrupt_disable();
  268. status = device->link_status;
  269. device->link_changed = 0x00;
  270. rt_hw_interrupt_enable(level);
  271. if (status)
  272. netifapi_netif_set_link_up(device->netif);
  273. else
  274. netifapi_netif_set_link_down(device->netif);
  275. }
  276. /* receive all of buffer */
  277. while (1)
  278. {
  279. p = device->eth_rx(&(device->parent));
  280. if (p != RT_NULL)
  281. {
  282. /* notify to upper layer */
  283. if( device->netif->input(p, device->netif) != ERR_OK )
  284. {
  285. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  286. pbuf_free(p);
  287. p = NULL;
  288. }
  289. }
  290. else break;
  291. }
  292. }
  293. else
  294. {
  295. LWIP_ASSERT("Should not happen!\n",0);
  296. }
  297. }
  298. }
  299. void eth_system_device_init()
  300. {
  301. rt_err_t result = RT_EOK;
  302. /* initialize Rx thread.
  303. * initialize mailbox and create Ethernet Rx thread */
  304. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  305. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  306. RT_IPC_FLAG_FIFO);
  307. RT_ASSERT(result == RT_EOK);
  308. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  309. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  310. RT_LWIP_ETHTHREAD_PRIORITY, 16);
  311. RT_ASSERT(result == RT_EOK);
  312. result = rt_thread_startup(&eth_rx_thread);
  313. RT_ASSERT(result == RT_EOK);
  314. /* initialize Tx thread */
  315. /* initialize mailbox and create Ethernet Tx thread */
  316. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  317. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  318. RT_IPC_FLAG_FIFO);
  319. RT_ASSERT(result == RT_EOK);
  320. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  321. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  322. RT_ETHERNETIF_THREAD_PREORITY, 16);
  323. RT_ASSERT(result == RT_EOK);
  324. result = rt_thread_startup(&eth_tx_thread);
  325. RT_ASSERT(result == RT_EOK);
  326. }
  327. #ifdef RT_USING_FINSH
  328. #include <finsh.h>
  329. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  330. {
  331. struct ip_addr *ip;
  332. struct ip_addr addr;
  333. struct netif * netif = netif_list;
  334. if(strlen(netif_name) > sizeof(netif->name))
  335. {
  336. rt_kprintf("network interface name too long!\r\n");
  337. return;
  338. }
  339. while(netif != RT_NULL)
  340. {
  341. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  342. break;
  343. netif = netif->next;
  344. if( netif == RT_NULL )
  345. {
  346. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  347. return;
  348. }
  349. }
  350. ip = (struct ip_addr *)&addr;
  351. /* set ip address */
  352. if ((ip_addr != RT_NULL) && ipaddr_aton(ip_addr, &addr))
  353. {
  354. netif_set_ipaddr(netif, ip);
  355. }
  356. /* set gateway address */
  357. if ((gw_addr != RT_NULL) && ipaddr_aton(gw_addr, &addr))
  358. {
  359. netif_set_gw(netif, ip);
  360. }
  361. /* set netmask address */
  362. if ((nm_addr != RT_NULL) && ipaddr_aton(nm_addr, &addr))
  363. {
  364. netif_set_netmask(netif, ip);
  365. }
  366. }
  367. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  368. #if LWIP_DNS
  369. #include <lwip/dns.h>
  370. void set_dns(char* dns_server)
  371. {
  372. struct ip_addr addr;
  373. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  374. {
  375. dns_setserver(0, &addr);
  376. }
  377. }
  378. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  379. #endif
  380. void list_if(void)
  381. {
  382. rt_ubase_t index;
  383. struct netif * netif;
  384. netif = netif_list;
  385. while( netif != RT_NULL )
  386. {
  387. rt_kprintf("network interface: %c%c%s\n", netif->name[0], netif->name[1], (netif == netif_default)?" (Default)":"");
  388. rt_kprintf("MTU: %d\n", netif->mtu);
  389. rt_kprintf("MAC: ");
  390. for (index = 0; index < netif->hwaddr_len; index ++)
  391. rt_kprintf("%02x ", netif->hwaddr[index]);
  392. rt_kprintf("\nFLAGS:");
  393. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  394. else rt_kprintf(" DOWN");
  395. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  396. else rt_kprintf(" LINK_DOWN");
  397. if (netif->flags & NETIF_FLAG_DHCP) rt_kprintf(" DHCP");
  398. if (netif->flags & NETIF_FLAG_POINTTOPOINT) rt_kprintf(" PPP");
  399. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  400. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  401. rt_kprintf("\n");
  402. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  403. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  404. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  405. rt_kprintf("\r\n");
  406. netif = netif->next;
  407. }
  408. #if LWIP_DNS
  409. {
  410. struct ip_addr ip_addr;
  411. for(index=0; index<DNS_MAX_SERVERS; index++)
  412. {
  413. ip_addr = dns_getserver(index);
  414. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  415. }
  416. }
  417. #endif /**< #if LWIP_DNS */
  418. }
  419. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  420. #if LWIP_TCP
  421. #include <lwip/tcp.h>
  422. #include <lwip/tcp_impl.h>
  423. void list_tcps()
  424. {
  425. struct tcp_pcb *pcb;
  426. extern struct tcp_pcb *tcp_active_pcbs;
  427. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  428. extern struct tcp_pcb *tcp_tw_pcbs;
  429. extern const char *tcp_state_str[];
  430. rt_enter_critical();
  431. rt_kprintf("Active PCB states:\n");
  432. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  433. {
  434. rt_kprintf("%s:%d <==> %s:%d snd_nxt %d rcv_nxt %d ",
  435. ipaddr_ntoa(&(pcb->local_ip)), pcb->local_port,
  436. ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port,
  437. pcb->snd_nxt, pcb->rcv_nxt);
  438. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  439. }
  440. rt_kprintf("Listen PCB states:\n");
  441. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  442. {
  443. rt_kprintf("local port %d ", pcb->local_port);
  444. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  445. }
  446. rt_kprintf("TIME-WAIT PCB states:\n");
  447. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  448. {
  449. rt_kprintf("%s:%d <==> %s:%d snd_nxt %d rcv_nxt %d ",
  450. ipaddr_ntoa(&(pcb->local_ip)), pcb->local_port,
  451. ipaddr_ntoa(&(pcb->remote_ip)), pcb->remote_port,
  452. pcb->snd_nxt, pcb->rcv_nxt);
  453. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  454. }
  455. rt_exit_critical();
  456. }
  457. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  458. #endif
  459. #endif