]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
Remove superceded code. Calculate priority of ping based on priority of clock.
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Sun, 7 Nov 2021 10:52:45 +0000 (10:52 +0000)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Sun, 7 Nov 2021 10:52:45 +0000 (10:52 +0000)
general-utilities.c
general-utilities.h
nqptp-clock-sources.h
nqptp-message-handlers.c
nqptp-message-handlers.h
nqptp-ptp-definitions.h
nqptp.c
nqptp.h

index 304de0b287c0320dd406406df6900be131d3a56c..1a89578f2e521e2bc362a7de21fc21e8164799d2 100644 (file)
@@ -37,13 +37,12 @@ void hcton64(uint64_t num, uint8_t *p) {
 uint64_t nctoh64(const uint8_t *p) { // read 4 characters from *p and do ntohl on them
   // this is to avoid possible aliasing violations
   uint64_t value = nctohl(p);
-  uint64_t value_low = nctohl(p+4);
+  uint64_t value_low = nctohl(p + 4);
   value = value << 32;
   value = value + value_low;
   return value;
 }
 
-
 uint32_t nctohl(const uint8_t *p) { // read 4 characters from *p and do ntohl on them
   // this is to avoid possible aliasing violations
   uint32_t holder;
index 79f34f338ff945cbe167bea66ea1641ce5f67bbd..3dbeb89fdc19e34d263cf002f3624cbc908ffea3 100644 (file)
@@ -38,9 +38,9 @@
 
 void hcton64(uint64_t num, uint8_t *p);
 
-//these are designed to avoid aliasing check errors
+// these are designed to avoid aliasing check errors
 uint64_t nctoh64(const uint8_t *p);
-uint32_t nctohl(const uint8_t *p); 
+uint32_t nctohl(const uint8_t *p);
 uint16_t nctohs(const uint8_t *p);
 uint64_t timespec_to_ns(struct timespec *tn);
 uint64_t get_time_now();
index 3b30ff713fc13c49bfaa68ac023dac3642f8b62b..54d3bf60db7e5ecea50c6bd5d2c724c02f2204b9 100644 (file)
@@ -41,9 +41,10 @@ typedef struct {
 
 // information about each clock source
 typedef struct {
-  char ip[64];            // 64 is nicely aligned and bigger than INET6_ADDRSTRLEN (46)
-  int family;             // AF_INET or AF_INET6
-  int announcements_sent; // number of announce messages returned to this clock
+  char ip[64];                         // 64 is nicely aligned and bigger than INET6_ADDRSTRLEN (46)
+  int family;                          // AF_INET or AF_INET6
+  int announcements_without_followups; // add 1 for every announce, reset with a followup
+  int announcements_sent;              // number of announce messages returned to this clock
   uint64_t clock_id;
   uint64_t local_time; // the local time when the offset was calculated
   uint64_t source_time;
index ce4cf8808e243777b49bd2e058df971965c20924..d5c89a748e4cd0bb502f74a9fbe1bc08aa6c2bc4 100644 (file)
@@ -26,8 +26,7 @@
 #include "nqptp-utilities.h"
 
 void handle_control_port_messages(char *buf, ssize_t recv_len,
-                                  clock_source_private_data *clock_private_info,
-                                  uint64_t reception_time) {
+                                  clock_source_private_data *clock_private_info) {
   if (recv_len != -1) {
     buf[recv_len - 1] = 0; // make sure there's a null in it!
     debug(2, "New timing peer list: \"%s\".", buf);
@@ -56,9 +55,6 @@ void handle_control_port_messages(char *buf, ssize_t recv_len,
         }
       }
 
-      timing_peer_list_creation_time = reception_time;
-      timing_peer_list_followup_seen = 0;
-
       // now find and mark the best clock in the timing peer list as the master
       update_master();
 
@@ -90,10 +86,17 @@ void handle_announce(char *buf, ssize_t recv_len, clock_source_private_data *clo
       packet_clock_id = packet_clock_id << 32;
       packet_clock_id = packet_clock_id + packet_clock_id_low;
       clock_private_info->clock_id = packet_clock_id;
+      clock_private_info->grandmasterPriority1 =
+          msg->announce.grandmasterPriority1; // need this for possibly pinging it later...
+      clock_private_info->grandmasterPriority2 =
+          msg->announce.grandmasterPriority2; // need this for possibly pinging it later...
 
       debug(2, "announcement seen from %" PRIx64 " at %s.", clock_private_info->clock_id,
             clock_private_info->ip);
 
+      if (clock_private_info->announcements_without_followups < 5)
+        clock_private_info->announcements_without_followups++;
+
       int i;
       // number of elements in the array is 4, hence the 4-1 stuff
       for (i = 4 - 1; i > 1 - 1; i--) {
@@ -241,14 +244,7 @@ void handle_follow_up(char *buf, __attribute__((unused)) ssize_t recv_len,
 
   // update our sample information
 
-  if ((timing_peer_list_followup_seen == 0) &&
-      ((clock_private_info->flags & (1 << clock_is_a_timing_peer)) != 0)) {
-    debug(2, "Follow_Up from: %" PRIx64 " at %s.", clock_private_info->clock_id,
-          clock_private_info->ip);
-  }
-
-  timing_peer_list_followup_seen =
-      1; // say we've seen a follow_up (to suppress sending an Announce message)
+  clock_private_info->announcements_without_followups = 0; // we've seen a followup
 
   clock_private_info->samples[clock_private_info->next_sample_goes_here].local_time =
       reception_time;
index 9fb158e616d84ccc962e5c4b26ffe8b5866dc744..a0e28656c94de25939d4729dde390b8bb7c9a971 100644 (file)
@@ -35,7 +35,6 @@ void handle_delay_resp(char *buf, ssize_t recv_len, clock_source_private_data *c
                        uint64_t reception_time);
 
 void handle_control_port_messages(char *buf, ssize_t recv_len,
-                                  clock_source_private_data *clock_private_info,
-                                  uint64_t reception_time);
+                                  clock_source_private_data *clock_private_info);
 
 #endif
\ No newline at end of file
index 3030f03bc4f40e7c053cd10693cbcbd044dde81b..5250a560aea17971b7b1de43030e1b06fa145aec 100644 (file)
@@ -176,8 +176,6 @@ struct __attribute__((__packed__)) ptp_tlv {
   uint8_t dataField[0];
 };
 
-
-
 // this is the extra part for a Signaling message (13.12, pp 132) without any TLVs
 struct __attribute__((__packed__)) ptp_signaling {
   uint8_t targetPortIdentity[10];
@@ -225,5 +223,4 @@ struct __attribute__((__packed__)) ptp_signaling_message {
   struct ptp_signaling signaling;
 };
 
-
 #endif
\ No newline at end of file
diff --git a/nqptp.c b/nqptp.c
index 5eed96450ed7b2aca05ccd202b0873fc9909d365..96c1a8f76160f0cf1153e27ebfea949e21aca802 100644 (file)
--- a/nqptp.c
+++ b/nqptp.c
@@ -65,12 +65,6 @@ sockets_open_bundle sockets_open_stuff;
 
 int master_clock_index = -1;
 
-uint64_t timing_peer_list_creation_time;
-int timing_peer_list_followup_seen; // set to true when a followup has come into one of the timing
-                                    // peers
-int timing_peer_list_announcement_sent; // set to true when an announce message has been sent to all
-                                        // relevant timing peers
-
 typedef struct {
   uint64_t trigger_time;
   uint64_t (*task)(uint64_t nominal_call_time, void *private_data);
@@ -266,8 +260,6 @@ int main(int argc, char **argv) {
     die("mutex initialization failed - %s.", strerror(errno));
   }
 
-
-
   // start the timed tasks
   uint64_t broadcasting_task(uint64_t call_time, void *private_data);
 
@@ -346,8 +338,8 @@ int main(int argc, char **argv) {
               // check if it's a control port message before checking for the length of the
               // message.
             } else if (receiver_port == NQPTP_CONTROL_PORT) {
-              handle_control_port_messages(
-                  buf, recv_len, (clock_source_private_data *)&clocks_private, reception_time);
+              handle_control_port_messages(buf, recv_len,
+                                           (clock_source_private_data *)&clocks_private);
             } else if (recv_len >= (ssize_t)sizeof(struct ptp_common_message_header)) {
               debug_print_buffer(2, buf, recv_len);
 
@@ -428,141 +420,117 @@ int main(int argc, char **argv) {
   return 0;
 }
 
-uint64_t timing_peer_list_creation_time = 0;
-int followup_seen = 0;
-
 uint64_t broadcasting_task(uint64_t call_time, __attribute__((unused)) void *private_data) {
-  int64_t time_since_timing_list_creation = call_time - timing_peer_list_creation_time;
-  if ((time_since_timing_list_creation > 250000000) && (timing_peer_list_followup_seen == 0)) {
-    clock_source_private_data *clocks_private = (clock_source_private_data *)private_data;
-    int i;
-    uint32_t acceptance_mask =
-        (1 << clock_is_a_timing_peer) | (1 << clock_is_qualified);
-    for (i = 0; i < MAX_CLOCKS; i++) {
-      if (((clocks_private[i].flags & acceptance_mask) != 0) &&
-          (clocks_private[i].announcements_sent == 0) && (clocks_private[i].is_one_of_ours == 0)) {
-
-        // create a message to attempt to waken this silent PTP clock by
-        // getting it to negotiate with an apparently better clock
-        // that then disappears
-
-        struct ptp_announce_message *msg;
-        size_t msg_length = sizeof(struct ptp_announce_message);
-        msg = malloc(msg_length);
-        memset((void *)msg, 0, msg_length);
-
-        uint64_t my_clock_id = get_self_clock_id();
-        msg->header.transportSpecificAndMessageID = 0x10 + Announce;
-        msg->header.reservedAndVersionPTP = 0x02;
-        msg->header.messageLength = htons(sizeof(struct ptp_announce_message));
-        msg->header.flags = htons(0x0408);
-        hcton64(my_clock_id, &msg->header.clockIdentity[0]);
-        msg->header.sourcePortID = htons(32776);
-        msg->header.controlOtherMessage = 0x05;
-        msg->header.logMessagePeriod = 0xFE;
-        msg->announce.currentUtcOffset = htons(37);
-        hcton64(my_clock_id, &msg->announce.grandmasterIdentity[0]);
-        uint32_t my_clock_quality = 0xf8fe436a;
-        msg->announce.grandmasterClockQuality = htonl(my_clock_quality);
+  clock_source_private_data *clocks_private = (clock_source_private_data *)private_data;
+  int i;
+  for (i = 0; i < MAX_CLOCKS; i++) {
+    if ((clocks_private[i].announcements_without_followups == 3) &&
+        (clocks_private[i].is_one_of_ours == 0)) {
+      debug(1, "Found a silent clock %" PRIx64 " at %s.", clocks_private[i].clock_id,
+            clocks_private[i].ip);
+      // send an Announce message to attempt to waken this silent PTP clock by
+      // getting it to negotiate with an apparently better clock
+      // that then immediately sends another Announce message indicating that it's worse
+
+      struct ptp_announce_message *msg;
+      size_t msg_length = sizeof(struct ptp_announce_message);
+      msg = malloc(msg_length);
+      memset((void *)msg, 0, msg_length);
+
+      uint64_t my_clock_id = get_self_clock_id();
+      msg->header.transportSpecificAndMessageID = 0x10 + Announce;
+      msg->header.reservedAndVersionPTP = 0x02;
+      msg->header.messageLength = htons(sizeof(struct ptp_announce_message));
+      msg->header.flags = htons(0x0408);
+      hcton64(my_clock_id, &msg->header.clockIdentity[0]);
+      msg->header.sourcePortID = htons(32776);
+      msg->header.controlOtherMessage = 0x05;
+      msg->header.logMessagePeriod = 0xFE;
+      msg->announce.currentUtcOffset = htons(37);
+      hcton64(my_clock_id, &msg->announce.grandmasterIdentity[0]);
+      uint32_t my_clock_quality = 0xf8fe436a;
+      msg->announce.grandmasterClockQuality = htonl(my_clock_quality);
+      if (clocks_private[i].grandmasterPriority1 > 2) {
+        msg->announce.grandmasterPriority1 =
+            clocks_private[i].grandmasterPriority1 -
+            1; // make this announcement seem better than the clock we are about to ping
+        msg->announce.grandmasterPriority2 = clocks_private[i].grandmasterPriority2;
+      } else {
+        warn("Cannot select a suitable priority for pinging clock %" PRIx64 " at %s.",
+             clocks_private[i].clock_id, clocks_private[i].ip);
         msg->announce.grandmasterPriority1 = 248;
         msg->announce.grandmasterPriority2 = 248;
-        msg->announce.timeSource = 160;
-
-        /*
-        // Organizational Unique Identifier for Apple is 00:0D:93;
-        uint8_t appl_oui[] = {0x00,0x0D,0x93};
-
-        uint8_t tlv1SubType[] = {00,00,01};
-        uint8_t tlv1Data[] =
-        {0x00,0x07,0x03,0x03,0x00,0x00,0x27,0x10,0x00,0x00,0x27,0x10,0x00,0x1B,0x1F,0xD0};
-        // uint8_t tlv1Data[] =
-        {0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-
-        uint8_t tlv2SubType[] = {00,00,05};
-        uint8_t tlv2Data[] =
-        {0x00,0x1F,0x03,0x03,0x00,0x00,0x00,0x00,0x27,0x10,0x00,0x00,0x27,0x10,0x00,0x00,0x27,0x10,0x00,0x00,0x27,0x10,0x00,0x1B,0x1F,0xD0};
-        // uint8_t tlv2Data[] =
-        {0x00,0x00,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
-
-        struct ptp_signaling_message *msg;
-        size_t msg_length = sizeof(struct ptp_signaling_message)+ 2 * sizeof(struct ptp_tlv) +
-        sizeof(tlv1Data) + sizeof(tlv2Data); msg = malloc(msg_length); memset((void *)msg, 0,
-        msg_length); uint64_t my_clock_id = get_self_clock_id();
-        msg->header.transportSpecificAndMessageID = 0x10 + Signaling;
-        msg->header.reservedAndVersionPTP = 0x02;
-        msg->header.messageLength = htons(msg_length);
-        msg->header.flags = htons(0x0408);
-        hcton64(my_clock_id, &msg->header.clockIdentity[0]);
-        msg->header.sourcePortID = htons(32776);
-        msg->header.controlOtherMessage = 0x05;
-        msg->header.logMessagePeriod = 0x80;
-        struct ptp_tlv *tlvp = (void *)msg + sizeof(struct ptp_signaling_message);
-        tlvp->tlvType = htons(ORGANIZATION_EXTENSION);
-        tlvp->lengthField = htons(sizeof(tlv1Data) + 6);
-        memcpy(&tlvp->organizationId, &appl_oui, sizeof(appl_oui));
-        memcpy(&tlvp->organizationSubType, &tlv1SubType, sizeof(tlv1SubType));
-        memcpy(&tlvp->dataField, &tlv1Data, sizeof(tlv1Data));
-
-        tlvp = (void *)msg + sizeof(struct ptp_signaling_message) + sizeof(struct ptp_tlv) +
-        sizeof(tlv1Data); tlvp->tlvType = htons(ORGANIZATION_EXTENSION); tlvp->lengthField =
-        htons(sizeof(tlv2Data) + 6); memcpy(&tlvp->organizationId, &appl_oui, sizeof(appl_oui));
-        memcpy(&tlvp->organizationSubType, &tlv2SubType, sizeof(tlv2SubType));
-        memcpy(&tlvp->dataField, &tlv2Data, sizeof(tlv2Data));
-        */
-
-        // get the socket for the correct port -- 320 -- and family -- IPv4 or IPv6 -- to send it
-        // from.
-
-        int s = 0;
-        unsigned t;
-        for (t = 0; t < sockets_open_stuff.sockets_open; t++) {
-          if ((sockets_open_stuff.sockets[t].port == 320) &&
-              (sockets_open_stuff.sockets[t].family == clocks_private[i].family))
-            s = sockets_open_stuff.sockets[t].number;
-        }
-        if (s == 0) {
-          debug(1, "sending socket not found for clock %" PRIx64 " at %s, family %s.",
-                clocks_private[i].clock_id, clocks_private[i].ip,
-                clocks_private[i].family == AF_INET
-                    ? "IPv4"
-                    : clocks_private[i].family == AF_INET6 ? "IPv6" : "Unknown");
+      }
+      msg->announce.timeSource = 160; // Internal Oscillator
+
+      // get the socket for the correct port -- 320 -- and family -- IPv4 or IPv6 -- to send it
+      // from.
+
+      int s = 0;
+      unsigned t;
+      for (t = 0; t < sockets_open_stuff.sockets_open; t++) {
+        if ((sockets_open_stuff.sockets[t].port == 320) &&
+            (sockets_open_stuff.sockets[t].family == clocks_private[i].family))
+          s = sockets_open_stuff.sockets[t].number;
+      }
+      if (s == 0) {
+        debug(1, "sending socket not found for clock %" PRIx64 " at %s, family %s.",
+              clocks_private[i].clock_id, clocks_private[i].ip,
+              clocks_private[i].family == AF_INET
+                  ? "IPv4"
+                  : clocks_private[i].family == AF_INET6 ? "IPv6" : "Unknown");
+      } else {
+        // debug(1, "Send message from socket %d.", s);
+
+        const char *portname = "320";
+        struct addrinfo hints;
+        memset(&hints, 0, sizeof(hints));
+        hints.ai_family = AF_UNSPEC;
+        hints.ai_socktype = SOCK_DGRAM;
+        hints.ai_protocol = 0;
+        hints.ai_flags = AI_ADDRCONFIG;
+        struct addrinfo *res = NULL;
+        int err = getaddrinfo(clocks_private[i].ip, portname, &hints, &res);
+        if (err != 0) {
+          debug(1, "failed to resolve remote socket address (err=%d)", err);
         } else {
-          // debug(1, "Send message from socket %d.", s);
-
-          const char *portname = "320";
-          struct addrinfo hints;
-          memset(&hints, 0, sizeof(hints));
-          hints.ai_family = AF_UNSPEC;
-          hints.ai_socktype = SOCK_DGRAM;
-          hints.ai_protocol = 0;
-          hints.ai_flags = AI_ADDRCONFIG;
-          struct addrinfo *res = NULL;
-          int err = getaddrinfo(clocks_private[i].ip, portname, &hints, &res);
-          if (err != 0) {
-            debug(1, "failed to resolve remote socket address (err=%d)", err);
-          } else {
-            // here, we have the destination, so send it
+          // here, we have the destination, so send it
 
-            // if (clocks_private[i].family == AF_INET6) {
+          // if (clocks_private[i].family == AF_INET6) {
+          if (clocks_private[i].announcements_sent != 0) {
+            debug(1, "Warning: sending another Announce message to %" PRIx64 "at %s...",
+                  clocks_private[i].clock_id, clocks_private[i].ip);
+          } else {
             debug(1, "Sending an Announce message to %" PRIx64 "at %s...",
                   clocks_private[i].clock_id, clocks_private[i].ip);
-            // debug_print_buffer(1, (char *)msg, msg_length);
-            int ret = sendto(s, msg, msg_length, 0, res->ai_addr, res->ai_addrlen);
-            if (ret == -1)
-              debug(1, "result of sendto is %d.", ret);
-            clocks_private[i].announcements_sent++;
-            debug(2, "message clock \"%" PRIx64 "\" at %s on %s.", clocks_private[i].clock_id,
-                  clocks_private[i].ip, clocks_private[i].family == AF_INET6 ? "IPv6" : "IPv4");
+          }
+          // debug_print_buffer(1, (char *)msg, msg_length);
+          int ret = sendto(s, msg, msg_length, 0, res->ai_addr, res->ai_addrlen);
+          if (ret == -1)
+            debug(1, "result of sendto is %d.", ret);
+          clocks_private[i].announcements_sent++;
+          debug(2, "message clock \"%" PRIx64 "\" at %s on %s.", clocks_private[i].clock_id,
+                clocks_private[i].ip, clocks_private[i].family == AF_INET6 ? "IPv6" : "IPv4");
+
+          if (clocks_private[i].grandmasterPriority1 < 254) {
+            msg->announce.grandmasterPriority1 =
+                clocks_private[i].grandmasterPriority1 +
+                1; // make this announcement seem worse than the clock we about to ping
+          } else {
+            warn("Cannot select a suitable priority for second ping of clock %" PRIx64 " at %s.",
+                 clocks_private[i].clock_id, clocks_private[i].ip);
             msg->announce.grandmasterPriority1 = 250;
-            ret = sendto(s, msg, msg_length, 0, res->ai_addr, res->ai_addrlen);
-            if (ret == -1)
-              debug(1, "result of second sendto is %d.", ret);
-            // }
-            freeaddrinfo(res);
           }
+
+          msg->announce.grandmasterPriority1 = 250;
+          ret = sendto(s, msg, msg_length, 0, res->ai_addr, res->ai_addrlen);
+          if (ret == -1)
+            debug(1, "result of second sendto is %d.", ret);
+          // }
+          freeaddrinfo(res);
         }
-        free(msg);
       }
+      free(msg);
     }
   }
 
diff --git a/nqptp.h b/nqptp.h
index 9d488cf75c6d3cb91a5d89dc059d324bec02653d..327aba921add22eaf95cbdaf35125fda5fa93a8a 100644 (file)
--- a/nqptp.h
+++ b/nqptp.h
 
 extern int master_clock_index;
 extern struct shm_structure *shared_memory;
-extern uint64_t timing_peer_list_creation_time;
-extern int timing_peer_list_announcement_sent; // set to true when an announce message has been sent
-                                               // to all relevant timing peers
-extern int timing_peer_list_followup_seen; // set to true when a followup has come into one of the
-                                           // timing peers
 
 void update_master_clock_info(uint64_t master_clock_id, const char *ip, uint64_t local_time,
                               uint64_t local_to_master_offset, uint64_t mastership_start_time);