Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / ifmgmt_cmd.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <ipxe/netdevice.h>
26 #include <ipxe/command.h>
27 #include <ipxe/parseopt.h>
28 #include <usr/ifmgmt.h>
29 #include <hci/ifmgmt_cmd.h>
30
31 /** @file
32  *
33  * Network interface management commands
34  *
35  */
36
37 /**
38  * Execute if<xxx> command
39  *
40  * @v argc              Argument count
41  * @v argv              Argument list
42  * @v cmd               Command descriptor
43  * @v payload           Command to execute
44  * @v verb              Verb describing the action of the command
45  * @ret rc              Return status code
46  */
47 int ifcommon_exec ( int argc, char **argv,
48                     struct ifcommon_command_descriptor *ifcmd ) {
49         struct command_descriptor *cmd = &ifcmd->cmd;
50         uint8_t opts[cmd->len];
51         struct net_device *netdev;
52         int i;
53         int rc;
54
55         /* Parse options */
56         if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
57                 return rc;
58
59         if ( optind != argc ) {
60                 /* Treat arguments as a list of interfaces to try */
61                 for ( i = optind ; i < argc ; i++ ) {
62                         if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
63                                 continue;
64                         if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
65                              && ifcmd->stop_on_first_success ) {
66                                 return 0;
67                         }
68                 }
69         } else {
70                 /* Try all interfaces */
71                 rc = -ENODEV;
72                 for_each_netdev ( netdev ) {
73                         if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
74                              && ifcmd->stop_on_first_success ) {
75                                 return 0;
76                         }
77                 }
78         }
79
80         return rc;
81 }
82
83 /** "ifopen" options */
84 struct ifopen_options {};
85
86 /** "ifopen" option list */
87 static struct option_descriptor ifopen_opts[] = {};
88
89 /**
90  * "ifopen" payload
91  *
92  * @v netdev            Network device
93  * @v opts              Command options
94  * @ret rc              Return status code
95  */
96 static int ifopen_payload ( struct net_device *netdev,
97                             struct ifopen_options *opts __unused ) {
98         return ifopen ( netdev );
99 }
100
101 /** "ifopen" command descriptor */
102 static struct ifcommon_command_descriptor ifopen_cmd =
103         IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
104                                 0, MAX_ARGUMENTS, "[<interface>...]",
105                                 ifopen_payload, 0 );
106
107 /**
108  * The "ifopen" command
109  *
110  * @v argc              Argument count
111  * @v argv              Argument list
112  * @ret rc              Return status code
113  */
114 static int ifopen_exec ( int argc, char **argv ) {
115         return ifcommon_exec ( argc, argv, &ifopen_cmd );
116 }
117
118 /** "ifclose" options */
119 struct ifclose_options {};
120
121 /** "ifclose" option list */
122 static struct option_descriptor ifclose_opts[] = {};
123
124 /**
125  * "ifclose" payload
126  *
127  * @v netdev            Network device
128  * @v opts              Command options
129  * @ret rc              Return status code
130  */
131 static int ifclose_payload ( struct net_device *netdev,
132                              struct ifclose_options *opts __unused ) {
133         ifclose ( netdev );
134         return 0;
135 }
136
137 /** "ifclose" command descriptor */
138 static struct ifcommon_command_descriptor ifclose_cmd =
139         IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
140                                 0, MAX_ARGUMENTS, "[<interface>...]",
141                                 ifclose_payload, 0 );
142
143 /**
144  * The "ifclose" command
145  *
146  * @v argc              Argument count
147  * @v argv              Argument list
148  * @ret rc              Return status code
149  */
150 static int ifclose_exec ( int argc, char **argv ) {
151         return ifcommon_exec ( argc, argv, &ifclose_cmd );
152 }
153
154 /** "ifstat" options */
155 struct ifstat_options {};
156
157 /** "ifstat" option list */
158 static struct option_descriptor ifstat_opts[] = {};
159
160 /**
161  * "ifstat" payload
162  *
163  * @v netdev            Network device
164  * @v opts              Command options
165  * @ret rc              Return status code
166  */
167 static int ifstat_payload ( struct net_device *netdev,
168                             struct ifstat_options *opts __unused ) {
169         ifstat ( netdev );
170         return 0;
171 }
172
173 /** "ifstat" command descriptor */
174 static struct ifcommon_command_descriptor ifstat_cmd =
175         IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
176                                 0, MAX_ARGUMENTS, "[<interface>...]",
177                                 ifstat_payload, 0 );
178
179 /**
180  * The "ifstat" command
181  *
182  * @v argc              Argument count
183  * @v argv              Argument list
184  * @ret rc              Return status code
185  */
186 static int ifstat_exec ( int argc, char **argv ) {
187         return ifcommon_exec ( argc, argv, &ifstat_cmd );
188 }
189
190 /** "ifconf" options */
191 struct ifconf_options {
192         /** Configurator */
193         struct net_device_configurator *configurator;
194 };
195
196 /** "ifconf" option list */
197 static struct option_descriptor ifconf_opts[] = {
198         OPTION_DESC ( "configurator", 'c', required_argument,
199                       struct ifconf_options, configurator,
200                       parse_netdev_configurator ),
201 };
202
203 /**
204  * "ifconf" payload
205  *
206  * @v netdev            Network device
207  * @v opts              Command options
208  * @ret rc              Return status code
209  */
210 static int ifconf_payload ( struct net_device *netdev,
211                             struct ifconf_options *opts ) {
212         int rc;
213
214         /* Attempt configuration */
215         if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
216
217                 /* Close device on failure, to avoid memory exhaustion */
218                 netdev_close ( netdev );
219
220                 return rc;
221         }
222
223         return 0;
224 }
225
226 /** "ifconf" command descriptor */
227 static struct ifcommon_command_descriptor ifconf_cmd =
228         IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
229                                 0, MAX_ARGUMENTS, "[<interface>...]",
230                                 ifconf_payload, 1 );
231
232 /**
233  * The "ifconf" command
234  *
235  * @v argc              Argument count
236  * @v argv              Argument list
237  * @ret rc              Return status code
238  */
239 int ifconf_exec ( int argc, char **argv ) {
240         return ifcommon_exec ( argc, argv, &ifconf_cmd );
241 }
242
243 /** Interface management commands */
244 struct command ifmgmt_commands[] __command = {
245         {
246                 .name = "ifopen",
247                 .exec = ifopen_exec,
248         },
249         {
250                 .name = "ifclose",
251                 .exec = ifclose_exec,
252         },
253         {
254                 .name = "ifstat",
255                 .exec = ifstat_exec,
256         },
257         {
258                 .name = "ifconf",
259                 .exec = ifconf_exec,
260         },
261 };