|
@@ -19,9 +19,11 @@
|
19
|
19
|
FILE_LICENCE ( GPL2_OR_LATER );
|
20
|
20
|
|
21
|
21
|
#include <stdio.h>
|
|
22
|
+#include <errno.h>
|
22
|
23
|
#include <getopt.h>
|
23
|
24
|
#include <ipxe/netdevice.h>
|
24
|
25
|
#include <ipxe/command.h>
|
|
26
|
+#include <ipxe/parseopt.h>
|
25
|
27
|
#include <usr/ifmgmt.h>
|
26
|
28
|
#include <hci/ifmgmt_cmd.h>
|
27
|
29
|
|
|
@@ -31,136 +33,138 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
31
|
33
|
*
|
32
|
34
|
*/
|
33
|
35
|
|
34
|
|
-/** Options shared by all if<xxx> commands */
|
35
|
|
-static struct option ifcommon_longopts[] = {
|
36
|
|
- { "help", 0, NULL, 'h' },
|
37
|
|
- { NULL, 0, NULL, 0 },
|
38
|
|
-};
|
39
|
|
-
|
40
|
|
-/**
|
41
|
|
- * Print syntax of if<xxx> command
|
42
|
|
- *
|
43
|
|
- * @v argv Command arguments
|
44
|
|
- * @v verb Verb describing the action of the command
|
45
|
|
- */
|
46
|
|
-static void ifcommon_syntax ( char **argv, const char *verb ) {
|
47
|
|
- printf ( "Usage:\n"
|
48
|
|
- " %s [<interface>] [<interface>...]\n"
|
49
|
|
- "\n"
|
50
|
|
- "%s the specified network interfaces\n",
|
51
|
|
- argv[0], verb );
|
52
|
|
-}
|
53
|
|
-
|
54
|
|
-/**
|
55
|
|
- * Execute if<xxx> command over all network devices
|
56
|
|
- *
|
57
|
|
- * @v payload Command to execute
|
58
|
|
- * @ret rc Exit code
|
59
|
|
- */
|
60
|
|
-static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
|
61
|
|
- struct net_device *netdev;
|
62
|
|
- int rc = 0;
|
63
|
|
-
|
64
|
|
- /* Execute payload for each network device */
|
65
|
|
- for_each_netdev ( netdev ) {
|
66
|
|
- if ( payload ( netdev ) != 0 )
|
67
|
|
- rc = 1;
|
68
|
|
- }
|
69
|
|
- return rc;
|
70
|
|
-}
|
71
|
|
-
|
72
|
|
-/**
|
73
|
|
- * Execute if<xxx> command over list of network devices
|
74
|
|
- *
|
75
|
|
- * @v payload Command to execute
|
76
|
|
- * @ret rc Exit code
|
77
|
|
- */
|
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;
|
82
|
|
- int rc = 0;
|
83
|
|
-
|
84
|
|
- while ( count-- ) {
|
85
|
|
- netdev_name = *(list++);
|
86
|
|
- netdev = find_netdev ( netdev_name );
|
87
|
|
- if ( ! netdev ) {
|
88
|
|
- printf ( "%s: no such interface\n", netdev_name );
|
89
|
|
- rc = 1;
|
90
|
|
- continue;
|
91
|
|
- }
|
92
|
|
- if ( payload ( netdev ) != 0 )
|
93
|
|
- rc = 1;
|
94
|
|
- }
|
95
|
|
- return rc;
|
96
|
|
-}
|
|
36
|
+/** "if<xxx>" command options */
|
|
37
|
+struct option_descriptor ifcommon_opts[0];
|
97
|
38
|
|
98
|
39
|
/**
|
99
|
40
|
* Execute if<xxx> command
|
100
|
41
|
*
|
101
|
42
|
* @v argc Argument count
|
102
|
43
|
* @v argv Argument list
|
|
44
|
+ * @v cmd Command descriptor
|
103
|
45
|
* @v payload Command to execute
|
104
|
46
|
* @v verb Verb describing the action of the command
|
105
|
|
- * @ret rc Exit code
|
|
47
|
+ * @ret rc Return status code
|
106
|
48
|
*/
|
107
|
49
|
int ifcommon_exec ( int argc, char **argv,
|
|
50
|
+ struct command_descriptor *cmd,
|
108
|
51
|
int ( * payload ) ( struct net_device * ),
|
109
|
|
- const char *verb ) {
|
110
|
|
- int c;
|
|
52
|
+ int stop_on_first_success ) {
|
|
53
|
+ struct ifcommon_options opts;
|
|
54
|
+ struct net_device *netdev;
|
|
55
|
+ int rc;
|
111
|
56
|
|
112
|
57
|
/* Parse options */
|
113
|
|
- while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
|
114
|
|
- NULL ) ) >= 0 ) {
|
115
|
|
- switch ( c ) {
|
116
|
|
- case 'h':
|
117
|
|
- /* Display help text */
|
118
|
|
- default:
|
119
|
|
- /* Unrecognised/invalid option */
|
120
|
|
- ifcommon_syntax ( argv, verb );
|
121
|
|
- return 1;
|
|
58
|
+ if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
|
|
59
|
+ return rc;
|
|
60
|
+
|
|
61
|
+ if ( optind != argc ) {
|
|
62
|
+ /* Treat arguments as a list of interfaces to try */
|
|
63
|
+ while ( optind != argc ) {
|
|
64
|
+ if ( ( rc = parse_netdev ( argv[optind++],
|
|
65
|
+ &netdev ) ) != 0 ) {
|
|
66
|
+ continue;
|
|
67
|
+ }
|
|
68
|
+ if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
|
|
69
|
+ stop_on_first_success ) {
|
|
70
|
+ return 0;
|
|
71
|
+ }
|
122
|
72
|
}
|
123
|
|
- }
|
124
|
|
-
|
125
|
|
- if ( optind == argc ) {
|
126
|
|
- return ifcommon_do_all ( payload );
|
127
|
73
|
} else {
|
128
|
|
- return ifcommon_do_list ( payload, &argv[optind],
|
129
|
|
- ( argc - optind ) );
|
|
74
|
+ /* Try all interfaces */
|
|
75
|
+ rc = -ENODEV;
|
|
76
|
+ for_each_netdev ( netdev ) {
|
|
77
|
+ if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
|
|
78
|
+ stop_on_first_success ) {
|
|
79
|
+ return 0;
|
|
80
|
+ }
|
|
81
|
+ }
|
130
|
82
|
}
|
|
83
|
+
|
|
84
|
+ return rc;
|
131
|
85
|
}
|
132
|
86
|
|
133
|
|
-/* "ifopen" command */
|
|
87
|
+/** "ifopen" command descriptor */
|
|
88
|
+static struct command_descriptor ifopen_cmd =
|
|
89
|
+ COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
|
|
90
|
+ "[<interface>...]",
|
|
91
|
+ "Open network interface(s)" );
|
134
|
92
|
|
|
93
|
+/**
|
|
94
|
+ * "ifopen" payload
|
|
95
|
+ *
|
|
96
|
+ * @v netdev Network device
|
|
97
|
+ * @ret rc Return status code
|
|
98
|
+ */
|
135
|
99
|
static int ifopen_payload ( struct net_device *netdev ) {
|
136
|
100
|
return ifopen ( netdev );
|
137
|
101
|
}
|
138
|
102
|
|
|
103
|
+/**
|
|
104
|
+ * The "ifopen" command
|
|
105
|
+ *
|
|
106
|
+ * @v argc Argument count
|
|
107
|
+ * @v argv Argument list
|
|
108
|
+ * @ret rc Return status code
|
|
109
|
+ */
|
139
|
110
|
static int ifopen_exec ( int argc, char **argv ) {
|
140
|
|
- return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
|
|
111
|
+ return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
|
141
|
112
|
}
|
142
|
113
|
|
143
|
|
-/* "ifclose" command */
|
|
114
|
+/** "ifclose" command descriptor */
|
|
115
|
+static struct command_descriptor ifclose_cmd =
|
|
116
|
+ COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
|
|
117
|
+ "[<interface>...]",
|
|
118
|
+ "Close network interface(s)" );
|
144
|
119
|
|
|
120
|
+/**
|
|
121
|
+ * "ifclose" payload
|
|
122
|
+ *
|
|
123
|
+ * @v netdev Network device
|
|
124
|
+ * @ret rc Return status code
|
|
125
|
+ */
|
145
|
126
|
static int ifclose_payload ( struct net_device *netdev ) {
|
146
|
127
|
ifclose ( netdev );
|
147
|
128
|
return 0;
|
148
|
129
|
}
|
149
|
130
|
|
|
131
|
+/**
|
|
132
|
+ * The "ifclose" command
|
|
133
|
+ *
|
|
134
|
+ * @v argc Argument count
|
|
135
|
+ * @v argv Argument list
|
|
136
|
+ * @ret rc Return status code
|
|
137
|
+ */
|
150
|
138
|
static int ifclose_exec ( int argc, char **argv ) {
|
151
|
|
- return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
|
|
139
|
+ return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
|
152
|
140
|
}
|
153
|
141
|
|
154
|
|
-/* "ifstat" command */
|
|
142
|
+/** "ifstat" command descriptor */
|
|
143
|
+static struct command_descriptor ifstat_cmd =
|
|
144
|
+ COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
|
|
145
|
+ "[<interface>...]",
|
|
146
|
+ "Show network interface(s)" );
|
155
|
147
|
|
|
148
|
+/**
|
|
149
|
+ * "ifstat" payload
|
|
150
|
+ *
|
|
151
|
+ * @v netdev Network device
|
|
152
|
+ * @ret rc Return status code
|
|
153
|
+ */
|
156
|
154
|
static int ifstat_payload ( struct net_device *netdev ) {
|
157
|
155
|
ifstat ( netdev );
|
158
|
156
|
return 0;
|
159
|
157
|
}
|
160
|
158
|
|
|
159
|
+/**
|
|
160
|
+ * The "ifstat" command
|
|
161
|
+ *
|
|
162
|
+ * @v argc Argument count
|
|
163
|
+ * @v argv Argument list
|
|
164
|
+ * @ret rc Return status code
|
|
165
|
+ */
|
161
|
166
|
static int ifstat_exec ( int argc, char **argv ) {
|
162
|
|
- return ifcommon_exec ( argc, argv,
|
163
|
|
- ifstat_payload, "Display status of" );
|
|
167
|
+ return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
|
164
|
168
|
}
|
165
|
169
|
|
166
|
170
|
/** Interface management commands */
|