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.

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