From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Wed, 7 Apr 2021 11:46:07 +0000 (+0100) Subject: switch to using flag bits X-Git-Tag: 1.1-dev~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d11e4f59aa23165870a324f571f6a0b8327ea7f2;p=thirdparty%2Fnqptp.git switch to using flag bits --- diff --git a/nqptp-clock-sources.c b/nqptp-clock-sources.c index bcc1d9b..d5bee8a 100644 --- a/nqptp-clock-sources.c +++ b/nqptp-clock-sources.c @@ -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 diff --git a/nqptp-clock-sources.h b/nqptp-clock-sources.h index 62d2b0b..d53d07b 100644 --- a/nqptp-clock-sources.h +++ b/nqptp-clock-sources.h @@ -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, diff --git a/nqptp-message-handlers.c b/nqptp-message-handlers.c index 48f0c3e..b8287dc 100644 --- a/nqptp-message-handlers.c +++ b/nqptp-message-handlers.c @@ -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!"); } diff --git a/nqptp-shm-structures.h b/nqptp-shm-structures.h index 99cd4a7..16226d1 100644 --- a/nqptp-shm-structures.h +++ b/nqptp-shm-structures.h @@ -34,15 +34,17 @@ #include #include +// 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 74931d9..4d1c44f 100644 --- 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);