ethernetif.c 30 KB

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