|
@@ -0,0 +1,182 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
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.
|
|
8
|
+ *
|
|
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.
|
|
13
|
+ *
|
|
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.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <getopt.h>
|
|
20
|
+#include <vsprintf.h>
|
|
21
|
+#include <gpxe/netdevice.h>
|
|
22
|
+#include <gpxe/command.h>
|
|
23
|
+#include <usr/ifmgmt.h>
|
|
24
|
+
|
|
25
|
+/** @file
|
|
26
|
+ *
|
|
27
|
+ * Network interface management commands
|
|
28
|
+ *
|
|
29
|
+ */
|
|
30
|
+
|
|
31
|
+/** Options shared by all if<xxx> commands */
|
|
32
|
+static struct option ifcommon_longopts[] = {
|
|
33
|
+ { "help", 0, NULL, 'h' },
|
|
34
|
+ { NULL, 0, NULL, 0 },
|
|
35
|
+};
|
|
36
|
+
|
|
37
|
+/**
|
|
38
|
+ * Print syntax of if<xxx> command
|
|
39
|
+ *
|
|
40
|
+ * @v argv Command arguments
|
|
41
|
+ * @v verb Verb describing the action of the command
|
|
42
|
+ */
|
|
43
|
+static void ifcommon_syntax ( char **argv, const char *verb ) {
|
|
44
|
+ printf ( "Usage:\n"
|
|
45
|
+ " %s [<interface>] [<interface>...]\n"
|
|
46
|
+ "\n"
|
|
47
|
+ "%s the specified network interfaces\n",
|
|
48
|
+ argv[0], verb );
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+/**
|
|
52
|
+ * Execute if<xxx> command over all network devices
|
|
53
|
+ *
|
|
54
|
+ * @v payload Command to execute
|
|
55
|
+ * @ret rc Exit code
|
|
56
|
+ */
|
|
57
|
+static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
|
|
58
|
+ struct net_device *netdev;
|
|
59
|
+ int rc = 0;
|
|
60
|
+
|
|
61
|
+ /* Print error if no network devices exist */
|
|
62
|
+ if ( ! have_netdevs() ) {
|
|
63
|
+ printf ( "No network interfaces\n" );
|
|
64
|
+ return 1;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ /* Execute payload for each network device */
|
|
68
|
+ for_each_netdev ( netdev ) {
|
|
69
|
+ if ( payload ( netdev ) != 0 )
|
|
70
|
+ rc = 1;
|
|
71
|
+ }
|
|
72
|
+ return rc;
|
|
73
|
+}
|
|
74
|
+
|
|
75
|
+/**
|
|
76
|
+ * Execute if<xxx> command over list of network devices
|
|
77
|
+ *
|
|
78
|
+ * @v payload Command to execute
|
|
79
|
+ * @ret rc Exit code
|
|
80
|
+ */
|
|
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;
|
|
85
|
+ int rc = 0;
|
|
86
|
+
|
|
87
|
+ while ( count-- ) {
|
|
88
|
+ netdev_name = *(list++);
|
|
89
|
+ netdev = find_netdev ( netdev_name );
|
|
90
|
+ if ( ! netdev ) {
|
|
91
|
+ printf ( "%s: no such interface\n", netdev_name );
|
|
92
|
+ rc = 1;
|
|
93
|
+ continue;
|
|
94
|
+ }
|
|
95
|
+ if ( payload ( netdev ) != 0 )
|
|
96
|
+ rc = 1;
|
|
97
|
+ }
|
|
98
|
+ return rc;
|
|
99
|
+}
|
|
100
|
+
|
|
101
|
+/**
|
|
102
|
+ * Execute if<xxx> command
|
|
103
|
+ *
|
|
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
|
|
108
|
+ * @ret rc Exit code
|
|
109
|
+ */
|
|
110
|
+static __attribute__ (( regparm ( 2 ) )) int
|
|
111
|
+ifcommon_exec ( int ( * payload ) ( struct net_device * ),
|
|
112
|
+ const char *verb, int argc, char **argv ) {
|
|
113
|
+ int c;
|
|
114
|
+
|
|
115
|
+ /* Parse options */
|
|
116
|
+ while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
|
|
117
|
+ NULL ) ) >= 0 ) {
|
|
118
|
+ switch ( c ) {
|
|
119
|
+ case 'h':
|
|
120
|
+ /* Display help text */
|
|
121
|
+ default:
|
|
122
|
+ /* Unrecognised/invalid option */
|
|
123
|
+ ifcommon_syntax ( argv, verb );
|
|
124
|
+ return 1;
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ if ( optind == argc ) {
|
|
129
|
+ return ifcommon_do_all ( payload );
|
|
130
|
+ } else {
|
|
131
|
+ return ifcommon_do_list ( payload, &argv[optind],
|
|
132
|
+ ( argc - optind ) );
|
|
133
|
+ }
|
|
134
|
+}
|
|
135
|
+
|
|
136
|
+/* "ifopen" command */
|
|
137
|
+
|
|
138
|
+static int ifopen_payload ( struct net_device *netdev ) {
|
|
139
|
+ return ifopen ( netdev );
|
|
140
|
+}
|
|
141
|
+
|
|
142
|
+static int ifopen_exec ( int argc, char **argv ) {
|
|
143
|
+ return ifcommon_exec ( ifopen_payload, "Open", argc, argv );
|
|
144
|
+}
|
|
145
|
+
|
|
146
|
+struct command ifopen_command __command = {
|
|
147
|
+ .name = "ifopen",
|
|
148
|
+ .exec = ifopen_exec,
|
|
149
|
+};
|
|
150
|
+
|
|
151
|
+/* "ifclose" command */
|
|
152
|
+
|
|
153
|
+static int ifclose_payload ( struct net_device *netdev ) {
|
|
154
|
+ ifclose ( netdev );
|
|
155
|
+ return 0;
|
|
156
|
+}
|
|
157
|
+
|
|
158
|
+static int ifclose_exec ( int argc, char **argv ) {
|
|
159
|
+ return ifcommon_exec ( ifclose_payload, "Close", argc, argv );
|
|
160
|
+}
|
|
161
|
+
|
|
162
|
+struct command ifclose_command __command = {
|
|
163
|
+ .name = "ifclose",
|
|
164
|
+ .exec = ifclose_exec,
|
|
165
|
+};
|
|
166
|
+
|
|
167
|
+/* "ifstat" command */
|
|
168
|
+
|
|
169
|
+static int ifstat_payload ( struct net_device *netdev ) {
|
|
170
|
+ ifstat ( netdev );
|
|
171
|
+ return 0;
|
|
172
|
+}
|
|
173
|
+
|
|
174
|
+static int ifstat_exec ( int argc, char **argv ) {
|
|
175
|
+ return ifcommon_exec ( ifstat_payload, "Display status of",
|
|
176
|
+ argc, argv );
|
|
177
|
+}
|
|
178
|
+
|
|
179
|
+struct command ifstat_command __command = {
|
|
180
|
+ .name = "ifstat",
|
|
181
|
+ .exec = ifstat_exec,
|
|
182
|
+};
|