Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ifmgmt.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <ipxe/console.h>
  25. #include <ipxe/netdevice.h>
  26. #include <ipxe/device.h>
  27. #include <ipxe/job.h>
  28. #include <ipxe/monojob.h>
  29. #include <ipxe/timer.h>
  30. #include <usr/ifmgmt.h>
  31. /** @file
  32. *
  33. * Network interface management
  34. *
  35. */
  36. /** Default time to wait for link-up */
  37. #define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
  38. /** Default unsuccessful configuration status code */
  39. #define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
  40. #define EINFO_EADDRNOTAVAIL_CONFIG \
  41. __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
  42. "No configuration methods succeeded" )
  43. /**
  44. * Open network device
  45. *
  46. * @v netdev Network device
  47. * @ret rc Return status code
  48. */
  49. int ifopen ( struct net_device *netdev ) {
  50. int rc;
  51. if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
  52. printf ( "Could not open %s: %s\n",
  53. netdev->name, strerror ( rc ) );
  54. return rc;
  55. }
  56. return 0;
  57. }
  58. /**
  59. * Close network device
  60. *
  61. * @v netdev Network device
  62. */
  63. void ifclose ( struct net_device *netdev ) {
  64. netdev_close ( netdev );
  65. }
  66. /**
  67. * Print network device error breakdown
  68. *
  69. * @v stats Network device statistics
  70. * @v prefix Message prefix
  71. */
  72. static void ifstat_errors ( struct net_device_stats *stats,
  73. const char *prefix ) {
  74. unsigned int i;
  75. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  76. sizeof ( stats->errors[0] ) ) ; i++ ) {
  77. if ( stats->errors[i].count )
  78. printf ( " [%s: %d x \"%s\"]\n", prefix,
  79. stats->errors[i].count,
  80. strerror ( stats->errors[i].rc ) );
  81. }
  82. }
  83. /**
  84. * Print status of network device
  85. *
  86. * @v netdev Network device
  87. */
  88. void ifstat ( struct net_device *netdev ) {
  89. printf ( "%s: %s using %s on %s (%s)\n"
  90. " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
  91. netdev->name, netdev_addr ( netdev ),
  92. netdev->dev->driver_name, netdev->dev->name,
  93. ( netdev_is_open ( netdev ) ? "open" : "closed" ),
  94. ( netdev_link_ok ( netdev ) ? "up" : "down" ),
  95. netdev->tx_stats.good, netdev->tx_stats.bad,
  96. netdev->rx_stats.good, netdev->rx_stats.bad );
  97. if ( ! netdev_link_ok ( netdev ) ) {
  98. printf ( " [Link status: %s]\n",
  99. strerror ( netdev->link_rc ) );
  100. }
  101. ifstat_errors ( &netdev->tx_stats, "TXE" );
  102. ifstat_errors ( &netdev->rx_stats, "RXE" );
  103. }
  104. /** Network device poller */
  105. struct ifpoller {
  106. /** Job control interface */
  107. struct interface job;
  108. /** Network device */
  109. struct net_device *netdev;
  110. /** Network device configurator (if applicable) */
  111. struct net_device_configurator *configurator;
  112. /**
  113. * Check progress
  114. *
  115. * @v ifpoller Network device poller
  116. * @ret ongoing_rc Ongoing job status code (if known)
  117. */
  118. int ( * progress ) ( struct ifpoller *ifpoller );
  119. };
  120. /**
  121. * Report network device poller progress
  122. *
  123. * @v ifpoller Network device poller
  124. * @v progress Progress report to fill in
  125. * @ret ongoing_rc Ongoing job status code (if known)
  126. */
  127. static int ifpoller_progress ( struct ifpoller *ifpoller,
  128. struct job_progress *progress __unused ) {
  129. /* Hand off to current progress checker */
  130. return ifpoller->progress ( ifpoller );
  131. }
  132. /** Network device poller operations */
  133. static struct interface_operation ifpoller_job_op[] = {
  134. INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
  135. };
  136. /** Network device poller descriptor */
  137. static struct interface_descriptor ifpoller_job_desc =
  138. INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
  139. /**
  140. * Poll network device until completion
  141. *
  142. * @v netdev Network device
  143. * @v configurator Network device configurator (if applicable)
  144. * @v timeout Timeout period, in ticks
  145. * @v progress Method to check progress
  146. * @ret rc Return status code
  147. */
  148. static int ifpoller_wait ( struct net_device *netdev,
  149. struct net_device_configurator *configurator,
  150. unsigned long timeout,
  151. int ( * progress ) ( struct ifpoller *ifpoller ) ) {
  152. static struct ifpoller ifpoller = {
  153. .job = INTF_INIT ( ifpoller_job_desc ),
  154. };
  155. ifpoller.netdev = netdev;
  156. ifpoller.configurator = configurator;
  157. ifpoller.progress = progress;
  158. intf_plug_plug ( &monojob, &ifpoller.job );
  159. return monojob_wait ( "", timeout );
  160. }
  161. /**
  162. * Check link-up progress
  163. *
  164. * @v ifpoller Network device poller
  165. * @ret ongoing_rc Ongoing job status code (if known)
  166. */
  167. static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
  168. struct net_device *netdev = ifpoller->netdev;
  169. int ongoing_rc = netdev->link_rc;
  170. /* Terminate successfully if link is up */
  171. if ( ongoing_rc == 0 )
  172. intf_close ( &ifpoller->job, 0 );
  173. /* Otherwise, report link status as ongoing job status */
  174. return ongoing_rc;
  175. }
  176. /**
  177. * Wait for link-up, with status indication
  178. *
  179. * @v netdev Network device
  180. * @v timeout Timeout period, in ticks
  181. */
  182. int iflinkwait ( struct net_device *netdev, unsigned long timeout ) {
  183. int rc;
  184. /* Ensure device is open */
  185. if ( ( rc = ifopen ( netdev ) ) != 0 )
  186. return rc;
  187. /* Return immediately if link is already up */
  188. netdev_poll ( netdev );
  189. if ( netdev_link_ok ( netdev ) )
  190. return 0;
  191. /* Wait for link-up */
  192. printf ( "Waiting for link-up on %s", netdev->name );
  193. return ifpoller_wait ( netdev, NULL, timeout, iflinkwait_progress );
  194. }
  195. /**
  196. * Check configuration progress
  197. *
  198. * @v ifpoller Network device poller
  199. * @ret ongoing_rc Ongoing job status code (if known)
  200. */
  201. static int ifconf_progress ( struct ifpoller *ifpoller ) {
  202. struct net_device *netdev = ifpoller->netdev;
  203. struct net_device_configurator *configurator = ifpoller->configurator;
  204. struct net_device_configuration *config;
  205. int rc;
  206. /* Do nothing unless configuration has completed */
  207. if ( netdev_configuration_in_progress ( netdev ) )
  208. return 0;
  209. /* Terminate with appropriate overall return status code */
  210. if ( configurator ) {
  211. config = netdev_configuration ( netdev, configurator );
  212. rc = config->rc;
  213. } else {
  214. rc = ( netdev_configuration_ok ( netdev ) ?
  215. 0 : -EADDRNOTAVAIL_CONFIG );
  216. }
  217. intf_close ( &ifpoller->job, rc );
  218. return rc;
  219. }
  220. /**
  221. * Perform network device configuration
  222. *
  223. * @v netdev Network device
  224. * @v configurator Network device configurator, or NULL to use all
  225. * @ret rc Return status code
  226. */
  227. int ifconf ( struct net_device *netdev,
  228. struct net_device_configurator *configurator ) {
  229. int rc;
  230. /* Ensure device is open and link is up */
  231. if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT ) ) != 0 )
  232. return rc;
  233. /* Start configuration */
  234. if ( configurator ) {
  235. if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
  236. printf ( "Could not configure %s via %s: %s\n",
  237. netdev->name, configurator->name,
  238. strerror ( rc ) );
  239. return rc;
  240. }
  241. } else {
  242. if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
  243. printf ( "Could not configure %s: %s\n",
  244. netdev->name, strerror ( rc ) );
  245. return rc;
  246. }
  247. }
  248. /* Wait for configuration to complete */
  249. printf ( "Configuring %s%s%s(%s %s)",
  250. ( configurator ? "[" : "" ),
  251. ( configurator ? configurator->name : "" ),
  252. ( configurator ? "] " : "" ),
  253. netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
  254. return ifpoller_wait ( netdev, configurator, 0, ifconf_progress );
  255. }