[chrony-dev] [PATCH] enable stratum setting for refclocks

[ Thread Index | Date Index | More chrony.tuxfamily.org/chrony-dev Archives ]


The attached patch allows the setting of the stratum for refclocks.
This is missing as e.g. a PHC refclock may not be stratum 0 (actual
timesource -> qemu host -> guest with chronyd using PHC).

Example configuration with patch applied:

refclock PHC /dev/ptp0 poll 3 dpoll -2 offset 0 stratum 2

Reference:
https://groups.google.com/forum/#!topic/linux.kernel/jYMCiZqQjII

Patch applies to chrony 3.2.
-- 
Andreas Steinmetz                       SPAMmers use robotrap@xxxxxxxx
diff -rNu chrony-3.2.orig/conf.c chrony-3.2/conf.c
--- chrony-3.2.orig/conf.c	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/conf.c	2017-09-20 18:35:51.751273210 +0200
@@ -680,7 +680,7 @@
 static void
 parse_refclock(char *line)
 {
-  int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options;
+  int n, poll, dpoll, filter_length, pps_rate, min_samples, max_samples, sel_options, stratum;
   int max_lock_age, pps_forced;
   uint32_t ref_id, lock_ref_id;
   double offset, delay, precision, max_dispersion, pulse_width;
@@ -704,6 +704,7 @@
   ref_id = 0;
   max_lock_age = 2;
   lock_ref_id = 0;
+  stratum = 0;
 
   if (!*line) {
     command_parse_error();
@@ -756,6 +757,10 @@
     } else if (!strcasecmp(cmd, "maxlockage")) {
       if (sscanf(line, "%d%n", &max_lock_age, &n) != 1)
         break;
+    } else if (!strcasecmp(cmd, "stratum")) {
+      if (sscanf(line, "%d%n", &stratum, &n) != 1 ||
+          stratum >= NTP_MAX_STRATUM || stratum < 0)
+        break;
     } else if (!strcasecmp(cmd, "maxsamples")) {
       if (sscanf(line, "%d%n", &max_samples, &n) != 1)
         break;
@@ -819,6 +824,7 @@
   refclock->ref_id = ref_id;
   refclock->max_lock_age = max_lock_age;
   refclock->lock_ref_id = lock_ref_id;
+  refclock->stratum = stratum;
 }
 
 /* ================================================== */
diff -rNu chrony-3.2.orig/ntp_core.c chrony-3.2/ntp_core.c
--- chrony-3.2.orig/ntp_core.c	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/ntp_core.c	2017-09-20 18:43:21.035147111 +0200
@@ -585,7 +585,7 @@
   result->source = SRC_CreateNewInstance(UTI_IPToRefid(&remote_addr->ip_addr),
                                          SRC_NTP, params->sel_options,
                                          &result->remote_addr.ip_addr,
-                                         params->min_samples, params->max_samples,
+                                         params->min_samples, params->max_samples, 0,
                                          params->min_delay, params->asymmetry);
 
   result->rx_timeout_id = 0;
diff -rNu chrony-3.2.orig/refclock.c chrony-3.2/refclock.c
--- chrony-3.2.orig/refclock.c	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/refclock.c	2017-09-20 18:42:11.825704319 +0200
@@ -260,7 +260,7 @@
   filter_init(&inst->filter, params->filter_length, params->max_dispersion);
 
   inst->source = SRC_CreateNewInstance(inst->ref_id, SRC_REFCLOCK, params->sel_options, NULL,
-                                       params->min_samples, params->max_samples, 0.0, 0.0);
+                                       params->min_samples, params->max_samples, params->stratum, 0.0, 0.0);
 
   DEBUG_LOG("refclock %s refid=%s poll=%d dpoll=%d filter=%d",
       params->driver_name, UTI_RefidToString(inst->ref_id),
diff -rNu chrony-3.2.orig/refclock.h chrony-3.2/refclock.h
--- chrony-3.2.orig/refclock.h	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/refclock.h	2017-09-20 18:35:51.753273223 +0200
@@ -43,6 +43,7 @@
   int max_samples;
   int sel_options;
   int max_lock_age;
+  int stratum;
   uint32_t ref_id;
   uint32_t lock_ref_id;
   double offset;
diff -rNu chrony-3.2.orig/sources.c chrony-3.2/sources.c
--- chrony-3.2.orig/sources.c	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/sources.c	2017-09-20 18:39:16.456581828 +0200
@@ -214,7 +214,7 @@
    the individual source-type instance creation routines. */
 
 SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int sel_options,
-                                   IPAddr *addr, int min_samples, int max_samples,
+                                   IPAddr *addr, int min_samples, int max_samples, int stratum,
                                    double min_delay, double asymmetry)
 {
   SRC_Instance result;
@@ -253,6 +253,9 @@
   SRC_SetRefid(result, ref_id, addr);
   SRC_ResetInstance(result);
 
+  if (result->type == SRC_REFCLOCK)
+    result->sel_info.stratum = stratum;
+
   n_sources++;
 
   return result;
@@ -652,7 +655,7 @@
     }
 
     si = &sources[i]->sel_info;
-    SST_GetSelectionData(sources[i]->stats, &now, &si->stratum,
+    SST_GetSelectionData(sources[i]->stats, &now, sources[i]->type == SRC_REFCLOCK ? NULL : &si->stratum,
                          &si->lo_limit, &si->hi_limit, &si->root_distance,
                          &si->std_dev, &first_sample_ago,
                          &si->last_sample_ago, &si->select_ok);
@@ -1351,6 +1354,9 @@
     /* Call stats module to fill out estimates */
     SST_DoSourceReport(src->stats, report, now);
 
+    if (src->type == SRC_REFCLOCK)
+      report->stratum = src->sel_info.stratum;
+
     return 1;
   }
 
diff -rNu chrony-3.2.orig/sources.h chrony-3.2/sources.h
--- chrony-3.2.orig/sources.h	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/sources.h	2017-09-20 18:40:56.873225170 +0200
@@ -60,7 +60,7 @@
    the individual source-type instance creation routines. */
 
 extern SRC_Instance SRC_CreateNewInstance(uint32_t ref_id, SRC_Type type, int sel_options,
-                                          IPAddr *addr, int min_samples, int max_samples,
+                                          IPAddr *addr, int min_samples, int max_samples, int stratum,
                                           double min_delay, double asymmetry);
 
 /* Function to get rid of a source when it is being unconfigured.
diff -rNu chrony-3.2.orig/sourcestats.c chrony-3.2/sourcestats.c
--- chrony-3.2.orig/sourcestats.c	2017-09-15 08:32:09.000000000 +0200
+++ chrony-3.2/sourcestats.c	2017-09-20 18:35:51.755273236 +0200
@@ -653,7 +653,8 @@
   i = get_runsbuf_index(inst, inst->best_single_sample);
   j = get_buf_index(inst, inst->best_single_sample);
 
-  *stratum = inst->strata[get_buf_index(inst, inst->n_samples - 1)];
+  if (stratum)
+    *stratum = inst->strata[get_buf_index(inst, inst->n_samples - 1)];
   *std_dev = inst->std_dev;
 
   sample_elapsed = fabs(UTI_DiffTimespecsToDouble(now, &inst->sample_times[i]));


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/