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 /* Execute payload for each network device */
62 for_each_netdev ( netdev
) {
63 if ( payload ( netdev
) != 0 )
70 * Execute if<xxx> command over list of network devices
72 * @v payload Command to execute
75 static int ifcommon_do_list ( int ( * payload
) ( struct net_device
* ),
76 char **list
, unsigned int count
) {
77 const char *netdev_name
;
78 struct net_device
*netdev
;
82 netdev_name
= *(list
++);
83 netdev
= find_netdev ( netdev_name
);
85 printf ( "%s: no such interface\n", netdev_name
);
89 if ( payload ( netdev
) != 0 )
96 * Execute if<xxx> command
98 * @v payload Command to execute
99 * @v verb Verb describing the action of the command
100 * @v argc Argument count
101 * @v argv Argument list
104 static __attribute__ (( regparm ( 2 ) )) int
105 ifcommon_exec ( int ( * payload
) ( struct net_device
* ),
106 const char *verb
, int argc
, char **argv
) {
110 while ( ( c
= getopt_long ( argc
, argv
, "h", ifcommon_longopts
,
114 /* Display help text */
116 /* Unrecognised/invalid option */
117 ifcommon_syntax ( argv
, verb
);
122 if ( optind
== argc
) {
123 return ifcommon_do_all ( payload
);
125 return ifcommon_do_list ( payload
, &argv
[optind
],
130 /* "ifopen" command */
132 static int ifopen_payload ( struct net_device
*netdev
) {
133 return ifopen ( netdev
);
136 static int ifopen_exec ( int argc
, char **argv
) {
137 return ifcommon_exec ( ifopen_payload
, "Open", argc
, argv
);
140 /* "ifclose" command */
142 static int ifclose_payload ( struct net_device
*netdev
) {
147 static int ifclose_exec ( int argc
, char **argv
) {
148 return ifcommon_exec ( ifclose_payload
, "Close", argc
, argv
);
151 /* "ifstat" command */
153 static int ifstat_payload ( struct net_device
*netdev
) {
158 static int ifstat_exec ( int argc
, char **argv
) {
159 return ifcommon_exec ( ifstat_payload
, "Display status of",
163 /** Interface management commands */
164 struct command ifmgmt_commands
[] __command
= {
171 .exec
= ifclose_exec
,