netdev.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-03-18 ChenYong First version
  9. * 2025-01-04 Evlers add statistics and more inupt parameters to ping command
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <netdev_ipaddr.h>
  17. #include <netdev.h>
  18. #ifdef RT_USING_SAL
  19. #include <sal_netdb.h>
  20. #include <sal_low_lvl.h>
  21. #endif /* RT_USING_SAL */
  22. #define DBG_TAG "netdev"
  23. #define DBG_LVL DBG_INFO
  24. #include <rtdbg.h>
  25. #if defined(SAL_USING_AF_NETLINK)
  26. #include <route_netlink.h>
  27. #endif
  28. /* The list of network interface device */
  29. struct netdev *netdev_list = RT_NULL;
  30. /* The default network interface device */
  31. struct netdev *netdev_default = RT_NULL;
  32. /* The global network register callback */
  33. static netdev_callback_fn g_netdev_register_callback = RT_NULL;
  34. static netdev_callback_fn g_netdev_default_change_callback = RT_NULL;
  35. static RT_DEFINE_SPINLOCK(_spinlock);
  36. static int netdev_num;
  37. /**
  38. * This function will register network interface device and
  39. * add it to network interface device list.
  40. *
  41. * @param netdev the network interface device object
  42. * @param name the network interface device name
  43. * @param user_data user-specific data
  44. *
  45. * @return 0: registered successfully
  46. * -1: registered failed
  47. */
  48. int netdev_register(struct netdev *netdev, const char *name, void *user_data)
  49. {
  50. rt_uint16_t flags_mask;
  51. rt_uint16_t index;
  52. RT_ASSERT(netdev);
  53. RT_ASSERT(name);
  54. /* clean network interface device */
  55. flags_mask = NETDEV_FLAG_UP | NETDEV_FLAG_LINK_UP | NETDEV_FLAG_INTERNET_UP | NETDEV_FLAG_DHCP;
  56. netdev->flags &= ~flags_mask;
  57. ip_addr_set_zero(&(netdev->ip_addr));
  58. ip_addr_set_zero(&(netdev->netmask));
  59. ip_addr_set_zero(&(netdev->gw));
  60. IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V4);
  61. IP_SET_TYPE_VAL(netdev->netmask, IPADDR_TYPE_V4);
  62. IP_SET_TYPE_VAL(netdev->gw, IPADDR_TYPE_V4);
  63. #if NETDEV_IPV6
  64. for (index = 0; index < NETDEV_IPV6_NUM_ADDRESSES; index++)
  65. {
  66. ip_addr_set_zero(&(netdev->ip6_addr[index]));
  67. IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V6);
  68. }
  69. #endif /* NETDEV_IPV6 */
  70. for (index = 0; index < NETDEV_DNS_SERVERS_NUM; index++)
  71. {
  72. ip_addr_set_zero(&(netdev->dns_servers[index]));
  73. IP_SET_TYPE_VAL(netdev->ip_addr, IPADDR_TYPE_V4);
  74. }
  75. netdev->status_callback = RT_NULL;
  76. netdev->addr_callback = RT_NULL;
  77. if(rt_strlen(name) > RT_NAME_MAX)
  78. {
  79. char netdev_name[RT_NAME_MAX + 1] = {0};
  80. rt_strncpy(netdev_name, name, RT_NAME_MAX);
  81. LOG_E("netdev name[%s] length is so long that have been cut into [%s].", name, netdev_name);
  82. }
  83. /* fill network interface device */
  84. rt_strncpy(netdev->name, name, RT_NAME_MAX);
  85. netdev->user_data = user_data;
  86. /* initialize current network interface device single list */
  87. rt_slist_init(&(netdev->list));
  88. rt_spin_lock(&_spinlock);
  89. if (netdev_list == RT_NULL)
  90. {
  91. netdev_list = netdev;
  92. }
  93. else
  94. {
  95. /* tail insertion */
  96. rt_slist_append(&(netdev_list->list), &(netdev->list));
  97. }
  98. netdev_num++;
  99. netdev->ifindex = netdev_num;
  100. rt_spin_unlock(&_spinlock);
  101. if (netdev_default == RT_NULL)
  102. {
  103. netdev_set_default(netdev_list);
  104. }
  105. /* execute netdev register callback */
  106. if (g_netdev_register_callback)
  107. {
  108. g_netdev_register_callback(netdev, NETDEV_CB_REGISTER);
  109. }
  110. #if defined(SAL_USING_AF_NETLINK)
  111. rtnl_ip_notify(netdev, RTM_NEWLINK);
  112. #endif
  113. return RT_EOK;
  114. }
  115. /**
  116. * This function will unregister network interface device and
  117. * delete it from network interface device list.
  118. *
  119. * @param netdev the network interface device object
  120. *
  121. * @return 0: unregistered successfully
  122. * -1: unregistered failed
  123. */
  124. int netdev_unregister(struct netdev *netdev)
  125. {
  126. rt_slist_t *node = RT_NULL;
  127. struct netdev *cur_netdev = RT_NULL;
  128. RT_ASSERT(netdev);
  129. if (netdev_list == RT_NULL)
  130. {
  131. return -RT_ERROR;
  132. }
  133. rt_spin_lock(&_spinlock);
  134. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  135. {
  136. cur_netdev = rt_slist_entry(node, struct netdev, list);
  137. if (cur_netdev == netdev)
  138. {
  139. /* find this network interface device in network interface device list */
  140. if (netdev_list == netdev)
  141. {
  142. rt_slist_t *next = rt_slist_next(node);
  143. if (next)
  144. {
  145. netdev_list = rt_slist_entry(next, struct netdev, list);
  146. }
  147. else
  148. {
  149. netdev_list = RT_NULL;
  150. }
  151. }
  152. else
  153. {
  154. rt_slist_remove(&(netdev_list->list), &(cur_netdev->list));
  155. }
  156. if (netdev_default == netdev)
  157. {
  158. netdev_default = RT_NULL;
  159. }
  160. break;
  161. }
  162. }
  163. rt_spin_unlock(&_spinlock);
  164. #if defined(SAL_USING_AF_NETLINK)
  165. rtnl_ip_notify(netdev, RTM_DELLINK);
  166. #endif
  167. if (netdev_default == RT_NULL)
  168. {
  169. netdev_set_default(netdev_list);
  170. }
  171. if (cur_netdev == netdev)
  172. {
  173. #ifdef RT_USING_SAL
  174. extern int sal_netdev_cleanup(struct netdev *netdev);
  175. sal_netdev_cleanup(netdev);
  176. #endif
  177. rt_memset(netdev, 0, sizeof(*netdev));
  178. }
  179. return -RT_ERROR;
  180. }
  181. /**
  182. * This function will set register callback
  183. *
  184. * @param register_callback the network register callback
  185. *
  186. */
  187. void netdev_set_register_callback(netdev_callback_fn register_callback)
  188. {
  189. g_netdev_register_callback = register_callback;
  190. }
  191. /**
  192. * This function will get the first network interface device
  193. * with the flags in network interface device list.
  194. *
  195. * @param flags the network interface device flags
  196. *
  197. * @return != NULL: network interface device object
  198. * NULL: get failed
  199. */
  200. struct netdev *netdev_get_first_by_flags(uint16_t flags)
  201. {
  202. rt_slist_t *node = RT_NULL;
  203. struct netdev *netdev = RT_NULL;
  204. if (netdev_list == RT_NULL)
  205. {
  206. return RT_NULL;
  207. }
  208. rt_spin_lock(&_spinlock);
  209. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  210. {
  211. netdev = rt_slist_entry(node, struct netdev, list);
  212. if (netdev && (netdev->flags & flags) != 0)
  213. {
  214. rt_spin_unlock(&_spinlock);
  215. return netdev;
  216. }
  217. }
  218. rt_spin_unlock(&_spinlock);
  219. return RT_NULL;
  220. }
  221. /**
  222. * This function will get the first network interface device
  223. * in network interface device list by IP address.
  224. *
  225. * @param ip_addr the network interface device IP address
  226. *
  227. * @return != NULL: network interface device object
  228. * NULL: get failed
  229. */
  230. struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr)
  231. {
  232. rt_slist_t *node = RT_NULL;
  233. struct netdev *netdev = RT_NULL;
  234. if (netdev_list == RT_NULL)
  235. {
  236. return RT_NULL;
  237. }
  238. rt_spin_lock(&_spinlock);
  239. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  240. {
  241. netdev = rt_slist_entry(node, struct netdev, list);
  242. if (netdev && ip_addr_cmp(&(netdev->ip_addr), ip_addr))
  243. {
  244. rt_spin_unlock(&_spinlock);
  245. return netdev;
  246. }
  247. }
  248. rt_spin_unlock(&_spinlock);
  249. return RT_NULL;
  250. }
  251. /**
  252. * This function will get network interface device
  253. * in network interface device list by netdev name.
  254. *
  255. * @param name the network interface device name
  256. *
  257. * @return != NULL: network interface device object
  258. * NULL: get failed
  259. */
  260. struct netdev *netdev_get_by_name(const char *name)
  261. {
  262. rt_slist_t *node = RT_NULL;
  263. struct netdev *netdev = RT_NULL;
  264. if (netdev_list == RT_NULL)
  265. {
  266. return RT_NULL;
  267. }
  268. rt_spin_lock(&_spinlock);
  269. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  270. {
  271. netdev = rt_slist_entry(node, struct netdev, list);
  272. if (netdev && (rt_strncmp(netdev->name, name, rt_strlen(name) < RT_NAME_MAX ? rt_strlen(name) : RT_NAME_MAX) == 0))
  273. {
  274. rt_spin_unlock(&_spinlock);
  275. return netdev;
  276. }
  277. }
  278. rt_spin_unlock(&_spinlock);
  279. return RT_NULL;
  280. }
  281. /**
  282. * This function will get network interface device
  283. * in network interface device list by netdev ifindex.
  284. *
  285. * @param ifindex the ifindex of network interface device
  286. *
  287. * @return != NULL: network interface device object
  288. * NULL: get failed
  289. */
  290. struct netdev *netdev_get_by_ifindex(int ifindex)
  291. {
  292. rt_slist_t *node = RT_NULL;
  293. struct netdev *netdev = RT_NULL;
  294. if (netdev_list == RT_NULL)
  295. {
  296. return RT_NULL;
  297. }
  298. rt_spin_lock(&_spinlock);
  299. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  300. {
  301. netdev = rt_slist_entry(node, struct netdev, list);
  302. if (netdev && (netdev->ifindex == ifindex))
  303. {
  304. rt_spin_unlock(&_spinlock);
  305. return netdev;
  306. }
  307. }
  308. rt_spin_unlock(&_spinlock);
  309. return RT_NULL;
  310. }
  311. #ifdef RT_USING_SAL
  312. /**
  313. * This function will get the first network interface device
  314. * in network interface device list by protocol family type.
  315. *
  316. * @param family the network interface device protocol family type
  317. *
  318. * @return != NULL: network interface device object
  319. * NULL: get failed
  320. */
  321. struct netdev *netdev_get_by_family(int family)
  322. {
  323. rt_slist_t *node = RT_NULL;
  324. struct netdev *netdev = RT_NULL;
  325. struct sal_proto_family *pf = RT_NULL;
  326. if (netdev_list == RT_NULL)
  327. {
  328. return RT_NULL;
  329. }
  330. rt_spin_lock(&_spinlock);
  331. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  332. {
  333. netdev = rt_slist_entry(node, struct netdev, list);
  334. pf = (struct sal_proto_family *) netdev->sal_user_data;
  335. if (pf && pf->skt_ops && pf->family == family && netdev_is_up(netdev))
  336. {
  337. rt_spin_unlock(&_spinlock);
  338. return netdev;
  339. }
  340. }
  341. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  342. {
  343. netdev = rt_slist_entry(node, struct netdev, list);
  344. pf = (struct sal_proto_family *) netdev->sal_user_data;
  345. if (pf && pf->skt_ops && pf->sec_family == family && netdev_is_up(netdev))
  346. {
  347. rt_spin_unlock(&_spinlock);
  348. return netdev;
  349. }
  350. }
  351. rt_spin_unlock(&_spinlock);
  352. return RT_NULL;
  353. }
  354. /**
  355. * This function will get the family type from network interface device
  356. *
  357. * @param netdev network interface device object
  358. *
  359. * @return the network interface device family type
  360. */
  361. int netdev_family_get(struct netdev *netdev)
  362. {
  363. RT_ASSERT(netdev);
  364. return ((struct sal_proto_family *)netdev->sal_user_data)->family;
  365. }
  366. #endif /* RT_USING_SAL */
  367. #if defined(SAL_USING_AF_NETLINK)
  368. int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx))
  369. {
  370. struct netdev *cur_nd_list = netdev_list;
  371. struct netdev *nd_node;
  372. int nd_num = 0;
  373. int err = 0;
  374. if (cur_nd_list == RT_NULL)
  375. return 0;
  376. rt_spin_lock(&_spinlock);
  377. nd_num = rt_slist_len(&cur_nd_list->list) + 1;
  378. rt_spin_unlock(&_spinlock);
  379. err = cb(msg, cur_nd_list, nd_num, nd.ifindex, ROUTE_IPV4_TRUE);
  380. if (err < 0)
  381. return err;
  382. rt_spin_lock(&_spinlock);
  383. rt_slist_for_each_entry(nd_node, &(cur_nd_list->list), list)
  384. {
  385. rt_spin_unlock(&_spinlock);
  386. err = cb(msg, nd_node, nd_num, nd.ifindex, ROUTE_IPV4_TRUE);
  387. if (err < 0)
  388. {
  389. return err;
  390. }
  391. rt_spin_lock(&_spinlock);
  392. }
  393. rt_spin_unlock(&_spinlock);
  394. return 0;
  395. }
  396. #endif
  397. /**
  398. * This function will set default network interface device.
  399. *
  400. * @param netdev the network interface device to change
  401. */
  402. void netdev_set_default(struct netdev *netdev)
  403. {
  404. if (netdev && (netdev != netdev_default))
  405. {
  406. netdev_default = netdev;
  407. /* execture the default network interface device in the current network stack */
  408. if (netdev->ops && netdev->ops->set_default)
  409. {
  410. netdev->ops->set_default(netdev);
  411. }
  412. /* execture application netdev default change callback */
  413. if (g_netdev_default_change_callback)
  414. {
  415. g_netdev_default_change_callback(netdev, NETDEV_CB_DEFAULT_CHANGE);
  416. }
  417. LOG_D("Setting default network interface device name(%s) successfully.", netdev->name);
  418. }
  419. }
  420. /**
  421. * This function will set defalut netdev change callback
  422. *
  423. * @param register_callback the network default change callback
  424. *
  425. */
  426. void netdev_set_default_change_callback(netdev_callback_fn register_callback)
  427. {
  428. g_netdev_default_change_callback = register_callback;
  429. }
  430. /**
  431. * This function will enable network interface device .
  432. *
  433. * @param netdev the network interface device to change
  434. *
  435. * @return 0: set status successfully
  436. * -1: set status failed
  437. */
  438. int netdev_set_up(struct netdev *netdev)
  439. {
  440. int err = 0;
  441. RT_ASSERT(netdev);
  442. if (!netdev->ops || !netdev->ops->set_up)
  443. {
  444. LOG_E("The network interface device(%s) not support to set status.", netdev->name);
  445. return -RT_ERROR;
  446. }
  447. /* network interface device status flags check */
  448. if (netdev_is_up(netdev))
  449. {
  450. return RT_EOK;
  451. }
  452. /* execute enable network interface device operations by network interface device driver */
  453. err = netdev->ops->set_up(netdev);
  454. #if defined(SAL_USING_AF_NETLINK)
  455. if (err)
  456. rtnl_ip_notify(netdev, RTM_NEWLINK);
  457. #endif
  458. return err;
  459. }
  460. /**
  461. * This function will disable network interface device.
  462. *
  463. * @param netdev the network interface device to change
  464. *
  465. * @return 0: set status successfully
  466. * -1: set sttaus failed
  467. */
  468. int netdev_set_down(struct netdev *netdev)
  469. {
  470. int err;
  471. RT_ASSERT(netdev);
  472. if (!netdev->ops || !netdev->ops->set_down)
  473. {
  474. LOG_E("The network interface device(%s) not support to set status.", netdev->name);
  475. return -RT_ERROR;
  476. }
  477. /* network interface device status flags check */
  478. if (!netdev_is_up(netdev))
  479. {
  480. return RT_EOK;
  481. }
  482. /* execute disable network interface device operations by network interface driver */
  483. err = netdev->ops->set_down(netdev);
  484. #if defined(SAL_USING_AF_NETLINK)
  485. if (err)
  486. rtnl_ip_notify(netdev, RTM_NEWLINK);
  487. #endif
  488. return err;
  489. }
  490. #ifdef RT_LWIP_DHCP
  491. /**
  492. * This function will control network interface device DHCP capability enable or disable.
  493. *
  494. * @param netdev the network interface device device to change
  495. * @param is_enable the new DHCP status
  496. *
  497. * @return 0: set DHCP status successfully
  498. * -1: set DHCP status failed
  499. */
  500. int netdev_dhcp_enabled(struct netdev *netdev, rt_bool_t is_enabled)
  501. {
  502. RT_ASSERT(netdev);
  503. if (!netdev->ops || !netdev->ops->set_dhcp)
  504. {
  505. LOG_E("The network interface device(%s) not support to set DHCP status.", netdev->name);
  506. return -RT_ERROR;
  507. }
  508. /* network interface device DHCP flags check */
  509. if (netdev_is_dhcp_enabled(netdev) == is_enabled)
  510. {
  511. return RT_EOK;
  512. }
  513. /* execute network interface device DHCP capability control operations */
  514. return netdev->ops->set_dhcp(netdev, is_enabled);
  515. }
  516. int netdev_dhcp_open(char *netdev_name)
  517. {
  518. struct netdev *netdev = RT_NULL;
  519. netdev = netdev_get_by_name(netdev_name);
  520. if (netdev == RT_NULL)
  521. {
  522. rt_kprintf("bad network interface device name(%s).\n", netdev_name);
  523. return -1;
  524. }
  525. netdev_dhcp_enabled(netdev, RT_TRUE);
  526. return 0;
  527. }
  528. int netdev_dhcp_close(char *netdev_name)
  529. {
  530. struct netdev *netdev = RT_NULL;
  531. netdev = netdev_get_by_name(netdev_name);
  532. if (netdev == RT_NULL)
  533. {
  534. rt_kprintf("bad network interface device name(%s).\n", netdev_name);
  535. return -1;
  536. }
  537. netdev_dhcp_enabled(netdev, RT_FALSE);
  538. return 0;
  539. }
  540. #endif /* RT_LWIP_DHCP */
  541. /**
  542. * This function will set network interface device IP address.
  543. *
  544. * @param netdev the network interface device to change
  545. * @param ip_addr the new IP address
  546. *
  547. * @return 0: set IP address successfully
  548. * -1: set IP address failed
  549. */
  550. int netdev_set_ipaddr(struct netdev *netdev, const ip_addr_t *ip_addr)
  551. {
  552. int err;
  553. RT_ASSERT(netdev);
  554. RT_ASSERT(ip_addr);
  555. if (!netdev->ops || !netdev->ops->set_addr_info)
  556. {
  557. LOG_E("The network interface device(%s) not support to set IP address.", netdev->name);
  558. return -RT_ERROR;
  559. }
  560. if (netdev_is_dhcp_enabled(netdev))
  561. {
  562. LOG_E("The network interface device(%s) DHCP capability is enable, not support set IP address.", netdev->name);
  563. return -RT_ERROR;
  564. }
  565. /* execute network interface device set IP address operations */
  566. err = netdev->ops->set_addr_info(netdev, (ip_addr_t *)ip_addr, RT_NULL, RT_NULL);
  567. #if defined(SAL_USING_AF_NETLINK)
  568. if (err == 0)
  569. rtnl_ip_notify(netdev, RTM_SETLINK);
  570. #endif
  571. return err;
  572. }
  573. /**
  574. * This function will set network interface device netmask address.
  575. *
  576. * @param netdev the network interface device to change
  577. * @param netmask the new netmask address
  578. *
  579. * @return 0: set netmask address successfully
  580. * -1: set netmask address failed
  581. */
  582. int netdev_set_netmask(struct netdev *netdev, const ip_addr_t *netmask)
  583. {
  584. RT_ASSERT(netdev);
  585. RT_ASSERT(netmask);
  586. if (!netdev->ops || !netdev->ops->set_addr_info)
  587. {
  588. LOG_E("The network interface device(%s) not support to set netmask address.", netdev->name);
  589. return -RT_ERROR;
  590. }
  591. if (netdev_is_dhcp_enabled(netdev))
  592. {
  593. LOG_E("The network interface device(%s) DHCP capability is enable, not support set netmask address.", netdev->name);
  594. return -RT_ERROR;
  595. }
  596. /* execute network interface device set netmask address operations */
  597. return netdev->ops->set_addr_info(netdev, RT_NULL, (ip_addr_t *)netmask, RT_NULL);
  598. }
  599. /**
  600. * This function will set network interface device gateway address.
  601. *
  602. * @param netdev the network interface device to change
  603. * @param gw the new gateway address
  604. *
  605. * @return 0: set gateway address successfully
  606. * -1: set gateway address failed
  607. */
  608. int netdev_set_gw(struct netdev *netdev, const ip_addr_t *gw)
  609. {
  610. RT_ASSERT(netdev);
  611. RT_ASSERT(gw);
  612. if (!netdev->ops || !netdev->ops->set_addr_info)
  613. {
  614. LOG_E("The network interface device(%s) not support to set gateway address.", netdev->name);
  615. return -RT_ERROR;
  616. }
  617. if (netdev_is_dhcp_enabled(netdev))
  618. {
  619. LOG_E("The network interface device(%s) DHCP capability is enable, not support set gateway address.", netdev->name);
  620. return -RT_ERROR;
  621. }
  622. /* execute network interface device set gateway address operations */
  623. return netdev->ops->set_addr_info(netdev, RT_NULL, RT_NULL, (ip_addr_t *)gw);
  624. }
  625. /**
  626. * This function will try to get network device and set DNS server address.
  627. *
  628. * @param netdev_name the network interface device name
  629. * @param dns_num the number of the DNS server
  630. * @param dns_server the new DNS server address
  631. */
  632. void netdev_set_dns(char *netdev_name, uint8_t dns_num, char *dns_server)
  633. {
  634. struct netdev *netdev = RT_NULL;
  635. ip_addr_t dns_addr;
  636. netdev = netdev_get_by_name(netdev_name);
  637. if (netdev == RT_NULL)
  638. {
  639. rt_kprintf("bad network interface device name(%s).\n", netdev_name);
  640. return;
  641. }
  642. inet_aton(dns_server, &dns_addr);
  643. if (netdev_set_dns_server(netdev, dns_num, &dns_addr) == RT_EOK)
  644. {
  645. rt_kprintf("set network interface device(%s) dns server #%d: %s\n", netdev_name, dns_num, dns_server);
  646. }
  647. }
  648. /**
  649. * This function will set network interface device DNS server address.
  650. *
  651. * @param netdev the network interface device to change
  652. * @param dns_num the number of the DNS server
  653. * @param dns_server the new DNS server address
  654. *
  655. * @return 0: set netmask address successfully
  656. * -1: set netmask address failed
  657. */
  658. int netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server)
  659. {
  660. RT_ASSERT(netdev);
  661. RT_ASSERT(dns_server);
  662. if (dns_num >= NETDEV_DNS_SERVERS_NUM)
  663. {
  664. LOG_E("The number of DNS servers(%d) set exceeds the maximum number(%d).", dns_num + 1, NETDEV_DNS_SERVERS_NUM);
  665. return -RT_ERROR;
  666. }
  667. if (!netdev->ops || !netdev->ops->set_dns_server)
  668. {
  669. LOG_E("The network interface device(%s) not support to set DNS server address.", netdev->name);
  670. return -RT_ERROR;
  671. }
  672. /* execute network interface device set DNS server address operations */
  673. return netdev->ops->set_dns_server(netdev, dns_num, (ip_addr_t *)dns_server);
  674. }
  675. /**
  676. * This function will set network interface device IP, gateway and netmask address according to device name.
  677. *
  678. * @param netdev_name the network interface device name
  679. * @param ip_addr the new IP address
  680. * @param gw_addr the new gateway address
  681. * @param nm_addr the new netmask address
  682. */
  683. void netdev_set_if(char *netdev_name, char *ip_addr, char *gw_addr, char *nm_addr)
  684. {
  685. struct netdev *netdev = RT_NULL;
  686. ip_addr_t addr;
  687. netdev = netdev_get_by_name(netdev_name);
  688. if (netdev == RT_NULL)
  689. {
  690. rt_kprintf("bad network interface device name(%s).\n", netdev_name);
  691. return;
  692. }
  693. #ifdef RT_LWIP_DHCP
  694. netdev_dhcp_close(netdev_name);
  695. #endif
  696. /* set IP address */
  697. if ((ip_addr != RT_NULL) && inet_aton(ip_addr, &addr))
  698. {
  699. netdev_set_ipaddr(netdev, &addr);
  700. }
  701. /* set gateway address */
  702. if ((gw_addr != RT_NULL) && inet_aton(gw_addr, &addr))
  703. {
  704. netdev_set_gw(netdev, &addr);
  705. }
  706. /* set netmask address */
  707. if ((nm_addr != RT_NULL) && inet_aton(nm_addr, &addr))
  708. {
  709. netdev_set_netmask(netdev, &addr);
  710. }
  711. }
  712. /**
  713. * This function will set callback to be called when the network interface device status has been changed.
  714. *
  715. * @param netdev the network interface device to change
  716. * @param status_callback the callback be called when the status has been changed.
  717. */
  718. void netdev_set_status_callback(struct netdev *netdev, netdev_callback_fn status_callback)
  719. {
  720. RT_ASSERT(netdev);
  721. RT_ASSERT(status_callback);
  722. netdev->status_callback = status_callback;
  723. }
  724. /**
  725. * This function will set callback to be called when the network interface device address has been changed.
  726. *
  727. * @param netdev the network interface device to change
  728. * @param addr_callback the callback be called when the address has been changed.
  729. */
  730. void netdev_set_addr_callback(struct netdev *netdev, netdev_callback_fn addr_callback)
  731. {
  732. RT_ASSERT(netdev);
  733. RT_ASSERT(addr_callback);
  734. netdev->addr_callback = addr_callback;
  735. }
  736. /**
  737. * This function will set network interface device IP address.
  738. * @NOTE it can only be called in the network interface device driver.
  739. *
  740. * @param netdev the network interface device to change
  741. * @param ip_addr the new IP address
  742. */
  743. void netdev_low_level_set_ipaddr(struct netdev *netdev, const ip_addr_t *ip_addr)
  744. {
  745. RT_ASSERT(ip_addr);
  746. if (netdev && ip_addr_cmp(&(netdev->ip_addr), ip_addr) == 0)
  747. {
  748. ip_addr_copy(netdev->ip_addr, *ip_addr);
  749. #ifdef RT_USING_SAL
  750. /* set network interface device flags to internet up */
  751. if (netdev_is_up(netdev) && netdev_is_link_up(netdev))
  752. {
  753. sal_check_netdev_internet_up(netdev);
  754. }
  755. #endif /* RT_USING_SAL */
  756. /* execute IP address change callback function */
  757. if (netdev->addr_callback)
  758. {
  759. netdev->addr_callback(netdev, NETDEV_CB_ADDR_IP);
  760. }
  761. }
  762. }
  763. /**
  764. * This function will set network interface device netmask address.
  765. * @NOTE it can only be called in the network interface device driver.
  766. *
  767. * @param netdev the network interface device to change
  768. * @param netmask the new netmask address
  769. */
  770. void netdev_low_level_set_netmask(struct netdev *netdev, const ip_addr_t *netmask)
  771. {
  772. RT_ASSERT(netmask);
  773. if (netdev && ip_addr_cmp(&(netdev->netmask), netmask) == 0)
  774. {
  775. ip_addr_copy(netdev->netmask, *netmask);
  776. #ifdef RT_USING_SAL
  777. /* set network interface device flags to internet up */
  778. if (netdev_is_up(netdev) && netdev_is_link_up(netdev) &&
  779. !ip_addr_isany(&(netdev->ip_addr)))
  780. {
  781. sal_check_netdev_internet_up(netdev);
  782. }
  783. #endif /* RT_USING_SAL */
  784. /* execute netmask address change callback function */
  785. if (netdev->addr_callback)
  786. {
  787. netdev->addr_callback(netdev, NETDEV_CB_ADDR_NETMASK);
  788. }
  789. }
  790. }
  791. /**
  792. * This function will set network interface device gateway address.
  793. * @NOTE it can only be called in the network interface device driver.
  794. *
  795. * @param netdev the network interface device to change
  796. * @param gw the new gateway address
  797. */
  798. void netdev_low_level_set_gw(struct netdev *netdev, const ip_addr_t *gw)
  799. {
  800. RT_ASSERT(gw);
  801. if (netdev && ip_addr_cmp(&(netdev->gw), gw) == 0)
  802. {
  803. ip_addr_copy(netdev->gw, *gw);
  804. #ifdef RT_USING_SAL
  805. /* set network interface device flags to internet up */
  806. if (netdev_is_up(netdev) && netdev_is_link_up(netdev) &&
  807. !ip_addr_isany(&(netdev->ip_addr)))
  808. {
  809. sal_check_netdev_internet_up(netdev);
  810. }
  811. #endif /* RT_USING_SAL */
  812. /* execute gateway address change callback function */
  813. if (netdev->addr_callback)
  814. {
  815. netdev->addr_callback(netdev, NETDEV_CB_ADDR_GATEWAY);
  816. }
  817. }
  818. }
  819. /**
  820. * This function will set network interface device DNS server address.
  821. * @NOTE it can only be called in the network interface device driver.
  822. *
  823. * @param netdev the network interface device to change
  824. * @param dns_num the number of the DNS server
  825. * @param dns_server the new DNS server address
  826. *
  827. */
  828. void netdev_low_level_set_dns_server(struct netdev *netdev, uint8_t dns_num, const ip_addr_t *dns_server)
  829. {
  830. unsigned int index;
  831. RT_ASSERT(dns_server);
  832. if (netdev == RT_NULL)
  833. {
  834. return;
  835. }
  836. /* check DNS servers is exist */
  837. for (index = 0; index < NETDEV_DNS_SERVERS_NUM; index++)
  838. {
  839. if (ip_addr_cmp(&(netdev->dns_servers[index]), dns_server))
  840. {
  841. return;
  842. }
  843. }
  844. if (dns_num < NETDEV_DNS_SERVERS_NUM)
  845. {
  846. ip_addr_copy(netdev->dns_servers[dns_num], *dns_server);
  847. /* execute DNS servers address change callback function */
  848. if (netdev->addr_callback)
  849. {
  850. netdev->addr_callback(netdev, NETDEV_CB_ADDR_DNS_SERVER);
  851. }
  852. }
  853. }
  854. #ifdef NETDEV_USING_AUTO_DEFAULT
  855. /* Change to the first link_up network interface device automatically */
  856. static void netdev_auto_change_default(struct netdev *netdev)
  857. {
  858. struct netdev *new_netdev = RT_NULL;
  859. if (netdev->flags & NETDEV_FLAG_LINK_UP)
  860. {
  861. if (!(netdev_default->flags & NETDEV_FLAG_LINK_UP))
  862. {
  863. netdev_set_default(netdev);
  864. }
  865. return;
  866. }
  867. if (rt_memcmp(netdev, netdev_default, sizeof(struct netdev)) == 0)
  868. {
  869. new_netdev = netdev_get_first_by_flags(NETDEV_FLAG_LINK_UP);
  870. if (new_netdev)
  871. {
  872. netdev_set_default(new_netdev);
  873. }
  874. }
  875. }
  876. #endif /* NETDEV_USING_AUTO_DEFAULT */
  877. /**
  878. * This function will set network interface device status.
  879. * @NOTE it can only be called in the network interface device driver.
  880. *
  881. * @param netdev the network interface device to change
  882. * @param is_up the new status
  883. */
  884. void netdev_low_level_set_status(struct netdev *netdev, rt_bool_t is_up)
  885. {
  886. if (netdev && netdev_is_up(netdev) != is_up)
  887. {
  888. if (is_up)
  889. {
  890. netdev->flags |= NETDEV_FLAG_UP;
  891. }
  892. else
  893. {
  894. netdev->flags &= ~NETDEV_FLAG_UP;
  895. #ifdef NETDEV_USING_AUTO_DEFAULT
  896. /* change to the first link_up network interface device automatically */
  897. netdev_auto_change_default(netdev);
  898. #endif /* NETDEV_USING_AUTO_DEFAULT */
  899. }
  900. /* execute network interface device status change callback function */
  901. if (netdev->status_callback)
  902. {
  903. netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_UP : NETDEV_CB_STATUS_DOWN);
  904. }
  905. }
  906. }
  907. /**
  908. * This function will set network interface device active link status.
  909. * @NOTE it can only be called in the network interface device driver.
  910. *
  911. * @param netdev the network interface device to change
  912. * @param is_up the new link status
  913. */
  914. void netdev_low_level_set_link_status(struct netdev *netdev, rt_bool_t is_up)
  915. {
  916. if (netdev && netdev_is_link_up(netdev) != is_up)
  917. {
  918. if (is_up)
  919. {
  920. netdev->flags |= NETDEV_FLAG_LINK_UP;
  921. #ifdef RT_USING_SAL
  922. /* set network interface device flags to internet up */
  923. if (netdev_is_up(netdev) && !ip_addr_isany(&(netdev->ip_addr)))
  924. {
  925. sal_check_netdev_internet_up(netdev);
  926. }
  927. #endif /* RT_USING_SAL */
  928. }
  929. else
  930. {
  931. netdev->flags &= ~NETDEV_FLAG_LINK_UP;
  932. /* set network interface device flags to internet down */
  933. netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
  934. #ifdef NETDEV_USING_AUTO_DEFAULT
  935. /* change to the first link_up network interface device automatically */
  936. netdev_auto_change_default(netdev);
  937. #endif /* NETDEV_USING_AUTO_DEFAULT */
  938. }
  939. /* execute link status change callback function */
  940. if (netdev->status_callback)
  941. {
  942. netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_LINK_UP : NETDEV_CB_STATUS_LINK_DOWN);
  943. }
  944. }
  945. }
  946. /**
  947. * This function will set network interface device active internet status.
  948. * @NOTE it can only be called in the network interface device driver.
  949. *
  950. * @param netdev the network interface device to change
  951. * @param is_up the new internet status
  952. */
  953. void netdev_low_level_set_internet_status(struct netdev *netdev, rt_bool_t is_up)
  954. {
  955. if (netdev && netdev_is_internet_up(netdev) != is_up)
  956. {
  957. if (is_up)
  958. {
  959. netdev->flags |= NETDEV_FLAG_INTERNET_UP;
  960. }
  961. else
  962. {
  963. netdev->flags &= ~NETDEV_FLAG_INTERNET_UP;
  964. }
  965. /* execute network interface device status change callback function */
  966. if (netdev->status_callback)
  967. {
  968. netdev->status_callback(netdev, is_up ? NETDEV_CB_STATUS_INTERNET_UP : NETDEV_CB_STATUS_INTERNET_DOWN);
  969. }
  970. }
  971. }
  972. /**
  973. * This function will set network interface device DHCP status.
  974. * @NOTE it can only be called in the network interface device driver.
  975. *
  976. * @param netdev the network interface device to change
  977. * @param is_up the new DHCP status
  978. */
  979. void netdev_low_level_set_dhcp_status(struct netdev *netdev, rt_bool_t is_enable)
  980. {
  981. if (netdev && netdev_is_dhcp_enabled(netdev) != is_enable)
  982. {
  983. if (is_enable)
  984. {
  985. netdev->flags |= NETDEV_FLAG_DHCP;
  986. }
  987. else
  988. {
  989. netdev->flags &= ~NETDEV_FLAG_DHCP;
  990. }
  991. /* execute DHCP status change callback function */
  992. if (netdev->status_callback)
  993. {
  994. netdev->status_callback(netdev, is_enable ? NETDEV_CB_STATUS_DHCP_ENABLE : NETDEV_CB_STATUS_DHCP_DISABLE);
  995. }
  996. }
  997. }
  998. #ifdef RT_USING_FINSH
  999. #include <finsh.h>
  1000. #ifdef NETDEV_USING_IFCONFIG
  1001. static void netdev_list_if(void)
  1002. {
  1003. #define NETDEV_IFCONFIG_MAC_MAX_LEN 6
  1004. #define NETDEV_IFCONFIG_IMEI_MAX_LEN 8
  1005. rt_ubase_t index;
  1006. rt_slist_t *node = RT_NULL;
  1007. struct netdev *netdev = RT_NULL;
  1008. struct netdev *cur_netdev_list = netdev_list;
  1009. if (cur_netdev_list == RT_NULL)
  1010. {
  1011. rt_kprintf("ifconfig: network interface device list error.\n");
  1012. return;
  1013. }
  1014. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1015. {
  1016. netdev = rt_list_entry(node, struct netdev, list);
  1017. rt_kprintf("network interface device: %.*s%s\n",
  1018. RT_NAME_MAX, netdev->name,
  1019. (netdev == netdev_default) ? " (Default)" : "");
  1020. rt_kprintf("MTU: %d\n", netdev->mtu);
  1021. /* 6 - MAC address, 8 - IEMI */
  1022. if (netdev->hwaddr_len == NETDEV_IFCONFIG_MAC_MAX_LEN)
  1023. {
  1024. rt_kprintf("MAC: ");
  1025. for (index = 0; index < netdev->hwaddr_len; index++)
  1026. {
  1027. rt_kprintf("%02x ", netdev->hwaddr[index]);
  1028. }
  1029. }
  1030. else if (netdev->hwaddr_len == NETDEV_IFCONFIG_IMEI_MAX_LEN)
  1031. {
  1032. rt_kprintf("IMEI: ");
  1033. for (index = 0; index < netdev->hwaddr_len; index++)
  1034. {
  1035. /* two numbers are displayed at one time*/
  1036. if (netdev->hwaddr[index] < 10 && index != netdev->hwaddr_len - 1)
  1037. {
  1038. rt_kprintf("%02d", netdev->hwaddr[index]);
  1039. }
  1040. else
  1041. {
  1042. rt_kprintf("%d", netdev->hwaddr[index]);
  1043. }
  1044. }
  1045. }
  1046. rt_kprintf("\nFLAGS:");
  1047. if (netdev->flags & NETDEV_FLAG_UP) rt_kprintf(" UP");
  1048. else rt_kprintf(" DOWN");
  1049. if (netdev->flags & NETDEV_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  1050. else rt_kprintf(" LINK_DOWN");
  1051. #ifdef SAL_INTERNET_CHECK
  1052. if (netdev->flags & NETDEV_FLAG_INTERNET_UP) rt_kprintf(" INTERNET_UP");
  1053. else rt_kprintf(" INTERNET_DOWN");
  1054. #endif
  1055. if (netdev->flags & NETDEV_FLAG_DHCP) rt_kprintf(" DHCP_ENABLE");
  1056. else rt_kprintf(" DHCP_DISABLE");
  1057. if (netdev->flags & NETDEV_FLAG_ETHARP) rt_kprintf(" ETHARP");
  1058. if (netdev->flags & NETDEV_FLAG_BROADCAST) rt_kprintf(" BROADCAST");
  1059. if (netdev->flags & NETDEV_FLAG_IGMP) rt_kprintf(" IGMP");
  1060. rt_kprintf("\n");
  1061. rt_kprintf("ip address: %s\n", inet_ntoa(netdev->ip_addr));
  1062. rt_kprintf("gw address: %s\n", inet_ntoa(netdev->gw));
  1063. rt_kprintf("net mask : %s\n", inet_ntoa(netdev->netmask));
  1064. #if NETDEV_IPV6
  1065. {
  1066. ip_addr_t *addr;
  1067. int i;
  1068. addr = &netdev->ip6_addr[0];
  1069. if (!ip_addr_isany(addr))
  1070. {
  1071. rt_kprintf("ipv6 link-local: %s %s\n", inet_ntoa(*addr),
  1072. !ip_addr_isany(addr) ? "VALID" : "INVALID");
  1073. for (i = 1; i < NETDEV_IPV6_NUM_ADDRESSES; i++)
  1074. {
  1075. addr = &netdev->ip6_addr[i];
  1076. rt_kprintf("ipv6[%d] address: %s %s\n", i, inet_ntoa(*addr),
  1077. !ip_addr_isany(addr) ? "VALID" : "INVALID");
  1078. }
  1079. }
  1080. }
  1081. #endif /* NETDEV_IPV6 */
  1082. for (index = 0; index < NETDEV_DNS_SERVERS_NUM; index++)
  1083. {
  1084. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(netdev->dns_servers[index]));
  1085. }
  1086. if (rt_slist_next(node))
  1087. {
  1088. rt_kprintf("\n");
  1089. }
  1090. }
  1091. }
  1092. int netdev_ifconfig(int argc, char **argv)
  1093. {
  1094. if (argc == 1)
  1095. {
  1096. netdev_list_if();
  1097. }
  1098. #ifdef RT_LWIP_DHCP
  1099. else if(argc == 3)
  1100. {
  1101. if (!strcmp(argv[2], "dhcp"))
  1102. {
  1103. netdev_dhcp_open(argv[1]);
  1104. }
  1105. }
  1106. #endif
  1107. else if (argc == 5)
  1108. {
  1109. rt_kprintf("config : %s\n", argv[1]);
  1110. rt_kprintf("IP addr: %s\n", argv[2]);
  1111. rt_kprintf("Gateway: %s\n", argv[3]);
  1112. rt_kprintf("netmask: %s\n", argv[4]);
  1113. netdev_set_if(argv[1], argv[2], argv[3], argv[4]);
  1114. }
  1115. else
  1116. {
  1117. rt_kprintf("bad parameter! e.g: ifconfig e0 192.168.1.30 192.168.1.1 255.255.255.0\n");
  1118. #ifdef RT_LWIP_DHCP
  1119. rt_kprintf("bad parameter! e.g: ifconfig e0 dhcp\n");
  1120. #endif
  1121. }
  1122. return 0;
  1123. }
  1124. MSH_CMD_EXPORT_ALIAS(netdev_ifconfig, ifconfig, list the information of all network interfaces);
  1125. #endif /* NETDEV_USING_IFCONFIG */
  1126. #ifdef NETDEV_USING_PING
  1127. int netdev_cmd_ping(char* target_name, char *netdev_name, rt_uint32_t times, rt_size_t size)
  1128. {
  1129. #define NETDEV_PING_DATA_SIZE 32
  1130. /** ping receive timeout - in milliseconds */
  1131. #define NETDEV_PING_RECV_TIMEO (2 * RT_TICK_PER_SECOND)
  1132. /** ping delay - in milliseconds */
  1133. #define NETDEV_PING_DELAY (1 * RT_TICK_PER_SECOND)
  1134. /* check netdev ping options */
  1135. #define NETDEV_PING_IS_COMMONICABLE(netdev) \
  1136. ((netdev) && (netdev)->ops && (netdev)->ops->ping && \
  1137. netdev_is_up(netdev) && netdev_is_link_up(netdev)) \
  1138. struct netdev *netdev = RT_NULL;
  1139. struct netdev_ping_resp ping_resp;
  1140. rt_uint32_t index, received, loss, max_time, min_time, avg_time;
  1141. int ret = 0;
  1142. rt_bool_t isbind = RT_FALSE;
  1143. if (size == 0)
  1144. {
  1145. size = NETDEV_PING_DATA_SIZE;
  1146. }
  1147. if (netdev_name != RT_NULL)
  1148. {
  1149. netdev = netdev_get_by_name(netdev_name);
  1150. isbind = RT_TRUE;
  1151. }
  1152. if (netdev == RT_NULL)
  1153. {
  1154. netdev = netdev_default;
  1155. rt_kprintf("ping: not found specified netif, using default netdev %s.\n", netdev->name);
  1156. }
  1157. if (!NETDEV_PING_IS_COMMONICABLE(netdev))
  1158. {
  1159. if (netdev == RT_NULL)
  1160. {
  1161. rt_kprintf("ping: not found available network interface device.\n");
  1162. return -RT_ERROR;
  1163. }
  1164. else if (netdev->ops == RT_NULL || netdev->ops->ping == RT_NULL)
  1165. {
  1166. rt_kprintf("ping: network interface device(%s) not support ping feature.\n", netdev->name);
  1167. return -RT_ERROR;
  1168. }
  1169. else if (!netdev_is_up(netdev) || !netdev_is_link_up(netdev))
  1170. {
  1171. rt_kprintf("ping: network interface device(%s) status error.\n", netdev->name);
  1172. return -RT_ERROR;
  1173. }
  1174. }
  1175. max_time = avg_time = received = 0;
  1176. min_time = 0xFFFFFFFF;
  1177. for (index = 0; index < times; index++)
  1178. {
  1179. int delay_tick = 0;
  1180. rt_tick_t start_tick = 0;
  1181. rt_memset(&ping_resp, 0x00, sizeof(struct netdev_ping_resp));
  1182. start_tick = rt_tick_get();
  1183. ret = netdev->ops->ping(netdev, (const char *)target_name, size, NETDEV_PING_RECV_TIMEO, &ping_resp, isbind);
  1184. if (ret == -RT_ETIMEOUT)
  1185. {
  1186. rt_kprintf("ping: from %s icmp_seq=%d timeout\n",
  1187. (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr), index + 1);
  1188. }
  1189. else if (ret == -RT_ERROR)
  1190. {
  1191. rt_kprintf("ping: unknown %s %s\n",
  1192. (ip_addr_isany(&(ping_resp.ip_addr))) ? "host" : "address",
  1193. (ip_addr_isany(&(ping_resp.ip_addr))) ? target_name : inet_ntoa(ping_resp.ip_addr));
  1194. }
  1195. else
  1196. {
  1197. if (ping_resp.ttl == 0)
  1198. {
  1199. rt_kprintf("%d bytes from %s icmp_seq=%d time=%d ms\n",
  1200. ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ticks);
  1201. }
  1202. else
  1203. {
  1204. rt_kprintf("%d bytes from %s icmp_seq=%d ttl=%d time=%d ms\n",
  1205. ping_resp.data_len, inet_ntoa(ping_resp.ip_addr), index + 1, ping_resp.ttl, ping_resp.ticks);
  1206. }
  1207. received += 1;
  1208. if (ping_resp.ticks > max_time)
  1209. {
  1210. max_time = ping_resp.ticks;
  1211. }
  1212. else if (ping_resp.ticks < min_time)
  1213. {
  1214. min_time = ping_resp.ticks;
  1215. }
  1216. avg_time += ping_resp.ticks;
  1217. }
  1218. /* if the response time is more than NETDEV_PING_DELAY, no need to delay */
  1219. delay_tick = ((rt_tick_get() - start_tick) > NETDEV_PING_DELAY) || (index == times) ? 0 : NETDEV_PING_DELAY;
  1220. rt_thread_delay(delay_tick);
  1221. }
  1222. /* print ping statistics */
  1223. loss = (uint32_t)((1 - ((float)received) / index) * 100);
  1224. avg_time = (uint32_t)(avg_time / received);
  1225. #if NETDEV_IPV4 && NETDEV_IPV6
  1226. if (IP_IS_V4_VAL(&ping_resp.ip_addr))
  1227. {
  1228. rt_kprintf("\n--- %s ping statistics ---\n", inet_ntoa(*ip_2_ip4(&ping_resp.ip_addr)));
  1229. }
  1230. else
  1231. {
  1232. rt_kprintf("\n--- %s ping statistics ---\n", inet6_ntoa(*ip_2_ip6(&ping_resp.ip_addr)));
  1233. }
  1234. #elif NETDEV_IPV4
  1235. rt_kprintf("\n--- %s ping statistics ---\n", inet_ntoa(ping_resp.ip_addr));
  1236. #elif NETDEV_IPV6
  1237. rt_kprintf("\n--- %s ping statistics ---\n", inet6_ntoa(ping_resp.ip_addr));
  1238. #endif
  1239. rt_kprintf("%d packets transmitted, %d received, %d%% packet loss\n", index, received, loss);
  1240. if (received > 0)
  1241. {
  1242. rt_kprintf("minimum = %dms, maximum = %dms, average = %dms\n", min_time, max_time, avg_time);
  1243. }
  1244. return RT_EOK;
  1245. }
  1246. int netdev_ping(int argc, char **argv)
  1247. {
  1248. if (argc == 1)
  1249. {
  1250. rt_kprintf("Please input: ping <host address> [netdev name] [times] [data size]\n");
  1251. }
  1252. else if (argc == 2)
  1253. {
  1254. netdev_cmd_ping(argv[1], RT_NULL, 4, 0);
  1255. }
  1256. else if (argc == 3)
  1257. {
  1258. netdev_cmd_ping(argv[1], argv[2], 4, 0);
  1259. }
  1260. else if (argc == 4)
  1261. {
  1262. netdev_cmd_ping(argv[1], argv[2], atoi(argv[3]), 0);
  1263. }
  1264. else if (argc == 5)
  1265. {
  1266. netdev_cmd_ping(argv[1], argv[2], atoi(argv[3]), atoi(argv[4]));
  1267. }
  1268. return 0;
  1269. }
  1270. MSH_CMD_EXPORT_ALIAS(netdev_ping, ping, ping network host);
  1271. #endif /* NETDEV_USING_PING */
  1272. static void netdev_list_dns(void)
  1273. {
  1274. unsigned int index = 0;
  1275. struct netdev *netdev = RT_NULL;
  1276. rt_slist_t *node = RT_NULL;
  1277. for (node = &(netdev_list->list); node; node = rt_slist_next(node))
  1278. {
  1279. netdev = rt_list_entry(node, struct netdev, list);
  1280. rt_kprintf("network interface device: %.*s%s\n",
  1281. RT_NAME_MAX, netdev->name,
  1282. (netdev == netdev_default)?" (Default)":"");
  1283. for(index = 0; index < NETDEV_DNS_SERVERS_NUM; index++)
  1284. {
  1285. rt_kprintf("dns server #%d: %s\n", index, inet_ntoa(netdev->dns_servers[index]));
  1286. }
  1287. if (rt_slist_next(node))
  1288. {
  1289. rt_kprintf("\n");
  1290. }
  1291. }
  1292. }
  1293. int netdev_dns(int argc, char **argv)
  1294. {
  1295. if (argc == 1)
  1296. {
  1297. netdev_list_dns();
  1298. }
  1299. else if (argc == 3)
  1300. {
  1301. netdev_set_dns(argv[1], 0, argv[2]);
  1302. }
  1303. else if (argc == 4)
  1304. {
  1305. netdev_set_dns(argv[1], atoi(argv[2]), argv[3]);
  1306. }
  1307. else
  1308. {
  1309. rt_kprintf("bad parameter! input: dns <netdev_name> [dns_num] <dns_server>\n");
  1310. return -1;
  1311. }
  1312. return 0;
  1313. }
  1314. MSH_CMD_EXPORT_ALIAS(netdev_dns, dns, list and set the information of dns);
  1315. #ifdef NETDEV_USING_NETSTAT
  1316. static void netdev_cmd_netstat(void)
  1317. {
  1318. rt_slist_t *node = RT_NULL;
  1319. struct netdev *netdev = RT_NULL;
  1320. struct netdev *cur_netdev_list = netdev_list;
  1321. if (cur_netdev_list == RT_NULL)
  1322. {
  1323. rt_kprintf("netstat: network interface device list error.\n");
  1324. return;
  1325. }
  1326. for (node = &(cur_netdev_list->list); node; node = rt_slist_next(node))
  1327. {
  1328. netdev = rt_list_entry(node, struct netdev, list);
  1329. if (netdev && netdev->ops && netdev->ops->netstat)
  1330. {
  1331. break;
  1332. }
  1333. }
  1334. if (netdev->ops->netstat != RT_NULL)
  1335. {
  1336. netdev->ops->netstat(netdev);
  1337. }
  1338. else
  1339. {
  1340. rt_kprintf("netstat: this command is not supported!\n");
  1341. }
  1342. }
  1343. int netdev_netstat(int argc, char **argv)
  1344. {
  1345. if (argc != 1)
  1346. {
  1347. rt_kprintf("Please input: netstat \n");
  1348. }
  1349. else
  1350. {
  1351. netdev_cmd_netstat();
  1352. }
  1353. return 0;
  1354. }
  1355. MSH_CMD_EXPORT_ALIAS(netdev_netstat, netstat, list the information of TCP / IP);
  1356. #endif /* NETDEV_USING_NETSTAT */
  1357. #endif /* RT_USING_FINSH */