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