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