]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
switch to using flag bits
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Wed, 7 Apr 2021 11:46:07 +0000 (12:46 +0100)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Wed, 7 Apr 2021 11:46:07 +0000 (12:46 +0100)
nqptp-clock-sources.c
nqptp-clock-sources.h
nqptp-message-handlers.c
nqptp-shm-structures.h
nqptp.c

index bcc1d9b14a44bbd593b150d35be3e001a51b2124..d5bee8a15b77191af8379ff95d0485e4d3b83ad4 100644 (file)
@@ -93,7 +93,8 @@ void manage_clock_sources(uint64_t reception_time, clock_source *clocks_shared_i
   // do a garbage collect for clock records no longer in use
   for (i = 0; i < MAX_CLOCKS; i++) {
     // only if its in use and not a timing peer... don't need a mutex to check
-    if ((clocks_private_info[i].in_use != 0) && (clocks_shared_info[i].timing_peer == 0)) {
+    if ((clocks_private_info[i].in_use != 0) &&
+        ((clocks_shared_info[i].flags & (1 << clock_is_a_timing_peer)) == 0)) {
       int64_t time_since_last_use = reception_time - clocks_private_info[i].time_of_last_use;
       // using a sync timeout to determine when to drop the record...
       // the following give the sync receipt time in whole seconds
index 62d2b0b0f82bc0c9de42ed71244db9479592a7cf..d53d07b64f4de6cc6c13560a9512adf4d25c03e0 100644 (file)
@@ -28,12 +28,17 @@ enum stage {
   sync_seen,
 };
 
+#define MAX_TIMING_SAMPLES 480
+typedef struct {
+  uint64_t local, local_to_remote_offset;
+} timing_samples;
+
 // private information -- not for putting in shared memory -- about each clock source
 typedef struct {
   uint16_t sequence_number;
   uint16_t in_use;
   enum stage current_stage;
-  uint64_t t2;
+  uint64_t t2, previous_offset, previous_estimated_offset;
   // for garbage collection
   uint64_t time_of_last_use; // will be taken out of use if not used for a while and not in the
                              // timing peer group
@@ -41,6 +46,10 @@ typedef struct {
   // for Announce Qualification
   uint64_t announce_times[4]; // we'll check qualification and currency using these
   int is_one_of_ours;         // true if it is one of our own clocks
+  timing_samples samples[MAX_TIMING_SAMPLES];
+  int vacant_samples; // the number of elements in the timing_samples array that are not yet used
+  int next_sample_goes_here; // point to where in the timing samples array the next entries should
+                             // go
 } clock_source_private_data;
 
 int find_clock_source_record(char *sender_string, clock_source *clocks_shared_info,
index 48f0c3eb769991720ba483b06f03a7d74e933421..b8287dc3111e5ed5cccd6fa33050fb81d20acace 100644 (file)
@@ -37,7 +37,7 @@ void handle_control_port_messages(char *buf, ssize_t recv_len, clock_source *clo
       // turn off all is_timing_peers
       int i;
       for (i = 0; i < MAX_CLOCKS; i++)
-        clock_info[i].timing_peer = 0;
+        clock_info[i].flags &= ~(1 << clock_is_a_timing_peer); // turn off peer flags
 
       while (ip_list != NULL) {
         char *new_ip = strsep(&ip_list, " ");
@@ -47,7 +47,7 @@ void handle_control_port_messages(char *buf, ssize_t recv_len, clock_source *clo
           t = create_clock_source_record(new_ip, clock_info, clock_private_info,
                                          0); // don't use the mutex
 
-        clock_info[t].timing_peer = 1;
+        clock_info[t].flags |= (1 << clock_is_a_timing_peer);
       }
 
       rc = pthread_mutex_unlock(&shared_memory->shm_mutex);
@@ -55,7 +55,7 @@ void handle_control_port_messages(char *buf, ssize_t recv_len, clock_source *clo
         warn("Can't release mutex after set_timing_peers!");
 
       for (i = 0; i < MAX_CLOCKS; i++) {
-        if (clock_info[i].timing_peer != 0)
+        if ((clock_info[i].flags & (1 << clock_is_a_timing_peer)) != 0)
           debug(3, "%s is in the timing peer group.", &clock_info[i].ip);
       }
     } else {
@@ -106,7 +106,7 @@ void handle_announce(char *buf, ssize_t recv_len, clock_source *clock_info,
         i++;
       }
       if (valid_count >= foreign_master_threshold) {
-        if (clock_info->qualified == 0) {
+        if ((clock_info->flags & (1 << clock_is_qualified)) == 0) {
           uint64_t grandmaster_clock_id = nctohl(&msg->announce.grandmasterIdentity[0]);
           uint64_t grandmaster_clock_id_low = nctohl(&msg->announce.grandmasterIdentity[4]);
           grandmaster_clock_id = grandmaster_clock_id << 32;
@@ -131,18 +131,19 @@ void handle_announce(char *buf, ssize_t recv_len, clock_source *clock_info,
         }
         if (pthread_mutex_lock(&shared_memory->shm_mutex) != 0)
           warn("Can't acquire mutex to set_timing_peers!");
-        clock_info->qualified = 1;
+        clock_info->flags |= (1 << clock_is_qualified);
         if (pthread_mutex_unlock(&shared_memory->shm_mutex) != 0)
           warn("Can't release mutex after set_timing_peers!");
       } else {
-        if (clock_info->qualified != 0)
+        if ((clock_info->flags & (1 << clock_is_qualified)) !=
+            0) // if it was qualified, but now isn't
           debug(1,
                 "clock_id %" PRIx64
                 " on ip: %s \"Announce\" message is not Qualified -- See 9.3.2.5.",
                 clock_info->clock_id, clock_info->ip);
         if (pthread_mutex_lock(&shared_memory->shm_mutex) != 0)
           warn("Can't acquire mutex to set_timing_peers!");
-        clock_info->qualified = 0;
+        clock_info->flags &= ~(1 << clock_is_qualified);
         if (pthread_mutex_unlock(&shared_memory->shm_mutex) != 0)
           warn("Can't release mutex after set_timing_peers!");
       }
index 99cd4a76ae59bf5a64eb1403dd6288b3415b0773..16226d15ab1fa5e3ecff6277cd63a9946c570cc6 100644 (file)
 #include <netinet/in.h>
 #include <pthread.h>
 
+// most of this will probably become private when
+// the master clock selection stuff works automatically
+
+typedef enum { clock_is_valid, clock_is_a_timing_peer, clock_is_qualified } clock_flags;
+
 typedef struct {
   char ip[64]; // 64 is nicely aligned and bigger than INET6_ADDRSTRLEN (46)
   uint64_t clock_id;
   uint64_t local_time;                  // the local time when the offset was calculated
   uint64_t local_to_source_time_offset; // add this to the local time to get source time
-  uint8_t flags;                        // not used yet
-  uint8_t valid;                        // this entry is valid
-  uint8_t timing_peer;                  // true if this is in the current timing peer group
-  uint8_t qualified;                    // true if it has valid Announce messages
+  uint32_t flags;
 } clock_source;
 
 struct shm_structure {
diff --git a/nqptp.c b/nqptp.c
index 74931d9420df40d8bb24be41e093c33cde88a345..4d1c44f78e981bb74bf8364a70564ecedabfa50b 100644 (file)
--- a/nqptp.c
+++ b/nqptp.c
@@ -438,7 +438,7 @@ int main(void) {
                     // update/set the clock_id
 
                     shared_memory->clocks[the_clock].clock_id = packet_clock_id;
-                    shared_memory->clocks[the_clock].valid = 1;
+                    shared_memory->clocks[the_clock].flags |= (1 << clock_is_valid);
                     shared_memory->clocks[the_clock].local_time = clocks_private[the_clock].t2;
                     shared_memory->clocks[the_clock].local_to_source_time_offset = offset;
                     rc = pthread_mutex_unlock(&shared_memory->shm_mutex);