00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <stdio.h>
00022 #include <getopt.h>
00023 #include <gpxe/netdevice.h>
00024 #include <gpxe/command.h>
00025 #include <usr/ifmgmt.h>
00026 #include <hci/ifmgmt_cmd.h>
00027
00028
00029
00030
00031
00032
00033
00034
00035 static struct option ifcommon_longopts[] = {
00036 { "help", 0, NULL, 'h' },
00037 { NULL, 0, NULL, 0 },
00038 };
00039
00040
00041
00042
00043
00044
00045
00046 static void ifcommon_syntax ( char **argv, const char *verb ) {
00047 printf ( "Usage:\n"
00048 " %s [<interface>] [<interface>...]\n"
00049 "\n"
00050 "%s the specified network interfaces\n",
00051 argv[0], verb );
00052 }
00053
00054
00055
00056
00057
00058
00059
00060 static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
00061 struct net_device *netdev;
00062 int rc = 0;
00063
00064
00065 for_each_netdev ( netdev ) {
00066 if ( payload ( netdev ) != 0 )
00067 rc = 1;
00068 }
00069 return rc;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
00079 char **list, unsigned int count ) {
00080 const char *netdev_name;
00081 struct net_device *netdev;
00082 int rc = 0;
00083
00084 while ( count-- ) {
00085 netdev_name = *(list++);
00086 netdev = find_netdev ( netdev_name );
00087 if ( ! netdev ) {
00088 printf ( "%s: no such interface\n", netdev_name );
00089 rc = 1;
00090 continue;
00091 }
00092 if ( payload ( netdev ) != 0 )
00093 rc = 1;
00094 }
00095 return rc;
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 int ifcommon_exec ( int argc, char **argv,
00108 int ( * payload ) ( struct net_device * ),
00109 const char *verb ) {
00110 int c;
00111
00112
00113 while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
00114 NULL ) ) >= 0 ) {
00115 switch ( c ) {
00116 case 'h':
00117
00118 default:
00119
00120 ifcommon_syntax ( argv, verb );
00121 return 1;
00122 }
00123 }
00124
00125 if ( optind == argc ) {
00126 return ifcommon_do_all ( payload );
00127 } else {
00128 return ifcommon_do_list ( payload, &argv[optind],
00129 ( argc - optind ) );
00130 }
00131 }
00132
00133
00134
00135 static int ifopen_payload ( struct net_device *netdev ) {
00136 return ifopen ( netdev );
00137 }
00138
00139 static int ifopen_exec ( int argc, char **argv ) {
00140 return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
00141 }
00142
00143
00144
00145 static int ifclose_payload ( struct net_device *netdev ) {
00146 ifclose ( netdev );
00147 return 0;
00148 }
00149
00150 static int ifclose_exec ( int argc, char **argv ) {
00151 return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
00152 }
00153
00154
00155
00156 static int ifstat_payload ( struct net_device *netdev ) {
00157 ifstat ( netdev );
00158 return 0;
00159 }
00160
00161 static int ifstat_exec ( int argc, char **argv ) {
00162 return ifcommon_exec ( argc, argv,
00163 ifstat_payload, "Display status of" );
00164 }
00165
00166
00167 struct command ifmgmt_commands[] __command = {
00168 {
00169 .name = "ifopen",
00170 .exec = ifopen_exec,
00171 },
00172 {
00173 .name = "ifclose",
00174 .exec = ifclose_exec,
00175 },
00176 {
00177 .name = "ifstat",
00178 .exec = ifstat_exec,
00179 },
00180 };