]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sources: turn select options into flags
authorMiroslav Lichvar <mlichvar@redhat.com>
Fri, 18 Dec 2015 15:18:53 +0000 (16:18 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Fri, 18 Dec 2015 15:29:47 +0000 (16:29 +0100)
This will allow adding new options for source selection which can be
combined with others.

client.c
cmdmon.c
cmdparse.c
conf.c
ntp_core.c
refclock.c
refclock.h
reports.h
sources.c
sources.h
srcparams.h

index 25d7c7adec7ab69af17eb0f0a46cd88020486e3e..d19ec849e1a1fad0d35dafe224763e4122e7aa9c 100644 (file)
--- 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;
index a0ded4a364f4dc18a77cf779fff2a386bac24c7b..bf22de95ff829e0569c7c7830e39c2ed238f37d8 100644 (file)
--- 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);
 
index 08fc6ddd12195199c7e5cfdad8c360597c8c5893..51a93641c5ff616c14abfef0621110461cc55105 100644 (file)
@@ -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 2a9227353a0b5c38b73874774f53e05c8c8908af..c5564766e23f4d08032889d70f5f4e5ff79e9b6d 100644 (file)
--- 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;
 }
index 262db61a34f4e670254eaba94cfbedab6dbbcf3a..3b18f8b26b3e5c9c99c2c1de1ccc01904707719f 100644 (file)
@@ -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);
 
index d995c7ae6abdb2f0bcdae0999108491bba317372..86c0de2f500ef610f1037d255e146bc92663a790 100644 (file)
@@ -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",
index 08996a7deba4362a6b9a496e575c27951eb3ac02..e089992f127c4eec7bd89b3160821af456fa3c36 100644 (file)
@@ -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;
index 24881dfd74f924f5c6fe896765fe70d435e77dfb..c6529d6353e53ec09400690e51aa5f42764ea37d 100644 (file)
--- 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 */
index e338f7f94d5a406a57a0df2e0ffe8caee498f89a..c67cf51e482df31dfcff60c4bbeaa5890fa4551c 100644 (file)
--- 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 */
index 8c6027bec05785fb6bbf9c09d4d9ad1069ebda83..4187d1339eef884631b9e93880532299022e3d39 100644 (file)
--- 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
index 826b78d2ad282c317d5cfad8d10c8db9fa02b59e..1e15af6b738d33d7615e2ac8e10070062084474f 100644 (file)
@@ -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 */