ethernetif.c 27 KB

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