iPXE
ifmgmt.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <string.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <errno.h>
31#include <ipxe/console.h>
32#include <ipxe/netdevice.h>
33#include <ipxe/device.h>
34#include <ipxe/job.h>
35#include <ipxe/monojob.h>
36#include <ipxe/timer.h>
37#include <ipxe/errortab.h>
38#include <usr/ifmgmt.h>
39
40/** @file
41 *
42 * Network interface management
43 *
44 */
45
46/** Default time to wait for link-up */
47#define LINK_WAIT_TIMEOUT ( 15 * TICKS_PER_SEC )
48
49/** Default unsuccessful configuration status code */
50#define EADDRNOTAVAIL_CONFIG __einfo_error ( EINFO_EADDRNOTAVAIL_CONFIG )
51#define EINFO_EADDRNOTAVAIL_CONFIG \
52 __einfo_uniqify ( EINFO_EADDRNOTAVAIL, 0x01, \
53 "No configuration methods succeeded" )
54
55/** Human-readable error message */
56struct errortab ifmgmt_errors[] __errortab = {
58};
59
60/**
61 * Open network device
62 *
63 * @v netdev Network device
64 * @ret rc Return status code
65 */
66int ifopen ( struct net_device *netdev ) {
67 int rc;
68
69 if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
70 printf ( "Could not open %s: %s\n",
71 netdev->name, strerror ( rc ) );
72 return rc;
73 }
74
75 return 0;
76}
77
78/**
79 * Close network device
80 *
81 * @v netdev Network device
82 */
83void ifclose ( struct net_device *netdev ) {
85}
86
87/**
88 * Print network device error breakdown
89 *
90 * @v stats Network device statistics
91 * @v prefix Message prefix
92 */
93static void ifstat_errors ( struct net_device_stats *stats,
94 const char *prefix ) {
95 unsigned int i;
96
97 for ( i = 0 ; i < ( sizeof ( stats->errors ) /
98 sizeof ( stats->errors[0] ) ) ; i++ ) {
99 if ( stats->errors[i].count )
100 printf ( " [%s: %d x \"%s\"]\n", prefix,
101 stats->errors[i].count,
102 strerror ( stats->errors[i].rc ) );
103 }
104}
105
106/**
107 * Print status of network device
108 *
109 * @v netdev Network device
110 */
111void ifstat ( struct net_device *netdev ) {
112 printf ( "%s: %s using %s on %s (%s) [%s]\n"
113 " [Link:%s%s, TX:%d TXE:%d RX:%d RXE:%d]\n",
114 netdev->name, netdev_addr ( netdev ),
115 netdev->dev->driver_name, netdev->dev->name,
116 netdev->ll_protocol->name,
117 ( netdev_is_open ( netdev ) ? "open" : "closed" ),
118 ( netdev_link_ok ( netdev ) ? "up" : "down" ),
119 ( netdev_link_blocked ( netdev ) ? " (blocked)" : "" ),
120 netdev->tx_stats.good, netdev->tx_stats.bad,
121 netdev->rx_stats.good, netdev->rx_stats.bad );
122 if ( ! netdev_link_ok ( netdev ) ) {
123 printf ( " [Link status: %s]\n",
124 strerror ( netdev->link_rc ) );
125 }
126 ifstat_errors ( &netdev->tx_stats, "TXE" );
127 ifstat_errors ( &netdev->rx_stats, "RXE" );
128}
129
130/** Network device poller */
131struct ifpoller {
132 /** Job control interface */
134 /** Network device */
136 /** Network device configurator (if applicable) */
138 /**
139 * Check progress
140 *
141 * @v ifpoller Network device poller
142 * @ret ongoing_rc Ongoing job status code (if known)
143 */
144 int ( * progress ) ( struct ifpoller *ifpoller );
145};
146
147/**
148 * Report network device poller progress
149 *
150 * @v ifpoller Network device poller
151 * @v progress Progress report to fill in
152 * @ret ongoing_rc Ongoing job status code (if known)
153 */
155 struct job_progress *progress __unused ) {
156
157 /* Hand off to current progress checker */
158 return ifpoller->progress ( ifpoller );
159}
160
161/** Network device poller operations */
165
166/** Network device poller descriptor */
168 INTF_DESC ( struct ifpoller, job, ifpoller_job_op );
169
170/**
171 * Poll network device until completion
172 *
173 * @v netdev Network device
174 * @v configurator Network device configurator (if applicable)
175 * @v timeout Timeout period, in ticks
176 * @v progress Method to check progress
177 * @ret rc Return status code
178 */
179static int ifpoller_wait ( struct net_device *netdev,
180 struct net_device_configurator *configurator,
181 unsigned long timeout,
182 int ( * progress ) ( struct ifpoller *ifpoller ) ) {
183 static struct ifpoller ifpoller = {
184 .job = INTF_INIT ( ifpoller_job_desc ),
185 };
186
191 return monojob_wait ( "", timeout );
192}
193
194/**
195 * Check link-up progress
196 *
197 * @v ifpoller Network device poller
198 * @ret ongoing_rc Ongoing job status code (if known)
199 */
200static int iflinkwait_progress ( struct ifpoller *ifpoller ) {
201 struct net_device *netdev = ifpoller->netdev;
202 int ongoing_rc = netdev->link_rc;
203
204 /* Terminate successfully if link is up */
205 if ( ongoing_rc == 0 )
206 intf_close ( &ifpoller->job, 0 );
207
208 /* Otherwise, report link status as ongoing job status */
209 return ongoing_rc;
210}
211
212/**
213 * Wait for link-up, with status indication
214 *
215 * @v netdev Network device
216 * @v timeout Timeout period, in ticks
217 * @v verbose Always display progress message
218 * @ret rc Return status code
219 */
220int iflinkwait ( struct net_device *netdev, unsigned long timeout,
221 int verbose ) {
222 int rc;
223
224 /* Ensure device is open */
225 if ( ( rc = ifopen ( netdev ) ) != 0 )
226 return rc;
227
228 /* Return immediately if link is already up, unless being verbose */
230 if ( netdev_link_ok ( netdev ) && ( ! verbose ) )
231 return 0;
232
233 /* Wait for link-up */
234 printf ( "Waiting for link-up on %s", netdev->name );
236}
237
238/**
239 * Check configuration progress
240 *
241 * @v ifpoller Network device poller
242 * @ret ongoing_rc Ongoing job status code (if known)
243 */
244static int ifconf_progress ( struct ifpoller *ifpoller ) {
245 struct net_device *netdev = ifpoller->netdev;
246 struct net_device_configurator *configurator = ifpoller->configurator;
247 struct net_device_configuration *config;
248 int rc;
249
250 /* Do nothing unless configuration has completed */
252 return 0;
253
254 /* Terminate with appropriate overall return status code */
255 if ( configurator ) {
257 rc = config->rc;
258 } else {
261 }
262 intf_close ( &ifpoller->job, rc );
263
264 return rc;
265}
266
267/**
268 * Perform network device configuration
269 *
270 * @v netdev Network device
271 * @v configurator Network device configurator, or NULL to use all
272 * @v timeout Timeout period, in ticks
273 * @ret rc Return status code
274 */
277 unsigned long timeout ) {
278 int rc;
279
280 /* Ensure device is open and link is up */
281 if ( ( rc = iflinkwait ( netdev, LINK_WAIT_TIMEOUT, 0 ) ) != 0 )
282 return rc;
283
284 /* Start configuration */
285 if ( configurator ) {
286 if ( ( rc = netdev_configure ( netdev, configurator ) ) != 0 ) {
287 printf ( "Could not configure %s via %s: %s\n",
288 netdev->name, configurator->name,
289 strerror ( rc ) );
290 return rc;
291 }
292 } else {
293 if ( ( rc = netdev_configure_all ( netdev ) ) != 0 ) {
294 printf ( "Could not configure %s: %s\n",
295 netdev->name, strerror ( rc ) );
296 return rc;
297 }
298 }
299
300 /* Wait for configuration to complete */
301 printf ( "Configuring %s%s%s(%s %s)",
302 ( configurator ? "[" : "" ),
303 ( configurator ? configurator->name : "" ),
304 ( configurator ? "] " : "" ),
305 netdev->name, netdev->ll_protocol->ntoa ( netdev->ll_addr ) );
307}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
void timeout(int)
Device model.
Error codes.
Error message tables.
#define __errortab
Definition errortab.h:22
#define __einfo_errortab(einfo)
Definition errortab.h:24
static struct net_device * netdev
Definition gdbudp.c:53
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
int ifopen(struct net_device *netdev)
Open network device.
Definition ifmgmt.c:66
int ifconf(struct net_device *netdev, struct net_device_configurator *configurator, unsigned long timeout)
Perform network device configuration.
Definition ifmgmt.c:275
static struct interface_operation ifpoller_job_op[]
Network device poller operations.
Definition ifmgmt.c:162
#define LINK_WAIT_TIMEOUT
Default time to wait for link-up.
Definition ifmgmt.c:47
void ifclose(struct net_device *netdev)
Close network device.
Definition ifmgmt.c:83
static void ifstat_errors(struct net_device_stats *stats, const char *prefix)
Print network device error breakdown.
Definition ifmgmt.c:93
static int ifpoller_progress(struct ifpoller *ifpoller, struct job_progress *progress __unused)
Report network device poller progress.
Definition ifmgmt.c:154
#define EADDRNOTAVAIL_CONFIG
Default unsuccessful configuration status code.
Definition ifmgmt.c:50
static int ifconf_progress(struct ifpoller *ifpoller)
Check configuration progress.
Definition ifmgmt.c:244
static int ifpoller_wait(struct net_device *netdev, struct net_device_configurator *configurator, unsigned long timeout, int(*progress)(struct ifpoller *ifpoller))
Poll network device until completion.
Definition ifmgmt.c:179
int iflinkwait(struct net_device *netdev, unsigned long timeout, int verbose)
Wait for link-up, with status indication.
Definition ifmgmt.c:220
void ifstat(struct net_device *netdev)
Print status of network device.
Definition ifmgmt.c:111
static int iflinkwait_progress(struct ifpoller *ifpoller)
Check link-up progress.
Definition ifmgmt.c:200
static struct interface_descriptor ifpoller_job_desc
Network device poller descriptor.
Definition ifmgmt.c:167
#define EINFO_EADDRNOTAVAIL_CONFIG
Definition ifmgmt.c:51
Network interface management.
User interaction.
iPXE timers
String functions.
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition interface.c:250
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition interface.c:108
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition interface.h:81
#define INTF_INIT(descriptor)
Initialise a static object interface.
Definition interface.h:218
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
Job control interfaces.
struct interface monojob
Definition monojob.c:57
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition monojob.c:82
Single foreground job.
int netdev_configure_all(struct net_device *netdev)
Start network device configuration via all supported configurators.
Definition netdevice.c:1336
int netdev_configuration_ok(struct net_device *netdev)
Check if network device has at least one successful configuration.
Definition netdevice.c:1392
int netdev_open(struct net_device *netdev)
Open network device.
Definition netdevice.c:862
void netdev_close(struct net_device *netdev)
Close network device.
Definition netdevice.c:896
int netdev_configure(struct net_device *netdev, struct net_device_configurator *configurator)
Start network device configuration.
Definition netdevice.c:1296
void netdev_poll(struct net_device *netdev)
Poll for completed and received packets on network device.
Definition netdevice.c:613
int netdev_configuration_in_progress(struct net_device *netdev)
Check if network device configuration is in progress.
Definition netdevice.c:1380
Network device management.
static int netdev_link_blocked(struct net_device *netdev)
Check link block state of network device.
Definition netdevice.h:651
static int netdev_is_open(struct net_device *netdev)
Check whether or not network device is open.
Definition netdevice.h:662
static int netdev_link_ok(struct net_device *netdev)
Check link state of network device.
Definition netdevice.h:640
static const char * netdev_addr(struct net_device *netdev)
Get printable network device link-layer address.
Definition netdevice.h:542
static struct net_device_configuration * netdev_configuration(struct net_device *netdev, struct net_device_configurator *configurator)
Get network device configuration.
Definition netdevice.h:612
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
Network device poller.
Definition ifmgmt.c:131
struct interface job
Job control interface.
Definition ifmgmt.c:133
struct net_device_configurator * configurator
Network device configurator (if applicable)
Definition ifmgmt.c:137
struct net_device * netdev
Network device.
Definition ifmgmt.c:135
int(* progress)(struct ifpoller *ifpoller)
Check progress.
Definition ifmgmt.c:144
An object interface descriptor.
Definition interface.h:56
An object interface operation.
Definition interface.h:18
An object interface.
Definition interface.h:125
Job progress.
Definition job.h:16
A network device configuration.
Definition netdevice.h:302
int rc
Configuration status.
Definition netdevice.h:308
struct net_device_configurator * configurator
Network device configurator.
Definition netdevice.h:306
A network device configurator.
Definition netdevice.h:314
const char * name
Name.
Definition netdevice.h:316
int rc
Error status code.
Definition netdevice.h:283
unsigned int count
Error count.
Definition netdevice.h:285
Network device statistics.
Definition netdevice.h:292
struct net_device_error errors[NETDEV_MAX_UNIQUE_ERRORS]
Error breakdowns.
Definition netdevice.h:298
A network device.
Definition netdevice.h:353
char prefix[4]
Definition vmconsole.c:53
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465