소스 검색

[Settings] Introduce settings applicators.

Convert DHCP option applicators in dns.c and iscsi.c to settings
applicators.

Kill off DHCP option applicators.
tags/v0.9.4
Michael Brown 16 년 전
부모
커밋
cf03304620
6개의 변경된 파일222개의 추가작업 그리고 138개의 파일을 삭제
  1. 102
    9
      src/core/settings.c
  2. 0
    17
      src/include/gpxe/dhcp.h
  3. 18
    14
      src/include/gpxe/settings.h
  4. 0
    24
      src/net/dhcpopts.c
  5. 85
    56
      src/net/tcp/iscsi.c
  6. 17
    18
      src/net/udp/dns.c

+ 102
- 9
src/core/settings.c 파일 보기

@@ -37,15 +37,21 @@
37 37
 
38 38
 /** Registered setting types */
39 39
 static struct setting_type setting_types[0]
40
-__table_start ( struct setting_type, setting_types );
40
+	__table_start ( struct setting_type, setting_types );
41 41
 static struct setting_type setting_types_end[0]
42
-__table_end ( struct setting_type, setting_types );
42
+	__table_end ( struct setting_type, setting_types );
43 43
 
44 44
 /** Registered named settings */
45 45
 static struct named_setting named_settings[0]
46
-__table_start ( struct named_setting, named_settings );
46
+	__table_start ( struct named_setting, named_settings );
47 47
 static struct named_setting named_settings_end[0]
48
-__table_end ( struct named_setting, named_settings );
48
+	__table_end ( struct named_setting, named_settings );
49
+
50
+/** Registered settings applicators */
51
+static struct settings_applicator settings_applicators[0]
52
+	__table_start ( struct settings_applicator, settings_applicators );
53
+static struct settings_applicator settings_applicators_end[0]
54
+	__table_end ( struct settings_applicator, settings_applicators );
49 55
 
50 56
 /**
51 57
  * Obtain printable version of a settings tag number
@@ -94,10 +100,6 @@ int simple_settings_fetch ( struct settings *settings, unsigned int tag,
94 100
 	return ( len ? len : 8 );
95 101
 }
96 102
 
97
-// Dummy routine just for testing
98
-static void apply_settings ( void ) {
99
-}
100
-
101 103
 /** Simple settings operations */
