Browse Source

Add priority mechanism

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
291f072b82
2 changed files with 76 additions and 16 deletions
  1. 9
    0
      src/include/gpxe/dhcp.h
  2. 67
    16
      src/net/dhcpopts.c

+ 9
- 0
src/include/gpxe/dhcp.h View File

84
 	size_t len;
84
 	size_t len;
85
 };
85
 };
86
 
86
 
87
+extern unsigned long dhcp_num_option ( struct dhcp_option *option );
88
+extern struct dhcp_option * find_dhcp_option ( unsigned int tag,
89
+					   struct dhcp_option_block *options );
90
+
91
+static inline unsigned long
92
+find_dhcp_num_option ( unsigned int tag, struct dhcp_option_block *options ) {
93
+	return dhcp_num_option ( find_dhcp_option ( tag, options ) );
94
+}
95
+
87
 #endif /* _GPXE_DHCP_H */
96
 #endif /* _GPXE_DHCP_H */

+ 67
- 16
src/net/dhcpopts.c View File

93
  * the option data is well-formatted, and so must guard against flaws
93
  * the option data is well-formatted, and so must guard against flaws
94
  * such as options missing a @c DHCP_END terminator, or options whose
94
  * such as options missing a @c DHCP_END terminator, or options whose
95
  * length would take them beyond the end of the data block.
95
  * length would take them beyond the end of the data block.
96
- *
97
- * Searching for @c DHCP_PAD or @c DHCP_END tags, or using either @c
98
- * DHCP_PAD or @c DHCP_END as the encapsulator when constructing the
99
- * tag via DHCP_ENCAP_OPT() will produce undefined behaviour.
100
  */
96
  */
101
 static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
