2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/netdevice.h>
26 #include <ipxe/command.h>
27 #include <ipxe/parseopt.h>
28 #include <usr/ifmgmt.h>
29 #include <hci/ifmgmt_cmd.h>
33 * Network interface management commands
38 * Execute if<xxx> command
40 * @v argc Argument count
41 * @v argv Argument list
42 * @v cmd Command descriptor
43 * @v payload Command to execute
44 * @v verb Verb describing the action of the command
45 * @ret rc Return status code
47 int ifcommon_exec ( int argc, char **argv,
48 struct ifcommon_command_descriptor *ifcmd ) {
49 struct command_descriptor *cmd = &ifcmd->cmd;
50 uint8_t opts[cmd->len];
51 struct net_device *netdev;
56 if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
59 if ( optind != argc ) {
60 /* Treat arguments as a list of interfaces to try */
61 for ( i = optind ; i < argc ; i++ ) {
62 if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
64 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
65 && ifcmd->stop_on_first_success ) {
70 /* Try all interfaces */
72 for_each_netdev ( netdev ) {
73 if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
74 && ifcmd->stop_on_first_success ) {
83 /** "ifopen" options */
84 struct ifopen_options {};
86 /** "ifopen" option list */
87 static struct option_descriptor ifopen_opts[] = {};
92 * @v netdev Network device
93 * @v opts Command options
94 * @ret rc Return status code
96 static int ifopen_payload ( struct net_device *netdev,
97 struct ifopen_options *opts __unused ) {
98 return ifopen ( netdev );
101 /** "ifopen" command descriptor */
102 static struct ifcommon_command_descriptor ifopen_cmd =
103 IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
104 0, MAX_ARGUMENTS, "[<interface>...]",
108 * The "ifopen" command
110 * @v argc Argument count
111 * @v argv Argument list
112 * @ret rc Return status code
114 static int ifopen_exec ( int argc, char **argv ) {
115 return ifcommon_exec ( argc, argv, &ifopen_cmd );
118 /** "ifclose" options */
119 struct ifclose_options {};
121 /** "ifclose" option list */
122 static struct option_descriptor ifclose_opts[] = {};
127 * @v netdev Network device
128 * @v opts Command options
129 * @ret rc Return status code
131 static int ifclose_payload ( struct net_device *netdev,
132 struct ifclose_options *opts __unused ) {
137 /** "ifclose" command descriptor */
138 static struct ifcommon_command_descriptor ifclose_cmd =
139 IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
140 0, MAX_ARGUMENTS, "[<interface>...]",
141 ifclose_payload, 0 );
144 * The "ifclose" command
146 * @v argc Argument count
147 * @v argv Argument list
148 * @ret rc Return status code
150 static int ifclose_exec ( int argc, char **argv ) {
151 return ifcommon_exec ( argc, argv, &ifclose_cmd );
154 /** "ifstat" options */
155 struct ifstat_options {};
157 /** "ifstat" option list */
158 static struct option_descriptor ifstat_opts[] = {};
163 * @v netdev Network device
164 * @v opts Command options
165 * @ret rc Return status code
167 static int ifstat_payload ( struct net_device *netdev,
168 struct ifstat_options *opts __unused ) {
173 /** "ifstat" command descriptor */
174 static struct ifcommon_command_descriptor ifstat_cmd =
175 IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
176 0, MAX_ARGUMENTS, "[<interface>...]",
180 * The "ifstat" command
182 * @v argc Argument count
183 * @v argv Argument list
184 * @ret rc Return status code
186 static int ifstat_exec ( int argc, char **argv ) {
187 return ifcommon_exec ( argc, argv, &ifstat_cmd );
190 /** "ifconf" options */
191 struct ifconf_options {
193 struct net_device_configurator *configurator;
196 /** "ifconf" option list */
197 static struct option_descriptor ifconf_opts[] = {
198 OPTION_DESC ( "configurator", 'c', required_argument,
199 struct ifconf_options, configurator,
200 parse_netdev_configurator ),
206 * @v netdev Network device
207 * @v opts Command options
208 * @ret rc Return status code
210 static int ifconf_payload ( struct net_device *netdev,
211 struct ifconf_options *opts ) {
214 /* Attempt configuration */
215 if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
217 /* Close device on failure, to avoid memory exhaustion */
218 netdev_close ( netdev );
226 /** "ifconf" command descriptor */
227 static struct ifcommon_command_descriptor ifconf_cmd =
228 IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
229 0, MAX_ARGUMENTS, "[<interface>...]",
233 * The "ifconf" command
235 * @v argc Argument count
236 * @v argv Argument list
237 * @ret rc Return status code
239 int ifconf_exec ( int argc, char **argv ) {
240 return ifcommon_exec ( argc, argv, &ifconf_cmd );
243 /** Interface management commands */
244 struct command ifmgmt_commands[] __command = {
251 .exec = ifclose_exec,