102 104
 struct settings_operations simple_settings_operations = {
103 105
 	.store = simple_settings_store,
@@ -113,6 +115,66 @@ struct settings settings_root = {
113 115
 	.op = &simple_settings_operations,
114 116
 };
115 117
 
118
+/**
119
+ * Apply all settings
120
+ *
121
+ * @ret rc		Return status code
122
+ */
123
+static int apply_settings ( void ) {
124
+	struct settings_applicator *applicator;
125
+	int rc;
126
+
127
+	/* Call all settings applicators */
128
+	for ( applicator = settings_applicators ;
129
+	      applicator < settings_applicators_end ; applicator++ ) {
130
+		if ( ( rc = applicator->apply() ) != 0 ) {
131
+			DBG ( "Could not apply settings using applicator "
132
+			      "%p: %s\n", applicator, strerror ( rc ) );
133
+			return rc;
134
+		}
135
+	}
136
+
137
+	return 0;
138
+}
139
+
140
+/**
141
+ * Reprioritise settings
142
+ *
143
+ * @v settings		Settings block
144
+ *
145
+ * Reorders the settings block amongst its siblings according to its
146
+ * priority.
147
+ */
148
+static void reprioritise_settings ( struct settings *settings ) {
149
+	struct settings *parent = settings->parent;
150
+	long priority;
151
+	struct settings *tmp;
152
+	long tmp_priority;
153
+
154
+	/* Stop when we reach the top of the tree */
155
+	if ( ! parent )
156
+		return;
157
+
158
+	/* Read priority, if present */
159
+	priority = 0;
160
+	fetch_int_setting ( settings, DHCP_EB_PRIORITY, &priority );
161
+
162
+	/* Remove from siblings list */
163
+	list_del ( &settings->siblings );
164
+
165
+	/* Reinsert after any existing blocks which have a higher priority */
166
+	list_for_each_entry ( tmp, &parent->children, siblings ) {
167
+		tmp_priority = 0;
168
+		fetch_int_setting ( tmp, DHCP_EB_PRIORITY, &tmp_priority );
169
+		if ( priority > tmp_priority )
170
+			break;
171
+	}
172
+	list_add_tail ( &settings->siblings, &tmp->siblings );
173
+
174
+	/* Recurse up the tree */
175
+	reprioritise_settings ( parent );
176
+}
177
+
116 178
 /**
117 179
  * Register settings block
118 180
  *
@@ -134,6 +196,9 @@ int register_settings ( struct settings *settings, struct settings *parent ) {
134 196
 	list_add_tail ( &settings->siblings, &parent->children );
135 197
 	DBGC ( settings, "Settings %p registered\n", settings );
136 198
 
199
+	/* Fix up settings priority */
200
+	reprioritise_settings ( settings );
201
+
137 202
 	/* Apply potentially-updated settings */
138 203
 	apply_settings();
139 204
 
@@ -206,6 +271,34 @@ struct settings * find_settings ( const char *name ) {
206 271
  ******************************************************************************
207 272
  */
208 273
 
274
+/**
275
+ * Store value of setting
276
+ *
277
+ * @v settings		Settings block
278
+ * @v tag		Setting tag number
279
+ * @v data		Setting data, or NULL to clear setting
280
+ * @v len		Length of setting data
281
+ * @ret rc		Return status code
282
+ */
283
+int store_setting ( struct settings *settings, unsigned int tag,
284
+		    const void *data, size_t len ) {
285
+	int rc;
286
+
287
+	/* Store setting */
288
+	if ( ( rc = settings->op->store ( settings, tag, data, len ) ) != 0 )
289
+		return rc;
290
+
291
+	/* Reprioritise settings if necessary */
292
+	if ( tag == DHCP_EB_PRIORITY )
293
+		reprioritise_settings ( settings );
294
+
295
+	/* Apply potentially-updated setting */
296
+	if ( ( rc = apply_settings() ) != 0 )
297
+		return rc;
298
+
299
+	return 0;
300
+}
301
+
209 302
 /**
210 303
  * Fetch value of setting
211 304
  *
@@ -288,7 +381,7 @@ int fetch_ipv4_setting ( struct settings *settings, unsigned int tag,
288 381
 	len = fetch_setting ( settings, tag, inp, sizeof ( *inp ) );
289 382
 	if ( len < 0 )
290 383
 		return len;
291
-	if ( len != sizeof ( *inp ) )
384
+	if ( len < ( int ) sizeof ( *inp ) )
292 385
 		return -ERANGE;
293 386
 	return len;
294 387
 }

+ 0
- 17
src/include/gpxe/dhcp.h 파일 보기

@@ -489,23 +489,6 @@ struct dhcp_packet {
489 489
 	struct dhcp_option_block options;
490 490
 };
491 491
 
492
-/** A DHCP option applicator */
493
-struct dhcp_option_applicator {
494
-	/** DHCP option tag */
495
-	unsigned int tag;
496
-	/** Applicator
497
-	 *
498
-	 * @v tag	DHCP option tag
499
-	 * @v option	DHCP option
500
-	 * @ret rc	Return status code
501
-	 */
502
-	int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
503
-};
504
-
505
-/** Declare a DHCP option applicator */
506
-#define __dhcp_applicator \
507
-	__table ( struct dhcp_option_applicator, dhcp_applicators, 01 )
508
-
509 492
 /**
510 493
  * Get reference to DHCP options block
511 494
  *

+ 18
- 14
src/include/gpxe/settings.h 파일 보기

@@ -122,6 +122,22 @@ struct named_setting {
122 122
 /** Declare a configuration setting */
123 123
 #define	__named_setting __table ( struct named_setting, named_settings, 01 )
124 124
 
125
+/**
126
+ * A settings applicator
127
+ *
128
+ */
129
+struct settings_applicator {
130
+	/** Apply updated settings
131
+	 *
132
+	 * @ret rc		Return status code
133
+	 */
134
+	int ( * apply ) ( void );
135
+};
136
+
137
+/** Declare a settings applicator */
138
+#define __settings_applicator \
139
+	__table ( struct settings_applicator, settings_applicators, 01 )
140
+
125 141
 extern int simple_settings_store ( struct settings *settings, unsigned int tag,
126 142
 				   const void *data, size_t len );
127 143
 extern int simple_settings_fetch ( struct settings *settings, unsigned int tag,
@@ -131,6 +147,8 @@ extern struct settings_operations simple_settings_operations;
131 147
 extern int register_settings ( struct settings *settings,
132 148
 			       struct settings *parent );
133 149
 extern void unregister_settings ( struct settings *settings );
150
+extern int store_setting ( struct settings *settings, unsigned int tag,
151
+			   const void *data, size_t len );
134 152
 extern int fetch_setting ( struct settings *settings, unsigned int tag,
135 153
 			   void *data, size_t len );
136 154
 extern int fetch_setting_len ( struct settings *settings, unsigned int tag );
@@ -179,20 +197,6 @@ static inline void settings_init ( struct settings *settings,
179 197
 	settings->name = name;
180 198
 }
181 199
 
182
-/**
183
- * Store value of setting
184
- *
185
- * @v settings		Settings block
186
- * @v tag		Setting tag number
187
- * @v data		Setting data, or NULL to clear setting
188
- * @v len		Length of setting data
189
- * @ret rc		Return status code
190
- */
191
-static inline int store_setting ( struct settings *settings, unsigned int tag,
192
-				  const void *data, size_t len ) {
193
-	return settings->op->store ( settings, tag, data, len );
194
-}
195
-
196 200
 /**
197 201
  * Delete setting
198 202
  *

+ 0
- 24
src/net/dhcpopts.c 파일 보기

@@ -37,12 +37,6 @@
37 37
 /** List of registered DHCP option blocks */
38 38
 LIST_HEAD ( dhcp_option_blocks );
39 39
 
40
-/** Registered DHCP option applicators */
41
-static struct dhcp_option_applicator dhcp_option_applicators[0]
42
-	__table_start ( struct dhcp_option_applicator, dhcp_applicators );
43
-static struct dhcp_option_applicator dhcp_option_applicators_end[0]
44
-	__table_end ( struct dhcp_option_applicator, dhcp_applicators );
45
-
46 40
 /**
47 41
  * Obtain printable version of a DHCP option tag
48 42
  *
@@ -578,13 +572,9 @@ void delete_dhcp_option ( struct dhcp_option_block *options,
578 572
  * @ret rc		Return status code
579 573
  */
580 574
 int apply_dhcp_options ( struct dhcp_option_block *options ) {
581
-	struct dhcp_option_applicator *applicator;
582
-	struct dhcp_option *option;
583 575
 	struct in_addr tftp_server;
584 576
 	struct uri *uri;
585 577
 	char uri_string[32];
586
-	unsigned int tag;
587
-	int rc;
588 578
 
589 579
 	/* Set current working URI based on TFTP server */
590 580
 	find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
@@ -596,20 +586,6 @@ int apply_dhcp_options ( struct dhcp_option_block *options ) {
596 586
 	churi ( uri );
597 587
 	uri_put ( uri );
598 588
 
599
-	/* Call all registered DHCP option applicators */
600
-	for ( applicator = dhcp_option_applicators ;
601
-	      applicator < dhcp_option_applicators_end ; applicator++ ) {
602
-		tag = applicator->tag;
603
-		option = find_dhcp_option ( options, tag );
604
-		if ( ! option )
605
-			continue;
606
-		if ( ( rc = applicator->apply ( tag, option ) ) != 0 ) {
607
-			DBG ( "Could not apply DHCP option %s: %s\n",
608
-			      dhcp_tag_name ( tag ), strerror ( rc ) );
609
-			return rc;
610
-		}
611
-	}
612
-
613 589
 	return 0;
614 590
 }
615 591
 

+ 85
- 56
src/net/tcp/iscsi.c 파일 보기

@@ -31,7 +31,7 @@
31 31
 #include <gpxe/process.h>
32 32
 #include <gpxe/uaccess.h>
33 33
 #include <gpxe/tcpip.h>
34
-#include <gpxe/dhcp.h>
34
+#include <gpxe/settings.h>
35 35
 #include <gpxe/features.h>
36 36
 #include <gpxe/iscsi.h>
37 37
 
@@ -1591,80 +1591,109 @@ int iscsi_attach ( struct scsi_device *scsi, const char *root_path ) {
1591 1591
 
1592 1592
 /****************************************************************************
1593 1593
  *
1594
- * DHCP option applicators
1594
+ * Settings applicators
1595 1595
  *
1596 1596
  */
1597 1597
 
1598
-/**
1599
- * Apply DHCP iSCSI option
1600
- *
1601
- * @v tag		DHCP option tag
1602
- * @v option		DHCP option
1603
- * @ret rc		Return status code
1604
- */
1605
-static int apply_dhcp_iscsi_string ( unsigned int tag,
1606
-				     struct dhcp_option *option ) {
1607
-	char *prefix = "";
1608
-	size_t prefix_len;
1609
-	size_t len;
1598
+/** An iSCSI string setting */
1599
+struct iscsi_string_setting {
1600
+	/** Setting tag number */
1601
+	unsigned int tag;
1602
+	/** String to update */
1610 1603
 	char **string;
1611
-	char *p;
1612
-
1613
-	/* Identify string and prefix */
1614
-	switch ( tag ) {
1615
-	case DHCP_ISCSI_INITIATOR_IQN:
1616
-		string = &iscsi_explicit_initiator_iqn;
1617
-		break;
1618
-	case DHCP_EB_USERNAME:
1619
-		string = &iscsi_username;
1620
-		break;
1621
-	case DHCP_EB_PASSWORD:
1622
-		string = &iscsi_password;
1623
-		break;
1624
-	case DHCP_HOST_NAME:
1625
-		string = &iscsi_default_initiator_iqn;
1626
-		prefix = "iqn.2000-09.org.etherboot:";
1627
-		break;
1628
-	default:
1629
-		assert ( 0 );
1630
-		return -EINVAL;
1631
-	}
1632
-
1633
-	/* Free old string */
1634
-	free ( *string );
1635
-	*string = NULL;
1636
-
1637
-	/* Allocate and fill new string */
1638
-	prefix_len = strlen ( prefix );
1639
-	len = ( prefix_len + option->len + 1 );
1640
-	p = *string = malloc ( len );
1641
-	if ( ! p )
1642
-		return -ENOMEM;
1643
-	strcpy ( p, prefix );
1644
-	dhcp_snprintf ( ( p + prefix_len ), ( len - prefix_len ), option );
1645
-	return 0;
1646
-}
1604
+	/** String prefix */
1605
+	const char *prefix;
1606
+};
1647 1607
 
1648
-/** DHCP iSCSI option applicators */
1649
-struct dhcp_option_applicator dhcp_iscsi_applicators[] __dhcp_applicator = {
1608
+/** iSCSI string settings */
1609
+static struct iscsi_string_setting iscsi_string_settings[] = {
1650 1610
 	{
1651 1611
 		.tag = DHCP_ISCSI_INITIATOR_IQN,
1652
-		.apply = apply_dhcp_iscsi_string,
1612
+		.string = &iscsi_explicit_initiator_iqn,
1613
+		.prefix = "",
1653 1614
 	},
1654 1615
 	{
1655 1616
 		.tag = DHCP_EB_USERNAME,
1656
-		.apply = apply_dhcp_iscsi_string,
1617
+		.string = &iscsi_username,
1618
+		.prefix = "",
1657 1619
 	},
1658 1620
 	{
1659 1621
 		.tag = DHCP_EB_PASSWORD,
1660
-		.apply = apply_dhcp_iscsi_string,
1622
+		.string = &iscsi_password,
1623
+		.prefix = "",
1661 1624
 	},
1662 1625
 	{
1663 1626
 		.tag = DHCP_HOST_NAME,
1664
-		.apply = apply_dhcp_iscsi_string,
1627
+		.string = &iscsi_default_initiator_iqn,
1628
+		.prefix = "iqn.2000-09.org.etherboot:",
1665 1629
 	},
1666 1630
 };
1667 1631
 
1632
+/**
1633
+ * Apply iSCSI setting
1634
+ *
1635
+ * @v setting		iSCSI string setting
1636
+ * @ret rc		Return status code
1637
+ */
1638
+static int apply_iscsi_string_setting ( struct iscsi_string_setting *setting ){
1639
+	size_t prefix_len;
1640
+	int setting_len;
1641
+	size_t len;
1642
+	int check_len;
1643
+	char *p;
1644
+
1645
+	/* Free old string */
1646
+	free ( *setting->string );
1647
+	*setting->string = NULL;
1648
+
1649
+	/* Allocate new string */
1650
+	prefix_len = strlen ( setting->prefix );
1651
+	setting_len = fetch_setting_len ( NULL, setting->tag );
1652
+	if ( setting_len < 0 )
1653
+		return setting_len;
1654
+	len = ( prefix_len + setting_len + 1 );
1655
+	p = *setting->string = malloc ( len );
1656
+	if ( ! p )
1657
+		return -ENOMEM;
1658
+
1659
+	/* Fill new string */
1660
+	strcpy ( p, setting->prefix );
1661
+	check_len = fetch_string_setting ( NULL, setting->tag,
1662
+					   ( p + prefix_len ),
1663
+					   ( len - prefix_len ) );
1664
+	assert ( check_len == setting_len );
1665
+
1666
+	return 0;
1667
+}
1668
+
1669
+/**
1670
+ * Apply iSCSI settings
1671
+ *
1672
+ * @ret rc		Return status code
1673
+ */
1674
+static int apply_iscsi_settings ( void ) {
1675
+	struct iscsi_string_setting *setting;
1676
+	unsigned int i;
1677
+	int rc;
1678
+
1679
+	for ( i = 0 ; i < ( sizeof ( iscsi_string_settings ) /
1680
+			    sizeof ( iscsi_string_settings[0] ) ) ; i++ ) {
1681
+		setting = &iscsi_string_settings[i];
1682
+		if ( ( rc = apply_iscsi_string_setting ( setting ) ) != 0 ) {
1683
+			DBG ( "iSCSI could not apply setting %d\n",
1684
+			      setting->tag );
1685
+			return rc;
1686
+		}
1687
+	}
1688
+
1689
+	return 0;
1690
+}
1691
+
1692
+/** iSCSI settings applicator */
1693
+struct settings_applicator iscsi_settings_applicator __settings_applicator = {
1694
+	.apply = apply_iscsi_settings,
1695
+};
1696
+
1668 1697
 /****************************************************************************
1669 1698
  *
1670 1699
  * Initiator name

+ 17
- 18
src/net/udp/dns.c 파일 보기

@@ -30,7 +30,7 @@
30 30
 #include <gpxe/resolv.h>
31 31
 #include <gpxe/retry.h>
32 32
 #include <gpxe/tcpip.h>
33
-#include <gpxe/dhcp.h>
33
+#include <gpxe/settings.h>
34 34
 #include <gpxe/features.h>
35 35
 #include <gpxe/dns.h>
36 36
 
@@ -507,27 +507,26 @@ struct resolver dns_resolver __resolver ( RESOLV_NORMAL ) = {
507 507
 };
508 508
 
509 509
 /**
510
- * Apply DHCP nameserver option
510
+ * Apply nameserver setting
511 511
  *
512
- * @v tag		DHCP option tag
513
- * @v option		DHCP option
512
+ * @ret rc		Return status code
514 513
  */
515
-static int apply_dhcp_nameserver ( unsigned int tag __unused,
516
-				   struct dhcp_option *option ) {
517
-	struct sockaddr_in *sin_nameserver;
518
-
519
-	sin_nameserver = ( struct sockaddr_in * ) &nameserver;
520
-	sin_nameserver->sin_family = AF_INET;
521
-	dhcp_ipv4_option ( option, &sin_nameserver->sin_addr );
522
-
523
-	DBG ( "DNS using nameserver %s\n",
524
-	      inet_ntoa ( sin_nameserver->sin_addr ) );
514
+static int apply_nameserver_setting ( void ) {
515
+	struct sockaddr_in *sin_nameserver =
516
+		( struct sockaddr_in * ) &nameserver;
517
+	int len;
518
+
519
+	if ( ( len = fetch_ipv4_setting ( NULL, DHCP_DNS_SERVERS,
520
+					  &sin_nameserver->sin_addr ) ) >= 0 ){
521
+		sin_nameserver->sin_family = AF_INET;
522
+		DBG ( "DNS using nameserver %s\n",
523
+		      inet_ntoa ( sin_nameserver->sin_addr ) );
524
+	}
525 525
 
526 526
 	return 0;
527 527
 }
528 528
 
529
-/** DHCP nameserver applicator */
530
-struct dhcp_option_applicator dhcp_nameserver_applicator __dhcp_applicator = {
531
-	.tag = DHCP_DNS_SERVERS,
532
-	.apply = apply_dhcp_nameserver,
529
+/** Nameserver setting applicator */
530
+struct settings_applicator nameserver_applicator __settings_applicator = {
531
+	.apply = apply_nameserver_setting,
533 532
 };

Loading…
취소
저장