1
0

ethernetif.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-07-07 Bernard fix send mail to mailbox issue.
  9. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  10. * 2012-04-10 Bernard add more compatible with RT-Thread.
  11. * 2012-11-12 Bernard The network interface can be initialized
  12. * after lwIP initialization.
  13. * 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
  14. * 2016-08-18 Bernard port to lwIP 2.0.0
  15. * 2018-11-02 MurphyZhao port to lwIP 2.1.0
  16. * 2021-09-07 Grissiom fix eth_tx_msg ack bug
  17. * 2022-02-22 xiangxistu integrate v1.4.1 v2.0.3 and v2.1.2 porting layer
  18. */
  19. /*
  20. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without modification,
  24. * are permitted provided that the following conditions are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote products
  32. * derived from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  37. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  42. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. * OF SUCH DAMAGE.
  44. *
  45. * This file is part of the lwIP TCP/IP stack.
  46. *
  47. * Author: Adam Dunkels <adam@sics.se>
  48. */
  49. #include <string.h>
  50. #include <lwip/init.h>
  51. #include <lwip/opt.h>
  52. #include <lwip/debug.h>
  53. #include <lwip/def.h>
  54. #include <lwip/mem.h>
  55. #include <lwip/pbuf.h>
  56. #include <lwip/sys.h>
  57. #include <lwip/netif.h>
  58. #include <lwip/stats.h>
  59. #include <lwip/tcpip.h>
  60. #include <lwip/dhcp.h>
  61. #include <lwip/netifapi.h>
  62. #include <lwip/inet.h>
  63. #include <netif/etharp.h>
  64. #include <netif/ethernetif.h>
  65. #include <ipc/completion.h>
  66. #if LWIP_IPV6
  67. #include "lwip/ethip6.h"
  68. #endif /* LWIP_IPV6 */
  69. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  70. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  71. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  72. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  73. #else
  74. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  75. #endif
  76. #ifndef LWIP_NO_TX_THREAD
  77. /**
  78. * Tx message structure for Ethernet interface
  79. */
  80. struct eth_tx_msg
  81. {
  82. struct netif *netif;
  83. struct pbuf *buf;
  84. struct rt_completion ack;
  85. };
  86. static struct rt_mailbox eth_tx_thread_mb;
  87. static struct rt_thread eth_tx_thread;
  88. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  89. static char eth_tx_thread_mb_pool[32 * sizeof(rt_ubase_t)];
  90. static char eth_tx_thread_stack[512];
  91. #else
  92. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * sizeof(rt_ubase_t)];
  93. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  94. #endif
  95. #endif
  96. #ifndef LWIP_NO_RX_THREAD
  97. static struct rt_mailbox eth_rx_thread_mb;
  98. static struct rt_thread eth_rx_thread;
  99. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  100. static char eth_rx_thread_mb_pool[48 * sizeof(rt_ubase_t)];
  101. static char eth_rx_thread_stack[1024];
  102. #else
  103. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * sizeof(rt_ubase_t)];
  104. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  105. #endif
  106. #endif
  107. #ifdef RT_USING_NETDEV
  108. #include "lwip/ip.h"
  109. #include "lwip/init.h"
  110. #include "lwip/netdb.h"
  111. #include <netdev.h>
  112. static int lwip_netdev_set_up(struct netdev *netif)
  113. {
  114. netif_set_up((struct netif *)netif->user_data);
  115. return ERR_OK;
  116. }
  117. static int lwip_netdev_set_down(struct netdev *netif)
  118. {
  119. netif_set_down((struct netif *)netif->user_data);
  120. return ERR_OK;
  121. }
  122. #ifndef ip_2_ip4
  123. #define ip_2_ip4(ipaddr) (ipaddr)
  124. #endif
  125. static int lwip_netdev_set_addr_info(struct netdev *netif, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  126. {
  127. if (ip_addr && netmask && gw)
  128. {
  129. netif_set_addr((struct netif *)netif->user_data, ip_2_ip4(ip_addr), ip_2_ip4(netmask), ip_2_ip4(gw));
  130. }
  131. else
  132. {
  133. if (ip_addr)
  134. {
  135. netif_set_ipaddr((struct netif *)netif->user_data, ip_2_ip4(ip_addr));
  136. }
  137. if (netmask)
  138. {
  139. netif_set_netmask((struct netif *)netif->user_data, ip_2_ip4(netmask));
  140. }
  141. if (gw)
  142. {
  143. netif_set_gw((struct netif *)netif->user_data, ip_2_ip4(gw));
  144. }
  145. }
  146. return ERR_OK;
  147. }
  148. #ifdef RT_LWIP_DNS
  149. static int lwip_netdev_set_dns_server(struct netdev *netif, uint8_t dns_num, ip_addr_t *dns_server)
  150. {
  151. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  152. extern void dns_setserver(u8_t numdns, ip_addr_t *dnsserver);
  153. #else /* >=2.x */
  154. extern void dns_setserver(uint8_t dns_num, const ip_addr_t *dns_server);
  155. #endif /* LWIP_VERSION_MAJOR == 1U */
  156. dns_setserver(dns_num, dns_server);
  157. return ERR_OK;
  158. }
  159. #endif /* RT_LWIP_DNS */
  160. #ifdef RT_LWIP_DHCP
  161. static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
  162. {
  163. netdev_low_level_set_dhcp_status(netif, is_enabled);
  164. if(RT_TRUE == is_enabled)
  165. {
  166. dhcp_start((struct netif *)netif->user_data);
  167. }
  168. else
  169. {
  170. dhcp_stop((struct netif *)netif->user_data);
  171. }
  172. return ERR_OK;
  173. }
  174. #endif /* RT_LWIP_DHCP */
  175. #ifdef RT_USING_FINSH
  176. #ifdef RT_LWIP_USING_PING
  177. extern int lwip_ping_recv(int s, int *ttl);
  178. extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
  179. int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
  180. uint32_t timeout, struct netdev_ping_resp *ping_resp)
  181. {
  182. int s, ttl, recv_len, result = 0;
  183. int elapsed_time;
  184. rt_tick_t recv_start_tick;
  185. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  186. int recv_timeout = timeout * 1000UL / RT_TICK_PER_SECOND;
  187. #else /* >= v2.x */
  188. struct timeval recv_timeout = { timeout / RT_TICK_PER_SECOND, timeout % RT_TICK_PER_SECOND };
  189. #endif
  190. ip_addr_t target_addr;
  191. struct addrinfo hint, *res = RT_NULL;
  192. struct sockaddr_in *h = RT_NULL;
  193. struct in_addr ina;
  194. struct sockaddr_in local;
  195. RT_ASSERT(netif);
  196. RT_ASSERT(host);
  197. RT_ASSERT(ping_resp);
  198. rt_memset(&hint, 0x00, sizeof(hint));
  199. /* convert URL to IP */
  200. if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0)
  201. {
  202. return -RT_ERROR;
  203. }
  204. SMEMCPY(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  205. SMEMCPY(&ina, &h->sin_addr, sizeof(ina));
  206. lwip_freeaddrinfo(res);
  207. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  208. {
  209. return -RT_ERROR;
  210. }
  211. SMEMCPY(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
  212. /* new a socket */
  213. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  214. {
  215. return -RT_ERROR;
  216. }
  217. local.sin_len = sizeof(local);
  218. local.sin_family = AF_INET;
  219. local.sin_port = 0;
  220. local.sin_addr.s_addr = (netif->ip_addr.addr);
  221. lwip_bind(s, (struct sockaddr *)&local, sizeof(struct sockaddr_in));
  222. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
  223. if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
  224. {
  225. recv_start_tick = rt_tick_get();
  226. if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
  227. {
  228. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  229. ping_resp->data_len = recv_len;
  230. ping_resp->ttl = ttl;
  231. ping_resp->ticks = elapsed_time;
  232. }
  233. else
  234. {
  235. result = -RT_ETIMEOUT;
  236. goto __exit;
  237. }
  238. }
  239. else
  240. {
  241. result = -RT_ETIMEOUT;
  242. goto __exit;
  243. }
  244. __exit:
  245. lwip_close(s);
  246. return result;
  247. }
  248. #endif /* RT_LWIP_USING_PING */
  249. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  250. void lwip_netdev_netstat(struct netdev *netif)
  251. {
  252. extern void list_tcps(void);
  253. extern void list_udps(void);
  254. #ifdef RT_LWIP_TCP
  255. list_tcps();
  256. #endif
  257. #ifdef RT_LWIP_UDP
  258. list_udps();
  259. #endif
  260. }
  261. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  262. #endif /* RT_USING_FINSH */
  263. static int lwip_netdev_set_default(struct netdev *netif)
  264. {
  265. netif_set_default((struct netif *)netif->user_data);
  266. return ERR_OK;
  267. }
  268. const struct netdev_ops lwip_netdev_ops =
  269. {
  270. lwip_netdev_set_up,
  271. lwip_netdev_set_down,
  272. lwip_netdev_set_addr_info,
  273. #ifdef RT_LWIP_DNS
  274. lwip_netdev_set_dns_server,
  275. #else
  276. NULL,
  277. #endif /* RT_LWIP_DNS */
  278. #ifdef RT_LWIP_DHCP
  279. lwip_netdev_set_dhcp,
  280. #else
  281. NULL,
  282. #endif /* RT_LWIP_DHCP */
  283. #ifdef RT_USING_FINSH
  284. #ifdef RT_LWIP_USING_PING
  285. lwip_netdev_ping,
  286. #else
  287. NULL,
  288. #endif /* RT_LWIP_USING_PING */
  289. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  290. lwip_netdev_netstat,
  291. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  292. #endif /* RT_USING_FINSH */
  293. lwip_netdev_set_default,
  294. };
  295. /* synchronize lwIP network interface device and network interface device flags */
  296. static int netdev_flags_sync(struct netif *lwip_netif)
  297. {
  298. struct netdev *netdev = NULL;
  299. RT_ASSERT(lwip_netif);
  300. netdev = netdev_get_by_name(lwip_netif->name);
  301. if (netdev == RT_NULL)
  302. {
  303. return -ERR_IF;
  304. }
  305. netdev->mtu = lwip_netif->mtu;
  306. /* the macro definition is different from lwip-1.4.1 and lwip-2.x.x about 'flags'. */
  307. if(lwip_netif->flags & NETIF_FLAG_BROADCAST)
  308. {
  309. netdev->flags |= NETDEV_FLAG_BROADCAST;
  310. }
  311. if(lwip_netif->flags & NETIF_FLAG_ETHARP)
  312. {
  313. netdev->flags |= NETDEV_FLAG_ETHARP;
  314. }
  315. if(lwip_netif->flags & NETIF_FLAG_IGMP)
  316. {
  317. netdev->flags |= NETDEV_FLAG_IGMP;
  318. }
  319. #if LWIP_VERSION_MAJOR >= 2U /* >= v2.x */
  320. if(lwip_netif->flags & NETIF_FLAG_MLD6)
  321. {
  322. netdev->flags |= NETDEV_FLAG_MLD6;
  323. }
  324. #endif /* LWIP_VERSION_MAJOR >= 2U */
  325. #if LWIP_DHCP
  326. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  327. #else
  328. netdev_low_level_set_dhcp_status(netdev, RT_FALSE);
  329. #endif
  330. return ERR_OK;
  331. }
  332. static int netdev_add(struct netif *lwip_netif)
  333. {
  334. #define LWIP_NETIF_NAME_LEN 2
  335. int result = 0;
  336. struct netdev *netdev = RT_NULL;
  337. char name[LWIP_NETIF_NAME_LEN + 1] = {0};
  338. RT_ASSERT(lwip_netif);
  339. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  340. if (netdev == RT_NULL)
  341. {
  342. return -ERR_IF;
  343. }
  344. #ifdef SAL_USING_LWIP
  345. extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev);
  346. /* set the lwIP network interface device protocol family information */
  347. sal_lwip_netdev_set_pf_info(netdev);
  348. #endif /* SAL_USING_LWIP */
  349. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  350. result = netdev_register(netdev, name, (void *)lwip_netif);
  351. /* Update netdev info after registered */
  352. netdev_flags_sync(lwip_netif);
  353. netdev->ops = &lwip_netdev_ops;
  354. netdev->hwaddr_len = lwip_netif->hwaddr_len;
  355. SMEMCPY(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len);
  356. netdev->ip_addr = lwip_netif->ip_addr;
  357. netdev->gw = lwip_netif->gw;
  358. netdev->netmask = lwip_netif->netmask;
  359. return result;
  360. }
  361. static void netdev_del(struct netif *lwip_netif)
  362. {
  363. char name[LWIP_NETIF_NAME_LEN + 1];
  364. struct netdev *netdev;
  365. RT_ASSERT(lwip_netif);
  366. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  367. netdev = netdev_get_by_name(name);
  368. netdev_unregister(netdev);
  369. rt_free(netdev);
  370. }
  371. #endif /* RT_USING_NETDEV */
  372. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  373. {
  374. #ifndef LWIP_NO_TX_THREAD
  375. struct eth_tx_msg msg;
  376. RT_ASSERT(netif != RT_NULL);
  377. /* send a message to eth tx thread */
  378. msg.netif = netif;
  379. msg.buf = p;
  380. rt_completion_init(&msg.ack);
  381. if (rt_mb_send(&eth_tx_thread_mb, (rt_ubase_t) &msg) == RT_EOK)
  382. {
  383. /* waiting for ack */
  384. rt_completion_wait(&msg.ack, RT_WAITING_FOREVER);
  385. }
  386. #else
  387. struct eth_device* enetif;
  388. RT_ASSERT(netif != RT_NULL);
  389. enetif = (struct eth_device*)netif->state;
  390. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  391. {
  392. return ERR_IF;
  393. }
  394. #endif
  395. return ERR_OK;
  396. }
  397. static err_t eth_netif_device_init(struct netif *netif)
  398. {
  399. struct eth_device *ethif;
  400. ethif = (struct eth_device*)netif->state;
  401. if (ethif != RT_NULL)
  402. {
  403. rt_device_t device;
  404. #ifdef RT_USING_NETDEV
  405. /* network interface device register */
  406. netdev_add(netif);
  407. #endif /* RT_USING_NETDEV */
  408. /* get device object */
  409. device = (rt_device_t) ethif;
  410. if (rt_device_open(device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
  411. {
  412. return ERR_IF;
  413. }
  414. /* copy device flags to netif flags */
  415. netif->flags = (ethif->flags & 0xff);
  416. netif->mtu = ETHERNET_MTU;
  417. /* set output */
  418. netif->output = etharp_output;
  419. #if LWIP_IPV6
  420. netif->output_ip6 = ethip6_output;
  421. netif->ip6_autoconfig_enabled = 1;
  422. netif_create_ip6_linklocal_address(netif, 1);
  423. #if LWIP_IPV6_MLD
  424. netif->flags |= NETIF_FLAG_MLD6;
  425. /*
  426. * For hardware/netifs that implement MAC filtering.
  427. * All-nodes link-local is handled by default, so we must let the hardware know
  428. * to allow multicast packets in.
  429. * Should set mld_mac_filter previously. */
  430. if (netif->mld_mac_filter != NULL)
  431. {
  432. ip6_addr_t ip6_allnodes_ll;
  433. ip6_addr_set_allnodes_linklocal(&ip6_allnodes_ll);
  434. netif->mld_mac_filter(netif, &ip6_allnodes_ll, NETIF_ADD_MAC_FILTER);
  435. }
  436. #endif /* LWIP_IPV6_MLD */
  437. #endif /* LWIP_IPV6 */
  438. /* set default netif */
  439. if (netif_default == RT_NULL)
  440. netif_set_default(ethif->netif);
  441. /* set interface up */
  442. netif_set_up(ethif->netif);
  443. #if LWIP_DHCP
  444. /* if this interface uses DHCP, start the DHCP client */
  445. dhcp_start(ethif->netif);
  446. #endif
  447. if (ethif->flags & ETHIF_LINK_PHYUP)
  448. {
  449. /* set link_up for this netif */
  450. netif_set_link_up(ethif->netif);
  451. }
  452. #ifdef RT_USING_NETDEV
  453. /* network interface device flags synchronize */
  454. netdev_flags_sync(netif);
  455. #endif /* RT_USING_NETDEV */
  456. return ERR_OK;
  457. }
  458. return ERR_IF;
  459. }
  460. /* Keep old drivers compatible in RT-Thread */
  461. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  462. {
  463. struct netif* netif;
  464. #if LWIP_NETIF_HOSTNAME
  465. #define LWIP_HOSTNAME_LEN 16
  466. char *hostname = RT_NULL;
  467. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  468. #else
  469. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  470. #endif
  471. if (netif == RT_NULL)
  472. {
  473. rt_kprintf("malloc netif failed\n");
  474. return -RT_ERROR;
  475. }
  476. /* set netif */
  477. dev->netif = netif;
  478. dev->flags = flags;
  479. /* link changed status of device */
  480. dev->link_changed = 0x00;
  481. /* avoid send the same mail to mailbox */
  482. dev->rx_notice = 0x00;
  483. dev->parent.type = RT_Device_Class_NetIf;
  484. /* register to RT-Thread device manager */
  485. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  486. /* set name */
  487. netif->name[0] = name[0];
  488. netif->name[1] = name[1];
  489. /* set hw address to 6 */
  490. netif->hwaddr_len = 6;
  491. /* maximum transfer unit */
  492. netif->mtu = ETHERNET_MTU;
  493. /* set linkoutput */
  494. netif->linkoutput = ethernetif_linkoutput;
  495. /* get hardware MAC address */
  496. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  497. #if LWIP_NETIF_HOSTNAME
  498. /* Initialize interface hostname */
  499. hostname = (char *)netif + sizeof(struct netif);
  500. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  501. netif->hostname = hostname;
  502. #endif /* LWIP_NETIF_HOSTNAME */
  503. /* if tcp thread has been started up, we add this netif to the system */
  504. if (rt_thread_find("tcpip") != RT_NULL)
  505. {
  506. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  507. struct ip_addr ipaddr, netmask, gw;
  508. #else /* >= v2.x */
  509. ip4_addr_t ipaddr, netmask, gw;
  510. #endif /* LWIP_VERSION_MAJOR == 1U */
  511. #if !LWIP_DHCP
  512. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  513. gw.addr = inet_addr(RT_LWIP_GWADDR);
  514. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  515. #else
  516. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  517. IP4_ADDR(&gw, 0, 0, 0, 0);
  518. IP4_ADDR(&netmask, 0, 0, 0, 0);
  519. #endif
  520. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  521. }
  522. return RT_EOK;
  523. }
  524. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  525. {
  526. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  527. #if LWIP_IGMP
  528. /* IGMP support */
  529. flags |= NETIF_FLAG_IGMP;
  530. #endif
  531. return eth_device_init_with_flag(dev, name, flags);
  532. }
  533. void eth_device_deinit(struct eth_device *dev)
  534. {
  535. struct netif* netif = dev->netif;
  536. #if LWIP_DHCP
  537. dhcp_stop(netif);
  538. dhcp_cleanup(netif);
  539. #endif
  540. netif_set_down(netif);
  541. netif_remove(netif);
  542. #ifdef RT_USING_NETDEV
  543. netdev_del(netif);
  544. #endif
  545. rt_device_close(&(dev->parent));
  546. rt_device_unregister(&(dev->parent));
  547. rt_free(netif);
  548. }
  549. #ifndef LWIP_NO_RX_THREAD
  550. rt_err_t eth_device_ready(struct eth_device* dev)
  551. {
  552. if (dev->netif)
  553. {
  554. if(dev->rx_notice == RT_FALSE)
  555. {
  556. dev->rx_notice = RT_TRUE;
  557. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  558. }
  559. else
  560. return RT_EOK;
  561. /* post message to Ethernet thread */
  562. }
  563. else
  564. return -RT_ERROR; /* netif is not initialized yet, just return. */
  565. }
  566. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  567. {
  568. rt_base_t level;
  569. RT_ASSERT(dev != RT_NULL);
  570. level = rt_hw_interrupt_disable();
  571. dev->link_changed = 0x01;
  572. if (up == RT_TRUE)
  573. dev->link_status = 0x01;
  574. else
  575. dev->link_status = 0x00;
  576. rt_hw_interrupt_enable(level);
  577. /* post message to ethernet thread */
  578. return rt_mb_send(&eth_rx_thread_mb, (rt_ubase_t)dev);
  579. }
  580. #else
  581. /* NOTE: please not use it in interrupt when no RxThread exist */
  582. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  583. {
  584. if (up == RT_TRUE)
  585. netifapi_netif_set_link_up(dev->netif);
  586. else
  587. netifapi_netif_set_link_down(dev->netif);
  588. return RT_EOK;
  589. }
  590. #endif
  591. #ifndef LWIP_NO_TX_THREAD
  592. /* Ethernet Tx Thread */
  593. static void eth_tx_thread_entry(void* parameter)
  594. {
  595. struct eth_tx_msg* msg;
  596. while (1)
  597. {
  598. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t *)&msg, RT_WAITING_FOREVER) == RT_EOK)
  599. {
  600. struct eth_device* enetif;
  601. RT_ASSERT(msg->netif != RT_NULL);
  602. RT_ASSERT(msg->buf != RT_NULL);
  603. enetif = (struct eth_device*)msg->netif->state;
  604. if (enetif != RT_NULL)
  605. {
  606. /* call driver's interface */
  607. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  608. {
  609. /* transmit eth packet failed */
  610. }
  611. }
  612. /* send ACK */
  613. rt_completion_done(&msg->ack);
  614. }
  615. }
  616. }
  617. #endif
  618. #ifndef LWIP_NO_RX_THREAD
  619. /* Ethernet Rx Thread */
  620. static void eth_rx_thread_entry(void* parameter)
  621. {
  622. struct eth_device* device;
  623. while (1)
  624. {
  625. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t *)&device, RT_WAITING_FOREVER) == RT_EOK)
  626. {
  627. rt_base_t level;
  628. struct pbuf *p;
  629. /* check link status */
  630. if (device->link_changed)
  631. {
  632. int status;
  633. level = rt_hw_interrupt_disable();
  634. status = device->link_status;
  635. device->link_changed = 0x00;
  636. rt_hw_interrupt_enable(level);
  637. if (status)
  638. netifapi_netif_set_link_up(device->netif);
  639. else
  640. netifapi_netif_set_link_down(device->netif);
  641. }
  642. level = rt_hw_interrupt_disable();
  643. /* 'rx_notice' will be modify in the interrupt or here */
  644. device->rx_notice = RT_FALSE;
  645. rt_hw_interrupt_enable(level);
  646. /* receive all of buffer */
  647. while (1)
  648. {
  649. if(device->eth_rx == RT_NULL) break;
  650. p = device->eth_rx(&(device->parent));
  651. if (p != RT_NULL)
  652. {
  653. /* notify to upper layer */
  654. if( device->netif->input(p, device->netif) != ERR_OK )
  655. {
  656. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  657. pbuf_free(p);
  658. p = NULL;
  659. }
  660. }
  661. else break;
  662. }
  663. }
  664. else
  665. {
  666. LWIP_ASSERT("Should not happen!\n",0);
  667. }
  668. }
  669. }
  670. #endif
  671. /* this function does not need,
  672. * use eth_system_device_init_private()
  673. * call by lwip_system_init().
  674. */
  675. int eth_system_device_init(void)
  676. {
  677. return 0;
  678. }
  679. int eth_system_device_init_private(void)
  680. {
  681. rt_err_t result = RT_EOK;
  682. /* initialize Rx thread. */
  683. #ifndef LWIP_NO_RX_THREAD
  684. /* initialize mailbox and create Ethernet Rx thread */
  685. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  686. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/sizeof(rt_ubase_t),
  687. RT_IPC_FLAG_FIFO);
  688. RT_ASSERT(result == RT_EOK);
  689. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  690. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  691. RT_ETHERNETIF_THREAD_PREORITY, 16);
  692. RT_ASSERT(result == RT_EOK);
  693. result = rt_thread_startup(&eth_rx_thread);
  694. RT_ASSERT(result == RT_EOK);
  695. #endif
  696. /* initialize Tx thread */
  697. #ifndef LWIP_NO_TX_THREAD
  698. /* initialize mailbox and create Ethernet Tx thread */
  699. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  700. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/sizeof(rt_ubase_t),
  701. RT_IPC_FLAG_FIFO);
  702. RT_ASSERT(result == RT_EOK);
  703. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  704. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  705. RT_ETHERNETIF_THREAD_PREORITY, 16);
  706. RT_ASSERT(result == RT_EOK);
  707. result = rt_thread_startup(&eth_tx_thread);
  708. RT_ASSERT(result == RT_EOK);
  709. #endif
  710. return (int)result;
  711. }
  712. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  713. {
  714. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  715. struct ip_addr *ip;
  716. struct ip_addr addr;
  717. #else /* >= v2.x */
  718. ip4_addr_t *ip;
  719. ip4_addr_t addr;
  720. #endif /* LWIP_VERSION_MAJOR == 1U */
  721. struct netif * netif = netif_list;
  722. if(strlen(netif_name) > sizeof(netif->name))
  723. {
  724. rt_kprintf("network interface name too long!\r\n");
  725. return;
  726. }
  727. while(netif != RT_NULL)
  728. {
  729. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  730. break;
  731. netif = netif->next;
  732. if( netif == RT_NULL )
  733. {
  734. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  735. return;
  736. }
  737. }
  738. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  739. ip = (struct ip_addr *)&addr;
  740. #else /* >= v2.x */
  741. ip = (ip4_addr_t *)&addr;
  742. #endif /* LWIP_VERSION_MAJOR == 1U */
  743. /* set ip address */
  744. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  745. {
  746. netif_set_ipaddr(netif, ip);
  747. }
  748. /* set gateway address */
  749. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  750. {
  751. netif_set_gw(netif, ip);
  752. }
  753. /* set netmask address */
  754. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  755. {
  756. netif_set_netmask(netif, ip);
  757. }
  758. }
  759. #ifdef RT_USING_FINSH
  760. #include <finsh.h>
  761. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  762. #if LWIP_DNS
  763. #include <lwip/dns.h>
  764. void set_dns(uint8_t dns_num, char* dns_server)
  765. {
  766. ip_addr_t addr;
  767. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  768. {
  769. dns_setserver(dns_num, &addr);
  770. }
  771. }
  772. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  773. #endif
  774. void list_if(void)
  775. {
  776. rt_ubase_t index;
  777. struct netif * netif;
  778. rt_enter_critical();
  779. netif = netif_list;
  780. while( netif != RT_NULL )
  781. {
  782. rt_kprintf("network interface: %c%c%s\n",
  783. netif->name[0],
  784. netif->name[1],
  785. (netif == netif_default)?" (Default)":"");
  786. rt_kprintf("MTU: %d\n", netif->mtu);
  787. rt_kprintf("MAC: ");
  788. for (index = 0; index < netif->hwaddr_len; index ++)
  789. rt_kprintf("%02x ", netif->hwaddr[index]);
  790. rt_kprintf("\nFLAGS:");
  791. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  792. else rt_kprintf(" DOWN");
  793. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  794. else rt_kprintf(" LINK_DOWN");
  795. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  796. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  797. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  798. rt_kprintf("\n");
  799. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  800. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  801. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  802. #if LWIP_IPV6
  803. {
  804. ip6_addr_t *addr;
  805. int addr_state;
  806. int i;
  807. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  808. addr_state = netif->ip6_addr_state[0];
  809. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  810. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  811. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  812. {
  813. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  814. addr_state = netif->ip6_addr_state[i];
  815. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  816. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  817. }
  818. }
  819. rt_kprintf("\r\n");
  820. #endif /* LWIP_IPV6 */
  821. netif = netif->next;
  822. }
  823. #if LWIP_DNS
  824. {
  825. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  826. struct ip_addr ip_addr;
  827. for(index=0; index<DNS_MAX_SERVERS; index++)
  828. {
  829. ip_addr = dns_getserver(index);
  830. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  831. }
  832. #else /* >= v2.x */
  833. const ip_addr_t *ip_addr;
  834. for(index=0; index<DNS_MAX_SERVERS; index++)
  835. {
  836. ip_addr = dns_getserver(index);
  837. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(ip_addr));
  838. }
  839. #endif /* LWIP_VERSION_MAJOR == 1U */
  840. }
  841. #endif /**< #if LWIP_DNS */
  842. rt_exit_critical();
  843. }
  844. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  845. #if LWIP_TCP
  846. #include <lwip/tcp.h>
  847. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  848. #include <lwip/tcp_impl.h>
  849. #else /* >= v2.x */
  850. #include <lwip/priv/tcp_priv.h>
  851. #endif /* LWIP_VERSION_MAJOR == 1U */
  852. void list_tcps(void)
  853. {
  854. rt_uint32_t num = 0;
  855. struct tcp_pcb *pcb;
  856. char local_ip_str[16];
  857. char remote_ip_str[16];
  858. extern struct tcp_pcb *tcp_active_pcbs;
  859. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  860. extern struct tcp_pcb *tcp_tw_pcbs;
  861. rt_enter_critical();
  862. rt_kprintf("Active PCB states:\n");
  863. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  864. {
  865. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  866. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  867. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  868. num++,
  869. local_ip_str,
  870. pcb->local_port,
  871. remote_ip_str,
  872. pcb->remote_port,
  873. pcb->snd_nxt,
  874. pcb->rcv_nxt);
  875. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  876. }
  877. rt_kprintf("Listen PCB states:\n");
  878. num = 0;
  879. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  880. {
  881. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  882. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  883. }
  884. rt_kprintf("TIME-WAIT PCB states:\n");
  885. num = 0;
  886. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  887. {
  888. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  889. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  890. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  891. num++,
  892. local_ip_str,
  893. pcb->local_port,
  894. remote_ip_str,
  895. pcb->remote_port,
  896. pcb->snd_nxt,
  897. pcb->rcv_nxt);
  898. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  899. }
  900. rt_exit_critical();
  901. }
  902. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  903. #endif /* LWIP_TCP */
  904. #if LWIP_UDP
  905. #include "lwip/udp.h"
  906. void list_udps(void)
  907. {
  908. struct udp_pcb *pcb;
  909. rt_uint32_t num = 0;
  910. char local_ip_str[16];
  911. char remote_ip_str[16];
  912. rt_enter_critical();
  913. rt_kprintf("Active UDP PCB states:\n");
  914. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  915. {
  916. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  917. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  918. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  919. num, (int)pcb->flags,
  920. local_ip_str,
  921. pcb->local_port,
  922. remote_ip_str,
  923. pcb->remote_port);
  924. num++;
  925. }
  926. rt_exit_critical();
  927. }
  928. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  929. #endif /* LWIP_UDP */
  930. #endif