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.
19 FILE_LICENCE ( GPL2_OR_LATER );
23 #include <gpxe/netdevice.h>
24 #include <gpxe/command.h>
25 #include <usr/ifmgmt.h>
26 #include <hci/ifmgmt_cmd.h>
30 * Network interface management commands
34 /** Options shared by all if<xxx> commands */
35 static struct option ifcommon_longopts[] = {
36 { "help", 0, NULL, 'h' },
41 * Print syntax of if<xxx> command
43 * @v argv Command arguments
44 * @v verb Verb describing the action of the command
46 static void ifcommon_syntax ( char **argv, const char *verb ) {
48 " %s [<interface>] [<interface>...]\n"
50 "%s the specified network interfaces\n",
55 * Execute if<xxx> command over all network devices
57 * @v payload Command to execute
60 static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
61 struct net_device *netdev;
64 /* Execute payload for each network device */
65 for_each_netdev ( netdev ) {
66 if ( payload ( netdev ) != 0 )
73 * Execute if<xxx> command over list of network devices
75 * @v payload Command to execute
78 static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
79 char **list, unsigned int count ) {
80 const char *netdev_name;
81 struct net_device *netdev;
85 netdev_name = *(list++);
86 netdev = find_netdev ( netdev_name );
88 printf ( "%s: no such interface\n", netdev_name );
92 if ( payload ( netdev ) != 0 )
99 * Execute if<xxx> command
101 * @v argc Argument count
102 * @v argv Argument list
103 * @v payload Command to execute
104 * @v verb Verb describing the action of the command
107 int ifcommon_exec ( int argc, char **argv,
108 int ( * payload ) ( struct net_device * ),
113 while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
117 /* Display help text */
119 /* Unrecognised/invalid option */
120 ifcommon_syntax ( argv, verb );
125 if ( optind == argc ) {
126 return ifcommon_do_all ( payload );
128 return ifcommon_do_list ( payload, &argv[optind],
133 /* "ifopen" command */
135 static int ifopen_payload ( struct net_device *netdev ) {
136 return ifopen ( netdev );
139 static int ifopen_exec ( int argc, char **argv ) {
140 return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
143 /* "ifclose" command */
145 static int ifclose_payload ( struct net_device *netdev ) {
150 static int ifclose_exec ( int argc, char **argv ) {
151 return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
154 /* "ifstat" command */
156 static int ifstat_payload ( struct net_device *netdev ) {
161 static int ifstat_exec ( int argc, char **argv ) {
162 return ifcommon_exec ( argc, argv,
163 ifstat_payload, "Display status of" );
166 /** Interface management commands */
167 struct command ifmgmt_commands[] __command = {
174 .exec = ifclose_exec,