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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_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 */
52int 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 */
90
91/** "ifopen" option list */
92static 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 */
101static 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 */
119static int ifopen_exec ( int argc, char **argv ) {
120 return ifcommon_exec ( argc, argv, &ifopen_cmd );
121}
122
123/** "ifclose" options */
125
126/** "ifclose" option list */
127static 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 */
136static 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 */
155static int ifclose_exec ( int argc, char **argv ) {
156 return ifcommon_exec ( argc, argv, &ifclose_cmd );
157}
158
159/** "ifstat" options */
161
162/** "ifstat" option list */
163static 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 */
172static 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 */
191static 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 */
205 OPTION_DESC ( "configurator", 'c', required_argument,
206 struct ifconf_options, configurator,
208 OPTION_DESC ( "timeout", 't', required_argument,
209 struct ifconf_options, 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 */
220static 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 */
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 */
250int 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 */
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 */
282
283 return rc;
284 }
285
286 return 0;
287}
288
289/** "iflinkwait" command descriptor */
292 0, MAX_ARGUMENTS, "[<interface>...]",
294
295/**
296 * The "iflinkwait" command
297 *
298 * @v argc Argument count
299 * @v argv Argument list
300 * @ret rc Return status code
301 */
302static int iflinkwait_exec ( int argc, char **argv ) {
303 return ifcommon_exec ( argc, argv, &iflinkwait_cmd );
304}
305
306/** Interface management commands */
struct golan_eqe_cmd cmd
Definition CIB_PRM.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned char uint8_t
Definition stdint.h:10
static union @024010030001061367220137227263210031030210157031 opts
"cert<xxx>" option list
#define COMMAND(name, exec)
Definition command.h:27
void timeout(int)
Error codes.
static struct net_device * netdev
Definition gdbudp.c:53
int optind
Current option index.
Definition getopt.c:52
Parse command-line options.
@ required_argument
Option requires an argument.
Definition getopt.h:19
#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 ENODEV
No such device.
Definition errno.h:510
#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
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
void ifstat(struct net_device *netdev)
Print status of network device.
Definition ifmgmt.c:111
Network interface management.
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
static struct ifcommon_command_descriptor ifconf_cmd
"ifconf" command descriptor
Definition ifmgmt_cmd.c:238
static struct option_descriptor ifstat_opts[]
"ifstat" option list
Definition ifmgmt_cmd.c:163
static int ifclose_exec(int argc, char **argv)
The "ifclose" command.
Definition ifmgmt_cmd.c:155
static struct ifcommon_command_descriptor ifclose_cmd
"ifclose" command descriptor
Definition ifmgmt_cmd.c:143
static struct option_descriptor ifopen_opts[]
"ifopen" option list
Definition ifmgmt_cmd.c:92
int ifconf_exec(int argc, char **argv)
The "ifconf" command.
Definition ifmgmt_cmd.c:250
static struct option_descriptor ifconf_opts[]
"ifconf" option list
Definition ifmgmt_cmd.c:204
static int ifstat_payload(struct net_device *netdev, struct ifstat_options *opts __unused)
"ifstat" payload
Definition ifmgmt_cmd.c:172
static struct ifcommon_command_descriptor iflinkwait_cmd
"iflinkwait" command descriptor
Definition ifmgmt_cmd.c:290
static int iflinkwait_exec(int argc, char **argv)
The "iflinkwait" command.
Definition ifmgmt_cmd.c:302
static int ifopen_payload(struct net_device *netdev, struct ifopen_options *opts __unused)
"ifopen" payload
Definition ifmgmt_cmd.c:101
static int ifopen_exec(int argc, char **argv)
The "ifopen" command.
Definition ifmgmt_cmd.c:119
static struct option_descriptor ifclose_opts[]
"ifclose" option list
Definition ifmgmt_cmd.c:127
static int ifclose_payload(struct net_device *netdev, struct ifclose_options *opts __unused)
"ifclose" payload
Definition ifmgmt_cmd.c:136
static int ifconf_payload(struct net_device *netdev, struct ifconf_options *opts)
"ifconf" payload
Definition ifmgmt_cmd.c:220
static int ifstat_exec(int argc, char **argv)
The "ifstat" command.
Definition ifmgmt_cmd.c:191
static struct option_descriptor iflinkwait_opts[]
"iflinkwait" option list
Definition ifmgmt_cmd.c:261
static struct ifcommon_command_descriptor ifopen_cmd
"ifopen" command descriptor
Definition ifmgmt_cmd.c:107
static int iflinkwait_payload(struct net_device *netdev, struct iflinkwait_options *opts)
"iflinkwait" payload
Definition ifmgmt_cmd.c:273
#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
void netdev_close(struct net_device *netdev)
Close network device.
Definition netdevice.c:896
Network device management.
#define for_each_netdev(netdev)
Iterate over all network devices.
Definition netdevice.h:547
int parse_netdev(char *text, struct net_device **netdev)
Parse network device name.
Definition parseopt.c:159
int parse_netdev_configurator(char *text, struct net_device_configurator **configurator)
Parse network device configurator name.
Definition parseopt.c:181
int parse_timeout(char *text, unsigned long *value)
Parse timeout value (in ms)
Definition parseopt.c:115
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition parseopt.c:485
Command line option parsing.
#define MAX_ARGUMENTS
No maximum number of arguments.
Definition parseopt.h:98
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition parseopt.h:68
A command descriptor.
Definition parseopt.h:78
"ifclose" options
Definition ifmgmt_cmd.c:124
An "if<xxx>" command descriptor.
Definition ifmgmt_cmd.h:35
int(* payload)(struct net_device *netdev, void *opts)
Payload.
Definition ifmgmt_cmd.h:44
struct command_descriptor cmd
Command descriptor.
Definition ifmgmt_cmd.h:37
int stop_on_first_success
Stop on first success.
Definition ifmgmt_cmd.h:46
"ifconf" options
Definition ifmgmt_cmd.c:196
struct net_device_configurator * configurator
Configurator.
Definition ifmgmt_cmd.c:200
unsigned long timeout
Configuration timeout.
Definition ifmgmt_cmd.c:198
"iflinkwait" option list
Definition ifmgmt_cmd.c:255
unsigned long timeout
Link timeout.
Definition ifmgmt_cmd.c:257
"ifopen" options
Definition ifmgmt_cmd.c:89
"ifstat" options
Definition ifmgmt_cmd.c:160
A network device configurator.
Definition netdevice.h:314
A network device.
Definition netdevice.h:353
A command-line option descriptor.
Definition parseopt.h:24