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