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 /** Interface management commands */
254 struct command ifmgmt_commands[] __command = {
255  {
256  .name = "ifopen",
257  .exec = ifopen_exec,
258  },
259  {
260  .name = "ifclose",
261  .exec = ifclose_exec,
262  },
263  {
264  .name = "ifstat",
265  .exec = ifstat_exec,
266  },
267  {
268  .name = "ifconf",
269  .exec = ifconf_exec,
270  },
271 };
"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:270
static int ifopen_exec(int argc, char **argv)
The "ifopen" command.
Definition: ifmgmt_cmd.c:118
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:154
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
Error codes.
A command-line command.
Definition: command.h:9
static union @424 opts
"cert<xxx>" option list
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition: parseopt.c:111
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:447
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:76
"ifclose" options
Definition: ifmgmt_cmd.c:123
void ifclose(struct net_device *netdev)
Close network device.
Definition: ifmgmt.c:82
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:254
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition: parseopt.h:96
static struct net_device * netdev
Definition: gdbudp.c:52
#define for_each_netdev(netdev)
Iterate over all network devices.
Definition: netdevice.h:526
static struct option_descriptor ifopen_opts[]
"ifopen" option list
Definition: ifmgmt_cmd.c:91
Command line option parsing.
A network device.
Definition: netdevice.h:348
#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:801
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
static struct option_descriptor ifclose_opts[]
"ifclose" option list
Definition: ifmgmt_cmd.c:126
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:66
Option requires an argument.
Definition: getopt.h:18
A network device configurator.
Definition: netdevice.h:309
A command-line option descriptor.
Definition: parseopt.h:22
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
#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:132