ethernetif.c 28 KB

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