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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gpxe/netdevice.h>
22 #include <gpxe/command.h>
23 #include <usr/ifmgmt.h>
27 * Network interface management commands
31 /** Options shared by all if<xxx> commands */
32 static struct option ifcommon_longopts[] = {
33 { "help", 0, NULL, 'h' },
38 * Print syntax of if<xxx> command
40 * @v argv Command arguments
41 * @v verb Verb describing the action of the command
43 static void ifcommon_syntax ( char **argv, const char *verb ) {
45 " %s [<interface>] [<interface>...]\n"
47 "%s the specified network interfaces\n",
52 * Execute if<xxx> command over all network devices
54 * @v payload Command to execute
57 static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
58 struct net_device *netdev;
61 /* Print error if no network devices exist */
62 if ( ! have_netdevs() ) {
63 printf ( "No network interfaces\n" );
67 /* Execute payload for each network device */
68 for_each_netdev ( netdev ) {
69 if ( payload ( netdev ) != 0 )
76 * Execute if<xxx> command over list of network devices
78 * @v payload Command to execute
81 static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
82 char **list, unsigned int count ) {
83 const char *netdev_name;
84 struct net_device *netdev;
88 netdev_name = *(list++);
89 netdev = find_netdev ( netdev_name );
91 printf ( "%s: no such interface\n", netdev_name );
95 if ( payload ( netdev ) != 0 )
102 * Execute if<xxx> command
104 * @v payload Command to execute
105 * @v verb Verb describing the action of the command
106 * @v argc Argument count
107 * @v argv Argument list
110 static __attribute__ (( regparm ( 2 ) )) int
111 ifcommon_exec ( int ( * payload ) ( struct net_device * ),
112 const char *verb, int argc, char **argv ) {
116 while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
120 /* Display help text */
122 /* Unrecognised/invalid option */
123 ifcommon_syntax ( argv, verb );
128 if ( optind == argc ) {
129 return ifcommon_do_all ( payload );
131 return ifcommon_do_list ( payload, &argv[optind],
136 /* "ifopen" command */
138 static int ifopen_payload ( struct net_device *netdev ) {
139 return ifopen ( netdev );
142 static int ifopen_exec ( int argc, char **argv ) {
143 return ifcommon_exec ( ifopen_payload, "Open", argc, argv );
146 struct command ifopen_command __command = {
151 /* "ifclose" command */
153 static int ifclose_payload ( struct net_device *netdev ) {
158 static int ifclose_exec ( int argc, char **argv ) {
159 return ifcommon_exec ( ifclose_payload, "Close", argc, argv );
162 struct command ifclose_command __command = {
164 .exec = ifclose_exec,
167 /* "ifstat" command */
169 static int ifstat_payload ( struct net_device *netdev ) {
174 static int ifstat_exec ( int argc, char **argv ) {
175 return ifcommon_exec ( ifstat_payload, "Display status of",
179 struct command ifstat_command __command = {