ethernetif.c 27 KB

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