Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Settings] Introduce settings applicators.
Convert DHCP option applicators in dns.c and iscsi.c to settings
applicators.

Kill off DHCP option applicators.
  • Loading branch information
Michael Brown committed Mar 20, 2008
1 parent acfa144 commit cf03304
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 138 deletions.
111 changes: 102 additions & 9 deletions src/core/settings.c
Expand Up @@ -37,15 +37,21 @@

/** Registered setting types */
static struct setting_type setting_types[0]
__table_start ( struct setting_type, setting_types );
__table_start ( struct setting_type, setting_types );
static struct setting_type setting_types_end[0]
__table_end ( struct setting_type, setting_types );
__table_end ( struct setting_type, setting_types );

/** Registered named settings */
static struct named_setting named_settings[0]
__table_start ( struct named_setting, named_settings );
__table_start ( struct named_setting, named_settings );
static struct named_setting named_settings_end[0]
__table_end ( struct named_setting, named_settings );
__table_end ( struct named_setting, named_settings );

/** Registered settings applicators */
static struct settings_applicator settings_applicators[0]
__table_start ( struct settings_applicator, settings_applicators );
static struct settings_applicator settings_applicators_end[0]
__table_end ( struct settings_applicator, settings_applicators );

/**
* Obtain printable version of a settings tag number
Expand Down Expand Up @@ -94,10 +100,6 @@ int simple_settings_fetch ( struct settings *settings, unsigned int tag,
return ( len ? len : 8 );
}

// Dummy routine just for testing
static void apply_settings ( void ) {
}

/** Simple settings operations */
struct settings_operations simple_settings_operations = {
.store = simple_settings_store,
Expand All @@ -113,6 +115,66 @@ struct settings settings_root = {
.op = &simple_settings_operations,
};

/**
* Apply all settings
*
* @ret rc Return status code
*/
static int apply_settings ( void ) {
struct settings_applicator *applicator;
int rc;

/* Call all settings applicators */
for ( applicator = settings_applicators ;
applicator < settings_applicators_end ; applicator++ ) {
if ( ( rc = applicator->apply() ) != 0 ) {
DBG ( "Could not apply settings using applicator "
"%p: %s\n", applicator, strerror ( rc ) );
return rc;
}
}

return 0;
}

/**
* Reprioritise settings
*
* @v settings Settings block
*
* Reorders the settings block amongst its siblings according to its
* priority.
*/
static void reprioritise_settings ( struct settings *settings ) {
struct settings *parent = settings->parent;
long priority;
struct settings *tmp;
long tmp_priority;

/* Stop when we reach the top of the tree */
if ( ! parent )
return;

/* Read priority, if present */
priority = 0;
fetch_int_setting ( settings, DHCP_EB_PRIORITY, &priority );

/* Remove from siblings list */
list_del ( &settings->siblings );

/* Reinsert after any existing blocks which have a higher priority */
list_for_each_entry ( tmp, &parent->children, siblings ) {
tmp_priority = 0;
fetch_int_setting ( tmp, DHCP_EB_PRIORITY, &tmp_priority );
if ( priority > tmp_priority )
break;
}
list_add_tail ( &settings->siblings, &tmp->siblings );

/* Recurse up the tree */
reprioritise_settings ( parent );
}

/**
* Register settings block
*
Expand All @@ -134,6 +196,9 @@ int register_settings ( struct settings *settings, struct settings *parent ) {
list_add_tail ( &settings->siblings, &parent->children );
DBGC ( settings, "Settings %p registered\n", settings );

/* Fix up settings priority */
reprioritise_settings ( settings );

/* Apply potentially-updated settings */
apply_settings();

Expand Down Expand Up @@ -206,6 +271,34 @@ struct settings * find_settings ( const char *name ) {
******************************************************************************
*/

/**
* Store value of setting
*
* @v settings Settings block
* @v tag Setting tag number
* @v data Setting data, or NULL to clear setting
* @v len Length of setting data
* @ret rc Return status code
*/
int store_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) {
int rc;

/* Store setting */
if ( ( rc = settings->op->store ( settings, tag, data, len ) ) != 0 )
return rc;

/* Reprioritise settings if necessary */
if ( tag == DHCP_EB_PRIORITY )
reprioritise_settings ( settings );

/* Apply potentially-updated setting */
if ( ( rc = apply_settings() ) != 0 )
return rc;

return 0;
}

/**
* Fetch value of setting
*
Expand Down Expand Up @@ -288,7 +381,7 @@ int fetch_ipv4_setting ( struct settings *settings, unsigned int tag,
len = fetch_setting ( settings, tag, inp, sizeof ( *inp ) );
if ( len < 0 )
return len;
if ( len != sizeof ( *inp ) )
if ( len < ( int ) sizeof ( *inp ) )
return -ERANGE;
return len;
}
Expand Down
17 changes: 0 additions & 17 deletions src/include/gpxe/dhcp.h
Expand Up @@ -489,23 +489,6 @@ struct dhcp_packet {
struct dhcp_option_block options;
};

/** A DHCP option applicator */
struct dhcp_option_applicator {
/** DHCP option tag */
unsigned int tag;
/** Applicator
*
* @v tag DHCP option tag
* @v option DHCP option
* @ret rc Return status code
*/
int ( * apply ) ( unsigned int tag, struct dhcp_option *option );
};

/** Declare a DHCP option applicator */
#define __dhcp_applicator \
__table ( struct dhcp_option_applicator, dhcp_applicators, 01 )

/**
* Get reference to DHCP options block
*
Expand Down
32 changes: 18 additions & 14 deletions src/include/gpxe/settings.h
Expand Up @@ -122,6 +122,22 @@ struct named_setting {
/** Declare a configuration setting */
#define __named_setting __table ( struct named_setting, named_settings, 01 )

/**
* A settings applicator
*
*/
struct settings_applicator {
/** Apply updated settings
*
* @ret rc Return status code
*/
int ( * apply ) ( void );
};

/** Declare a settings applicator */
#define __settings_applicator \
__table ( struct settings_applicator, settings_applicators, 01 )

extern int simple_settings_store ( struct settings *settings, unsigned int tag,
const void *data, size_t len );
extern int simple_settings_fetch ( struct settings *settings, unsigned int tag,
Expand All @@ -131,6 +147,8 @@ extern struct settings_operations simple_settings_operations;
extern int register_settings ( struct settings *settings,
struct settings *parent );
extern void unregister_settings ( struct settings *settings );
extern int store_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len );
extern int fetch_setting ( struct settings *settings, unsigned int tag,
void *data, size_t len );
extern int fetch_setting_len ( struct settings *settings, unsigned int tag );
Expand Down Expand Up @@ -179,20 +197,6 @@ static inline void settings_init ( struct settings *settings,
settings->name = name;
}

/**
* Store value of setting
*
* @v settings Settings block
* @v tag Setting tag number
* @v data Setting data, or NULL to clear setting
* @v len Length of setting data
* @ret rc Return status code
*/
static inline int store_setting ( struct settings *settings, unsigned int tag,
const void *data, size_t len ) {
return settings->op->store ( settings, tag, data, len );
}

/**
* Delete setting
*
Expand Down
24 changes: 0 additions & 24 deletions src/net/dhcpopts.c
Expand Up @@ -37,12 +37,6 @@
/** List of registered DHCP option blocks */
LIST_HEAD ( dhcp_option_blocks );

/** Registered DHCP option applicators */
static struct dhcp_option_applicator dhcp_option_applicators[0]
__table_start ( struct dhcp_option_applicator, dhcp_applicators );
static struct dhcp_option_applicator dhcp_option_applicators_end[0]
__table_end ( struct dhcp_option_applicator, dhcp_applicators );

/**
* Obtain printable version of a DHCP option tag
*
Expand Down Expand Up @@ -578,13 +572,9 @@ void delete_dhcp_option ( struct dhcp_option_block *options,
* @ret rc Return status code
*/
int apply_dhcp_options ( struct dhcp_option_block *options ) {
struct dhcp_option_applicator *applicator;
struct dhcp_option *option;
struct in_addr tftp_server;
struct uri *uri;
char uri_string[32];
unsigned int tag;
int rc;

/* Set current working URI based on TFTP server */
find_dhcp_ipv4_option ( options, DHCP_EB_SIADDR, &tftp_server );
Expand All @@ -596,20 +586,6 @@ int apply_dhcp_options ( struct dhcp_option_block *options ) {
churi ( uri );
uri_put ( uri );

/* Call all registered DHCP option applicators */
for ( applicator = dhcp_option_applicators ;
applicator < dhcp_option_applicators_end ; applicator++ ) {
tag = applicator->tag;
option = find_dhcp_option ( options, tag );
if ( ! option )
continue;
if ( ( rc = applicator->apply ( tag, option ) ) != 0 ) {
DBG ( "Could not apply DHCP option %s: %s\n",
dhcp_tag_name ( tag ), strerror ( rc ) );
return rc;
}
}

return 0;
}

Expand Down

0 comments on commit cf03304

Please sign in to comment.