iPXE
ifmgmt_cmd.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdio.h>
00027 #include <errno.h>
00028 #include <getopt.h>
00029 #include <ipxe/netdevice.h>
00030 #include <ipxe/command.h>
00031 #include <ipxe/parseopt.h>
00032 #include <usr/ifmgmt.h>
00033 #include <hci/ifmgmt_cmd.h>
00034 
00035 /** @file
00036  *
00037  * Network interface management commands
00038  *
00039  */
00040 
00041 /**
00042  * Execute if<xxx> command
00043  *
00044  * @v argc              Argument count
00045  * @v argv              Argument list
00046  * @v cmd               Command descriptor
00047  * @v payload           Command to execute
00048  * @v verb              Verb describing the action of the command
00049  * @ret rc              Return status code
00050  */
00051 int ifcommon_exec ( int argc, char **argv,
00052                     struct ifcommon_command_descriptor *ifcmd ) {
00053         struct command_descriptor *cmd = &ifcmd->cmd;
00054         uint8_t opts[cmd->len];
00055         struct net_device *netdev;
00056         int i;
00057         int rc;
00058 
00059         /* Parse options */
00060         if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
00061                 return rc;
00062 
00063         if ( optind != argc ) {
00064                 /* Treat arguments as a list of interfaces to try */
00065                 for ( i = optind ; i < argc ; i++ ) {
00066                         if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
00067                                 continue;
00068                         if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
00069                              && ifcmd->stop_on_first_success ) {
00070                                 return 0;
00071                         }
00072                 }
00073         } else {
00074                 /* Try all interfaces */
00075                 rc = -ENODEV;
00076                 for_each_netdev ( netdev ) {
00077                         if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
00078                              && ifcmd->stop_on_first_success ) {
00079                                 return 0;
00080                         }
00081                 }
00082         }
00083 
00084         return rc;
00085 }
00086 
00087 /** "ifopen" options */
00088 struct ifopen_options {};
00089 
00090 /** "ifopen" option list */
00091 static struct option_descriptor ifopen_opts[] = {};
00092 
00093 /**
00094  * "ifopen" payload
00095  *
00096  * @v netdev            Network device
00097  * @v opts              Command options
00098  * @ret rc              Return status code
00099  */
00100 static int ifopen_payload ( struct net_device *netdev,
00101                             struct ifopen_options *opts __unused ) {
00102         return ifopen ( netdev );
00103 }
00104 
00105 /** "ifopen" command descriptor */
00106 static struct ifcommon_command_descriptor ifopen_cmd =
00107         IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
00108                                 0, MAX_ARGUMENTS, "[<interface>...]",
00109                                 ifopen_payload, 0 );
00110 
00111 /**
00112  * The "ifopen" command
00113  *
00114  * @v argc              Argument count
00115  * @v argv              Argument list
00116  * @ret rc              Return status code
00117  */
00118 static int ifopen_exec ( int argc, char **argv ) {
00119         return ifcommon_exec ( argc, argv, &ifopen_cmd );
00120 }
00121 
00122 /** "ifclose" options */
00123 struct ifclose_options {};
00124 
00125 /** "ifclose" option list */
00126 static struct option_descriptor ifclose_opts[] = {};
00127 
00128 /**
00129  * "ifclose" payload
00130  *
00131  * @v netdev            Network device
00132  * @v opts              Command options
00133  * @ret rc              Return status code
00134  */
00135 static int ifclose_payload ( struct net_device *netdev,
00136                              struct ifclose_options *opts __unused ) {
00137         ifclose ( netdev );
00138         return 0;
00139 }
00140 
00141 /** "ifclose" command descriptor */
00142 static struct ifcommon_command_descriptor ifclose_cmd =
00143         IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
00144                                 0, MAX_ARGUMENTS, "[<interface>...]",
00145                                 ifclose_payload, 0 );
00146 
00147 /**
00148  * The "ifclose" command
00149  *
00150  * @v argc              Argument count
00151  * @v argv              Argument list
00152  * @ret rc              Return status code
00153  */
00154 static int ifclose_exec ( int argc, char **argv ) {
00155         return ifcommon_exec ( argc, argv, &ifclose_cmd );
00156 }
00157 
00158 /** "ifstat" options */
00159 struct ifstat_options {};
00160 
00161 /** "ifstat" option list */
00162 static struct option_descriptor ifstat_opts[] = {};
00163 
00164 /**
00165  * "ifstat" payload
00166  *
00167  * @v netdev            Network device
00168  * @v opts              Command options
00169  * @ret rc              Return status code
00170  */
00171 static int ifstat_payload ( struct net_device *netdev,
00172                             struct ifstat_options *opts __unused ) {
00173         ifstat ( netdev );
00174         return 0;
00175 }
00176 
00177 /** "ifstat" command descriptor */
00178 static struct ifcommon_command_descriptor ifstat_cmd =
00179         IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
00180                                 0, MAX_ARGUMENTS, "[<interface>...]",
00181                                 ifstat_payload, 0 );
00182 
00183 /**
00184  * The "ifstat" command
00185  *
00186  * @v argc              Argument count
00187  * @v argv              Argument list
00188  * @ret rc              Return status code
00189  */
00190 static int ifstat_exec ( int argc, char **argv ) {
00191         return ifcommon_exec ( argc, argv, &ifstat_cmd );
00192 }
00193 
00194 /** "ifconf" options */
00195 struct ifconf_options {
00196         /** Configurator */
00197         struct net_device_configurator *configurator;
00198 };
00199 
00200 /** "ifconf" option list */
00201 static struct option_descriptor ifconf_opts[] = {
00202         OPTION_DESC ( "configurator", 'c', required_argument,
00203                       struct ifconf_options, configurator,
00204                       parse_netdev_configurator ),
00205 };
00206 
00207 /**
00208  * "ifconf" payload
00209  *
00210  * @v netdev            Network device
00211  * @v opts              Command options
00212  * @ret rc              Return status code
00213  */
00214 static int ifconf_payload ( struct net_device *netdev,
00215                             struct ifconf_options *opts ) {
00216         int rc;
00217 
00218         /* Attempt configuration */
00219         if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
00220 
00221                 /* Close device on failure, to avoid memory exhaustion */
00222                 netdev_close ( netdev );
00223 
00224                 return rc;
00225         }
00226 
00227         return 0;
00228 }
00229 
00230 /** "ifconf" command descriptor */
00231 static struct ifcommon_command_descriptor ifconf_cmd =
00232         IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
00233                                 0, MAX_ARGUMENTS, "[<interface>...]",
00234                                 ifconf_payload, 1 );
00235 
00236 /**
00237  * The "ifconf" command
00238  *
00239  * @v argc              Argument count
00240  * @v argv              Argument list
00241  * @ret rc              Return status code
00242  */
00243 int ifconf_exec ( int argc, char **argv ) {
00244         return ifcommon_exec ( argc, argv, &ifconf_cmd );
00245 }
00246 
00247 /** Interface management commands */
00248 struct command ifmgmt_commands[] __command = {
00249         {
00250                 .name = "ifopen",
00251                 .exec = ifopen_exec,
00252         },
00253         {
00254                 .name = "ifclose",
00255                 .exec = ifclose_exec,
00256         },
00257         {
00258                 .name = "ifstat",
00259                 .exec = ifstat_exec,
00260         },
00261         {
00262                 .name = "ifconf",
00263                 .exec = ifconf_exec,
00264         },
00265 };