iPXE
ifmgmt_cmd.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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdio.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/command.h>
32 #include <ipxe/parseopt.h>
33 #include <usr/ifmgmt.h>
34 #include <hci/ifmgmt_cmd.h>
35 
36 /** @file
37  *
38  * Network interface management commands
39  *
40  */
41 
42 /**
43  * Execute if<xxx> command
44  *
45  * @v argc Argument count
46  * @v argv Argument list
47  * @v cmd Command descriptor
48  * @v payload Command to execute
49  * @v verb Verb describing the action of the command
50  * @ret rc Return status code
51  */
52 int ifcommon_exec ( int argc, char **argv,
53  struct ifcommon_command_descriptor *ifcmd ) {
54  struct command_descriptor *cmd = &ifcmd->cmd;
55  uint8_t opts[cmd->len];
56  struct net_device *netdev;
57  int i;
58  int rc;
59 
60  /* Parse options */
61  if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
62  return rc;
63 
64  if ( optind != argc ) {
65  /* Treat arguments as a list of interfaces to try */
66  for ( i = optind ; i < argc ; i++ ) {
67  if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
68  continue;
69  if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
70  && ifcmd->stop_on_first_success ) {
71  return 0;
72  }
73  }
74  } else {
75  /* Try all interfaces */
76  rc = -ENODEV;
78  if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
79  && ifcmd->stop_on_first_success ) {
80  return 0;
81  }
82  }
83  }
84 
85  return rc;
86 }
87 
88 /** "ifopen" options */
89 struct ifopen_options {};
90 
91 /** "ifopen" option list */
92 static struct option_descriptor ifopen_opts[] = {};
93 
94 /**
95  * "ifopen" payload
96  *
97  * @v netdev Network device
98  * @v opts Command options
99  * @ret rc Return status code
100  */
101 static int ifopen_payload ( struct net_device *netdev,
102  struct ifopen_options *opts __unused ) {
103  return ifopen ( netdev );
104 }
105 
106 /** "ifopen" command descriptor */
109  0, MAX_ARGUMENTS, "[<interface>...]",
110  ifopen_payload, 0 );
111 
112 /**
113  * The "ifopen" command
114  *
115  * @v argc Argument count
116  * @v argv Argument list
117  * @ret rc Return status code
118  */
119 static int ifopen_exec ( int argc, char **argv ) {
120  return ifcommon_exec ( argc, argv, &ifopen_cmd );
121 }
122 
123 /** "ifclose" options */
124 struct ifclose_options {};
125 
126 /** "ifclose" option list */
127 static struct option_descriptor ifclose_opts[] = {};
128 
129 /**
130  * "ifclose" payload
131  *
132  * @v netdev Network device
133  * @v opts Command options
134  * @ret rc Return status code
135  */
136 static int ifclose_payload ( struct net_device *netdev,
137  struct ifclose_options *opts __unused ) {
138  ifclose ( netdev );
139  return 0;
140 }
141 
142 /** "ifclose" command descriptor */
145  0, MAX_ARGUMENTS, "[<interface>...]",
146  ifclose_payload, 0 );
147 
148 /**
149  * The "ifclose" command
150  *
151  * @v argc Argument count
152  * @v argv Argument list
153  * @ret rc Return status code
154  */
155 static int ifclose_exec ( int argc, char **argv ) {
156  return ifcommon_exec ( argc, argv, &ifclose_cmd );
157 }
158 
159 /** "ifstat" options */
160 struct ifstat_options {};
161 
162 /** "ifstat" option list */
163 static struct option_descriptor ifstat_opts[] = {};
164 
165 /**
166  * "ifstat" payload
167  *
168  * @v netdev Network device
169  * @v opts Command options
170  * @ret rc Return status code
171  */
172 static int ifstat_payload ( struct net_device *netdev,
173  struct ifstat_options *opts __unused ) {
174  ifstat ( netdev );
175  return 0;
176 }
177 
178 /** "ifstat" command descriptor */
181  0, MAX_ARGUMENTS, "[<interface>...]",
182  ifstat_payload, 0 );
183 
184 /**
185  * The "ifstat" command
186  *
187  * @v argc Argument count
188  * @v argv Argument list
189  * @ret rc Return status code
190  */
191 static int ifstat_exec ( int argc, char **argv ) {
192  return ifcommon_exec ( argc, argv, &ifstat_cmd );
193 }
194 
195 /** "ifconf" options */
197  /** Configuration timeout */
198  unsigned long timeout;
199  /** Configurator */
201 };
202 
203 /** "ifconf" option list */
204 static struct option_descriptor ifconf_opts[] = {
205  OPTION_DESC ( "configurator", 'c', required_argument,
206  struct ifconf_options, configurator,
208  OPTION_DESC ( "timeout", 't', required_argument,
209  struct ifconf_options, timeout,
210  parse_timeout ),
211 };
212 
213 /**
214  * "ifconf" payload
215  *
216  * @v netdev Network device
217  * @v opts Command options
218  * @ret rc Return status code
219  */
220 static int ifconf_payload ( struct net_device *netdev,
221  struct ifconf_options *opts ) {
222  int rc;
223 
224  /* Attempt configuration */
225  if ( ( rc = ifconf ( netdev, opts->configurator,
226  opts->timeout ) ) != 0 ) {
227 
228  /* Close device on failure, to avoid memory exhaustion */
229  netdev_close ( netdev );
230 
231  return rc;
232  }
233 
234  return 0;
235 }
236 
237 /** "ifconf" command descriptor */
240  0, MAX_ARGUMENTS, "[<interface>...]",
241  ifconf_payload, 1 );
242 
243 /**
244  * The "ifconf" command
245  *
246  * @v argc Argument count
247  * @v argv Argument list
248  * @ret rc Return status code
249  */
250 int ifconf_exec ( int argc, char **argv ) {
251  return ifcommon_exec ( argc, argv, &ifconf_cmd );
252 }
253 
254 /** "iflinkwait" option list */
256  /** Link timeout */
257  unsigned long timeout;
258 };
259 
260 /** "iflinkwait" option list */
262  OPTION_DESC ( "timeout", 't', required_argument,
264 };
265 
266 /**
267  * "iflinkwait" payload
268  *
269  * @v netdev Network device
270  * @v opts Command options
271  * @ret rc Return status code
272  */
273 static int iflinkwait_payload ( struct net_device *netdev,
274  struct iflinkwait_options *opts ) {
275  int rc;
276 
277  /* Wait for link-up */
278  if ( ( rc = iflinkwait ( netdev, opts->timeout, 1 ) ) != 0 ) {
279 
280  /* Close device on failure, to avoid memory exhaustion */
281  netdev_close ( netdev );
282 
283  return rc;
284  }
285 
286  return 0;
287 }
288 
289 /** "iflinkwait" command descriptor */
292  0, MAX_ARGUMENTS, "[<interface>...]",
293  iflinkwait_payload, 1 );
294 
295 /**
296  * The "iflinkwait" command
297  *
298  * @v argc Argument count
299  * @v argv Argument list
300  * @ret rc Return status code
301  */
302 static int iflinkwait_exec ( int argc, char **argv ) {
303  return ifcommon_exec ( argc, argv, &iflinkwait_cmd );
304 }
305 
306 /** Interface management commands */
COMMAND(ifopen, ifopen_exec)
Interface management commands.
"ifopen" options
Definition: ifmgmt_cmd.c:89
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int ifconf(struct net_device *netdev, struct net_device_configurator *configurator, unsigned long timeout)
Perform network device configuration.
Definition: ifmgmt.c:275
static int ifopen_exec(int argc, char **argv)
The "ifopen" command.
Definition: ifmgmt_cmd.c:119
"iflinkwait" option list
Definition: ifmgmt_cmd.c:255
static struct option_descriptor ifstat_opts[]
"ifstat" option list
Definition: ifmgmt_cmd.c:163
int optind
Current option index.
Definition: getopt.c:52
int parse_netdev_configurator(char *text, struct net_device_configurator **configurator)
Parse network device configurator name.
Definition: parseopt.c:181
static int ifopen_payload(struct net_device *netdev, struct ifopen_options *opts __unused)
"ifopen" payload
Definition: ifmgmt_cmd.c:101
FILE_SECBOOT(PERMITTED)
"ifstat" options
Definition: ifmgmt_cmd.c:160
static struct option_descriptor iflinkwait_opts[]
"iflinkwait" option list
Definition: ifmgmt_cmd.c:261
Error codes.
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:115
static struct option_descriptor ifconf_opts[]
"ifconf" option list
Definition: ifmgmt_cmd.c:204
int(* payload)(struct net_device *netdev, void *opts)
Payload.
Definition: ifmgmt_cmd.h:44
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:485
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
An "if<xxx>" command descriptor.
Definition: ifmgmt_cmd.h:35
int ifconf_exec(int argc, char **argv)
The "ifconf" command.
Definition: ifmgmt_cmd.c:250
A command descriptor.
Definition: parseopt.h:78
"ifclose" options
Definition: ifmgmt_cmd.c:124
static struct ifcommon_command_descriptor iflinkwait_cmd
"iflinkwait" command descriptor
Definition: ifmgmt_cmd.c:290
void ifclose(struct net_device *netdev)
Close network device.
Definition: ifmgmt.c:83
int iflinkwait(struct net_device *netdev, unsigned long timeout, int verbose)
Wait for link-up, with status indication.
Definition: ifmgmt.c:220
static struct ifcommon_command_descriptor ifconf_cmd
"ifconf" command descriptor
Definition: ifmgmt_cmd.c:238
static int ifstat_exec(int argc, char **argv)
The "ifstat" command.
Definition: ifmgmt_cmd.c:191
static int ifconf_payload(struct net_device *netdev, struct ifconf_options *opts)
"ifconf" payload
Definition: ifmgmt_cmd.c:220
Parse command-line options.
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static int iflinkwait_payload(struct net_device *netdev, struct iflinkwait_options *opts)
"iflinkwait" payload
Definition: ifmgmt_cmd.c:273
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:98
static struct net_device * netdev
Definition: gdbudp.c:52
#define for_each_netdev(netdev)
Iterate over all network devices.
Definition: netdevice.h:547
static struct option_descriptor ifopen_opts[]
"ifopen" option list
Definition: ifmgmt_cmd.c:92
Command line option parsing.
A network device.
Definition: netdevice.h:353
#define ENODEV
No such device.
Definition: errno.h:510
"ifconf" options
Definition: ifmgmt_cmd.c:196
static struct ifcommon_command_descriptor ifclose_cmd
"ifclose" command descriptor
Definition: ifmgmt_cmd.c:143
unsigned char uint8_t
Definition: stdint.h:10
int ifopen(struct net_device *netdev)
Open network device.
Definition: ifmgmt.c:66
void netdev_close(struct net_device *netdev)
Close network device.
Definition: netdevice.c:896
Network device management.
static struct ifcommon_command_descriptor ifopen_cmd
"ifopen" command descriptor
Definition: ifmgmt_cmd.c:107
static struct ifcommon_command_descriptor ifstat_cmd
"ifstat" command descriptor
Definition: ifmgmt_cmd.c:179
int ifcommon_exec(int argc, char **argv, struct ifcommon_command_descriptor *ifcmd)
Execute if<xxx> command.
Definition: ifmgmt_cmd.c:52
unsigned long timeout
Link timeout.
Definition: ifmgmt_cmd.c:257
static struct option_descriptor ifclose_opts[]
"ifclose" option list
Definition: ifmgmt_cmd.c:127
static int iflinkwait_exec(int argc, char **argv)
The "iflinkwait" command.
Definition: ifmgmt_cmd.c:302
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:68
Option requires an argument.
Definition: getopt.h:19
A network device configurator.
Definition: netdevice.h:314
A command-line option descriptor.
Definition: parseopt.h:24
static union @447 opts
"cert<xxx>" option list
int stop_on_first_success
Stop on first success.
Definition: ifmgmt_cmd.h:46
Network interface management.
void timeout(int)
void ifstat(struct net_device *netdev)
Print status of network device.
Definition: ifmgmt.c:111
struct net_device_configurator * configurator
Configurator.
Definition: ifmgmt_cmd.c:200
#define IFCOMMON_COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage, _payload, _stop_on_first_success)
Construct "if<xxx>" command descriptor.
Definition: ifmgmt_cmd.h:58
static int ifstat_payload(struct net_device *netdev, struct ifstat_options *opts __unused)
"ifstat" payload
Definition: ifmgmt_cmd.c:172
static int ifclose_payload(struct net_device *netdev, struct ifclose_options *opts __unused)
"ifclose" payload
Definition: ifmgmt_cmd.c:136
struct golan_eqe_cmd cmd
Definition: CIB_PRM.h:29
unsigned long timeout
Configuration timeout.
Definition: ifmgmt_cmd.c:198
static int ifclose_exec(int argc, char **argv)
The "ifclose" command.
Definition: ifmgmt_cmd.c:155
struct command_descriptor cmd
Command descriptor.
Definition: ifmgmt_cmd.h:37
int parse_netdev(char *text, struct net_device **netdev)
Parse network device name.
Definition: parseopt.c:159