ethernetif.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * COPYRIGHT (C) 2006-2010, RT-Thread Development Team
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  22. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  26. * OF SUCH DAMAGE.
  27. *
  28. * This file is part of the lwIP TCP/IP stack.
  29. *
  30. * Author: Adam Dunkels <adam@sics.se>
  31. *
  32. * Change Logs:
  33. * Date Author Notes
  34. * 2010-07-07 Bernard fix send mail to mailbox issue.
  35. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  36. * 2012-04-10 Bernard add more compatible with RT-Thread.
  37. * 2012-11-12 Bernard The network interface can be initialized
  38. * after lwIP initialization.
  39. * 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
  40. */
  41. #include <rtthread.h>
  42. #include "lwip/opt.h"
  43. #include "lwip/debug.h"
  44. #include "lwip/def.h"
  45. #include "lwip/mem.h"
  46. #include "lwip/pbuf.h"
  47. #include "lwip/sys.h"
  48. #include "lwip/netif.h"
  49. #include "lwip/stats.h"
  50. #include "lwip/tcpip.h"
  51. #include "netif/etharp.h"
  52. #include "netif/ethernetif.h"
  53. #include "lwip/inet.h"
  54. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  55. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  56. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  57. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  58. #else
  59. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  60. #endif
  61. #ifndef LWIP_NO_TX_THREAD
  62. /**
  63. * Tx message structure for Ethernet interface
  64. */
  65. struct eth_tx_msg
  66. {
  67. struct netif *netif;
  68. struct pbuf *buf;
  69. };
  70. static struct rt_mailbox eth_tx_thread_mb;
  71. static struct rt_thread eth_tx_thread;
  72. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  73. static char eth_tx_thread_mb_pool[32 * 4];
  74. static char eth_tx_thread_stack[512];
  75. #else
  76. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  77. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  78. #endif
  79. #endif
  80. #ifndef LWIP_NO_RX_THREAD
  81. static struct rt_mailbox eth_rx_thread_mb;
  82. static struct rt_thread eth_rx_thread;
  83. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  84. static char eth_rx_thread_mb_pool[48 * 4];
  85. static char eth_rx_thread_stack[1024];
  86. #else
  87. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  88. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  89. #endif
  90. #endif
  91. #ifdef RT_USING_NETDEV
  92. #include "lwip/ip.h"
  93. #include "lwip/init.h"
  94. #include "lwip/netdb.h"
  95. #include <netdev.h>
  96. static int lwip_netdev_set_up(struct netdev *netif)
  97. {
  98. netif_set_up((struct netif *)netif->user_data);
  99. return ERR_OK;
  100. }
  101. static int lwip_netdev_set_down(struct netdev *netif)
  102. {
  103. netif_set_down((struct netif *)netif->user_data);
  104. return ERR_OK;
  105. }
  106. static int lwip_netdev_set_addr_info(struct netdev *netif, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  107. {
  108. if (ip_addr && netmask && gw)
  109. {
  110. netif_set_addr((struct netif *)netif->user_data, ip_addr, netmask, gw);
  111. }
  112. else
  113. {
  114. if (ip_addr)
  115. {
  116. netif_set_ipaddr((struct netif *)netif->user_data, ip_addr);
  117. }
  118. if (netmask)
  119. {
  120. netif_set_netmask((struct netif *)netif->user_data, netmask);
  121. }
  122. if (gw)
  123. {
  124. netif_set_gw((struct netif *)netif->user_data, gw);
  125. }
  126. }
  127. return ERR_OK;
  128. }
  129. #ifdef RT_LWIP_DNS
  130. static int lwip_netdev_set_dns_server(struct netdev *netif, uint8_t dns_num, ip_addr_t *dns_server)
  131. {
  132. extern void dns_setserver(uint8_t dns_num, ip_addr_t *dns_server);
  133. dns_setserver(dns_num, dns_server);
  134. return ERR_OK;
  135. }
  136. #endif /* RT_LWIP_DNS */
  137. #ifdef RT_LWIP_DHCP
  138. static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
  139. {
  140. netdev_low_level_set_dhcp_status(netif, is_enabled);
  141. return ERR_OK;
  142. }
  143. #endif /* RT_LWIP_DHCP */
  144. #ifdef RT_USING_FINSH
  145. #ifdef RT_LWIP_USING_PING
  146. extern int lwip_ping_recv(int s, int *ttl);
  147. extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
  148. int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
  149. uint32_t timeout, struct netdev_ping_resp *ping_resp)
  150. {
  151. int s, ttl, recv_len, result = 0;
  152. int elapsed_time;
  153. rt_tick_t recv_start_tick;
  154. #if LWIP_VERSION_MAJOR >= 2U
  155. struct timeval recv_timeout = { timeout / RT_TICK_PER_SECOND, timeout % RT_TICK_PER_SECOND };
  156. #else
  157. int recv_timeout = timeout * 1000UL / RT_TICK_PER_SECOND;
  158. #endif
  159. ip_addr_t target_addr;
  160. struct addrinfo hint, *res = RT_NULL;
  161. struct sockaddr_in *h = RT_NULL;
  162. struct in_addr ina;
  163. RT_ASSERT(netif);
  164. RT_ASSERT(host);
  165. RT_ASSERT(ping_resp);
  166. rt_memset(&hint, 0x00, sizeof(hint));
  167. /* convert URL to IP */
  168. if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0)
  169. {
  170. return -RT_ERROR;
  171. }
  172. rt_memcpy(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  173. rt_memcpy(&ina, &h->sin_addr, sizeof(ina));
  174. lwip_freeaddrinfo(res);
  175. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  176. {
  177. return -RT_ERROR;
  178. }
  179. rt_memcpy(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
  180. /* new a socket */
  181. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  182. {
  183. return -RT_ERROR;
  184. }
  185. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
  186. if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
  187. {
  188. recv_start_tick = rt_tick_get();
  189. if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
  190. {
  191. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  192. ping_resp->data_len = recv_len;
  193. ping_resp->ttl = ttl;
  194. ping_resp->ticks = elapsed_time;
  195. }
  196. else
  197. {
  198. result = -RT_ETIMEOUT;
  199. goto __exit;
  200. }
  201. }
  202. else
  203. {
  204. result = -RT_ETIMEOUT;
  205. goto __exit;
  206. }
  207. __exit:
  208. lwip_close(s);
  209. return result;
  210. }
  211. #endif /* RT_LWIP_USING_PING */
  212. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  213. void lwip_netdev_netstat(struct netdev *netif)
  214. {
  215. extern void list_tcps(void);
  216. extern void list_udps(void);
  217. #ifdef RT_LWIP_TCP
  218. list_tcps();
  219. #endif
  220. #ifdef RT_LWIP_UDP
  221. list_udps();
  222. #endif
  223. }
  224. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  225. #endif /* RT_USING_FINSH */
  226. const struct netdev_ops lwip_netdev_ops =
  227. {
  228. lwip_netdev_set_up,
  229. lwip_netdev_set_down,
  230. lwip_netdev_set_addr_info,
  231. #ifdef RT_LWIP_DNS
  232. lwip_netdev_set_dns_server,
  233. #else
  234. NULL,
  235. #endif /* RT_LWIP_DNS */
  236. #ifdef RT_LWIP_DHCP
  237. lwip_netdev_set_dhcp,
  238. #else
  239. NULL,
  240. #endif /* RT_LWIP_DHCP */
  241. #ifdef RT_USING_FINSH
  242. #ifdef RT_LWIP_USING_PING
  243. lwip_netdev_ping,
  244. #else
  245. NULL,
  246. #endif /* RT_LWIP_USING_PING */
  247. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  248. lwip_netdev_netstat,
  249. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  250. #endif /* RT_USING_FINSH */
  251. };
  252. static int netdev_add(struct netif *lwip_netif)
  253. {
  254. #define LWIP_NETIF_NAME_LEN 2
  255. int result = 0;
  256. struct netdev *netdev = RT_NULL;
  257. char name[LWIP_NETIF_NAME_LEN + 1] = {0};
  258. RT_ASSERT(lwip_netif);
  259. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  260. if (netdev == RT_NULL)
  261. {
  262. return -ERR_IF;
  263. }
  264. #ifdef SAL_USING_LWIP
  265. extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev);
  266. /* set the lwIP network interface device protocol family information */
  267. sal_lwip_netdev_set_pf_info(netdev);
  268. #endif /* SAL_USING_LWIP */
  269. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  270. result = netdev_register(netdev, name, (void *)lwip_netif);
  271. /* Update netdev info after registered */
  272. netdev->flags = lwip_netif->flags;
  273. netdev->mtu = lwip_netif->mtu;
  274. netdev->ops = &lwip_netdev_ops;
  275. netdev->hwaddr_len = lwip_netif->hwaddr_len;
  276. rt_memcpy(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len);
  277. netdev->ip_addr = lwip_netif->ip_addr;
  278. netdev->gw = lwip_netif->gw;
  279. netdev->netmask = lwip_netif->netmask;
  280. #ifdef RT_LWIP_DHCP
  281. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  282. #endif
  283. return result;
  284. }
  285. /* synchronize lwIP network interface device and network interface device flags */
  286. static int netdev_flags_sync(struct netif *lwip_netif)
  287. {
  288. struct netdev *netdev = NULL;
  289. RT_ASSERT(lwip_netif);
  290. netdev = netdev_get_by_name(lwip_netif->name);
  291. if (netdev == RT_NULL)
  292. {
  293. return -ERR_IF;
  294. }
  295. netdev->flags |= lwip_netif->flags;
  296. return ERR_OK;
  297. }
  298. #endif /* RT_USING_NETDEV */
  299. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  300. {
  301. #ifndef LWIP_NO_TX_THREAD
  302. struct eth_tx_msg msg;
  303. struct eth_device* enetif;
  304. RT_ASSERT(netif != RT_NULL);
  305. enetif = (struct eth_device*)netif->state;
  306. /* send a message to eth tx thread */
  307. msg.netif = netif;
  308. msg.buf = p;
  309. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  310. {
  311. /* waiting for ack */
  312. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  313. }
  314. #else
  315. struct eth_device* enetif;
  316. RT_ASSERT(netif != RT_NULL);
  317. enetif = (struct eth_device*)netif->state;
  318. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  319. {
  320. return ERR_IF;
  321. }
  322. #endif
  323. return ERR_OK;
  324. }
  325. static err_t eth_netif_device_init(struct netif *netif)
  326. {
  327. struct eth_device *ethif;
  328. ethif = (struct eth_device*)netif->state;
  329. if (ethif != RT_NULL)
  330. {
  331. rt_device_t device;
  332. #ifdef RT_USING_NETDEV
  333. /* network interface device register */
  334. netdev_add(netif);
  335. #endif /* RT_USING_NETDEV */
  336. /* get device object */
  337. device = (rt_device_t) ethif;
  338. if (rt_device_init(device) != RT_EOK)
  339. {
  340. return ERR_IF;
  341. }
  342. /* copy device flags to netif flags */
  343. netif->flags = (ethif->flags & 0xff);
  344. /* set default netif */
  345. if (netif_default == RT_NULL)
  346. netif_set_default(ethif->netif);
  347. #if LWIP_DHCP
  348. if (ethif->flags & NETIF_FLAG_DHCP)
  349. {
  350. /* if this interface uses DHCP, start the DHCP client */
  351. dhcp_start(ethif->netif);
  352. }
  353. else
  354. #endif
  355. {
  356. /* set interface up */
  357. netif_set_up(ethif->netif);
  358. }
  359. if (!(ethif->flags & ETHIF_LINK_PHYUP))
  360. {
  361. /* set link_up for this netif */
  362. netif_set_link_up(ethif->netif);
  363. }
  364. return ERR_OK;
  365. }
  366. return ERR_IF;
  367. }
  368. /* Keep old drivers compatible in RT-Thread */
  369. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  370. {
  371. struct netif* netif;
  372. netif = (struct netif*) rt_malloc (sizeof(struct netif));
  373. if (netif == RT_NULL)
  374. {
  375. rt_kprintf("malloc netif failed\n");
  376. return -RT_ERROR;
  377. }
  378. rt_memset(netif, 0, sizeof(struct netif));
  379. /* set netif */
  380. dev->netif = netif;
  381. /* device flags, which will be set to netif flags when initializing */
  382. dev->flags = flags;
  383. /* link changed status of device */
  384. dev->link_changed = 0x00;
  385. dev->parent.type = RT_Device_Class_NetIf;
  386. /* register to RT-Thread device manager */
  387. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  388. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  389. /* set name */
  390. netif->name[0] = name[0];
  391. netif->name[1] = name[1];
  392. /* set hw address to 6 */
  393. netif->hwaddr_len = 6;
  394. /* maximum transfer unit */
  395. netif->mtu = ETHERNET_MTU;
  396. /* get hardware MAC address */
  397. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  398. /* set output */
  399. netif->output = etharp_output;
  400. netif->linkoutput = ethernetif_linkoutput;
  401. /* if tcp thread has been started up, we add this netif to the system */
  402. if (rt_thread_find("tcpip") != RT_NULL)
  403. {
  404. struct ip_addr ipaddr, netmask, gw;
  405. #if LWIP_DHCP
  406. if (dev->flags & NETIF_FLAG_DHCP)
  407. {
  408. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  409. IP4_ADDR(&gw, 0, 0, 0, 0);
  410. IP4_ADDR(&netmask, 0, 0, 0, 0);
  411. }
  412. else
  413. #endif
  414. {
  415. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  416. gw.addr = inet_addr(RT_LWIP_GWADDR);
  417. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  418. }
  419. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  420. }
  421. #ifdef RT_USING_NETDEV
  422. /* network interface device flags synchronize */
  423. netdev_flags_sync(netif);
  424. #endif /* RT_USING_NETDEV */
  425. return RT_EOK;
  426. }
  427. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  428. {
  429. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  430. #if LWIP_DHCP
  431. /* DHCP support */
  432. flags |= NETIF_FLAG_DHCP;
  433. #endif
  434. #if LWIP_IGMP
  435. /* IGMP support */
  436. flags |= NETIF_FLAG_IGMP;
  437. #endif
  438. return eth_device_init_with_flag(dev, name, flags);
  439. }
  440. #ifndef LWIP_NO_RX_THREAD
  441. rt_err_t eth_device_ready(struct eth_device* dev)
  442. {
  443. if (dev->netif)
  444. /* post message to Ethernet thread */
  445. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  446. else
  447. return ERR_OK; /* netif is not initialized yet, just return. */
  448. }
  449. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  450. {
  451. rt_uint32_t level;
  452. RT_ASSERT(dev != RT_NULL);
  453. level = rt_hw_interrupt_disable();
  454. dev->link_changed = 0x01;
  455. if (up == RT_TRUE)
  456. dev->link_status = 0x01;
  457. else
  458. dev->link_status = 0x00;
  459. rt_hw_interrupt_enable(level);
  460. /* post message to ethernet thread */
  461. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  462. }
  463. #else
  464. /* NOTE: please not use it in interrupt when no RxThread exist */
  465. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  466. {
  467. if (up == RT_TRUE)
  468. netifapi_netif_set_link_up(dev->netif);
  469. else
  470. netifapi_netif_set_link_down(dev->netif);
  471. return RT_EOK;
  472. }
  473. #endif
  474. #ifndef LWIP_NO_TX_THREAD
  475. /* Ethernet Tx Thread */
  476. static void eth_tx_thread_entry(void* parameter)
  477. {
  478. struct eth_tx_msg* msg;
  479. while (1)
  480. {
  481. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
  482. {
  483. struct eth_device* enetif;
  484. RT_ASSERT(msg->netif != RT_NULL);
  485. RT_ASSERT(msg->buf != RT_NULL);
  486. enetif = (struct eth_device*)msg->netif->state;
  487. if (enetif != RT_NULL)
  488. {
  489. /* call driver's interface */
  490. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  491. {
  492. /* transmit eth packet failed */
  493. }
  494. }
  495. /* send ACK */
  496. rt_sem_release(&(enetif->tx_ack));
  497. }
  498. }
  499. }
  500. #endif
  501. #ifndef LWIP_NO_RX_THREAD
  502. /* Ethernet Rx Thread */
  503. static void eth_rx_thread_entry(void* parameter)
  504. {
  505. struct eth_device* device;
  506. while (1)
  507. {
  508. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
  509. {
  510. struct pbuf *p;
  511. /* check link status */
  512. if (device->link_changed)
  513. {
  514. int status;
  515. rt_uint32_t level;
  516. level = rt_hw_interrupt_disable();
  517. status = device->link_status;
  518. device->link_changed = 0x00;
  519. rt_hw_interrupt_enable(level);
  520. if (status)
  521. netifapi_netif_set_link_up(device->netif);
  522. else
  523. netifapi_netif_set_link_down(device->netif);
  524. }
  525. /* receive all of buffer */
  526. while (1)
  527. {
  528. if(device->eth_rx == RT_NULL) break;
  529. p = device->eth_rx(&(device->parent));
  530. if (p != RT_NULL)
  531. {
  532. /* notify to upper layer */
  533. if( device->netif->input(p, device->netif) != ERR_OK )
  534. {
  535. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  536. pbuf_free(p);
  537. p = NULL;
  538. }
  539. }
  540. else break;
  541. }
  542. }
  543. else
  544. {
  545. LWIP_ASSERT("Should not happen!\n",0);
  546. }
  547. }
  548. }
  549. #endif
  550. int eth_system_device_init(void)
  551. {
  552. rt_err_t result = RT_EOK;
  553. /* initialize Rx thread. */
  554. #ifndef LWIP_NO_RX_THREAD
  555. /* initialize mailbox and create Ethernet Rx thread */
  556. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  557. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  558. RT_IPC_FLAG_FIFO);
  559. RT_ASSERT(result == RT_EOK);
  560. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  561. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  562. RT_ETHERNETIF_THREAD_PREORITY, 16);
  563. RT_ASSERT(result == RT_EOK);
  564. result = rt_thread_startup(&eth_rx_thread);
  565. RT_ASSERT(result == RT_EOK);
  566. #endif
  567. /* initialize Tx thread */
  568. #ifndef LWIP_NO_TX_THREAD
  569. /* initialize mailbox and create Ethernet Tx thread */
  570. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  571. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  572. RT_IPC_FLAG_FIFO);
  573. RT_ASSERT(result == RT_EOK);
  574. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  575. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  576. RT_ETHERNETIF_THREAD_PREORITY, 16);
  577. RT_ASSERT(result == RT_EOK);
  578. result = rt_thread_startup(&eth_tx_thread);
  579. RT_ASSERT(result == RT_EOK);
  580. #endif
  581. return (int)result;
  582. }
  583. INIT_PREV_EXPORT(eth_system_device_init);
  584. #ifdef RT_USING_FINSH
  585. #include <finsh.h>
  586. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  587. {
  588. struct ip_addr *ip;
  589. struct ip_addr addr;
  590. struct netif * netif = netif_list;
  591. if(strlen(netif_name) > sizeof(netif->name))
  592. {
  593. rt_kprintf("network interface name too long!\r\n");
  594. return;
  595. }
  596. while(netif != RT_NULL)
  597. {
  598. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  599. break;
  600. netif = netif->next;
  601. if( netif == RT_NULL )
  602. {
  603. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  604. return;
  605. }
  606. }
  607. ip = (struct ip_addr *)&addr;
  608. /* set ip address */
  609. if ((ip_addr != RT_NULL) && ipaddr_aton(ip_addr, &addr))
  610. {
  611. netif_set_ipaddr(netif, ip);
  612. }
  613. /* set gateway address */
  614. if ((gw_addr != RT_NULL) && ipaddr_aton(gw_addr, &addr))
  615. {
  616. netif_set_gw(netif, ip);
  617. }
  618. /* set netmask address */
  619. if ((nm_addr != RT_NULL) && ipaddr_aton(nm_addr, &addr))
  620. {
  621. netif_set_netmask(netif, ip);
  622. }
  623. }
  624. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  625. #if LWIP_DNS
  626. #include <lwip/dns.h>
  627. void set_dns(uint8_t dns_num, char* dns_server)
  628. {
  629. struct ip_addr addr;
  630. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  631. {
  632. dns_setserver(dns_num, &addr);
  633. }
  634. }
  635. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  636. #endif
  637. void list_if(void)
  638. {
  639. rt_ubase_t index;
  640. struct netif * netif;
  641. rt_enter_critical();
  642. netif = netif_list;
  643. while( netif != RT_NULL )
  644. {
  645. rt_kprintf("network interface: %c%c%s\n",
  646. netif->name[0],
  647. netif->name[1],
  648. (netif == netif_default)?" (Default)":"");
  649. rt_kprintf("MTU: %d\n", netif->mtu);
  650. rt_kprintf("MAC: ");
  651. for (index = 0; index < netif->hwaddr_len; index ++)
  652. rt_kprintf("%02x ", netif->hwaddr[index]);
  653. rt_kprintf("\nFLAGS:");
  654. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  655. else rt_kprintf(" DOWN");
  656. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  657. else rt_kprintf(" LINK_DOWN");
  658. if (netif->flags & NETIF_FLAG_DHCP) rt_kprintf(" DHCP");
  659. if (netif->flags & NETIF_FLAG_POINTTOPOINT) rt_kprintf(" PPP");
  660. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  661. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  662. rt_kprintf("\n");
  663. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  664. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  665. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  666. rt_kprintf("\r\n");
  667. netif = netif->next;
  668. }
  669. #if LWIP_DNS
  670. {
  671. struct ip_addr ip_addr;
  672. for(index=0; index<DNS_MAX_SERVERS; index++)
  673. {
  674. ip_addr = dns_getserver(index);
  675. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  676. }
  677. }
  678. #endif /**< #if LWIP_DNS */
  679. rt_exit_critical();
  680. }
  681. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  682. #if LWIP_TCP
  683. #include <lwip/tcp.h>
  684. #include <lwip/tcp_impl.h>
  685. void list_tcps(void)
  686. {
  687. rt_uint32_t num = 0;
  688. struct tcp_pcb *pcb;
  689. char local_ip_str[16];
  690. char remote_ip_str[16];
  691. extern struct tcp_pcb *tcp_active_pcbs;
  692. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  693. extern struct tcp_pcb *tcp_tw_pcbs;
  694. extern const char *tcp_state_str[];
  695. rt_enter_critical();
  696. rt_kprintf("Active PCB states:\n");
  697. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  698. {
  699. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  700. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  701. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  702. num++,
  703. local_ip_str,
  704. pcb->local_port,
  705. remote_ip_str,
  706. pcb->remote_port,
  707. pcb->snd_nxt,
  708. pcb->rcv_nxt);
  709. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  710. }
  711. rt_kprintf("Listen PCB states:\n");
  712. num = 0;
  713. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  714. {
  715. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  716. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  717. }
  718. rt_kprintf("TIME-WAIT PCB states:\n");
  719. num = 0;
  720. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  721. {
  722. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  723. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  724. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  725. num++,
  726. local_ip_str,
  727. pcb->local_port,
  728. remote_ip_str,
  729. pcb->remote_port,
  730. pcb->snd_nxt,
  731. pcb->rcv_nxt);
  732. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  733. }
  734. rt_exit_critical();
  735. }
  736. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  737. #endif
  738. #if LWIP_UDP
  739. #include "lwip/udp.h"
  740. void list_udps(void)
  741. {
  742. struct udp_pcb *pcb;
  743. rt_uint32_t num = 0;
  744. char local_ip_str[16];
  745. char remote_ip_str[16];
  746. rt_enter_critical();
  747. rt_kprintf("Active UDP PCB states:\n");
  748. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  749. {
  750. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  751. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  752. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  753. num, (int)pcb->flags,
  754. local_ip_str,
  755. pcb->local_port,
  756. remote_ip_str,
  757. pcb->remote_port);
  758. num++;
  759. }
  760. rt_exit_critical();
  761. }
  762. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  763. #endif /* LWIP_UDP */
  764. #endif