ethernetif.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. #if LWIP_NETIF_HOSTNAME
  411. #define LWIP_HOSTNAME_LEN 16
  412. char *hostname = RT_NULL;
  413. netif = (struct netif*) rt_malloc (sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  414. #else
  415. netif = (struct netif*) rt_malloc (sizeof(struct netif));
  416. #endif
  417. if (netif == RT_NULL)
  418. {
  419. rt_kprintf("malloc netif failed\n");
  420. return -RT_ERROR;
  421. }
  422. rt_memset(netif, 0, sizeof(struct netif));
  423. /* set netif */
  424. dev->netif = netif;
  425. /* device flags, which will be set to netif flags when initializing */
  426. dev->flags = flags;
  427. /* link changed status of device */
  428. dev->link_changed = 0x00;
  429. dev->parent.type = RT_Device_Class_NetIf;
  430. /* register to RT-Thread device manager */
  431. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  432. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  433. /* set name */
  434. netif->name[0] = name[0];
  435. netif->name[1] = name[1];
  436. /* set hw address to 6 */
  437. netif->hwaddr_len = 6;
  438. /* maximum transfer unit */
  439. netif->mtu = ETHERNET_MTU;
  440. /* get hardware MAC address */
  441. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  442. /* set output */
  443. netif->output = etharp_output;
  444. netif->linkoutput = ethernetif_linkoutput;
  445. #if LWIP_NETIF_HOSTNAME
  446. /* Initialize interface hostname */
  447. hostname = (char *)netif + sizeof(struct netif);
  448. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  449. netif->hostname = hostname;
  450. #endif /* LWIP_NETIF_HOSTNAME */
  451. /* if tcp thread has been started up, we add this netif to the system */
  452. if (rt_thread_find("tcpip") != RT_NULL)
  453. {
  454. ip4_addr_t ipaddr, netmask, gw;
  455. #if !LWIP_DHCP
  456. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  457. gw.addr = inet_addr(RT_LWIP_GWADDR);
  458. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  459. #else
  460. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  461. IP4_ADDR(&gw, 0, 0, 0, 0);
  462. IP4_ADDR(&netmask, 0, 0, 0, 0);
  463. #endif
  464. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  465. }
  466. #ifdef RT_USING_NETDEV
  467. /* network interface device flags synchronize */
  468. netdev_flags_sync(netif);
  469. #endif /* RT_USING_NETDEV */
  470. return RT_EOK;
  471. }
  472. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  473. {
  474. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  475. #if LWIP_IGMP
  476. /* IGMP support */
  477. flags |= NETIF_FLAG_IGMP;
  478. #endif
  479. return eth_device_init_with_flag(dev, name, flags);
  480. }
  481. void eth_device_deinit(struct eth_device *dev)
  482. {
  483. struct netif* netif = dev->netif;
  484. #if LWIP_DHCP
  485. dhcp_stop(netif);
  486. dhcp_cleanup(netif);
  487. #endif
  488. netif_set_down(netif);
  489. netif_remove(netif);
  490. #ifdef RT_USING_NETDEV
  491. netdev_del(netif);
  492. #endif
  493. rt_device_close(&(dev->parent));
  494. rt_device_unregister(&(dev->parent));
  495. rt_sem_detach(&(dev->tx_ack));
  496. rt_free(netif);
  497. }
  498. #ifndef LWIP_NO_RX_THREAD
  499. rt_err_t eth_device_ready(struct eth_device* dev)
  500. {
  501. if (dev->netif)
  502. /* post message to Ethernet thread */
  503. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  504. else
  505. return ERR_OK; /* netif is not initialized yet, just return. */
  506. }
  507. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  508. {
  509. rt_uint32_t level;
  510. RT_ASSERT(dev != RT_NULL);
  511. level = rt_hw_interrupt_disable();
  512. dev->link_changed = 0x01;
  513. if (up == RT_TRUE)
  514. dev->link_status = 0x01;
  515. else
  516. dev->link_status = 0x00;
  517. rt_hw_interrupt_enable(level);
  518. /* post message to ethernet thread */
  519. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  520. }
  521. #else
  522. /* NOTE: please not use it in interrupt when no RxThread exist */
  523. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  524. {
  525. if (up == RT_TRUE)
  526. netifapi_netif_set_link_up(dev->netif);
  527. else
  528. netifapi_netif_set_link_down(dev->netif);
  529. return RT_EOK;
  530. }
  531. #endif
  532. #ifndef LWIP_NO_TX_THREAD
  533. /* Ethernet Tx Thread */
  534. static void eth_tx_thread_entry(void* parameter)
  535. {
  536. struct eth_tx_msg* msg;
  537. while (1)
  538. {
  539. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t *)&msg, RT_WAITING_FOREVER) == RT_EOK)
  540. {
  541. struct eth_device* enetif;
  542. RT_ASSERT(msg->netif != RT_NULL);
  543. RT_ASSERT(msg->buf != RT_NULL);
  544. enetif = (struct eth_device*)msg->netif->state;
  545. if (enetif != RT_NULL)
  546. {
  547. /* call driver's interface */
  548. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  549. {
  550. /* transmit eth packet failed */
  551. }
  552. }
  553. /* send ACK */
  554. rt_sem_release(&(enetif->tx_ack));
  555. }
  556. }
  557. }
  558. #endif
  559. #ifndef LWIP_NO_RX_THREAD
  560. /* Ethernet Rx Thread */
  561. static void eth_rx_thread_entry(void* parameter)
  562. {
  563. struct eth_device* device;
  564. while (1)
  565. {
  566. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t *)&device, RT_WAITING_FOREVER) == RT_EOK)
  567. {
  568. struct pbuf *p;
  569. /* check link status */
  570. if (device->link_changed)
  571. {
  572. int status;
  573. rt_uint32_t level;
  574. level = rt_hw_interrupt_disable();
  575. status = device->link_status;
  576. device->link_changed = 0x00;
  577. rt_hw_interrupt_enable(level);
  578. if (status)
  579. netifapi_netif_set_link_up(device->netif);
  580. else
  581. netifapi_netif_set_link_down(device->netif);
  582. }
  583. /* receive all of buffer */
  584. while (1)
  585. {
  586. if(device->eth_rx == RT_NULL) break;
  587. p = device->eth_rx(&(device->parent));
  588. if (p != RT_NULL)
  589. {
  590. /* notify to upper layer */
  591. if( device->netif->input(p, device->netif) != ERR_OK )
  592. {
  593. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  594. pbuf_free(p);
  595. p = NULL;
  596. }
  597. }
  598. else break;
  599. }
  600. }
  601. else
  602. {
  603. LWIP_ASSERT("Should not happen!\n",0);
  604. }
  605. }
  606. }
  607. #endif
  608. /* this function does not need,
  609. * use eth_system_device_init_private()
  610. * call by lwip_system_init().
  611. */
  612. int eth_system_device_init(void)
  613. {
  614. return 0;
  615. }
  616. int eth_system_device_init_private(void)
  617. {
  618. rt_err_t result = RT_EOK;
  619. /* initialize Rx thread. */
  620. #ifndef LWIP_NO_RX_THREAD
  621. /* initialize mailbox and create Ethernet Rx thread */
  622. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  623. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  624. RT_IPC_FLAG_FIFO);
  625. RT_ASSERT(result == RT_EOK);
  626. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  627. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  628. RT_ETHERNETIF_THREAD_PREORITY, 16);
  629. RT_ASSERT(result == RT_EOK);
  630. result = rt_thread_startup(&eth_rx_thread);
  631. RT_ASSERT(result == RT_EOK);
  632. #endif
  633. /* initialize Tx thread */
  634. #ifndef LWIP_NO_TX_THREAD
  635. /* initialize mailbox and create Ethernet Tx thread */
  636. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  637. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  638. RT_IPC_FLAG_FIFO);
  639. RT_ASSERT(result == RT_EOK);
  640. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  641. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  642. RT_ETHERNETIF_THREAD_PREORITY, 16);
  643. RT_ASSERT(result == RT_EOK);
  644. result = rt_thread_startup(&eth_tx_thread);
  645. RT_ASSERT(result == RT_EOK);
  646. #endif
  647. return (int)result;
  648. }
  649. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  650. {
  651. ip4_addr_t *ip;
  652. ip4_addr_t addr;
  653. struct netif * netif = netif_list;
  654. if(strlen(netif_name) > sizeof(netif->name))
  655. {
  656. rt_kprintf("network interface name too long!\r\n");
  657. return;
  658. }
  659. while(netif != RT_NULL)
  660. {
  661. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  662. break;
  663. netif = netif->next;
  664. if( netif == RT_NULL )
  665. {
  666. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  667. return;
  668. }
  669. }
  670. ip = (ip4_addr_t *)&addr;
  671. /* set ip address */
  672. if ((ip_addr != RT_NULL) && ip4addr_aton(ip_addr, &addr))
  673. {
  674. netif_set_ipaddr(netif, ip);
  675. }
  676. /* set gateway address */
  677. if ((gw_addr != RT_NULL) && ip4addr_aton(gw_addr, &addr))
  678. {
  679. netif_set_gw(netif, ip);
  680. }
  681. /* set netmask address */
  682. if ((nm_addr != RT_NULL) && ip4addr_aton(nm_addr, &addr))
  683. {
  684. netif_set_netmask(netif, ip);
  685. }
  686. }
  687. #ifdef RT_USING_FINSH
  688. #include <finsh.h>
  689. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  690. #if LWIP_DNS
  691. #include <lwip/dns.h>
  692. void set_dns(uint8_t dns_num, char* dns_server)
  693. {
  694. ip_addr_t addr;
  695. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  696. {
  697. dns_setserver(dns_num, &addr);
  698. }
  699. }
  700. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  701. #endif
  702. void list_if(void)
  703. {
  704. rt_ubase_t index;
  705. struct netif * netif;
  706. rt_enter_critical();
  707. netif = netif_list;
  708. while( netif != RT_NULL )
  709. {
  710. rt_kprintf("network interface: %c%c%s\n",
  711. netif->name[0],
  712. netif->name[1],
  713. (netif == netif_default)?" (Default)":"");
  714. rt_kprintf("MTU: %d\n", netif->mtu);
  715. rt_kprintf("MAC: ");
  716. for (index = 0; index < netif->hwaddr_len; index ++)
  717. rt_kprintf("%02x ", netif->hwaddr[index]);
  718. rt_kprintf("\nFLAGS:");
  719. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  720. else rt_kprintf(" DOWN");
  721. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  722. else rt_kprintf(" LINK_DOWN");
  723. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  724. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  725. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  726. rt_kprintf("\n");
  727. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  728. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  729. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  730. #if LWIP_IPV6
  731. {
  732. ip6_addr_t *addr;
  733. int addr_state;
  734. int i;
  735. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  736. addr_state = netif->ip6_addr_state[0];
  737. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  738. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  739. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  740. {
  741. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  742. addr_state = netif->ip6_addr_state[i];
  743. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  744. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  745. }
  746. }
  747. rt_kprintf("\r\n");
  748. #endif /* LWIP_IPV6 */
  749. netif = netif->next;
  750. }
  751. #if LWIP_DNS
  752. {
  753. const ip_addr_t *ip_addr;
  754. for(index=0; index<DNS_MAX_SERVERS; index++)
  755. {
  756. ip_addr = dns_getserver(index);
  757. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(ip_addr));
  758. }
  759. }
  760. #endif /**< #if LWIP_DNS */
  761. rt_exit_critical();
  762. }
  763. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  764. #if LWIP_TCP
  765. #include <lwip/tcp.h>
  766. #include <lwip/priv/tcp_priv.h>
  767. void list_tcps(void)
  768. {
  769. rt_uint32_t num = 0;
  770. struct tcp_pcb *pcb;
  771. char local_ip_str[16];
  772. char remote_ip_str[16];
  773. extern struct tcp_pcb *tcp_active_pcbs;
  774. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  775. extern struct tcp_pcb *tcp_tw_pcbs;
  776. rt_enter_critical();
  777. rt_kprintf("Active PCB states:\n");
  778. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  779. {
  780. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  781. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  782. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  783. num++,
  784. local_ip_str,
  785. pcb->local_port,
  786. remote_ip_str,
  787. pcb->remote_port,
  788. pcb->snd_nxt,
  789. pcb->rcv_nxt);
  790. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  791. }
  792. rt_kprintf("Listen PCB states:\n");
  793. num = 0;
  794. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  795. {
  796. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  797. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  798. }
  799. rt_kprintf("TIME-WAIT PCB states:\n");
  800. num = 0;
  801. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  802. {
  803. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  804. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  805. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  806. num++,
  807. local_ip_str,
  808. pcb->local_port,
  809. remote_ip_str,
  810. pcb->remote_port,
  811. pcb->snd_nxt,
  812. pcb->rcv_nxt);
  813. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  814. }
  815. rt_exit_critical();
  816. }
  817. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  818. #endif /* LWIP_TCP */
  819. #if LWIP_UDP
  820. #include "lwip/udp.h"
  821. void list_udps(void)
  822. {
  823. struct udp_pcb *pcb;
  824. rt_uint32_t num = 0;
  825. char local_ip_str[16];
  826. char remote_ip_str[16];
  827. rt_enter_critical();
  828. rt_kprintf("Active UDP PCB states:\n");
  829. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  830. {
  831. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  832. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  833. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  834. num, (int)pcb->flags,
  835. local_ip_str,
  836. pcb->local_port,
  837. remote_ip_str,
  838. pcb->remote_port);
  839. num++;
  840. }
  841. rt_exit_critical();
  842. }
  843. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  844. #endif /* LWIP_UDP */
  845. #endif