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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/console.h>
27 #include <ipxe/netdevice.h>
28 #include <ipxe/device.h>
30 #include <ipxe/monojob.h>
31 #include <ipxe/timer.h>
32 #include <usr/ifmgmt.h>
36 * Network interface management
40 /** Default time to wait for link-up */
41 #define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
43 /** Default unsuccessful configuration status code */
44 #define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
45 #define EINFO_EADDRNOTAVAIL_CONFIG \
46 __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
47 "No configuration methods succeeded" )
52 * @v netdev Network device
53 * @ret rc Return status code
55 int ifopen ( struct net_device *netdev ) {
58 if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
59 printf ( "Could not open %s: %s\n",
60 netdev->name, strerror ( rc ) );
68 * Close network device
70 * @v netdev Network device
72 void ifclose ( struct net_device *netdev ) {
73 netdev_close ( netdev );
77 * Print network device error breakdown
79 * @v stats Network device statistics
80 * @v prefix Message prefix
82 static void ifstat_errors ( struct net_device_stats *stats,
83 const char *prefix ) {
86 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
87 sizeof ( stats->errors[0] ) ) ; i++ ) {
88 if ( stats->errors[i].count )
89 printf ( " [%s: %d x \"%s\"]\n", prefix,
90 stats->errors[i].count,
91 strerror ( stats->errors[i].rc ) );
96 * Print status of network device
98 * @v netdev Network device
100 void ifstat ( struct net_device *netdev ) {
101 printf ( "%s: %s using %s on %s (%s)\n"
102 " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
103 netdev->name, netdev_addr ( netdev ),
104 netdev->dev->driver_name, netdev->dev->name,
105 ( netdev_is_open ( netdev ) ? "open" : "closed" ),
106 ( netdev_link_ok ( netdev ) ? "up" : "down" ),
107 netdev->tx_stats.good, netdev->tx_stats.bad,
108 netdev->rx_stats.good, netdev->rx_stats.bad );
109 if ( ! netdev_link_ok ( netdev ) ) {
110 printf ( " [Link status: %s]\n",
111 strerror ( netdev->link_rc ) );
113 ifstat_errors ( &netdev->tx_stats, "TXE" );
114 ifstat_errors ( &netdev->rx_stats, "RXE" );
117 /** Network device poller */
119 /** Job control interface */
120 struct interface job;
121 /** Network device */
122 struct net_device *netdev;
123 /** Network device configurator (if applicable) */
124 struct net_device_configurator *configurator;
128 * @v ifpoller Network device poller
129 * @ret ongoing_rc Ongoing job status code (if known)
131 int ( * progress ) ( struct ifpoller *ifpoller );
135 * Report network device poller progress
137 * @v ifpoller Network device poller
138 * @v progress Progress report to fill in
139 * @ret ongoing_rc Ongoing job status code (if known)
141 static int ifpoller_progress ( struct ifpoller *ifpoller,
142 struct job_progress *progress __unused ) {
144 /* Hand off to current progress checker */
145 return ifpoller->progress ( ifpoller );
148 /** Network device poller operations */
149 static struct interface_operation ifpoller_job_op[] = {
150 INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
153 /** Network device poller descriptor */
154 static struct interface_descriptor ifpoller_job_desc =
155 INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
158 * Poll network device until completion
160 * @v netdev Network device
161 * @v configurator Network device configurator (if applicable)
162 * @v timeout Timeout period, in ticks
163 * @v progress Method to check progress
164 * @ret rc Return status code
166 static int ifpoller_wait ( struct net_device *netdev,
167 struct net_device_configurator *configurator,
168 unsigned long timeout,
169 int ( * progress ) ( struct ifpoller *ifpoller ) ) {
170 static struct ifpoller ifpoller = {
171 .job = INTF_INIT ( ifpoller_job_desc ),
174 ifpoller.netdev = netdev;
175 ifpoller.configurator = configurator;
176 ifpoller.progress = progress;
177 intf_plug_plug ( &monojob, &ifpoller.job );
178 return monojob_wait ( "", timeout );
182 * Check link-up progress
184 * @v ifpoller Network device poller
185 * @ret ongoing_rc Ongoing job status code (if known)
187 static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
188 struct net_device *netdev = ifpoller->netdev;
189 int ongoing_rc = netdev->link_rc;
191 /* Terminate successfully if link is up */
192 if ( ongoing_rc == 0 )
193 intf_close ( &ifpoller->job, 0 );
195 /* Otherwise, report link status as ongoing job status */
200 * Wait for link-up, with status indication
202 * @v netdev Network device
203 * @v timeout Timeout period, in ticks
205 int iflinkwait ( struct net_device *netdev, unsigned long timeout ) {
208 /* Ensure device is open */
209 if ( ( rc = ifopen ( netdev ) ) != 0 )
212 /* Return immediately if link is already up */
213 netdev_poll ( netdev );
214 if ( netdev_link_ok ( netdev ) )
217 /* Wait for link-up */
218 printf ( "Waiting for link-up on %s", netdev->name );
219 return ifpoller_wait ( netdev, NULL, timeout, iflinkwait_progress );
223 * Check configuration progress
225 * @v ifpoller Network device poller
226 * @ret ongoing_rc Ongoing job status code (if known)
228 static int ifconf_progress ( struct ifpoller *ifpoller ) {
229 struct net_device *netdev = ifpoller->netdev;
230 struct net_device_configurator *configurator = ifpoller->configurator;
231 struct net_device_configuration *config;
234 /* Do nothing unless configuration has completed */
235 if ( netdev_configuration_in_progress ( netdev ) )
238 /* Terminate with appropriate overall return status code */
239 if ( configurator ) {
240 config = netdev_configuration ( netdev, configurator );
243 rc = ( netdev_configuration_ok ( netdev ) ?
244 0 : -EADDRNOTAVAIL_CONFIG );
246 intf_close ( &ifpoller->job, rc );
252 * Perform network device configuration
254 * @v netdev Network device
255 * @v configurator Network device configurator, or NULL to use all
256 * @ret rc Return status code
258 int ifconf ( struct net_device *netdev,
259 struct net_device_configurator *configurator ) {
262 /* Ensure device is open and link is up */
263 if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT ) ) != 0 )
266 /* Start configuration */
267 if ( configurator ) {
268 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
269 printf ( "Could not configure %s via %s: %s\n",
270 netdev->name, configurator->name,
275 if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
276 printf ( "Could not configure %s: %s\n",
277 netdev->name, strerror ( rc ) );
282 /* Wait for configuration to complete */
283 printf ( "Configuring %s%s%s(%s %s)",
284 ( configurator ? "[" : "" ),
285 ( configurator ? configurator->name : "" ),
286 ( configurator ? "] " : "" ),
287 netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
288 return ifpoller_wait ( netdev, configurator, 0, ifconf_progress );