瀏覽代碼

Added network interface management commands

tags/v0.9.3
Michael Brown 17 年之前
父節點
當前提交
d24b80acf2
共有 7 個檔案被更改,包括 291 行新增35 行删除
  1. 3
    0
      src/core/config.c
  2. 182
    0
      src/hci/commands/ifmgmt_cmd.c
  3. 14
    1
      src/include/gpxe/netdevice.h
  4. 16
    0
      src/include/usr/ifmgmt.h
  5. 1
    21
      src/net/netdevice.c
  6. 9
    13
      src/usr/autoboot.c
  7. 66
    0
      src/usr/ifmgmt.c

+ 3
- 0
src/core/config.c 查看文件

@@ -152,6 +152,9 @@ REQUIRE_OBJECT ( nvo_cmd );
152 152
 #ifdef CONFIG_CMD
153 153
 REQUIRE_OBJECT ( config_cmd );
154 154
 #endif
155
+#ifdef IFMGMT_CMD
156
+REQUIRE_OBJECT ( ifmgmt_cmd );
157
+#endif
155 158
 
156 159
 /*
157 160
  * Drag in miscellaneous objects

+ 182
- 0
src/hci/commands/ifmgmt_cmd.c 查看文件

@@ -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
+};

+ 14
- 1
src/include/gpxe/netdevice.h 查看文件

@@ -224,6 +224,8 @@ struct net_device {
224 224
 /** Declare a network-layer protocol */
225 225
 #define __net_protocol __table ( net_protocols, 01 )
226 226
 
227
+extern struct list_head net_devices;
228
+
227 229
 /**
228 230
  * Get printable network device hardware address
229 231
  *
@@ -234,6 +236,18 @@ static inline const char * netdev_hwaddr ( struct net_device *netdev ) {
234 236
 	return netdev->ll_protocol->ntoa ( netdev->ll_addr );
235 237
 }
236 238
 
239
+/** Iterate over all network devices */
240
+#define for_each_netdev( netdev ) \
241
+	list_for_each_entry ( (netdev), &net_devices, list )
242
+
243
+/** There exist some network devices
244
+ *
245
+ * @ret existence	Existence of network devices
246
+ */
247
+static inline int have_netdevs ( void ) {
248
+	return ( ! list_empty ( &net_devices ) );
249
+}
250
+
237 251
 extern int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb );
238 252
 void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb );
239 253
 void netdev_tx_complete_next ( struct net_device *netdev );
@@ -247,7 +261,6 @@ extern void netdev_close ( struct net_device *netdev );
247 261
 extern void unregister_netdev ( struct net_device *netdev );
248 262
 extern void free_netdev ( struct net_device *netdev );
249 263
 struct net_device * find_netdev ( const char *name );
250
-extern struct net_device * next_netdev ( void );
251 264
 extern int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
252 265
 		    struct net_protocol *net_protocol, const void *ll_dest );
