2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
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>
34 * Network interface management
41 * @v netdev Network device
42 * @ret rc Return status code
44 int ifopen ( struct net_device
*netdev
) {
47 if ( ( rc
= netdev_open ( netdev
) ) != 0 ) {
48 printf ( "Could not open %s: %s\n",
49 netdev
->name
, strerror ( rc
) );
57 * Close network device
59 * @v netdev Network device
61 void ifclose ( struct net_device
*netdev
) {
62 netdev_close ( netdev
);
66 * Print network device error breakdown
68 * @v stats Network device statistics
69 * @v prefix Message prefix
71 static void ifstat_errors ( struct net_device_stats
*stats
,
72 const char *prefix
) {
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
) );
85 * Print status of network device
87 * @v netdev Network device
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
) );
101 ifstat_errors ( &netdev
->tx_stats
, "TXE" );
102 ifstat_errors ( &netdev
->rx_stats
, "RXE" );
106 * Wait for link-up, with status indication
108 * @v netdev Network device
109 * @v max_wait_ms Maximum time to wait, in ms
111 int iflinkwait ( struct net_device
*netdev
, unsigned int max_wait_ms
) {
115 if ( netdev_link_ok ( netdev
) )
118 printf ( "Waiting for link-up on %s...", netdev
->name
);
121 if ( netdev_link_ok ( netdev
) ) {
125 if ( max_wait_ms
-- == 0 ) {
126 rc
= netdev
->link_rc
;
132 if ( key
== CTRL_C
) {
143 printf ( " failed: %s\n", strerror ( rc
) );