ifmgmt.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <errno.h>
00025 #include <console.h>
00026 #include <gpxe/netdevice.h>
00027 #include <gpxe/device.h>
00028 #include <gpxe/process.h>
00029 #include <gpxe/keys.h>
00030 #include <usr/ifmgmt.h>
00031 
00032 /** @file
00033  *
00034  * Network interface management
00035  *
00036  */
00037 
00038 /**
00039  * Open network device
00040  *
00041  * @v netdev            Network device
00042  * @ret rc              Return status code
00043  */
00044 int ifopen ( struct net_device *netdev ) {
00045         int rc;
00046 
00047         if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
00048                 printf ( "Could not open %s: %s\n",
00049                          netdev->name, strerror ( rc ) );
00050                 return rc;
00051         }
00052 
00053         return 0;
00054 }
00055 
00056 /**
00057  * Close network device
00058  *
00059  * @v netdev            Network device
00060  */
00061 void ifclose ( struct net_device *netdev ) {
00062         netdev_close ( netdev );
00063 }
00064 
00065 /**
00066  * Print network device error breakdown
00067  *
00068  * @v stats             Network device statistics
00069  * @v prefix            Message prefix
00070  */
00071 static void ifstat_errors ( struct net_device_stats *stats,
00072                             const char *prefix ) {
00073         unsigned int i;
00074 
00075         for ( i = 0 ; i < ( sizeof ( stats->errors ) /
00076                             sizeof ( stats->errors[0] ) ) ; i++ ) {
00077                 if ( stats->errors[i].count )
00078                         printf ( "  [%s: %d x \"%s\"]\n", prefix,
00079                                  stats->errors[i].count,
00080                                  strerror ( stats->errors[i].rc ) );
00081         }
00082 }
00083 
00084 /**
00085  * Print status of network device
00086  *
00087  * @v netdev            Network device
00088  */
00089 void ifstat ( struct net_device *netdev ) {
00090         printf ( "%s: %s on %s (%s)\n"
00091                  "  [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
00092                  netdev->name, netdev_addr ( netdev ), netdev->dev->name,
00093                  ( netdev_is_open ( netdev ) ? "open" : "closed" ),
00094                  ( netdev_link_ok ( netdev ) ? "up" : "down" ),
00095                  netdev->tx_stats.good, netdev->tx_stats.bad,
00096                  netdev->rx_stats.good, netdev->rx_stats.bad );
00097         if ( ! netdev_link_ok ( netdev ) ) {
00098                 printf ( "  [Link status: %s]\n",
00099                          strerror ( netdev->link_rc ) );
00100         }
00101         ifstat_errors ( &netdev->tx_stats, "TXE" );
00102         ifstat_errors ( &netdev->rx_stats, "RXE" );
00103 }
00104 
00105 /**
00106  * Wait for link-up, with status indication
00107  *
00108  * @v netdev            Network device
00109  * @v max_wait_ms       Maximum time to wait, in ms
00110  */
00111 int iflinkwait ( struct net_device *netdev, unsigned int max_wait_ms ) {
00112         int key;
00113         int rc;
00114 
00115         if ( netdev_link_ok ( netdev ) )
00116                 return 0;
00117 
00118         printf ( "Waiting for link-up on %s...", netdev->name );
00119 
00120         while ( 1 ) {
00121                 if ( netdev_link_ok ( netdev ) ) {
00122                         rc = 0;
00123                         break;
00124                 }
00125                 if ( max_wait_ms-- == 0 ) {
00126                         rc = netdev->link_rc;
00127                         break;
00128                 }
00129                 step();
00130                 if ( iskey() ) {
00131                         key = getchar();
00132                         if ( key == CTRL_C ) {
00133                                 rc = -ECANCELED;
00134                                 break;
00135                         }
00136                 }
00137                 mdelay ( 1 );
00138         }
00139 
00140         if ( rc == 0 ) {
00141                 printf ( " ok\n" );
00142         } else {
00143                 printf ( " failed: %s\n", strerror ( rc ) );
00144         }
00145 
00146         return rc;
00147 }

Generated on Tue Apr 6 20:01:11 2010 for gPXE by  doxygen 1.5.7.1