Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <usr/ifmgmt.h>
  31. /** @file
  32. *
  33. * Network interface management
  34. *
  35. */
  36. /**
  37. * Open network device
  38. *
  39. * @v netdev Network device
  40. * @ret rc Return status code
  41. */
  42. int ifopen ( struct net_device *netdev ) {
  43. int rc;
  44. if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
  45. printf ( "Could not open %s: %s\n",
  46. netdev->name, strerror ( rc ) );
  47. return rc;
  48. }
  49. return 0;
  50. }
  51. /**
  52. * Close network device
  53. *
  54. * @v netdev Network device
  55. */
  56. void ifclose ( struct net_device *netdev ) {
  57. netdev_close ( netdev );
  58. }
  59. /**
  60. * Print network device error breakdown
  61. *
  62. * @v stats Network device statistics
  63. * @v prefix Message prefix
  64. */
  65. static void ifstat_errors ( struct net_device_stats *stats,
  66. const char *prefix ) {
  67. unsigned int i;
  68. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  69. sizeof ( stats->errors[0] ) ) ; i++ ) {
  70. if ( stats->errors[i].count )
  71. printf ( " [%s: %d x \"%s\"]\n", prefix,
  72. stats->errors[i].count,
  73. strerror ( stats->errors[i].rc ) );
  74. }
  75. }
  76. /**
  77. * Print status of network device
  78. *
  79. * @v netdev Network device
  80. */
  81. void ifstat ( struct net_device *netdev ) {
  82. printf ( "%s: %s using %s on %s (%s)\n"
  83. " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
  84. netdev->name, netdev_addr ( netdev ),
  85. netdev->dev->driver_name, netdev->dev->name,
  86. ( netdev_is_open ( netdev ) ? "open" : "closed" ),
  87. ( netdev_link_ok ( netdev ) ? "up" : "down" ),
  88. netdev->tx_stats.good, netdev->tx_stats.bad,
  89. netdev->rx_stats.good, netdev->rx_stats.bad );
  90. if ( ! netdev_link_ok ( netdev ) ) {
  91. printf ( " [Link status: %s]\n",
  92. strerror ( netdev->link_rc ) );
  93. }
  94. ifstat_errors ( &netdev->tx_stats, "TXE" );
  95. ifstat_errors ( &netdev->rx_stats, "RXE" );
  96. }
  97. /** Network device poller */
  98. struct ifpoller {
  99. /** Job control interface */
  100. struct interface job;
  101. /** Network device */
  102. struct net_device *netdev;
  103. /**
  104. * Check progress
  105. *
  106. * @v ifpoller Network device poller
  107. * @ret ongoing_rc Ongoing job status code (if known)
  108. */
  109. int ( * progress ) ( struct ifpoller *ifpoller );
  110. };
  111. /**
  112. * Report network device poller progress
  113. *
  114. * @v ifpoller Network device poller
  115. * @v progress Progress report to fill in
  116. * @ret ongoing_rc Ongoing job status code (if known)
  117. */
  118. static int ifpoller_progress ( struct ifpoller *ifpoller,
  119. struct job_progress *progress __unused ) {
  120. /* Reduce CPU utilisation */
  121. cpu_nap();
  122. /* Hand off to current progress checker */
  123. return ifpoller->progress ( ifpoller );
  124. }
  125. /** Network device poller operations */
  126. static struct interface_operation ifpoller_job_op[] = {
  127. INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
  128. };
  129. /** Network device poller descriptor */
  130. static struct interface_descriptor ifpoller_job_desc =
  131. INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
  132. /**
  133. * Poll network device until completion
  134. *
  135. * @v netdev Network device
  136. * @v timeout Timeout period, in ticks
  137. * @v progress Method to check progress
  138. * @ret rc Return status code
  139. */
  140. static int ifpoller_wait ( struct net_device *netdev, unsigned long timeout,
  141. int ( * progress ) ( struct ifpoller *ifpoller ) ) {
  142. static struct ifpoller ifpoller = {
  143. .job = INTF_INIT ( ifpoller_job_desc ),
  144. };
  145. ifpoller.netdev = netdev;
  146. ifpoller.progress = progress;
  147. intf_plug_plug ( &monojob, &ifpoller.job );
  148. return monojob_wait ( "", timeout );
  149. }
  150. /**
  151. * Check link-up progress
  152. *
  153. * @v ifpoller Network device poller
  154. * @ret ongoing_rc Ongoing job status code (if known)
  155. */
  156. static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
  157. struct net_device *netdev = ifpoller->netdev;
  158. int ongoing_rc = netdev->link_rc;
  159. /* Terminate successfully if link is up */
  160. if ( ongoing_rc == 0 )
  161. intf_close ( &ifpoller->job, 0 );
  162. /* Otherwise, report link status as ongoing job status */
  163. return ongoing_rc;
  164. }
  165. /**
  166. * Wait for link-up, with status indication
  167. *
  168. * @v netdev Network device
  169. * @v timeout Timeout period, in ticks
  170. */
  171. int iflinkwait ( struct net_device *netdev, unsigned long timeout ) {
  172. int rc;
  173. /* Ensure device is open */
  174. if ( ( rc = ifopen ( netdev ) ) != 0 )
  175. return rc;
  176. /* Return immediately if link is already up */
  177. netdev_poll ( netdev );
  178. if ( netdev_link_ok ( netdev ) )
  179. return 0;
  180. /* Wait for link-up */
  181. printf ( "Waiting for link-up on %s", netdev->name );
  182. return ifpoller_wait ( netdev, timeout, iflinkwait_progress );
  183. }