[efi] Add EFI time source
[ipxe.git] / src / usr / ifmgmt.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <ipxe/console.h>
31 #include <ipxe/netdevice.h>
32 #include <ipxe/device.h>
33 #include <ipxe/job.h>
34 #include <ipxe/monojob.h>
35 #include <ipxe/timer.h>
36 #include <usr/ifmgmt.h>
37
38 /** @file
39 *
40 * Network interface management
41 *
42 */
43
44 /** Default time to wait for link-up */
45 #define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
46
47 /** Default unsuccessful configuration status code */
48 #define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
49 #define EINFO_EADDRNOTAVAIL_CONFIG \
50 __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
51 "No configuration methods succeeded" )
52
53 /**
54 * Open network device
55 *
56 * @v netdev Network device
57 * @ret rc Return status code
58 */
59 int ifopen ( struct net_device *netdev ) {
60 int rc;
61
62 if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
63 printf ( "Could not open %s: %s\n",
64 netdev->name, strerror ( rc ) );
65 return rc;
66 }
67
68 return 0;
69 }
70
71 /**
72 * Close network device
73 *
74 * @v netdev Network device
75 */
76 void ifclose ( struct net_device *netdev ) {
77 netdev_close ( netdev );
78 }
79
80 /**
81 * Print network device error breakdown
82 *
83 * @v stats Network device statistics
84 * @v prefix Message prefix
85 */
86 static void ifstat_errors ( struct net_device_stats *stats,
87 const char *prefix ) {
88 unsigned int i;
89
90 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
91 sizeof ( stats->errors[0] ) ) ; i++ ) {
92 if ( stats->errors[i].count )
93 printf ( " [%s: %d x \"%s\"]\n", prefix,
94 stats->errors[i].count,
95 strerror ( stats->errors[i].rc ) );
96 }
97 }
98
99 /**
100 * Print status of network device
101 *
102 * @v netdev Network device
103 */
104 void ifstat ( struct net_device *netdev ) {
105 printf ( "%s: %s using %s on %s (%s)\n"
106 " [Link:%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
107 netdev->name, netdev_addr ( netdev ),
108 netdev->dev->driver_name, netdev->dev->name,
109 ( netdev_is_open ( netdev ) ? "open" : "closed" ),
110 ( netdev_link_ok ( netdev ) ? "up" : "down" ),
111 netdev->tx_stats.good, netdev->tx_stats.bad,
112 netdev->rx_stats.good, netdev->rx_stats.bad );
113 if ( ! netdev_link_ok ( netdev ) ) {
114 printf ( " [Link status: %s]\n",
115 strerror ( netdev->link_rc ) );
116 }
117 ifstat_errors ( &netdev->tx_stats, "TXE" );
118 ifstat_errors ( &netdev->rx_stats, "RXE" );
119 }
120
121 /** Network device poller */
122 struct ifpoller {
123 /** Job control interface */
124 struct interface job;
125 /** Network device */
126 struct net_device *netdev;
127 /** Network device configurator (if applicable) */
128 struct net_device_configurator *configurator;
129 /**
130 * Check progress
131 *
132 * @v ifpoller Network device poller
133 * @ret ongoing_rc Ongoing job status code (if known)
134 */
135 int ( * progress ) ( struct ifpoller *ifpoller );
136 };
137
138 /**
139 * Report network device poller progress
140 *
141 * @v ifpoller Network device poller
142 * @v progress Progress report to fill in
143 * @ret ongoing_rc Ongoing job status code (if known)
144 */
145 static int ifpoller_progress ( struct ifpoller *ifpoller,
146 struct job_progress *progress __unused ) {
147
148 /* Hand off to current progress checker */
149 return ifpoller->progress ( ifpoller );
150 }
151
152 /** Network device poller operations */
153 static struct interface_operation ifpoller_job_op[] = {
154 INTF_OP ( job_progress, struct ifpoller *, ifpoller_progress ),
155 };
156
157 /** Network device poller descriptor */
158 static struct interface_descriptor ifpoller_job_desc =
159 INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
160
161 /**
162 * Poll network device until completion
163 *
164 * @v netdev Network device
165 * @v configurator Network device configurator (if applicable)
166 * @v timeout Timeout period, in ticks
167 * @v progress Method to check progress
168 * @ret rc Return status code
169 */
170 static int ifpoller_wait ( struct net_device *netdev,
171 struct net_device_configurator *configurator,
172 unsigned long timeout,
173 int ( * progress ) ( struct ifpoller *ifpoller ) ) {
174 static struct ifpoller ifpoller = {
175 .job = INTF_INIT ( ifpoller_job_desc ),
176 };
177
178 ifpoller.netdev = netdev;
179 ifpoller.configurator = configurator;
180 ifpoller.progress = progress;
181 intf_plug_plug ( &monojob, &ifpoller.job );
182 return monojob_wait ( "", timeout );
183 }
184
185 /**
186 * Check link-up progress
187 *
188 * @v ifpoller Network device poller
189 * @ret ongoing_rc Ongoing job status code (if known)
190 */
191 static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
192 struct net_device *netdev = ifpoller->netdev;
193 int ongoing_rc = netdev->link_rc;
194
195 /* Terminate successfully if link is up */
196 if ( ongoing_rc == 0 )
197 intf_close ( &ifpoller->job, 0 );
198
199 /* Otherwise, report link status as ongoing job status */
200 return ongoing_rc;
201 }
202
203 /**
204 * Wait for link-up, with status indication
205 *
206 * @v netdev Network device
207 * @v timeout Timeout period, in ticks
208 */
209 int iflinkwait ( struct net_device *netdev, unsigned long timeout ) {
210 int rc;
211
212 /* Ensure device is open */
213 if ( ( rc = ifopen ( netdev ) ) != 0 )
214 return rc;
215
216 /* Return immediately if link is already up */
217 netdev_poll ( netdev );
218 if ( netdev_link_ok ( netdev ) )
219 return 0;
220
221 /* Wait for link-up */
222 printf ( "Waiting for link-up on %s", netdev->name );
223 return ifpoller_wait ( netdev, NULL, timeout, iflinkwait_progress );
224 }
225
226 /**
227 * Check configuration progress
228 *
229 * @v ifpoller Network device poller
230 * @ret ongoing_rc Ongoing job status code (if known)
231 */
232 static int ifconf_progress ( struct ifpoller *ifpoller ) {
233 struct net_device *netdev = ifpoller->netdev;
234 struct net_device_configurator *configurator = ifpoller->configurator;
235 struct net_device_configuration *config;
236 int rc;
237
238 /* Do nothing unless configuration has completed */
239 if ( netdev_configuration_in_progress ( netdev ) )
240 return 0;
241
242 /* Terminate with appropriate overall return status code */
243 if ( configurator ) {
244 config = netdev_configuration ( netdev, configurator );
245 rc = config->rc;
246 } else {
247 rc = ( netdev_configuration_ok ( netdev ) ?
248 0 : -EADDRNOTAVAIL_CONFIG );
249 }
250 intf_close ( &ifpoller->job, rc );
251
252 return rc;
253 }
254
255 /**
256 * Perform network device configuration
257 *
258 * @v netdev Network device
259 * @v configurator Network device configurator, or NULL to use all
260 * @ret rc Return status code
261 */
262 int ifconf ( struct net_device *netdev,
263 struct net_device_configurator *configurator ) {
264 int rc;
265
266 /* Ensure device is open and link is up */
267 if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT ) ) != 0 )
268 return rc;
269
270 /* Start configuration */
271 if ( configurator ) {
272 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
273 printf ( "Could not configure %s via %s: %s\n",
274 netdev->name, configurator->name,
275 strerror ( rc ) );
276 return rc;
277 }
278 } else {
279 if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
280 printf ( "Could not configure %s: %s\n",
281 netdev->name, strerror ( rc ) );
282 return rc;
283 }
284 }
285
286 /* Wait for configuration to complete */
287 printf ( "Configuring %s%s%s(%s %s)",
288 ( configurator ? "[" : "" ),
289 ( configurator ? configurator->name : "" ),
290 ( configurator ? "] " : "" ),
291 netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
292 return ifpoller_wait ( netdev, configurator, 0, ifconf_progress );
293 }