Selaa lähdekoodia

[Settings] Add per-netdevice settings block

Add a configuration settings block for each net device.  This will
provide the parent scope for settings applicable only to that network
device (e.g. non-volatile options stored on the NIC, options obtained via
DHCP, etc.).

Expose the MAC address as a setting.
tags/v0.9.4
Michael Brown 17 vuotta sitten
vanhempi
commit
acfa14423e

+ 9
- 0
src/include/gpxe/dhcp.h Näytä tiedosto

@@ -177,6 +177,15 @@ struct job_interface;
177 177
  */
178 178
 #define DHCP_EB_SIADDR DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 3 )
179 179
 
180
+/** MAC address
181
+ *
182
+ * This option is used internally to contain the network device
183
+ * hardware address, in order to provide a consistent approach to
184
+ * storing and processing options.  It should never be present in a
185
+ * DHCP packet.
186
+ */
187
+#define DHCP_EB_MAC DHCP_ENCAP_OPT ( DHCP_EB_ENCAP, 4 )
188
+
180 189
 /*
181 190
  * Tags in the range 0x10-0x7f are reserved for feature markers
182 191
  *

+ 1
- 0
src/include/gpxe/errfile.h Näytä tiedosto

@@ -129,6 +129,7 @@
129 129
 #define ERRFILE_dns			( ERRFILE_NET | 0x00110000 )
130 130
 #define ERRFILE_tftp			( ERRFILE_NET | 0x00120000 )
131 131
 #define ERRFILE_infiniband		( ERRFILE_NET | 0x00130000 )
132
+#define ERRFILE_netdev_settings		( ERRFILE_NET | 0x00140000 )
132 133
 
133 134
 #define ERRFILE_image		      ( ERRFILE_IMAGE | 0x00000000 )
134 135
 #define ERRFILE_elf		      ( ERRFILE_IMAGE | 0x00010000 )

+ 6
- 0
src/include/gpxe/netdevice.h Näytä tiedosto

@@ -11,6 +11,7 @@
11 11
 #include <gpxe/list.h>
12 12
 #include <gpxe/tables.h>
13 13
 #include <gpxe/refcnt.h>
14
+#include <gpxe/settings.h>
14 15
 
15 16
 struct io_buffer;
16 17
 struct net_device;
@@ -243,6 +244,9 @@ struct net_device {
243 244
 	/** Device statistics */
244 245
 	struct net_device_stats stats;
245 246
 
247
+	/** Configuration settings applicable to this device */
248
+	struct settings settings;
249
+
246 250
 	/** Driver private data */
247 251
 	void *priv;
248 252
 };
@@ -360,6 +364,8 @@ extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
360 364
 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
361 365
 		    uint16_t net_proto, const void *ll_source );
362 366
 
367
+extern struct settings_operations netdev_settings_operations;
368
+
363 369
 /**
364 370
  * Complete network transmission
365 371
  *

+ 1
- 1
src/include/gpxe/settings.h Näytä tiedosto

@@ -17,7 +17,7 @@ struct in_addr;
17 17
 
18 18
 /** Settings block operations */