253 266
 extern int net_rx ( struct pk_buff *pkb, struct net_device *netdev,

+ 16
- 0
src/include/usr/ifmgmt.h 查看文件

@@ -0,0 +1,16 @@
1
+#ifndef _USR_IFMGMT_H
2
+#define _USR_IFMGMT_H
3
+
4
+/** @file
5
+ *
6
+ * Network interface management
7
+ *
8
+ */
9
+
10
+struct net_device;
11
+
12
+extern int ifopen ( struct net_device *netdev );
13
+extern void ifclose ( struct net_device *netdev );
14
+extern void ifstat ( struct net_device *netdev );
15
+
16
+#endif /* _USR_IFMGMT_H */

+ 1
- 21
src/net/netdevice.c 查看文件

@@ -40,7 +40,7 @@ static struct net_protocol net_protocols[0] __table_start ( net_protocols );
40 40
 static struct net_protocol net_protocols_end[0] __table_end ( net_protocols );
41 41
 
42 42
 /** List of network devices */
43
-static LIST_HEAD ( net_devices );
43
+struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
44 44
 
45 45
 /**
46 46
  * Transmit raw packet via network device
@@ -309,26 +309,6 @@ struct net_device * find_netdev ( const char *name ) {
309 309
 	return NULL;
310 310
 }
311 311
 
312
-/**
313
- * Iterate through network devices
314
- *
315
- * @ret netdev		Network device, or NULL
316
- *
317
- * This returns the registered network devices in the order of
318
- * registration.  If no network devices are registered, it will return
319
- * NULL.
320
- */
321
-struct net_device * next_netdev ( void ) {
322
-	struct net_device *netdev;
323
-
324
-	list_for_each_entry ( netdev, &net_devices, list ) {
325
-		list_del ( &netdev->list );
326
-		list_add_tail ( &netdev->list, &net_devices );
327
-		return netdev;
328
-	}
329
-	return NULL;
330
-}
331
-
332 312
 /**
333 313
  * Transmit network-layer packet
334 314
  *

+ 9
- 13
src/usr/autoboot.c 查看文件

@@ -19,6 +19,7 @@
19 19
 #include <string.h>
20 20
 #include <vsprintf.h>
21 21
 #include <gpxe/netdevice.h>
22
+#include <usr/ifmgmt.h>
22 23
 #include <gpxe/autoboot.h>
23 24
 
24 25
 /** @file
@@ -27,26 +28,21 @@
27 28
  *
28 29
  */
29 30
 
30
-#include <gpxe/netdevice.h>
31 31
 void test_dhcp ( struct net_device *netdev );
32 32
 
33 33
 void autoboot ( void ) {
34 34
 	struct net_device *netdev;
35 35
 	int rc;
36 36
 
37
-	netdev = next_netdev ();
38
-	if ( ! netdev ) {
39
-		printf ( "No network device found\n" );
40
-		return;
41
-	}
37
+	for_each_netdev ( netdev ) {
42 38
 
43
-	if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
44
-		printf ( "Could not open %s: %s\n", netdev->name,
45
-			 strerror ( rc ) );
46
-		return;
47
-	}
39
+		if ( ( rc = ifopen ( netdev ) ) != 0 )
40
+			continue;
48 41
 
49
-	test_dhcp ( netdev );
42
+		test_dhcp ( netdev );
43
+		
44
+		ifclose ( netdev );
45
+	}
50 46
 
51
-	netdev_close ( netdev );
47
+	printf ( "No more network devices\n" );
52 48
 }

+ 66
- 0
src/usr/ifmgmt.c 查看文件

@@ -0,0 +1,66 @@
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 <string.h>
20
+#include <vsprintf.h>
21
+#include <gpxe/netdevice.h>
22
+#include <usr/ifmgmt.h>
23
+
24
+/** @file
25
+ *
26
+ * Network interface management
27
+ *
28
+ */
29
+
30
+/**
31
+ * Open network device
32
+ *
33
+ * @v netdev		Network device
34
+ * @ret rc		Return status code
35
+ */
36
+int ifopen ( struct net_device *netdev ) {
37
+	int rc;
38
+
39
+	if ( ( rc = netdev_open ( netdev ) ) != 0 ) {
40
+		printf ( "Could not open %s: %s\n",
41
+			 netdev->name, strerror ( rc ) );
42
+		return rc;
43
+	}
44
+
45
+	return 0;
46
+}
47
+
48
+/**
49
+ * Close network device
50
+ *
51
+ * @v netdev		Network device
52
+ */
53
+void ifclose ( struct net_device *netdev ) {
54
+	netdev_close ( netdev );
55
+}
56
+
57
+/**
58
+ * Print status of network device
59
+ *
60
+ * @v netdev		Network device
61
+ */
62
+void ifstat ( struct net_device *netdev ) {
63
+	printf ( "%s %s %s\n",
64
+		 netdev->name, netdev_hwaddr ( netdev ),
65
+		 ( ( netdev->state & NETDEV_OPEN ) ? "open" : "closed" ) );
66
+}

Loading…
取消
儲存