summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/hci/commands/ifmgmt_cmd.c
blob: 5307c9423d998f91aa6d894c6db3f8842e417c8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/netdevice.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <usr/ifmgmt.h>
#include <hci/ifmgmt_cmd.h>

/** @file
 *
 * Network interface management commands
 *
 */

/**
 * Execute if<xxx> command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @v cmd		Command descriptor
 * @v payload		Command to execute
 * @v verb		Verb describing the action of the command
 * @ret rc		Return status code
 */
int ifcommon_exec ( int argc, char **argv,
		    struct ifcommon_command_descriptor *ifcmd ) {
	struct command_descriptor *cmd = &ifcmd->cmd;
	uint8_t opts[cmd->len];
	struct net_device *netdev;
	int i;
	int rc;

	/* Parse options */
	if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
		return rc;

	if ( optind != argc ) {
		/* Treat arguments as a list of interfaces to try */
		for ( i = optind ; i < argc ; i++ ) {
			if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
				continue;
			if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
			     && ifcmd->stop_on_first_success ) {
				return 0;
			}
		}
	} else {
		/* Try all interfaces */
		rc = -ENODEV;
		for_each_netdev ( netdev ) {
			if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
			     && ifcmd->stop_on_first_success ) {
				return 0;
			}
		}
	}

	return rc;
}

/** "ifopen" options */
struct ifopen_options {};

/** "ifopen" option list */
static struct option_descriptor ifopen_opts[] = {};

/**
 * "ifopen" payload
 *
 * @v netdev		Network device
 * @v opts		Command options
 * @ret rc		Return status code
 */
static int ifopen_payload ( struct net_device *netdev,
			    struct ifopen_options *opts __unused ) {
	return ifopen ( netdev );
}

/** "ifopen" command descriptor */
static struct ifcommon_command_descriptor ifopen_cmd =
	IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
				0, MAX_ARGUMENTS, "[<interface>...]",
				ifopen_payload, 0 );

/**
 * The "ifopen" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
static int ifopen_exec ( int argc, char **argv ) {
	return ifcommon_exec ( argc, argv, &ifopen_cmd );
}

/** "ifclose" options */
struct ifclose_options {};

/** "ifclose" option list */
static struct option_descriptor ifclose_opts[] = {};

/**
 * "ifclose" payload
 *
 * @v netdev		Network device
 * @v opts		Command options
 * @ret rc		Return status code
 */
static int ifclose_payload ( struct net_device *netdev,
			     struct ifclose_options *opts __unused ) {
	ifclose ( netdev );
	return 0;
}

/** "ifclose" command descriptor */
static struct ifcommon_command_descriptor ifclose_cmd =
	IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
				0, MAX_ARGUMENTS, "[<interface>...]",
				ifclose_payload, 0 );

/**
 * The "ifclose" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
static int ifclose_exec ( int argc, char **argv ) {
	return ifcommon_exec ( argc, argv, &ifclose_cmd );
}

/** "ifstat" options */
struct ifstat_options {};

/** "ifstat" option list */
static struct option_descriptor ifstat_opts[] = {};

/**
 * "ifstat" payload
 *
 * @v netdev		Network device
 * @v opts		Command options
 * @ret rc		Return status code
 */
static int ifstat_payload ( struct net_device *netdev,
			    struct ifstat_options *opts __unused ) {
	ifstat ( netdev );
	return 0;
}

/** "ifstat" command descriptor */
static struct ifcommon_command_descriptor ifstat_cmd =
	IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
				0, MAX_ARGUMENTS, "[<interface>...]",
				ifstat_payload, 0 );

/**
 * The "ifstat" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
static int ifstat_exec ( int argc, char **argv ) {
	return ifcommon_exec ( argc, argv, &ifstat_cmd );
}

/** "ifconf" options */
struct ifconf_options {
	/** Configurator */
	struct net_device_configurator *configurator;
};

/** "ifconf" option list */
static struct option_descriptor ifconf_opts[] = {
	OPTION_DESC ( "configurator", 'c', required_argument,
		      struct ifconf_options, configurator,
		      parse_netdev_configurator ),
};

/**
 * "ifconf" payload
 *
 * @v netdev		Network device
 * @v opts		Command options
 * @ret rc		Return status code
 */
static int ifconf_payload ( struct net_device *netdev,
			    struct ifconf_options *opts ) {
	int rc;

	/* Attempt configuration */
	if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {

		/* Close device on failure, to avoid memory exhaustion */
		netdev_close ( netdev );

		return rc;
	}

	return 0;
}

/** "ifconf" command descriptor */
static struct ifcommon_command_descriptor ifconf_cmd =
	IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
				0, MAX_ARGUMENTS, "[<interface>...]",
				ifconf_payload, 1 );

/**
 * The "ifconf" command
 *
 * @v argc		Argument count
 * @v argv		Argument list
 * @ret rc		Return status code
 */
int ifconf_exec ( int argc, char **argv ) {
	return ifcommon_exec ( argc, argv, &ifconf_cmd );
}

/** Interface management commands */
struct command ifmgmt_commands[] __command = {
	{
		.name = "ifopen",
		.exec = ifopen_exec,
	},
	{
		.name = "ifclose",
		.exec = ifclose_exec,
	},
	{
		.name = "ifstat",
		.exec = ifstat_exec,
	},
	{
		.name = "ifconf",
		.exec = ifconf_exec,
	},
};