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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/process.h>
  28. #include <ipxe/keys.h>
  29. #include <usr/ifmgmt.h>
  30. /** @file
  31. *
  32. * Network interface management
  33. *
  34. */
  35. /**
  36. * Open network device
  37. *
  38. * @v netdev Network device
  39. * @ret rc Return status code
  40. */
  41. int ifopen ( struct net_device *netdev ) {
  42. int rc;
  43. if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
  44. printf ( "Could not open %s: %s\n",
  45. netdev->name, strerror ( rc ) );
  46. return rc;
  47. }
  48. return 0;
  49. }
  50. /**
  51. * Close network device
  52. *
  53. * @v netdev Network device
  54. */
  55. void ifclose ( struct net_device *netdev ) {
  56. netdev_close ( netdev );
  57. }
  58. /**
  59. * Print network device error breakdown
  60. *
  61. * @v stats Network device statistics
  62. * @v prefix Message prefix
  63. */
  64. static void ifstat_errors ( struct net_device_stats *stats,
  65. const char *prefix ) {
  66. unsigned int i;
  67. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  68. sizeof ( stats->errors[0] ) ) ; i++ ) {
  69. if ( stats->errors[i].count )
  70. printf ( " [%s: %d x \"%s\"]\n", prefix,
  71. stats->errors[i].count,
  72. strerror ( stats->errors[i].rc ) );
  73. }
  74. }
  75. /**
  76. * Print status of network device
  77. *
  78. * @v netdev Network device
  79. */
  80. void ifstat ( struct net_device *netdev ) {
  81. printf ( "%s: %s using %s on %s (%s)\n"
  82. " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
  83. netdev->name, netdev_addr ( netdev ),
  84. netdev->dev->driver_name, netdev->dev->name,
  85. ( netdev_is_open ( netdev ) ? "open" : "closed" ),
  86. ( netdev_link_ok ( netdev ) ? "up" : "down" ),
  87. netdev->tx_stats.good, netdev->tx_stats.bad,
  88. netdev->rx_stats.good, netdev->rx_stats.bad );
  89. if ( ! netdev_link_ok ( netdev ) ) {
  90. printf ( " [Link status: %s]\n",
  91. strerror ( netdev->link_rc ) );
  92. }
  93. ifstat_errors ( &netdev->tx_stats, "TXE" );
  94. ifstat_errors ( &netdev->rx_stats, "RXE" );
  95. }
  96. /**
  97. * Wait for link-up, with status indication
  98. *
  99. * @v netdev Network device
  100. * @v max_wait_ms Maximum time to wait, in ms
  101. */
  102. int iflinkwait ( struct net_device *netdev, unsigned int max_wait_ms ) {
  103. int key;
  104. int rc;
  105. /* Allow link state to be updated */
  106. netdev_poll ( netdev );
  107. if ( netdev_link_ok ( netdev ) )
  108. return 0;
  109. printf ( "Waiting for link-up on %s...", netdev->name );
  110. while ( 1 ) {
  111. if ( netdev_link_ok ( netdev ) ) {
  112. rc = 0;
  113. break;
  114. }
  115. if ( max_wait_ms-- == 0 ) {
  116. rc = netdev->link_rc;
  117. break;
  118. }
  119. step();
  120. if ( iskey() ) {
  121. key = getchar();
  122. if ( key == CTRL_C ) {
  123. rc = -ECANCELED;
  124. break;
  125. }
  126. }
  127. mdelay ( 1 );
  128. }
  129. if ( rc == 0 ) {
  130. printf ( " ok\n" );
  131. } else {
  132. printf ( " failed: %s\n", strerror ( rc ) );
  133. }
  134. return rc;
  135. }