]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
check for mastership whenever a clock entry is garbage collected
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Sat, 1 May 2021 13:27:02 +0000 (14:27 +0100)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Sat, 1 May 2021 13:27:02 +0000 (14:27 +0100)
nqptp-clock-sources.c
nqptp-clock-sources.h
nqptp-message-handlers.c

index c38a9cdf9e29b058fcd8b74d5d239513f8666449..c32d4240a13663cbac247cba3ee8c6baf0c787b3 100644 (file)
@@ -98,8 +98,11 @@ void manage_clock_sources(uint64_t reception_time, clock_source_private_data *cl
       // seconds to nanoseconds
       syncTimeout = syncTimeout * 1000000000;
       if (time_since_last_use > syncTimeout) {
+        uint32_t old_flags = clocks_private_info[i].flags;
         debug(2, "delete record for: %s.", &clocks_private_info[i].ip);
         memset(&clocks_private_info[i], 0, sizeof(clock_source_private_data));
+        if (old_flags != 0)
+          update_master();
       }
     }
   }
@@ -148,3 +151,105 @@ void update_clock_self_identifications(clock_source_private_data *clocks_private
   }
   freeifaddrs(ifap);
 }
+
+void update_master() {
+  int old_master = -1;
+  // find the current master clock if there is one and turn off all mastership
+  int i;
+  for (i = 0; i < MAX_CLOCKS; i++) {
+    if ((clocks_private[i].flags & (1 << clock_is_master)) != 0)
+      if (old_master == -1)
+        old_master = i;                                     // find old master
+    clocks_private[i].flags &= ~(1 << clock_is_master); // turn them all off
+  }
+
+  int best_so_far = -1;
+  int timing_peer_count = 0;
+  uint32_t acceptance_mask =
+      (1 << clock_is_valid) | (1 << clock_is_qualified) | (1 << clock_is_a_timing_peer);
+  for (i = 0; i < MAX_CLOCKS; i++) {
+    if ((clocks_private[i].flags & acceptance_mask) == acceptance_mask) {
+      // found a possible clock candidate
+      timing_peer_count++;
+      if (best_so_far == -1) {
+        best_so_far = i;
+      } else {
+        // do the data set comparison detailed in Figure 27 and Figure 28 on pp89-90
+        if (clocks_private[i].grandmasterIdentity ==
+            clocks_private[best_so_far].grandmasterIdentity) {
+          // should implement Figure 28 here
+        } else if (clocks_private[i].grandmasterPriority1 <
+                   clocks_private[best_so_far].grandmasterPriority1) {
+          best_so_far = i;
+        } else if (clocks_private[i].grandmasterClass <
+                   clocks_private[best_so_far].grandmasterClass) {
+          best_so_far = i;
+        } else if (clocks_private[i].grandmasterAccuracy <
+                   clocks_private[best_so_far].grandmasterAccuracy) {
+          best_so_far = i;
+        } else if (clocks_private[i].grandmasterVariance <
+                   clocks_private[best_so_far].grandmasterVariance) {
+          best_so_far = i;
+        } else if (clocks_private[i].grandmasterPriority2 <
+                   clocks_private[best_so_far].grandmasterPriority2) {
+          best_so_far = i;
+        } else if (clocks_private[i].grandmasterIdentity <
+                   clocks_private[best_so_far].grandmasterIdentity) {
+          best_so_far = i;
+        }
+      }
+    }
+  }
+  if (best_so_far == -1) {
+    // no master clock
+    if (old_master != -1) {
+      // but there was a master clock, so remove it
+      debug(1,"shm interface -- remove master clock designation");
+      update_master_clock_info(0, 0, 0);
+    }
+    if (timing_peer_count == 0)
+      debug(2, "No timing peer list found");
+    else
+      debug(1, "No master clock not found!");
+  } else {
+    // we found a master clock
+    clocks_private[best_so_far].flags |= (1 << clock_is_master);
+    // master_clock_index = best_so_far;
+    if (old_master != best_so_far) {
+      update_master_clock_info(clocks_private[best_so_far].clock_id,
+                               clocks_private[best_so_far].local_time,
+                               clocks_private[best_so_far].local_to_source_time_offset);
+    }
+  }
+
+  int records_in_use = 0;
+  for (i = 0; i < MAX_CLOCKS; i++)
+   if (clocks_private[i].in_use != 0)
+     records_in_use++;
+  if (records_in_use > 0) {
+    debug(1,"");
+    debug(1,"Current NQPTP Status:");
+    uint32_t peer_mask = (1 << clock_is_a_timing_peer);
+    uint32_t peer_clock_mask =  peer_mask | (1 << clock_is_valid);
+    uint32_t peer_master_mask = peer_clock_mask | (1 << clock_is_master);
+    uint32_t non_peer_clock_mask =  (1 << clock_is_valid);
+    uint32_t non_peer_master_mask = non_peer_clock_mask | (1 << clock_is_master);
+    for (i = 0; i < MAX_CLOCKS; i++) {
+      if (clocks_private[i].in_use != 0) {
+        if ((clocks_private[i].flags & peer_master_mask) == peer_master_mask) {
+          debug(1,"  Peer Master:     %" PRIx64 "  %s.", clocks_private[i].clock_id, clocks_private[i].ip);
+        } else if ((clocks_private[i].flags & peer_clock_mask) == peer_clock_mask) {
+          debug(1,"  Peer Clock:      %" PRIx64 "  %s.", clocks_private[i].clock_id, clocks_private[i].ip);
+        } else if ((clocks_private[i].flags & peer_mask) == peer_mask) {
+          debug(1,"  Peer:                              %s.",clocks_private[i].ip);
+        } else if ((clocks_private[i].flags & non_peer_master_mask) == non_peer_master_mask) {
+          debug(1,"  Non Peer Master: %" PRIx64 "  %s.", clocks_private[i].clock_id, clocks_private[i].ip);
+        } else if ((clocks_private[i].flags & non_peer_clock_mask) == non_peer_clock_mask) {
+          debug(1,"  Non Peer Clock:  %" PRIx64 "  %s.", clocks_private[i].clock_id, clocks_private[i].ip);
+        } else {
+          debug(1,"  Non Peer Record:                   %s.",clocks_private[i].ip);
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
index a2cdbf8307bf5400d1d09fa2381d447a3fea2405..6ed7001629ae1a5412ebac39f719e9f16fcefbda 100644 (file)
@@ -87,4 +87,6 @@ void manage_clock_sources(uint64_t reception_time, clock_source_private_data *cl
 
 extern clock_source_private_data clocks_private[MAX_CLOCKS];
 
+void update_master();
+
 #endif
index 7007fae616919544799b67bb55a07310941902c4..21cbaa4fd862b0e0b5c6a848f9166fa7c7547491 100644 (file)
 #include "nqptp-ptp-definitions.h"
 #include "nqptp-utilities.h"
 
-void update_master_old(clock_source_private_data *clock_private_info) {
-  int old_master = -1;
-  // find the current master clock if there is one and turn off all mastership
-  int i;
-  for (i = 0; i < MAX_CLOCKS; i++) {
-    if ((clock_private_info[i].flags & (1 << clock_is_master)) != 0)
-      if (old_master == -1)
-        old_master = i;                                     // find old master
-    clock_private_info[i].flags &= ~(1 << clock_is_master); // turn them all off
-  }
-
-  int best_so_far = -1;
-  int timing_peer_count = 0;
-  uint32_t acceptance_mask =
-      (1 << clock_is_valid) | (1 << clock_is_qualified) | (1 << clock_is_a_timing_peer);
-  for (i = 0; i < MAX_CLOCKS; i++) {
-    if ((clock_private_info[i].flags & acceptance_mask) == acceptance_mask) {
-      // found a possible clock candidate
-      timing_peer_count++;
-      if (best_so_far == -1) {
-        best_so_far = i;
-      } else {
-        // do the data set comparison detailed in Figure 27 and Figure 28 on pp89-90
-        if (clock_private_info[i].grandmasterIdentity ==
-            clock_private_info[best_so_far].grandmasterIdentity) {
-          // should implement Figure 28 here
-        } else if (clock_private_info[i].grandmasterPriority1 <
-                   clock_private_info[best_so_far].grandmasterPriority1) {
-          best_so_far = i;
-        } else if (clock_private_info[i].grandmasterClass <
-                   clock_private_info[best_so_far].grandmasterClass) {
-          best_so_far = i;
-        } else if (clock_private_info[i].grandmasterAccuracy <
-                   clock_private_info[best_so_far].grandmasterAccuracy) {
-          best_so_far = i;
-        } else if (clock_private_info[i].grandmasterVariance <
-                   clock_private_info[best_so_far].grandmasterVariance) {
-          best_so_far = i;
-        } else if (clock_private_info[i].grandmasterPriority2 <
-                   clock_private_info[best_so_far].grandmasterPriority2) {
-          best_so_far = i;
-        } else if (clock_private_info[i].grandmasterIdentity <
-                   clock_private_info[best_so_far].grandmasterIdentity) {
-          best_so_far = i;
-        }
-      }
-    }
-  }
-  if (best_so_far == -1) {
-    // no master clock
-    if (old_master != -1) {
-      // but there was a master clock, so remove it
-      debug(1,"remove master clock designation");
-      update_master_clock_info(0, 0, 0);
-    }
-    if (timing_peer_count == 0)
-      debug(2, "No timing peer list found");
-    else
-      debug(1, "No master clock not found!");
-  } else {
-    // we found a master clock
-    clock_private_info[best_so_far].flags |= (1 << clock_is_master);
-    // master_clock_index = best_so_far;
-    if (old_master != best_so_far) {
-      update_master_clock_info(clock_private_info[best_so_far].clock_id,
-                               clock_private_info[best_so_far].local_time,
-                               clock_private_info[best_so_far].local_to_source_time_offset);
-    }
-  }
-
-  int records_in_use = 0;
-  for (i = 0; i < MAX_CLOCKS; i++)
-   if (clock_private_info[i].in_use != 0)
-     records_in_use++;
-  if (records_in_use > 0) {
-    debug(1,"");
-    debug(1,"Current NQPTP Status:");
-    uint32_t peer_mask = (1 << clock_is_a_timing_peer);
-    uint32_t peer_clock_mask =  peer_mask | (1 << clock_is_valid);
-    uint32_t peer_master_mask = peer_clock_mask | (1 << clock_is_master);
-    uint32_t non_peer_clock_mask =  (1 << clock_is_valid);
-    uint32_t non_peer_master_mask = non_peer_clock_mask | (1 << clock_is_master);
-    for (i = 0; i < MAX_CLOCKS; i++) {
-      if (clock_private_info[i].in_use != 0) {
-        if ((clock_private_info[i].flags & peer_master_mask) == peer_master_mask) {
-          debug(1,"  Peer Master:     %" PRIx64 "  %s.", clock_private_info[i].clock_id, clock_private_info[i].ip);
-        } else if ((clock_private_info[i].flags & peer_clock_mask) == peer_clock_mask) {
-          debug(1,"  Peer Clock:      %" PRIx64 "  %s.", clock_private_info[i].clock_id, clock_private_info[i].ip);
-        } else if ((clock_private_info[i].flags & peer_mask) == peer_mask) {
-          debug(1,"  Peer:                              %s.",clock_private_info[i].ip);
-        } else if ((clock_private_info[i].flags & non_peer_master_mask) == non_peer_master_mask) {
-          debug(1,"  Non Peer Master: %" PRIx64 "  %s.", clock_private_info[i].clock_id, clock_private_info[i].ip);
-        } else if ((clock_private_info[i].flags & non_peer_clock_mask) == non_peer_clock_mask) {
-          debug(1,"  Non Peer Clock:  %" PRIx64 "  %s.", clock_private_info[i].clock_id, clock_private_info[i].ip);
-        } else {
-          debug(1,"  Non Peer Record:                   %s.",clock_private_info[i].ip);
-        }
-      }
-    }
-  }
-}
-
-void update_master() { update_master_old(clocks_private); }
-
 void handle_control_port_messages(char *buf, ssize_t recv_len,
                                   clock_source_private_data *clock_private_info) {
   if (recv_len != -1) {