ethernetif.c 26 KB

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