|
|
1.1 root 1: /*
2: * Copyright (C) 2007 Michael Brown <[email protected]>.
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 <ipxe/console.h>
26: #include <ipxe/netdevice.h>
27: #include <ipxe/device.h>
28: #include <ipxe/process.h>
29: #include <ipxe/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: */
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: */
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: */
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: */
89: void ifstat ( struct net_device *netdev ) {
90: printf ( "%s: %s using %s on %s (%s)\n"
91: " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
92: netdev->name, netdev_addr ( netdev ),
93: netdev->dev->driver_name, netdev->dev->name,
94: ( netdev_is_open ( netdev ) ? "open" : "closed" ),
95: ( netdev_link_ok ( netdev ) ? "up" : "down" ),
96: netdev->tx_stats.good, netdev->tx_stats.bad,
97: netdev->rx_stats.good, netdev->rx_stats.bad );
98: if ( ! netdev_link_ok ( netdev ) ) {
99: printf ( " [Link status: %s]\n",
100: strerror ( netdev->link_rc ) );
101: }
102: ifstat_errors ( &netdev->tx_stats, "TXE" );
103: ifstat_errors ( &netdev->rx_stats, "RXE" );
104: }
105:
106: /**
107: * Wait for link-up, with status indication
108: *
109: * @v netdev Network device
110: * @v max_wait_ms Maximum time to wait, in ms
111: */
112: int iflinkwait ( struct net_device *netdev, unsigned int max_wait_ms ) {
113: int key;
114: int rc;
115:
116: if ( netdev_link_ok ( netdev ) )
117: return 0;
118:
119: printf ( "Waiting for link-up on %s...", netdev->name );
120:
121: while ( 1 ) {
122: if ( netdev_link_ok ( netdev ) ) {
123: rc = 0;
124: break;
125: }
126: if ( max_wait_ms-- == 0 ) {
127: rc = netdev->link_rc;
128: break;
129: }
130: step();
131: if ( iskey() ) {
132: key = getchar();
133: if ( key == CTRL_C ) {
134: rc = -ECANCELED;
135: break;
136: }
137: }
138: mdelay ( 1 );
139: }
140:
141: if ( rc == 0 ) {
142: printf ( " ok\n" );
143: } else {
144: printf ( " failed: %s\n", strerror ( rc ) );
145: }
146:
147: return rc;
148: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.