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 8.2KB

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