ethernetif.c 27 KB

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