97
 static struct dhcp_option * find_dhcp_option_raw ( unsigned int tag,
102
 						   void *data, size_t len ) {
98
 						   void *data, size_t len ) {
104
 	ssize_t remaining = len;
100
 	ssize_t remaining = len;
105
 	unsigned int option_len;
101
 	unsigned int option_len;
106
 
102
 
107
-	assert ( tag != DHCP_PAD );
108
-	assert ( tag != DHCP_END );
109
-	assert ( DHCP_ENCAPSULATOR ( tag ) != DHCP_END );
110
-
111
 	while ( remaining ) {
103
 	while ( remaining ) {
112
-		/* Check for explicit end marker */
113
-		if ( option->tag == DHCP_END )
114
-			break;
115
 		/* Calculate length of this option.  Abort processing
104
 		/* Calculate length of this option.  Abort processing
116
 		 * if the length is malformed (i.e. takes us beyond
105
 		 * if the length is malformed (i.e. takes us beyond
117
 		 * the end of the data block).
106
 		 * the end of the data block).
123
 		/* Check for matching tag */
112
 		/* Check for matching tag */
124
 		if ( option->tag == tag )
113
 		if ( option->tag == tag )
125
 			return option;
114
 			return option;
115
+		/* Check for explicit end marker */
116
+		if ( option->tag == DHCP_END )
117
+			break;
126
 		/* Check for start of matching encapsulation block */
118
 		/* Check for start of matching encapsulation block */
127
 		if ( DHCP_ENCAPSULATOR ( tag ) &&
119
 		if ( DHCP_ENCAPSULATOR ( tag ) &&
128
 		     ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
120
 		     ( option->tag == DHCP_ENCAPSULATOR ( tag ) ) ) {
136
 	return NULL;
128
 	return NULL;
137
 }
129
 }
138
 
130
 
131
+/**
132
+ * Find DHCP option within options block
133
+ *
134
+ * @v tag		DHCP option tag to search for
135
+ * @v options		DHCP options block
136
+ * @ret option		DHCP option, or NULL if not found
137
+ *
138
+ * Searches for the DHCP option matching the specified tag within the
139
+ * options block.  Encapsulated options may be searched for by using
140
+ * DHCP_ENCAP_OPT() to construct the tag value.
141
+ */
142
+struct dhcp_option * find_dhcp_option ( unsigned int tag,
143
+					struct dhcp_option_block *options ) {
144
+	return find_dhcp_option_raw ( tag, options->data, options->len );
145
+}
146
+
147
+/**
148
+ * Find length of used portion of DHCP options block
149
+ *
150
+ * @v options		DHCP options block
151
+ * @ret len		Length of used portion of data block
152
+ *
153
+ * This searches for the @c DHCP_END marker within the options block.
154
+ * If found, the length of the used portion of the block (i.e. the
155
+ * portion containing everything @b before the @c DHCP_END marker, but
156
+ * excluding the @c DHCP_END marker itself) is returned.
157
+ *
158
+ * If no @c DHCP_END marker is present, the length of the whole
159
+ * options block is returned.
160
+ */
161
+size_t dhcp_option_block_len ( struct dhcp_option_block *options ) {
162
+	void *dhcpend;
163
+
164
+	if ( ( dhcpend = find_dhcp_option ( DHCP_END, options ) ) ) {
165
+		return ( dhcpend - options->data );
166
+	} else {
167
+		return options->len;
168
+	}
169
+}
170
+
139
 /**
171
 /**
140
  * Find DHCP option within all registered DHCP options blocks
172
  * Find DHCP option within all registered DHCP options blocks
141
  *
173
  *
145
  * Searches within all registered DHCP option blocks for the specified
177
  * Searches within all registered DHCP option blocks for the specified
146
  * tag.  Encapsulated options may be searched for by using
178
  * tag.  Encapsulated options may be searched for by using
147
  * DHCP_ENCAP_OPT() to construct the tag value.
179
  * DHCP_ENCAP_OPT() to construct the tag value.
180
+ *
181
+ * Where multiple option blocks contain the same DHCP option, the
182
+ * option from the highest-priority block will be returned.  (Priority
183
+ * of an options block is determined by the value of the @c
184
+ * DHCP_EB_PRIORITY option within the block, if present; the default
185
+ * priority is zero).
148
  */
186
  */
149
-struct dhcp_option * find_dhcp_option ( unsigned int tag ) {
187
+struct dhcp_option * find_global_dhcp_option ( unsigned int tag ) {
150
 	struct dhcp_option_block *options;
188
 	struct dhcp_option_block *options;
151
 	struct dhcp_option *option;
189
 	struct dhcp_option *option;
152
 
190
 
153
 	list_for_each_entry ( options, &option_blocks, list ) {
191
 	list_for_each_entry ( options, &option_blocks, list ) {
154
-		if ( ( option = find_dhcp_option_raw ( tag, options->data,
155
-						       options->len ) ) )
192
+		if ( ( option = find_dhcp_option ( tag, options ) ) )
156
 			return option;
193
 			return option;
157
 	}
194
 	}
158
 	return NULL;
195
 	return NULL;
163
  *
200
  *
164
  * @v options		DHCP option block
201
  * @v options		DHCP option block
165
  *
202
  *
166
- * Register a block of DHCP options
203
+ * Register a block of DHCP options.
167
  */
204
  */
168
 void register_dhcp_options ( struct dhcp_option_block *options ) {
205
 void register_dhcp_options ( struct dhcp_option_block *options ) {
169
-	list_add ( &options->list, &option_blocks );
206
+	struct dhcp_option_block *existing_options;
207
+	signed int existing_priority;
208
+	signed int priority;
209
+
210
+	/* Determine priority of new block */
211
+	priority = find_dhcp_num_option ( DHCP_EB_PRIORITY, options );
212
+
213
+	/* Insert after any existing blocks which have a higher priority */
214
+	list_for_each_entry ( existing_options, &option_blocks, list ) {
215
+		existing_priority = find_dhcp_num_option ( DHCP_EB_PRIORITY,
216
+							   existing_options );
217
+		if ( priority > existing_priority )
218
+			break;
219
+	}
220
+	list_add_tail ( &options->list, &existing_options->list );
170
 }
221
 }
171
 
222
 
172
 /**
223
 /**

Loading…
Cancel
Save