ethernetif.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. /* 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_uint32_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. rt_uint32_t level;
  634. level = rt_hw_interrupt_disable();
  635. status = device->link_status;
  636. device->link_changed = 0x00;
  637. rt_hw_interrupt_enable(level);
  638. if (status)
  639. netifapi_netif_set_link_up(device->netif);
  640. else
  641. netifapi_netif_set_link_down(device->netif);
  642. }
  643. level = rt_hw_interrupt_disable();
  644. /* 'rx_notice' will be modify in the interrupt or here */
  645. device->rx_notice = RT_FALSE;
  646. rt_hw_interrupt_enable(level);
  647. /* receive all of buffer */
  648. while (1)
  649. {
  650. if(device->eth_rx == RT_NULL) break;
  651. p = device->eth_rx(&(device->parent));
  652. if (p != RT_NULL)
  653. {
  654. /* notify to upper layer */
  655. if( device->netif->input(p, device->netif) != ERR_OK )
  656. {
  657. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  658. pbuf_free(p);
  659. p = NULL;
  660. }
  661. }
  662. else break;
  663. }
  664. }
  665. else
  666. {
  667. LWIP_ASSERT("Should not happen!\n",0);
  668. }
  669. }
  670. }
  671. #endif
  672. /* this function does not need,
  673. * use eth_system_device_init_private()
  674. * call by lwip_system_init().
  675. */
  676. int eth_system_device_init(void)
  677. {
  678. return 0;
  679. }
  680. int eth_system_device_init_private(void)
  681. {
  682. rt_err_t result = RT_EOK;
  683. /* initialize Rx thread. */
  684. #ifndef LWIP_NO_RX_THREAD
  685. /* initialize mailbox and create Ethernet Rx thread */
  686. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  687. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/sizeof(rt_ubase_t),
  688. RT_IPC_FLAG_FIFO);
  689. RT_ASSERT(result == RT_EOK);
  690. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  691. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  692. RT_ETHERNETIF_THREAD_PREORITY, 16);
  693. RT_ASSERT(result == RT_EOK);
  694. result = rt_thread_startup(&eth_rx_thread);
  695. RT_ASSERT(result == RT_EOK);
  696. #endif
  697. /* initialize Tx thread */
  698. #ifndef LWIP_NO_TX_THREAD
  699. /* initialize mailbox and create Ethernet Tx thread */
  700. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  701. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/sizeof(rt_ubase_t),
  702. RT_IPC_FLAG_FIFO);
  703. RT_ASSERT(result == RT_EOK);
  704. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  705. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  706. RT_ETHERNETIF_THREAD_PREORITY, 16);
  707. RT_ASSERT(result == RT_EOK);
  708. result = rt_thread_startup(&eth_tx_thread);
  709. RT_ASSERT(result == RT_EOK);
  710. #endif
  711. return (int)result;
  712. }
  713. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  714. {
  715. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  716. struct ip_addr *ip;
  717. struct ip_addr addr;
  718. #else /* >= v2.x */
  719. ip4_addr_t *ip;
  720. ip4_addr_t addr;
  721. #endif /* LWIP_VERSION_MAJOR == 1U */
  722. struct netif * netif = netif_list;
  723. if(strlen(netif_name) > sizeof(netif->name))
  724. {
  725. rt_kprintf("network interface name too long!\r\n");
  726. return;
  727. }
  728. while(netif != RT_NULL)
  729. {
  730. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  731. break;
  732. netif = netif->next;
  733. if( netif == RT_NULL )
  734. {
  735. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  736. return;
  737. }
  738. }
  739. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  740. ip = (struct ip_addr *)&addr;
  741. #else /* >= v2.x */
  742. ip = (ip4_addr_t *)&addr;
  743. #endif /* LWIP_VERSION_MAJOR == 1U */
  744. /* set ip address */
  745. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  746. {
  747. netif_set_ipaddr(netif, ip);
  748. }
  749. /* set gateway address */
  750. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  751. {
  752. netif_set_gw(netif, ip);
  753. }
  754. /* set netmask address */
  755. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  756. {
  757. netif_set_netmask(netif, ip);
  758. }
  759. }
  760. #ifdef RT_USING_FINSH
  761. #include <finsh.h>
  762. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  763. #if LWIP_DNS
  764. #include <lwip/dns.h>
  765. void set_dns(uint8_t dns_num, char* dns_server)
  766. {
  767. ip_addr_t addr;
  768. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  769. {
  770. dns_setserver(dns_num, &addr);
  771. }
  772. }
  773. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  774. #endif
  775. void list_if(void)
  776. {
  777. rt_ubase_t index;
  778. struct netif * netif;
  779. rt_enter_critical();
  780. netif = netif_list;
  781. while( netif != RT_NULL )
  782. {
  783. rt_kprintf("network interface: %c%c%s\n",
  784. netif->name[0],
  785. netif->name[1],
  786. (netif == netif_default)?" (Default)":"");
  787. rt_kprintf("MTU: %d\n", netif->mtu);
  788. rt_kprintf("MAC: ");
  789. for (index = 0; index < netif->hwaddr_len; index ++)
  790. rt_kprintf("%02x ", netif->hwaddr[index]);
  791. rt_kprintf("\nFLAGS:");
  792. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  793. else rt_kprintf(" DOWN");
  794. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  795. else rt_kprintf(" LINK_DOWN");
  796. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  797. if (netif->flags & NETIF_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  798. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  799. rt_kprintf("\n");
  800. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  801. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  802. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  803. #if LWIP_IPV6
  804. {
  805. ip6_addr_t *addr;
  806. int addr_state;
  807. int i;
  808. addr = (ip6_addr_t *)&netif->ip6_addr[0];
  809. addr_state = netif->ip6_addr_state[0];
  810. rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
  811. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  812. for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
  813. {
  814. addr = (ip6_addr_t *)&netif->ip6_addr[i];
  815. addr_state = netif->ip6_addr_state[i];
  816. rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
  817. addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
  818. }
  819. }
  820. rt_kprintf("\r\n");
  821. #endif /* LWIP_IPV6 */
  822. netif = netif->next;
  823. }
  824. #if LWIP_DNS
  825. {
  826. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  827. struct ip_addr ip_addr;
  828. for(index=0; index<DNS_MAX_SERVERS; index++)
  829. {
  830. ip_addr = dns_getserver(index);
  831. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  832. }
  833. #else /* >= v2.x */
  834. const ip_addr_t *ip_addr;
  835. for(index=0; index<DNS_MAX_SERVERS; index++)
  836. {
  837. ip_addr = dns_getserver(index);
  838. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(ip_addr));
  839. }
  840. #endif /* LWIP_VERSION_MAJOR == 1U */
  841. }
  842. #endif /**< #if LWIP_DNS */
  843. rt_exit_critical();
  844. }
  845. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  846. #if LWIP_TCP
  847. #include <lwip/tcp.h>
  848. #if LWIP_VERSION_MAJOR == 1U /* v1.x */
  849. #include <lwip/tcp_impl.h>
  850. #else /* >= v2.x */
  851. #include <lwip/priv/tcp_priv.h>
  852. #endif /* LWIP_VERSION_MAJOR == 1U */
  853. void list_tcps(void)
  854. {
  855. rt_uint32_t num = 0;
  856. struct tcp_pcb *pcb;
  857. char local_ip_str[16];
  858. char remote_ip_str[16];
  859. extern struct tcp_pcb *tcp_active_pcbs;
  860. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  861. extern struct tcp_pcb *tcp_tw_pcbs;
  862. rt_enter_critical();
  863. rt_kprintf("Active PCB states:\n");
  864. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  865. {
  866. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  867. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  868. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  869. num++,
  870. local_ip_str,
  871. pcb->local_port,
  872. remote_ip_str,
  873. pcb->remote_port,
  874. pcb->snd_nxt,
  875. pcb->rcv_nxt);
  876. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  877. }
  878. rt_kprintf("Listen PCB states:\n");
  879. num = 0;
  880. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  881. {
  882. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  883. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  884. }
  885. rt_kprintf("TIME-WAIT PCB states:\n");
  886. num = 0;
  887. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  888. {
  889. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  890. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  891. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  892. num++,
  893. local_ip_str,
  894. pcb->local_port,
  895. remote_ip_str,
  896. pcb->remote_port,
  897. pcb->snd_nxt,
  898. pcb->rcv_nxt);
  899. rt_kprintf("state: %s\n", tcp_debug_state_str(pcb->state));
  900. }
  901. rt_exit_critical();
  902. }
  903. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  904. #endif /* LWIP_TCP */
  905. #if LWIP_UDP
  906. #include "lwip/udp.h"
  907. void list_udps(void)
  908. {
  909. struct udp_pcb *pcb;
  910. rt_uint32_t num = 0;
  911. char local_ip_str[16];
  912. char remote_ip_str[16];
  913. rt_enter_critical();
  914. rt_kprintf("Active UDP PCB states:\n");
  915. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  916. {
  917. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  918. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  919. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  920. num, (int)pcb->flags,
  921. local_ip_str,
  922. pcb->local_port,
  923. remote_ip_str,
  924. pcb->remote_port);
  925. num++;
  926. }
  927. rt_exit_critical();
  928. }
  929. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  930. #endif /* LWIP_UDP */
  931. #endif