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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #include <string.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <errno.h>
  22. #include <gpxe/netdevice.h>
  23. #include <gpxe/device.h>
  24. #include <gpxe/process.h>
  25. #include <usr/ifmgmt.h>
  26. /** @file
  27. *
  28. * Network interface management
  29. *
  30. */
  31. /**
  32. * Open network device
  33. *
  34. * @v netdev Network device
  35. * @ret rc Return status code
  36. */
  37. int ifopen ( struct net_device *netdev ) {
  38. int rc;
  39. if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
  40. printf ( "Could not open %s: %s\n",
  41. netdev->name, strerror ( rc ) );
  42. return rc;
  43. }
  44. return 0;
  45. }
  46. /**
  47. * Close network device
  48. *
  49. * @v netdev Network device
  50. */
  51. void ifclose ( struct net_device *netdev ) {
  52. netdev_close ( netdev );
  53. }
  54. /**
  55. * Print network device error breakdown
  56. *
  57. * @v stats Network device statistics
  58. * @v prefix Message prefix
  59. */
  60. static void ifstat_errors ( struct net_device_stats *stats,
  61. const char *prefix ) {
  62. unsigned int i;
  63. for ( i = 0 ; i < ( sizeof ( stats->errors ) /
  64. sizeof ( stats->errors[0] ) ) ; i++ ) {
  65. if ( stats->errors[i].count )
  66. printf ( " [%s: %d x \"%s\"]\n", prefix,
  67. stats->errors[i].count,
  68. strerror ( stats->errors[i].rc ) );
  69. }
  70. }
  71. /**
  72. * Print status of network device
  73. *
  74. * @v netdev Network device
  75. */
  76. void ifstat ( struct net_device *netdev ) {
  77. printf ( "%s: %s on %s (%s)\n"
  78. " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
  79. netdev->name, netdev_hwaddr ( netdev ), netdev->dev->name,
  80. ( ( netdev->state & NETDEV_OPEN ) ? "open" : "closed" ),
  81. ( netdev_link_ok ( netdev ) ? "up" : "down" ),
  82. netdev->tx_stats.good, netdev->tx_stats.bad,
  83. netdev->rx_stats.good, netdev->rx_stats.bad );
  84. ifstat_errors ( &netdev->tx_stats, "TXE" );
  85. ifstat_errors ( &netdev->rx_stats, "RXE" );
  86. }
  87. /**
  88. * Wait for link-up
  89. *
  90. * @v netdev Network device
  91. * @v max_wait_ms Maximum time to wait, in ms
  92. */
  93. int iflinkwait ( struct net_device *netdev, unsigned int max_wait_ms ) {
  94. while ( 1 ) {
  95. if ( netdev_link_ok ( netdev ) )
  96. return 0;
  97. if ( max_wait_ms-- == 0 )
  98. return -ETIMEDOUT;
  99. step();
  100. mdelay ( 1 );
  101. }
  102. }