ethernetif.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. */
  14. /*
  15. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright notice,
  22. * this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. * 3. The name of the author may not be used to endorse or promote products
  27. * derived from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  32. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  34. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  38. * OF SUCH DAMAGE.
  39. *
  40. * This file is part of the lwIP TCP/IP stack.
  41. *
  42. * Author: Adam Dunkels <adam@sics.se>
  43. *
  44. */
  45. #include <rtthread.h>
  46. #include "lwip/debug.h"
  47. #include "lwip/opt.h"
  48. #include "lwip/def.h"
  49. #include "lwip/mem.h"
  50. #include "lwip/pbuf.h"
  51. #include "lwip/sys.h"
  52. #include "lwip/netif.h"
  53. #include "lwip/stats.h"
  54. #include "lwip/tcpip.h"
  55. #include "netif/etharp.h"
  56. #include "netif/ethernetif.h"
  57. /* eth rx/tx thread */
  58. static struct rt_mailbox eth_rx_thread_mb;
  59. static struct rt_thread eth_rx_thread;
  60. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  61. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  62. static char eth_rx_thread_mb_pool[48 * 4];
  63. static char eth_rx_thread_stack[1024];
  64. #else
  65. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  66. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  67. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  68. #endif
  69. struct eth_tx_msg
  70. {
  71. struct netif *netif;
  72. struct pbuf *buf;
  73. };
  74. static struct rt_mailbox eth_tx_thread_mb;
  75. static struct rt_thread eth_tx_thread;
  76. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  77. static char eth_tx_thread_mb_pool[32 * 4];
  78. static char eth_tx_thread_stack[512];
  79. #else
  80. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  81. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  82. #endif
  83. /* ================================================================================== */
  84. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  85. {
  86. struct eth_tx_msg msg;
  87. struct eth_device* enetif;
  88. enetif = (struct eth_device*)netif->state;
  89. /* send a message to eth tx thread */
  90. msg.netif = netif;
  91. msg.buf = p;
  92. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  93. {
  94. /* waiting for ack */
  95. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  96. }
  97. return ERR_OK;
  98. }
  99. /* Since "ethernetif_init" is called back from netif_add() and we are
  100. unable to get the pointer to the dev which owns this netif, this
  101. global var is used to solve this problem. */
  102. static struct eth_device* eth_dev;
  103. /* the interface provided to LwIP */
  104. static err_t ethernetif_init(struct netif *netif)
  105. {
  106. if( (eth_dev == RT_NULL) || (eth_dev->netif != netif) )
  107. return ERR_MEM;
  108. /* set name */
  109. netif->name[0] = eth_dev->parent.parent.name[0];
  110. netif->name[1] = eth_dev->parent.parent.name[1];
  111. /* set hw address to 6 */
  112. netif->hwaddr_len = 6;
  113. /* maximum transfer unit */
  114. netif->mtu = ETHERNET_MTU;
  115. /* NOTE: the NETIF_FLAG_UP and NETIF_FLAG_LINK_UP flag is not set here.
  116. They should be set by netif_set_up() and netif_set_link_up() automatically */
  117. netif->flags = NETIF_FLAG_BROADCAST |
  118. NETIF_FLAG_ETHARP;
  119. #ifdef LWIP_IGMP
  120. netif->flags |= NETIF_FLAG_IGMP;
  121. #endif
  122. #ifdef LWIP_DHCP
  123. netif->flags |= NETIF_FLAG_DHCP;
  124. #endif
  125. /* get hardware address */
  126. rt_device_control(&(eth_dev->parent), NIOCTL_GADDR, netif->hwaddr);
  127. /* set output */
  128. netif->output = etharp_output;
  129. netif->linkoutput = ethernetif_linkoutput;
  130. return ERR_OK;
  131. }
  132. /* ethernetif APIs */
  133. rt_err_t eth_device_init(struct eth_device* dev, const char* name)
  134. {
  135. struct netif* pnetif;
  136. /* allocate memory */
  137. pnetif = (struct netif*) rt_malloc (sizeof(struct netif));
  138. if (pnetif == RT_NULL)
  139. {
  140. rt_kprintf("malloc netif failed\n");
  141. return -RT_ERROR;
  142. }
  143. rt_memset(pnetif, 0, sizeof(struct netif));
  144. /* set netif */
  145. dev->netif = pnetif;
  146. /* register to rt-thread device manager */
  147. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  148. dev->parent.type = RT_Device_Class_NetIf;
  149. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  150. /* add netif to lwip */
  151. /* NOTE: eth_init will be called back by netif_add, we should put some initialization
  152. code to eth_init(). See include/lwip/netif.h line 97 */
  153. eth_dev = dev;
  154. if (netif_add(pnetif, IP_ADDR_ANY, IP_ADDR_BROADCAST, IP_ADDR_ANY, dev,
  155. ethernetif_init, ethernet_input) == RT_NULL)
  156. {
  157. /* failed, unregister device and free netif */
  158. rt_device_unregister(&(dev->parent));
  159. rt_free(pnetif);
  160. eth_dev = RT_NULL;
  161. return -RT_ERROR;
  162. }
  163. eth_dev = RT_NULL;
  164. netif_set_default(pnetif);
  165. /* We bring up the netif here cause we still don't have a call back function
  166. which indicates the ethernet interface status from the ethernet driver. */
  167. netif_set_up(pnetif);
  168. netif_set_link_up(pnetif);
  169. return RT_EOK;
  170. }
  171. /* ================================================================================== */
  172. /* NOTE: Previous functions are interface for lwip, and below are for rt-thread */
  173. void eth_tx_thread_entry(void* parameter)
  174. {
  175. struct eth_tx_msg* msg;
  176. while (1)
  177. {
  178. if (rt_mb_recv(&eth_tx_thread_mb, (rt_uint32_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
  179. {
  180. struct eth_device* enetif;
  181. RT_ASSERT(msg->netif != RT_NULL);
  182. RT_ASSERT(msg->buf != RT_NULL);
  183. enetif = (struct eth_device*)msg->netif->state;
  184. if (enetif != RT_NULL)
  185. {
  186. /* call driver's interface */
  187. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  188. {
  189. rt_kprintf("transmit eth packet failed\n");
  190. }
  191. }
  192. /* send ack */
  193. rt_sem_release(&(enetif->tx_ack));
  194. }
  195. }
  196. }
  197. /* ethernet buffer */
  198. void eth_rx_thread_entry(void* parameter)
  199. {
  200. struct eth_device* device;
  201. while (1)
  202. {
  203. if (rt_mb_recv(&eth_rx_thread_mb, (rt_uint32_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
  204. {
  205. struct pbuf *p;
  206. /* receive all of buffer */
  207. while (1)
  208. {
  209. p = device->eth_rx(&(device->parent));
  210. if (p != RT_NULL)
  211. {
  212. /* notify to upper layer */
  213. ethernet_input(p, device->netif);
  214. }
  215. else break;
  216. }
  217. }
  218. }
  219. }
  220. rt_err_t eth_device_ready(struct eth_device* dev)
  221. {
  222. /* post message to ethernet thread */
  223. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  224. }
  225. rt_err_t eth_system_device_init()
  226. {
  227. rt_err_t result = RT_EOK;
  228. /* init rx thread */
  229. /* init mailbox and create ethernet thread */
  230. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  231. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  232. RT_IPC_FLAG_FIFO);
  233. RT_ASSERT(result == RT_EOK);
  234. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  235. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  236. RT_ETHERNETIF_THREAD_PREORITY, 16);
  237. RT_ASSERT(result == RT_EOK);
  238. result = rt_thread_startup(&eth_rx_thread);
  239. RT_ASSERT(result == RT_EOK);
  240. /* init tx thread */
  241. /* init mailbox and create ethernet thread */
  242. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  243. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  244. RT_IPC_FLAG_FIFO);
  245. RT_ASSERT(result == RT_EOK);
  246. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  247. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  248. RT_ETHERNETIF_THREAD_PREORITY, 16);
  249. RT_ASSERT(result == RT_EOK);
  250. result = rt_thread_startup(&eth_tx_thread);
  251. RT_ASSERT(result == RT_EOK);
  252. return result;
  253. }
  254. #ifdef RT_USING_FINSH
  255. #include <finsh.h>
  256. #include "ipv4/lwip/inet.h"
  257. void set_if(char* ip_addr, char* gw_addr, char* nm_addr)
  258. {
  259. struct ip_addr *ip;
  260. struct in_addr addr;
  261. ip = (struct ip_addr *)&addr;
  262. /* set ip address */
  263. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  264. {
  265. netif_set_ipaddr(netif_default, ip);
  266. }
  267. /* set gateway address */
  268. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  269. {
  270. netif_set_gw(netif_default, ip);
  271. }
  272. /* set netmask address */
  273. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  274. {
  275. netif_set_netmask(netif_default, ip);
  276. }
  277. }
  278. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  279. #if LWIP_DNS
  280. #include <lwip/dns.h>
  281. void set_dns(char* dns_server)
  282. {
  283. struct in_addr addr;
  284. if ((dns_server != RT_NULL) && inet_aton(dns_server, &addr))
  285. {
  286. dns_setserver(0, (struct ip_addr *)&addr);
  287. }
  288. }
  289. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  290. #endif
  291. void list_if()
  292. {
  293. rt_kprintf("Default network interface: %c%c\n", netif_default->name[0], netif_default->name[1]);
  294. rt_kprintf("ip address: %s\n", inet_ntoa(*((struct in_addr*)&(netif_default->ip_addr))));
  295. rt_kprintf("gw address: %s\n", inet_ntoa(*((struct in_addr*)&(netif_default->gw))));
  296. rt_kprintf("net mask : %s\n", inet_ntoa(*((struct in_addr*)&(netif_default->netmask))));
  297. #if LWIP_DNS
  298. {
  299. struct ip_addr ip_addr;
  300. ip_addr = dns_getserver(0);
  301. rt_kprintf("dns server: %s\n", inet_ntoa(*((struct in_addr*)&(ip_addr))));
  302. }
  303. #endif
  304. }
  305. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  306. #endif