You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ifmgmt.c 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/nap.h>
  30. #include <ipxe/timer.h>
  31. #include <usr/ifmgmt.h>
  32. /** @file
  33. *
  34. * Network interface management
  35. *
  36. */
  37. /** Default time to wait for link-up */
  38. #define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
  39. /** Default unsuccessful configuration status code */
  40. #define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
  41. #define EINFO_EADDRNOTAVAIL_CONFIG \
  42. __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
  43. "No configuration methods succeeded" )
  44. /**
  45. * Open network device
  46. *
  47. * @v netdev Network device
  48. * @ret rc Return status code
  49. */
  50. int ifopen ( struct net_device *netdev ) {
  51. int rc;
  52. if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
  53. printf ( "Could not open %s: %s\n",
  54. netdev->name, strerror ( rc ) );
  55. return rc;
  56. }
  57. return 0;
  58. }
  59. /**
  60. * Close network device
  61. *
  62. * @v netdev Network device
  63. */
  64. void ifclose ( struct net_device *netdev ) {
  65. netdev_close ( netdev );
  66. }
  67. /**
  68. * Print network device error breakdown
  69. *
  70. * @v stats Network device statistics
  71. * @v prefix Message prefix
  72. */
  73. static void ifstat_errors ( struct net_device_stats *stats,
  74. const char *prefix ) {
  75. unsigned int i;
  76. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  77. sizeof ( stats->errors[0] ) ) ; i++ ) {
  78. if ( stats->errors[i].count )
  79. printf ( " [%s: %d x \"%s\"]\n", prefix,
  80. stats->errors[i].count,
  81. strerror ( stats->errors[i].rc ) );
  82. }
  83. }
  84. /**
  85. * Print status of network device
  86. *
  87. * @v netdev Network device
  88. */
  89. void ifstat ( struct net_device *netdev ) {
  90. printf ( "%s: %s using %s on %s (%s)\n"
  91. " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
  92. netdev->name, netdev_addr ( netdev ),
  93. netdev->dev->driver_name, netdev->dev->name,
  94. ( netdev_is_open ( netdev ) ? "open" : "closed" ),
  95. ( netdev_link_ok ( netdev ) ? "up" : "down" ),
  96. netdev->tx_stats.good, netdev->tx_stats.bad,
  97. netdev->rx_stats.good, netdev->rx_stats.bad );
  98. if ( ! netdev_link_ok ( netdev ) ) {
  99. printf ( " [Link status: %s]\n",
  100. strerror ( netdev->link_rc ) );
  101. }
  102. ifstat_errors ( &netdev->tx_stats, "TXE" );
  103. ifstat_errors ( &netdev->rx_stats, "RXE" );
  104. }
  105. /** Network device poller */
  106. struct ifpoller {
  107. /** Job control interface */
  108. struct interface job;
  109. /** Network device */
  110. struct net_device *netdev;
  111. /** Network device configurator (if applicable) */
  112. struct net_device_configurator *configurator;
  113. /**
  114. * Check progress
  115. *
  116. * @v ifpoller Network device poller
  117. * @ret ongoing_rc Ongoing job status code (if known)
  118. */
  119. int ( * progress ) ( struct ifpoller *ifpoller );
  120. };
  121. /**
  122. * Report network device poller progress
  123. *
  124. * @v ifpoller Network device poller
  125. * @v progress Progress report to fill in
  126. * @ret ongoing_rc Ongoing job status code (if known)
  127. */
  128. static int ifpoller_progress ( struct ifpoller *ifpoller,
  129. struct job_progress *progress __unused ) {
  130. /* Reduce CPU utilisation */
  131. cpu_nap();
  132. /* Hand off to current progress checker */
  133. return ifpoller->progress ( ifpoller );
  134. }
  135. /** Network device poller operations */
  136. static struct interface_operation ifpoller_job_op[] = {
  137. INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
  138. };
  139. /** Network device poller descriptor */
  140. static struct interface_descriptor ifpoller_job_desc =
  141. INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
  142. /**
  143. * Poll network device until completion
  144. *
  145. * @v netdev Network device
  146. * @v configurator Network device configurator (if applicable)
  147. * @v timeout Timeout period, in ticks
  148. * @v progress Method to check progress
  149. * @ret rc Return status code
  150. */
  151. static int ifpoller_wait ( struct net_device *netdev,
  152. struct net_device_configurator *configurator,
  153. unsigned long timeout,
  154. int ( * progress ) ( struct ifpoller *ifpoller ) ) {
  155. static struct ifpoller ifpoller = {
  156. .job = INTF_INIT ( ifpoller_job_desc ),
  157. };
  158. ifpoller.netdev = netdev;
  159. ifpoller.configurator = configurator;
  160. ifpoller.progress = progress;
  161. intf_plug_plug ( &monojob, &ifpoller.job );
  162. return monojob_wait ( "", timeout );
  163. }
  164. /**
  165. * Check link-up progress
  166. *
  167. * @v ifpoller Network device poller
  168. * @ret ongoing_rc Ongoing job status code (if known)
  169. */
  170. static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
  171. struct net_device *netdev = ifpoller->netdev;
  172. int ongoing_rc = netdev->link_rc;
  173. /* Terminate successfully if link is up */
  174. if ( ongoing_rc == 0 )
  175. intf_close ( &ifpoller->job, 0 );
  176. /* Otherwise, report link status as ongoing job status */
  177. return ongoing_rc;
  178. }
  179. /**
  180. * Wait for link-up, with status indication
  181. *
  182. * @v netdev Network device
  183. * @v timeout Timeout period, in ticks
  184. */
  185. int iflinkwait ( struct net_device *netdev, unsigned long timeout ) {
  186. int rc;
  187. /* Ensure device is open */
  188. if ( ( rc = ifopen ( netdev ) ) != 0 )
  189. return rc;
  190. /* Return immediately if link is already up */
  191. netdev_poll ( netdev );
  192. if ( netdev_link_ok ( netdev ) )
  193. return 0;
  194. /* Wait for link-up */
  195. printf ( "Waiting for link-up on %s", netdev->name );
  196. return ifpoller_wait ( netdev, NULL, timeout, iflinkwait_progress );
  197. }
  198. /**
  199. * Check configuration progress
  200. *
  201. * @v ifpoller Network device poller
  202. * @ret ongoing_rc Ongoing job status code (if known)
  203. */
  204. static int ifconf_progress ( struct ifpoller *ifpoller ) {
  205. struct net_device *netdev = ifpoller->netdev;
  206. struct net_device_configurator *configurator = ifpoller->configurator;
  207. struct net_device_configuration *config;
  208. int rc;
  209. /* Do nothing unless configuration has completed */
  210. if ( netdev_configuration_in_progress ( netdev ) )
  211. return 0;
  212. /* Terminate with appropriate overall return status code */
  213. if ( configurator ) {
  214. config = netdev_configuration ( netdev, configurator );
  215. rc = config->rc;
  216. } else {
  217. rc = ( netdev_configuration_ok ( netdev ) ?
  218. 0 : -EADDRNOTAVAIL_CONFIG );
  219. }
  220. intf_close ( &ifpoller->job, rc );
  221. return rc;
  222. }
  223. /**
  224. * Perform network device configuration
  225. *
  226. * @v netdev Network device
  227. * @v configurator Network device configurator, or NULL to use all
  228. * @ret rc Return status code
  229. */
  230. int ifconf ( struct net_device *netdev,
  231. struct net_device_configurator *configurator ) {
  232. int rc;
  233. /* Ensure device is open and link is up */
  234. if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT ) ) != 0 )
  235. return rc;
  236. /* Start configuration */
  237. if ( configurator ) {
  238. if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
  239. printf ( "Could not configure %s via %s: %s\n",
  240. netdev->name, configurator->name,
  241. strerror ( rc ) );
  242. return rc;
  243. }
  244. } else {
  245. if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
  246. printf ( "Could not configure %s: %s\n",
  247. netdev->name, strerror ( rc ) );
  248. return rc;
  249. }
  250. }
  251. /* Wait for configuration to complete */
  252. printf ( "Configuring %s%s%s(%s %s)",
  253. ( configurator ? "[" : "" ),
  254. ( configurator ? configurator->name : "" ),
  255. ( configurator ? "] " : "" ),
  256. netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
  257. return ifpoller_wait ( netdev, configurator, 0, ifconf_progress );
  258. }