ethernetif.c 27 KB

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