19 19
 struct settings_operations {
20
-	/** Set value of setting
20
+	/** Store value of setting
21 21
 	 *
22 22
 	 * @v settings		Settings block
23 23
 	 * @v tag		Setting tag number

+ 95
- 0
src/net/netdev_settings.c Näytä tiedosto

@@ -0,0 +1,95 @@
1
+/*
2
+ * Copyright (C) 2008 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 <errno.h>
21
+#include <gpxe/dhcp.h>
22
+#include <gpxe/settings.h>
23
+#include <gpxe/netdevice.h>
24
+
25
+/** @file
26
+ *
27
+ * Network device configuration settings
28
+ *
29
+ */
30
+
31
+/**
32
+ * Store value of network device setting
33
+ *
34
+ * @v settings		Settings block
35
+ * @v tag		Setting tag number
36
+ * @v data		Setting data, or NULL to clear setting
37
+ * @v len		Length of setting data
38
+ * @ret rc		Return status code
39
+ */
40
+static int netdev_store ( struct settings *settings, unsigned int tag,
41
+			  const void *data, size_t len ) {
42
+	struct net_device *netdev =
43
+		container_of ( settings, struct net_device, settings );
44
+
45
+	switch ( tag ) {
46
+	case DHCP_EB_MAC:
47
+		if ( len != netdev->ll_protocol->ll_addr_len )
48
+			return -EINVAL;
49
+		memcpy ( netdev->ll_addr, data, len );
50
+		return 0;
51
+	default :
52
+		return simple_settings_store ( settings, tag, data, len );
53
+	}
54
+}
55
+
56
+/**
57
+ * Fetch value of network device setting
58
+ *
59
+ * @v settings		Settings block
60
+ * @v tag		Setting tag number
61
+ * @v data		Setting data, or NULL to clear setting
62
+ * @v len		Length of setting data
63
+ * @ret rc		Return status code
64
+ */
65
+static int netdev_fetch ( struct settings *settings, unsigned int tag,
66
+			  void *data, size_t len ) {
67
+	struct net_device *netdev =
68
+		container_of ( settings, struct net_device, settings );
69
+
70
+	switch ( tag ) {
71
+	case DHCP_EB_MAC:
72
+		if ( len > netdev->ll_protocol->ll_addr_len )
73
+			len = netdev->ll_protocol->ll_addr_len;
74
+		memcpy ( data, netdev->ll_addr, len );
75
+		return netdev->ll_protocol->ll_addr_len;
76
+	default :
77
+		return simple_settings_fetch ( settings, tag, data, len );
78
+	}
79
+}
80
+
81
+/** Network device configuration settings operations */
82
+struct settings_operations netdev_settings_operations = {
83
+	.store = netdev_store,
84
+	.fetch = netdev_fetch,
85
+};
86
+
87
+/** Network device named settings */
88
+struct named_setting netdev_named_settings[] __named_setting = {
89
+	{
90
+		.name = "mac",
91
+		.description = "MAC address",
92
+		.tag = DHCP_EB_MAC,
93
+		.type = &setting_type_hex,
94
+	},
95
+};

+ 14
- 0
src/net/netdevice.c Näytä tiedosto

@@ -266,6 +266,9 @@ struct net_device * alloc_netdev ( size_t priv_size ) {
266 266
 		netdev->refcnt.free = free_netdev;
267 267
 		INIT_LIST_HEAD ( &netdev->tx_queue );
268 268
 		INIT_LIST_HEAD ( &netdev->rx_queue );
269
+		settings_init ( &netdev->settings,
270
+				&netdev_settings_operations, &netdev->refcnt,
271
+				netdev->name );
269 272
 		netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
270 273
 	}
271 274
 	return netdev;
@@ -282,11 +285,19 @@ struct net_device * alloc_netdev ( size_t priv_size ) {
282 285
  */
283 286
 int register_netdev ( struct net_device *netdev ) {
284 287
 	static unsigned int ifindex = 0;
288
+	int rc;
285 289
 
286 290
 	/* Create device name */
287 291
 	snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
288 292
 		   ifindex++ );
289 293
 
294
+	/* Register per-netdev configuration settings */
295
+	if ( ( rc = register_settings ( &netdev->settings, NULL ) ) != 0 ) {
296
+		DBGC ( netdev, "NETDEV %p could not register settings: %s\n",
297
+		       netdev, strerror ( rc ) );
298
+		return rc;
299
+	}
300
+
290 301
 	/* Add to device list */
291 302
 	netdev_get ( netdev );
292 303
 	list_add_tail ( &netdev->list, &net_devices );
@@ -357,6 +368,9 @@ void unregister_netdev ( struct net_device *netdev ) {
357 368
 	/* Ensure device is closed */
358 369
 	netdev_close ( netdev );
359 370
 
371
+	/* Unregister per-netdev configuration settings */
372
+	unregister_settings ( &netdev->settings );
373
+
360 374
 	/* Remove from device list */
361 375
 	list_del ( &netdev->list );
362 376
 	netdev_put ( netdev );

Loading…
Peruuta
Tallenna