From fa15fb3d535201c59e9467d03d1e73e68ec5e45e Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Fri, 18 Dec 2015 16:18:53 +0100 Subject: [PATCH] sources: turn select options into flags This will allow adding new options for source selection which can be combined with others. --- client.c | 4 ++-- cmdmon.c | 19 ++++++------------- cmdparse.c | 6 +++--- conf.c | 11 +++++------ ntp_core.c | 2 +- refclock.c | 2 +- refclock.h | 2 +- reports.h | 2 +- sources.c | 29 ++++++++--------------------- sources.h | 9 +-------- srcparams.h | 6 +++++- 11 files changed, 34 insertions(+), 58 deletions(-) diff --git a/client.c b/client.c index 25d7c7a..d19ec84 100644 --- a/client.c +++ b/client.c @@ -1117,8 +1117,8 @@ process_cmd_add_server_or_peer(CMD_Request *msg, char *line) (data.params.online ? REQ_ADDSRC_ONLINE : 0) | (data.params.auto_offline ? REQ_ADDSRC_AUTOOFFLINE : 0) | (data.params.iburst ? REQ_ADDSRC_IBURST : 0) | - (data.params.sel_option == SRC_SelectPrefer ? REQ_ADDSRC_PREFER : 0) | - (data.params.sel_option == SRC_SelectNoselect ? REQ_ADDSRC_NOSELECT : 0)); + (data.params.sel_options & SRC_SELECT_PREFER ? REQ_ADDSRC_PREFER : 0) | + (data.params.sel_options & SRC_SELECT_NOSELECT ? REQ_ADDSRC_NOSELECT : 0)); result = 1; break; diff --git a/cmdmon.c b/cmdmon.c index a0ded4a..bf22de9 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -661,17 +661,9 @@ handle_source_data(CMD_Request *rx_message, CMD_Reply *tx_message) tx_message->data.source_data.mode = htons(RPY_SD_MD_REF); break; } - switch (report.sel_option) { - case RPT_NORMAL: - tx_message->data.source_data.flags = htons(0); - break; - case RPT_PREFER: - tx_message->data.source_data.flags = htons(RPY_SD_FLAG_PREFER); - break; - case RPT_NOSELECT: - tx_message->data.source_data.flags = htons(RPY_SD_FLAG_NOSELECT); - break; - } + tx_message->data.source_data.flags = + htons((report.sel_options & SRC_SELECT_PREFER ? RPY_SD_FLAG_PREFER : 0) | + (report.sel_options & SRC_SELECT_NOSELECT ? RPY_SD_FLAG_NOSELECT : 0)); tx_message->data.source_data.reachability = htons(report.reachability); tx_message->data.source_data.since_sample = htonl(report.latest_meas_ago); tx_message->data.source_data.orig_latest_meas = UTI_FloatHostToNetwork(report.orig_latest_meas); @@ -764,8 +756,9 @@ handle_add_source(NTP_Source_Type type, CMD_Request *rx_message, CMD_Reply *tx_m params.online = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_ONLINE ? 1 : 0; params.auto_offline = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_AUTOOFFLINE ? 1 : 0; params.iburst = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_IBURST ? 1 : 0; - params.sel_option = ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_PREFER ? SRC_SelectPrefer : - ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NOSELECT ? SRC_SelectNoselect : SRC_SelectNormal; + params.sel_options = + (ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_PREFER ? SRC_SELECT_PREFER : 0) | + (ntohl(rx_message->data.ntp_source.flags) & REQ_ADDSRC_NOSELECT ? SRC_SELECT_NOSELECT : 0); params.max_delay = UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay); params.max_delay_ratio = UTI_FloatNetworkToHost(rx_message->data.ntp_source.max_delay_ratio); diff --git a/cmdparse.c b/cmdparse.c index 08fc6dd..51a9364 100644 --- a/cmdparse.c +++ b/cmdparse.c @@ -63,7 +63,7 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) src->params.max_sources = SRC_DEFAULT_MAXSOURCES; src->params.min_samples = SRC_DEFAULT_MINSAMPLES; src->params.max_samples = SRC_DEFAULT_MAXSAMPLES; - src->params.sel_option = SRC_SelectNormal; + src->params.sel_options = 0; result = CPS_Success; @@ -165,10 +165,10 @@ CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src) } } else if (!strcasecmp(cmd, "noselect")) { - src->params.sel_option = SRC_SelectNoselect; + src->params.sel_options |= SRC_SELECT_NOSELECT; } else if (!strcasecmp(cmd, "prefer")) { - src->params.sel_option = SRC_SelectPrefer; + src->params.sel_options |= SRC_SELECT_PREFER; } else if (!strcasecmp(cmd, "version")) { if (sscanf(line, "%d%n", &src->params.version, &n) != 1) { diff --git a/conf.c b/conf.c index 2a92273..c556476 100644 --- a/conf.c +++ b/conf.c @@ -653,12 +653,11 @@ parse_ratelimit(char *line, int *enabled, int *interval, int *burst, int *leak) static void parse_refclock(char *line) { - int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples; + int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options; uint32_t ref_id, lock_ref_id; double offset, delay, precision, max_dispersion; char *p, *cmd, *name, *param; unsigned char ref[5]; - SRC_SelectOption sel_option; RefclockParameters *refclock; poll = 4; @@ -667,13 +666,13 @@ parse_refclock(char *line) pps_rate = 0; min_samples = SRC_DEFAULT_MINSAMPLES; max_samples = SRC_DEFAULT_MAXSAMPLES; + sel_options = 0; offset = 0.0; delay = 1e-9; precision = 0.0; max_dispersion = 0.0; ref_id = 0; lock_ref_id = 0; - sel_option = SRC_SelectNormal; if (!*line) { command_parse_error(); @@ -740,10 +739,10 @@ parse_refclock(char *line) break; } else if (!strcasecmp(cmd, "noselect")) { n = 0; - sel_option = SRC_SelectNoselect; + sel_options |= SRC_SELECT_NOSELECT; } else if (!strcasecmp(cmd, "prefer")) { n = 0; - sel_option = SRC_SelectPrefer; + sel_options |= SRC_SELECT_PREFER; } else { other_parse_error("Invalid refclock option"); return; @@ -765,11 +764,11 @@ parse_refclock(char *line) refclock->pps_rate = pps_rate; refclock->min_samples = min_samples; refclock->max_samples = max_samples; + refclock->sel_options = sel_options; refclock->offset = offset; refclock->delay = delay; refclock->precision = precision; refclock->max_dispersion = max_dispersion; - refclock->sel_option = sel_option; refclock->ref_id = ref_id; refclock->lock_ref_id = lock_ref_id; } diff --git a/ntp_core.c b/ntp_core.c index 262db61..3b18f8b 100644 --- a/ntp_core.c +++ b/ntp_core.c @@ -504,7 +504,7 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar /* Create a source instance for this NTP source */ result->source = SRC_CreateNewInstance(UTI_IPToRefid(&remote_addr->ip_addr), - SRC_NTP, params->sel_option, + SRC_NTP, params->sel_options, &result->remote_addr.ip_addr, params->min_samples, params->max_samples); diff --git a/refclock.c b/refclock.c index d995c7a..86c0de2 100644 --- a/refclock.c +++ b/refclock.c @@ -260,7 +260,7 @@ RCL_AddRefclock(RefclockParameters *params) filter_init(&inst->filter, params->filter_length, params->max_dispersion); - inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, params->sel_option, NULL, + inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, params->sel_options, NULL, params->min_samples, params->max_samples); DEBUG_LOG(LOGF_Refclock, "refclock %s refid=%s poll=%d dpoll=%d filter=%d", diff --git a/refclock.h b/refclock.h index 08996a7..e089992 100644 --- a/refclock.h +++ b/refclock.h @@ -40,13 +40,13 @@ typedef struct { int pps_rate; int min_samples; int max_samples; + int sel_options; uint32_t ref_id; uint32_t lock_ref_id; double offset; double delay; double precision; double max_dispersion; - SRC_SelectOption sel_option; } RefclockParameters; typedef struct RCL_Instance_Record *RCL_Instance; diff --git a/reports.h b/reports.h index 24881df..c6529d6 100644 --- a/reports.h +++ b/reports.h @@ -38,7 +38,7 @@ typedef struct { int poll; enum {RPT_NTP_CLIENT, RPT_NTP_PEER, RPT_LOCAL_REFERENCE} mode; enum {RPT_SYNC, RPT_UNREACH, RPT_FALSETICKER, RPT_JITTERY, RPT_CANDIDATE, RPT_OUTLIER} state; - enum {RPT_NORMAL, RPT_PREFER, RPT_NOSELECT} sel_option; + int sel_options; int reachability; unsigned long latest_meas_ago; /* seconds */ diff --git a/sources.c b/sources.c index e338f7f..c67cf51 100644 --- a/sources.c +++ b/sources.c @@ -118,7 +118,7 @@ struct SRC_Instance_Record { SRC_Type type; /* Options used when selecting sources */ - SRC_SelectOption sel_option; + int sel_options; /* Score against currently selected source */ double sel_score; @@ -209,7 +209,7 @@ void SRC_Finalise(void) /* Function to create a new instance. This would be called by one of the individual source-type instance creation routines. */ -SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_SelectOption sel_option, IPAddr *addr, int min_samples, int max_samples) +SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int sel_options, IPAddr *addr, int min_samples, int max_samples) { SRC_Instance result; @@ -241,7 +241,7 @@ SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_SelectOpt result->index = n_sources; result->type = type; - result->sel_option = sel_option; + result->sel_options = sel_options; SRC_SetRefid(result, ref_id, addr); SRC_ResetInstance(result); @@ -637,7 +637,7 @@ SRC_SelectSource(SRC_Instance updated_inst) assert(sources[i]->status != SRC_OK); /* Ignore sources which were added with the noselect option */ - if (sources[i]->sel_option == SRC_SelectNoselect) { + if (sources[i]->sel_options & SRC_SELECT_NOSELECT) { sources[i]->status = SRC_UNSELECTABLE; continue; } @@ -845,12 +845,12 @@ SRC_SelectSource(SRC_Instance updated_inst) /* If there are any sources with prefer option, reduce the list again only to the preferred sources */ for (i = 0; i < n_sel_sources; i++) { - if (sources[sel_sources[i]]->sel_option == SRC_SelectPrefer) + if (sources[sel_sources[i]]->sel_options & SRC_SELECT_PREFER) break; } if (i < n_sel_sources) { for (i = j = 0; i < n_sel_sources; i++) { - if (sources[sel_sources[i]]->sel_option != SRC_SelectPrefer) + if (!(sources[sel_sources[i]]->sel_options & SRC_SELECT_PREFER)) sources[sel_sources[i]]->status = SRC_NONPREFERRED; else sel_sources[j++] = sel_sources[i]; @@ -886,7 +886,7 @@ SRC_SelectSource(SRC_Instance updated_inst) for (i = 0; i < n_sources; i++) { /* Reset score for non-selectable sources */ if (sources[i]->status != SRC_OK || - (sel_prefer && sources[i]->sel_option != SRC_SelectPrefer)) { + (sel_prefer && !(sources[i]->sel_options & SRC_SELECT_PREFER))) { sources[i]->sel_score = 1.0; sources[i]->distant = DISTANT_PENALTY; continue; @@ -1256,20 +1256,7 @@ SRC_ReportSource(int index, RPT_SourceReport *report, struct timeval *now) break; } - switch (src->sel_option) { - case SRC_SelectNormal: - report->sel_option = RPT_NORMAL; - break; - case SRC_SelectPrefer: - report->sel_option = RPT_PREFER; - break; - case SRC_SelectNoselect: - report->sel_option = RPT_NOSELECT; - break; - default: - assert(0); - } - + report->sel_options = src->sel_options; report->reachability = src->reachability; /* Call stats module to fill out estimates */ diff --git a/sources.h b/sources.h index 8c6027b..4187d13 100644 --- a/sources.h +++ b/sources.h @@ -55,17 +55,10 @@ typedef enum { SRC_REFCLOCK /* Rerefence clock */ } SRC_Type; -/* Options used when selecting sources */ -typedef enum { - SRC_SelectNormal, - SRC_SelectNoselect, - SRC_SelectPrefer -} SRC_SelectOption; - /* Function to create a new instance. This would be called by one of the individual source-type instance creation routines. */ -extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, SRC_SelectOption sel_option, IPAddr *addr, int min_samples, int max_samples); +extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int sel_options, IPAddr *addr, int min_samples, int max_samples); /* Function to get rid of a source when it is being unconfigured. This may cause the current reference source to be reselected, if this diff --git a/srcparams.h b/srcparams.h index 826b78d..1e15af6 100644 --- a/srcparams.h +++ b/srcparams.h @@ -42,11 +42,11 @@ typedef struct { int max_sources; int min_samples; int max_samples; + int sel_options; uint32_t authkey; double max_delay; double max_delay_ratio; double max_delay_dev_ratio; - SRC_SelectOption sel_option; } SourceParameters; #define SRC_DEFAULT_PORT 123 @@ -63,4 +63,8 @@ typedef struct { #define SRC_DEFAULT_MAXSAMPLES (-1) #define INACTIVE_AUTHKEY 0 +/* Flags for source selection */ +#define SRC_SELECT_NOSELECT 0x1 +#define SRC_SELECT_PREFER 0x2 + #endif /* GOT_SRCPARAMS_H */