ethernetif.c 26 KB

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