Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ifmgmt.c 3.3KB

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